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