git-annex/Command/Lock.hs

53 lines
1.3 KiB
Haskell
Raw Normal View History

2010-11-09 19:59:49 +00:00
{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Lock where
import Control.Monad.State (liftIO)
import System.Directory
import System.Posix.Files
2010-11-10 17:01:01 +00:00
import Types
2010-11-09 19:59:49 +00:00
import Command
import Messages
import qualified Annex
import qualified GitRepo as Git
{- Undo unlock -}
start :: SubCmdStartString
start file = do
2010-11-10 17:01:01 +00:00
locked <- isLocked file
if locked
2010-11-09 19:59:49 +00:00
then return Nothing
else do
showStart "lock" file
return $ Just $ perform file
perform :: FilePath -> SubCmdPerform
perform file = do
liftIO $ removeFile file
g <- Annex.gitRepo
2010-11-10 17:01:01 +00:00
-- first reset the file to drop any changes checked into the index
liftIO $ Git.run g ["reset", "-q", "--", file]
-- checkout the symlink
liftIO $ Git.run g ["checkout", "--", file]
2010-11-09 19:59:49 +00:00
return $ Just $ return True -- no cleanup needed
2010-11-10 17:01:01 +00:00
{- Checks if a file is unlocked for edit. -}
2010-11-10 17:01:01 +00:00
isLocked :: FilePath -> Annex Bool
isLocked file = do
-- check if it's a symlink first, as that's cheapest
2010-11-10 17:01:01 +00:00
s <- liftIO $ getSymbolicLinkStatus file
if (isSymbolicLink s)
then return True -- Symlinked files are always locked.
else do
-- Not a symlink, so see if the type has changed,
-- if so it is presumed to have been unlocked.
g <- Annex.gitRepo
typechanged <- liftIO $ Git.typeChangedFiles g file
return $ not $ elem file typechanged