7 #include "string-list.h"
9 static const char * const fmt_merge_msg_usage
[] = {
10 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
14 static int shortlog_len
;
16 static int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
18 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
20 shortlog_len
= git_config_bool_or_int(key
, value
, &is_bool
);
21 if (!is_bool
&& shortlog_len
< 0)
22 return error("%s: negative length %s", key
, value
);
23 if (is_bool
&& shortlog_len
)
24 shortlog_len
= DEFAULT_MERGE_LOG_LEN
;
30 struct string_list branch
, tag
, r_branch
, generic
;
34 void init_src_data(struct src_data
*data
)
36 data
->branch
.strdup_strings
= 1;
37 data
->tag
.strdup_strings
= 1;
38 data
->r_branch
.strdup_strings
= 1;
39 data
->generic
.strdup_strings
= 1;
42 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
43 static struct string_list origins
= STRING_LIST_INIT_DUP
;
45 static int handle_line(char *line
)
47 int i
, len
= strlen(line
);
50 struct src_data
*src_data
;
51 struct string_list_item
*item
;
54 if (len
< 43 || line
[40] != '\t')
57 if (!prefixcmp(line
+ 41, "not-for-merge"))
65 i
= get_sha1(line
, sha1
);
70 if (line
[len
- 1] == '\n')
74 src
= strstr(line
, " of ");
84 item
= unsorted_string_list_lookup(&srcs
, src
);
86 item
= string_list_append(&srcs
, src
);
87 item
->util
= xcalloc(1, sizeof(struct src_data
));
88 init_src_data(item
->util
);
90 src_data
= item
->util
;
94 src_data
->head_status
|= 1;
95 } else if (!prefixcmp(line
, "branch ")) {
97 string_list_append(&src_data
->branch
, origin
);
98 src_data
->head_status
|= 2;
99 } else if (!prefixcmp(line
, "tag ")) {
101 string_list_append(&src_data
->tag
, origin
+ 4);
102 src_data
->head_status
|= 2;
103 } else if (!prefixcmp(line
, "remote-tracking branch ")) {
104 origin
= line
+ strlen("remote-tracking branch ");
105 string_list_append(&src_data
->r_branch
, origin
);
106 src_data
->head_status
|= 2;
109 string_list_append(&src_data
->generic
, line
);
110 src_data
->head_status
|= 2;
113 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
114 int len
= strlen(origin
);
115 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
116 origin
= xmemdupz(origin
+ 1, len
- 2);
118 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
119 sprintf(new_origin
, "%s of %s", origin
, src
);
122 string_list_append(&origins
, origin
)->util
= sha1
;
126 static void print_joined(const char *singular
, const char *plural
,
127 struct string_list
*list
, struct strbuf
*out
)
132 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
135 strbuf_addstr(out
, plural
);
136 for (i
= 0; i
< list
->nr
- 1; i
++)
137 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
138 list
->items
[i
].string
);
139 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
143 static void shortlog(const char *name
, unsigned char *sha1
,
144 struct commit
*head
, struct rev_info
*rev
, int limit
,
148 struct commit
*commit
;
149 struct object
*branch
;
150 struct string_list subjects
= STRING_LIST_INIT_DUP
;
151 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
152 struct strbuf sb
= STRBUF_INIT
;
154 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
155 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
158 setup_revisions(0, NULL
, rev
, NULL
);
159 rev
->ignore_merges
= 1;
160 add_pending_object(rev
, branch
, name
);
161 add_pending_object(rev
, &head
->object
, "^HEAD");
162 head
->object
.flags
|= UNINTERESTING
;
163 if (prepare_revision_walk(rev
))
164 die("revision walk setup failed");
165 while ((commit
= get_revision(rev
)) != NULL
) {
166 struct pretty_print_context ctx
= {0};
169 if (commit
->parents
&& commit
->parents
->next
)
173 if (subjects
.nr
> limit
)
176 format_commit_message(commit
, "%s", &sb
, &ctx
);
180 string_list_append(&subjects
,
181 sha1_to_hex(commit
->object
.sha1
));
183 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
187 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
189 strbuf_addf(out
, "\n* %s:\n", name
);
191 for (i
= 0; i
< subjects
.nr
; i
++)
193 strbuf_addf(out
, " ...\n");
195 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
197 clear_commit_marks((struct commit
*)branch
, flags
);
198 clear_commit_marks(head
, flags
);
199 free_commit_list(rev
->commits
);
203 string_list_clear(&subjects
, 0);
206 static void do_fmt_merge_msg_title(struct strbuf
*out
,
207 const char *current_branch
) {
211 strbuf_addstr(out
, "Merge ");
212 for (i
= 0; i
< srcs
.nr
; i
++) {
213 struct src_data
*src_data
= srcs
.items
[i
].util
;
214 const char *subsep
= "";
216 strbuf_addstr(out
, sep
);
219 if (src_data
->head_status
== 1) {
220 strbuf_addstr(out
, srcs
.items
[i
].string
);
223 if (src_data
->head_status
== 3) {
225 strbuf_addstr(out
, "HEAD");
227 if (src_data
->branch
.nr
) {
228 strbuf_addstr(out
, subsep
);
230 print_joined("branch ", "branches ", &src_data
->branch
,
233 if (src_data
->r_branch
.nr
) {
234 strbuf_addstr(out
, subsep
);
236 print_joined("remote-tracking branch ", "remote-tracking branches ",
237 &src_data
->r_branch
, out
);
239 if (src_data
->tag
.nr
) {
240 strbuf_addstr(out
, subsep
);
242 print_joined("tag ", "tags ", &src_data
->tag
, out
);
244 if (src_data
->generic
.nr
) {
245 strbuf_addstr(out
, subsep
);
246 print_joined("commit ", "commits ", &src_data
->generic
,
249 if (strcmp(".", srcs
.items
[i
].string
))
250 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
253 if (!strcmp("master", current_branch
))
254 strbuf_addch(out
, '\n');
256 strbuf_addf(out
, " into %s\n", current_branch
);
259 static int do_fmt_merge_msg(int merge_title
, struct strbuf
*in
,
260 struct strbuf
*out
, int shortlog_len
) {
262 unsigned char head_sha1
[20];
263 const char *current_branch
;
265 /* get current branch */
266 current_branch
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
268 die("No current branch");
269 if (!prefixcmp(current_branch
, "refs/heads/"))
270 current_branch
+= 11;
273 while (pos
< in
->len
) {
275 char *newline
, *p
= in
->buf
+ pos
;
277 newline
= strchr(p
, '\n');
278 len
= newline
? newline
- p
: strlen(p
);
279 pos
+= len
+ !!newline
;
283 die ("Error in line %d: %.*s", i
, len
, p
);
290 do_fmt_merge_msg_title(out
, current_branch
);
296 head
= lookup_commit(head_sha1
);
297 init_revisions(&rev
, NULL
);
298 rev
.commit_format
= CMIT_FMT_ONELINE
;
299 rev
.ignore_merges
= 1;
302 if (suffixcmp(out
->buf
, "\n"))
303 strbuf_addch(out
, '\n');
305 for (i
= 0; i
< origins
.nr
; i
++)
306 shortlog(origins
.items
[i
].string
, origins
.items
[i
].util
,
307 head
, &rev
, shortlog_len
, out
);
312 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
313 int merge_title
, int shortlog_len
) {
314 return do_fmt_merge_msg(merge_title
, in
, out
, shortlog_len
);
317 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
319 const char *inpath
= NULL
;
320 const char *message
= NULL
;
321 struct option options
[] = {
322 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
323 "populate log with at most <n> entries from shortlog",
324 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
325 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
326 "alias for --log (deprecated)",
327 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
328 DEFAULT_MERGE_LOG_LEN
},
329 OPT_STRING('m', "message", &message
, "text",
330 "use <text> as start of message"),
331 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
336 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
339 git_config(fmt_merge_msg_config
, NULL
);
340 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
343 usage_with_options(fmt_merge_msg_usage
, options
);
344 if (message
&& !shortlog_len
) {
346 write_in_full(STDOUT_FILENO
, message
, strlen(message
));
347 write_in_full(STDOUT_FILENO
, &nl
, 1);
350 if (shortlog_len
< 0)
351 die("Negative --log=%d", shortlog_len
);
353 if (inpath
&& strcmp(inpath
, "-")) {
354 in
= fopen(inpath
, "r");
356 die_errno("cannot open '%s'", inpath
);
359 if (strbuf_read(&input
, fileno(in
), 0) < 0)
360 die_errno("could not read input file");
363 strbuf_addstr(&output
, message
);
364 ret
= fmt_merge_msg(&input
, &output
,
370 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);