reuse hashes of dotfiles/dirs/submodules when entering view

This fixes a crash when a git submodule has a name starting with a dot.
Such a submodule might contain dotfiles that are intended to be used when
inside the view (since a dot-directory that's not a submodule was already
preserved when entering a view). So, rather than eliminating the submodule
from the view, its git ls-files --stage hash is copied over into the view.

dotfiles/dirs have their git ls-files --stage hashes similarly copied over
to the view. This is more efficient and simpler than the old method,
and also won't break if git ever adds a new type of tree item, like was
done with submodules.

Since the content of dotfiles in the working tree is no longer hashed
when entering a view, when there are unstaged modifications, they are
not included in the view branch. Entering the view branch still works,
but git checkout shows "M .dotfile", and git diff will show the unstaged
changes. This seems like an improvement over the old behavior.

Also made Command.View not delete empty directories that are submodules
when entering a view, while still deleting other empty directories.

This commit was supported by the NSF-funded DataLad project.
This commit is contained in:
Joey Hess 2018-05-14 14:58:13 -04:00
parent 0b7f6d24d3
commit d7021d420f
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 48 additions and 29 deletions

View file

@ -19,7 +19,6 @@ import qualified Git.LsFiles
import qualified Git.Ref import qualified Git.Ref
import Git.UpdateIndex import Git.UpdateIndex
import Git.Sha import Git.Sha
import Annex.HashObject
import Git.Types import Git.Types
import Git.FilePath import Git.FilePath
import Annex.WorkTree import Annex.WorkTree
@ -29,7 +28,6 @@ import Annex.CatFile
import Logs.MetaData import Logs.MetaData
import Logs.View import Logs.View
import Utility.Glob import Utility.Glob
import Utility.FileMode
import Types.Command import Types.Command
import CmdLine.Action import CmdLine.Action
@ -327,8 +325,9 @@ applyView = applyView' viewedFileFromReference getWorkTreeMetaData
narrowView :: View -> Annex Git.Branch narrowView :: View -> Annex Git.Branch
narrowView = applyView' viewedFileReuse getViewedFileMetaData narrowView = applyView' viewedFileReuse getViewedFileMetaData
{- Go through each file in the currently checked out branch. {- Go through each staged file.
- If the file is not annexed, skip it, unless it's a dotfile in the top. - If the file is not annexed, skip it, unless it's a dotfile in the top,
- or a file in a dotdir in the top.
- Look up the metadata of annexed files, and generate any ViewedFiles, - Look up the metadata of annexed files, and generate any ViewedFiles,
- and stage them. - and stage them.
- -
@ -337,39 +336,32 @@ narrowView = applyView' viewedFileReuse getViewedFileMetaData
applyView' :: MkViewedFile -> (FilePath -> MetaData) -> View -> Annex Git.Branch applyView' :: MkViewedFile -> (FilePath -> MetaData) -> View -> Annex Git.Branch
applyView' mkviewedfile getfilemetadata view = do applyView' mkviewedfile getfilemetadata view = do
top <- fromRepo Git.repoPath top <- fromRepo Git.repoPath
(l, clean) <- inRepo $ Git.LsFiles.inRepo [top] (l, clean) <- inRepo $ Git.LsFiles.stagedDetails [top]
liftIO . nukeFile =<< fromRepo gitAnnexViewIndex liftIO . nukeFile =<< fromRepo gitAnnexViewIndex
uh <- withViewIndex $ inRepo Git.UpdateIndex.startUpdateIndex uh <- withViewIndex $ inRepo Git.UpdateIndex.startUpdateIndex
forM_ l $ \f -> do forM_ l $ \(f, sha, mode) -> do
topf <- inRepo (toTopFilePath f) topf <- inRepo (toTopFilePath f)
go uh topf =<< lookupFile f go uh topf sha (toTreeItemType =<< mode) =<< lookupFile f
liftIO $ do liftIO $ do
void $ stopUpdateIndex uh void $ stopUpdateIndex uh
void clean void clean
genViewBranch view genViewBranch view
where where
genviewedfiles = viewedFiles view mkviewedfile -- enables memoization genviewedfiles = viewedFiles view mkviewedfile -- enables memoization
go uh topf (Just k) = do
go uh topf _sha _mode (Just k) = do
metadata <- getCurrentMetaData k metadata <- getCurrentMetaData k
let f = getTopFilePath topf let f = getTopFilePath topf
let metadata' = getfilemetadata f `unionMetaData` metadata let metadata' = getfilemetadata f `unionMetaData` metadata
forM_ (genviewedfiles f metadata') $ \fv -> do forM_ (genviewedfiles f metadata') $ \fv -> do
f' <- fromRepo $ fromTopFilePath $ asTopFilePath fv f' <- fromRepo $ fromTopFilePath $ asTopFilePath fv
stagesymlink uh f' =<< calcRepo (gitAnnexLink f' k) stagesymlink uh f' =<< calcRepo (gitAnnexLink f' k)
go uh topf Nothing go uh topf (Just sha) (Just treeitemtype) Nothing
| "." `isPrefixOf` getTopFilePath topf = do | "." `isPrefixOf` getTopFilePath topf =
f <- fromRepo $ fromTopFilePath topf liftIO $ Git.UpdateIndex.streamUpdateIndex' uh $
s <- liftIO $ getSymbolicLinkStatus f pureStreamer $ updateIndexLine sha treeitemtype topf
if isSymbolicLink s go _ _ _ _ _ = noop
then stagesymlink uh f =<< liftIO (readSymbolicLink f)
else do
sha <- hashFile f
let blobtype = if isExecutable (fileMode s)
then ExecutableBlob
else FileBlob
liftIO . Git.UpdateIndex.streamUpdateIndex' uh
=<< inRepo (Git.UpdateIndex.stageFile sha blobtype f)
| otherwise = noop
stagesymlink uh f linktarget = do stagesymlink uh f linktarget = do
sha <- hashSymlink linktarget sha <- hashSymlink linktarget
liftIO . Git.UpdateIndex.streamUpdateIndex' uh liftIO . Git.UpdateIndex.streamUpdateIndex' uh

View file

@ -1,3 +1,11 @@
git-annex (6.20180510) UNRELEASED; urgency=medium
* view, vadd: Fix crash when a git submodule has a name starting with a dot.
* Improve handling of unstaged modifications to dotfiles when entering a
view.
-- Joey Hess <id@joeyh.name> Mon, 14 May 2018 13:42:41 -0400
git-annex (6.20180509) upstream; urgency=medium git-annex (6.20180509) upstream; urgency=medium
* The old git-annex Android app is now deprecated in favor of running * The old git-annex Android app is now deprecated in favor of running

View file

@ -12,6 +12,8 @@ import qualified Git
import qualified Git.Command import qualified Git.Command
import qualified Git.Ref import qualified Git.Ref
import qualified Git.Branch import qualified Git.Branch
import qualified Git.LsFiles as LsFiles
import Git.FilePath
import Types.View import Types.View
import Annex.View import Annex.View
import Logs.View import Logs.View
@ -65,15 +67,21 @@ checkoutViewBranch view mkbranch = do
when ok $ do when ok $ do
setView view setView view
{- A git repo can easily have empty directories in it, {- A git repo can easily have empty directories in it,
- and this pollutes the view, so remove them. -} - and this pollutes the view, so remove them.
top <- fromRepo Git.repoPath - (However, emptry directories used by submodules are not
liftIO $ removeemptydirs top - removed.) -}
top <- liftIO . absPath =<< fromRepo Git.repoPath
(l, cleanup) <- inRepo $
LsFiles.notInRepoIncludingEmptyDirectories False [top]
forM_ l (removeemptydir top)
liftIO $ void cleanup
unlessM (liftIO $ doesDirectoryExist here) $ do unlessM (liftIO $ doesDirectoryExist here) $ do
showLongNote (cwdmissing top) showLongNote (cwdmissing top)
return ok return ok
where where
removeemptydirs top = mapM_ (tryIO . removeDirectory) removeemptydir top d = do
=<< dirTreeRecursiveSkipping (".git" `isSuffixOf`) top p <- inRepo $ toTopFilePath d
liftIO $ tryIO $ removeDirectory (top </> getTopFilePath p)
cwdmissing top = unlines cwdmissing top = unlines
[ "This view does not include the subdirectory you are currently in." [ "This view does not include the subdirectory you are currently in."
, "Perhaps you should: cd " ++ top , "Perhaps you should: cd " ++ top

View file

@ -1,6 +1,6 @@
{- git ls-files interface {- git ls-files interface
- -
- Copyright 2010,2012 Joey Hess <id@joeyh.name> - Copyright 2010-2018 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -8,6 +8,7 @@
module Git.LsFiles ( module Git.LsFiles (
inRepo, inRepo,
notInRepo, notInRepo,
notInRepoIncludingEmptyDirectories,
allFiles, allFiles,
deleted, deleted,
modified, modified,
@ -44,10 +45,14 @@ inRepo l = pipeNullSplit $
{- Scans for files at the specified locations that are not checked into git. -} {- Scans for files at the specified locations that are not checked into git. -}
notInRepo :: Bool -> [FilePath] -> Repo -> IO ([FilePath], IO Bool) notInRepo :: Bool -> [FilePath] -> Repo -> IO ([FilePath], IO Bool)
notInRepo include_ignored l repo = pipeNullSplit params repo notInRepo = notInRepo' []
notInRepo' :: [CommandParam] -> Bool -> [FilePath] -> Repo -> IO ([FilePath], IO Bool)
notInRepo' ps include_ignored l repo = pipeNullSplit params repo
where where
params = concat params = concat
[ [ Param "ls-files", Param "--others"] [ [ Param "ls-files", Param "--others"]
, ps
, exclude , exclude
, [ Param "-z", Param "--" ] , [ Param "-z", Param "--" ]
, map File l , map File l
@ -56,6 +61,11 @@ notInRepo include_ignored l repo = pipeNullSplit params repo
| include_ignored = [] | include_ignored = []
| otherwise = [Param "--exclude-standard"] | otherwise = [Param "--exclude-standard"]
{- Scans for files at the specified locations that are not checked into
- git. Empty directories are included in the result. -}
notInRepoIncludingEmptyDirectories :: Bool -> [FilePath] -> Repo -> IO ([FilePath], IO Bool)
notInRepoIncludingEmptyDirectories = notInRepo' [Param "--directory"]
{- Finds all files in the specified locations, whether checked into git or {- Finds all files in the specified locations, whether checked into git or
- not. -} - not. -}
allFiles :: [FilePath] -> Repo -> IO ([FilePath], IO Bool) allFiles :: [FilePath] -> Repo -> IO ([FilePath], IO Bool)

View file

@ -32,3 +32,4 @@ copy of the repository will be provided via email
> [[fixed|done]] --[[Joey]]