2010-11-10 14:52:43 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2014-02-19 18:14:44 +00:00
|
|
|
- Copyright 2010-2014 Joey Hess <joey@kitenet.net>
|
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
|
2014-02-19 18:14:44 +00:00
|
|
|
import Annex.View
|
2014-02-22 17:35:50 +00:00
|
|
|
import Annex.View.ViewedFile
|
2014-03-21 18:39:50 +00:00
|
|
|
import Annex.Perms
|
|
|
|
import Annex.Exception
|
2014-02-19 18:14:44 +00:00
|
|
|
import Logs.View
|
|
|
|
import Logs.MetaData
|
|
|
|
import Types.View
|
|
|
|
import Types.MetaData
|
|
|
|
|
2014-03-21 18:39:50 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import Utility.WinLock
|
|
|
|
#endif
|
|
|
|
|
2014-02-19 18:14:44 +00:00
|
|
|
import qualified Data.Set as S
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
2013-03-24 22:28:21 +00:00
|
|
|
def = [command "pre-commit" paramPaths seek SectionPlumbing
|
|
|
|
"run by git pre-commit hook"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
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
|
|
|
seek :: 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
|
|
|
|
-- fix symlinks to files being committed
|
|
|
|
withFilesToBeCommitted (whenAnnexed Command.Fix.start) ps
|
|
|
|
-- inject unlocked files into the annex
|
|
|
|
withFilesUnlockedToBeCommitted startIndirect ps
|
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
|
|
|
|
2013-02-06 16:40:59 +00:00
|
|
|
startIndirect :: FilePath -> CommandStart
|
2014-02-19 18:14:44 +00:00
|
|
|
startIndirect f = next $ do
|
|
|
|
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
|
2013-12-01 17:59:39 +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
|
|
|
|
lockPreCommitHook a = do
|
|
|
|
lockfile <- fromRepo gitAnnexPreCommitLock
|
|
|
|
createAnnexDirectory $ takeDirectory lockfile
|
|
|
|
mode <- annexFileMode
|
|
|
|
bracketIO (lock lockfile mode) unlock (const a)
|
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
lock lockfile mode = do
|
|
|
|
l <- liftIO $ noUmask mode $ createFile lockfile mode
|
|
|
|
liftIO $ waitToSetLock l (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
return l
|
|
|
|
unlock = closeFd
|
|
|
|
#else
|
|
|
|
lock lockfile _mode = liftIO $ waitToLock $ lockExclusive lockfile
|
|
|
|
unlock = dropLock
|
|
|
|
#endif
|