merge: allow --log to append shortlog to -m
cover merge: allow --log to append shortlog to -m
Previously, --log didn't have any effect if a message was specified with -m. This allows one to create commit messages like:
Merge in new feature
* commit c2:
c2's message
Contents:
[PATCH 1/8] t7604-merge-custom-message: shift expected output creation [PATCH 2/8] t7604-merge-custom-message: show that --log doesn't append to -m [PATCH 3/8] merge: update comment
[PATCH 4/8] merge: rename variable
[PATCH 5/8] fmt-merge-msg: minor refactor of fmt_merge_msg() [PATCH 6/8] fmt-merge-msg: refactor merge title formatting [PATCH 7/8] fmt-merge-msg: add function to append shortlog only [PATCH 8/8] merge: --log appends shortlog to message if specified
Documentation/git-merge.txt | 7 +++- builtin.h | 1 + builtin/fmt-merge-msg.c | 76 ++++++++++++++++++++++++-------------- builtin/merge.c | 20 ++++++---- t/t7604-merge-custom-message.sh | 24 ++++++++++-- 5 files changed, 87 insertions(+), 41 deletions(-)
PATCH 1/8 t7604-merge-custom-message: shift expected output creation
Squash in a minor rename too.
--- t/t7604-merge-custom-message.sh | 12 ++++++++---- 1 files changed, 8 insertions(+), 4 deletions(-)
t/t7604-merge-custom-message.sh +8 -4
@@ -6,6 +6,10 @@ Testing merge when using a custom message for the merge commit.' . ./test-lib.sh +create_merge_msgs() { + echo >exp.subject "custom message" +} + test_expect_success 'setup' ' echo c0 > c0.c && git add c0.c &&
@@ -19,16 +23,16 @@ test_expect_success 'setup' ' echo c2 > c2.c && git add c2.c && git commit -m c2 && - git tag c2 + git tag c2 && + create_merge_msgs ' test_expect_success 'merge c2 with a custom message' ' git reset --hard c1 && - echo >expected "custom message" && - git merge -m "custom message" c2 && + git merge -m "$(cat exp.subject)" c2 && git cat-file commit HEAD | sed -e "1,/^$/d" >actual && - test_cmp expected actual + test_cmp exp.subject actual ' test_done
Signature · 1 line
1.7.1.189.g07419
PATCH 2/8 t7604-merge-custom-message: show that --log doesn't append to -m
In reply to Tay Ray Chuan--- t/t7604-merge-custom-message.sh | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-)
t/t7604-merge-custom-message.sh +12 -0
@@ -8,6 +8,11 @@ Testing merge when using a custom message for the merge commit.' create_merge_msgs() { echo >exp.subject "custom message" + + cp exp.subject exp.log && + echo >>exp.log "" && + echo >>exp.log "* commit 'c2':" && + echo >>exp.log " c2" } test_expect_success 'setup' '
@@ -35,4 +40,11 @@ test_expect_success 'merge c2 with a custom message' ' test_cmp exp.subject actual ' +test_expect_failure 'merge --log appends to custom message' ' + git reset --hard c1 && + git merge --log -m "$(cat exp.subject)" c2 && + git cat-file commit HEAD | sed -e "1,/^$/d" >actual && + test_cmp exp.log actual +' + test_done
Signature · 1 line
1.7.1.189.g07419
PATCH 3/8 merge: update comment
In reply to Tay Ray Chuance9d823 (merge: do not add standard message when message is given with -m option) changed the behaviour of the code that the comment addressed, but the comment was not similarly updated.
Fix this.
--- builtin/merge.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
builtin/merge.c +1 -1
@@ -990,7 +990,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)/* * All the rest are the commits being merged; * prepare the standard merge summary message to -* be appended to the given message. If remote +* used as the merge message. If remote * is invalid we will die later in the common * codepath so we discard the error in this * loop.
Signature · 1 line
1.7.1.189.g07419
PATCH 4/8 merge: rename variable
In reply to Tay Ray ChuanIt is more accurate to call it 'merge_names' instead of 'msg', as it does not contain the final message.
--- builtin/merge.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
builtin/merge.c +3 -3
@@ -982,7 +982,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)reset_hard (remote_head->sha1,0 );return 0 ; }else { -struct strbuf msg =STRBUF_INIT ; +struct strbuf merge_names =STRBUF_INIT ;/* We are invoked directly as the first-class UI. */ head_arg ="HEAD" ;
@@ -997,8 +997,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)*/ if (!have_message) {for (i =0 ; i < argc; i++) -merge_name (argv[i], &msg); -fmt_merge_msg (option_log, &msg, &merge_msg); +merge_name (argv[i], &merge_names); +fmt_merge_msg (option_log, &merge_names, &merge_msg);if (merge_msg.len)strbuf_setlen (&merge_msg, merge_msg.len-1 ); }
Signature · 1 line
1.7.1.189.g07419
PATCH 5/8 fmt-merge-msg: minor refactor of fmt_merge_msg()
In reply to Tay Ray ChuanShift implementation into a private function, do_fmt_merge_msg(). This allows for further changes to the implementation, without affecting the interface.
--- builtin/fmt-merge-msg.c | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-)
builtin/fmt-merge-msg.c +6 -1
@@ -202,7 +202,8 @@ static void shortlog(const char *name, unsigned char *sha1,string_list_clear (&subjects,0 ); } -int fmt_merge_msg (int merge_summary,struct strbuf *in,struct strbuf *out) { +static int do_fmt_merge_msg (int merge_summary,struct strbuf *in, +struct strbuf *out) {int limit =20 , i =0 , pos =0 ;char *sep ="" ;unsigned char head_sha1[20 ];
@@ -296,6 +297,10 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {return 0 ; } +int fmt_merge_msg (int merge_summary,struct strbuf *in,struct strbuf *out) { +return do_fmt_merge_msg (merge_summary, in, out); +} +int cmd_fmt_merge_msg (int argc,const char **argv,const char *prefix) {const char *inpath =NULL ;
Signature · 1 line
1.7.1.189.g07419
PATCH 6/8 fmt-merge-msg: refactor merge title formatting
In reply to Tay Ray Chuan--- builtin/fmt-merge-msg.c | 65 ++++++++++++++++++++++++++--------------------- 1 files changed, 36 insertions(+), 29 deletions(-)
builtin/fmt-merge-msg.c +36 -29
@@ -202,36 +202,10 @@ static void shortlog(const char *name, unsigned char *sha1,string_list_clear (&subjects,0 ); } -static int do_fmt_merge_msg (int merge_summary,struct strbuf *in, -struct strbuf *out) { -int limit =20 , i =0 , pos =0 ; +static void do_fmt_merge_msg_title (struct strbuf *out, +const char *current_branch) { +int i =0 ;char *sep ="" ; -unsigned char head_sha1[20 ]; -const char *current_branch; - -/* get current branch */ - current_branch =resolve_ref ("HEAD" , head_sha1,1 ,NULL ); -if (!current_branch) -die ("No current branch" ); -if (!prefixcmp (current_branch,"refs/heads/" )) - current_branch +=11 ; - -/* get a line */ -while (pos < in->len) { -int len; -char *newline, *p = in->buf + pos; - - newline =strchr (p,'\n' ); - len = newline ? newline - p :strlen (p); - pos += len + !!newline; - i++; - p[len] =0 ; -if (handle_line (p)) -die ("Error in line %d: %.*s" , i, len, p); - } - -if (!srcs.nr) -return 0 ;strbuf_addstr (out,"Merge " );for (i =0 ; i < srcs.nr; i++) {
@@ -279,6 +253,39 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,strbuf_addch (out,'\n' ); else strbuf_addf(out, " into %s\n", current_branch); +} + +static int do_fmt_merge_msg (int merge_summary,struct strbuf *in, +struct strbuf *out) { +int limit =20 , i =0 , pos =0 ; +unsigned char head_sha1[20 ]; +const char *current_branch; + +/* get current branch */ + current_branch =resolve_ref ("HEAD" , head_sha1,1 ,NULL ); +if (!current_branch) +die ("No current branch" ); +if (!prefixcmp (current_branch,"refs/heads/" )) + current_branch +=11 ; + +/* get a line */ +while (pos < in->len) { +int len; +char *newline, *p = in->buf + pos; + + newline =strchr (p,'\n' ); + len = newline ? newline - p :strlen (p); + pos += len + !!newline; + i++; + p[len] =0 ; +if (handle_line (p)) +die ("Error in line %d: %.*s" , i, len, p); + } + +if (!srcs.nr) +return 0 ; + +do_fmt_merge_msg_title (out, current_branch);if (merge_summary) {struct commit *head;
Signature · 1 line
1.7.1.189.g07419
PATCH 7/8 fmt-merge-msg: add function to append shortlog only
In reply to Tay Ray Chuan--- builtin.h | 1 + builtin/fmt-merge-msg.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
builtin.h +1 -0
@@ -16,6 +16,7 @@ extern const char *help_unknown_cmd(const char *cmd);extern void prune_packed_objects (int );extern int fmt_merge_msg (int merge_summary,struct strbuf *in,struct strbuf *out); +extern int fmt_merge_msg_shortlog (struct strbuf *in,struct strbuf *out);extern int commit_notes (struct notes_tree *t,const char *msg);struct notes_rewrite_cfg {
builtin/fmt-merge-msg.c +9 -4
@@ -255,8 +255,8 @@ static void do_fmt_merge_msg_title(struct strbuf *out,strbuf_addf (out," into %s\n" , current_branch); } -static int do_fmt_merge_msg (int merge_summary,struct strbuf *in, -struct strbuf *out) { +static int do_fmt_merge_msg (int merge_title,int merge_summary, +struct strbuf *in,struct strbuf *out) {int limit =20 , i =0 , pos =0 ;unsigned char head_sha1[20 ];const char *current_branch;
@@ -285,7 +285,8 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in,if (!srcs.nr)return 0 ; -do_fmt_merge_msg_title (out, current_branch); +if (merge_title) +do_fmt_merge_msg_title (out, current_branch);if (merge_summary) {struct commit *head;
@@ -305,7 +306,11 @@ static int do_fmt_merge_msg(int merge_summary, struct strbuf *in, }int fmt_merge_msg (int merge_summary,struct strbuf *in,struct strbuf *out) { -return do_fmt_merge_msg (merge_summary, in, out); +return do_fmt_merge_msg (1 , merge_summary, in, out); +} + +int fmt_merge_msg_shortlog (struct strbuf *in,struct strbuf *out) { +return do_fmt_merge_msg (0 ,1 , in, out); }int cmd_fmt_merge_msg (int argc,const char **argv,const char *prefix)
Signature · 1 line
1.7.1.189.g07419
PATCH 8/8 merge: --log appends shortlog to message if specified
In reply to Tay Ray ChuanWhen the user specifies a message, use fmt_merge_msg_shortlog() to append the shortlog.
Previously, when a message was specified, we ignored the merge title ("Merge <foo> into <bar>") and shortlog from fmt_merge_msg().
Update the documentation for -m to reflect this too.
--- Documentation/git-merge.txt | 7 ++++++- builtin/fmt-merge-msg.c | 3 +++ builtin/merge.c | 18 +++++++++++------- t/t7604-merge-custom-message.sh | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-)
Documentation/git-merge.txt +6 -1
@@ -58,7 +58,12 @@ include::merge-options.txt[] -m <msg>:: Set the commit message to be used for the merge commit (in - case one is created). The 'git fmt-merge-msg' command can be + case one is created). + + If `--log` is specified, a shortlog of the commits being merged + will be appended to the specified message. + + The 'git fmt-merge-msg' command can be used to give a good default for automated 'git merge' invocations.
builtin/fmt-merge-msg.c +3 -0
@@ -298,6 +298,9 @@ static int do_fmt_merge_msg(int merge_title, int merge_summary, rev.ignore_merges =1 ; rev.limited =1 ; +if (suffixcmp (out->buf,"\n" )) +strbuf_addch (out,'\n' ); +for (i =0 ; i < origins.nr; i++)shortlog (origins.items[i].string, origins.items[i].util, head, &rev, limit, out);
builtin/merge.c +11 -7
@@ -990,18 +990,22 @@ int cmd_merge(int argc, const char **argv, const char *prefix)/* * All the rest are the commits being merged; * prepare the standard merge summary message to -* used as the merge message. If remote +* be appended to the given message. If remote * is invalid we will die later in the common * codepath so we discard the error in this * loop. */ -if (!have_message) { -for (i =0 ; i < argc; i++) -merge_name (argv[i], &merge_names); +for (i =0 ; i < argc; i++) +merge_name (argv[i], &merge_names); + +if (have_message && option_log) +fmt_merge_msg_shortlog (&merge_names, &merge_msg); +else if (!have_message)fmt_merge_msg (option_log, &merge_names, &merge_msg); -if (merge_msg.len) -strbuf_setlen (&merge_msg, merge_msg.len-1 ); - } + + +if (!(have_message && !option_log) && merge_msg.len) +strbuf_setlen (&merge_msg, merge_msg.len-1 ); }if (head_invalid || !argc)
t/t7604-merge-custom-message.sh +1 -1
@@ -40,7 +40,7 @@ test_expect_success 'merge c2 with a custom message' ' test_cmp exp.subject actual ' -test_expect_failure 'merge --log appends to custom message' ' +test_expect_success 'merge --log appends to custom message' ' git reset --hard c1 && git merge --log -m "$(cat exp.subject)" c2 && git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
Signature · 1 line
1.7.1.189.g07419