Support unannex and uninit in direct mode.

In direct mode, it's best to whenever possible not move direct mode files
out of the way, and so I made unannex avoid touching the direct mode file at
all.

That actually turns out to be easy, because in direct mode, unlike indirect
mode, the pre-commit hook won't get confused if the unannexed file later
gets added back by git add. So there's no need to commit the unannex right
away; it can be staged for the user to commit later. This also means that
unannex in direct mode is a lot faster than in indirect mode!

Another subtle bit is the bookkeeping that is done when unannexing a direct
mode file. The inode cache needs to be removed so that when uninit runs
getKeysPresent, it doesn't see the cache and think the key is still
present and crash when it's not.

This commit is sponsored by Douglas Butts. Thanks!
This commit is contained in:
Joey Hess 2013-07-22 17:06:00 -04:00
parent 66e3d57149
commit f353f13c9d
5 changed files with 60 additions and 30 deletions

View file

@ -1,6 +1,6 @@
{- git-annex command {- git-annex command
- -
- Copyright 2010 Joey Hess <joey@kitenet.net> - Copyright 2010-2013 Joey Hess <joey@kitenet.net>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -11,15 +11,16 @@ module Command.Unannex where
import Common.Annex import Common.Annex
import Command import Command
import Config
import qualified Annex import qualified Annex
import Logs.Location import Logs.Location
import Annex.Content import Annex.Content
import Annex.Content.Direct
import qualified Git.Command import qualified Git.Command
import qualified Git.LsFiles as LsFiles import qualified Git.LsFiles as LsFiles
def :: [Command] def :: [Command]
def = [notDirect $ def = [command "unannex" paramPaths seek SectionUtility
command "unannex" paramPaths seek SectionUtility
"undo accidential add command"] "undo accidential add command"]
seek :: [CommandSeek] seek :: [CommandSeek]
@ -28,36 +29,41 @@ seek = [withFilesInGit $ whenAnnexed start]
start :: FilePath -> (Key, Backend) -> CommandStart start :: FilePath -> (Key, Backend) -> CommandStart
start file (key, _) = stopUnless (inAnnex key) $ do start file (key, _) = stopUnless (inAnnex key) $ do
showStart "unannex" file showStart "unannex" file
next $ perform file key next $ ifM isDirect
( performDirect file key
, performIndirect file key)
perform :: FilePath -> Key -> CommandPerform performIndirect :: FilePath -> Key -> CommandPerform
perform file key = next $ cleanup file key performIndirect file key = do
cleanup :: FilePath -> Key -> CommandCleanup
cleanup file key = do
liftIO $ removeFile file liftIO $ removeFile file
-- git rm deletes empty directory without --cached -- git rm deletes empty directory without --cached
inRepo $ Git.Command.run [Params "rm --cached --quiet --", File file] inRepo $ Git.Command.run [Params "rm --cached --force --quiet --", File file]
-- If the file was already committed, it is now staged for removal. -- If the file was already committed, it is now staged for removal.
-- Commit that removal now, to avoid later confusing the -- Commit that removal now, to avoid later confusing the
-- pre-commit hook if this file is later added back to -- pre-commit hook, if this file is later added back to
-- git as a normal, non-annexed file. -- git as a normal non-annexed file, to thinking that the
(s, clean) <- inRepo $ LsFiles.staged [file] -- file has been unlocked and needs to be re-annexed.
when (not $ null s) $ do (s, reap) <- inRepo $ LsFiles.staged [file]
when (not $ null s) $
inRepo $ Git.Command.run inRepo $ Git.Command.run
[ Param "commit" [ Param "commit"
, Param "-q" , Param "-q"
, Param "--no-verify"
, Param "-m", Param "content removed from git annex" , Param "-m", Param "content removed from git annex"
, Param "--", File file , Param "--", File file
] ]
void $ liftIO clean void $ liftIO reap
next $ cleanupIndirect file key
cleanupIndirect :: FilePath -> Key -> CommandCleanup
cleanupIndirect file key = do
ifM (Annex.getState Annex.fast) ifM (Annex.getState Annex.fast)
( goFast ( goFast
, go , go
) )
return True return True
where where
#ifdef __WINDOWS__ #ifdef __WINDOWS__
@ -75,3 +81,22 @@ cleanup file key = do
go = do go = do
fromAnnex key file fromAnnex key file
logStatus key InfoMissing logStatus key InfoMissing
performDirect :: FilePath -> Key -> CommandPerform
performDirect file key = do
-- --force is needed when the file is not committed
inRepo $ Git.Command.run [Params "rm --cached --force --quiet --", File file]
next $ cleanupDirect file key
{- The direct mode file is not touched during unannex, so the content
- is already where it needs to be, so this does not need to do anything
- except remove it from the associated file map (which also updates
- the location log if this was the last copy), and, if this was the last
- associated file, remove the inode cache. -}
cleanupDirect :: FilePath -> Key -> CommandCleanup
cleanupDirect file key = do
fs <- removeAssociatedFile key file
when (null fs) $
removeInodeCache key
return True

View file

@ -18,7 +18,7 @@ import qualified Annex.Branch
import Annex.Content import Annex.Content
def :: [Command] def :: [Command]
def = [notDirect $ addCheck check $ command "uninit" paramPaths seek def = [addCheck check $ command "uninit" paramPaths seek
SectionUtility "de-initialize git-annex and clean out repository"] SectionUtility "de-initialize git-annex and clean out repository"]
check :: Annex () check :: Annex ()
@ -104,6 +104,6 @@ removeUnannexed = go []
go c ks go c ks
, go (k:c) ks , go (k:c) ks
) )
enoughlinks f = do enoughlinks f = catchBoolIO $ do
s <- getFileStatus f s <- getFileStatus f
return $ linkCount s > 1 return $ linkCount s > 1

26
Test.hs
View file

@ -243,11 +243,11 @@ test_reinject env = "git-annex reinject/fromkey" ~: TestCase $ intmpclonerepoInD
test_unannex :: TestEnv -> Test test_unannex :: TestEnv -> Test
test_unannex env = "git-annex unannex" ~: TestList [nocopy, withcopy] test_unannex env = "git-annex unannex" ~: TestList [nocopy, withcopy]
where where
nocopy = "no content" ~: intmpclonerepoInDirect env $ do nocopy = "no content" ~: intmpclonerepo env $ do
annexed_notpresent annexedfile annexed_notpresent annexedfile
git_annex env "unannex" [annexedfile] @? "unannex failed with no copy" git_annex env "unannex" [annexedfile] @? "unannex failed with no copy"
annexed_notpresent annexedfile annexed_notpresent annexedfile
withcopy = "with content" ~: intmpclonerepoInDirect env $ do withcopy = "with content" ~: intmpclonerepo env $ do
git_annex env "get" [annexedfile] @? "get failed" git_annex env "get" [annexedfile] @? "get failed"
annexed_present annexedfile annexed_present annexedfile
git_annex env "unannex" [annexedfile, sha1annexedfile] @? "unannex failed" git_annex env "unannex" [annexedfile, sha1annexedfile] @? "unannex failed"
@ -734,16 +734,18 @@ test_map env = "git-annex map" ~: intmpclonerepo env $ do
git_annex env "map" ["--fast"] @? "map failed" git_annex env "map" ["--fast"] @? "map failed"
test_uninit :: TestEnv -> Test test_uninit :: TestEnv -> Test
test_uninit env = "git-annex uninit" ~: intmpclonerepoInDirect env $ do test_uninit env = "git-annex uninit" ~: TestList [inbranch, normal]
git_annex env "get" [] @? "get failed" where
annexed_present annexedfile inbranch = "in branch" ~: intmpclonerepoInDirect env $ do
boolSystem "git" [Params "checkout git-annex"] @? "git checkout git-annex" boolSystem "git" [Params "checkout git-annex"] @? "git checkout git-annex"
not <$> git_annex env "uninit" [] @? "uninit failed to fail when git-annex branch was checked out" not <$> git_annex env "uninit" [] @? "uninit failed to fail when git-annex branch was checked out"
boolSystem "git" [Params "checkout master"] @? "git checkout master" normal = "normal" ~: intmpclonerepo env $ do
_ <- git_annex env "uninit" [] -- exit status not checked; does abnormal exit git_annex env "get" [] @? "get failed"
checkregularfile annexedfile annexed_present annexedfile
doesDirectoryExist ".git" @? ".git vanished in uninit" _ <- git_annex env "uninit" [] -- exit status not checked; does abnormal exit
not <$> doesDirectoryExist ".git/annex" @? ".git/annex still present after uninit" checkregularfile annexedfile
doesDirectoryExist ".git" @? ".git vanished in uninit"
not <$> doesDirectoryExist ".git/annex" @? ".git/annex still present after uninit"
test_upgrade :: TestEnv -> Test test_upgrade :: TestEnv -> Test
test_upgrade env = "git-annex upgrade" ~: intmpclonerepo env $ do test_upgrade env = "git-annex upgrade" ~: intmpclonerepo env $ do

1
debian/changelog vendored
View file

@ -32,6 +32,7 @@ git-annex (4.20130710) UNRELEASED; urgency=low
back to the regular host if they all fail. back to the regular host if they all fail.
* For long hostnames, use a hash of the hostname to generate the socket * For long hostnames, use a hash of the hostname to generate the socket
file for ssh connection caching. file for ssh connection caching.
* Support unannex and uninit in direct mode.
-- Joey Hess <joeyh@debian.org> Tue, 09 Jul 2013 19:17:13 -0400 -- Joey Hess <joeyh@debian.org> Tue, 09 Jul 2013 19:17:13 -0400

View file

@ -19,3 +19,5 @@ git-annex version: 4.20130601-g7483ca4
# End of transcript or log. # End of transcript or log.
"""]] """]]
> [[done]]; added support for direct mode --[[Joey]]