Skip to contentSkip to conversation

CHERRY_PICK_HEAD

PATCH Superseded Jay Soffian 3 participants 17 messages Jun 15, 2016 Documentation

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover CHERRY_PICK_HEAD

Incorporating feedback from http://thread.gmane.org/gmane.comp.version-control.git/166945

Jay Soffian (4):
Introduce CHERRY_PICK_HEAD
bash: teach __git_ps1 about CHERRY_PICK_HEAD commit.c: replace some literal strings with constants Teach commit about CHERRY_PICK_HEAD

 Documentation/git-cherry-pick.txt      |   19 +++
 Documentation/git-commit.txt           |    7 +-
 Documentation/revisions.txt            |    5 +-
 branch.c                               |    1 +
 builtin/commit.c                       |  196 ++++++++++++++++++++++----------
 builtin/merge.c                        |    7 +
 builtin/revert.c                       |   74 ++++---------
 contrib/completion/git-completion.bash |    2 +
 t/t3507-cherry-pick-conflict.sh        |  138 ++++++++++++++++++++++-
 t/t7509-commit.sh                      |   29 +++++
 wt-status.c                            |    4 +-
 wt-status.h                            |    9 ++-
 12 files changed, 371 insertions(+), 120 deletions(-)
Signature · 1 line
1.7.4.1.30.g7fe09
#1 lore raw

PATCH v3 1/4 Introduce CHERRY_PICK_HEAD

When a cherry-pick conflicts git advises to use:

 $ git commit -c <original commit id>

to preserve the original commit message and authorship. Instead, let's record the original commit id in CHERRY_PICK_HEAD and advise to use:

  $ git commit -c CHERRY_PICK_HEAD

A later patch teaches git to handle the '-c CHERRY_PICK_HEAD' part. Note that we record CHERRY_PICK_HEAD even in the case where there are no conflicts so that we may use it to communicate authorship to commit; this will then allow us to remove set_author_ident_env from revert.c.

Tests and documentation contributed by Jonathan Nieder.

---
Incorporated feedback and additional tests from
http://article.gmane.org/gmane.comp.version-control.git/167007

 Documentation/git-cherry-pick.txt |   19 +++++
 Documentation/revisions.txt       |    5 +-
 branch.c                          |    1 +
 builtin/commit.c                  |    1 +
 builtin/merge.c                   |    7 ++
 builtin/revert.c                  |   26 +++++++-
 t/t3507-cherry-pick-conflict.sh   |  138 ++++++++++++++++++++++++++++++++++++-
 7 files changed, 193 insertions(+), 4 deletions(-)
Documentation/git-cherry-pick.txt +19 -0
index 749d68a..5d85daa 100644
@@ -16,6 +16,25 @@ Given one or more existing commits, apply the change each one
 introduces, recording a new commit for each.  This requires your
 working tree to be clean (no modifications from the HEAD commit).
 
+When it is not obvious how to apply a change, the following
+happens:
+
+1. The current branch and `HEAD` pointer stay at the last commit
+   successfully made.
+2. The `CHERRY_PICK_HEAD` ref is set to point at the commit that
+   introduced the change that is difficult to apply.
+3. Paths in which the change applied cleanly are updated both
+   in the index file and in your working tree.
+4. For conflicting paths, the index file records up to three
+   versions, as described in the "TRUE MERGE" section of
+   linkgit:git-merge[1].  The working tree files will include
+   a description of the conflict bracketed by the usual
+   conflict markers `<<<<<<<` and `>>>>>>>`.
+5. No other modifications are made.
+
+See linkgit:git-merge[1] for some hints on resolving such
+conflicts.
+
 OPTIONS
 -------
 <commit>...::
Documentation/revisions.txt +4 -1
index 9e92734..04fceee 100644
@@ -25,7 +25,8 @@ blobs contained in a commit.
   first match in the following rules:
 
   . if `$GIT_DIR/<name>` exists, that is what you mean (this is usually
-    useful only for `HEAD`, `FETCH_HEAD`, `ORIG_HEAD` and `MERGE_HEAD`);
+    useful only for `HEAD`, `FETCH_HEAD`, `ORIG_HEAD`, `MERGE_HEAD`
+    and `CHERRY_PICK_HEAD`);
 
   . otherwise, `refs/<name>` if exists;
 
@@ -46,6 +47,8 @@ you can change the tip of the branch back to the state before you ran
 them easily.
 MERGE_HEAD records the commit(s) you are merging into your branch
 when you run 'git merge'.
+CHERRY_PICK_HEAD records the commit you are cherry-picking
+when you run 'git cherry-pick'.
 +
 Note that any of the `refs/*` cases above may come either from
 the `$GIT_DIR/refs` directory or from the `$GIT_DIR/packed-refs` file.
branch.c +1 -0
index 93dc866..dc23e95 100644
@@ -217,6 +217,7 @@ void create_branch(const char *head,
 
 void remove_branch_state(void)
 {
+	unlink(git_path("CHERRY_PICK_HEAD"));
 	unlink(git_path("MERGE_HEAD"));
 	unlink(git_path("MERGE_RR"));
 	unlink(git_path("MERGE_MSG"));
builtin/commit.c +1 -0
index 03cff5a..0def540 100644
@@ -1424,6 +1424,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		die("cannot update HEAD ref");
 	}
 
+	unlink(git_path("CHERRY_PICK_HEAD"));
 	unlink(git_path("MERGE_HEAD"));
 	unlink(git_path("MERGE_MSG"));
 	unlink(git_path("MERGE_MODE"));
builtin/merge.c +7 -0
index 9403747..454dad2 100644
@@ -971,6 +971,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			die("You have not concluded your merge (MERGE_HEAD exists).");
 	}
+	if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
+		if (advice_resolve_conflict)
+			die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
+			    "Please, commit your changes before you can merge.");
+		else
+			die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).");
+	}
 	resolve_undo_clear();
 
 	if (verbosity < 0)
builtin/revert.c +24 -2
index dc1b702..fbb465a 100644
@@ -248,6 +248,22 @@ static void set_author_ident_env(const char *message)
 			sha1_to_hex(commit->object.sha1));
 }
 
+static void write_cherry_pick_head(void)
+{
+	int fd;
+	struct strbuf buf = STRBUF_INIT;
+
+	strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
+
+	fd = open(git_path("CHERRY_PICK_HEAD"), O_WRONLY | O_CREAT, 0666);
+	if (fd < 0)
+		die_errno("Could not open '%s' for writing",
+			  git_path("CHERRY_PICK_HEAD"));
+	if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
+		die_errno("Could not write to '%s'", git_path("CHERRY_PICK_HEAD"));
+	strbuf_release(&buf);
+}
+
 static void advise(const char *advice, ...)
 {
 	va_list params;
@@ -263,6 +279,12 @@ static void print_advice(void)
 
 	if (msg) {
 		fprintf(stderr, "%s\n", msg);
+		/*
+		 * A conflict has occured but the porcelain
+		 * (typically rebase --interactive) wants to take care
+		 * of the commit itself so remove CHERRY_PICK_HEAD
+		 */
+		unlink(git_path("CHERRY_PICK_HEAD"));
 		return;
 	}
 
@@ -270,8 +292,7 @@ static void print_advice(void)
 	advise("with 'git add <paths>' or 'git rm <paths>'");
 
 	if (action == CHERRY_PICK)
-		advise("and commit the result with 'git commit -c %s'",
-		       find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
+		advise("and commit the result with 'git commit -c CHERRY_PICK_HEAD'");
 }
 
 static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -504,6 +525,7 @@ static int do_pick_commit(void)
 			strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
 			strbuf_addstr(&msgbuf, ")\n");
 		}
+		write_cherry_pick_head();
 	}
 
 	if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
t/t3507-cherry-pick-conflict.sh +137 -1

156 lines not on this page · raw message

Signature · 1 line
1.7.4.1.30.g7fe09
#2 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

This series in 'pu' seems to break t3404#16.

#3 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

Quoted 1 line · Junio C Hamano

This series in 'pu' seems to break t3404#16.

Sorry for the breakage, I was running the test suite piecemeal and must've not run that test (which was dumb of me). I'll figure out what's going on and re-roll with Jonathan's t3507 improvements as well.

Thank you for your patience.

j.

#4 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

Quoted 6 lines · Jay Soffian
Quoted 1 line · Junio C Hamano

This series in 'pu' seems to break t3404#16.

Sorry for the breakage, I was running the test suite piecemeal and must've not run that test (which was dumb of me).

I made the same mistake, too. Sorry for the trouble.

Quoted 3 lines

I'll figure out
what's going on and re-roll with Jonathan's t3507 improvements as well.

FWIW (probably you noticed, but just in case) I imagine the t3507 improvements should go as a separate patch, since their main effect is to clean up existing tests.

#5 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

In reply to Jay Soffian
Quoted 5 lines · Jay Soffian
builtin/merge.c +0 -0
index 9403747..454dad2 100644

builtin/merge.c int cmd_merge(int argc, const char **argv, const char *prefix) · line 980

Quoted 10 lines
builtin/merge.c +7 -0
@@ -971,6 +971,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			die("You have not concluded your merge (MERGE_HEAD exists).");
 	}
+	if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
+		if (advice_resolve_conflict)
+			die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
+			    "Please, commit your changes before you can merge.");
+		else
+			die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).");
+	}

Micronit: "Please, commit your changes before you can merge."

- We are not merging in this codepath to begin with;

- I'd suggest rephrasing this (together with "MERGE_HEAD" codepath) to something like:

"Commit your changes first before retrying."

builtin/merge.c int cmd_merge(int argc, const char **argv, const char *prefix)

Quoted 8 lines
builtin/merge.c +8 -0
+test_expect_success 'cherry-pick --no-commit sets CHERRY_PICK_HEAD' '
+
+	git checkout -f initial^0 &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x &&
+
+	git update-index --refresh &&
+	git diff-index --exit-code HEAD &&

Getting tired of seeing these five lines repeated over and over. Perhaps it is time to introduce:

	pristine_detach () {
        	git checkout -f "$1^0" &&
                git read-tree -u --reset HEAD &&
                git clean -d -f -f -q -x
	}

I don't think "diff-index --exit-code HEAD" is necessary as the point of this testsuite is not about testing "read-tree --reset", and the index refresh is just a prerequisite for diff-index that can go together with it.

builtin/merge.c

Quoted 3 lines
builtin/merge.c +3 -0
+	git cherry-pick --no-commit base &&
+
+	test_cmp_rev base CHERRY_PICK_HEAD

If the next "git commit" would notice and use this information, that would introduce an unpleasant regression to one use case in my workflow, which is to pick and consolidate one or more small pieces made on a private "misc" branch, possibly with a bit of further work, into a new commit with a readable explanation that is unrelated to any of the original commits:

	git cherry-pick --no-commit $some_commit
	git cherry-pick --no-commit $another_commit ;# optional
	edit ;# optional
        git commit

I'd prefer to see a way to tell cherry-pick not to leave CHERRY_PICK_HEAD behind when "cherry-pick --no-commit" results in a successful cherry-pick to avoid a backward incompatibility surprise. Otherwise people need to retrain their fingers to say --reset-author when they run "git commit".

Other then the above three points, this patch looks good. Thanks.

#6 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

Quoted 14 lines · Junio C Hamano

Review

Quoted 8 lines · Jay Soffian
(quoted diff) +8 -0
+test_expect_success 'cherry-pick --no-commit sets CHERRY_PICK_HEAD' '
+
+	git checkout -f initial^0 &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x &&
+
+	git update-index --refresh &&
+	git diff-index --exit-code HEAD &&

Getting tired of seeing these five lines repeated over and over. Perhaps it is time to introduce:

Quoted 1 line
	pristine_detach () {

The following patch (modulo naming) was sitting in my tree as a separate patch on top.

Quoted 4 lines

I don't think "diff-index --exit-code HEAD" is necessary as the point of this testsuite is not about testing "read-tree --reset", and the index refresh is just a prerequisite for diff-index that can go together with it.

Yep. Confusingly, the index refresh was just meant to check for a clean index.

-- 8< --
Subject: [PATCH] tests: introduce pristine-detach helper

All the tests in t3507 (cherry-pick with conflicts) begin with the same checkout + read-tree + clean incantation to ensure a predictable starting point. Factor out a function for that so the interesting part of the tests is easier to read.

The "update-index --refresh" and "diff-index --exit-code HEAD" are not necessary as the point of this testsuite is not about testing "read-tree --reset".

---
Applies on top of the CHERRY_PICK_HEAD tests.

 t/t3507-cherry-pick-conflict.sh |  142 +++++++--------------------------------
 1 files changed, 24 insertions(+), 118 deletions(-)
t/t3507-cherry-pick-conflict.sh +24 -118
index ea52720..2d8437e 100755
@@ -17,6 +17,12 @@ test_cmp_rev () {
 	test_cmp expect.rev actual.rev
 }
 
+pristine_detach () {
+	git checkout -f "$1^0" &&
+	git read-tree -u --reset HEAD &&
+	git clean -d -f -f -q -x
+}
+
 test_expect_success setup '
 
 	echo unrelated >unrelated &&
@@ -29,13 +35,7 @@ test_expect_success setup '
 '
 
 test_expect_success 'failed cherry-pick does not advance HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	head=$(git rev-parse HEAD) &&
 	test_must_fail git cherry-pick picked &&
@@ -45,12 +45,7 @@ test_expect_success 'failed cherry-pick does not advance HEAD' '
 '
 
 test_expect_success 'advice from failed cherry-pick' "
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	picked=\$(git rev-parse --short picked) &&
 	cat <<-EOF >expected &&
@@ -65,73 +60,35 @@ test_expect_success 'advice from failed cherry-pick' "
 "
 
 test_expect_success 'failed cherry-pick sets CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
+	pristine_detach initial &&
 	test_must_fail git cherry-pick picked &&
-
 	test_cmp_rev picked CHERRY_PICK_HEAD
 '
 
 test_expect_success 'successful cherry-pick does not set CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
+	pristine_detach initial &&
 	git cherry-pick base &&
-
 	test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
 '
 
 test_expect_success 'cherry-pick --no-commit sets CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
+	pristine_detach initial &&
 	git cherry-pick --no-commit base &&
-
 	test_cmp_rev base CHERRY_PICK_HEAD
 '
 
 test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
+	pristine_detach initial &&
 	(
 		GIT_CHERRY_PICK_HELP="and then do something else" &&
 		export GIT_CHERRY_PICK_HELP &&
 		test_must_fail git cherry-pick picked
 	) &&
-
 	test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
 '
 
 test_expect_success 'git reset clears CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	test_must_fail git cherry-pick picked &&
 	git reset &&
@@ -140,13 +97,7 @@ test_expect_success 'git reset clears CHERRY_PICK_HEAD' '
 '
 
 test_expect_success 'failed commit does not clear CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	test_must_fail git cherry-pick picked &&
 	test_must_fail git commit &&
@@ -155,13 +106,7 @@ test_expect_success 'failed commit does not clear CHERRY_PICK_HEAD' '
 '
 
 test_expect_success 'cancelled commit does not clear CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	test_must_fail git cherry-pick picked &&
 	echo resolved >foo &&
@@ -178,13 +123,7 @@ test_expect_success 'cancelled commit does not clear CHERRY_PICK_HEAD' '
 '
 
 test_expect_success 'successful commit clears CHERRY_PICK_HEAD' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	test_must_fail git cherry-pick picked &&
 	echo resolved >foo &&
@@ -195,13 +134,7 @@ test_expect_success 'successful commit clears CHERRY_PICK_HEAD' '
 '
 
 test_expect_success 'failed cherry-pick produces dirty index' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	pristine_detach initial &&
 
 	test_must_fail git cherry-pick picked &&
 
@@ -210,9 +143,7 @@ test_expect_success 'failed cherry-pick produces dirty index' '
 '
 
 test_expect_success 'failed cherry-pick registers participants in index' '
-
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
+	pristine_detach initial &&
 	{
 		git checkout base -- foo &&
 		git ls-files --stage foo &&
@@ -226,10 +157,7 @@ test_expect_success 'failed cherry-pick registers participants in index' '
 		2 s/ 0	/ 2	/
 		3 s/ 0	/ 3	/
 	" < stages > expected &&
-	git checkout -f initial^0 &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	git read-tree -u --reset HEAD &&
 
 	test_must_fail git cherry-pick picked &&
 	git ls-files --stage --unmerged > actual &&
@@ -238,10 +166,7 @@ test_expect_success 'failed cherry-pick registers participants in index' '
 '
 
 test_expect_success 'failed cherry-pick describes conflict in work tree' '
-
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
+	pristine_detach initial &&
 	cat <<-EOF > expected &&
 	<<<<<<< HEAD
 	a
@@ -250,9 +175,6 @@ test_expect_success 'failed cherry-pick describes conflict in work tree' '
 	>>>>>>> objid picked
 	EOF
 
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
 	test_must_fail git cherry-pick picked &&
 
 	sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
@@ -260,11 +182,8 @@ test_expect_success 'failed cherry-pick describes conflict in work tree' '
 '
 
 test_expect_success 'diff3 -m style' '
-
+	pristine_detach initial &&
 	git config merge.conflictstyle diff3 &&
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
 	cat <<-EOF > expected &&
 	<<<<<<< HEAD
 	a
@@ -275,9 +194,6 @@ test_expect_success 'diff3 -m style' '
 	>>>>>>> objid picked
 	EOF
 
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
 	test_must_fail git cherry-pick picked &&
 
 	sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
@@ -285,10 +201,8 @@ test_expect_success 'diff3 -m style' '
 '
 
 test_expect_success 'revert also handles conflicts sanely' '
-
 	git config --unset merge.conflictstyle &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
+	pristine_detach initial &&
 	cat <<-EOF > expected &&
 	<<<<<<< HEAD
 	a
@@ -309,10 +223,7 @@ test_expect_success 'revert also handles conflicts sanely' '
 		2 s/ 0	/ 2	/
 		3 s/ 0	/ 3	/
 	" < stages > expected-stages &&
-	git checkout -f initial^0 &&
-
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
+	git read-tree -u --reset HEAD &&
 
 	head=$(git rev-parse HEAD) &&
 	test_must_fail git revert picked &&
@@ -328,10 +239,8 @@ test_expect_success 'revert also handles conflicts sanely' '
 '
 
 test_expect_success 'revert conflict, diff3 -m style' '
+	pristine_detach initial &&
 	git config merge.conflictstyle diff3 &&
-	git checkout -f initial^0 &&
-	git read-tree -u --reset HEAD &&
-	git clean -d -f -f -q -x &&
 	cat <<-EOF > expected &&
 	<<<<<<< HEAD
 	a
@@ -342,9 +251,6 @@ test_expect_success 'revert conflict, diff3 -m style' '
 	>>>>>>> parent of objid picked
 	EOF
 
-	git update-index --refresh &&
-	git diff-index --exit-code HEAD &&
-
 	test_must_fail git revert picked &&
 
 	sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
Signature · 1 line
1.7.4.1
#7 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

In reply to Junio C Hamano
Quoted 20 lines · Junio C Hamano
Quoted 7 lines · Jay Soffian
builtin/merge.c +0 -0
index 9403747..454dad2 100644
@@ -971,6 +971,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)

              else
                      die("You have not concluded your merge (MERGE_HEAD exists).");

Quoted 8 lines

      }
+     if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
+             if (advice_resolve_conflict)
+                     die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
+                         "Please, commit your changes before you can merge.");
+             else
+                     die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).");
+     }

Micronit: "Please, commit your changes before you can merge."

Quoted 6 lines

 - We are not merging in this codepath to begin with;

 - I'd suggest rephrasing this (together with "MERGE_HEAD" codepath) to something like:

       "Commit your changes first before retrying."

This hunk is from Jonathan, so I'd like him to address your comment as well, but I think the current message is correct. This hunk is inside cmd_merge and the user is about to start a merge. IOW:

$ git cherry-pick   # fails, leave behinds CHERRY_PICK_HEAD
 [... time passes ...]
$ git merge
You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).
Please, commit your changes before you can merge.
Quoted 2 lines

Getting tired of seeing these five lines repeated over and over.  Perhaps it is time to introduce:

Okay. (Will roll in from Jonathan's later email.)

Quoted 15 lines
Quoted 3 lines

+     git cherry-pick --no-commit base &&
+
+     test_cmp_rev base CHERRY_PICK_HEAD

If the next "git commit" would notice and use this information, that would introduce an unpleasant regression to one use case in my workflow, which is to pick and consolidate one or more small pieces made on a private "misc" branch, possibly with a bit of further work, into a new commit with a readable explanation that is unrelated to any of the original commits:

       git cherry-pick --no-commit $some_commit
       git cherry-pick --no-commit $another_commit ;# optional edit ;# optional
       git commit

Quoted 4 lines

I'd prefer to see a way to tell cherry-pick not to leave CHERRY_PICK_HEAD behind when "cherry-pick --no-commit" results in a successful cherry-pick to avoid a backward incompatibility surprise.  Otherwise people need to retrain their fingers to say --reset-author when they run "git commit".

Okay. In that case, I think when using --no-commit, we shouldn't write CHERRY_PICK_HEAD regardless of whether there is a conflict or not.

j.

#8 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

Hi,

Quoted 4 lines · Jay Soffian
Quoted 2 lines · Junio C Hamano

Micronit: "Please, commit your changes before you can merge."

Quoted 8 lines
Quoted 6 lines

 - We are not merging in this codepath to begin with;

 - I'd suggest rephrasing this (together with "MERGE_HEAD" codepath) to something like:

       "Commit your changes first before retrying."

This hunk is from Jonathan

I agree with what Jay said --- this is in cmd_merge, so the current message is correct. I like your wording ("before retrying") more, but that seems orthogonal to the topic at hand. I can send a patch rephrasing it on top if someone reminds me.

Quoted 2 lines

Okay. In that case, I think when using --no-commit, we shouldn't write CHERRY_PICK_HEAD regardless of whether there is a conflict or not.

Thanks for your attention to detail.
Jonathan

#9 lore raw

Re: [PATCH v3 1/4] Introduce CHERRY_PICK_HEAD

In reply to Jay Soffian
Quoted 2 lines · Jay Soffian

..., but I think the current message is correct. This hunk is inside cmd_merge and the user is about to start a merge.

Yes, you are right; I misread the patch. Sorry about that.

#10 lore raw

PATCH v3 2/4 bash: teach __git_ps1 about CHERRY_PICK_HEAD

In reply to Jay Soffian

Make the git prompt (when enabled) show a CHERRY-PICKING indicator when we are in the middle of a conflicted cherry-pick, analogous to the existing MERGING and BISECTING flags.

---
Split this out into its own patch per
http://article.gmane.org/gmane.comp.version-control.git/167009

 contrib/completion/git-completion.bash |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
contrib/completion/git-completion.bash +2 -0
index 893b771..0b0b913 100755
@@ -246,6 +246,8 @@ __git_ps1 ()
 				fi
 			elif [ -f "$g/MERGE_HEAD" ]; then
 				r="|MERGING"
+			elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+				r="|CHERRY-PICKING"
 			elif [ -f "$g/BISECT_LOG" ]; then
 				r="|BISECTING"
 			fi
Signature · 1 line
1.7.4.1.30.g7fe09
#11 lore raw

PATCH v3 3/4 commit.c: replace some literal strings with constants

In reply to Jay Soffian

A typo in any of these would be bad, so let's use
constants for them

---
I converted these per
http://article.gmane.org/gmane.comp.version-control.git/167015

Maybe this should be the last patch in the series; it's questionable to me whether it's even worth doing.

 builtin/commit.c |   48 +++++++++++++++++++++++++++---------------------
 1 files changed, 27 insertions(+), 21 deletions(-)
builtin/commit.c +27 -21
index 0def540..5b32743 100644
@@ -54,10 +54,16 @@ static const char empty_amend_advice[] =
 "it empty. You can repeat your command with --allow-empty, or you can\n"
 "remove the commit entirely with \"git reset HEAD^\".\n";
 
+static const char commit_editmsg[] = "COMMIT_EDITMSG";
+static const char cherry_pick_head[] = "CHERRY_PICK_HEAD";
+static const char merge_head[] = "MERGE_HEAD";
+static const char merge_msg[] = "MERGE_MSG";
+static const char merge_mode[] = "MERGE_MODE";
+static const char squash_msg[] = "SQUASH_MSG";
+
 static unsigned char head_sha1[20];
 
 static char *use_message_buffer;
-static const char commit_editmsg[] = "COMMIT_EDITMSG";
 static struct lock_file index_lock; /* real index */
 static struct lock_file false_lock; /* used only for partial commits */
 static enum {
@@ -626,13 +632,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		format_commit_message(commit, "fixup! %s\n\n",
 				      &sb, &ctx);
 		hook_arg1 = "message";
-	} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
-			die_errno("could not read MERGE_MSG");
+	} else if (!stat(git_path(merge_msg), &statbuf)) {
+		if (strbuf_read_file(&sb, git_path(merge_msg), 0) < 0)
+			die_errno("could not read %s", merge_msg);
 		hook_arg1 = "merge";
-	} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
-			die_errno("could not read SQUASH_MSG");
+	} else if (!stat(git_path(squash_msg), &statbuf)) {
+		if (strbuf_read_file(&sb, git_path(squash_msg), 0) < 0)
+			die_errno("could not read %s", squash_msg);
 		hook_arg1 = "squash";
 	} else if (template_file && !stat(template_file, &statbuf)) {
 		if (strbuf_read_file(&sb, template_file, 0) < 0)
@@ -702,7 +708,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 				"#	%s\n"
 				"# and try again.\n"
 				"#\n",
-				git_path("MERGE_HEAD"));
+				git_path(merge_head));
 
 		fprintf(fp,
 			"\n"
@@ -1117,7 +1123,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	wt_status_prepare(&s);
 	gitmodules_config();
 	git_config(git_status_config, &s);
-	in_merge = file_exists(git_path("MERGE_HEAD"));
+	in_merge = file_exists(git_path(merge_head));
 	argc = parse_options(argc, argv, prefix,
 			     builtin_status_options,
 			     builtin_status_usage, 0);
@@ -1302,7 +1308,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	wt_status_prepare(&s);
 	git_config(git_commit_config, &s);
-	in_merge = file_exists(git_path("MERGE_HEAD"));
+	in_merge = file_exists(git_path(merge_head));
 	s.in_merge = in_merge;
 
 	if (s.use_color == -1)
@@ -1347,21 +1353,21 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		if (!reflog_msg)
 			reflog_msg = "commit (merge)";
 		pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
-		fp = fopen(git_path("MERGE_HEAD"), "r");
+		fp = fopen(git_path(merge_head), "r");
 		if (fp == NULL)
 			die_errno("could not open '%s' for reading",
-				  git_path("MERGE_HEAD"));
+				  git_path(merge_head));
 		while (strbuf_getline(&m, fp, '\n') != EOF) {
 			unsigned char sha1[20];
 			if (get_sha1_hex(m.buf, sha1) < 0)
-				die("Corrupt MERGE_HEAD file (%s)", m.buf);
+				die("Corrupt %s file (%s)", merge_head, m.buf);
 			pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next;
 		}
 		fclose(fp);
 		strbuf_release(&m);
-		if (!stat(git_path("MERGE_MODE"), &statbuf)) {
-			if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
-				die_errno("could not read MERGE_MODE");
+		if (!stat(git_path(merge_mode), &statbuf)) {
+			if (strbuf_read_file(&sb, git_path(merge_mode), 0) < 0)
+				die_errno("could not read %s", merge_mode);
 			if (!strcmp(sb.buf, "no-ff"))
 				allow_fast_forward = 0;
 		}
@@ -1424,11 +1430,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		die("cannot update HEAD ref");
 	}
 
-	unlink(git_path("CHERRY_PICK_HEAD"));
-	unlink(git_path("MERGE_HEAD"));
-	unlink(git_path("MERGE_MSG"));
-	unlink(git_path("MERGE_MODE"));
-	unlink(git_path("SQUASH_MSG"));
+	unlink(git_path(cherry_pick_head));
+	unlink(git_path(merge_head));
+	unlink(git_path(merge_msg));
+	unlink(git_path(merge_mode));
+	unlink(git_path(squash_msg));
 
 	if (commit_index_files())
 		die ("Repository has been updated, but unable to write\n"
Signature · 1 line
1.7.4.1.30.g7fe09
#12 lore raw

Re: [PATCH v3 3/4] commit.c: replace some literal strings with constants

Quoted 5 lines · Jay Soffian

I converted these per
http://article.gmane.org/gmane.comp.version-control.git/167015

Maybe this should be the last patch in the series; it's questionable to me whether it's even worth doing.

What have I wrought? I think this makes the code much less readable.

builtin/commit.c static int prepare_to_commit(const char *index_file, const char *prefix, · line 637

Quoted 12 lines
builtin/commit.c +3 -3
@@ -626,13 +632,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		format_commit_message(commit, "fixup! %s\n\n",
 				      &sb, &ctx);
 		hook_arg1 = "message";
-	} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
-			die_errno("could not read MERGE_MSG");
+	} else if (!stat(git_path(merge_msg), &statbuf)) {
+		if (strbuf_read_file(&sb, git_path(merge_msg), 0) < 0)
+			die_errno("could not read %s", merge_msg);

So now a person needs to look earlier in the file to see that merge_msg means "MERGE_MSG" rather than being something that can vary.

Yes, commit_editmsg has the same problem. If I get to choose[*] then cache.h would contain something like

	#define MERGE_HEAD_FILE "MERGE_HEAD"
	#define PREPARED_COMMIT_MESSAGE_FILE "MERGE_MSG"
	#define <description of SQUASH_MSG goes here> "SQUASH_MSG"

and commit.c or cache.h would contain something like

	#define EDITABLE_COMMIT_MESSAGE_FILE "COMMIT_EDITMSG"

and those symbolic names would be used throughout builtin code to name those files. So we would get

(1) documentation of what filenames are special to git, collected in one place
(2) protection against typos

without losing

(3) names that look like constants

Jonathan

[*] and I shouldn't! What I like should matter much less than what is right.

#13 lore raw

PATCH 1/1 Teach commit about CHERRY_PICK_HEAD

Previously the user was advised to use commit -c CHERRY_PICK_HEAD after a conflicting cherry-pick. While this would preserve the original commit's authorship, it would sadly discard cherry-pick's carefully crafted MERGE_MSG (which contains the list of conflicts as well as the original commit-id in the case of cherry-pick -x).

On the other hand, if a bare 'commit' were performed, it would preserve the MERGE_MSG while resetting the authorship.

In other words, there was no way to simultaneously take the authorship from CHERRY_PICK_HEAD and the commit message from MERGE_MSG.

This change fixes that situation. A bare 'commit' will now take the authorship from CHERRY_PICK_HEAD and the commit message from MERGE_MSG. If the user wishes to reset authorship, that must now be done explicitly via --reset-author.

A side-benefit of passing commit authorship along this way is that we can eliminate redundant authorship parsing code from revert.c.

(Also removed an unused include from revert.c)

---

Quoted 1 line · Jonathan Nieder

What have I wrought?  I think this makes the code much less readable.

I agree. Junio - this is a respin of "[PATCH v3 4/4] Teach commit about CHERRY_PICK_HEAD" with "[PATCH v3 3/4] commit.c: replace some literal strings with constants" dropped.

 Documentation/git-commit.txt    |    7 +-
 builtin/commit.c                |  155 ++++++++++++++++++++++++++++-----------
 builtin/revert.c                |   56 +--------------
 t/t3507-cherry-pick-conflict.sh |    2 +-
 t/t7509-commit.sh               |   29 +++++++
 wt-status.c                     |    4 +-
 wt-status.h                     |    9 ++-
 7 files changed, 158 insertions(+), 104 deletions(-)
Documentation/git-commit.txt +4 -3
index b586c0f..fd6a1f7 100644
@@ -84,9 +84,10 @@ OPTIONS
 	linkgit:git-rebase[1] for details.
 
 --reset-author::
-	When used with -C/-c/--amend options, declare that the
-	authorship of the resulting commit now belongs of the committer.
-	This also renews the author timestamp.
+	When used with -C/-c/--amend options, or when committing after a
+	a conflicting cherry-pick, declare that the authorship of the
+	resulting commit now belongs of the committer. This also renews
+	the author timestamp.
 
 --short::
 	When doing a dry-run, give the output in the short-format. See
builtin/commit.c +113 -42

281 lines not on this page · raw message

builtin/revert.c +1 -55

80 lines not on this page · raw message

t/t3507-cherry-pick-conflict.sh +1 -1
index ea52720..3e8d177 100755
@@ -57,7 +57,7 @@ test_expect_success 'advice from failed cherry-pick' "
 	error: could not apply \$picked... picked
 	hint: after resolving the conflicts, mark the corrected paths
 	hint: with 'git add <paths>' or 'git rm <paths>'
-	hint: and commit the result with 'git commit -c CHERRY_PICK_HEAD'
+	hint: and commit the result with 'git commit'
 	EOF
 	test_must_fail git cherry-pick picked 2>actual &&
 
t/t7509-commit.sh +29 -0
index 77b6920..b61fd3c 100755
@@ -157,4 +157,33 @@ test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
 	test_must_fail git commit -a --reset-author -m done
 '
 
+test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
+	echo "cherry-pick 1a" >>foo &&
+	test_tick &&
+	git commit -am "cherry-pick 1" --author="Cherry <[email protected]>" &&
+	git tag cherry-pick-head &&
+	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
+	echo "cherry-pick 1b" >>foo &&
+	test_tick &&
+	git commit -a &&
+	author_header cherry-pick-head >expect &&
+	author_header HEAD >actual &&
+	test_cmp expect actual &&
+
+	echo "This is a MERGE_MSG" >expect &&
+	message_body HEAD >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
+	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	echo "cherry-pick 2" >>foo &&
+	test_tick &&
+	git commit -am "cherry-pick 2" --reset-author &&
+	echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+	author_header HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_done
wt-status.c +2 -2
index 123582b..fbaaf54 100644
@@ -60,7 +60,7 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Unmerged paths:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		;
 	else if (!s->is_initial)
 		color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
@@ -77,7 +77,7 @@ static void wt_status_print_cached_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Changes to be committed:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		; /* NEEDSWORK: use "git reset --unresolve"??? */
 	else if (!s->is_initial)
 		color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
wt-status.h +8 -1
index 20b17cf..cec482a 100644
@@ -24,6 +24,13 @@ enum untracked_status_type {
 	SHOW_ALL_UNTRACKED_FILES
 };
 
+/* from where does this commit originate */
+enum commit_whence {
+	FROM_COMMIT,     /* normal */
+	FROM_MERGE,      /* commit came from merge */
+	FROM_CHERRY_PICK /* commit came from cherry-pick */
+};
+
 struct wt_status_change_data {
 	int worktree_status;
 	int index_status;
@@ -40,7 +47,7 @@ struct wt_status {
 	const char **pathspec;
 	int verbose;
 	int amend;
-	int in_merge;
+	enum commit_whence whence;
 	int nowarn;
 	int use_color;
 	int relative_paths;
Signature · 1 line
1.7.4.1.29.g21713
#14 lore raw

Re: [PATCH] Teach commit about CHERRY_PICK_HEAD

builtin/commit.c static const char empty_amend_advice[] = · line 62

Quoted 12 lines · Jay Soffian
builtin/commit.c +6 -0
@@ -54,9 +54,16 @@ static const char empty_amend_advice[] =
 "it empty. You can repeat your command with --allow-empty, or you can\n"
 "remove the commit entirely with \"git reset HEAD^\".\n";
 
+static const char empty_cherry_pick_advice[] =
+"The most recent cherry-pick is empty. If you wish to commit it, use:\n"
+"\n"
+"    git commit --allow-empty\n"
+"\n"
+"Otherwise, please remove the file %s\n";

After scratching my head a little, this seems to mean

After conflict resolution, your last cherry-pick does not introduce any changes. To commit it as an empty commit, use:\n \n
git commit --allow-empty\n
\n
Alternatively, you can cancel the cherry-pick by removing\n the file %s\n

It suspect it might be more intuitive to say

	Alternatively, you can cancel the cherry-pick by running\n
	"git reset".\n

which works because the index is known to be clean at that point.

builtin/commit.c static const char empty_amend_advice[] = · line 66

Quoted 4 lines
builtin/commit.c +4 -0
+/*
+ * The _message variables are commit names from which to take
+ * the commit message and/or authorship.
+ */

Makes sense, thanks. I'll think more about an intuitive and consistent name for these and hopefully send a patch for it later as a separate topic.

builtin/commit.c static const char empty_amend_advice[] = · line 69

Quoted 3 lines
builtin/commit.c +3 -0
+       if (whence == FROM_CHERRY_PICK && !renew_authorship) {
+               author_message = "CHERRY_PICK_HEAD";
+               author_message_buffer = read_commit_message(author_message);

I mentioned in a side thread that script authors might be happier if their carefully prepared GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL do not get overridden in this case, but I was wrong[*]. Might make sense to add a test for that:

	GIT_AUTHOR_NAME="Someone else" &&
	[email protected] &&
	export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
	git cherry-pick something &&

	git diff-tree -s --format="%an <%ae>" >actual &&
	test_cmp expect actual

(This is just a side note; the patch is good.)

In the $GIT_CHERRY_PICK_HELP set case, won't the CHERRY_PICK_HEAD behavior be harmless? rebase --interactive --continue wants to set GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL but that is only in order to copy the author identity from the original commit.

wt-status.c static void wt_status_print_unmerged_header(struct wt_status *s) · line 66

Quoted 11 lines
wt-status.c +1 -1
@@ -60,7 +60,7 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Unmerged paths:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		;
 	else if (!s->is_initial)
 		color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);

The advice 'use "git reset HEAD -- <file>..." to unstage' makes perfect sense to me in the cherry-pick case, no? The operator is replaying an existing commit, and tweaking the result amounts to pretending she made the change herself and tweaking that.

wt-status.c static void wt_status_print_cached_header(struct wt_status *s) · line 81

Quoted 7 lines
wt-status.c +1 -1
@@ -77,7 +77,7 @@ static void wt_status_print_cached_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Changes to be committed:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		; /* NEEDSWORK: use "git reset --unresolve"??? */

Likewise.

wt-status.h enum untracked_status_type { · line 32

Quoted 12 lines
wt-status.h +6 -0
@@ -24,6 +24,13 @@ enum untracked_status_type {
 	SHOW_ALL_UNTRACKED_FILES
 };
 
+/* from where does this commit originate */
+enum commit_whence {
+	FROM_COMMIT,     /* normal */
+	FROM_MERGE,      /* commit came from merge */
+	FROM_CHERRY_PICK /* commit came from cherry-pick */
+};

I think these comments are not in a place that will help a person understand the code, but I don't have the energy to go back and forth on it.

Everything not mentioned above except the --no-commit case mentioned by Junio looks good to me, though I haven't tested.

Thanks for your thoughtfulness.
Jonathan

[*]
-- 8< --
Subject: [BAD IDEA] commit: let GIT_AUTHOR_NAME/EMAIL take effect when commiting a cherry-pick

commit -c/-C/--amend to take the commit message and author from another message has always overridden the GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL variables. Letting the command line override the environment in this way is generally convenient and more intuitive if a person has forgotten what is in the environment:

GIT_AUTHOR_NAME=me
[email protected]
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL

...
git commit; # using identity from environment
...
git commit; # another commit as me
...
# commit by someone else
git commit --author='Someone Else <[email protected]>'
...
# commit by someone else
git commit -c someone-elses-commit

The new CHERRY_PICK_HEAD feature inherits the same semantics. Uncareful scripts that want to commit with a different author name and email when taking over after a failed cherry-pick use authorship from the cherry-picked commit instead of the environment:

! git cherry-pick complicated; # failed cherry-pick
... resolve the conflict ...
git commit

While that could theoretically break some scripts, it's worth it for consistency, the intuitiveness-when-user-forgets-environment issue mentioned above, and because cherry-pick itself, which internally does something like

	git cherry-pick --no-commit $1;	# simple cherry-pick
	git commit

has always ignored the GIT_AUTHOR_NAME/EMAIL environment settings.

The patch below makes commit with CHERRY_PICK_HEAD respect GIT_AUTHOR_NAME/EMAIL anyway, to demonstrate how broken that is. It breaks tests t3404 and t3506.

---
 builtin/commit.c |   59 ++++++++++++++++++++++++++++++++---------------------
 1 files changed, 36 insertions(+), 23 deletions(-)
builtin/commit.c +36 -23
index 35eb024..f398910 100644
@@ -80,6 +80,7 @@ static const char *template_file;
  * the commit message and/or authorship.
  */
 static const char *author_message, *author_message_buffer;
+static int author_message_overrides_environ;
 static char *edit_message, *use_message;
 static char *fixup_message, *squash_message;
 static int all, edit_flag, also, interactive, only, amend, signoff;
@@ -504,6 +505,38 @@ static int is_a_merge(const unsigned char *sha1)
 
 static const char sign_off_header[] = "Signed-off-by: ";
 
+static void reuse_author_info(char **name, char **email, char **date)
+{
+	const char *a, *lb, *rb, *eol;
+
+	if (author_message_overrides_environ)
+		*name = *email = *date = NULL;
+
+	a = strstr(author_message_buffer, "\nauthor ");
+	if (!a)
+		die("invalid commit: %s", author_message);
+
+	lb = strchrnul(a + strlen("\nauthor "), '<');
+	rb = strchrnul(lb, '>');
+	eol = strchrnul(rb, '\n');
+	if (!*lb || !*rb || !*eol)
+		die("invalid commit: %s", author_message);
+
+	if (!*name) {
+		if (lb == a + strlen("\nauthor "))
+			/* \nauthor <[email protected]> */
+			*name = xcalloc(1, 1);
+		else
+			*name = xmemdupz(a + strlen("\nauthor "),
+					 (lb - strlen(" ") -
+					  (a + strlen("\nauthor "))));
+	}
+	if (!*email)
+		*email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
+	if (!*date)
+		*date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
+}
+
 static void determine_author_info(struct strbuf *author_ident)
 {
 	char *name, *email, *date;
@@ -512,29 +545,8 @@ static void determine_author_info(struct strbuf *author_ident)
 	email = getenv("GIT_AUTHOR_EMAIL");
 	date = getenv("GIT_AUTHOR_DATE");
 
-	if (author_message) {
-		const char *a, *lb, *rb, *eol;
-
-		a = strstr(author_message_buffer, "\nauthor ");
-		if (!a)
-			die("invalid commit: %s", author_message);
-
-		lb = strchrnul(a + strlen("\nauthor "), '<');
-		rb = strchrnul(lb, '>');
-		eol = strchrnul(rb, '\n');
-		if (!*lb || !*rb || !*eol)
-			die("invalid commit: %s", author_message);
-
-		if (lb == a + strlen("\nauthor "))
-			/* \nauthor <[email protected]> */
-			name = xcalloc(1, 1);
-		else
-			name = xmemdupz(a + strlen("\nauthor "),
-					(lb - strlen(" ") -
-					 (a + strlen("\nauthor "))));
-		email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
-		date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
-	}
+	if (author_message)
+		reuse_author_info(&name, &email, &date);
 
 	if (force_author) {
 		const char *lb = strstr(force_author, " <");
@@ -1034,6 +1046,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
 			author_message = use_message;
 			author_message_buffer = use_message_buffer;
 		}
+		author_message_overrides_environ = 1;
 	}
 	if (whence == FROM_CHERRY_PICK && !renew_authorship) {
 		author_message = "CHERRY_PICK_HEAD";
Signature · 1 line
1.7.4.1
#15 lore raw

Re: [PATCH v3 3/4] commit.c: replace some literal strings with constants

In reply to Jay Soffian
Quoted 10 lines · Jay Soffian

A typo in any of these would be bad, so let's use
constants for them

---
I converted these per
http://article.gmane.org/gmane.comp.version-control.git/167015

Maybe this should be the last patch in the series; it's questionable to me whether it's even worth doing.

Yeah, it's horrible ;-)

builtin/commit.c static const char empty_amend_advice[] = · line 62

Quoted 6 lines
builtin/commit.c +6 -0
+static const char commit_editmsg[] = "COMMIT_EDITMSG";
+static const char cherry_pick_head[] = "CHERRY_PICK_HEAD";
+static const char merge_head[] = "MERGE_HEAD";
+static const char merge_msg[] = "MERGE_MSG";
+static const char merge_mode[] = "MERGE_MODE";
+static const char squash_msg[] = "SQUASH_MSG";

If they were like

	static const char COMMIT_EDITMSG[] = "...";

then that would give us typo-safety while keeping the similarity from original code. Uppercase variable names are a bit weird, though.

#16 lore raw

PATCH v3 4/4 Teach commit about CHERRY_PICK_HEAD

In reply to Jay Soffian

Previously the user was advised to use commit -c CHERRY_PICK_HEAD after a conflicting cherry-pick. While this would preserve the original commit's authorship, it would sadly discard cherry-pick's carefully crafted MERGE_MSG (which contains the list of conflicts as well as the original commit-id in the case of cherry-pick -x).

On the other hand, if a bare 'commit' were performed, it would preserve the MERGE_MSG while resetting the authorship.

In other words, there was no way to simultaneously take the authorship from CHERRY_PICK_HEAD and the commit message from MERGE_MSG.

This change fixes that situation. A bare 'commit' will now take the authorship from CHERRY_PICK_HEAD and the commit message from MERGE_MSG. If the user wishes to reset authorship, that must now be done explicitly via --reset-author.

A side-benefit of passing commit authorship along this way is that we can eliminate redundant authorship parsing code from revert.c.

(Also removed an unused include from revert.c)

---
Incorporates feedback from:
- http://article.gmane.org/gmane.comp.version-control.git/167000
- http://article.gmane.org/gmane.comp.version-control.git/167015

 Documentation/git-commit.txt    |    7 +-
 builtin/commit.c                |  155 ++++++++++++++++++++++++++++-----------
 builtin/revert.c                |   56 +--------------
 t/t3507-cherry-pick-conflict.sh |    2 +-
 t/t7509-commit.sh               |   29 +++++++
 wt-status.c                     |    4 +-
 wt-status.h                     |    9 ++-
 7 files changed, 158 insertions(+), 104 deletions(-)
Documentation/git-commit.txt +4 -3
index b586c0f..fd6a1f7 100644
@@ -84,9 +84,10 @@ OPTIONS
 	linkgit:git-rebase[1] for details.
 
 --reset-author::
-	When used with -C/-c/--amend options, declare that the
-	authorship of the resulting commit now belongs of the committer.
-	This also renews the author timestamp.
+	When used with -C/-c/--amend options, or when committing after a
+	a conflicting cherry-pick, declare that the authorship of the
+	resulting commit now belongs of the committer. This also renews
+	the author timestamp.
 
 --short::
 	When doing a dry-run, give the output in the short-format. See
builtin/commit.c +113 -42

285 lines not on this page · raw message

builtin/revert.c +1 -55

80 lines not on this page · raw message

t/t3507-cherry-pick-conflict.sh +1 -1
index ea52720..3e8d177 100755
@@ -57,7 +57,7 @@ test_expect_success 'advice from failed cherry-pick' "
 	error: could not apply \$picked... picked
 	hint: after resolving the conflicts, mark the corrected paths
 	hint: with 'git add <paths>' or 'git rm <paths>'
-	hint: and commit the result with 'git commit -c CHERRY_PICK_HEAD'
+	hint: and commit the result with 'git commit'
 	EOF
 	test_must_fail git cherry-pick picked 2>actual &&
 
t/t7509-commit.sh +29 -0
index 77b6920..b61fd3c 100755
@@ -157,4 +157,33 @@ test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
 	test_must_fail git commit -a --reset-author -m done
 '
 
+test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
+	echo "cherry-pick 1a" >>foo &&
+	test_tick &&
+	git commit -am "cherry-pick 1" --author="Cherry <[email protected]>" &&
+	git tag cherry-pick-head &&
+	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
+	echo "cherry-pick 1b" >>foo &&
+	test_tick &&
+	git commit -a &&
+	author_header cherry-pick-head >expect &&
+	author_header HEAD >actual &&
+	test_cmp expect actual &&
+
+	echo "This is a MERGE_MSG" >expect &&
+	message_body HEAD >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
+	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	echo "cherry-pick 2" >>foo &&
+	test_tick &&
+	git commit -am "cherry-pick 2" --reset-author &&
+	echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+	author_header HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_done
wt-status.c +2 -2
index 123582b..fbaaf54 100644
@@ -60,7 +60,7 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Unmerged paths:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		;
 	else if (!s->is_initial)
 		color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
@@ -77,7 +77,7 @@ static void wt_status_print_cached_header(struct wt_status *s)
 	color_fprintf_ln(s->fp, c, "# Changes to be committed:");
 	if (!advice_status_hints)
 		return;
-	if (s->in_merge)
+	if (s->whence != FROM_COMMIT)
 		; /* NEEDSWORK: use "git reset --unresolve"??? */
 	else if (!s->is_initial)
 		color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
wt-status.h +8 -1
index 20b17cf..cec482a 100644
@@ -24,6 +24,13 @@ enum untracked_status_type {
 	SHOW_ALL_UNTRACKED_FILES
 };
 
+/* from where does this commit originate */
+enum commit_whence {
+	FROM_COMMIT,     /* normal */
+	FROM_MERGE,      /* commit came from merge */
+	FROM_CHERRY_PICK /* commit came from cherry-pick */
+};
+
 struct wt_status_change_data {
 	int worktree_status;
 	int index_status;
@@ -40,7 +47,7 @@ struct wt_status {
 	const char **pathspec;
 	int verbose;
 	int amend;
-	int in_merge;
+	enum commit_whence whence;
 	int nowarn;
 	int use_color;
 	int relative_paths;
Signature · 1 line
1.7.4.1.30.g7fe09