8 static const char *fmt_merge_msg_usage
=
9 "git-fmt-merge-msg [--log] [--no-log] [--file <file>]";
11 static int merge_summary
;
13 static int fmt_merge_msg_config(const char *key
, const char *value
, void *cb
)
15 static int found_merge_log
= 0;
16 if (!strcmp("merge.log", key
)) {
18 merge_summary
= git_config_bool(key
, value
);
20 if (!found_merge_log
&& !strcmp("merge.summary", key
))
21 merge_summary
= git_config_bool(key
, value
);
31 static void append_to_list(struct list
*list
, char *value
, void *payload
)
33 if (list
->nr
== list
->alloc
) {
35 list
->list
= xrealloc(list
->list
, sizeof(char *) * list
->alloc
);
36 list
->payload
= xrealloc(list
->payload
,
37 sizeof(char *) * list
->alloc
);
39 list
->payload
[list
->nr
] = payload
;
40 list
->list
[list
->nr
++] = value
;
43 static int find_in_list(struct list
*list
, char *value
)
47 for (i
= 0; i
< list
->nr
; i
++)
48 if (!strcmp(list
->list
[i
], value
))
54 static void free_list(struct list
*list
)
61 for (i
= 0; i
< list
->nr
; i
++) {
63 free(list
->payload
[i
]);
67 list
->nr
= list
->alloc
= 0;
71 struct list branch
, tag
, r_branch
, generic
;
75 static struct list srcs
= { NULL
, NULL
, 0, 0};
76 static struct list origins
= { NULL
, NULL
, 0, 0};
78 static int handle_line(char *line
)
80 int i
, len
= strlen(line
);
83 struct src_data
*src_data
;
86 if (len
< 43 || line
[40] != '\t')
89 if (!prefixcmp(line
+ 41, "not-for-merge"))
97 i
= get_sha1(line
, sha1
);
102 if (line
[len
- 1] == '\n')
106 src
= strstr(line
, " of ");
116 i
= find_in_list(&srcs
, src
);
119 append_to_list(&srcs
, xstrdup(src
),
120 xcalloc(1, sizeof(struct src_data
)));
122 src_data
= srcs
.payload
[i
];
125 origin
= xstrdup(src
);
126 src_data
->head_status
|= 1;
127 } else if (!prefixcmp(line
, "branch ")) {
128 origin
= xstrdup(line
+ 7);
129 append_to_list(&src_data
->branch
, origin
, NULL
);
130 src_data
->head_status
|= 2;
131 } else if (!prefixcmp(line
, "tag ")) {
133 append_to_list(&src_data
->tag
, xstrdup(origin
+ 4), NULL
);
134 src_data
->head_status
|= 2;
135 } else if (!prefixcmp(line
, "remote branch ")) {
136 origin
= xstrdup(line
+ 14);
137 append_to_list(&src_data
->r_branch
, origin
, NULL
);
138 src_data
->head_status
|= 2;
140 origin
= xstrdup(src
);
141 append_to_list(&src_data
->generic
, xstrdup(line
), NULL
);
142 src_data
->head_status
|= 2;
145 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
146 int len
= strlen(origin
);
147 if (origin
[0] == '\'' && origin
[len
- 1] == '\'') {
148 origin
= xmemdupz(origin
+ 1, len
- 2);
150 origin
= xstrdup(origin
);
153 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
154 sprintf(new_origin
, "%s of %s", origin
, src
);
157 append_to_list(&origins
, origin
, sha1
);
161 static void print_joined(const char *singular
, const char *plural
,
162 struct list
*list
, struct strbuf
*out
)
167 strbuf_addf(out
, "%s%s", singular
, list
->list
[0]);
170 strbuf_addstr(out
, plural
);
171 for (i
= 0; i
< list
->nr
- 1; i
++)
172 strbuf_addf(out
, "%s%s", i
> 0 ? ", " : "", list
->list
[i
]);
173 strbuf_addf(out
, " and %s", list
->list
[list
->nr
- 1]);
177 static void shortlog(const char *name
, unsigned char *sha1
,
178 struct commit
*head
, struct rev_info
*rev
, int limit
,
182 struct commit
*commit
;
183 struct object
*branch
;
184 struct list subjects
= { NULL
, NULL
, 0, 0 };
185 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
187 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
188 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
191 setup_revisions(0, NULL
, rev
, NULL
);
192 rev
->ignore_merges
= 1;
193 add_pending_object(rev
, branch
, name
);
194 add_pending_object(rev
, &head
->object
, "^HEAD");
195 head
->object
.flags
|= UNINTERESTING
;
196 if (prepare_revision_walk(rev
))
197 die("revision walk setup failed");
198 while ((commit
= get_revision(rev
)) != NULL
) {
199 char *oneline
, *bol
, *eol
;
202 if (commit
->parents
&& commit
->parents
->next
)
206 if (subjects
.nr
> limit
)
209 bol
= strstr(commit
->buffer
, "\n\n");
214 } while (isspace(c
));
220 append_to_list(&subjects
, xstrdup(sha1_to_hex(
221 commit
->object
.sha1
)),
226 eol
= strchr(bol
, '\n');
228 oneline
= xmemdupz(bol
, eol
- bol
);
230 oneline
= xstrdup(bol
);
232 append_to_list(&subjects
, oneline
, NULL
);
236 strbuf_addf(out
, "\n* %s: (%d commits)\n", name
, count
);
238 strbuf_addf(out
, "\n* %s:\n", name
);
240 for (i
= 0; i
< subjects
.nr
; i
++)
242 strbuf_addf(out
, " ...\n");
244 strbuf_addf(out
, " %s\n", subjects
.list
[i
]);
246 clear_commit_marks((struct commit
*)branch
, flags
);
247 clear_commit_marks(head
, flags
);
248 free_commit_list(rev
->commits
);
252 free_list(&subjects
);
255 int fmt_merge_msg(int merge_summary
, struct strbuf
*in
, struct strbuf
*out
) {
256 int limit
= 20, i
= 0, pos
= 0;
258 char *p
= line
, *sep
= "";
259 unsigned char head_sha1
[20];
260 const char *current_branch
;
262 /* get current branch */
263 current_branch
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
265 die("No current branch");
266 if (!prefixcmp(current_branch
, "refs/heads/"))
267 current_branch
+= 11;
270 while (pos
< in
->len
) {
275 newline
= strchr(p
, '\n');
276 len
= newline
? newline
- p
: strlen(p
);
277 pos
+= len
+ !!newline
;
281 die ("Error in line %d: %.*s", i
, len
, p
);
284 strbuf_addstr(out
, "Merge ");
285 for (i
= 0; i
< srcs
.nr
; i
++) {
286 struct src_data
*src_data
= srcs
.payload
[i
];
287 const char *subsep
= "";
289 strbuf_addstr(out
, sep
);
292 if (src_data
->head_status
== 1) {
293 strbuf_addstr(out
, srcs
.list
[i
]);
296 if (src_data
->head_status
== 3) {
298 strbuf_addstr(out
, "HEAD");
300 if (src_data
->branch
.nr
) {
301 strbuf_addstr(out
, subsep
);
303 print_joined("branch ", "branches ", &src_data
->branch
,
306 if (src_data
->r_branch
.nr
) {
307 strbuf_addstr(out
, subsep
);
309 print_joined("remote branch ", "remote branches ",
310 &src_data
->r_branch
, out
);
312 if (src_data
->tag
.nr
) {
313 strbuf_addstr(out
, subsep
);
315 print_joined("tag ", "tags ", &src_data
->tag
, out
);
317 if (src_data
->generic
.nr
) {
318 strbuf_addstr(out
, subsep
);
319 print_joined("commit ", "commits ", &src_data
->generic
,
322 if (strcmp(".", srcs
.list
[i
]))
323 strbuf_addf(out
, " of %s", srcs
.list
[i
]);
326 if (!strcmp("master", current_branch
))
327 strbuf_addch(out
, '\n');
329 strbuf_addf(out
, " into %s\n", 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 for (i
= 0; i
< origins
.nr
; i
++)
342 shortlog(origins
.list
[i
], origins
.payload
[i
],
343 head
, &rev
, limit
, out
);
348 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
351 struct strbuf input
, output
;
354 git_config(fmt_merge_msg_config
, NULL
);
357 if (!strcmp(argv
[1], "--log") || !strcmp(argv
[1], "--summary"))
359 else if (!strcmp(argv
[1], "--no-log")
360 || !strcmp(argv
[1], "--no-summary"))
362 else if (!strcmp(argv
[1], "-F") || !strcmp(argv
[1], "--file")) {
365 if (!strcmp(argv
[2], "-"))
369 in
= fopen(argv
[2], "r");
371 die("cannot open %s", argv
[2]);
380 usage(fmt_merge_msg_usage
);
382 strbuf_init(&input
, 0);
383 if (strbuf_read(&input
, fileno(in
), 0) < 0)
384 die("could not read input file %s", strerror(errno
));
385 strbuf_init(&output
, 0);
387 ret
= fmt_merge_msg(merge_summary
, &input
, &output
);
390 printf("%s", output
.buf
);