unlock: Don't allow unlocking files that have never been committed to git before

Avoids an intractable problem that prevents the pre-commit hook from
telling if such a file is intended to be an annexed file or not.
This commit is contained in:
Joey Hess 2015-01-02 13:48:56 -04:00
parent 7294439e1f
commit fd55563b02
3 changed files with 57 additions and 18 deletions

View file

@ -10,6 +10,7 @@ module Command.Unlock where
import Common.Annex import Common.Annex
import Command import Command
import Annex.Content import Annex.Content
import Annex.CatFile
import Utility.CopyFile import Utility.CopyFile
cmd :: [Command] cmd :: [Command]
@ -29,10 +30,10 @@ start :: FilePath -> Key -> CommandStart
start file key = do start file key = do
showStart "unlock" file showStart "unlock" file
ifM (inAnnex key) ifM (inAnnex key)
( ifM (checkDiskSpace Nothing key 0) ( ifM (isJust <$> catKeyFileHEAD file)
( next $ perform file key ( next $ perform file key
, do , do
warning "not enough disk space to copy file" warning "this has not yet been committed to git; cannot unlock it"
next $ next $ return False next $ next $ return False
) )
, do , do
@ -41,19 +42,24 @@ start file key = do
) )
perform :: FilePath -> Key -> CommandPerform perform :: FilePath -> Key -> CommandPerform
perform dest key = do perform dest key = ifM (checkDiskSpace Nothing key 0)
src <- calcRepo $ gitAnnexLocation key ( do
tmpdest <- fromRepo $ gitAnnexTmpObjectLocation key src <- calcRepo $ gitAnnexLocation key
liftIO $ createDirectoryIfMissing True (parentDir tmpdest) tmpdest <- fromRepo $ gitAnnexTmpObjectLocation key
showAction "copying" liftIO $ createDirectoryIfMissing True (parentDir tmpdest)
ifM (liftIO $ copyFileExternal CopyAllMetaData src tmpdest) showAction "copying"
( do ifM (liftIO $ copyFileExternal CopyAllMetaData src tmpdest)
liftIO $ do ( do
removeFile dest liftIO $ do
moveFile tmpdest dest removeFile dest
thawContent dest moveFile tmpdest dest
next $ return True thawContent dest
, do next $ return True
warning "copy failed!" , do
next $ return False warning "copy failed!"
) next $ return False
)
, do
warning "not enough disk space to copy file"
next $ return False
)

8
debian/changelog vendored
View file

@ -1,3 +1,11 @@
git-annex (5.20141232) UNRELEASED; urgency=medium
* unlock: Don't allow unlocking files that have never been committed to git
before, to avoid an intractable problem that prevents the pre-commit
hook from telling if such a file is intended to be an annexed file or not.
-- Joey Hess <id@joeyh.name> Fri, 02 Jan 2015 13:35:13 -0400
git-annex (5.20141231) unstable; urgency=medium git-annex (5.20141231) unstable; urgency=medium
* vicfg: Avoid crashing on badly encoded config data. * vicfg: Avoid crashing on badly encoded config data.

View file

@ -2,8 +2,16 @@
If you `git annex edit FILE`, an already-committed file, then do `git annex sync`, FILE will be added to .git/objects, but can be cleaned up with `git prune`. An annoyance, but not a huge problem. If you `git annex edit FILE`, an already-committed file, then do `git annex sync`, FILE will be added to .git/objects, but can be cleaned up with `git prune`. An annoyance, but not a huge problem.
> This is why you're recommended to use `git add` after you're done
> changing unlocked files. This avoids the git commit staging the file
> in the index, even though the pre-commit hook fixes that up. It does
> indeed leave the file contents in .git/objects, although with no refs
> pointing to it, `git prune` will delete it sooner or later. --[[Joey]]
If, on the other hand, you do git annex add, then edit, then sync, it will actually be committed to the git repository. Fixing that is a lot less trivial than `git prune`. If, on the other hand, you do git annex add, then edit, then sync, it will actually be committed to the git repository. Fixing that is a lot less trivial than `git prune`.
> This is indeed a problem..
### What steps will reproduce the problem? ### What steps will reproduce the problem?
anthony@Watt:/tmp/test$ du -sh .git/objects/ anthony@Watt:/tmp/test$ du -sh .git/objects/
@ -29,3 +37,20 @@ If, on the other hand, you do git annex add, then edit, then sync, it will actua
### What version of git-annex are you using? On what operating system? ### What version of git-annex are you using? On what operating system?
5.20141125 on Debian testing/unstable 5.20141125 on Debian testing/unstable
> Analysis: This only happens when a file is added and then unlocked
> without first being committed.
>
> In this situation, the file has a typechange between the index and
> the working tree. However, by the time the pre-commit hook gets
> run, git commit has already staged the unlocked file content into the
> index. So, there is nolonger a typechange between the index and the work
> tree. And, since the file is new, there is no typechange between the
> index and last commit either. The latter is what the pre-commit hook
> looks for.
>
> Conclusion: The pre-commit hook cannot possibly detect this situation.
> Instead, it seems the only way to block the problem is to prevent
> unlocking a file that does not have any git history.
>
> And I've [[done]] that now. --[[Joey]]