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