9

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?

ziemowit141
  • 465
  • 3
  • 18
  • `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`. – ElpieKay Sep 02 '20 at 13:45
  • @ElpieKay So 'pre-rebase' hook will be called on cherry-pick? – ziemowit141 Sep 02 '20 at 13:47
  • @ziemowit141 No. Then it's possible to distinguish `git cherry-pick` from `git rebase`. – ElpieKay Sep 02 '20 at 13:55

1 Answers1

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.

BitLauncher
  • 587
  • 4
  • 15