git-annex/Command/Unlock.hs
Joey Hess 965e106f24 made parentDir return a Maybe FilePath; removed most uses of it
parentDir is less safe than takeDirectory, especially when working
with relative FilePaths. It's really only useful in loops that
want to terminate at /

This commit was sponsored by Audric SCHILTKNECHT.
2015-01-06 18:55:56 -04:00

65 lines
1.5 KiB
Haskell

{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Unlock where
import Common.Annex
import Command
import Annex.Content
import Annex.CatFile
import Utility.CopyFile
cmd :: [Command]
cmd =
[ c "unlock" "unlock files for modification"
, c "edit" "same as unlock"
]
where
c n = notDirect . command n paramPaths seek SectionCommon
seek :: CommandSeek
seek = withFilesInGit $ whenAnnexed start
{- The unlock subcommand replaces the symlink with a copy of the file's
- content. -}
start :: FilePath -> Key -> CommandStart
start file key = do
showStart "unlock" file
ifM (inAnnex key)
( ifM (isJust <$> catKeyFileHEAD file)
( next $ perform file key
, do
warning "this has not yet been committed to git; cannot unlock it"
next $ next $ return False
)
, do
warning "content not present; cannot unlock"
next $ next $ return False
)
perform :: FilePath -> Key -> CommandPerform
perform dest key = ifM (checkDiskSpace Nothing key 0)
( do
src <- calcRepo $ gitAnnexLocation key
tmpdest <- fromRepo $ gitAnnexTmpObjectLocation key
liftIO $ createDirectoryIfMissing True (takeDirectory tmpdest)
showAction "copying"
ifM (liftIO $ copyFileExternal CopyAllMetaData src tmpdest)
( do
liftIO $ do
removeFile dest
moveFile tmpdest dest
thawContent dest
next $ return True
, do
warning "copy failed!"
next $ return False
)
, do
warning "not enough disk space to copy file"
next $ return False
)