8 static const char *fmt_merge_msg_usage
=
9 "git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]";
11 static int merge_summary
;
13 static int fmt_merge_msg_config(const char *key
, const char *value
)
15 if (!strcmp("merge.summary", key
))
16 merge_summary
= git_config_bool(key
, value
);
26 static void append_to_list(struct list
*list
, char *value
, void *payload
)
28 if (list
->nr
== list
->alloc
) {
30 list
->list
= xrealloc(list
->list
, sizeof(char *) * list
->alloc
);
31 list
->payload
= xrealloc(list
->payload
,
32 sizeof(char *) * list
->alloc
);
34 list
->payload
[list
->nr
] = payload
;
35 list
->list
[list
->nr
++] = value
;
38 static int find_in_list(struct list
*list
, char *value
)
42 for (i
= 0; i
< list
->nr
; i
++)
43 if (!strcmp(list
->list
[i
], value
))
49 static void free_list(struct list
*list
)
56 for (i
= 0; i
< list
->nr
; i
++) {
58 free(list
->payload
[i
]);
62 list
->nr
= list
->alloc
= 0;
66 struct list branch
, tag
, r_branch
, generic
;
70 static struct list srcs
= { NULL
, NULL
, 0, 0};
71 static struct list origins
= { NULL
, NULL
, 0, 0};
73 static int handle_line(char *line
)
75 int i
, len
= strlen(line
);
78 struct src_data
*src_data
;
81 if (len
< 43 || line
[40] != '\t')
84 if (!prefixcmp(line
+ 41, "not-for-merge"))
92 i
= get_sha1(line
, sha1
);
97 if (line
[len
- 1] == '\n')
101 src
= strstr(line
, " of ");
111 i
= find_in_list(&srcs
, src
);
114 append_to_list(&srcs
, xstrdup(src
),
115 xcalloc(1, sizeof(struct src_data
)));
117 src_data
= srcs
.payload
[i
];
120 origin
= xstrdup(src
);
121 src_data
->head_status
|= 1;
122 } else if (!prefixcmp(line
, "branch ")) {
123 origin
= xstrdup(line
+ 7);
124 append_to_list(&src_data
->branch
, origin
, NULL
);
125 src_data
->head_status
|= 2;
126 } else if (!prefixcmp(line
, "tag ")) {
128 append_to_list(&src_data
->tag
, xstrdup(origin
+ 4), NULL
);
129 src_data
->head_status
|= 2;
130 } else if (!prefixcmp(line
, "remote branch ")) {
131 origin
= xstrdup(line
+ 14);
132 append_to_list(&src_data
->r_branch
, origin
, NULL
);
133 src_data
->head_status
|= 2;
135 origin
= xstrdup(src
);
136 append_to_list(&src_data
->generic
, xstrdup(line
), NULL
);
137 src_data
->head_status
|= 2;
140 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
141 int len
= strlen(origin
);
142 if (origin
[0] == '\'' && origin
[len
- 1] == '\'') {
143 origin
= xmemdupz(origin
+ 1, len
- 2);
145 origin
= xstrdup(origin
);
148 char *new_origin
= xmalloc(strlen(origin
) + strlen(src
) + 5);
149 sprintf(new_origin
, "%s of %s", origin
, src
);
152 append_to_list(&origins
, origin
, sha1
);
156 static void print_joined(const char *singular
, const char *plural
,
162 printf("%s%s", singular
, list
->list
[0]);
165 printf("%s", plural
);
166 for (i
= 0; i
< list
->nr
- 1; i
++)
167 printf("%s%s", i
> 0 ? ", " : "", list
->list
[i
]);
168 printf(" and %s", list
->list
[list
->nr
- 1]);
172 static void shortlog(const char *name
, unsigned char *sha1
,
173 struct commit
*head
, struct rev_info
*rev
, int limit
)
176 struct commit
*commit
;
177 struct object
*branch
;
178 struct list subjects
= { NULL
, NULL
, 0, 0 };
179 int flags
= UNINTERESTING
| TREESAME
| SEEN
| SHOWN
| ADDED
;
181 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
182 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
185 setup_revisions(0, NULL
, rev
, NULL
);
186 rev
->ignore_merges
= 1;
187 add_pending_object(rev
, branch
, name
);
188 add_pending_object(rev
, &head
->object
, "^HEAD");
189 head
->object
.flags
|= UNINTERESTING
;
190 if (prepare_revision_walk(rev
))
191 die("revision walk setup failed");
192 while ((commit
= get_revision(rev
)) != NULL
) {
193 char *oneline
, *bol
, *eol
;
196 if (commit
->parents
&& commit
->parents
->next
)
200 if (subjects
.nr
> limit
)
203 bol
= strstr(commit
->buffer
, "\n\n");
205 append_to_list(&subjects
, xstrdup(sha1_to_hex(
206 commit
->object
.sha1
)),
212 eol
= strchr(bol
, '\n');
214 oneline
= xmemdupz(bol
, eol
- bol
);
216 oneline
= xstrdup(bol
);
218 append_to_list(&subjects
, oneline
, NULL
);
222 printf("\n* %s: (%d commits)\n", name
, count
);
224 printf("\n* %s:\n", name
);
226 for (i
= 0; i
< subjects
.nr
; i
++)
230 printf(" %s\n", subjects
.list
[i
]);
232 clear_commit_marks((struct commit
*)branch
, flags
);
233 clear_commit_marks(head
, flags
);
234 free_commit_list(rev
->commits
);
238 free_list(&subjects
);
241 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
243 int limit
= 20, i
= 0;
246 const char *sep
= "";
247 unsigned char head_sha1
[20];
248 const char *current_branch
;
250 git_config(fmt_merge_msg_config
);
253 if (!strcmp(argv
[1], "--summary"))
255 else if (!strcmp(argv
[1], "--no-summary"))
257 else if (!strcmp(argv
[1], "-F") || !strcmp(argv
[1], "--file")) {
260 if (!strcmp(argv
[2], "-"))
264 in
= fopen(argv
[2], "r");
266 die("cannot open %s", argv
[2]);
275 usage(fmt_merge_msg_usage
);
277 /* get current branch */
278 current_branch
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
280 die("No current branch");
281 if (!prefixcmp(current_branch
, "refs/heads/"))
282 current_branch
+= 11;
284 while (fgets(line
, sizeof(line
), in
)) {
288 if (handle_line(line
))
289 die ("Error in line %d: %s", i
, line
);
293 for (i
= 0; i
< srcs
.nr
; i
++) {
294 struct src_data
*src_data
= srcs
.payload
[i
];
295 const char *subsep
= "";
300 if (src_data
->head_status
== 1) {
301 printf(srcs
.list
[i
]);
304 if (src_data
->head_status
== 3) {
308 if (src_data
->branch
.nr
) {
311 print_joined("branch ", "branches ", &src_data
->branch
);
313 if (src_data
->r_branch
.nr
) {
316 print_joined("remote branch ", "remote branches ",
317 &src_data
->r_branch
);
319 if (src_data
->tag
.nr
) {
322 print_joined("tag ", "tags ", &src_data
->tag
);
324 if (src_data
->generic
.nr
) {
326 print_joined("commit ", "commits ", &src_data
->generic
);
328 if (strcmp(".", srcs
.list
[i
]))
329 printf(" of %s", srcs
.list
[i
]);
332 if (!strcmp("master", current_branch
))
335 printf(" into %s\n", current_branch
);
341 head
= lookup_commit(head_sha1
);
342 init_revisions(&rev
, prefix
);
343 rev
.commit_format
= CMIT_FMT_ONELINE
;
344 rev
.ignore_merges
= 1;
347 for (i
= 0; i
< origins
.nr
; i
++)
348 shortlog(origins
.list
[i
], origins
.payload
[i
],
352 /* No cleanup yet; is standalone anyway */