2010-11-10 14:52:43 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2014 Joey Hess <id@joeyh.name>
|
2010-11-10 14:52:43 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-03-21 18:39:50 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2010-11-10 14:52:43 +00:00
|
|
|
module Command.PreCommit where
|
|
|
|
|
2012-03-14 21:43:34 +00:00
|
|
|
import Common.Annex
|
2010-11-10 14:52:43 +00:00
|
|
|
import Command
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
import Config
|
2010-11-10 17:01:17 +00:00
|
|
|
import qualified Command.Add
|
2010-11-11 22:54:52 +00:00
|
|
|
import qualified Command.Fix
|
2013-12-01 17:59:39 +00:00
|
|
|
import Annex.Direct
|
2014-03-02 22:01:07 +00:00
|
|
|
import Annex.Hook
|
2015-12-09 19:18:25 +00:00
|
|
|
import Annex.Link
|
2014-02-19 18:14:44 +00:00
|
|
|
import Annex.View
|
2015-12-09 19:18:25 +00:00
|
|
|
import Annex.Version
|
2014-02-22 17:35:50 +00:00
|
|
|
import Annex.View.ViewedFile
|
2014-08-20 20:45:58 +00:00
|
|
|
import Annex.LockFile
|
2014-02-19 18:14:44 +00:00
|
|
|
import Logs.View
|
|
|
|
import Logs.MetaData
|
|
|
|
import Types.View
|
|
|
|
import Types.MetaData
|
2014-11-10 19:36:24 +00:00
|
|
|
import qualified Git.Index as Git
|
|
|
|
import qualified Git.LsFiles as Git
|
2014-02-19 18:14:44 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = command "pre-commit" SectionPlumbing
|
2015-07-08 16:33:27 +00:00
|
|
|
"run by git pre-commit hook"
|
2015-07-08 19:08:02 +00:00
|
|
|
paramPaths
|
|
|
|
(withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2014-03-21 18:39:50 +00:00
|
|
|
seek ps = lockPreCommitHook $ ifM isDirect
|
2014-03-02 22:01:07 +00:00
|
|
|
( do
|
|
|
|
-- update direct mode mappings for committed files
|
|
|
|
withWords startDirect ps
|
|
|
|
runAnnexHook preCommitAnnexHook
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
, do
|
2014-11-10 19:36:24 +00:00
|
|
|
ifM (liftIO Git.haveFalseIndex)
|
|
|
|
( do
|
|
|
|
(fs, cleanup) <- inRepo $ Git.typeChangedStaged ps
|
|
|
|
whenM (anyM isUnlocked fs) $
|
|
|
|
error "Cannot make a partial commit with unlocked annexed files. You should `git annex add` the files you want to commit, and then run git commit."
|
|
|
|
void $ liftIO cleanup
|
|
|
|
, do
|
|
|
|
-- fix symlinks to files being committed
|
2015-12-09 19:18:25 +00:00
|
|
|
flip withFilesToBeCommitted ps $ \f ->
|
|
|
|
maybe stop (Command.Fix.start f)
|
|
|
|
=<< isAnnexLink f
|
2014-11-10 19:36:24 +00:00
|
|
|
-- inject unlocked files into the annex
|
2015-12-09 19:18:25 +00:00
|
|
|
-- (not needed when repo version uses
|
|
|
|
-- unlocked pointer files)
|
|
|
|
unlessM versionSupportsUnlockedPointers $
|
|
|
|
withFilesUnlockedToBeCommitted startInjectUnlocked ps
|
2014-11-10 19:36:24 +00:00
|
|
|
)
|
2014-03-02 22:01:07 +00:00
|
|
|
runAnnexHook preCommitAnnexHook
|
2014-02-19 18:14:44 +00:00
|
|
|
-- committing changes to a view updates metadata
|
|
|
|
mv <- currentView
|
|
|
|
case mv of
|
|
|
|
Nothing -> noop
|
|
|
|
Just v -> withViewChanges
|
|
|
|
(addViewMetaData v)
|
|
|
|
(removeViewMetaData v)
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
)
|
2014-03-02 22:01:07 +00:00
|
|
|
|
2010-11-10 14:52:43 +00:00
|
|
|
|
2015-12-09 19:18:25 +00:00
|
|
|
startInjectUnlocked :: FilePath -> CommandStart
|
|
|
|
startInjectUnlocked f = next $ do
|
2014-02-19 18:14:44 +00:00
|
|
|
unlessM (callCommandAction $ Command.Add.start f) $
|
|
|
|
error $ "failed to add " ++ f ++ "; canceling commit"
|
2012-03-14 21:43:34 +00:00
|
|
|
next $ return True
|
2013-02-06 16:40:59 +00:00
|
|
|
|
|
|
|
startDirect :: [String] -> CommandStart
|
2014-10-09 19:35:19 +00:00
|
|
|
startDirect _ = next $ next preCommitDirect
|
2014-02-19 18:14:44 +00:00
|
|
|
|
2014-02-22 17:35:50 +00:00
|
|
|
addViewMetaData :: View -> ViewedFile -> Key -> CommandStart
|
2014-02-19 18:14:44 +00:00
|
|
|
addViewMetaData v f k = do
|
|
|
|
showStart "metadata" f
|
|
|
|
next $ next $ changeMetaData k $ fromView v f
|
|
|
|
|
2014-02-22 17:35:50 +00:00
|
|
|
removeViewMetaData :: View -> ViewedFile -> Key -> CommandStart
|
2014-02-19 18:14:44 +00:00
|
|
|
removeViewMetaData v f k = do
|
|
|
|
showStart "metadata" f
|
|
|
|
next $ next $ changeMetaData k $ unsetMetaData $ fromView v f
|
|
|
|
|
|
|
|
changeMetaData :: Key -> MetaData -> CommandCleanup
|
|
|
|
changeMetaData k metadata = do
|
|
|
|
showMetaDataChange metadata
|
|
|
|
addMetaData k metadata
|
|
|
|
return True
|
|
|
|
|
|
|
|
showMetaDataChange :: MetaData -> Annex ()
|
|
|
|
showMetaDataChange = showLongNote . unlines . concatMap showmeta . fromMetaData
|
|
|
|
where
|
|
|
|
showmeta (f, vs) = map (showmetavalue f) $ S.toList vs
|
|
|
|
showmetavalue f v = fromMetaField f ++ showset v ++ "=" ++ fromMetaValue v
|
|
|
|
showset v
|
|
|
|
| isSet v = "+"
|
|
|
|
| otherwise = "-"
|
2014-03-21 18:39:50 +00:00
|
|
|
|
|
|
|
{- Takes exclusive lock; blocks until available. -}
|
|
|
|
lockPreCommitHook :: Annex a -> Annex a
|
2014-08-20 20:45:58 +00:00
|
|
|
lockPreCommitHook = withExclusiveLock gitAnnexPreCommitLock
|