Skip to contentSkip to conversation

Teach merge the '[-e|--edit]' option

PATCH In review Jay Soffian 3 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Teach merge the '[-e|--edit]' option

Implemented internally instead of as "git merge --no-commit && git commit" so that "merge --edit" is otherwise consistent (hooks, etc) with "merge".

Note: the edit message does not include the status information that one gets with "commit --status" and it is cleaned up after editing like one gets with "commit --cleanup=default". A later patch could add the status information if desired.

Note: previously we were not calling stripspace() after running the prepare-commit-msg hook. Now we are, stripping comments and leading/trailing whitespace lines if --edit is given, otherwise only stripping leading/trailing whitespace lines if not given --edit.

---
I probably can't spend more time on this patch any time soon. Hopefully this iteration is close enough. If not, maybe Todd can help.

 Documentation/merge-options.txt |    6 ++
 builtin/merge.c                 |  109 +++++++++++++++++++++++++--------------
 t/t7600-merge.sh                |   15 +++++
 3 files changed, 91 insertions(+), 39 deletions(-)
Documentation/merge-options.txt +6 -0
index b613d4ed08..6bd0b041c3 100644
@@ -7,6 +7,12 @@ With --no-commit perform the merge but pretend the merge
 failed and do not autocommit, to give the user a chance to
 inspect and further tweak the merge result before committing.
 
+--edit::
+-e::
++
+	Invoke editor before committing successful merge to further
+	edit the default merge message.
+
 --ff::
 --no-ff::
 	Do not generate a merge commit if the merge resolved as
builtin/merge.c +70 -39

168 lines not on this page · raw message

t/t7600-merge.sh +15 -0
index 87aac835a1..8c6b811718 100755
@@ -643,4 +643,19 @@ test_expect_success 'amending no-ff merge commit' '
 
 test_debug 'git log --graph --decorate --oneline --all'
 
+cat >editor <<\EOF
+#!/bin/sh
+# strip comments and blank lines from end of message
+sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
+EOF
+chmod 755 editor
+
+test_expect_success 'merge --no-ff --edit' '
+	git reset --hard c0 &&
+	EDITOR=./editor git merge --no-ff --edit c1 &&
+	verify_parents $c0 $c1 &&
+	git cat-file commit HEAD | sed "1,/^$/d" > actual &&
+	test_cmp actual expected
+'
+
 test_done
Signature · 1 line
1.7.7.147.g4f6dc9
#1 lore raw
Quoted 8 lines · Jay Soffian

Implemented internally instead of as "git merge --no-commit && git commit" so that "merge --edit" is otherwise consistent (hooks, etc) with "merge".

Note: the edit message does not include the status information that one gets with "commit --status" and it is cleaned up after editing like one gets with "commit --cleanup=default". A later patch could add the status information if desired.

Quoted 7 lines

Note: previously we were not calling stripspace() after running the prepare-commit-msg hook. Now we are, stripping comments and leading/trailing whitespace lines if --edit is given, otherwise only stripping leading/trailing whitespace lines if not given --edit.

---

Thanks.

Quoted 11 lines
(quoted diff) +8 -1
+static void prepare_to_commit(void)
+{
+	struct strbuf msg = STRBUF_INIT;
+	strbuf_addbuf(&msg, &merge_msg);
+	strbuf_addch(&msg, '\n');
+	write_merge_msg(&msg);
 	run_hook(get_index_file(), "prepare-commit-msg",
 		 git_path("MERGE_MSG"), "merge", NULL, NULL);
-	read_merge_msg();
+	if (option_edit) {
+		if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))

Review

Quoted 10 lines
(quoted diff) +9 -0
+			abort_commit(NULL);
+	}
+	read_merge_msg(&msg);
+	stripspace(&msg, option_edit);
+	if (!msg.len)
+		abort_commit(_("Empty commit message."));
+	strbuf_release(&merge_msg);
+	strbuf_addbuf(&merge_msg, &msg);
+	strbuf_release(&msg);
 }

An abstraction very nicely done.

I am not sure about the '\n' you unconditionally added at the end of the existing message.

I think running stripspace(&msg, option_edit) is a good change, even though some people might feel it is a regression. "git commit" also cleans up the whitespace cruft left by prepare-commit-message hook when the editor is not in use, and this change makes it consistent.

Quoted 21 lines
(quoted diff) +19 -0
@@ -1015,6 +1041,36 @@ static int setup_with_upstream(const char ***argv)
...
+static void write_merge_state(void)
+{
+	int fd;
+	struct commit_list *j;
+	struct strbuf buf = STRBUF_INIT;
+
+	for (j = remoteheads; j; j = j->next)
+		strbuf_addf(&buf, "%s\n",
+			sha1_to_hex(j->item->object.sha1));
+	fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
+	if (fd < 0)
+		die_errno(_("Could not open '%s' for writing"),
+			  git_path("MERGE_HEAD"));
+	if (write_in_full(fd, buf.buf, buf.len) != buf.len)
+		die_errno(_("Could not write to '%s'"), git_path("MERGE_HEAD"));
+	close(fd);
+	strbuf_addch(&merge_msg, '\n');
+	write_merge_msg(&merge_msg);
+	fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);

Review static int setup_with_upstream(const char ***argv)

Quoted 10 lines
(quoted diff) +10 -0
+	if (fd < 0)
+		die_errno(_("Could not open '%s' for writing"),
+			  git_path("MERGE_MODE"));
+	strbuf_reset(&buf);
+	if (!allow_fast_forward)
+		strbuf_addf(&buf, "no-ff");
+	if (write_in_full(fd, buf.buf, buf.len) != buf.len)
+		die_errno(_("Could not write to '%s'"), git_path("MERGE_MODE"));
+	close(fd);
+}

Again very nicely done.

Quoted 5 lines
t/t7600-merge.sh +0 -0
index 87aac835a1..8c6b811718 100755

t/t7600-merge.sh test_expect_success 'amending no-ff merge commit' ' · line 651

Quoted 9 lines
t/t7600-merge.sh +6 -0
@@ -643,4 +643,19 @@ test_expect_success 'amending no-ff merge commit' '
 
 test_debug 'git log --graph --decorate --oneline --all'
 
+cat >editor <<\EOF
+#!/bin/sh
+# strip comments and blank lines from end of message
+sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
+EOF
+chmod 755 editor

I am not sure about this one. Wouldn't this want to be editing the given file to make sure that the edited content appear in the result, not just testing the additional stripspace() call you added in the codepath?

t/t7600-merge.sh test_expect_success 'amending no-ff merge commit' ' · line 660

Quoted 9 lines
t/t7600-merge.sh +8 -0
+test_expect_success 'merge --no-ff --edit' '
+	git reset --hard c0 &&
+	EDITOR=./editor git merge --no-ff --edit c1 &&
+	verify_parents $c0 $c1 &&
+	git cat-file commit HEAD | sed "1,/^$/d" > actual &&
+	test_cmp actual expected
+'
+
 test_done

So perhaps this one on top? I am just suspecting that your additional '\n' is to make sure we do not write out a file with an incomplete line with this patch, but that change is not explained in your commit log message, so I am not sure.

 builtin/merge.c  |    3 ++-
 t/t7600-merge.sh |   10 +++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)
builtin/merge.c +2 -1
index a8dbf4a..09ffc07 100644
@@ -867,7 +867,8 @@ static void prepare_to_commit(void)
 {
 	struct strbuf msg = STRBUF_INIT;
 	strbuf_addbuf(&msg, &merge_msg);
-	strbuf_addch(&msg, '\n');
+	if (msg.len && msg.buf[msg.len-1] != '\n')
+		strbuf_addch(&msg, '\n');
 	write_merge_msg(&msg);
 	run_hook(get_index_file(), "prepare-commit-msg",
 		 git_path("MERGE_MSG"), "merge", NULL, NULL);
t/t7600-merge.sh +9 -1
index 8c6b811..3008e4e 100755
@@ -645,6 +645,12 @@ test_debug 'git log --graph --decorate --oneline --all'
 
 cat >editor <<\EOF
 #!/bin/sh
+# Add a new message string that was not in the template
+(
+	echo "Merge work done on the side branch c1"
+	echo
+	cat <"$1"
+) >"$1.tmp" && mv "$1.tmp" "$1"
 # strip comments and blank lines from end of message
 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
 EOF
@@ -654,7 +660,9 @@ test_expect_success 'merge --no-ff --edit' '
 	git reset --hard c0 &&
 	EDITOR=./editor git merge --no-ff --edit c1 &&
 	verify_parents $c0 $c1 &&
-	git cat-file commit HEAD | sed "1,/^$/d" > actual &&
+	git cat-file commit HEAD >raw &&
+	grep "work done on the side branch" raw &&
+	sed "1,/^$/d" >actual raw &&
 	test_cmp actual expected
 '
 
#2 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 15 lines · Jay Soffian
(quoted diff) +6 -0
+static void prepare_to_commit(void)
+{
+     struct strbuf msg = STRBUF_INIT;
+     strbuf_addbuf(&msg, &merge_msg);
+     strbuf_addch(&msg, '\n');
+     write_merge_msg(&msg);

      run_hook(get_index_file(), "prepare-commit-msg", git_path("MERGE_MSG"), "merge", NULL, NULL);
-     read_merge_msg();
+     if (option_edit) {
+             if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
+                     abort_commit(NULL);
+     }
+     read_merge_msg(&msg);
+     stripspace(&msg, option_edit);

Quoted 8 lines
Quoted 6 lines

+     if (!msg.len)
+             abort_commit(_("Empty commit message."));
+     strbuf_release(&merge_msg);
+     strbuf_addbuf(&merge_msg, &msg);
+     strbuf_release(&msg);
 }

An abstraction very nicely done.

<blush> :-)

Quoted 2 lines

I am not sure about the '\n' you unconditionally added at the end of the existing message.

Right, the old code does that when the merge fails, counting on (I think) git-commit to then take care of any extra newlines. My reasoning was tack it on before running prepare-commit-msg, then run stripspace() after the hook and and editor, which will take care of any excess newlines. I guess this would be a regression if someone's prepare-commit-msg hook blindly appends to the commit message.

Quoted 4 lines

I think running stripspace(&msg, option_edit) is a good change, even though some people might feel it is a regression. "git commit" also cleans up the whitespace cruft left by prepare-commit-message hook when the editor is not in use, and this change makes it consistent.

Correct.

Quoted 8 lines
Quoted 8 lines
t/t7600-merge.sh +0 -0
index 87aac835a1..8c6b811718 100755
@@ -643,4 +643,19 @@ test_expect_success 'amending no-ff merge commit' '
 

 test_debug 'git log --graph --decorate --oneline --all'

Quoted 10 lines

t/t7600-merge.sh

Quoted 6 lines
t/t7600-merge.sh +6 -0
+cat >editor <<\EOF
+#!/bin/sh
+# strip comments and blank lines from end of message
+sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
+EOF
+chmod 755 editor

I am not sure about this one. Wouldn't this want to be editing the given file to make sure that the edited content appear in the result, not just testing the additional stripspace() call you added in the codepath?

Yep.

I added this test under the previous iteration of the patch when I was concerned that commit message make it through the external commit code path correctly. It doesn't really make sense with this iteration now that I think about it. The part about stripping comments and newlines is no longer needed.

Quoted 6 lines
Quoted 6 lines

+test_expect_success 'merge --no-ff --edit' '
+     git reset --hard c0 &&
+     EDITOR=./editor git merge --no-ff --edit c1 &&
+     verify_parents $c0 $c1 &&
+     git cat-file commit HEAD | sed "1,/^$/d" > actual &&
+     test_cmp actual expected

Quoted 8 lines
Quoted 3 lines

+'
+
 test_done

So perhaps this one on top? I am just suspecting that your additional '\n' is to make sure we do not write out a file with an incomplete line with this patch, but that change is not explained in your commit log message, so I am not sure.

I assumed the '\n' was needed as it's added (unconditionally) before writing MERGE_MSG when the merge fails. I didn't notice that when I added the prepare-commit-msg hook support to merge.c (65969d43d1).

Quoted 9 lines

 builtin/merge.c  |    3 ++-
 t/t7600-merge.sh |   10 +++++++++-

 2 files changed, 11 insertions(+), 2 deletions(-)
builtin/merge.c +0 -0
index a8dbf4a..09ffc07 100644

builtin/merge.c static void prepare_to_commit(void) · line 874

Quoted 9 lines
builtin/merge.c +2 -1
@@ -867,7 +867,8 @@ static void prepare_to_commit(void)
  {
        struct strbuf msg = STRBUF_INIT;
        strbuf_addbuf(&msg, &merge_msg);
-       strbuf_addch(&msg, '\n');
+       if (msg.len && msg.buf[msg.len-1] != '\n')
+               strbuf_addch(&msg, '\n');
        write_merge_msg(&msg);
        run_hook(get_index_file(), "prepare-commit-msg",
                 git_path("MERGE_MSG"), "merge", NULL, NULL);

I'm guessing the '\n' is always needed (per above), but I'm not sure.

Quoted 19 lines
t/t7600-merge.sh +0 -0
index 8c6b811..3008e4e 100755
@@ -645,6 +645,12 @@ test_debug 'git log --graph --decorate --oneline --all'
 

 cat >editor <<\EOF
 #!/bin/sh

t/t7600-merge.sh +6 -0
+# Add a new message string that was not in the template
+(
+       echo "Merge work done on the side branch c1"
+       echo
+       cat <"$1"
+) >"$1.tmp" && mv "$1.tmp" "$1"
 # strip comments and blank lines from end of message
 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
 EOF
t/t7600-merge.sh +0 -0
@@ -654,7 +660,9 @@ test_expect_success 'merge --no-ff --edit' '

       git reset --hard c0 &&

Quoted 8 lines

       EDITOR=./editor git merge --no-ff --edit c1 && verify_parents $c0 $c1 &&
-       git cat-file commit HEAD | sed "1,/^$/d" > actual &&
+       git cat-file commit HEAD >raw &&
+       grep "work done on the side branch" raw &&
+       sed "1,/^$/d" >actual raw &&
       test_cmp actual expected
 '

Okay. A test that the merge is aborted if the message is empty would also be good.

I'll try to find time to send another iteration with better tests. May not be till next week though.

j.

#3 lore raw
Quoted 5 lines · Jay Soffian
Quoted 2 lines

I am not sure about the '\n' you unconditionally added at the end of the existing message.

Right, the old code does that when the merge fails, counting on (I think) git-commit to then take care of any extra newlines.

Ahh, that explains it. I was originally about to suggest running the stripspace() only when we run the editor, and saw a failure from an unrelated test and realized that running stripspace() on the result of prepare-commit-msg is the right thing to do after all, because that is what is done by "git commit". Yes, the current code does rely on the stripspace to remove it, so there is no need to make it conditional.

So if we drop the "conditionally add '\n'" part in builtin/merge.c from my "how about this on top" patch and we should be ready to go, right?

Thanks.

#4 lore raw
Quoted 2 lines · Junio C Hamano

So if we drop the "conditionally add '\n'" part in builtin/merge.c from my "how about this on top" patch and we should be ready to go, right?

Yes. I can send a followup patch adding an additional test case next week (for the case where the editor zeros out the message).

Thanks Junio!

j.

#5 lore raw
In reply to Jay Soffian

Jay Soffian:

Documentation/merge-options.txt With --no-commit perform the merge but pretend the merge · line 14

Quoted 5 lines
+--edit::
+-e::

++

Documentation/merge-options.txt +2 -0
+	Invoke editor before committing successful merge to further
+	edit the default merge message.

I have a feature request, and that is to also add a configuration option to make this the default behaviour.

Signature · 1 line
\\// Peter - http://www.softwolves.pp.se/