7 #include "string-list.h"
9 #include "fmt-merge-msg.h"
11 static const char * const fmt_merge_msg_usage
[] = {
12 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
16 static int use_branch_desc
;
18 int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
20 if (!strcmp(key
, "merge.log") || !strcmp(key
, "merge.summary")) {
22 merge_log_config
= git_config_bool_or_int(key
, value
, &is_bool
);
23 if (!is_bool
&& merge_log_config
< 0)
24 return error("%s: negative length %s", key
, value
);
25 if (is_bool
&& merge_log_config
)
26 merge_log_config
= DEFAULT_MERGE_LOG_LEN
;
27 } else if (!strcmp(key
, "merge.branchdesc")) {
28 use_branch_desc
= git_config_bool(key
, value
);
34 struct string_list branch
, tag
, r_branch
, generic
;
39 unsigned char sha1
[20];
40 unsigned is_local_branch
:1;
43 static void init_src_data(struct src_data
*data
)
45 data
->branch
.strdup_strings
= 1;
46 data
->tag
.strdup_strings
= 1;
47 data
->r_branch
.strdup_strings
= 1;
48 data
->generic
.strdup_strings
= 1;
51 static struct string_list srcs
= STRING_LIST_INIT_DUP
;
52 static struct string_list origins
= STRING_LIST_INIT_DUP
;
54 static int handle_line(char *line
)
56 int i
, len
= strlen(line
);
57 struct origin_data
*origin_data
;
59 struct src_data
*src_data
;
60 struct string_list_item
*item
;
63 if (len
< 43 || line
[40] != '\t')
66 if (!prefixcmp(line
+ 41, "not-for-merge"))
73 origin_data
= xcalloc(1, sizeof(struct origin_data
));
74 i
= get_sha1(line
, origin_data
->sha1
);
81 if (line
[len
- 1] == '\n')
85 src
= strstr(line
, " of ");
95 item
= unsorted_string_list_lookup(&srcs
, src
);
97 item
= string_list_append(&srcs
, src
);
98 item
->util
= xcalloc(1, sizeof(struct src_data
));
99 init_src_data(item
->util
);
101 src_data
= item
->util
;
105 src_data
->head_status
|= 1;
106 } else if (!prefixcmp(line
, "branch ")) {
107 origin_data
->is_local_branch
= 1;
109 string_list_append(&src_data
->branch
, origin
);
110 src_data
->head_status
|= 2;
111 } else if (!prefixcmp(line
, "tag ")) {
113 string_list_append(&src_data
->tag
, origin
+ 4);
114 src_data
->head_status
|= 2;
115 } else if (!prefixcmp(line
, "remote-tracking branch ")) {
116 origin
= line
+ strlen("remote-tracking branch ");
117 string_list_append(&src_data
->r_branch
, origin
);
118 src_data
->head_status
|= 2;
121 string_list_append(&src_data
->generic
, line
);
122 src_data
->head_status
|= 2;
125 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
126 int len
= strlen(origin
);
127 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
128 origin
= xmemdupz(origin
+ 1, len
- 2);
130 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
131 sprintf(new_origin
, "%s of %s", origin
, src
);
134 if (strcmp(".", src
))
135 origin_data
->is_local_branch
= 0;
136 string_list_append(&origins
, origin
)->util
= origin_data
;
140 static void print_joined(const char *singular
, const char *plural
,
141 struct string_list
*list
, struct strbuf
*out
)
146 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
149 strbuf_addstr(out
, plural
);
150 for (i
= 0; i
< list
->nr
- 1; i
++)
151 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
152 list
->items
[i
].string
);
153 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
157 static void add_branch_desc(struct strbuf
*out
, const char *name
)
159 struct strbuf desc
= STRBUF_INIT
;
161 if (!read_branch_desc(&desc
, name
)) {
162 const char *bp
= desc
.buf
;
164 const char *ep
= strchrnul(bp
, '\n');
167 strbuf_addf(out
, " : %.*s", (int)(ep
- bp
), bp
);
170 if (out
->buf
[out
->len
- 1] != '\n')
171 strbuf_addch(out
, '\n');
173 strbuf_release(&desc
);
176 static void shortlog(const char *name
,
177 struct origin_data
*origin_data
,
179 struct rev_info
*rev
, int limit
,
183 struct commit
*commit
;
184 struct object
*branch
;
185 struct string_list subjects
= STRING_LIST_INIT_DUP
;
186 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
187 struct strbuf sb
= STRBUF_INIT
;
188 const unsigned char *sha1
= origin_data
->sha1
;
190 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
191 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
194 setup_revisions(0, NULL
, rev
, NULL
);
195 rev
->ignore_merges
= 1;
196 add_pending_object(rev
, branch
, name
);
197 add_pending_object(rev
, &head
->object
, "^HEAD");
198 head
->object
.flags
|= UNINTERESTING
;
199 if (prepare_revision_walk(rev
))
200 die("revision walk setup failed");
201 while ((commit
= get_revision(rev
)) != NULL
) {
202 struct pretty_print_context ctx
= {0};
205 if (commit
->parents
&& commit
->parents
->next
)
209 if (subjects
.nr
> limit
)
212 format_commit_message(commit
, "%s", &sb
, &ctx
);
216 string_list_append(&subjects
,
217 sha1_to_hex(commit
->object
.sha1
));
219 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
223 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
225 strbuf_addf(out
, "\n* %s:\n", name
);
227 if (origin_data
->is_local_branch
&& use_branch_desc
)
228 add_branch_desc(out
, name
);
230 for (i
= 0; i
< subjects
.nr
; i
++)
232 strbuf_addf(out
, " ...\n");
234 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
236 clear_commit_marks((struct commit
*)branch
, flags
);
237 clear_commit_marks(head
, flags
);
238 free_commit_list(rev
->commits
);
242 string_list_clear(&subjects
, 0);
245 static void do_fmt_merge_msg_title(struct strbuf
*out
,
246 const char *current_branch
) {
250 strbuf_addstr(out
, "Merge ");
251 for (i
= 0; i
< srcs
.nr
; i
++) {
252 struct src_data
*src_data
= srcs
.items
[i
].util
;
253 const char *subsep
= "";
255 strbuf_addstr(out
, sep
);
258 if (src_data
->head_status
== 1) {
259 strbuf_addstr(out
, srcs
.items
[i
].string
);
262 if (src_data
->head_status
== 3) {
264 strbuf_addstr(out
, "HEAD");
266 if (src_data
->branch
.nr
) {
267 strbuf_addstr(out
, subsep
);
269 print_joined("branch ", "branches ", &src_data
->branch
,
272 if (src_data
->r_branch
.nr
) {
273 strbuf_addstr(out
, subsep
);
275 print_joined("remote-tracking branch ", "remote-tracking branches ",
276 &src_data
->r_branch
, out
);
278 if (src_data
->tag
.nr
) {
279 strbuf_addstr(out
, subsep
);
281 print_joined("tag ", "tags ", &src_data
->tag
, out
);
283 if (src_data
->generic
.nr
) {
284 strbuf_addstr(out
, subsep
);
285 print_joined("commit ", "commits ", &src_data
->generic
,
288 if (strcmp(".", srcs
.items
[i
].string
))
289 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
292 if (!strcmp("master", current_branch
))
293 strbuf_addch(out
, '\n');
295 strbuf_addf(out
, " into %s\n", current_branch
);
298 static int do_fmt_merge_msg(int merge_title
, struct strbuf
*in
,
299 struct strbuf
*out
, int shortlog_len
) {
301 unsigned char head_sha1
[20];
302 const char *current_branch
;
304 /* get current branch */
305 current_branch
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
307 die("No current branch");
308 if (!prefixcmp(current_branch
, "refs/heads/"))
309 current_branch
+= 11;
312 while (pos
< in
->len
) {
314 char *newline
, *p
= in
->buf
+ pos
;
316 newline
= strchr(p
, '\n');
317 len
= newline
? newline
- p
: strlen(p
);
318 pos
+= len
+ !!newline
;
322 die ("Error in line %d: %.*s", i
, len
, p
);
329 do_fmt_merge_msg_title(out
, current_branch
);
335 head
= lookup_commit(head_sha1
);
336 init_revisions(&rev
, NULL
);
337 rev
.commit_format
= CMIT_FMT_ONELINE
;
338 rev
.ignore_merges
= 1;
341 if (suffixcmp(out
->buf
, "\n"))
342 strbuf_addch(out
, '\n');
344 for (i
= 0; i
< origins
.nr
; i
++)
345 shortlog(origins
.items
[i
].string
,
346 origins
.items
[i
].util
,
347 head
, &rev
, shortlog_len
, out
);
352 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
353 int merge_title
, int shortlog_len
) {
354 return do_fmt_merge_msg(merge_title
, in
, out
, shortlog_len
);
357 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
359 const char *inpath
= NULL
;
360 const char *message
= NULL
;
361 int shortlog_len
= -1;
362 struct option options
[] = {
363 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
364 "populate log with at most <n> entries from shortlog",
365 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
366 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
367 "alias for --log (deprecated)",
368 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
369 DEFAULT_MERGE_LOG_LEN
},
370 OPT_STRING('m', "message", &message
, "text",
371 "use <text> as start of message"),
372 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
377 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
380 git_config(fmt_merge_msg_config
, NULL
);
381 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
384 usage_with_options(fmt_merge_msg_usage
, options
);
385 if (shortlog_len
< 0)
386 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
387 if (message
&& !shortlog_len
) {
389 write_in_full(STDOUT_FILENO
, message
, strlen(message
));
390 write_in_full(STDOUT_FILENO
, &nl
, 1);
393 if (shortlog_len
< 0)
394 die("Negative --log=%d", shortlog_len
);
396 if (inpath
&& strcmp(inpath
, "-")) {
397 in
= fopen(inpath
, "r");
399 die_errno("cannot open '%s'", inpath
);
402 if (strbuf_read(&input
, fileno(in
), 0) < 0)
403 die_errno("could not read input file");
406 strbuf_addstr(&output
, message
);
407 ret
= fmt_merge_msg(&input
, &output
,
413 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);