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