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