8 static const char *fmt_merge_msg_usage
=
9 "git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]";
11 static int merge_summary
= 0;
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
= realloc(list
->list
, sizeof(char *) * list
->alloc
);
31 list
->payload
= realloc(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
++) {
59 free(list
->payload
[i
]);
63 list
->nr
= list
->alloc
= 0;
67 struct list branch
, tag
, r_branch
, generic
;
71 static struct list srcs
= { NULL
, NULL
, 0, 0};
72 static struct list origins
= { NULL
, NULL
, 0, 0};
74 static int handle_line(char *line
)
76 int i
, len
= strlen(line
);
79 struct src_data
*src_data
;
82 if (len
< 43 || line
[40] != '\t')
85 if (!strncmp(line
+ 41, "not-for-merge", 13))
93 i
= get_sha1(line
, sha1
);
98 if (line
[len
- 1] == '\n')
102 src
= strstr(line
, " of ");
112 i
= find_in_list(&srcs
, src
);
115 append_to_list(&srcs
, strdup(src
),
116 xcalloc(1, sizeof(struct src_data
)));
118 src_data
= srcs
.payload
[i
];
121 origin
= strdup(src
);
122 src_data
->head_status
|= 1;
123 } else if (!strncmp(line
, "branch ", 7)) {
124 origin
= strdup(line
+ 7);
125 append_to_list(&src_data
->branch
, origin
, NULL
);
126 src_data
->head_status
|= 2;
127 } else if (!strncmp(line
, "tag ", 4)) {
129 append_to_list(&src_data
->tag
, strdup(origin
+ 4), NULL
);
130 src_data
->head_status
|= 2;
131 } else if (!strncmp(line
, "remote branch ", 14)) {
132 origin
= strdup(line
+ 14);
133 append_to_list(&src_data
->r_branch
, origin
, NULL
);
134 src_data
->head_status
|= 2;
136 origin
= strdup(src
);
137 append_to_list(&src_data
->generic
, strdup(line
), NULL
);
138 src_data
->head_status
|= 2;
141 if (!strcmp(".", src
) || !strcmp(src
, origin
)) {
142 int len
= strlen(origin
);
143 if (origin
[0] == '\'' && origin
[len
- 1] == '\'') {
144 char *new_origin
= malloc(len
- 1);
145 memcpy(new_origin
, origin
+ 1, len
- 2);
146 new_origin
[len
- 1] = 0;
149 origin
= strdup(origin
);
151 char *new_origin
= malloc(strlen(origin
) + strlen(src
) + 5);
152 sprintf(new_origin
, "%s of %s", origin
, src
);
155 append_to_list(&origins
, origin
, sha1
);
159 static void print_joined(const char *singular
, const char *plural
,
165 printf("%s%s", singular
, list
->list
[0]);
168 printf("%s", plural
);
169 for (i
= 0; i
< list
->nr
- 1; i
++)
170 printf("%s%s", i
> 0 ? ", " : "", list
->list
[i
]);
171 printf(" and %s", list
->list
[list
->nr
- 1]);
175 static void shortlog(const char *name
, unsigned char *sha1
,
176 struct commit
*head
, struct rev_info
*rev
, int limit
)
179 struct commit
*commit
;
180 struct object
*branch
;
181 struct list subjects
= { NULL
, NULL
, 0, 0 };
182 int flags
= UNINTERESTING
| TREECHANGE
| SEEN
| SHOWN
| ADDED
;
184 branch
= deref_tag(parse_object(sha1
), sha1_to_hex(sha1
), 40);
185 if (!branch
|| branch
->type
!= OBJ_COMMIT
)
188 setup_revisions(0, NULL
, rev
, NULL
);
189 rev
->ignore_merges
= 1;
190 add_pending_object(rev
, branch
, name
);
191 add_pending_object(rev
, &head
->object
, "^HEAD");
192 head
->object
.flags
|= UNINTERESTING
;
193 prepare_revision_walk(rev
);
194 while ((commit
= get_revision(rev
)) != NULL
) {
195 char *oneline
, *bol
, *eol
;
198 if (commit
->parents
&& commit
->parents
->next
)
202 if (subjects
.nr
> limit
)
205 bol
= strstr(commit
->buffer
, "\n\n");
207 append_to_list(&subjects
, strdup(sha1_to_hex(
208 commit
->object
.sha1
)),
214 eol
= strchr(bol
, '\n');
218 oneline
= malloc(len
+ 1);
219 memcpy(oneline
, bol
, len
);
222 oneline
= strdup(bol
);
223 append_to_list(&subjects
, oneline
, NULL
);
227 printf("\n* %s: (%d commits)\n", name
, count
);
229 printf("\n* %s:\n", name
);
231 for (i
= 0; i
< subjects
.nr
; i
++)
235 printf(" %s\n", subjects
.list
[i
]);
237 clear_commit_marks((struct commit
*)branch
, flags
);
238 clear_commit_marks(head
, flags
);
239 free_commit_list(rev
->commits
);
243 free_list(&subjects
);
246 int cmd_fmt_merge_msg(int argc
, const char **argv
, const char *prefix
)
248 int limit
= 20, i
= 0;
251 const char *sep
= "";
252 unsigned char head_sha1
[20];
253 const char *head
, *current_branch
;
255 git_config(fmt_merge_msg_config
);
258 if (!strcmp(argv
[1], "--summary"))
260 else if (!strcmp(argv
[1], "--no-summary"))
262 else if (!strcmp(argv
[1], "-F") || !strcmp(argv
[1], "--file")) {
265 if (!strcmp(argv
[2], "-"))
269 in
= fopen(argv
[2], "r");
278 usage(fmt_merge_msg_usage
);
280 /* get current branch */
281 head
= strdup(git_path("HEAD"));
282 current_branch
= resolve_ref(head
, head_sha1
, 1);
283 current_branch
+= strlen(head
) - 4;
285 if (!strncmp(current_branch
, "refs/heads/", 11))
286 current_branch
+= 11;
288 while (fgets(line
, sizeof(line
), in
)) {
292 if (handle_line(line
))
293 die ("Error in line %d: %s", i
, line
);
297 for (i
= 0; i
< srcs
.nr
; i
++) {
298 struct src_data
*src_data
= srcs
.payload
[i
];
299 const char *subsep
= "";
304 if (src_data
->head_status
== 1) {
305 printf(srcs
.list
[i
]);
308 if (src_data
->head_status
== 3) {
312 if (src_data
->branch
.nr
) {
315 print_joined("branch ", "branches ", &src_data
->branch
);
317 if (src_data
->r_branch
.nr
) {
320 print_joined("remote branch ", "remote branches ",
321 &src_data
->r_branch
);
323 if (src_data
->tag
.nr
) {
326 print_joined("tag ", "tags ", &src_data
->tag
);
328 if (src_data
->generic
.nr
) {
330 print_joined("commit ", "commits ", &src_data
->generic
);
332 if (strcmp(".", srcs
.list
[i
]))
333 printf(" of %s", srcs
.list
[i
]);
336 if (!strcmp("master", current_branch
))
339 printf(" into %s\n", current_branch
);
345 head
= lookup_commit(head_sha1
);
346 init_revisions(&rev
, prefix
);
347 rev
.commit_format
= CMIT_FMT_ONELINE
;
348 rev
.ignore_merges
= 1;
351 for (i
= 0; i
< origins
.nr
; i
++)
352 shortlog(origins
.list
[i
], origins
.payload
[i
],
356 /* No cleanup yet; is standalone anyway */