]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/fmt-merge-msg.c
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 static int found_merge_log
= 0;
19 if (!strcmp("merge.log", key
)) {
21 shortlog_len
= git_config_bool(key
, value
) ? DEFAULT_MERGE_LOG_LEN
: 0;
24 if (!found_merge_log
&& !strcmp("merge.summary", key
)) {
25 shortlog_len
= git_config_bool(key
, value
) ? DEFAULT_MERGE_LOG_LEN
: 0;
32 struct string_list branch
, tag
, r_branch
, generic
;
36 void init_src_data(struct src_data
*data
)
38 data
->branch
.strdup_strings
= 1;
39 data
->tag
.strdup_strings
= 1;
40 data
->r_branch
.strdup_strings
= 1;
41 data
->generic
.strdup_strings
= 1;
44 static struct string_list srcs
= { NULL
, 0, 0, 1 };
45 static struct string_list origins
= { NULL
, 0, 0, 1 };
47 static int handle_line(char *line
)
49 int i
, len
= strlen(line
);
52 struct src_data
*src_data
;
53 struct string_list_item
*item
;
56 if (len
< 43 || line
[40] != '\t')
59 if (!prefixcmp(line
+ 41, "not-for-merge"))
67 i
= get_sha1(line
, sha1
);
72 if (line
[len
- 1] == '\n')
76 src
= strstr(line
, " of ");
86 item
= unsorted_string_list_lookup(&srcs
, src
);
88 item
= string_list_append(&srcs
, src
);
89 item
->util
= xcalloc(1, sizeof(struct src_data
));
90 init_src_data(item
->util
);
92 src_data
= item
->util
;
96 src_data
->head_status
|= 1;
97 } else if (!prefixcmp(line
, "branch ")) {
99 string_list_append(&src_data
->branch
, origin
);
100 src_data
->head_status
|= 2;
101 } else if (!prefixcmp(line
, "tag ")) {
103 string_list_append(&src_data
->tag
, origin
+ 4);
104 src_data
->head_status
|= 2;
105 } else if (!prefixcmp(line
, "remote branch ")) {
107 string_list_append(&src_data
->r_branch
, origin
);
108 src_data
->head_status
|= 2;
111 string_list_append(&src_data
->generic
, line
);
112 src_data
->head_status
|= 2;
115 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
116 int len
= strlen(origin
);
117 if (origin
[0] == '\'' && origin
[len
- 1] == '\'')
118 origin
= xmemdupz(origin
+ 1, len
- 2);
120 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
121 sprintf(new_origin
, "%s of %s", origin
, src
);
124 string_list_append(&origins
, origin
)->util
= sha1
;
128 static void print_joined(const char *singular
, const char *plural
,
129 struct string_list
*list
, struct strbuf
*out
)
134 strbuf_addf(out
, "%s%s", singular
, list
->items
[0].string
);
137 strbuf_addstr(out
, plural
);
138 for (i
= 0; i
< list
->nr
- 1; i
++)
139 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "",
140 list
->items
[i
].string
);
141 strbuf_addf(out
, " and %s", list
->items
[list
->nr
- 1].string
);
145 static void shortlog(const char *name
, unsigned char *sha1
,
146 struct commit
*head
, struct rev_info
*rev
, int limit
,
150 struct commit
*commit
;
151 struct object
*branch
;
152 struct string_list subjects
= { NULL
, 0, 0, 1 };
153 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
154 struct strbuf sb
= STRBUF_INIT
;
156 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
157 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
160 setup_revisions(0, NULL
, rev
, NULL
);
161 rev
->ignore_merges
= 1;
162 add_pending_object(rev
, branch
, name
);
163 add_pending_object(rev
, &head
->object
, "^HEAD");
164 head
->object
.flags
|= UNINTERESTING
;
165 if (prepare_revision_walk(rev
))
166 die("revision walk setup failed");
167 while ((commit
= get_revision(rev
)) != NULL
) {
168 struct pretty_print_context ctx
= {0};
171 if (commit
->parents
&& commit
->parents
->next
)
175 if (subjects
.nr
> limit
)
178 format_commit_message(commit
, "%s", &sb
, &ctx
);
182 string_list_append(&subjects
,
183 sha1_to_hex(commit
->object
.sha1
));
185 string_list_append(&subjects
, strbuf_detach(&sb
, NULL
));
189 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
191 strbuf_addf(out
, "\n* %s:\n", name
);
193 for (i
= 0; i
< subjects
.nr
; i
++)
195 strbuf_addf(out
, " ...\n");
197 strbuf_addf(out
, " %s\n", subjects
.items
[i
].string
);
199 clear_commit_marks((struct commit
*)branch
, flags
);
200 clear_commit_marks(head
, flags
);
201 free_commit_list(rev
->commits
);
205 string_list_clear(&subjects
, 0);
208 static void do_fmt_merge_msg_title(struct strbuf
*out
,
209 const char *current_branch
) {
213 strbuf_addstr(out
, "Merge ");
214 for (i
= 0; i
< srcs
.nr
; i
++) {
215 struct src_data
*src_data
= srcs
.items
[i
].util
;
216 const char *subsep
= "";
218 strbuf_addstr(out
, sep
);
221 if (src_data
->head_status
== 1) {
222 strbuf_addstr(out
, srcs
.items
[i
].string
);
225 if (src_data
->head_status
== 3) {
227 strbuf_addstr(out
, "HEAD");
229 if (src_data
->branch
.nr
) {
230 strbuf_addstr(out
, subsep
);
232 print_joined("branch ", "branches ", &src_data
->branch
,
235 if (src_data
->r_branch
.nr
) {
236 strbuf_addstr(out
, subsep
);
238 print_joined("remote branch ", "remote branches ",
239 &src_data
->r_branch
, out
);
241 if (src_data
->tag
.nr
) {
242 strbuf_addstr(out
, subsep
);
244 print_joined("tag ", "tags ", &src_data
->tag
, out
);
246 if (src_data
->generic
.nr
) {
247 strbuf_addstr(out
, subsep
);
248 print_joined("commit ", "commits ", &src_data
->generic
,
251 if (strcmp(".", srcs
.items
[i
].string
))
252 strbuf_addf(out
, " of %s", srcs
.items
[i
].string
);
255 if (!strcmp("master", current_branch
))
256 strbuf_addch(out
, '\n');
258 strbuf_addf(out
, " into %s\n", current_branch
);
261 static int do_fmt_merge_msg(int merge_title
, struct strbuf
*in
,
262 struct strbuf
*out
, int shortlog_len
) {
264 unsigned char head_sha1
[20];
265 const char *current_branch
;
267 /* get current branch */
268 current_branch
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
270 die("No current branch");
271 if (!prefixcmp(current_branch
, "refs/heads/"))
272 current_branch
+= 11;
275 while (pos
< in
->len
) {
277 char *newline
, *p
= in
->buf
+ pos
;
279 newline
= strchr(p
, '\n');
280 len
= newline
? newline
- p
: strlen(p
);
281 pos
+= len
+ !!newline
;
285 die ("Error in line %d: %.*s", i
, len
, p
);
292 do_fmt_merge_msg_title(out
, current_branch
);
298 head
= lookup_commit(head_sha1
);
299 init_revisions(&rev
, NULL
);
300 rev
.commit_format
= CMIT_FMT_ONELINE
;
301 rev
.ignore_merges
= 1;
304 if (suffixcmp(out
->buf
, "\n"))
305 strbuf_addch(out
, '\n');
307 for (i
= 0; i
< origins
.nr
; i
++)
308 shortlog(origins
.items
[i
].string
, origins
.items
[i
].util
,
309 head
, &rev
, shortlog_len
, out
);
314 int fmt_merge_msg(struct strbuf
*in
, struct strbuf
*out
,
315 int merge_title
, int shortlog_len
) {
316 return do_fmt_merge_msg(merge_title
, in
, out
, shortlog_len
);
319 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
321 const char *inpath
= NULL
;
322 const char *message
= NULL
;
323 struct option options
[] = {
324 { OPTION_INTEGER
, 0, "log", &shortlog_len
, "n",
325 "populate log with at most <n> entries from shortlog",
326 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
327 { OPTION_INTEGER
, 0, "summary", &shortlog_len
, "n",
328 "alias for --log (deprecated)",
329 PARSE_OPT_OPTARG
| PARSE_OPT_HIDDEN
, NULL
,
330 DEFAULT_MERGE_LOG_LEN
},
331 OPT_STRING('m', "message", &message
, "text",
332 "use <text> as start of message"),
333 OPT_FILENAME('F', "file", &inpath
, "file to read from"),
338 struct strbuf input
= STRBUF_INIT
, output
= STRBUF_INIT
;
341 git_config(fmt_merge_msg_config
, NULL
);
342 argc
= parse_options(argc
, argv
, prefix
, options
, fmt_merge_msg_usage
,
345 usage_with_options(fmt_merge_msg_usage
, options
);
346 if (message
&& !shortlog_len
) {
348 write_in_full(STDOUT_FILENO
, message
, strlen(message
));
349 write_in_full(STDOUT_FILENO
, &nl
, 1);
352 if (shortlog_len
< 0)
353 die("Negative --log=%d", shortlog_len
);
355 if (inpath
&& strcmp(inpath
, "-")) {
356 in
= fopen(inpath
, "r");
358 die_errno("cannot open '%s'", inpath
);
361 if (strbuf_read(&input
, fileno(in
), 0) < 0)
362 die_errno("could not read input file");
365 strbuf_addstr(&output
, message
);
366 ret
= fmt_merge_msg(&input
, &output
,
372 write_in_full(STDOUT_FILENO
, output
.buf
, output
.len
);