From ca7de61454fb441d225e489e2e546346241c7530 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 25 Oct 2018 15:40:12 -0400 Subject: [PATCH] git post-checkout and post-merge hooks * init, upgrade: Install git post-checkout and post-merge hooks that run git annex smudge --update. * precommit: Run git annex smudge --update, because the post-merge hook is not run when there is a merge conflict. So the work tree will be updated when a commit is made to resolve the merge conflict. * precommit: Run git annex smudge --update, because the post-merge hook is not run when there is a merge conflict. So the work tree will be updated when a commit is made to resolve the merge conflict. * Note that git has no hooks run after git stash or git cherry-pick, so the user will have to manually run git annex smudge --update after such commands. Nothing currently installs the hooks into v6 repos that already exist. Something will need to be done about that, either move this behavior to v7, or document that the user will need to manually fix up their v6 repos. This commit was sponsored by Eric Drechsel on Patreon. --- Annex/Hook.hs | 11 +++++++++++ Annex/Init.hs | 3 +++ CHANGELOG | 8 ++++++++ Command/PreCommit.hs | 21 ++++++++++++++++----- Command/Smudge.hs | 2 +- Upgrade/V5.hs | 4 ++++ 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Annex/Hook.hs b/Annex/Hook.hs index efcc5de836..55826d8182 100644 --- a/Annex/Hook.hs +++ b/Annex/Hook.hs @@ -32,6 +32,17 @@ postReceiveHook = Git.Hook "post-receive" [ mkHookScript "git annex post-receive" ] +postCheckoutHook :: Git.Hook +postCheckoutHook = Git.Hook "post-checkout" smudgeHook [] + +postMergeHook :: Git.Hook +postMergeHook = Git.Hook "post-merge" smudgeHook [] + +-- Only run git-annex smudge --update when git-annex supports it. +-- Older versions of git-annex didn't need this hook. +smudgeHook :: String +smudgeHook = mkHookScript "if git annex smudge --update >/dev/null 2>&1; then git-annex smudge --update; fi" + preCommitAnnexHook :: Git.Hook preCommitAnnexHook = Git.Hook "pre-commit-annex" "" [] diff --git a/Annex/Init.hs b/Annex/Init.hs index 802524c82c..79c505d4b1 100644 --- a/Annex/Init.hs +++ b/Annex/Init.hs @@ -112,6 +112,9 @@ initialize' ai mversion = checkCanInitialize ai $ do whenM versionSupportsUnlockedPointers $ do configureSmudgeFilter scanUnlockedFiles + unlessM isBareRepo $ do + hookWrite postCheckoutHook + hookWrite postMergeHook checkAdjustedClone >>= \case NeedUpgradeForAdjustedClone -> void $ upgrade True versionForAdjustedClone diff --git a/CHANGELOG b/CHANGELOG index 2ea7896924..ce74d99160 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -26,6 +26,14 @@ git-annex (6.20181012) UNRELEASED; urgency=medium honor annex.thin. Now git annex smudge --update has to be run after a checkout to update unlocked files in the working tree with annexed file contents. + * init, upgrade: Install git post-checkout and post-merge hooks that run + git annex smudge --update. + * precommit: Run git annex smudge --update, because the post-merge + hook is not run when there is a merge conflict. So the work tree will + be updated when a commit is made to resolve the merge conflict. + * Note that git has no hooks run after git stash or git cherry-pick, + so the user will have to manually run git annex smudge --update + after such commands. -- Joey Hess Sat, 13 Oct 2018 00:52:02 -0400 diff --git a/Command/PreCommit.hs b/Command/PreCommit.hs index 1222ba523e..088966dd02 100644 --- a/Command/PreCommit.hs +++ b/Command/PreCommit.hs @@ -13,6 +13,7 @@ import Command import Config import qualified Command.Add import qualified Command.Fix +import qualified Command.Smudge import Annex.Direct import Annex.Hook import Annex.Link @@ -54,11 +55,21 @@ seek ps = lockPreCommitHook $ ifM isDirect flip withFilesToBeCommitted l $ \f -> commandAction $ maybe stop (Command.Fix.start Command.Fix.FixSymlinks f) =<< isAnnexLink f - -- inject unlocked files into the annex - -- (not needed when repo version uses - -- unlocked pointer files) - unlessM versionSupportsUnlockedPointers $ - withFilesOldUnlockedToBeCommitted (commandAction . startInjectUnlocked) l + ifM versionSupportsUnlockedPointers + -- after a merge conflict or git + -- cherry-pick or stash, pointer + -- files in the worktree won't + -- be populated, so populate them + -- here + ( Command.Smudge.updateSmudged + -- When there's a false index, + -- restaging the files won't work. + . Restage =<< liftIO Git.haveFalseIndex + -- inject unlocked files into the annex + -- (not needed when repo version uses + -- unlocked pointer files) + , withFilesOldUnlockedToBeCommitted (commandAction . startInjectUnlocked) l + ) ) runAnnexHook preCommitAnnexHook -- committing changes to a view updates metadata diff --git a/Command/Smudge.hs b/Command/Smudge.hs index d335cf82b5..68488cc796 100644 --- a/Command/Smudge.hs +++ b/Command/Smudge.hs @@ -67,7 +67,7 @@ smudge file = do Just k -> do topfile <- inRepo (toTopFilePath file) Database.Keys.addAssociatedFile k topfile - smudgeLog k topfile + void $ smudgeLog k topfile liftIO $ B.putStr b stop diff --git a/Upgrade/V5.hs b/Upgrade/V5.hs index 9998901506..b5cd410bcd 100644 --- a/Upgrade/V5.hs +++ b/Upgrade/V5.hs @@ -17,6 +17,7 @@ import Annex.Direct import Annex.Content import Annex.CatFile import Annex.WorkTree +import Annex.Hook import qualified Database.Keys import qualified Annex.Content.Direct as Direct import qualified Git @@ -63,6 +64,9 @@ upgrade automatic = do - adjust branch. Instead, update HEAD manually. -} inRepo $ setHeadRef b configureSmudgeFilter + unlessM isBareRepo $ do + hookWrite postCheckoutHook + hookWrite postMergeHook -- Inode sentinal file was only used in direct mode and when -- locking down files as they were added. In v6, it's used more -- extensively, so make sure it exists, since old repos that didn't