Skip to content

Commit e3ed7f7

Browse files
committed
Merge branch 'tc/merge-m-log' into maint
* tc/merge-m-log: merge: --log appends shortlog to message if specified fmt-merge-msg: add function to append shortlog only fmt-merge-msg: refactor merge title formatting fmt-merge-msg: minor refactor of fmt_merge_msg() merge: rename variable merge: update comment t7604-merge-custom-message: show that --log doesn't append to -m t7604-merge-custom-message: shift expected output creation
2 parents 958ff4a + f0ecac2 commit e3ed7f7

File tree

5 files changed

+87
-41
lines changed

5 files changed

+87
-41
lines changed

Documentation/git-merge.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ include::merge-options.txt[]
5858

5959
-m <msg>::
6060
Set the commit message to be used for the merge commit (in
61-
case one is created). The 'git fmt-merge-msg' command can be
61+
case one is created).
62+
63+
If `--log` is specified, a shortlog of the commits being merged
64+
will be appended to the specified message.
65+
66+
The 'git fmt-merge-msg' command can be
6267
used to give a good default for automated 'git merge'
6368
invocations.
6469

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern const char *help_unknown_cmd(const char *cmd);
1616
extern void prune_packed_objects(int);
1717
extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
1818
struct strbuf *out);
19+
extern int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out);
1920
extern int commit_tree(const char *msg, unsigned char *tree,
2021
struct commit_list *parents, unsigned char *ret,
2122
const char *author);

builtin/fmt-merge-msg.c

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -202,35 +202,10 @@ static void shortlog(const char *name, unsigned char *sha1,
202202
string_list_clear(&subjects, 0);
203203
}
204204

205-
int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
206-
int limit = 20, i = 0, pos = 0;
205+
static void do_fmt_merge_msg_title(struct strbuf *out,
206+
const char *current_branch) {
207+
int i = 0;
207208
char *sep = "";
208-
unsigned char head_sha1[20];
209-
const char *current_branch;
210-
211-
/* get current branch */
212-
current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
213-
if (!current_branch)
214-
die("No current branch");
215-
if (!prefixcmp(current_branch, "refs/heads/"))
216-
current_branch += 11;
217-
218-
/* get a line */
219-
while (pos < in->len) {
220-
int len;
221-
char *newline, *p = in->buf + pos;
222-
223-
newline = strchr(p, '\n');
224-
len = newline ? newline - p : strlen(p);
225-
pos += len + !!newline;
226-
i++;
227-
p[len] = 0;
228-
if (handle_line(p))
229-
die ("Error in line %d: %.*s", i, len, p);
230-
}
231-
232-
if (!srcs.nr)
233-
return 0;
234209

235210
strbuf_addstr(out, "Merge ");
236211
for (i = 0; i < srcs.nr; i++) {
@@ -278,6 +253,40 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
278253
strbuf_addch(out, '\n');
279254
else
280255
strbuf_addf(out, " into %s\n", current_branch);
256+
}
257+
258+
static int do_fmt_merge_msg(int merge_title, int merge_summary,
259+
struct strbuf *in, struct strbuf *out) {
260+
int limit = 20, i = 0, pos = 0;
261+
unsigned char head_sha1[20];
262+
const char *current_branch;
263+
264+
/* get current branch */
265+
current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
266+
if (!current_branch)
267+
die("No current branch");
268+
if (!prefixcmp(current_branch, "refs/heads/"))
269+
current_branch += 11;
270+
271+
/* get a line */
272+
while (pos < in->len) {
273+
int len;
274+
char *newline, *p = in->buf + pos;
275+
276+
newline = strchr(p, '\n');
277+
len = newline ? newline - p : strlen(p);
278+
pos += len + !!newline;
279+
i++;
280+
p[len] = 0;
281+
if (handle_line(p))
282+
die ("Error in line %d: %.*s", i, len, p);
283+
}
284+
285+
if (!srcs.nr)
286+
return 0;
287+
288+
if (merge_title)
289+
do_fmt_merge_msg_title(out, current_branch);
281290

282291
if (merge_summary) {
283292
struct commit *head;
@@ -289,13 +298,24 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
289298
rev.ignore_merges = 1;
290299
rev.limited = 1;
291300

301+
if (suffixcmp(out->buf, "\n"))
302+
strbuf_addch(out, '\n');
303+
292304
for (i = 0; i < origins.nr; i++)
293305
shortlog(origins.items[i].string, origins.items[i].util,
294306
head, &rev, limit, out);
295307
}
296308
return 0;
297309
}
298310

311+
int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
312+
return do_fmt_merge_msg(1, merge_summary, in, out);
313+
}
314+
315+
int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out) {
316+
return do_fmt_merge_msg(0, 1, in, out);
317+
}
318+
299319
int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
300320
{
301321
const char *inpath = NULL;

builtin/merge.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
973973
reset_hard(remote_head->sha1, 0);
974974
return 0;
975975
} else {
976-
struct strbuf msg = STRBUF_INIT;
976+
struct strbuf merge_names = STRBUF_INIT;
977977

978978
/* We are invoked directly as the first-class UI. */
979979
head_arg = "HEAD";
@@ -986,13 +986,17 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
986986
* codepath so we discard the error in this
987987
* loop.
988988
*/
989-
if (!have_message) {
990-
for (i = 0; i < argc; i++)
991-
merge_name(argv[i], &msg);
992-
fmt_merge_msg(option_log, &msg, &merge_msg);
993-
if (merge_msg.len)
994-
strbuf_setlen(&merge_msg, merge_msg.len-1);
995-
}
989+
for (i = 0; i < argc; i++)
990+
merge_name(argv[i], &merge_names);
991+
992+
if (have_message && option_log)
993+
fmt_merge_msg_shortlog(&merge_names, &merge_msg);
994+
else if (!have_message)
995+
fmt_merge_msg(option_log, &merge_names, &merge_msg);
996+
997+
998+
if (!(have_message && !option_log) && merge_msg.len)
999+
strbuf_setlen(&merge_msg, merge_msg.len-1);
9961000
}
9971001

9981002
if (head_invalid || !argc)

t/t7604-merge-custom-message.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ Testing merge when using a custom message for the merge commit.'
66

77
. ./test-lib.sh
88

9+
create_merge_msgs() {
10+
echo >exp.subject "custom message"
11+
12+
cp exp.subject exp.log &&
13+
echo >>exp.log "" &&
14+
echo >>exp.log "* commit 'c2':" &&
15+
echo >>exp.log " c2"
16+
}
17+
918
test_expect_success 'setup' '
1019
echo c0 > c0.c &&
1120
git add c0.c &&
@@ -19,16 +28,23 @@ test_expect_success 'setup' '
1928
echo c2 > c2.c &&
2029
git add c2.c &&
2130
git commit -m c2 &&
22-
git tag c2
31+
git tag c2 &&
32+
create_merge_msgs
2333
'
2434

2535

2636
test_expect_success 'merge c2 with a custom message' '
2737
git reset --hard c1 &&
28-
echo >expected "custom message" &&
29-
git merge -m "custom message" c2 &&
38+
git merge -m "$(cat exp.subject)" c2 &&
39+
git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
40+
test_cmp exp.subject actual
41+
'
42+
43+
test_expect_success 'merge --log appends to custom message' '
44+
git reset --hard c1 &&
45+
git merge --log -m "$(cat exp.subject)" c2 &&
3046
git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
31-
test_cmp expected actual
47+
test_cmp exp.log actual
3248
'
3349

3450
test_done

0 commit comments

Comments
 (0)