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