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