configs annex.post-update-command and annex.pre-commit-command

Added git configs annex.post-update-command and annex.pre-commit-command
that correspond to the git-annex hook scripts post-update-annex and
pre-commit-annex.

Note that the hook files take precience over the git config, since the git
config can includ global config which should be overridden by local config.

These new git configs are probably not super useful. Especially the
pre-commit-annex hook is there to install scripts to instead of the
pre-commit hook, since git-annex installs that hook itself. So why would
someone want to use a git config for that? Only reason I can think of would
be in a global git config. Or possibly because it's easier to set a git
config than write a hook script, on an OS like Windows.

The real reason I'm adding these is as groundwork for making other
annex.*-command git configs also be available as hook scripts. I want
to avoid having some things available as only git hooks and others as
both gitconfigs and git hooks. (It seems that some annex.*-command configs
don't translate to git hooks though.)

In the man page, moved documentation of the hooks to be next to the
documentation of the git configs. This is to avoid repitition.
This commit is contained in:
Joey Hess 2025-01-10 13:27:51 -04:00
parent bd5d782c90
commit 5df1b2b36e
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
6 changed files with 43 additions and 18 deletions

View file

@ -257,7 +257,7 @@ updateTo' pairs = do
mergeIndex jl refs
let commitrefs = nub $ fullname:refs
ifM (handleTransitions jl localtransitions commitrefs)
( runAnnexHook postUpdateAnnexHook
( runAnnexHook postUpdateAnnexHook annexPostUpdateCommand
, do
ff <- if dirty
then return False
@ -724,7 +724,7 @@ setIndexSha :: Git.Sha -> Annex ()
setIndexSha ref = do
f <- fromRepo gitAnnexIndexStatus
writeLogFile f $ fromRef ref ++ "\n"
runAnnexHook postUpdateAnnexHook
runAnnexHook postUpdateAnnexHook annexPostUpdateCommand
{- Stages the journal into the index, and runs an action that
- commits the index to the branch. Note that the action is run

View file

@ -71,18 +71,26 @@ hookWarning h msg = do
{- Runs a hook. To avoid checking if the hook exists every time,
- the existing hooks are cached. -}
runAnnexHook :: Git.Hook -> Annex ()
runAnnexHook hook = do
runAnnexHook :: Git.Hook -> (GitConfig -> Maybe String) -> Annex ()
runAnnexHook hook commandcfg = do
m <- Annex.getState Annex.existinghooks
case M.lookup hook m of
Just True -> run
Just False -> noop
Just True -> runhook
Just False -> runcommandcfg
Nothing -> do
exists <- inRepo $ Git.hookExists hook
Annex.changeState $ \s -> s
{ Annex.existinghooks = M.insert hook exists m }
when exists run
if exists
then runhook
else runcommandcfg
where
run = unlessM (inRepo $ Git.runHook hook) $ do
runhook = unlessM (inRepo $ Git.runHook hook) $ do
h <- fromRepo $ Git.hookFile hook
warning $ UnquotedString $ h ++ " failed"
commandfailed h
runcommandcfg = commandcfg <$> Annex.getGitConfig >>= \case
Just command ->
unlessM (liftIO $ boolSystem "sh" [Param "-c", Param command]) $
commandfailed command
Nothing -> noop
commandfailed c = warning $ UnquotedString $ c ++ " failed"

View file

@ -11,6 +11,9 @@ git-annex (10.20250103) UNRELEASED; urgency=medium
* git-remote-annex: Use enableremote rather than initremote.
* Windows: Fix permission denied error when dropping files that
have the readonly attribute set.
* Added git configs annex.post-update-command and annex.pre-commit-command
that correspond to the git-annex hook scripts post-update-annex and
pre-commit-annex.
-- Joey Hess <id@joeyh.name> Fri, 03 Jan 2025 14:30:38 -0400

View file

@ -44,7 +44,7 @@ seek ps = do
-- files in the worktree won't be populated, so populate them here
Command.Smudge.updateSmudged (Restage False)
runAnnexHook preCommitAnnexHook
runAnnexHook preCommitAnnexHook annexPreCommitCommand
-- committing changes to a view updates metadata
currentView >>= \case

View file

@ -97,6 +97,8 @@ data GitConfig = GitConfig
, annexAlwaysCompact :: Bool
, annexCommitMessage :: Maybe String
, annexCommitMessageCommand :: Maybe String
, annexPreCommitCommand :: Maybe String
, annexPostUpdateCommand :: Maybe String
, annexMergeAnnexBranches :: Bool
, annexDelayAdd :: Maybe Int
, annexHttpHeaders :: [String]
@ -190,6 +192,8 @@ extractGitConfig configsource r = GitConfig
, annexAlwaysCompact = getbool (annexConfig "alwayscompact") True
, annexCommitMessage = getmaybe (annexConfig "commitmessage")
, annexCommitMessageCommand = getmaybe (annexConfig "commitmessage-command")
, annexPreCommitCommand = getmaybe (annexConfig "pre-commit-command")
, annexPostUpdateCommand = getmaybe (annexConfig "post-update-command")
, annexMergeAnnexBranches = getbool (annexConfig "merge-annex-branches") True
, annexDelayAdd = getmayberead (annexConfig "delayadd")
, annexHttpHeaders = getlist (annexConfig "http-headers")

View file

@ -1155,6 +1155,24 @@ repository, using [[git-annex-config]]. See its man page for a list.)
This command is run and its output is used as the commit message to the
git-annex branch.
* `annex.post-update-command`
This command is run after git-annex updates the git-annex branch.
Alternatively, a hook script can be installed in
`.git/hooks/post-update-annex`
When publishing a git-annex repository by http, this can be used to run
`git update-server-info`
* `annex.pre-commit-command`
This command is run whenever a commit is made to the HEAD branch of
the git repository, either by git commit, or by git-annex.
Alternatively, a hook script can be installed in
`.git/hooks/pre-commit-annex`
* `annex.alwayscompact`
By default, git-annex compacts data it records in the git-annex branch.
@ -2322,14 +2340,6 @@ used by git-annex.
`~/.config/git-annex/autostart` is a list of git repositories
to start the git-annex assistant in.
`.git/hooks/pre-commit-annex` in your git repository will be run whenever
a commit is made to the HEAD branch, either by git commit, git-annex
sync, or the git-annex assistant.
`.git/hooks/post-update-annex` in your git repository will be run
whenever the git-annex branch is updated. You can make this hook run
`git update-server-info` when publishing a git-annex repository by http.
# SEE ALSO
More git-annex documentation is available on its web site,