]>
Commit | Line | Data |
---|---|---|
70827b15 LT |
1 | /* |
2 | * Builtin "git log" and related commands (show, whatchanged) | |
3 | * | |
4 | * (C) Copyright 2006 Linus Torvalds | |
5 | * 2006 Junio Hamano | |
6 | */ | |
7 | #include "cache.h" | |
6b2f2d98 | 8 | #include "color.h" |
70827b15 LT |
9 | #include "commit.h" |
10 | #include "diff.h" | |
11 | #include "revision.h" | |
12 | #include "log-tree.h" | |
91efcf60 | 13 | #include "builtin.h" |
5d7eeee2 | 14 | #include "tag.h" |
cf39f54e | 15 | #include "reflog-walk.h" |
5d23e133 | 16 | #include "patch-ids.h" |
ca135e7a | 17 | #include "refs.h" |
a5a27c79 | 18 | #include "run-command.h" |
2bda2cf4 | 19 | #include "shortlog.h" |
70827b15 | 20 | |
0f03ca94 | 21 | static int default_show_root = 1; |
d54276f2 | 22 | static const char *fmt_patch_subject_prefix = "PATCH"; |
0f03ca94 | 23 | |
ca135e7a LT |
24 | static void add_name_decoration(const char *prefix, const char *name, struct object *obj) |
25 | { | |
26 | int plen = strlen(prefix); | |
27 | int nlen = strlen(name); | |
28 | struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen); | |
29 | memcpy(res->name, prefix, plen); | |
30 | memcpy(res->name + plen, name, nlen + 1); | |
31 | res->next = add_decoration(&name_decoration, obj, res); | |
32 | } | |
33 | ||
34 | static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data) | |
35 | { | |
36 | struct object *obj = parse_object(sha1); | |
37 | if (!obj) | |
38 | return 0; | |
39 | add_name_decoration("", refname, obj); | |
40 | while (obj->type == OBJ_TAG) { | |
41 | obj = ((struct tag *)obj)->tagged; | |
42 | if (!obj) | |
43 | break; | |
44 | add_name_decoration("tag: ", refname, obj); | |
45 | } | |
46 | return 0; | |
47 | } | |
48 | ||
a633fca0 | 49 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
70827b15 LT |
50 | struct rev_info *rev) |
51 | { | |
52883fbd | 52 | int i; |
ca135e7a | 53 | int decorate = 0; |
52883fbd | 54 | |
70827b15 LT |
55 | rev->abbrev = DEFAULT_ABBREV; |
56 | rev->commit_format = CMIT_FMT_DEFAULT; | |
57 | rev->verbose_header = 1; | |
8f67f8ae | 58 | DIFF_OPT_SET(&rev->diffopt, RECURSIVE); |
0f03ca94 | 59 | rev->show_root_diff = default_show_root; |
d54276f2 | 60 | rev->subject_prefix = fmt_patch_subject_prefix; |
70827b15 | 61 | argc = setup_revisions(argc, argv, rev, "HEAD"); |
9dafea26 TH |
62 | if (rev->diffopt.pickaxe || rev->diffopt.filter) |
63 | rev->always_show_header = 0; | |
8f67f8ae | 64 | if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) { |
750f7b66 LT |
65 | rev->always_show_header = 0; |
66 | if (rev->diffopt.nr_paths != 1) | |
67 | usage("git logs can only follow renames on one pathname at a time"); | |
68 | } | |
52883fbd JH |
69 | for (i = 1; i < argc; i++) { |
70 | const char *arg = argv[i]; | |
66688334 | 71 | if (!strcmp(arg, "--decorate")) { |
ca135e7a LT |
72 | if (!decorate) |
73 | for_each_ref(add_ref_decoration, NULL); | |
74 | decorate = 1; | |
75 | } else | |
52883fbd JH |
76 | die("unrecognized argument: %s", arg); |
77 | } | |
9dafea26 TH |
78 | } |
79 | ||
252a7c02 LT |
80 | /* |
81 | * This gives a rough estimate for how many commits we | |
82 | * will print out in the list. | |
83 | */ | |
84 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) | |
85 | { | |
86 | int n = 0; | |
87 | ||
88 | while (list) { | |
89 | struct commit *commit = list->item; | |
90 | unsigned int flags = commit->object.flags; | |
252a7c02 | 91 | list = list->next; |
7dc0fe3b | 92 | if (!(flags & (TREESAME | UNINTERESTING))) |
53b2c823 | 93 | n++; |
252a7c02 LT |
94 | } |
95 | return n; | |
96 | } | |
97 | ||
98 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
99 | { | |
100 | if (rev->shown_one) { | |
101 | rev->shown_one = 0; | |
102 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
103 | putchar(rev->diffopt.line_termination); | |
104 | } | |
105 | printf("Final output: %d %s\n", nr, stage); | |
106 | } | |
107 | ||
108 | struct itimerval early_output_timer; | |
109 | ||
cdcefbc9 LT |
110 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
111 | { | |
112 | int i = revs->early_output; | |
252a7c02 | 113 | int show_header = 1; |
cdcefbc9 LT |
114 | |
115 | sort_in_topological_order(&list, revs->lifo); | |
116 | while (list && i) { | |
117 | struct commit *commit = list->item; | |
252a7c02 LT |
118 | switch (simplify_commit(revs, commit)) { |
119 | case commit_show: | |
120 | if (show_header) { | |
121 | int n = estimate_commit_count(revs, list); | |
122 | show_early_header(revs, "incomplete", n); | |
123 | show_header = 0; | |
124 | } | |
125 | log_tree_commit(revs, commit); | |
126 | i--; | |
127 | break; | |
128 | case commit_ignore: | |
129 | break; | |
130 | case commit_error: | |
131 | return; | |
132 | } | |
cdcefbc9 | 133 | list = list->next; |
cdcefbc9 | 134 | } |
252a7c02 LT |
135 | |
136 | /* Did we already get enough commits for the early output? */ | |
137 | if (!i) | |
138 | return; | |
139 | ||
140 | /* | |
141 | * ..if no, then repeat it twice a second until we | |
142 | * do. | |
143 | * | |
144 | * NOTE! We don't use "it_interval", because if the | |
145 | * reader isn't listening, we want our output to be | |
146 | * throttled by the writing, and not have the timer | |
147 | * trigger every second even if we're blocked on a | |
148 | * reader! | |
149 | */ | |
150 | early_output_timer.it_value.tv_sec = 0; | |
151 | early_output_timer.it_value.tv_usec = 500000; | |
152 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
153 | } |
154 | ||
155 | static void early_output(int signal) | |
156 | { | |
157 | show_early_output = log_show_early; | |
158 | } | |
159 | ||
160 | static void setup_early_output(struct rev_info *rev) | |
161 | { | |
162 | struct sigaction sa; | |
cdcefbc9 LT |
163 | |
164 | /* | |
165 | * Set up the signal handler, minimally intrusively: | |
166 | * we only set a single volatile integer word (not | |
167 | * using sigatomic_t - trying to avoid unnecessary | |
168 | * system dependencies and headers), and using | |
169 | * SA_RESTART. | |
170 | */ | |
171 | memset(&sa, 0, sizeof(sa)); | |
172 | sa.sa_handler = early_output; | |
173 | sigemptyset(&sa.sa_mask); | |
174 | sa.sa_flags = SA_RESTART; | |
175 | sigaction(SIGALRM, &sa, NULL); | |
176 | ||
177 | /* | |
178 | * If we can get the whole output in less than a | |
179 | * tenth of a second, don't even bother doing the | |
180 | * early-output thing.. | |
181 | * | |
182 | * This is a one-time-only trigger. | |
183 | */ | |
252a7c02 LT |
184 | early_output_timer.it_value.tv_sec = 0; |
185 | early_output_timer.it_value.tv_usec = 100000; | |
186 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
187 | } |
188 | ||
189 | static void finish_early_output(struct rev_info *rev) | |
190 | { | |
252a7c02 | 191 | int n = estimate_commit_count(rev, rev->commits); |
cdcefbc9 | 192 | signal(SIGALRM, SIG_IGN); |
252a7c02 | 193 | show_early_header(rev, "done", n); |
cdcefbc9 LT |
194 | } |
195 | ||
9dafea26 TH |
196 | static int cmd_log_walk(struct rev_info *rev) |
197 | { | |
198 | struct commit *commit; | |
70827b15 | 199 | |
cdcefbc9 LT |
200 | if (rev->early_output) |
201 | setup_early_output(rev); | |
202 | ||
3d51e1b5 MK |
203 | if (prepare_revision_walk(rev)) |
204 | die("revision walk setup failed"); | |
cdcefbc9 LT |
205 | |
206 | if (rev->early_output) | |
207 | finish_early_output(rev); | |
208 | ||
70827b15 LT |
209 | while ((commit = get_revision(rev)) != NULL) { |
210 | log_tree_commit(rev, commit); | |
a6c73064 JS |
211 | if (!rev->reflog_info) { |
212 | /* we allow cycles in reflog ancestry */ | |
213 | free(commit->buffer); | |
214 | commit->buffer = NULL; | |
215 | } | |
cb115748 LT |
216 | free_commit_list(commit->parents); |
217 | commit->parents = NULL; | |
70827b15 LT |
218 | } |
219 | return 0; | |
220 | } | |
221 | ||
0f03ca94 PB |
222 | static int git_log_config(const char *var, const char *value) |
223 | { | |
d54276f2 AR |
224 | if (!strcmp(var, "format.subjectprefix")) { |
225 | if (!value) | |
995c4527 | 226 | config_error_nonbool(var); |
d54276f2 AR |
227 | fmt_patch_subject_prefix = xstrdup(value); |
228 | return 0; | |
229 | } | |
0f03ca94 PB |
230 | if (!strcmp(var, "log.showroot")) { |
231 | default_show_root = git_config_bool(var, value); | |
232 | return 0; | |
233 | } | |
234 | return git_diff_ui_config(var, value); | |
235 | } | |
236 | ||
a633fca0 | 237 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
238 | { |
239 | struct rev_info rev; | |
240 | ||
0f03ca94 | 241 | git_config(git_log_config); |
6b2f2d98 MK |
242 | |
243 | if (diff_use_color_default == -1) | |
244 | diff_use_color_default = git_use_color_default; | |
245 | ||
db6296a5 | 246 | init_revisions(&rev, prefix); |
70827b15 | 247 | rev.diff = 1; |
9202434c | 248 | rev.simplify_history = 0; |
a633fca0 | 249 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 TH |
250 | if (!rev.diffopt.output_format) |
251 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
252 | return cmd_log_walk(&rev); | |
70827b15 LT |
253 | } |
254 | ||
56122ed8 JS |
255 | static void show_tagger(char *buf, int len, struct rev_info *rev) |
256 | { | |
257 | char *email_end, *p; | |
258 | unsigned long date; | |
259 | int tz; | |
260 | ||
261 | email_end = memchr(buf, '>', len); | |
262 | if (!email_end) | |
263 | return; | |
264 | p = ++email_end; | |
265 | while (isspace(*p)) | |
266 | p++; | |
267 | date = strtoul(p, &p, 10); | |
268 | while (isspace(*p)) | |
269 | p++; | |
270 | tz = (int)strtol(p, NULL, 10); | |
271 | printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf, | |
272 | show_date(date, tz, rev->date_mode)); | |
273 | } | |
274 | ||
275 | static int show_object(const unsigned char *sha1, int show_tag_object, | |
276 | struct rev_info *rev) | |
5d7eeee2 JS |
277 | { |
278 | unsigned long size; | |
21666f1a NP |
279 | enum object_type type; |
280 | char *buf = read_sha1_file(sha1, &type, &size); | |
5d7eeee2 JS |
281 | int offset = 0; |
282 | ||
283 | if (!buf) | |
284 | return error("Could not read object %s", sha1_to_hex(sha1)); | |
285 | ||
56122ed8 JS |
286 | if (show_tag_object) |
287 | while (offset < size && buf[offset] != '\n') { | |
288 | int new_offset = offset + 1; | |
5d7eeee2 JS |
289 | while (new_offset < size && buf[new_offset++] != '\n') |
290 | ; /* do nothing */ | |
56122ed8 JS |
291 | if (!prefixcmp(buf + offset, "tagger ")) |
292 | show_tagger(buf + offset + 7, | |
293 | new_offset - offset - 7, rev); | |
5d7eeee2 JS |
294 | offset = new_offset; |
295 | } | |
296 | ||
297 | if (offset < size) | |
298 | fwrite(buf + offset, size - offset, 1, stdout); | |
299 | free(buf); | |
300 | return 0; | |
301 | } | |
302 | ||
303 | static int show_tree_object(const unsigned char *sha1, | |
304 | const char *base, int baselen, | |
305 | const char *pathname, unsigned mode, int stage) | |
306 | { | |
307 | printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
308 | return 0; | |
309 | } | |
310 | ||
a633fca0 | 311 | int cmd_show(int argc, const char **argv, const char *prefix) |
70827b15 LT |
312 | { |
313 | struct rev_info rev; | |
5d7eeee2 JS |
314 | struct object_array_entry *objects; |
315 | int i, count, ret = 0; | |
70827b15 | 316 | |
0f03ca94 | 317 | git_config(git_log_config); |
6b2f2d98 MK |
318 | |
319 | if (diff_use_color_default == -1) | |
320 | diff_use_color_default = git_use_color_default; | |
321 | ||
db6296a5 | 322 | init_revisions(&rev, prefix); |
70827b15 | 323 | rev.diff = 1; |
70827b15 LT |
324 | rev.combine_merges = 1; |
325 | rev.dense_combined_merges = 1; | |
326 | rev.always_show_header = 1; | |
327 | rev.ignore_merges = 0; | |
328 | rev.no_walk = 1; | |
a633fca0 | 329 | cmd_log_init(argc, argv, prefix, &rev); |
5d7eeee2 JS |
330 | |
331 | count = rev.pending.nr; | |
332 | objects = rev.pending.objects; | |
333 | for (i = 0; i < count && !ret; i++) { | |
334 | struct object *o = objects[i].item; | |
335 | const char *name = objects[i].name; | |
336 | switch (o->type) { | |
337 | case OBJ_BLOB: | |
56122ed8 | 338 | ret = show_object(o->sha1, 0, NULL); |
5d7eeee2 JS |
339 | break; |
340 | case OBJ_TAG: { | |
341 | struct tag *t = (struct tag *)o; | |
342 | ||
56122ed8 | 343 | printf("%stag %s%s\n", |
8f67f8ae | 344 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 345 | t->tag, |
8f67f8ae | 346 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
56122ed8 | 347 | ret = show_object(o->sha1, 1, &rev); |
5d7eeee2 JS |
348 | objects[i].item = (struct object *)t->tagged; |
349 | i--; | |
350 | break; | |
351 | } | |
352 | case OBJ_TREE: | |
353 | printf("%stree %s%s\n\n", | |
8f67f8ae | 354 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 355 | name, |
8f67f8ae | 356 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
5d7eeee2 JS |
357 | read_tree_recursive((struct tree *)o, "", 0, 0, NULL, |
358 | show_tree_object); | |
359 | break; | |
360 | case OBJ_COMMIT: | |
361 | rev.pending.nr = rev.pending.alloc = 0; | |
362 | rev.pending.objects = NULL; | |
363 | add_object_array(o, name, &rev.pending); | |
364 | ret = cmd_log_walk(&rev); | |
365 | break; | |
366 | default: | |
367 | ret = error("Unknown type: %d", o->type); | |
368 | } | |
369 | } | |
370 | free(objects); | |
371 | return ret; | |
70827b15 LT |
372 | } |
373 | ||
cf39f54e LT |
374 | /* |
375 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
376 | */ | |
377 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) | |
378 | { | |
379 | struct rev_info rev; | |
380 | ||
381 | git_config(git_log_config); | |
6b2f2d98 MK |
382 | |
383 | if (diff_use_color_default == -1) | |
384 | diff_use_color_default = git_use_color_default; | |
385 | ||
cf39f54e LT |
386 | init_revisions(&rev, prefix); |
387 | init_reflog_walk(&rev.reflog_info); | |
388 | rev.abbrev_commit = 1; | |
389 | rev.verbose_header = 1; | |
390 | cmd_log_init(argc, argv, prefix, &rev); | |
391 | ||
392 | /* | |
393 | * This means that we override whatever commit format the user gave | |
394 | * on the cmd line. Sad, but cmd_log_init() currently doesn't | |
395 | * allow us to set a different default. | |
396 | */ | |
397 | rev.commit_format = CMIT_FMT_ONELINE; | |
398 | rev.always_show_header = 1; | |
399 | ||
400 | /* | |
401 | * We get called through "git reflog", so unlike the other log | |
402 | * routines, we need to set up our pager manually.. | |
403 | */ | |
404 | setup_pager(); | |
405 | ||
406 | return cmd_log_walk(&rev); | |
407 | } | |
408 | ||
a633fca0 | 409 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
410 | { |
411 | struct rev_info rev; | |
412 | ||
0f03ca94 | 413 | git_config(git_log_config); |
6b2f2d98 MK |
414 | |
415 | if (diff_use_color_default == -1) | |
416 | diff_use_color_default = git_use_color_default; | |
417 | ||
db6296a5 | 418 | init_revisions(&rev, prefix); |
70827b15 | 419 | rev.always_show_header = 1; |
a633fca0 | 420 | cmd_log_init(argc, argv, prefix, &rev); |
9dafea26 | 421 | return cmd_log_walk(&rev); |
70827b15 | 422 | } |
91efcf60 | 423 | |
c06d2daa RR |
424 | /* format-patch */ |
425 | #define FORMAT_PATCH_NAME_MAX 64 | |
426 | ||
0377db77 JS |
427 | static int istitlechar(char c) |
428 | { | |
429 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | |
430 | (c >= '0' && c <= '9') || c == '.' || c == '_'; | |
431 | } | |
432 | ||
917a8f89 | 433 | static const char *fmt_patch_suffix = ".patch"; |
49604a4d BG |
434 | static int numbered = 0; |
435 | static int auto_number = 0; | |
20ff0680 | 436 | |
3ee79d9f DB |
437 | static char **extra_hdr; |
438 | static int extra_hdr_nr; | |
439 | static int extra_hdr_alloc; | |
440 | ||
441 | static char **extra_to; | |
442 | static int extra_to_nr; | |
443 | static int extra_to_alloc; | |
444 | ||
445 | static char **extra_cc; | |
446 | static int extra_cc_nr; | |
447 | static int extra_cc_alloc; | |
448 | ||
449 | static void add_header(const char *value) | |
450 | { | |
451 | int len = strlen(value); | |
452 | while (value[len - 1] == '\n') | |
453 | len--; | |
454 | if (!strncasecmp(value, "to: ", 4)) { | |
455 | ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc); | |
456 | extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4); | |
457 | return; | |
458 | } | |
459 | if (!strncasecmp(value, "cc: ", 4)) { | |
460 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); | |
461 | extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4); | |
462 | return; | |
463 | } | |
464 | ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc); | |
465 | extra_hdr[extra_hdr_nr++] = xstrndup(value, len); | |
466 | } | |
467 | ||
20ff0680 JS |
468 | static int git_format_config(const char *var, const char *value) |
469 | { | |
470 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 JH |
471 | if (!value) |
472 | die("format.headers without value"); | |
3ee79d9f | 473 | add_header(value); |
20ff0680 JS |
474 | return 0; |
475 | } | |
03eeaeae JH |
476 | if (!strcmp(var, "format.suffix")) { |
477 | if (!value) | |
90f5c186 | 478 | return config_error_nonbool(var); |
03eeaeae JH |
479 | fmt_patch_suffix = xstrdup(value); |
480 | return 0; | |
481 | } | |
a159ca0c | 482 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
f3aafa4d RA |
483 | return 0; |
484 | } | |
49604a4d | 485 | if (!strcmp(var, "format.numbered")) { |
90f5c186 | 486 | if (value && !strcasecmp(value, "auto")) { |
49604a4d BG |
487 | auto_number = 1; |
488 | return 0; | |
489 | } | |
49604a4d BG |
490 | numbered = git_config_bool(var, value); |
491 | return 0; | |
492 | } | |
dbd21447 | 493 | |
0f03ca94 | 494 | return git_log_config(var, value); |
20ff0680 JS |
495 | } |
496 | ||
497 | ||
a5a27c79 DB |
498 | static const char *get_oneline_for_filename(struct commit *commit, |
499 | int keep_subject) | |
500 | { | |
501 | static char filename[PATH_MAX]; | |
502 | char *sol; | |
503 | int len = 0; | |
504 | int suffix_len = strlen(fmt_patch_suffix) + 1; | |
505 | ||
506 | sol = strstr(commit->buffer, "\n\n"); | |
507 | if (!sol) | |
508 | filename[0] = '\0'; | |
509 | else { | |
510 | int j, space = 0; | |
511 | ||
512 | sol += 2; | |
513 | /* strip [PATCH] or [PATCH blabla] */ | |
514 | if (!keep_subject && !prefixcmp(sol, "[PATCH")) { | |
515 | char *eos = strchr(sol + 6, ']'); | |
516 | if (eos) { | |
517 | while (isspace(*eos)) | |
518 | eos++; | |
519 | sol = eos; | |
520 | } | |
521 | } | |
522 | ||
523 | for (j = 0; | |
524 | j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 && | |
525 | len < sizeof(filename) - suffix_len && | |
526 | sol[j] && sol[j] != '\n'; | |
527 | j++) { | |
528 | if (istitlechar(sol[j])) { | |
529 | if (space) { | |
530 | filename[len++] = '-'; | |
531 | space = 0; | |
532 | } | |
533 | filename[len++] = sol[j]; | |
534 | if (sol[j] == '.') | |
535 | while (sol[j + 1] == '.') | |
536 | j++; | |
537 | } else | |
538 | space = 1; | |
539 | } | |
540 | while (filename[len - 1] == '.' | |
541 | || filename[len - 1] == '-') | |
542 | len--; | |
543 | filename[len] = '\0'; | |
544 | } | |
545 | return filename; | |
546 | } | |
547 | ||
81f3a188 | 548 | static FILE *realstdout = NULL; |
efd02016 | 549 | static const char *output_directory = NULL; |
81f3a188 | 550 | |
a5a27c79 | 551 | static int reopen_stdout(const char *oneline, int nr, int total) |
0377db77 | 552 | { |
c06d2daa | 553 | char filename[PATH_MAX]; |
2448482b | 554 | int len = 0; |
c06d2daa | 555 | int suffix_len = strlen(fmt_patch_suffix) + 1; |
0377db77 | 556 | |
2448482b | 557 | if (output_directory) { |
a5a27c79 DB |
558 | len = snprintf(filename, sizeof(filename), "%s", |
559 | output_directory); | |
560 | if (len >= | |
c06d2daa RR |
561 | sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len) |
562 | return error("name of output directory is too long"); | |
2448482b JS |
563 | if (filename[len - 1] != '/') |
564 | filename[len++] = '/'; | |
565 | } | |
0377db77 | 566 | |
a5a27c79 DB |
567 | if (!oneline) |
568 | len += sprintf(filename + len, "%d", nr); | |
569 | else { | |
570 | len += sprintf(filename + len, "%04d-", nr); | |
571 | len += snprintf(filename + len, sizeof(filename) - len - 1 | |
572 | - suffix_len, "%s", oneline); | |
e6ff0f42 | 573 | strcpy(filename + len, fmt_patch_suffix); |
0377db77 | 574 | } |
e6ff0f42 | 575 | |
81f3a188 | 576 | fprintf(realstdout, "%s\n", filename); |
c06d2daa RR |
577 | if (freopen(filename, "w", stdout) == NULL) |
578 | return error("Cannot open patch file %s",filename); | |
c06d2daa | 579 | |
e6ff0f42 | 580 | return 0; |
0377db77 JS |
581 | } |
582 | ||
5d23e133 | 583 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix) |
9c6efa36 JS |
584 | { |
585 | struct rev_info check_rev; | |
586 | struct commit *commit; | |
587 | struct object *o1, *o2; | |
588 | unsigned flags1, flags2; | |
9c6efa36 JS |
589 | |
590 | if (rev->pending.nr != 2) | |
591 | die("Need exactly one range."); | |
592 | ||
593 | o1 = rev->pending.objects[0].item; | |
594 | flags1 = o1->flags; | |
595 | o2 = rev->pending.objects[1].item; | |
596 | flags2 = o2->flags; | |
597 | ||
598 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
599 | die("Not a range."); | |
600 | ||
5d23e133 | 601 | init_patch_ids(ids); |
9c6efa36 JS |
602 | |
603 | /* given a range a..b get all patch ids for b..a */ | |
db6296a5 | 604 | init_revisions(&check_rev, prefix); |
9c6efa36 JS |
605 | o1->flags ^= UNINTERESTING; |
606 | o2->flags ^= UNINTERESTING; | |
607 | add_pending_object(&check_rev, o1, "o1"); | |
608 | add_pending_object(&check_rev, o2, "o2"); | |
3d51e1b5 MK |
609 | if (prepare_revision_walk(&check_rev)) |
610 | die("revision walk setup failed"); | |
9c6efa36 JS |
611 | |
612 | while ((commit = get_revision(&check_rev)) != NULL) { | |
613 | /* ignore merges */ | |
614 | if (commit->parents && commit->parents->next) | |
615 | continue; | |
616 | ||
5d23e133 | 617 | add_commit_patch_id(commit, ids); |
9c6efa36 JS |
618 | } |
619 | ||
620 | /* reset for next revision walk */ | |
81db0941 JS |
621 | clear_commit_marks((struct commit *)o1, |
622 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
623 | clear_commit_marks((struct commit *)o2, | |
624 | SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
625 | o1->flags = flags1; |
626 | o2->flags = flags2; | |
627 | } | |
628 | ||
e1a37346 | 629 | static void gen_message_id(struct rev_info *info, char *base) |
d1566f78 | 630 | { |
774751a8 | 631 | const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME); |
d1566f78 JT |
632 | const char *email_start = strrchr(committer, '<'); |
633 | const char *email_end = strrchr(committer, '>'); | |
e1a37346 DB |
634 | struct strbuf buf; |
635 | if (!email_start || !email_end || email_start > email_end - 1) | |
d1566f78 | 636 | die("Could not extract email from committer identity."); |
e1a37346 DB |
637 | strbuf_init(&buf, 0); |
638 | strbuf_addf(&buf, "%s.%lu.git.%.*s", base, | |
639 | (unsigned long) time(NULL), | |
640 | (int)(email_end - email_start - 1), email_start + 1); | |
641 | info->message_id = strbuf_detach(&buf, NULL); | |
d1566f78 JT |
642 | } |
643 | ||
2bda2cf4 DB |
644 | static void make_cover_letter(struct rev_info *rev, int use_stdout, |
645 | int numbered, int numbered_files, | |
646 | struct commit *origin, | |
647 | int nr, struct commit **list, struct commit *head) | |
a5a27c79 DB |
648 | { |
649 | const char *committer; | |
39fe578b | 650 | char *head_sha1; |
a5a27c79 DB |
651 | const char *subject_start = NULL; |
652 | const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; | |
653 | const char *msg; | |
654 | const char *extra_headers = rev->extra_headers; | |
2bda2cf4 | 655 | struct shortlog log; |
a5a27c79 | 656 | struct strbuf sb; |
2bda2cf4 | 657 | int i; |
a5a27c79 | 658 | const char *encoding = "utf-8"; |
39fe578b | 659 | struct diff_options opts; |
a5a27c79 DB |
660 | |
661 | if (rev->commit_format != CMIT_FMT_EMAIL) | |
662 | die("Cover letter needs email format"); | |
663 | ||
664 | if (!use_stdout && reopen_stdout(numbered_files ? | |
665 | NULL : "cover-letter", 0, rev->total)) | |
666 | return; | |
667 | ||
a5a27c79 DB |
668 | head_sha1 = sha1_to_hex(head->object.sha1); |
669 | ||
670 | log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers); | |
671 | ||
672 | committer = git_committer_info(0); | |
673 | ||
674 | msg = body; | |
675 | strbuf_init(&sb, 0); | |
676 | pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822, | |
677 | encoding); | |
678 | pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers, | |
679 | encoding, 0); | |
680 | pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0); | |
681 | printf("%s\n", sb.buf); | |
682 | ||
683 | strbuf_release(&sb); | |
684 | ||
2bda2cf4 | 685 | shortlog_init(&log); |
859c4fbe JS |
686 | log.wrap_lines = 1; |
687 | log.wrap = 72; | |
688 | log.in1 = 2; | |
689 | log.in2 = 4; | |
2bda2cf4 DB |
690 | for (i = 0; i < nr; i++) |
691 | shortlog_add_commit(&log, list[i]); | |
692 | ||
693 | shortlog_output(&log); | |
694 | ||
a5a27c79 | 695 | /* |
2bda2cf4 | 696 | * We can only do diffstat with a unique reference point |
a5a27c79 DB |
697 | */ |
698 | if (!origin) | |
699 | return; | |
700 | ||
5d02294c JS |
701 | memcpy(&opts, &rev->diffopt, sizeof(opts)); |
702 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
a5a27c79 | 703 | |
39fe578b DB |
704 | diff_setup_done(&opts); |
705 | ||
706 | diff_tree_sha1(origin->tree->object.sha1, | |
707 | head->tree->object.sha1, | |
708 | "", &opts); | |
709 | diffcore_std(&opts); | |
710 | diff_flush(&opts); | |
a5a27c79 | 711 | |
a5a27c79 | 712 | printf("\n"); |
d1566f78 JT |
713 | } |
714 | ||
8419d2ee JH |
715 | static const char *clean_message_id(const char *msg_id) |
716 | { | |
717 | char ch; | |
718 | const char *a, *z, *m; | |
8419d2ee JH |
719 | |
720 | m = msg_id; | |
721 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
722 | m++; | |
723 | a = m; | |
724 | z = NULL; | |
725 | while ((ch = *m)) { | |
726 | if (!isspace(ch) && (ch != '>')) | |
727 | z = m; | |
728 | m++; | |
729 | } | |
730 | if (!z) | |
731 | die("insane in-reply-to: %s", msg_id); | |
732 | if (++z == m) | |
733 | return a; | |
182af834 | 734 | return xmemdupz(a, z - a); |
8419d2ee JH |
735 | } |
736 | ||
a633fca0 | 737 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
738 | { |
739 | struct commit *commit; | |
740 | struct commit **list = NULL; | |
741 | struct rev_info rev; | |
2448482b | 742 | int nr = 0, total, i, j; |
0377db77 | 743 | int use_stdout = 0; |
fa0f02df | 744 | int start_number = -1; |
8ac80a57 | 745 | int keep_subject = 0; |
e6ff0f42 | 746 | int numbered_files = 0; /* _just_ numbers */ |
2d9e4a47 | 747 | int subject_prefix = 0; |
9c6efa36 | 748 | int ignore_if_in_upstream = 0; |
cc35de84 | 749 | int thread = 0; |
a5a27c79 | 750 | int cover_letter = 0; |
2bda2cf4 | 751 | int boundary_count = 0; |
a5a27c79 | 752 | struct commit *origin = NULL, *head = NULL; |
76af0734 | 753 | const char *in_reply_to = NULL; |
5d23e133 | 754 | struct patch_ids ids; |
cf2251b6 | 755 | char *add_signoff = NULL; |
3ee79d9f | 756 | struct strbuf buf; |
91efcf60 | 757 | |
97beb812 | 758 | git_config(git_format_config); |
db6296a5 | 759 | init_revisions(&rev, prefix); |
91efcf60 JH |
760 | rev.commit_format = CMIT_FMT_EMAIL; |
761 | rev.verbose_header = 1; | |
762 | rev.diff = 1; | |
91efcf60 JH |
763 | rev.combine_merges = 0; |
764 | rev.ignore_merges = 1; | |
27e1b127 | 765 | rev.diffopt.msg_sep = ""; |
8f67f8ae | 766 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
91efcf60 | 767 | |
dbd21447 | 768 | rev.subject_prefix = fmt_patch_subject_prefix; |
20ff0680 | 769 | |
2448482b JS |
770 | /* |
771 | * Parse the arguments before setup_revisions(), or something | |
2dc189a3 | 772 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
2448482b JS |
773 | * possibly a valid SHA1. |
774 | */ | |
775 | for (i = 1, j = 1; i < argc; i++) { | |
776 | if (!strcmp(argv[i], "--stdout")) | |
0377db77 | 777 | use_stdout = 1; |
596524b3 JS |
778 | else if (!strcmp(argv[i], "-n") || |
779 | !strcmp(argv[i], "--numbered")) | |
780 | numbered = 1; | |
49604a4d BG |
781 | else if (!strcmp(argv[i], "-N") || |
782 | !strcmp(argv[i], "--no-numbered")) { | |
783 | numbered = 0; | |
784 | auto_number = 0; | |
785 | } | |
cc44c765 | 786 | else if (!prefixcmp(argv[i], "--start-number=")) |
fa0f02df | 787 | start_number = strtol(argv[i] + 15, NULL, 10); |
e6ff0f42 JL |
788 | else if (!strcmp(argv[i], "--numbered-files")) |
789 | numbered_files = 1; | |
fa0f02df JS |
790 | else if (!strcmp(argv[i], "--start-number")) { |
791 | i++; | |
792 | if (i == argc) | |
793 | die("Need a number for --start-number"); | |
794 | start_number = strtol(argv[i], NULL, 10); | |
cf2251b6 | 795 | } |
736cc67d DB |
796 | else if (!prefixcmp(argv[i], "--cc=")) { |
797 | ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc); | |
798 | extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5); | |
799 | } | |
cf2251b6 | 800 | else if (!strcmp(argv[i], "-k") || |
8ac80a57 JS |
801 | !strcmp(argv[i], "--keep-subject")) { |
802 | keep_subject = 1; | |
803 | rev.total = -1; | |
cf2251b6 | 804 | } |
efd02016 JH |
805 | else if (!strcmp(argv[i], "--output-directory") || |
806 | !strcmp(argv[i], "-o")) { | |
2448482b | 807 | i++; |
efd02016 JH |
808 | if (argc <= i) |
809 | die("Which directory?"); | |
810 | if (output_directory) | |
811 | die("Two output directories?"); | |
812 | output_directory = argv[i]; | |
698ce6f8 | 813 | } |
cf2251b6 JH |
814 | else if (!strcmp(argv[i], "--signoff") || |
815 | !strcmp(argv[i], "-s")) { | |
6c4cca1c EB |
816 | const char *committer; |
817 | const char *endpos; | |
774751a8 | 818 | committer = git_committer_info(IDENT_ERROR_ON_NO_NAME); |
6c4cca1c | 819 | endpos = strchr(committer, '>'); |
cf2251b6 JH |
820 | if (!endpos) |
821 | die("bogos committer info %s\n", committer); | |
182af834 | 822 | add_signoff = xmemdupz(committer, endpos - committer + 1); |
cf2251b6 | 823 | } |
c112f689 | 824 | else if (!strcmp(argv[i], "--attach")) { |
698ce6f8 | 825 | rev.mime_boundary = git_version_string; |
c112f689 JS |
826 | rev.no_inline = 1; |
827 | } | |
828 | else if (!prefixcmp(argv[i], "--attach=")) { | |
829 | rev.mime_boundary = argv[i] + 9; | |
830 | rev.no_inline = 1; | |
831 | } | |
832 | else if (!strcmp(argv[i], "--inline")) { | |
833 | rev.mime_boundary = git_version_string; | |
834 | rev.no_inline = 0; | |
835 | } | |
836 | else if (!prefixcmp(argv[i], "--inline=")) { | |
698ce6f8 | 837 | rev.mime_boundary = argv[i] + 9; |
c112f689 JS |
838 | rev.no_inline = 0; |
839 | } | |
9c6efa36 JS |
840 | else if (!strcmp(argv[i], "--ignore-if-in-upstream")) |
841 | ignore_if_in_upstream = 1; | |
cc35de84 JT |
842 | else if (!strcmp(argv[i], "--thread")) |
843 | thread = 1; | |
cc44c765 | 844 | else if (!prefixcmp(argv[i], "--in-reply-to=")) |
da56645d JT |
845 | in_reply_to = argv[i] + 14; |
846 | else if (!strcmp(argv[i], "--in-reply-to")) { | |
847 | i++; | |
848 | if (i == argc) | |
849 | die("Need a Message-Id for --in-reply-to"); | |
850 | in_reply_to = argv[i]; | |
2d9e4a47 RJ |
851 | } else if (!prefixcmp(argv[i], "--subject-prefix=")) { |
852 | subject_prefix = 1; | |
853 | rev.subject_prefix = argv[i] + 17; | |
854 | } else if (!prefixcmp(argv[i], "--suffix=")) | |
03eeaeae | 855 | fmt_patch_suffix = argv[i] + 9; |
a5a27c79 DB |
856 | else if (!strcmp(argv[i], "--cover-letter")) |
857 | cover_letter = 1; | |
698ce6f8 | 858 | else |
2448482b | 859 | argv[j++] = argv[i]; |
0377db77 | 860 | } |
2448482b JS |
861 | argc = j; |
862 | ||
3ee79d9f DB |
863 | strbuf_init(&buf, 0); |
864 | ||
865 | for (i = 0; i < extra_hdr_nr; i++) { | |
866 | strbuf_addstr(&buf, extra_hdr[i]); | |
867 | strbuf_addch(&buf, '\n'); | |
868 | } | |
869 | ||
870 | if (extra_to_nr) | |
871 | strbuf_addstr(&buf, "To: "); | |
872 | for (i = 0; i < extra_to_nr; i++) { | |
873 | if (i) | |
874 | strbuf_addstr(&buf, " "); | |
875 | strbuf_addstr(&buf, extra_to[i]); | |
876 | if (i + 1 < extra_to_nr) | |
877 | strbuf_addch(&buf, ','); | |
878 | strbuf_addch(&buf, '\n'); | |
879 | } | |
880 | ||
881 | if (extra_cc_nr) | |
882 | strbuf_addstr(&buf, "Cc: "); | |
883 | for (i = 0; i < extra_cc_nr; i++) { | |
884 | if (i) | |
885 | strbuf_addstr(&buf, " "); | |
886 | strbuf_addstr(&buf, extra_cc[i]); | |
887 | if (i + 1 < extra_cc_nr) | |
888 | strbuf_addch(&buf, ','); | |
889 | strbuf_addch(&buf, '\n'); | |
890 | } | |
891 | ||
892 | rev.extra_headers = strbuf_detach(&buf, 0); | |
893 | ||
add5c8a5 | 894 | if (start_number < 0) |
fa0f02df | 895 | start_number = 1; |
63b398a4 | 896 | if (numbered && keep_subject) |
8ac80a57 | 897 | die ("-n and -k are mutually exclusive."); |
2d9e4a47 RJ |
898 | if (keep_subject && subject_prefix) |
899 | die ("--subject-prefix and -k are mutually exclusive."); | |
e6ff0f42 JL |
900 | if (numbered_files && use_stdout) |
901 | die ("--numbered-files and --stdout are mutually exclusive."); | |
8ac80a57 | 902 | |
2448482b JS |
903 | argc = setup_revisions(argc, argv, &rev, "HEAD"); |
904 | if (argc > 1) | |
905 | die ("unrecognized argument: %s", argv[1]); | |
0377db77 | 906 | |
c9b5ef99 | 907 | if (!rev.diffopt.output_format) |
c261e438 | 908 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH; |
c9b5ef99 | 909 | |
8f67f8ae PH |
910 | if (!DIFF_OPT_TST(&rev.diffopt, TEXT)) |
911 | DIFF_OPT_SET(&rev.diffopt, BINARY); | |
e47f306d | 912 | |
90f70a91 | 913 | if (!output_directory && !use_stdout) |
77e565d8 ML |
914 | output_directory = prefix; |
915 | ||
efd02016 JH |
916 | if (output_directory) { |
917 | if (use_stdout) | |
918 | die("standard output, or directory, which one?"); | |
919 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) | |
920 | die("Could not create directory %s", | |
921 | output_directory); | |
922 | } | |
923 | ||
1f1e895f | 924 | if (rev.pending.nr == 1) { |
8a1d076e JH |
925 | if (rev.max_count < 0 && !rev.show_root_diff) { |
926 | /* | |
927 | * This is traditional behaviour of "git format-patch | |
928 | * origin" that prepares what the origin side still | |
929 | * does not have. | |
930 | */ | |
7c496280 | 931 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
3384a2df | 932 | add_head_to_pending(&rev); |
7c496280 | 933 | } |
8a1d076e JH |
934 | /* |
935 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
936 | * "format-patch --root HEAD". The user wants | |
937 | * get_revision() to do the usual traversal. | |
7c496280 | 938 | */ |
e686eb98 | 939 | } |
a5a27c79 DB |
940 | if (cover_letter) { |
941 | /* remember the range */ | |
a5a27c79 DB |
942 | int i; |
943 | for (i = 0; i < rev.pending.nr; i++) { | |
944 | struct object *o = rev.pending.objects[i].item; | |
2bda2cf4 | 945 | if (!(o->flags & UNINTERESTING)) |
a5a27c79 DB |
946 | head = (struct commit *)o; |
947 | } | |
a5a27c79 DB |
948 | /* We can't generate a cover letter without any patches */ |
949 | if (!head) | |
950 | return 0; | |
951 | } | |
e686eb98 | 952 | |
9c6efa36 | 953 | if (ignore_if_in_upstream) |
5d23e133 | 954 | get_patch_ids(&rev, &ids, prefix); |
9c6efa36 | 955 | |
81f3a188 | 956 | if (!use_stdout) |
f5788250 | 957 | realstdout = xfdopen(xdup(1), "w"); |
81f3a188 | 958 | |
3d51e1b5 MK |
959 | if (prepare_revision_walk(&rev)) |
960 | die("revision walk setup failed"); | |
2bda2cf4 | 961 | rev.boundary = 1; |
91efcf60 | 962 | while ((commit = get_revision(&rev)) != NULL) { |
2bda2cf4 | 963 | if (commit->object.flags & BOUNDARY) { |
2bda2cf4 DB |
964 | boundary_count++; |
965 | origin = (boundary_count == 1) ? commit : NULL; | |
966 | continue; | |
967 | } | |
968 | ||
0377db77 JS |
969 | /* ignore merges */ |
970 | if (commit->parents && commit->parents->next) | |
971 | continue; | |
9c6efa36 JS |
972 | |
973 | if (ignore_if_in_upstream && | |
5d23e133 | 974 | has_commit_patch_id(commit, &ids)) |
9c6efa36 JS |
975 | continue; |
976 | ||
91efcf60 | 977 | nr++; |
83572c1a | 978 | list = xrealloc(list, nr * sizeof(list[0])); |
91efcf60 JH |
979 | list[nr - 1] = commit; |
980 | } | |
0377db77 | 981 | total = nr; |
49604a4d BG |
982 | if (!keep_subject && auto_number && total > 1) |
983 | numbered = 1; | |
596524b3 | 984 | if (numbered) |
fa0f02df | 985 | rev.total = total + start_number - 1; |
8419d2ee JH |
986 | if (in_reply_to) |
987 | rev.ref_message_id = clean_message_id(in_reply_to); | |
a5a27c79 DB |
988 | if (cover_letter) { |
989 | if (thread) | |
990 | gen_message_id(&rev, "cover"); | |
991 | make_cover_letter(&rev, use_stdout, numbered, numbered_files, | |
2bda2cf4 | 992 | origin, nr, list, head); |
a5a27c79 DB |
993 | total++; |
994 | start_number--; | |
995 | } | |
996 | rev.add_signoff = add_signoff; | |
91efcf60 JH |
997 | while (0 <= --nr) { |
998 | int shown; | |
999 | commit = list[nr]; | |
add5c8a5 | 1000 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 1001 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 | 1002 | if (thread) { |
a5a27c79 | 1003 | /* Have we already had a message ID? */ |
e1a37346 | 1004 | if (rev.message_id) { |
a5a27c79 DB |
1005 | /* |
1006 | * If we've got the ID to be a reply | |
1007 | * to, discard the current ID; | |
1008 | * otherwise, make everything a reply | |
1009 | * to that. | |
1010 | */ | |
e1a37346 DB |
1011 | if (rev.ref_message_id) |
1012 | free(rev.message_id); | |
1013 | else | |
1014 | rev.ref_message_id = rev.message_id; | |
cc35de84 | 1015 | } |
e1a37346 | 1016 | gen_message_id(&rev, sha1_to_hex(commit->object.sha1)); |
d1566f78 | 1017 | } |
a5a27c79 DB |
1018 | if (!use_stdout && reopen_stdout(numbered_files ? NULL : |
1019 | get_oneline_for_filename(commit, keep_subject), | |
1020 | rev.nr, rev.total)) | |
1021 | die("Failed to create output files"); | |
91efcf60 JH |
1022 | shown = log_tree_commit(&rev, commit); |
1023 | free(commit->buffer); | |
1024 | commit->buffer = NULL; | |
add5c8a5 JH |
1025 | |
1026 | /* We put one extra blank line between formatted | |
1027 | * patches and this flag is used by log-tree code | |
1028 | * to see if it needs to emit a LF before showing | |
1029 | * the log; when using one file per patch, we do | |
1030 | * not want the extra blank line. | |
1031 | */ | |
1032 | if (!use_stdout) | |
1033 | rev.shown_one = 0; | |
698ce6f8 JS |
1034 | if (shown) { |
1035 | if (rev.mime_boundary) | |
1036 | printf("\n--%s%s--\n\n\n", | |
1037 | mime_boundary_leader, | |
1038 | rev.mime_boundary); | |
1039 | else | |
1040 | printf("-- \n%s\n\n", git_version_string); | |
1041 | } | |
0377db77 JS |
1042 | if (!use_stdout) |
1043 | fclose(stdout); | |
91efcf60 JH |
1044 | } |
1045 | free(list); | |
5d23e133 JH |
1046 | if (ignore_if_in_upstream) |
1047 | free_patch_ids(&ids); | |
91efcf60 JH |
1048 | return 0; |
1049 | } | |
1050 | ||
e827633a RS |
1051 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
1052 | { | |
1053 | unsigned char sha1[20]; | |
1054 | if (get_sha1(arg, sha1) == 0) { | |
1055 | struct commit *commit = lookup_commit_reference(sha1); | |
1056 | if (commit) { | |
1057 | commit->object.flags |= flags; | |
1058 | add_pending_object(revs, &commit->object, arg); | |
1059 | return 0; | |
1060 | } | |
1061 | } | |
1062 | return -1; | |
1063 | } | |
1064 | ||
1065 | static const char cherry_usage[] = | |
1066 | "git-cherry [-v] <upstream> [<head>] [<limit>]"; | |
1067 | int cmd_cherry(int argc, const char **argv, const char *prefix) | |
1068 | { | |
1069 | struct rev_info revs; | |
5d23e133 | 1070 | struct patch_ids ids; |
e827633a RS |
1071 | struct commit *commit; |
1072 | struct commit_list *list = NULL; | |
1073 | const char *upstream; | |
1074 | const char *head = "HEAD"; | |
1075 | const char *limit = NULL; | |
1076 | int verbose = 0; | |
1077 | ||
1078 | if (argc > 1 && !strcmp(argv[1], "-v")) { | |
1079 | verbose = 1; | |
1080 | argc--; | |
1081 | argv++; | |
1082 | } | |
1083 | ||
1084 | switch (argc) { | |
1085 | case 4: | |
1086 | limit = argv[3]; | |
1087 | /* FALLTHROUGH */ | |
1088 | case 3: | |
1089 | head = argv[2]; | |
1090 | /* FALLTHROUGH */ | |
1091 | case 2: | |
1092 | upstream = argv[1]; | |
1093 | break; | |
1094 | default: | |
1095 | usage(cherry_usage); | |
1096 | } | |
1097 | ||
1098 | init_revisions(&revs, prefix); | |
1099 | revs.diff = 1; | |
1100 | revs.combine_merges = 0; | |
1101 | revs.ignore_merges = 1; | |
8f67f8ae | 1102 | DIFF_OPT_SET(&revs.diffopt, RECURSIVE); |
e827633a RS |
1103 | |
1104 | if (add_pending_commit(head, &revs, 0)) | |
1105 | die("Unknown commit %s", head); | |
1106 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) | |
1107 | die("Unknown commit %s", upstream); | |
1108 | ||
1109 | /* Don't say anything if head and upstream are the same. */ | |
1110 | if (revs.pending.nr == 2) { | |
1111 | struct object_array_entry *o = revs.pending.objects; | |
1112 | if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0) | |
1113 | return 0; | |
1114 | } | |
1115 | ||
5d23e133 | 1116 | get_patch_ids(&revs, &ids, prefix); |
e827633a RS |
1117 | |
1118 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
1119 | die("Unknown commit %s", limit); | |
1120 | ||
1121 | /* reverse the list of commits */ | |
3d51e1b5 MK |
1122 | if (prepare_revision_walk(&revs)) |
1123 | die("revision walk setup failed"); | |
e827633a RS |
1124 | while ((commit = get_revision(&revs)) != NULL) { |
1125 | /* ignore merges */ | |
1126 | if (commit->parents && commit->parents->next) | |
1127 | continue; | |
1128 | ||
1129 | commit_list_insert(commit, &list); | |
1130 | } | |
1131 | ||
1132 | while (list) { | |
e827633a RS |
1133 | char sign = '+'; |
1134 | ||
1135 | commit = list->item; | |
5d23e133 | 1136 | if (has_commit_patch_id(commit, &ids)) |
e827633a RS |
1137 | sign = '-'; |
1138 | ||
1139 | if (verbose) { | |
674d1727 PH |
1140 | struct strbuf buf; |
1141 | strbuf_init(&buf, 0); | |
1142 | pretty_print_commit(CMIT_FMT_ONELINE, commit, | |
4593fb84 | 1143 | &buf, 0, NULL, NULL, 0, 0); |
e827633a | 1144 | printf("%c %s %s\n", sign, |
674d1727 PH |
1145 | sha1_to_hex(commit->object.sha1), buf.buf); |
1146 | strbuf_release(&buf); | |
e827633a RS |
1147 | } |
1148 | else { | |
1149 | printf("%c %s\n", sign, | |
1150 | sha1_to_hex(commit->object.sha1)); | |
1151 | } | |
1152 | ||
1153 | list = list->next; | |
1154 | } | |
1155 | ||
5d23e133 | 1156 | free_patch_ids(&ids); |
e827633a RS |
1157 | return 0; |
1158 | } |