What git hooks can be executed on cherry-pick operation?
Asked Answered
M

1

11

I am trying to find a way to check commits when cherry-picking them. Unfortunately, I cannot find a git hook that works with cherry-pick operation. What would be a possible solution to execute a script on cherry-picked commit?

Murry answered 2/9, 2020 at 13:31 Comment(3)
prepare-commit-msg is invoked for git cherry-pick(with out -n). But it seems impossible to distinguish git cherry-pick from other commands like git rebase.Guyenne
@Guyenne So 'pre-rebase' hook will be called on cherry-pick?Murry
@Murry No. Then it's possible to distinguish git cherry-pick from git rebase.Guyenne
P
3

As the other told in the comments above, there is no direct cherry-pick hook.

But I can propose you a workaround, that could work under certain conditions:

Some git front-ends add a default commit message to every cherry-pick commit.

Here just as an example GitExtension where a cherry pick command shows you a dialogue called "Cherry pick commit" with a tickable option "Add commit reference to commit message". With this option the commit message will look like e. g.:

Fix of security problem sp345

(cherry picked from commit c157f17740bad0c8c59ff3f693a747552008371d)

# Conflicts:
#   authorization/secure/check.py

A commit-msg hook could be written doing this:

  1. Checking if the commit message contains cherry picked from commit
  2. if yes then do whatever you want to do in a "cherry pick hook"

There is only one condition required: That every committer follows the rule to add (via tool or manually) the text to the commit message that is needed to detect it properly as a cherry-picked commit.

Peridot answered 31/10, 2021 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.