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, 22:05 UTC
Message-ID
<20101208220514.GA8865@burratino>
In-Reply-To
<m2oc8wt0xc.wl%dave@boostpro.com>
Dave Abrahams wrote:
Show 5 quoted lines
> You're going to love this: I had sent a pull request upstream and the
> maintainer of the project rejected my changes because I didn't follow
> some formatting convention he didn't tell me about ;-).  So I set up a
> commit hook that would prevent me from making the same mistake again,
> and cherry-picked the changes one-by-one.

Funny. Maybe "cherry-pick --no-commit" followed by ordinary commit would be appropriate? That way, when the checks fail, you are in a position to clean them up.

If the conventions were whitespace related, "git rebase --whitespace=fix" might be even more useful.

Just for kicks, here is the cherry-pick --verify for picky cherry-pickers.

-- 8< --
Subject: cherry-pick/revert: learn --verify to run pre-commit and commit-msg hooks

The main purpose of the pre-commit and commit-msg hooks is to avoid introducing regressions in whitespace style, encoding, and so forth; and it would make cherry-picking unnecessarily difficult, without preventing regressions, to unconditionally apply the same standards to existing code. For this reason, in v0.99.6~51 (2005-08-29), git learned to skip the usual hooks when cherry-picking or reverting an existing commit.

But sometimes the checks are wanted anyway. For example, with this patch applied, you can safely fetch some new contributor's code:

	$ git cherry-pick -s --verify HEAD..FETCH_HEAD

while allowing the pre-commit and commit-msg hooks to run their usual checks so the result can error out if the patches are not clean.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Untested.  Please feel free to add some documentation and tests and
submit it for real if this looks like a good idea. :)
 builtin/revert.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index bb6e9e8..511b2ea 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -36,7 +36,7 @@ static const char * const cherry_pick_usage[] = {
 	NULL
 };
 
-static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
+static int edit, no_replay, no_commit, verify, mainline, signoff, allow_ff;
 static enum { REVERT, CHERRY_PICK } action;
 static struct commit *commit;
 static int commit_argc;
@@ -67,6 +67,7 @@ static void parse_args(int argc, const char **argv)
 		OPT_INTEGER('m', "mainline", &mainline, "parent number"),
 		OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
 		OPT_STRING(0, "strategy", &strategy, "strategy", "merge strategy"),
+		OPT_BOOLEAN(0, "verify", &verify, "let hooks intervene before commiting"),
 		OPT_END(),
 		OPT_END(),
 		OPT_END(),
@@ -375,7 +376,8 @@ static int run_git_commit(const char *defmsg)
 	int i = 0;
 
 	args[i++] = "commit";
-	args[i++] = "-n";
+	if (!verify)
+		args[i++] = "-n";
 	if (signoff)
 		args[i++] = "-s";
 	if (!edit) {
-- 
1.7.2.4
Previous: Dave AbrahamsNext: Dave Abrahams
Message 4 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.