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