7 #include "string-list.h"
9 #include "fmt-merge-msg.h"
10 #include "gpg-interface.h"
12 static const char * const fmt_merge_msg_usage
[] = {
13 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
17 static int use_branch_desc
;
19 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
21 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
23 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
24 if (!is_bool
&& merge_log_config
< 0)
25 return error("%s: negative length %s", key
, value
);
26 if (is_bool
&& merge_log_config
)
27 merge_log_config
= DEFAULT_MERGE_LOG_LEN
;
28 } else if (!strcmp(key
, "merge.branchdesc")) {
29 use_branch_desc
= git_config_bool(key
, value
);
34 /* merge data per repository where the merged tips came from */
36 struct string_list branch
, tag
, r_branch
, generic
;
41 unsigned char sha1
[20];
42 unsigned is_local_branch
:1;
45 static void init_src_data(struct src_data
*data
)
47 data
->branch
.strdup_strings
= 1;
48 data
->tag
.strdup_strings
= 1;
49 data
->r_branch
.strdup_strings
= 1;
50 data
->generic
.strdup_strings
= 1;
53 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
54 static struct string_list origins
= STRING_LIST_INIT_DUP
;
56 static int handle_line(char *line
)
58 int i
, len
= strlen(line
);
59 struct origin_data
*origin_data
;
61 struct src_data
*src_data
;
62 struct string_list_item
*item
;
65 if (len
< 43 || line
[40] != '\t')
68 if (!prefixcmp(line
+ 41, "not-for-merge"))
75 origin_data
= xcalloc(1, sizeof(struct origin_data
));
76 i
= get_sha1(line
, origin_data
->sha1
);
83 if (line
[len
- 1] == '\n')
88 * At this point, line points at the beginning of comment e.g.
89 * "branch 'frotz' of git://that/repository.git".
90 * Find the repository name and point it with src.
92 src
= strstr(line
, " of ");
102 item
= unsorted_string_list_lookup(&srcs
, src
);
104 item
= string_list_append(&srcs
, src
);
105 item
->util
= xcalloc(1, sizeof(struct src_data
));
106 init_src_data(item
->util
);
108 src_data
= item
->util
;
112 src_data
->head_status
|= 1;
113 } else if (!prefixcmp(line
, "branch ")) {
114 origin_data
->is_local_branch
= 1;
116 string_list_append(&src_data
->branch
, origin
);
117 src_data
->head_status
|= 2;
118 } else if (!prefixcmp(line
, "tag ")) {
120 string_list_append(&src_data
->tag
, origin
+ 4);
121 src_data
->head_status
|= 2;
122 } else if (!prefixcmp(line
, "remote-tracking branch ")) {
123 origin
= line
+ strlen("remote-tracking branch ");
124 string_list_append(&src_data
->r_branch
, origin
);
125 src_data
->head_status
|= 2;
128 string_list_append(&src_data
->generic
, line
);
129 src_data
->head_status
|= 2;
132 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
133 int len
= strlen(origin
);
134 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
135 origin
= xmemdupz(origin
+ 1, len
- 2);
137 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
138 sprintf(new_origin
, "%s of %s", origin
, src
);
141 if (strcmp(".", src
))
142 origin_data
->is_local_branch
= 0;
143 string_list_append(&origins
, origin
)->util
= origin_data
;
147 static void print_joined(const char *singular
, const char *plural
,
148 struct string_list
*list
, struct strbuf
*out
)
153 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
156 strbuf_addstr(out
, plural
);
157 for (i
= 0; i
< list
->nr
- 1; i
++)
158 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
159 list
->items
[i
].string
);
160 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
164 static void add_branch_desc(struct strbuf
*out
, const char *name
)
166 struct strbuf desc
= STRBUF_INIT
;
168 if (!read_branch_desc(&desc
, name
)) {
169 const char *bp
= desc
.buf
;
171 const char *ep
= strchrnul(bp
, '\n');
174 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
177 if (out
->buf
[out
->len
- 1] != '\n')
178 strbuf_addch(out
, '\n');
180 strbuf_release(&desc
);
183 static void shortlog(const char *name
,
184 struct origin_data
*origin_data
,
186 struct rev_info
*rev
, int limit
,
190 struct commit
*commit
;
191 struct object
*branch
;
192 struct string_list subjects
= STRING_LIST_INIT_DUP
;
193 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
194 struct strbuf sb
= STRBUF_INIT
;
195 const unsigned char *sha1
= origin_data
->sha1
;
197 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
198 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
201 setup_revisions(0, NULL
, rev
, NULL
);
202 rev
->ignore_merges
= 1;
203 add_pending_object(rev
, branch
, name
);
204 add_pending_object(rev
, &head
->object
, "^HEAD");
205 head
->object
.flags
|= UNINTERESTING
;
206 if (prepare_revision_walk(rev
))
207 die("revision walk setup failed");
208 while ((commit
= get_revision(rev
)) != NULL
) {
209 struct pretty_print_context ctx
= {0};
212 if (commit
->parents
&& commit
->parents
->next
)
216 if (subjects
.nr
> limit
)
219 format_commit_message(commit
, "%s", &sb
, &ctx
);
223 string_list_append(&subjects
,
224 sha1_to_hex(commit
->object
.sha1
));
226 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
230 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
232 strbuf_addf(out
, "\n* %s:\n", name
);
234 if (origin_data
->is_local_branch
&& use_branch_desc
)
235 add_branch_desc(out
, name
);
237 for (i
= 0; i
< subjects
.nr
; i
++)
239 strbuf_addf(out
, " ...\n");
241 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
243 clear_commit_marks((struct commit
*)branch
, flags
);
244 clear_commit_marks(head
, flags
);
245 free_commit_list(rev
->commits
);
249 string_list_clear(&subjects
, 0);
252 static void fmt_merge_msg_title(struct strbuf
*out
,
253 const char *current_branch
) {
257 strbuf_addstr(out
, "Merge ");
258 for (i
= 0; i
< srcs
.nr
; i
++) {
259 struct src_data
*src_data
= srcs
.items
[i
].util
;
260 const char *subsep
= "";
262 strbuf_addstr(out
, sep
);
265 if (src_data
->head_status
== 1) {
266 strbuf_addstr(out
, srcs
.items
[i
].string
);
269 if (src_data
->head_status
== 3) {
271 strbuf_addstr(out
, "HEAD");
273 if (src_data
->branch
.nr
) {
274 strbuf_addstr(out
, subsep
);
276 print_joined("branch ", "branches ", &src_data
->branch
,
279 if (src_data
->r_branch
.nr
) {
280 strbuf_addstr(out
, subsep
);
282 print_joined("remote-tracking branch ", "remote-tracking branches ",
283 &src_data
->r_branch
, out
);
285 if (src_data
->tag
.nr
) {
286 strbuf_addstr(out
, subsep
);
288 print_joined("tag ", "tags ", &src_data
->tag
, out
);
290 if (src_data
->generic
.nr
) {
291 strbuf_addstr(out
, subsep
);
292 print_joined("commit ", "commits ", &src_data
->generic
,
295 if (strcmp(".", srcs
.items
[i
].string
))
296 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
299 if (!strcmp("master", current_branch
))
300 strbuf_addch(out
, '\n');
302 strbuf_addf(out
, " into %s\n", current_branch
);
305 static void fmt_tag_signature(struct strbuf
*tagbuf
,
310 const char *tag_body
= strstr(buf
, "\n\n");
313 strbuf_add(tagbuf
, tag_body
, buf
+ len
- tag_body
);
315 strbuf_complete_line(tagbuf
);
316 strbuf_add_lines(tagbuf
, "# ", sig
->buf
, sig
->len
);
319 static void fmt_merge_msg_sigs(struct strbuf
*out
)
321 int i
, tag_number
= 0, first_tag
= 0;
322 struct strbuf tagbuf
= STRBUF_INIT
;
324 for (i
= 0; i
< origins
.nr
; i
++) {
325 unsigned char *sha1
= origins
.items
[i
].util
;
326 enum object_type type
;
327 unsigned long size
, len
;
328 char *buf
= read_sha1_file(sha1
, &type
, &size
);
329 struct strbuf sig
= STRBUF_INIT
;
331 if (!buf
|| type
!= OBJ_TAG
)
333 len
= parse_signature(buf
, size
);
336 ; /* merely annotated */
337 else if (verify_signed_buffer(buf
, len
, buf
+ len
, size
- len
, &sig
)) {
339 strbuf_addstr(&sig
, "gpg verification failed.\n");
343 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
346 if (tag_number
== 2) {
347 struct strbuf tagline
= STRBUF_INIT
;
348 strbuf_addf(&tagline
, "\n# %s\n",
349 origins
.items
[first_tag
].string
);
350 strbuf_insert(&tagbuf
, 0, tagline
.buf
,
352 strbuf_release(&tagline
);
354 strbuf_addf(&tagbuf
, "\n# %s\n",
355 origins
.items
[i
].string
);
356 fmt_tag_signature(&tagbuf
, &sig
, buf
, len
);
358 strbuf_release(&sig
);
363 strbuf_addch(out
, '\n');
364 strbuf_addbuf(out
, &tagbuf
);
366 strbuf_release(&tagbuf
);
369 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
370 struct fmt_merge_msg_opts
*opts
)
373 unsigned char head_sha1
[20];
374 const char *current_branch
;
375 void *current_branch_to_free
;
377 /* get current branch */
378 current_branch
= current_branch_to_free
=
379 resolve_refdup("HEAD", head_sha1
, 1, NULL
);
381 die("No current branch");
382 if (!prefixcmp(current_branch
, "refs/heads/"))
383 current_branch
+= 11;
386 while (pos
< in
->len
) {
388 char *newline
, *p
= in
->buf
+ pos
;
390 newline
= strchr(p
, '\n');
391 len
= newline
? newline
- p
: strlen(p
);
392 pos
+= len
+ !!newline
;
396 die ("Error in line %d: %.*s", i
, len
, p
);
399 if (opts
->add_title
&& srcs
.nr
)
400 fmt_merge_msg_title(out
, current_branch
);
403 fmt_merge_msg_sigs(out
);
405 if (opts
->shortlog_len
) {
409 head
= lookup_commit_or_die(head_sha1
, "HEAD");
410 init_revisions(&rev
, NULL
);
411 rev
.commit_format
= CMIT_FMT_ONELINE
;
412 rev
.ignore_merges
= 1;
415 if (suffixcmp(out
->buf
, "\n"))
416 strbuf_addch(out
, '\n');
418 for (i
= 0; i
< origins
.nr
; i
++)
419 shortlog(origins
.items
[i
].string
,
420 origins
.items
[i
].util
,
421 head
, &rev
, opts
->shortlog_len
, out
);
424 strbuf_complete_line(out
);
425 free(current_branch_to_free
);
429 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
431 const char *inpath
= NULL
;
432 const char *message
= NULL
;
433 int shortlog_len
= -1;
434 struct option options
[] = {
435 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
436 "populate log with at most <n> entries from shortlog",
437 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
438 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
439 "alias for --log (deprecated)",
440 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
441 DEFAULT_MERGE_LOG_LEN
},
442 OPT_STRING('m', "message", &message
, "text",
443 "use <text> as start of message"),
444 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
449 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
451 struct fmt_merge_msg_opts opts
;
453 git_config(fmt_merge_msg_config
, NULL
);
454 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
457 usage_with_options(fmt_merge_msg_usage
, options
);
458 if (shortlog_len
< 0)
459 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
461 if (inpath
&& strcmp(inpath
, "-")) {
462 in
= fopen(inpath
, "r");
464 die_errno("cannot open '%s'", inpath
);
467 if (strbuf_read(&input
, fileno(in
), 0) < 0)
468 die_errno("could not read input file");
471 strbuf_addstr(&output
, message
);
473 memset(&opts
, 0, sizeof(opts
));
474 opts
.add_title
= !message
;
475 opts
.shortlog_len
= shortlog_len
;
477 ret
= fmt_merge_msg(&input
, &output
, &opts
);
480 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);