git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: cherry-pick / pre-commit hook?

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Dec 8, 2010, 17:53 UTC
Message-ID
<20101208175324.GB5687@burratino>
In-Reply-To
<m2wrnktcl2.wl%dave@boostpro.com>
Dave Abrahams wrote:
> Is there a good reason that git cherry-pick (without --no-commit)
> doesn't run my pre-commit hook?
Interesting question.
 $ git grep -F -e '"cherry-pick"'
 [...]
 git.c:          { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
 $ git grep -F -e cmd_cherry_pick
 builtin.h:extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
 builtin/revert.c:int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 git.c:          { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },

cherry-pick is implemented in builtin/revert.c. How does it invoke the "git commit" machinery? Explicitly, as luck would have it.

 $ git grep --show-function -F -h -C5 -e '"commit"' -- builtin/revert.c
 static int run_git_commit(const char *defmsg)
 {
         /* 6 is max possible length of our args array including NULL */
         const char *args[6];
         int i = 0;
 
         args[i++] = "commit";
         args[i++] = "-n";
         if (signoff)
                 args[i++] = "-s";
 [...]

So cherry-pick deliberately uses -n (= --no-verify) when it calls "git commit". Why, though?

 $ git log --oneline --follow -S'"-n"' -- builtin/revert.c
 cfd9c27 Allow cherry-pick (and revert) to add signoff line
 f810379 Make builtin-revert.c use parse_options.
 9509af6 Make git-revert & git-cherry-pick a builtin

The '-n' was copied from the old git-revert.sh script when cherry-pick was made builtin. Not to let the trail grow cold:

 $ git log --oneline --follow -S-n -- git-revert.sh
 9509af6 Make git-revert & git-cherry-pick a builtin
 abd6970 cherry-pick: make -r the default
 674b280 Add documentation for git-revert and git-cherry-pick.
 8bf14d6 Document the --(no-)edit switch of git-revert and git-cherry-pick
 b788498 git-revert: make --edit default.
 e2f5f6e Do not require clean tree when reverting and cherry-picking.
 9fa4db5 Do not verify reverted/cherry-picked/rebased patches.
 [...]
 $ git show -s 9fa4db5
 commit 9fa4db544e2e4d6c931f6adabc5270daec041536
 Author: Junio C Hamano <junkio@cox.net>
 Date:   Mon Aug 29 21:19:04 2005 -0700
     Do not verify reverted/cherry-picked/rebased patches.
    
     The original committer may have used validation criteria that is less
     stricter than yours.  You do not want to lose the changes even if they
     are done in substandard way from your 'commit -v' verifier's point of
     view.
    
     Signed-off-by: Junio C Hamano <junkio@cox.net>
 $

At last, an answer. The main purpose of the pre-commit hook (and builtin checks that preceded it) is to avoid introducing regressions in whitespace style, encoding, and so forth; but it would make cherry-picking unnecessarily difficult, without preventing regressions, to apply the same standards to existing code.

> Is there a hook that cherry-pick
> /will/ run instead?

"git log --grep=pre-commit" seems to suggest that the commit-msg and post-commit hooks will be run. But first, what are you trying to accomplish? Maybe there is a simpler way, or maybe with that use case in mind we can make changes to support it better.

Hope that helps, Jonathan

Previous: Dave AbrahamsNext: Dave Abrahams
Message 2 of 11 in “cherry-pick / pre-commit hook?”
  1. Dave AbrahamsDec 8, 2010
  2. Jonathan NiederDec 8, 2010
  3. Dave AbrahamsDec 8, 2010
  4. Jonathan NiederDec 8, 2010
  5. Dave AbrahamsDec 27, 2010
  6. Jonathan NiederDec 27, 2010
  7. Junio C HamanoDec 27, 2010
  8. Dave AbrahamsDec 27, 2010
  9. Junio C HamanoDec 28, 2010
  10. Jakub NarebskiDec 28, 2010
  11. Dave AbrahamsDec 29, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.