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