]>
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" | |
b2141fc1 | 8 | #include "config.h" |
fb58c8d5 | 9 | #include "refs.h" |
cbd53a21 | 10 | #include "object-store.h" |
6b2f2d98 | 11 | #include "color.h" |
70827b15 LT |
12 | #include "commit.h" |
13 | #include "diff.h" | |
14 | #include "revision.h" | |
15 | #include "log-tree.h" | |
91efcf60 | 16 | #include "builtin.h" |
5d7eeee2 | 17 | #include "tag.h" |
cf39f54e | 18 | #include "reflog-walk.h" |
5d23e133 | 19 | #include "patch-ids.h" |
a5a27c79 | 20 | #include "run-command.h" |
2bda2cf4 | 21 | #include "shortlog.h" |
f2968022 | 22 | #include "remote.h" |
b079c50e | 23 | #include "string-list.h" |
fff02ee6 | 24 | #include "parse-options.h" |
12da1d1f | 25 | #include "line-log.h" |
739453a3 | 26 | #include "branch.h" |
74775a09 | 27 | #include "streaming.h" |
816fb46b | 28 | #include "version.h" |
ea57bc0d | 29 | #include "mailmap.h" |
6005dbb9 | 30 | #include "gpg-interface.h" |
738e88a2 | 31 | #include "progress.h" |
7b680d32 | 32 | #include "commit-slab.h" |
109cd76d | 33 | #include "repository.h" |
64043556 | 34 | #include "commit-reach.h" |
126facf8 | 35 | #include "interdiff.h" |
31e2617a | 36 | #include "range-diff.h" |
70827b15 | 37 | |
43662b23 NTND |
38 | #define MAIL_DEFAULT_WRAP 72 |
39 | ||
dd0ffd5b HO |
40 | /* Set a default date-time format for git log ("log.date" config variable) */ |
41 | static const char *default_date_mode = NULL; | |
42 | ||
0c47695a | 43 | static int default_abbrev_commit; |
0f03ca94 | 44 | static int default_show_root = 1; |
076c9837 | 45 | static int default_follow; |
fce04c3c | 46 | static int default_show_signature; |
8a3d203b | 47 | static int decoration_style; |
1c40c36b | 48 | static int decoration_given; |
e6bb5f78 | 49 | static int use_mailmap_config; |
d54276f2 | 50 | static const char *fmt_patch_subject_prefix = "PATCH"; |
94c22a5e | 51 | static const char *fmt_pretty; |
0f03ca94 | 52 | |
1c40c36b | 53 | static const char * const builtin_log_usage[] = { |
ce41720c | 54 | N_("git log [<options>] [<revision-range>] [[--] <path>...]"), |
bb831db6 | 55 | N_("git show [<options>] <object>..."), |
1c40c36b CMN |
56 | NULL |
57 | }; | |
1c370ea4 | 58 | |
12da1d1f TR |
59 | struct line_opt_callback_data { |
60 | struct rev_info *rev; | |
61 | const char *prefix; | |
62 | struct string_list args; | |
63 | }; | |
64 | ||
940a911f AH |
65 | static int auto_decoration_style(void) |
66 | { | |
67 | return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0; | |
68 | } | |
69 | ||
f094b89a | 70 | static int parse_decoration_style(const char *value) |
8a3d203b | 71 | { |
89576613 | 72 | switch (git_parse_maybe_bool(value)) { |
8a3d203b JH |
73 | case 1: |
74 | return DECORATE_SHORT_REFS; | |
75 | case 0: | |
76 | return 0; | |
77 | default: | |
78 | break; | |
79 | } | |
80 | if (!strcmp(value, "full")) | |
81 | return DECORATE_FULL_REFS; | |
82 | else if (!strcmp(value, "short")) | |
83 | return DECORATE_SHORT_REFS; | |
15715866 | 84 | else if (!strcmp(value, "auto")) |
940a911f | 85 | return auto_decoration_style(); |
8a3d203b JH |
86 | return -1; |
87 | } | |
88 | ||
1c40c36b CMN |
89 | static int decorate_callback(const struct option *opt, const char *arg, int unset) |
90 | { | |
91 | if (unset) | |
92 | decoration_style = 0; | |
93 | else if (arg) | |
f094b89a | 94 | decoration_style = parse_decoration_style(arg); |
1c40c36b CMN |
95 | else |
96 | decoration_style = DECORATE_SHORT_REFS; | |
97 | ||
98 | if (decoration_style < 0) | |
e4a590ef | 99 | die(_("invalid --decorate option: %s"), arg); |
1c40c36b CMN |
100 | |
101 | decoration_given = 1; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
12da1d1f TR |
106 | static int log_line_range_callback(const struct option *option, const char *arg, int unset) |
107 | { | |
108 | struct line_opt_callback_data *data = option->value; | |
109 | ||
110 | if (!arg) | |
111 | return -1; | |
112 | ||
113 | data->rev->line_level_traverse = 1; | |
114 | string_list_append(&data->args, arg); | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
9501d191 MM |
119 | static void init_log_defaults(void) |
120 | { | |
38bbc2ea | 121 | init_grep_defaults(the_repository); |
5404c116 | 122 | init_diff_ui_defaults(); |
c74271aa | 123 | |
124 | decoration_style = auto_decoration_style(); | |
9501d191 MM |
125 | } |
126 | ||
ef803fd4 | 127 | static void cmd_log_init_defaults(struct rev_info *rev) |
70827b15 | 128 | { |
94c22a5e | 129 | if (fmt_pretty) |
4da45bef | 130 | get_commit_format(fmt_pretty, rev); |
076c9837 | 131 | if (default_follow) |
0d1e0e78 | 132 | rev->diffopt.flags.default_follow_renames = 1; |
70827b15 | 133 | rev->verbose_header = 1; |
0d1e0e78 | 134 | rev->diffopt.flags.recursive = 1; |
5e0ec15e | 135 | rev->diffopt.stat_width = -1; /* use full terminal width */ |
df44483a | 136 | rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */ |
0c47695a | 137 | rev->abbrev_commit = default_abbrev_commit; |
0f03ca94 | 138 | rev->show_root_diff = default_show_root; |
d54276f2 | 139 | rev->subject_prefix = fmt_patch_subject_prefix; |
fce04c3c | 140 | rev->show_signature = default_show_signature; |
0d1e0e78 | 141 | rev->diffopt.flags.allow_textconv = 1; |
dd0ffd5b HO |
142 | |
143 | if (default_date_mode) | |
a5481a6c | 144 | parse_date_format(default_date_mode, &rev->date_mode); |
ef803fd4 | 145 | } |
dd0ffd5b | 146 | |
ef803fd4 MG |
147 | static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, |
148 | struct rev_info *rev, struct setup_revision_opt *opt) | |
149 | { | |
ef803fd4 | 150 | struct userformat_want w; |
ea57bc0d | 151 | int quiet = 0, source = 0, mailmap = 0; |
12da1d1f | 152 | static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP}; |
65516f58 RA |
153 | static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP; |
154 | static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP; | |
155 | struct decoration_filter decoration_filter = {&decorate_refs_include, | |
156 | &decorate_refs_exclude}; | |
87be2523 | 157 | static struct revision_sources revision_sources; |
1c40c36b CMN |
158 | |
159 | const struct option builtin_log_options[] = { | |
b7df098c | 160 | OPT__QUIET(&quiet, N_("suppress diff output")), |
aa089cd9 FC |
161 | OPT_BOOL(0, "source", &source, N_("show source")), |
162 | OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")), | |
65516f58 RA |
163 | OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include, |
164 | N_("pattern"), N_("only decorate refs that match <pattern>")), | |
165 | OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude, | |
166 | N_("pattern"), N_("do not decorate refs that match <pattern>")), | |
e62cd35a | 167 | { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"), |
1c40c36b | 168 | PARSE_OPT_OPTARG, decorate_callback}, |
12da1d1f | 169 | OPT_CALLBACK('L', NULL, &line_cb, "n,m:file", |
e4a590ef | 170 | N_("Process line range n,m in file, counting from 1"), |
12da1d1f | 171 | log_line_range_callback), |
1c40c36b CMN |
172 | OPT_END() |
173 | }; | |
174 | ||
12da1d1f TR |
175 | line_cb.rev = rev; |
176 | line_cb.prefix = prefix; | |
177 | ||
e6bb5f78 | 178 | mailmap = use_mailmap_config; |
1c40c36b CMN |
179 | argc = parse_options(argc, argv, prefix, |
180 | builtin_log_options, builtin_log_usage, | |
181 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | | |
182 | PARSE_OPT_KEEP_DASHDASH); | |
52883fbd | 183 | |
01771a8e JH |
184 | if (quiet) |
185 | rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT; | |
f9c75d85 | 186 | argc = setup_revisions(argc, argv, rev, opt); |
dd0ffd5b | 187 | |
1c40c36b CMN |
188 | /* Any arguments at this point are not recognized */ |
189 | if (argc > 1) | |
e4a590ef | 190 | die(_("unrecognized argument: %s"), argv[1]); |
1c40c36b | 191 | |
5b163603 JG |
192 | memset(&w, 0, sizeof(w)); |
193 | userformat_find_requirements(NULL, &w); | |
194 | ||
195 | if (!rev->show_notes_given && (!rev->pretty_given || w.notes)) | |
66b2ed09 | 196 | rev->show_notes = 1; |
894a9d33 TR |
197 | if (rev->show_notes) |
198 | init_display_notes(&rev->notes_opt); | |
66b2ed09 | 199 | |
cf63051a SB |
200 | if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) || |
201 | rev->diffopt.filter || rev->diffopt.flags.follow_renames) | |
9dafea26 | 202 | rev->always_show_header = 0; |
1c40c36b | 203 | |
87be2523 NTND |
204 | if (source) { |
205 | init_revision_sources(&revision_sources); | |
206 | rev->sources = &revision_sources; | |
207 | } | |
635530a2 | 208 | |
ea57bc0d AP |
209 | if (mailmap) { |
210 | rev->mailmap = xcalloc(1, sizeof(struct string_list)); | |
211 | read_mailmap(rev->mailmap, NULL); | |
212 | } | |
213 | ||
0c47695a JS |
214 | if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) { |
215 | /* | |
216 | * "log --pretty=raw" is special; ignore UI oriented | |
217 | * configuration variables such as decoration. | |
218 | */ | |
219 | if (!decoration_given) | |
220 | decoration_style = 0; | |
221 | if (!rev->abbrev_commit_given) | |
222 | rev->abbrev_commit = 0; | |
223 | } | |
635530a2 | 224 | |
33e7018c LH |
225 | if (decoration_style) { |
226 | rev->show_decorations = 1; | |
65516f58 | 227 | load_ref_decorations(&decoration_filter, decoration_style); |
33e7018c | 228 | } |
12da1d1f TR |
229 | |
230 | if (rev->line_level_traverse) | |
231 | line_log_init(rev, line_cb.prefix, &line_cb.args); | |
232 | ||
1fda91b5 | 233 | setup_pager(); |
9dafea26 TH |
234 | } |
235 | ||
ef803fd4 MG |
236 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
237 | struct rev_info *rev, struct setup_revision_opt *opt) | |
238 | { | |
239 | cmd_log_init_defaults(rev); | |
240 | cmd_log_init_finish(argc, argv, prefix, rev, opt); | |
241 | } | |
242 | ||
252a7c02 LT |
243 | /* |
244 | * This gives a rough estimate for how many commits we | |
245 | * will print out in the list. | |
246 | */ | |
247 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) | |
248 | { | |
249 | int n = 0; | |
250 | ||
251 | while (list) { | |
252 | struct commit *commit = list->item; | |
253 | unsigned int flags = commit->object.flags; | |
252a7c02 | 254 | list = list->next; |
7dc0fe3b | 255 | if (!(flags & (TREESAME | UNINTERESTING))) |
53b2c823 | 256 | n++; |
252a7c02 LT |
257 | } |
258 | return n; | |
259 | } | |
260 | ||
261 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
262 | { | |
263 | if (rev->shown_one) { | |
264 | rev->shown_one = 0; | |
265 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
266 | putchar(rev->diffopt.line_termination); | |
267 | } | |
95235f5b | 268 | fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage); |
252a7c02 LT |
269 | } |
270 | ||
2af202be | 271 | static struct itimerval early_output_timer; |
252a7c02 | 272 | |
cdcefbc9 LT |
273 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
274 | { | |
6ea57703 | 275 | int i = revs->early_output, close_file = revs->diffopt.close_file; |
252a7c02 | 276 | int show_header = 1; |
cdcefbc9 | 277 | |
6ea57703 | 278 | revs->diffopt.close_file = 0; |
08f704f2 | 279 | sort_in_topological_order(&list, revs->sort_order); |
cdcefbc9 LT |
280 | while (list && i) { |
281 | struct commit *commit = list->item; | |
252a7c02 LT |
282 | switch (simplify_commit(revs, commit)) { |
283 | case commit_show: | |
284 | if (show_header) { | |
285 | int n = estimate_commit_count(revs, list); | |
286 | show_early_header(revs, "incomplete", n); | |
287 | show_header = 0; | |
288 | } | |
289 | log_tree_commit(revs, commit); | |
290 | i--; | |
291 | break; | |
292 | case commit_ignore: | |
293 | break; | |
294 | case commit_error: | |
6ea57703 JS |
295 | if (close_file) |
296 | fclose(revs->diffopt.file); | |
252a7c02 LT |
297 | return; |
298 | } | |
cdcefbc9 | 299 | list = list->next; |
cdcefbc9 | 300 | } |
252a7c02 LT |
301 | |
302 | /* Did we already get enough commits for the early output? */ | |
6ea57703 JS |
303 | if (!i) { |
304 | if (close_file) | |
305 | fclose(revs->diffopt.file); | |
252a7c02 | 306 | return; |
6ea57703 | 307 | } |
252a7c02 LT |
308 | |
309 | /* | |
310 | * ..if no, then repeat it twice a second until we | |
311 | * do. | |
312 | * | |
313 | * NOTE! We don't use "it_interval", because if the | |
314 | * reader isn't listening, we want our output to be | |
315 | * throttled by the writing, and not have the timer | |
316 | * trigger every second even if we're blocked on a | |
317 | * reader! | |
318 | */ | |
319 | early_output_timer.it_value.tv_sec = 0; | |
320 | early_output_timer.it_value.tv_usec = 500000; | |
321 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
322 | } |
323 | ||
324 | static void early_output(int signal) | |
325 | { | |
326 | show_early_output = log_show_early; | |
327 | } | |
328 | ||
329 | static void setup_early_output(struct rev_info *rev) | |
330 | { | |
331 | struct sigaction sa; | |
cdcefbc9 LT |
332 | |
333 | /* | |
334 | * Set up the signal handler, minimally intrusively: | |
335 | * we only set a single volatile integer word (not | |
336 | * using sigatomic_t - trying to avoid unnecessary | |
337 | * system dependencies and headers), and using | |
338 | * SA_RESTART. | |
339 | */ | |
340 | memset(&sa, 0, sizeof(sa)); | |
341 | sa.sa_handler = early_output; | |
342 | sigemptyset(&sa.sa_mask); | |
343 | sa.sa_flags = SA_RESTART; | |
344 | sigaction(SIGALRM, &sa, NULL); | |
345 | ||
346 | /* | |
347 | * If we can get the whole output in less than a | |
348 | * tenth of a second, don't even bother doing the | |
349 | * early-output thing.. | |
350 | * | |
351 | * This is a one-time-only trigger. | |
352 | */ | |
252a7c02 LT |
353 | early_output_timer.it_value.tv_sec = 0; |
354 | early_output_timer.it_value.tv_usec = 100000; | |
355 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
356 | } |
357 | ||
358 | static void finish_early_output(struct rev_info *rev) | |
359 | { | |
252a7c02 | 360 | int n = estimate_commit_count(rev, rev->commits); |
cdcefbc9 | 361 | signal(SIGALRM, SIG_IGN); |
252a7c02 | 362 | show_early_header(rev, "done", n); |
cdcefbc9 LT |
363 | } |
364 | ||
9dafea26 TH |
365 | static int cmd_log_walk(struct rev_info *rev) |
366 | { | |
367 | struct commit *commit; | |
f31027c9 | 368 | int saved_nrl = 0; |
6ea57703 | 369 | int saved_dcctc = 0, close_file = rev->diffopt.close_file; |
70827b15 | 370 | |
cdcefbc9 LT |
371 | if (rev->early_output) |
372 | setup_early_output(rev); | |
373 | ||
3d51e1b5 | 374 | if (prepare_revision_walk(rev)) |
5c5f1d7c | 375 | die(_("revision walk setup failed")); |
cdcefbc9 LT |
376 | |
377 | if (rev->early_output) | |
378 | finish_early_output(rev); | |
379 | ||
036d17fe | 380 | /* |
84102a33 PVM |
381 | * For --check and --exit-code, the exit code is based on CHECK_FAILED |
382 | * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to | |
383 | * retain that state information if replacing rev->diffopt in this loop | |
036d17fe | 384 | */ |
6ea57703 | 385 | rev->diffopt.close_file = 0; |
70827b15 | 386 | while ((commit = get_revision(rev)) != NULL) { |
3acf8dd8 | 387 | if (!log_tree_commit(rev, commit) && rev->max_count >= 0) |
251df09b MM |
388 | /* |
389 | * We decremented max_count in get_revision, | |
390 | * but we didn't actually show the commit. | |
391 | */ | |
392 | rev->max_count++; | |
a6c73064 | 393 | if (!rev->reflog_info) { |
822601e8 JK |
394 | /* |
395 | * We may show a given commit multiple times when | |
396 | * walking the reflogs. | |
397 | */ | |
0fb370da | 398 | free_commit_buffer(commit); |
f35650df JK |
399 | free_commit_list(commit->parents); |
400 | commit->parents = NULL; | |
a6c73064 | 401 | } |
f31027c9 JH |
402 | if (saved_nrl < rev->diffopt.needed_rename_limit) |
403 | saved_nrl = rev->diffopt.needed_rename_limit; | |
404 | if (rev->diffopt.degraded_cc_to_c) | |
405 | saved_dcctc = 1; | |
70827b15 | 406 | } |
f31027c9 JH |
407 | rev->diffopt.degraded_cc_to_c = saved_dcctc; |
408 | rev->diffopt.needed_rename_limit = saved_nrl; | |
6ea57703 JS |
409 | if (close_file) |
410 | fclose(rev->diffopt.file); | |
f31027c9 | 411 | |
036d17fe | 412 | if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && |
0d1e0e78 | 413 | rev->diffopt.flags.check_failed) { |
036d17fe PVM |
414 | return 02; |
415 | } | |
84102a33 | 416 | return diff_result_code(&rev->diffopt, 0); |
70827b15 LT |
417 | } |
418 | ||
ef90d6d4 | 419 | static int git_log_config(const char *var, const char *value, void *cb) |
0f03ca94 | 420 | { |
e3f1da98 RS |
421 | const char *slot_name; |
422 | ||
94c22a5e CR |
423 | if (!strcmp(var, "format.pretty")) |
424 | return git_config_string(&fmt_pretty, var, value); | |
70cff3ac BH |
425 | if (!strcmp(var, "format.subjectprefix")) |
426 | return git_config_string(&fmt_patch_subject_prefix, var, value); | |
0c47695a JS |
427 | if (!strcmp(var, "log.abbrevcommit")) { |
428 | default_abbrev_commit = git_config_bool(var, value); | |
429 | return 0; | |
430 | } | |
dd0ffd5b HO |
431 | if (!strcmp(var, "log.date")) |
432 | return git_config_string(&default_date_mode, var, value); | |
eb734454 | 433 | if (!strcmp(var, "log.decorate")) { |
f094b89a | 434 | decoration_style = parse_decoration_style(value); |
8a3d203b JH |
435 | if (decoration_style < 0) |
436 | decoration_style = 0; /* maybe warn? */ | |
eb734454 SD |
437 | return 0; |
438 | } | |
0f03ca94 PB |
439 | if (!strcmp(var, "log.showroot")) { |
440 | default_show_root = git_config_bool(var, value); | |
441 | return 0; | |
442 | } | |
076c9837 DT |
443 | if (!strcmp(var, "log.follow")) { |
444 | default_follow = git_config_bool(var, value); | |
445 | return 0; | |
446 | } | |
e3f1da98 | 447 | if (skip_prefix(var, "color.decorate.", &slot_name)) |
b9465768 | 448 | return parse_decorate_color_config(var, slot_name, value); |
e6bb5f78 AP |
449 | if (!strcmp(var, "log.mailmap")) { |
450 | use_mailmap_config = git_config_bool(var, value); | |
451 | return 0; | |
452 | } | |
fce04c3c MJ |
453 | if (!strcmp(var, "log.showsignature")) { |
454 | default_show_signature = git_config_bool(var, value); | |
455 | return 0; | |
456 | } | |
e6bb5f78 | 457 | |
0657bcbf JH |
458 | if (grep_config(var, value, cb) < 0) |
459 | return -1; | |
6005dbb9 | 460 | if (git_gpg_config(var, value, cb) < 0) |
0657bcbf | 461 | return -1; |
ef90d6d4 | 462 | return git_diff_ui_config(var, value, cb); |
0f03ca94 PB |
463 | } |
464 | ||
a633fca0 | 465 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
466 | { |
467 | struct rev_info rev; | |
32962c9b | 468 | struct setup_revision_opt opt; |
70827b15 | 469 | |
9501d191 | 470 | init_log_defaults(); |
ef90d6d4 | 471 | git_config(git_log_config, NULL); |
6b2f2d98 | 472 | |
2abf3503 | 473 | repo_init_revisions(the_repository, &rev, prefix); |
70827b15 | 474 | rev.diff = 1; |
9202434c | 475 | rev.simplify_history = 0; |
32962c9b JH |
476 | memset(&opt, 0, sizeof(opt)); |
477 | opt.def = "HEAD"; | |
d5f6b1d7 | 478 | opt.revarg_opt = REVARG_COMMITTISH; |
32962c9b | 479 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
9dafea26 TH |
480 | if (!rev.diffopt.output_format) |
481 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
482 | return cmd_log_walk(&rev); | |
70827b15 LT |
483 | } |
484 | ||
56122ed8 JS |
485 | static void show_tagger(char *buf, int len, struct rev_info *rev) |
486 | { | |
ea718e65 | 487 | struct strbuf out = STRBUF_INIT; |
6bf13944 | 488 | struct pretty_print_context pp = {0}; |
56122ed8 | 489 | |
6bf13944 JK |
490 | pp.fmt = rev->commit_format; |
491 | pp.date_mode = rev->date_mode; | |
492 | pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding()); | |
95235f5b | 493 | fprintf(rev->diffopt.file, "%s", out.buf); |
ea718e65 | 494 | strbuf_release(&out); |
56122ed8 JS |
495 | } |
496 | ||
d801627b | 497 | static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name) |
74775a09 | 498 | { |
d801627b | 499 | struct object_id oidc; |
083b9931 MG |
500 | struct object_context obj_context; |
501 | char *buf; | |
502 | unsigned long size; | |
503 | ||
95235f5b | 504 | fflush(rev->diffopt.file); |
0d1e0e78 BW |
505 | if (!rev->diffopt.flags.textconv_set_via_cmdline || |
506 | !rev->diffopt.flags.allow_textconv) | |
7eda0e4f | 507 | return stream_blob_to_fd(1, oid, NULL, 0); |
083b9931 | 508 | |
321c89bf | 509 | if (get_oid_with_context(obj_name, GET_OID_RECORD_PATH, |
e82caf38 | 510 | &oidc, &obj_context)) |
e4a590ef | 511 | die(_("Not a valid object name %s"), obj_name); |
dc944b65 | 512 | if (!obj_context.path || |
6afaf807 NTND |
513 | !textconv_object(the_repository, obj_context.path, |
514 | obj_context.mode, &oidc, 1, &buf, &size)) { | |
dc944b65 | 515 | free(obj_context.path); |
7eda0e4f | 516 | return stream_blob_to_fd(1, oid, NULL, 0); |
dc944b65 | 517 | } |
083b9931 MG |
518 | |
519 | if (!buf) | |
e4a590ef | 520 | die(_("git show %s: bad file"), obj_name); |
083b9931 MG |
521 | |
522 | write_or_die(1, buf, size); | |
dc944b65 | 523 | free(obj_context.path); |
083b9931 | 524 | return 0; |
74775a09 NTND |
525 | } |
526 | ||
d801627b | 527 | static int show_tag_object(const struct object_id *oid, struct rev_info *rev) |
5d7eeee2 JS |
528 | { |
529 | unsigned long size; | |
21666f1a | 530 | enum object_type type; |
b4f5aca4 | 531 | char *buf = read_object_file(oid, &type, &size); |
5d7eeee2 JS |
532 | int offset = 0; |
533 | ||
534 | if (!buf) | |
d801627b | 535 | return error(_("Could not read object %s"), oid_to_hex(oid)); |
5d7eeee2 | 536 | |
74775a09 NTND |
537 | assert(type == OBJ_TAG); |
538 | while (offset < size && buf[offset] != '\n') { | |
539 | int new_offset = offset + 1; | |
540 | while (new_offset < size && buf[new_offset++] != '\n') | |
541 | ; /* do nothing */ | |
59556548 | 542 | if (starts_with(buf + offset, "tagger ")) |
74775a09 NTND |
543 | show_tagger(buf + offset + 7, |
544 | new_offset - offset - 7, rev); | |
545 | offset = new_offset; | |
546 | } | |
5d7eeee2 JS |
547 | |
548 | if (offset < size) | |
95235f5b | 549 | fwrite(buf + offset, size - offset, 1, rev->diffopt.file); |
5d7eeee2 JS |
550 | free(buf); |
551 | return 0; | |
552 | } | |
553 | ||
df46d77e | 554 | static int show_tree_object(const struct object_id *oid, |
6a0b0b6d | 555 | struct strbuf *base, |
671f0707 | 556 | const char *pathname, unsigned mode, int stage, void *context) |
5d7eeee2 | 557 | { |
95235f5b JS |
558 | FILE *file = context; |
559 | fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
5d7eeee2 JS |
560 | return 0; |
561 | } | |
562 | ||
b130c706 JH |
563 | static void show_setup_revisions_tweak(struct rev_info *rev, |
564 | struct setup_revision_opt *opt) | |
b4490059 | 565 | { |
2bf65873 JH |
566 | if (rev->ignore_merges) { |
567 | /* There was no "-m" on the command line */ | |
568 | rev->ignore_merges = 0; | |
569 | if (!rev->first_parent_only && !rev->combine_merges) { | |
01689909 | 570 | /* No "--first-parent", "-c", or "--cc" */ |
2bf65873 JH |
571 | rev->combine_merges = 1; |
572 | rev->dense_combined_merges = 1; | |
573 | } | |
574 | } | |
b4490059 JH |
575 | if (!rev->diffopt.output_format) |
576 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
577 | } | |
578 | ||
a633fca0 | 579 | int cmd_show(int argc, const char **argv, const char *prefix) |
70827b15 LT |
580 | { |
581 | struct rev_info rev; | |
5d7eeee2 | 582 | struct object_array_entry *objects; |
32962c9b | 583 | struct setup_revision_opt opt; |
f0096c06 | 584 | struct pathspec match_all; |
5d7eeee2 | 585 | int i, count, ret = 0; |
70827b15 | 586 | |
9501d191 | 587 | init_log_defaults(); |
ef90d6d4 | 588 | git_config(git_log_config, NULL); |
6b2f2d98 | 589 | |
9a087274 | 590 | memset(&match_all, 0, sizeof(match_all)); |
2abf3503 | 591 | repo_init_revisions(the_repository, &rev, prefix); |
70827b15 | 592 | rev.diff = 1; |
70827b15 | 593 | rev.always_show_header = 1; |
ca92e59e | 594 | rev.no_walk = REVISION_WALK_NO_WALK_SORTED; |
666c92a2 ZJS |
595 | rev.diffopt.stat_width = -1; /* Scale to real terminal size */ |
596 | ||
32962c9b JH |
597 | memset(&opt, 0, sizeof(opt)); |
598 | opt.def = "HEAD"; | |
b130c706 | 599 | opt.tweak = show_setup_revisions_tweak; |
32962c9b | 600 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
5d7eeee2 | 601 | |
c5941f1a JH |
602 | if (!rev.no_walk) |
603 | return cmd_log_walk(&rev); | |
604 | ||
5d7eeee2 JS |
605 | count = rev.pending.nr; |
606 | objects = rev.pending.objects; | |
607 | for (i = 0; i < count && !ret; i++) { | |
608 | struct object *o = objects[i].item; | |
609 | const char *name = objects[i].name; | |
610 | switch (o->type) { | |
611 | case OBJ_BLOB: | |
d801627b | 612 | ret = show_blob_object(&o->oid, &rev, name); |
5d7eeee2 JS |
613 | break; |
614 | case OBJ_TAG: { | |
615 | struct tag *t = (struct tag *)o; | |
616 | ||
ae03ee64 JK |
617 | if (rev.shown_one) |
618 | putchar('\n'); | |
95235f5b | 619 | fprintf(rev.diffopt.file, "%stag %s%s\n", |
8f67f8ae | 620 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 621 | t->tag, |
8f67f8ae | 622 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
d801627b | 623 | ret = show_tag_object(&o->oid, &rev); |
ae03ee64 | 624 | rev.shown_one = 1; |
d2dadfe8 JH |
625 | if (ret) |
626 | break; | |
109cd76d | 627 | o = parse_object(the_repository, &t->tagged->oid); |
d2dadfe8 | 628 | if (!o) |
5c5f1d7c | 629 | ret = error(_("Could not read object %s"), |
f2fd0760 | 630 | oid_to_hex(&t->tagged->oid)); |
d2dadfe8 | 631 | objects[i].item = o; |
5d7eeee2 JS |
632 | i--; |
633 | break; | |
634 | } | |
635 | case OBJ_TREE: | |
ae03ee64 JK |
636 | if (rev.shown_one) |
637 | putchar('\n'); | |
95235f5b | 638 | fprintf(rev.diffopt.file, "%stree %s%s\n\n", |
8f67f8ae | 639 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 640 | name, |
8f67f8ae | 641 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
f0096c06 | 642 | read_tree_recursive((struct tree *)o, "", 0, 0, &match_all, |
95235f5b | 643 | show_tree_object, rev.diffopt.file); |
ae03ee64 | 644 | rev.shown_one = 1; |
5d7eeee2 JS |
645 | break; |
646 | case OBJ_COMMIT: | |
647 | rev.pending.nr = rev.pending.alloc = 0; | |
648 | rev.pending.objects = NULL; | |
649 | add_object_array(o, name, &rev.pending); | |
650 | ret = cmd_log_walk(&rev); | |
651 | break; | |
652 | default: | |
5c5f1d7c | 653 | ret = error(_("Unknown type: %d"), o->type); |
5d7eeee2 JS |
654 | } |
655 | } | |
656 | free(objects); | |
657 | return ret; | |
70827b15 LT |
658 | } |
659 | ||
cf39f54e LT |
660 | /* |
661 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
662 | */ | |
663 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) | |
664 | { | |
665 | struct rev_info rev; | |
32962c9b | 666 | struct setup_revision_opt opt; |
cf39f54e | 667 | |
9501d191 | 668 | init_log_defaults(); |
ef90d6d4 | 669 | git_config(git_log_config, NULL); |
6b2f2d98 | 670 | |
2abf3503 | 671 | repo_init_revisions(the_repository, &rev, prefix); |
cf39f54e | 672 | init_reflog_walk(&rev.reflog_info); |
cf39f54e | 673 | rev.verbose_header = 1; |
32962c9b JH |
674 | memset(&opt, 0, sizeof(opt)); |
675 | opt.def = "HEAD"; | |
4b56cf58 | 676 | cmd_log_init_defaults(&rev); |
0c47695a | 677 | rev.abbrev_commit = 1; |
cf39f54e | 678 | rev.commit_format = CMIT_FMT_ONELINE; |
4da45bef | 679 | rev.use_terminator = 1; |
cf39f54e | 680 | rev.always_show_header = 1; |
4b56cf58 | 681 | cmd_log_init_finish(argc, argv, prefix, &rev, &opt); |
cf39f54e | 682 | |
cf39f54e LT |
683 | return cmd_log_walk(&rev); |
684 | } | |
685 | ||
b130c706 JH |
686 | static void log_setup_revisions_tweak(struct rev_info *rev, |
687 | struct setup_revision_opt *opt) | |
076c9837 | 688 | { |
0d1e0e78 | 689 | if (rev->diffopt.flags.default_follow_renames && |
076c9837 | 690 | rev->prune_data.nr == 1) |
0d1e0e78 | 691 | rev->diffopt.flags.follow_renames = 1; |
c7eaf8b4 JH |
692 | |
693 | /* Turn --cc/-c into -p --cc/-c when -p was not given */ | |
694 | if (!rev->diffopt.output_format && rev->combine_merges) | |
695 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
82dee416 JH |
696 | |
697 | /* Turn -m on when --cc/-c was given */ | |
698 | if (rev->combine_merges) | |
699 | rev->ignore_merges = 0; | |
076c9837 DT |
700 | } |
701 | ||
a633fca0 | 702 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
703 | { |
704 | struct rev_info rev; | |
32962c9b | 705 | struct setup_revision_opt opt; |
70827b15 | 706 | |
9501d191 | 707 | init_log_defaults(); |
ef90d6d4 | 708 | git_config(git_log_config, NULL); |
6b2f2d98 | 709 | |
2abf3503 | 710 | repo_init_revisions(the_repository, &rev, prefix); |
70827b15 | 711 | rev.always_show_header = 1; |
32962c9b JH |
712 | memset(&opt, 0, sizeof(opt)); |
713 | opt.def = "HEAD"; | |
d5f6b1d7 | 714 | opt.revarg_opt = REVARG_COMMITTISH; |
b130c706 | 715 | opt.tweak = log_setup_revisions_tweak; |
32962c9b | 716 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
9dafea26 | 717 | return cmd_log_walk(&rev); |
70827b15 | 718 | } |
91efcf60 | 719 | |
c06d2daa | 720 | /* format-patch */ |
0377db77 | 721 | |
917a8f89 | 722 | static const char *fmt_patch_suffix = ".patch"; |
49604a4d | 723 | static int numbered = 0; |
a567fdcb | 724 | static int auto_number = 1; |
20ff0680 | 725 | |
0db5260b JW |
726 | static char *default_attach = NULL; |
727 | ||
2721ce21 JK |
728 | static struct string_list extra_hdr = STRING_LIST_INIT_NODUP; |
729 | static struct string_list extra_to = STRING_LIST_INIT_NODUP; | |
730 | static struct string_list extra_cc = STRING_LIST_INIT_NODUP; | |
3ee79d9f DB |
731 | |
732 | static void add_header(const char *value) | |
733 | { | |
ca9e0a1b | 734 | struct string_list_item *item; |
3ee79d9f | 735 | int len = strlen(value); |
c8c4450e | 736 | while (len && value[len - 1] == '\n') |
3ee79d9f | 737 | len--; |
ca9e0a1b | 738 | |
3ee79d9f | 739 | if (!strncasecmp(value, "to: ", 4)) { |
1d2f80fa | 740 | item = string_list_append(&extra_to, value + 4); |
ca9e0a1b SB |
741 | len -= 4; |
742 | } else if (!strncasecmp(value, "cc: ", 4)) { | |
1d2f80fa | 743 | item = string_list_append(&extra_cc, value + 4); |
ca9e0a1b SB |
744 | len -= 4; |
745 | } else { | |
1d2f80fa | 746 | item = string_list_append(&extra_hdr, value); |
3ee79d9f | 747 | } |
ca9e0a1b SB |
748 | |
749 | item->string[len] = '\0'; | |
3ee79d9f DB |
750 | } |
751 | ||
30984ed2 TR |
752 | #define THREAD_SHALLOW 1 |
753 | #define THREAD_DEEP 2 | |
6622d9c7 SB |
754 | static int thread; |
755 | static int do_signoff; | |
bb52995f | 756 | static int base_auto; |
6bc6b6c0 | 757 | static char *from; |
6622d9c7 | 758 | static const char *signature = git_version_string; |
7022650f | 759 | static const char *signature_file; |
2a4c2607 | 760 | static int config_cover_letter; |
bc6bf2d7 | 761 | static const char *config_output_directory; |
2a4c2607 FC |
762 | |
763 | enum { | |
764 | COVER_UNSET, | |
765 | COVER_OFF, | |
766 | COVER_ON, | |
767 | COVER_AUTO | |
768 | }; | |
30984ed2 | 769 | |
ef90d6d4 | 770 | static int git_format_config(const char *var, const char *value, void *cb) |
20ff0680 JS |
771 | { |
772 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 | 773 | if (!value) |
5c5f1d7c | 774 | die(_("format.headers without value")); |
3ee79d9f | 775 | add_header(value); |
20ff0680 JS |
776 | return 0; |
777 | } | |
70cff3ac BH |
778 | if (!strcmp(var, "format.suffix")) |
779 | return git_config_string(&fmt_patch_suffix, var, value); | |
ae6c098f SD |
780 | if (!strcmp(var, "format.to")) { |
781 | if (!value) | |
782 | return config_error_nonbool(var); | |
1d2f80fa | 783 | string_list_append(&extra_to, value); |
ae6c098f SD |
784 | return 0; |
785 | } | |
fe8928e6 MV |
786 | if (!strcmp(var, "format.cc")) { |
787 | if (!value) | |
788 | return config_error_nonbool(var); | |
1d2f80fa | 789 | string_list_append(&extra_cc, value); |
fe8928e6 MV |
790 | return 0; |
791 | } | |
787570c7 | 792 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") || |
339de508 | 793 | !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) { |
f3aafa4d RA |
794 | return 0; |
795 | } | |
49604a4d | 796 | if (!strcmp(var, "format.numbered")) { |
90f5c186 | 797 | if (value && !strcasecmp(value, "auto")) { |
49604a4d BG |
798 | auto_number = 1; |
799 | return 0; | |
800 | } | |
49604a4d | 801 | numbered = git_config_bool(var, value); |
a567fdcb | 802 | auto_number = auto_number && numbered; |
49604a4d BG |
803 | return 0; |
804 | } | |
0db5260b JW |
805 | if (!strcmp(var, "format.attach")) { |
806 | if (value && *value) | |
807 | default_attach = xstrdup(value); | |
808 | else | |
809 | default_attach = xstrdup(git_version_string); | |
810 | return 0; | |
811 | } | |
30984ed2 TR |
812 | if (!strcmp(var, "format.thread")) { |
813 | if (value && !strcasecmp(value, "deep")) { | |
814 | thread = THREAD_DEEP; | |
815 | return 0; | |
816 | } | |
817 | if (value && !strcasecmp(value, "shallow")) { | |
818 | thread = THREAD_SHALLOW; | |
819 | return 0; | |
820 | } | |
821 | thread = git_config_bool(var, value) && THREAD_SHALLOW; | |
822 | return 0; | |
823 | } | |
1d1876e9 HV |
824 | if (!strcmp(var, "format.signoff")) { |
825 | do_signoff = git_config_bool(var, value); | |
826 | return 0; | |
827 | } | |
6622d9c7 SB |
828 | if (!strcmp(var, "format.signature")) |
829 | return git_config_string(&signature, var, value); | |
7022650f JM |
830 | if (!strcmp(var, "format.signaturefile")) |
831 | return git_config_pathname(&signature_file, var, value); | |
2a4c2607 FC |
832 | if (!strcmp(var, "format.coverletter")) { |
833 | if (value && !strcasecmp(value, "auto")) { | |
834 | config_cover_letter = COVER_AUTO; | |
835 | return 0; | |
836 | } | |
837 | config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF; | |
838 | return 0; | |
839 | } | |
bc6bf2d7 AK |
840 | if (!strcmp(var, "format.outputdirectory")) |
841 | return git_config_string(&config_output_directory, var, value); | |
bb52995f XY |
842 | if (!strcmp(var, "format.useautobase")) { |
843 | base_auto = git_config_bool(var, value); | |
844 | return 0; | |
845 | } | |
6bc6b6c0 | 846 | if (!strcmp(var, "format.from")) { |
89576613 | 847 | int b = git_parse_maybe_bool(value); |
6bc6b6c0 JT |
848 | free(from); |
849 | if (b < 0) | |
850 | from = xstrdup(value); | |
851 | else if (b) | |
852 | from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
853 | else | |
854 | from = NULL; | |
855 | return 0; | |
856 | } | |
dbd21447 | 857 | |
ef90d6d4 | 858 | return git_log_config(var, value, cb); |
20ff0680 JS |
859 | } |
860 | ||
efd02016 | 861 | static const char *output_directory = NULL; |
9800a754 | 862 | static int outdir_offset; |
81f3a188 | 863 | |
95235f5b | 864 | static int open_next_file(struct commit *commit, const char *subject, |
a21c2f94 | 865 | struct rev_info *rev, int quiet) |
0377db77 | 866 | { |
cd2ef591 | 867 | struct strbuf filename = STRBUF_INIT; |
68cb7b6f | 868 | int suffix_len = strlen(rev->patch_suffix) + 1; |
0377db77 | 869 | |
2448482b | 870 | if (output_directory) { |
cd2ef591 SB |
871 | strbuf_addstr(&filename, output_directory); |
872 | if (filename.len >= | |
15d980a7 NTND |
873 | PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) { |
874 | strbuf_release(&filename); | |
5c5f1d7c | 875 | return error(_("name of output directory is too long")); |
15d980a7 | 876 | } |
00b6c178 | 877 | strbuf_complete(&filename, '/'); |
2448482b | 878 | } |
0377db77 | 879 | |
38ec23ac JH |
880 | if (rev->numbered_files) |
881 | strbuf_addf(&filename, "%d", rev->nr); | |
d28b5d47 JH |
882 | else if (commit) |
883 | fmt_output_commit(&filename, commit, rev); | |
38ec23ac | 884 | else |
d28b5d47 | 885 | fmt_output_subject(&filename, subject, rev); |
e6ff0f42 | 886 | |
250087f2 | 887 | if (!quiet) |
36a4d905 | 888 | printf("%s\n", filename.buf + outdir_offset); |
ec2956df | 889 | |
15d980a7 NTND |
890 | if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) { |
891 | error_errno(_("Cannot open patch file %s"), filename.buf); | |
892 | strbuf_release(&filename); | |
893 | return -1; | |
894 | } | |
c06d2daa | 895 | |
cd2ef591 | 896 | strbuf_release(&filename); |
e6ff0f42 | 897 | return 0; |
0377db77 JS |
898 | } |
899 | ||
811929f1 | 900 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids) |
9c6efa36 JS |
901 | { |
902 | struct rev_info check_rev; | |
9b7a61d7 | 903 | struct commit *commit, *c1, *c2; |
9c6efa36 JS |
904 | struct object *o1, *o2; |
905 | unsigned flags1, flags2; | |
9c6efa36 JS |
906 | |
907 | if (rev->pending.nr != 2) | |
5c5f1d7c | 908 | die(_("Need exactly one range.")); |
9c6efa36 JS |
909 | |
910 | o1 = rev->pending.objects[0].item; | |
9c6efa36 | 911 | o2 = rev->pending.objects[1].item; |
9b7a61d7 | 912 | flags1 = o1->flags; |
9c6efa36 | 913 | flags2 = o2->flags; |
2122f675 SB |
914 | c1 = lookup_commit_reference(the_repository, &o1->oid); |
915 | c2 = lookup_commit_reference(the_repository, &o2->oid); | |
9c6efa36 JS |
916 | |
917 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
5c5f1d7c | 918 | die(_("Not a range.")); |
9c6efa36 | 919 | |
a7edadda | 920 | init_patch_ids(the_repository, ids); |
9c6efa36 JS |
921 | |
922 | /* given a range a..b get all patch ids for b..a */ | |
2abf3503 | 923 | repo_init_revisions(the_repository, &check_rev, rev->prefix); |
51f4de3a | 924 | check_rev.max_parents = 1; |
9c6efa36 JS |
925 | o1->flags ^= UNINTERESTING; |
926 | o2->flags ^= UNINTERESTING; | |
927 | add_pending_object(&check_rev, o1, "o1"); | |
928 | add_pending_object(&check_rev, o2, "o2"); | |
3d51e1b5 | 929 | if (prepare_revision_walk(&check_rev)) |
5c5f1d7c | 930 | die(_("revision walk setup failed")); |
9c6efa36 JS |
931 | |
932 | while ((commit = get_revision(&check_rev)) != NULL) { | |
5d23e133 | 933 | add_commit_patch_id(commit, ids); |
9c6efa36 JS |
934 | } |
935 | ||
936 | /* reset for next revision walk */ | |
9b7a61d7 JH |
937 | clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED); |
938 | clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
939 | o1->flags = flags1; |
940 | o2->flags = flags2; | |
941 | } | |
942 | ||
e1a37346 | 943 | static void gen_message_id(struct rev_info *info, char *base) |
d1566f78 | 944 | { |
f285a2d7 | 945 | struct strbuf buf = STRBUF_INIT; |
cb71f8bd | 946 | strbuf_addf(&buf, "%s.%"PRItime".git.%s", base, |
dddbad72 | 947 | (timestamp_t) time(NULL), |
59f9b8a9 | 948 | git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT)); |
e1a37346 | 949 | info->message_id = strbuf_detach(&buf, NULL); |
d1566f78 JT |
950 | } |
951 | ||
95235f5b | 952 | static void print_signature(FILE *file) |
6622d9c7 | 953 | { |
c6076e2b JK |
954 | if (!signature || !*signature) |
955 | return; | |
956 | ||
95235f5b | 957 | fprintf(file, "-- \n%s", signature); |
c6076e2b | 958 | if (signature[strlen(signature)-1] != '\n') |
95235f5b JS |
959 | putc('\n', file); |
960 | putc('\n', file); | |
6622d9c7 SB |
961 | } |
962 | ||
739453a3 JH |
963 | static void add_branch_description(struct strbuf *buf, const char *branch_name) |
964 | { | |
965 | struct strbuf desc = STRBUF_INIT; | |
966 | if (!branch_name || !*branch_name) | |
967 | return; | |
968 | read_branch_desc(&desc, branch_name); | |
969 | if (desc.len) { | |
970 | strbuf_addch(buf, '\n'); | |
e992d1eb | 971 | strbuf_addbuf(buf, &desc); |
739453a3 JH |
972 | strbuf_addch(buf, '\n'); |
973 | } | |
94204bf5 | 974 | strbuf_release(&desc); |
739453a3 JH |
975 | } |
976 | ||
d7ddad01 FC |
977 | static char *find_branch_name(struct rev_info *rev) |
978 | { | |
979 | int i, positive = -1; | |
f2fd0760 | 980 | struct object_id branch_oid; |
981 | const struct object_id *tip_oid; | |
95b567c7 | 982 | const char *ref, *v; |
d7ddad01 FC |
983 | char *full_ref, *branch = NULL; |
984 | ||
985 | for (i = 0; i < rev->cmdline.nr; i++) { | |
986 | if (rev->cmdline.rev[i].flags & UNINTERESTING) | |
987 | continue; | |
988 | if (positive < 0) | |
989 | positive = i; | |
990 | else | |
991 | return NULL; | |
992 | } | |
993 | if (positive < 0) | |
994 | return NULL; | |
995 | ref = rev->cmdline.rev[positive].name; | |
f2fd0760 | 996 | tip_oid = &rev->cmdline.rev[positive].item->oid; |
cca5fa64 | 997 | if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) && |
95b567c7 | 998 | skip_prefix(full_ref, "refs/heads/", &v) && |
4a7e27e9 | 999 | oideq(tip_oid, &branch_oid)) |
95b567c7 | 1000 | branch = xstrdup(v); |
d7ddad01 FC |
1001 | free(full_ref); |
1002 | return branch; | |
1003 | } | |
1004 | ||
fa5b7ea6 ES |
1005 | static void show_diffstat(struct rev_info *rev, |
1006 | struct commit *origin, struct commit *head) | |
1007 | { | |
1008 | struct diff_options opts; | |
1009 | ||
1010 | memcpy(&opts, &rev->diffopt, sizeof(opts)); | |
1011 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
1012 | opts.stat_width = MAIL_DEFAULT_WRAP; | |
1013 | ||
1014 | diff_setup_done(&opts); | |
1015 | ||
1016 | diff_tree_oid(get_commit_tree_oid(origin), | |
1017 | get_commit_tree_oid(head), | |
1018 | "", &opts); | |
1019 | diffcore_std(&opts); | |
1020 | diff_flush(&opts); | |
1021 | ||
1022 | fprintf(rev->diffopt.file, "\n"); | |
1023 | } | |
1024 | ||
2bda2cf4 | 1025 | static void make_cover_letter(struct rev_info *rev, int use_stdout, |
2bda2cf4 | 1026 | struct commit *origin, |
d7ddad01 | 1027 | int nr, struct commit **list, |
739453a3 | 1028 | const char *branch_name, |
250087f2 | 1029 | int quiet) |
a5a27c79 DB |
1030 | { |
1031 | const char *committer; | |
a5a27c79 DB |
1032 | const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; |
1033 | const char *msg; | |
2bda2cf4 | 1034 | struct shortlog log; |
f285a2d7 | 1035 | struct strbuf sb = STRBUF_INIT; |
2bda2cf4 | 1036 | int i; |
330db18c | 1037 | const char *encoding = "UTF-8"; |
267123b4 | 1038 | int need_8bit_cte = 0; |
6bf13944 | 1039 | struct pretty_print_context pp = {0}; |
d7ddad01 | 1040 | struct commit *head = list[0]; |
a5a27c79 | 1041 | |
9f23e040 | 1042 | if (!cmit_fmt_is_mail(rev->commit_format)) |
5c5f1d7c | 1043 | die(_("Cover letter needs email format")); |
a5a27c79 | 1044 | |
cd2ef591 | 1045 | committer = git_committer_info(0); |
6df514af | 1046 | |
a21c2f94 | 1047 | if (!use_stdout && |
95235f5b | 1048 | open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet)) |
a5a27c79 DB |
1049 | return; |
1050 | ||
50cd54ef | 1051 | log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0); |
a5a27c79 | 1052 | |
bc6b8fc1 | 1053 | for (i = 0; !need_8bit_cte && i < nr; i++) { |
8597ea3a | 1054 | const char *buf = get_commit_buffer(list[i], NULL); |
bc6b8fc1 | 1055 | if (has_non_ascii(buf)) |
0a7f4483 | 1056 | need_8bit_cte = 1; |
bc6b8fc1 JK |
1057 | unuse_commit_buffer(list[i], buf); |
1058 | } | |
0a7f4483 | 1059 | |
d7ddad01 FC |
1060 | if (!branch_name) |
1061 | branch_name = find_branch_name(rev); | |
1062 | ||
a5a27c79 | 1063 | msg = body; |
6bf13944 | 1064 | pp.fmt = CMIT_FMT_EMAIL; |
a5481a6c | 1065 | pp.date_mode.type = DATE_RFC2822; |
6d167fd7 RS |
1066 | pp.rev = rev; |
1067 | pp.print_email_subject = 1; | |
6bf13944 JK |
1068 | pp_user_info(&pp, NULL, &sb, committer, encoding); |
1069 | pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte); | |
1070 | pp_remainder(&pp, &msg, &sb, 0); | |
739453a3 | 1071 | add_branch_description(&sb, branch_name); |
95235f5b | 1072 | fprintf(rev->diffopt.file, "%s\n", sb.buf); |
a5a27c79 DB |
1073 | |
1074 | strbuf_release(&sb); | |
1075 | ||
2bda2cf4 | 1076 | shortlog_init(&log); |
859c4fbe | 1077 | log.wrap_lines = 1; |
43662b23 | 1078 | log.wrap = MAIL_DEFAULT_WRAP; |
859c4fbe JS |
1079 | log.in1 = 2; |
1080 | log.in2 = 4; | |
95235f5b | 1081 | log.file = rev->diffopt.file; |
2bda2cf4 DB |
1082 | for (i = 0; i < nr; i++) |
1083 | shortlog_add_commit(&log, list[i]); | |
1084 | ||
1085 | shortlog_output(&log); | |
1086 | ||
fa5b7ea6 ES |
1087 | /* We can only do diffstat with a unique reference point */ |
1088 | if (origin) | |
1089 | show_diffstat(rev, origin, head); | |
a5a27c79 | 1090 | |
126facf8 | 1091 | if (rev->idiff_oid1) { |
5ac290f9 | 1092 | fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title); |
3b026417 | 1093 | show_interdiff(rev, 0); |
126facf8 | 1094 | } |
31e2617a ES |
1095 | |
1096 | if (rev->rdiff1) { | |
4ee99689 | 1097 | fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title); |
31e2617a ES |
1098 | show_range_diff(rev->rdiff1, rev->rdiff2, |
1099 | rev->creation_factor, 1, &rev->diffopt); | |
1100 | } | |
d1566f78 JT |
1101 | } |
1102 | ||
8419d2ee JH |
1103 | static const char *clean_message_id(const char *msg_id) |
1104 | { | |
1105 | char ch; | |
1106 | const char *a, *z, *m; | |
8419d2ee JH |
1107 | |
1108 | m = msg_id; | |
1109 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
1110 | m++; | |
1111 | a = m; | |
1112 | z = NULL; | |
1113 | while ((ch = *m)) { | |
1114 | if (!isspace(ch) && (ch != '>')) | |
1115 | z = m; | |
1116 | m++; | |
1117 | } | |
1118 | if (!z) | |
5c5f1d7c | 1119 | die(_("insane in-reply-to: %s"), msg_id); |
8419d2ee JH |
1120 | if (++z == m) |
1121 | return a; | |
182af834 | 1122 | return xmemdupz(a, z - a); |
8419d2ee JH |
1123 | } |
1124 | ||
9800a754 JH |
1125 | static const char *set_outdir(const char *prefix, const char *output_directory) |
1126 | { | |
1127 | if (output_directory && is_absolute_path(output_directory)) | |
1128 | return output_directory; | |
1129 | ||
1130 | if (!prefix || !*prefix) { | |
1131 | if (output_directory) | |
1132 | return output_directory; | |
1133 | /* The user did not explicitly ask for "./" */ | |
1134 | outdir_offset = 2; | |
1135 | return "./"; | |
1136 | } | |
1137 | ||
1138 | outdir_offset = strlen(prefix); | |
1139 | if (!output_directory) | |
1140 | return prefix; | |
1141 | ||
e4da43b1 | 1142 | return prefix_filename(prefix, output_directory); |
9800a754 JH |
1143 | } |
1144 | ||
fff02ee6 | 1145 | static const char * const builtin_format_patch_usage[] = { |
9c9b4f2f | 1146 | N_("git format-patch [<options>] [<since> | <revision-range>]"), |
fff02ee6 SB |
1147 | NULL |
1148 | }; | |
1149 | ||
1150 | static int keep_subject = 0; | |
1151 | ||
1152 | static int keep_callback(const struct option *opt, const char *arg, int unset) | |
1153 | { | |
1154 | ((struct rev_info *)opt->value)->total = -1; | |
1155 | keep_subject = 1; | |
1156 | return 0; | |
1157 | } | |
1158 | ||
1159 | static int subject_prefix = 0; | |
1160 | ||
1161 | static int subject_prefix_callback(const struct option *opt, const char *arg, | |
1162 | int unset) | |
1163 | { | |
1164 | subject_prefix = 1; | |
1165 | ((struct rev_info *)opt->value)->subject_prefix = arg; | |
1166 | return 0; | |
1167 | } | |
1168 | ||
68e83a5b JT |
1169 | static int rfc_callback(const struct option *opt, const char *arg, int unset) |
1170 | { | |
1171 | return subject_prefix_callback(opt, "RFC PATCH", unset); | |
1172 | } | |
1173 | ||
1af4731b JH |
1174 | static int numbered_cmdline_opt = 0; |
1175 | ||
fff02ee6 SB |
1176 | static int numbered_callback(const struct option *opt, const char *arg, |
1177 | int unset) | |
1178 | { | |
1af4731b | 1179 | *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1; |
fff02ee6 SB |
1180 | if (unset) |
1181 | auto_number = 0; | |
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | static int no_numbered_callback(const struct option *opt, const char *arg, | |
1186 | int unset) | |
1187 | { | |
1188 | return numbered_callback(opt, arg, 1); | |
1189 | } | |
1190 | ||
1191 | static int output_directory_callback(const struct option *opt, const char *arg, | |
1192 | int unset) | |
1193 | { | |
1194 | const char **dir = (const char **)opt->value; | |
1195 | if (*dir) | |
5c5f1d7c | 1196 | die(_("Two output directories?")); |
fff02ee6 SB |
1197 | *dir = arg; |
1198 | return 0; | |
1199 | } | |
1200 | ||
1201 | static int thread_callback(const struct option *opt, const char *arg, int unset) | |
1202 | { | |
1203 | int *thread = (int *)opt->value; | |
1204 | if (unset) | |
1205 | *thread = 0; | |
1206 | else if (!arg || !strcmp(arg, "shallow")) | |
1207 | *thread = THREAD_SHALLOW; | |
1208 | else if (!strcmp(arg, "deep")) | |
1209 | *thread = THREAD_DEEP; | |
1210 | else | |
1211 | return 1; | |
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | static int attach_callback(const struct option *opt, const char *arg, int unset) | |
1216 | { | |
1217 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1218 | if (unset) | |
1219 | rev->mime_boundary = NULL; | |
1220 | else if (arg) | |
1221 | rev->mime_boundary = arg; | |
1222 | else | |
1223 | rev->mime_boundary = git_version_string; | |
1224 | rev->no_inline = unset ? 0 : 1; | |
1225 | return 0; | |
1226 | } | |
1227 | ||
1228 | static int inline_callback(const struct option *opt, const char *arg, int unset) | |
1229 | { | |
1230 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1231 | if (unset) | |
1232 | rev->mime_boundary = NULL; | |
1233 | else if (arg) | |
1234 | rev->mime_boundary = arg; | |
1235 | else | |
1236 | rev->mime_boundary = git_version_string; | |
1237 | rev->no_inline = 0; | |
1238 | return 0; | |
1239 | } | |
1240 | ||
1241 | static int header_callback(const struct option *opt, const char *arg, int unset) | |
1242 | { | |
c4260034 SB |
1243 | if (unset) { |
1244 | string_list_clear(&extra_hdr, 0); | |
1245 | string_list_clear(&extra_to, 0); | |
1246 | string_list_clear(&extra_cc, 0); | |
1247 | } else { | |
1248 | add_header(arg); | |
1249 | } | |
fff02ee6 SB |
1250 | return 0; |
1251 | } | |
1252 | ||
ae6c098f SD |
1253 | static int to_callback(const struct option *opt, const char *arg, int unset) |
1254 | { | |
c4260034 SB |
1255 | if (unset) |
1256 | string_list_clear(&extra_to, 0); | |
1257 | else | |
1d2f80fa | 1258 | string_list_append(&extra_to, arg); |
fff02ee6 SB |
1259 | return 0; |
1260 | } | |
1261 | ||
1262 | static int cc_callback(const struct option *opt, const char *arg, int unset) | |
1263 | { | |
c4260034 SB |
1264 | if (unset) |
1265 | string_list_clear(&extra_cc, 0); | |
1266 | else | |
1d2f80fa | 1267 | string_list_append(&extra_cc, arg); |
fff02ee6 SB |
1268 | return 0; |
1269 | } | |
1270 | ||
a9080475 JK |
1271 | static int from_callback(const struct option *opt, const char *arg, int unset) |
1272 | { | |
1273 | char **from = opt->value; | |
1274 | ||
1275 | free(*from); | |
1276 | ||
1277 | if (unset) | |
1278 | *from = NULL; | |
1279 | else if (arg) | |
1280 | *from = xstrdup(arg); | |
1281 | else | |
1282 | *from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
1283 | return 0; | |
1284 | } | |
1285 | ||
fa2ab86d XY |
1286 | struct base_tree_info { |
1287 | struct object_id base_commit; | |
1288 | int nr_patch_id, alloc_patch_id; | |
1289 | struct object_id *patch_id; | |
1290 | }; | |
1291 | ||
1292 | static struct commit *get_base_commit(const char *base_commit, | |
1293 | struct commit **list, | |
1294 | int total) | |
1295 | { | |
1296 | struct commit *base = NULL; | |
1297 | struct commit **rev; | |
1298 | int i = 0, rev_nr = 0; | |
1299 | ||
bb52995f XY |
1300 | if (base_commit && strcmp(base_commit, "auto")) { |
1301 | base = lookup_commit_reference_by_name(base_commit); | |
1302 | if (!base) | |
1303 | die(_("Unknown commit %s"), base_commit); | |
1304 | } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) { | |
3de66517 XY |
1305 | struct branch *curr_branch = branch_get(NULL); |
1306 | const char *upstream = branch_get_upstream(curr_branch, NULL); | |
1307 | if (upstream) { | |
1308 | struct commit_list *base_list; | |
1309 | struct commit *commit; | |
d801627b | 1310 | struct object_id oid; |
3de66517 | 1311 | |
d801627b | 1312 | if (get_oid(upstream, &oid)) |
3de66517 | 1313 | die(_("Failed to resolve '%s' as a valid ref."), upstream); |
bc83266a | 1314 | commit = lookup_commit_or_die(&oid, "upstream base"); |
3de66517 XY |
1315 | base_list = get_merge_bases_many(commit, total, list); |
1316 | /* There should be one and only one merge base. */ | |
1317 | if (!base_list || base_list->next) | |
1318 | die(_("Could not find exact merge base.")); | |
1319 | base = base_list->item; | |
1320 | free_commit_list(base_list); | |
1321 | } else { | |
1322 | die(_("Failed to get upstream, if you want to record base commit automatically,\n" | |
1323 | "please use git branch --set-upstream-to to track a remote branch.\n" | |
1324 | "Or you could specify base commit by --base=<base-commit-id> manually.")); | |
1325 | } | |
3de66517 | 1326 | } |
fa2ab86d XY |
1327 | |
1328 | ALLOC_ARRAY(rev, total); | |
1329 | for (i = 0; i < total; i++) | |
1330 | rev[i] = list[i]; | |
1331 | ||
1332 | rev_nr = total; | |
1333 | /* | |
1334 | * Get merge base through pair-wise computations | |
1335 | * and store it in rev[0]. | |
1336 | */ | |
1337 | while (rev_nr > 1) { | |
1338 | for (i = 0; i < rev_nr / 2; i++) { | |
1339 | struct commit_list *merge_base; | |
1340 | merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]); | |
1341 | if (!merge_base || merge_base->next) | |
1342 | die(_("Failed to find exact merge base")); | |
1343 | ||
1344 | rev[i] = merge_base->item; | |
1345 | } | |
1346 | ||
1347 | if (rev_nr % 2) | |
1348 | rev[i] = rev[2 * i]; | |
42c78a21 | 1349 | rev_nr = DIV_ROUND_UP(rev_nr, 2); |
fa2ab86d XY |
1350 | } |
1351 | ||
1352 | if (!in_merge_bases(base, rev[0])) | |
1353 | die(_("base commit should be the ancestor of revision list")); | |
1354 | ||
1355 | for (i = 0; i < total; i++) { | |
1356 | if (base == list[i]) | |
1357 | die(_("base commit shouldn't be in revision list")); | |
1358 | } | |
1359 | ||
1360 | free(rev); | |
1361 | return base; | |
1362 | } | |
1363 | ||
7b680d32 NTND |
1364 | define_commit_slab(commit_base, int); |
1365 | ||
fa2ab86d XY |
1366 | static void prepare_bases(struct base_tree_info *bases, |
1367 | struct commit *base, | |
1368 | struct commit **list, | |
1369 | int total) | |
1370 | { | |
1371 | struct commit *commit; | |
1372 | struct rev_info revs; | |
1373 | struct diff_options diffopt; | |
7b680d32 | 1374 | struct commit_base commit_base; |
fa2ab86d XY |
1375 | int i; |
1376 | ||
1377 | if (!base) | |
1378 | return; | |
1379 | ||
7b680d32 | 1380 | init_commit_base(&commit_base); |
e6757652 | 1381 | repo_diff_setup(the_repository, &diffopt); |
0d1e0e78 | 1382 | diffopt.flags.recursive = 1; |
fa2ab86d XY |
1383 | diff_setup_done(&diffopt); |
1384 | ||
1385 | oidcpy(&bases->base_commit, &base->object.oid); | |
1386 | ||
2abf3503 | 1387 | repo_init_revisions(the_repository, &revs, NULL); |
fa2ab86d XY |
1388 | revs.max_parents = 1; |
1389 | revs.topo_order = 1; | |
1390 | for (i = 0; i < total; i++) { | |
1391 | list[i]->object.flags &= ~UNINTERESTING; | |
1392 | add_pending_object(&revs, &list[i]->object, "rev_list"); | |
7b680d32 | 1393 | *commit_base_at(&commit_base, list[i]) = 1; |
fa2ab86d XY |
1394 | } |
1395 | base->object.flags |= UNINTERESTING; | |
1396 | add_pending_object(&revs, &base->object, "base"); | |
1397 | ||
1398 | if (prepare_revision_walk(&revs)) | |
1399 | die(_("revision walk setup failed")); | |
1400 | /* | |
1401 | * Traverse the commits list, get prerequisite patch ids | |
1402 | * and stuff them in bases structure. | |
1403 | */ | |
1404 | while ((commit = get_revision(&revs)) != NULL) { | |
d801627b | 1405 | struct object_id oid; |
fa2ab86d | 1406 | struct object_id *patch_id; |
7b680d32 | 1407 | if (*commit_base_at(&commit_base, commit)) |
fa2ab86d | 1408 | continue; |
34f3c0eb | 1409 | if (commit_patch_id(commit, &diffopt, &oid, 0)) |
fa2ab86d XY |
1410 | die(_("cannot get patch id")); |
1411 | ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id); | |
1412 | patch_id = bases->patch_id + bases->nr_patch_id; | |
d801627b | 1413 | oidcpy(patch_id, &oid); |
fa2ab86d XY |
1414 | bases->nr_patch_id++; |
1415 | } | |
7b680d32 | 1416 | clear_commit_base(&commit_base); |
fa2ab86d XY |
1417 | } |
1418 | ||
95235f5b | 1419 | static void print_bases(struct base_tree_info *bases, FILE *file) |
fa2ab86d XY |
1420 | { |
1421 | int i; | |
1422 | ||
1423 | /* Only do this once, either for the cover or for the first one */ | |
1424 | if (is_null_oid(&bases->base_commit)) | |
1425 | return; | |
1426 | ||
1427 | /* Show the base commit */ | |
480871e0 | 1428 | fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit)); |
fa2ab86d XY |
1429 | |
1430 | /* Show the prerequisite patches */ | |
1431 | for (i = bases->nr_patch_id - 1; i >= 0; i--) | |
95235f5b | 1432 | fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i])); |
fa2ab86d XY |
1433 | |
1434 | free(bases->patch_id); | |
1435 | bases->nr_patch_id = 0; | |
1436 | bases->alloc_patch_id = 0; | |
1437 | oidclr(&bases->base_commit); | |
1438 | } | |
1439 | ||
5ac290f9 ES |
1440 | static const char *diff_title(struct strbuf *sb, int reroll_count, |
1441 | const char *generic, const char *rerolled) | |
1442 | { | |
1443 | if (reroll_count <= 0) | |
1444 | strbuf_addstr(sb, generic); | |
1445 | else /* RFC may be v0, so allow -v1 to diff against v0 */ | |
1446 | strbuf_addf(sb, rerolled, reroll_count - 1); | |
1447 | return sb->buf; | |
1448 | } | |
1449 | ||
31e2617a ES |
1450 | static void infer_range_diff_ranges(struct strbuf *r1, |
1451 | struct strbuf *r2, | |
1452 | const char *prev, | |
2e6fd71a | 1453 | struct commit *origin, |
31e2617a ES |
1454 | struct commit *head) |
1455 | { | |
1456 | const char *head_oid = oid_to_hex(&head->object.oid); | |
1457 | ||
2e6fd71a ES |
1458 | if (!strstr(prev, "..")) { |
1459 | strbuf_addf(r1, "%s..%s", head_oid, prev); | |
1460 | strbuf_addf(r2, "%s..%s", prev, head_oid); | |
1461 | } else if (!origin) { | |
1462 | die(_("failed to infer range-diff ranges")); | |
1463 | } else { | |
1464 | strbuf_addstr(r1, prev); | |
1465 | strbuf_addf(r2, "%s..%s", | |
1466 | oid_to_hex(&origin->object.oid), head_oid); | |
1467 | } | |
31e2617a ES |
1468 | } |
1469 | ||
a633fca0 | 1470 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
1471 | { |
1472 | struct commit *commit; | |
1473 | struct commit **list = NULL; | |
1474 | struct rev_info rev; | |
32962c9b | 1475 | struct setup_revision_opt s_r_opt; |
fff02ee6 | 1476 | int nr = 0, total, i; |
0377db77 | 1477 | int use_stdout = 0; |
fa0f02df | 1478 | int start_number = -1; |
ce37596c | 1479 | int just_numbers = 0; |
9c6efa36 | 1480 | int ignore_if_in_upstream = 0; |
2a4c2607 | 1481 | int cover_letter = -1; |
2bda2cf4 | 1482 | int boundary_count = 0; |
37c22a4b | 1483 | int no_binary_diff = 0; |
3a30aa17 | 1484 | int zero_commit = 0; |
d7ddad01 | 1485 | struct commit *origin = NULL; |
76af0734 | 1486 | const char *in_reply_to = NULL; |
5d23e133 | 1487 | struct patch_ids ids; |
f285a2d7 | 1488 | struct strbuf buf = STRBUF_INIT; |
1d46f2ea | 1489 | int use_patch_format = 0; |
250087f2 | 1490 | int quiet = 0; |
5fe10fe8 | 1491 | int reroll_count = -1; |
739453a3 | 1492 | char *branch_name = NULL; |
fa2ab86d XY |
1493 | char *base_commit = NULL; |
1494 | struct base_tree_info bases; | |
738e88a2 KW |
1495 | int show_progress = 0; |
1496 | struct progress *progress = NULL; | |
126facf8 | 1497 | struct oid_array idiff_prev = OID_ARRAY_INIT; |
5ac290f9 | 1498 | struct strbuf idiff_title = STRBUF_INIT; |
31e2617a ES |
1499 | const char *rdiff_prev = NULL; |
1500 | struct strbuf rdiff1 = STRBUF_INIT; | |
1501 | struct strbuf rdiff2 = STRBUF_INIT; | |
4ee99689 | 1502 | struct strbuf rdiff_title = STRBUF_INIT; |
8631bf1c | 1503 | int creation_factor = -1; |
fa2ab86d | 1504 | |
fff02ee6 SB |
1505 | const struct option builtin_format_patch_options[] = { |
1506 | { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL, | |
61007a58 | 1507 | N_("use [PATCH n/m] even with a single patch"), |
fff02ee6 SB |
1508 | PARSE_OPT_NOARG, numbered_callback }, |
1509 | { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL, | |
61007a58 | 1510 | N_("use [PATCH] even with multiple patches"), |
964fd83b | 1511 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback }, |
aa089cd9 FC |
1512 | OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")), |
1513 | OPT_BOOL(0, "stdout", &use_stdout, | |
61007a58 | 1514 | N_("print patches to standard out")), |
aa089cd9 | 1515 | OPT_BOOL(0, "cover-letter", &cover_letter, |
61007a58 | 1516 | N_("generate a cover letter")), |
aa089cd9 | 1517 | OPT_BOOL(0, "numbered-files", &just_numbers, |
61007a58 NTND |
1518 | N_("use simple number sequence for output file names")), |
1519 | OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"), | |
1520 | N_("use <sfx> instead of '.patch'")), | |
fff02ee6 | 1521 | OPT_INTEGER(0, "start-number", &start_number, |
61007a58 | 1522 | N_("start numbering patches at <n> instead of 1")), |
7952ea66 | 1523 | OPT_INTEGER('v', "reroll-count", &reroll_count, |
5fe10fe8 | 1524 | N_("mark the series as Nth re-roll")), |
68e83a5b JT |
1525 | { OPTION_CALLBACK, 0, "rfc", &rev, NULL, |
1526 | N_("Use [RFC PATCH] instead of [PATCH]"), | |
1527 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback }, | |
61007a58 NTND |
1528 | { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"), |
1529 | N_("Use [<prefix>] instead of [PATCH]"), | |
fff02ee6 SB |
1530 | PARSE_OPT_NONEG, subject_prefix_callback }, |
1531 | { OPTION_CALLBACK, 'o', "output-directory", &output_directory, | |
61007a58 | 1532 | N_("dir"), N_("store resulting files in <dir>"), |
fff02ee6 SB |
1533 | PARSE_OPT_NONEG, output_directory_callback }, |
1534 | { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL, | |
61007a58 | 1535 | N_("don't strip/add [PATCH]"), |
fff02ee6 | 1536 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback }, |
d5d09d47 SB |
1537 | OPT_BOOL(0, "no-binary", &no_binary_diff, |
1538 | N_("don't output binary diffs")), | |
3a30aa17 | 1539 | OPT_BOOL(0, "zero-commit", &zero_commit, |
1540 | N_("output all-zero hash in From header")), | |
d5d09d47 SB |
1541 | OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream, |
1542 | N_("don't include a patch matching a commit upstream")), | |
3e4a67b4 NTND |
1543 | OPT_SET_INT_F('p', "no-stat", &use_patch_format, |
1544 | N_("show patch format instead of default (patch + stat)"), | |
1545 | 1, PARSE_OPT_NONEG), | |
61007a58 NTND |
1546 | OPT_GROUP(N_("Messaging")), |
1547 | { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"), | |
1548 | N_("add email header"), 0, header_callback }, | |
1549 | { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"), | |
c4260034 | 1550 | 0, to_callback }, |
61007a58 | 1551 | { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"), |
c4260034 | 1552 | 0, cc_callback }, |
a9080475 JK |
1553 | { OPTION_CALLBACK, 0, "from", &from, N_("ident"), |
1554 | N_("set From address to <ident> (or committer ident if absent)"), | |
1555 | PARSE_OPT_OPTARG, from_callback }, | |
61007a58 NTND |
1556 | OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"), |
1557 | N_("make first mail a reply to <message-id>")), | |
1558 | { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"), | |
1559 | N_("attach the patch"), PARSE_OPT_OPTARG, | |
fff02ee6 | 1560 | attach_callback }, |
61007a58 NTND |
1561 | { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"), |
1562 | N_("inline the patch"), | |
fff02ee6 SB |
1563 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
1564 | inline_callback }, | |
61007a58 NTND |
1565 | { OPTION_CALLBACK, 0, "thread", &thread, N_("style"), |
1566 | N_("enable message threading, styles: shallow, deep"), | |
fff02ee6 | 1567 | PARSE_OPT_OPTARG, thread_callback }, |
61007a58 NTND |
1568 | OPT_STRING(0, "signature", &signature, N_("signature"), |
1569 | N_("add a signature")), | |
fa2ab86d XY |
1570 | OPT_STRING(0, "base", &base_commit, N_("base-commit"), |
1571 | N_("add prerequisite tree info to the patch series")), | |
7022650f JM |
1572 | OPT_FILENAME(0, "signature-file", &signature_file, |
1573 | N_("add a signature from a file")), | |
b7df098c | 1574 | OPT__QUIET(&quiet, N_("don't print the patch filenames")), |
738e88a2 KW |
1575 | OPT_BOOL(0, "progress", &show_progress, |
1576 | N_("show progress while generating patches")), | |
126facf8 | 1577 | OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"), |
ee6cbf71 | 1578 | N_("show changes against <rev> in cover letter or single patch"), |
126facf8 | 1579 | parse_opt_object_name), |
31e2617a | 1580 | OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"), |
40ce4160 | 1581 | N_("show changes against <refspec> in cover letter or single patch")), |
8631bf1c ES |
1582 | OPT_INTEGER(0, "creation-factor", &creation_factor, |
1583 | N_("percentage by which creation is weighted")), | |
fff02ee6 SB |
1584 | OPT_END() |
1585 | }; | |
91efcf60 | 1586 | |
ca9e0a1b SB |
1587 | extra_hdr.strdup_strings = 1; |
1588 | extra_to.strdup_strings = 1; | |
1589 | extra_cc.strdup_strings = 1; | |
9501d191 | 1590 | init_log_defaults(); |
ef90d6d4 | 1591 | git_config(git_format_config, NULL); |
2abf3503 | 1592 | repo_init_revisions(the_repository, &rev, prefix); |
91efcf60 | 1593 | rev.commit_format = CMIT_FMT_EMAIL; |
0893eec8 | 1594 | rev.expand_tabs_in_log_default = 0; |
91efcf60 JH |
1595 | rev.verbose_header = 1; |
1596 | rev.diff = 1; | |
ad5aeede | 1597 | rev.max_parents = 1; |
0d1e0e78 | 1598 | rev.diffopt.flags.recursive = 1; |
dbd21447 | 1599 | rev.subject_prefix = fmt_patch_subject_prefix; |
32962c9b JH |
1600 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
1601 | s_r_opt.def = "HEAD"; | |
d5f6b1d7 | 1602 | s_r_opt.revarg_opt = REVARG_COMMITTISH; |
20ff0680 | 1603 | |
0db5260b JW |
1604 | if (default_attach) { |
1605 | rev.mime_boundary = default_attach; | |
1606 | rev.no_inline = 1; | |
1607 | } | |
1608 | ||
2448482b JS |
1609 | /* |
1610 | * Parse the arguments before setup_revisions(), or something | |
2dc189a3 | 1611 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
2448482b JS |
1612 | * possibly a valid SHA1. |
1613 | */ | |
37782920 | 1614 | argc = parse_options(argc, argv, prefix, builtin_format_patch_options, |
fff02ee6 | 1615 | builtin_format_patch_usage, |
382da402 FC |
1616 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | |
1617 | PARSE_OPT_KEEP_DASHDASH); | |
2448482b | 1618 | |
5fe10fe8 JH |
1619 | if (0 < reroll_count) { |
1620 | struct strbuf sprefix = STRBUF_INIT; | |
1621 | strbuf_addf(&sprefix, "%s v%d", | |
1622 | rev.subject_prefix, reroll_count); | |
1623 | rev.reroll_count = reroll_count; | |
1624 | rev.subject_prefix = strbuf_detach(&sprefix, NULL); | |
1625 | } | |
1626 | ||
ca9e0a1b SB |
1627 | for (i = 0; i < extra_hdr.nr; i++) { |
1628 | strbuf_addstr(&buf, extra_hdr.items[i].string); | |
3ee79d9f DB |
1629 | strbuf_addch(&buf, '\n'); |
1630 | } | |
1631 | ||
ca9e0a1b | 1632 | if (extra_to.nr) |
3ee79d9f | 1633 | strbuf_addstr(&buf, "To: "); |
ca9e0a1b | 1634 | for (i = 0; i < extra_to.nr; i++) { |
3ee79d9f DB |
1635 | if (i) |
1636 | strbuf_addstr(&buf, " "); | |
ca9e0a1b SB |
1637 | strbuf_addstr(&buf, extra_to.items[i].string); |
1638 | if (i + 1 < extra_to.nr) | |
3ee79d9f DB |
1639 | strbuf_addch(&buf, ','); |
1640 | strbuf_addch(&buf, '\n'); | |
1641 | } | |
1642 | ||
ca9e0a1b | 1643 | if (extra_cc.nr) |
3ee79d9f | 1644 | strbuf_addstr(&buf, "Cc: "); |
ca9e0a1b | 1645 | for (i = 0; i < extra_cc.nr; i++) { |
3ee79d9f DB |
1646 | if (i) |
1647 | strbuf_addstr(&buf, " "); | |
ca9e0a1b SB |
1648 | strbuf_addstr(&buf, extra_cc.items[i].string); |
1649 | if (i + 1 < extra_cc.nr) | |
3ee79d9f DB |
1650 | strbuf_addch(&buf, ','); |
1651 | strbuf_addch(&buf, '\n'); | |
1652 | } | |
1653 | ||
2af202be | 1654 | rev.extra_headers = strbuf_detach(&buf, NULL); |
3ee79d9f | 1655 | |
a9080475 JK |
1656 | if (from) { |
1657 | if (split_ident_line(&rev.from_ident, from, strlen(from))) | |
1658 | die(_("invalid ident line: %s"), from); | |
1659 | } | |
1660 | ||
add5c8a5 | 1661 | if (start_number < 0) |
fa0f02df | 1662 | start_number = 1; |
ca6b91d2 JM |
1663 | |
1664 | /* | |
1665 | * If numbered is set solely due to format.numbered in config, | |
1666 | * and it would conflict with --keep-subject (-k) from the | |
1667 | * command line, reset "numbered". | |
1668 | */ | |
1669 | if (numbered && keep_subject && !numbered_cmdline_opt) | |
1670 | numbered = 0; | |
1671 | ||
63b398a4 | 1672 | if (numbered && keep_subject) |
1a07e59c | 1673 | die(_("-n and -k are mutually exclusive")); |
2d9e4a47 | 1674 | if (keep_subject && subject_prefix) |
1a07e59c | 1675 | die(_("--subject-prefix/--rfc and -k are mutually exclusive")); |
9553d2b2 | 1676 | rev.preserve_subject = keep_subject; |
8ac80a57 | 1677 | |
32962c9b | 1678 | argc = setup_revisions(argc, argv, &rev, &s_r_opt); |
2448482b | 1679 | if (argc > 1) |
1a07e59c | 1680 | die(_("unrecognized argument: %s"), argv[1]); |
0377db77 | 1681 | |
02bc5b03 | 1682 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME) |
f338cb83 | 1683 | die(_("--name-only does not make sense")); |
02bc5b03 | 1684 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) |
f338cb83 | 1685 | die(_("--name-status does not make sense")); |
02bc5b03 | 1686 | if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) |
f338cb83 | 1687 | die(_("--check does not make sense")); |
02bc5b03 BG |
1688 | |
1689 | if (!use_patch_format && | |
1690 | (!rev.diffopt.output_format || | |
1691 | rev.diffopt.output_format == DIFF_FORMAT_PATCH)) | |
1692 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; | |
071dd0ba NTND |
1693 | if (!rev.diffopt.stat_width) |
1694 | rev.diffopt.stat_width = MAIL_DEFAULT_WRAP; | |
02bc5b03 BG |
1695 | |
1696 | /* Always generate a patch */ | |
1697 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
c9b5ef99 | 1698 | |
3a30aa17 | 1699 | rev.zero_commit = zero_commit; |
1700 | ||
0d1e0e78 BW |
1701 | if (!rev.diffopt.flags.text && !no_binary_diff) |
1702 | rev.diffopt.flags.binary = 1; | |
e47f306d | 1703 | |
894a9d33 TR |
1704 | if (rev.show_notes) |
1705 | init_display_notes(&rev.notes_opt); | |
1706 | ||
bc6bf2d7 AK |
1707 | if (!output_directory && !use_stdout) |
1708 | output_directory = config_output_directory; | |
1709 | ||
9800a754 JH |
1710 | if (!use_stdout) |
1711 | output_directory = set_outdir(prefix, output_directory); | |
38a94bb6 TRC |
1712 | else |
1713 | setup_pager(); | |
77e565d8 | 1714 | |
efd02016 | 1715 | if (output_directory) { |
11f4eb19 JS |
1716 | if (rev.diffopt.use_color != GIT_COLOR_ALWAYS) |
1717 | rev.diffopt.use_color = GIT_COLOR_NEVER; | |
efd02016 | 1718 | if (use_stdout) |
5c5f1d7c | 1719 | die(_("standard output, or directory, which one?")); |
efd02016 | 1720 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) |
5c5f1d7c | 1721 | die_errno(_("Could not create directory '%s'"), |
0721c314 | 1722 | output_directory); |
efd02016 JH |
1723 | } |
1724 | ||
1f1e895f | 1725 | if (rev.pending.nr == 1) { |
427a8ec5 FC |
1726 | int check_head = 0; |
1727 | ||
8a1d076e JH |
1728 | if (rev.max_count < 0 && !rev.show_root_diff) { |
1729 | /* | |
1730 | * This is traditional behaviour of "git format-patch | |
1731 | * origin" that prepares what the origin side still | |
1732 | * does not have. | |
1733 | */ | |
7c496280 | 1734 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
3384a2df | 1735 | add_head_to_pending(&rev); |
427a8ec5 | 1736 | check_head = 1; |
7c496280 | 1737 | } |
8a1d076e JH |
1738 | /* |
1739 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
1740 | * "format-patch --root HEAD". The user wants | |
1741 | * get_revision() to do the usual traversal. | |
7c496280 | 1742 | */ |
427a8ec5 FC |
1743 | |
1744 | if (!strcmp(rev.pending.objects[0].name, "HEAD")) | |
1745 | check_head = 1; | |
1746 | ||
1747 | if (check_head) { | |
95b567c7 | 1748 | const char *ref, *v; |
7695d118 | 1749 | ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
744c040b | 1750 | NULL, NULL); |
95b567c7 JK |
1751 | if (ref && skip_prefix(ref, "refs/heads/", &v)) |
1752 | branch_name = xstrdup(v); | |
427a8ec5 FC |
1753 | else |
1754 | branch_name = xstrdup(""); /* no branch */ | |
1755 | } | |
e686eb98 | 1756 | } |
68c2ec7f JH |
1757 | |
1758 | /* | |
1759 | * We cannot move this anywhere earlier because we do want to | |
9517e6b8 | 1760 | * know if --root was given explicitly from the command line. |
68c2ec7f JH |
1761 | */ |
1762 | rev.show_root_diff = 1; | |
1763 | ||
657ab61e KB |
1764 | if (ignore_if_in_upstream) { |
1765 | /* Don't say anything if head and upstream are the same. */ | |
1766 | if (rev.pending.nr == 2) { | |
1767 | struct object_array_entry *o = rev.pending.objects; | |
4a7e27e9 | 1768 | if (oideq(&o[0].item->oid, &o[1].item->oid)) |
126facf8 | 1769 | goto done; |
657ab61e | 1770 | } |
811929f1 | 1771 | get_patch_ids(&rev, &ids); |
657ab61e | 1772 | } |
9c6efa36 | 1773 | |
3d51e1b5 | 1774 | if (prepare_revision_walk(&rev)) |
5c5f1d7c | 1775 | die(_("revision walk setup failed")); |
2bda2cf4 | 1776 | rev.boundary = 1; |
91efcf60 | 1777 | while ((commit = get_revision(&rev)) != NULL) { |
2bda2cf4 | 1778 | if (commit->object.flags & BOUNDARY) { |
2bda2cf4 DB |
1779 | boundary_count++; |
1780 | origin = (boundary_count == 1) ? commit : NULL; | |
1781 | continue; | |
1782 | } | |
1783 | ||
3acf8dd8 | 1784 | if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids)) |
9c6efa36 JS |
1785 | continue; |
1786 | ||
91efcf60 | 1787 | nr++; |
2756ca43 | 1788 | REALLOC_ARRAY(list, nr); |
91efcf60 JH |
1789 | list[nr - 1] = commit; |
1790 | } | |
80d35ca0 FC |
1791 | if (nr == 0) |
1792 | /* nothing to do */ | |
126facf8 | 1793 | goto done; |
0377db77 | 1794 | total = nr; |
2a4c2607 FC |
1795 | if (cover_letter == -1) { |
1796 | if (config_cover_letter == COVER_AUTO) | |
1797 | cover_letter = (total > 1); | |
1798 | else | |
1799 | cover_letter = (config_cover_letter == COVER_ON); | |
1800 | } | |
957ed3a5 JK |
1801 | if (!keep_subject && auto_number && (total > 1 || cover_letter)) |
1802 | numbered = 1; | |
1803 | if (numbered) | |
1804 | rev.total = total + start_number - 1; | |
2a4c2607 | 1805 | |
126facf8 | 1806 | if (idiff_prev.nr) { |
ee6cbf71 ES |
1807 | if (!cover_letter && total != 1) |
1808 | die(_("--interdiff requires --cover-letter or single patch")); | |
126facf8 ES |
1809 | rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1]; |
1810 | rev.idiff_oid2 = get_commit_tree_oid(list[0]); | |
5ac290f9 ES |
1811 | rev.idiff_title = diff_title(&idiff_title, reroll_count, |
1812 | _("Interdiff:"), | |
1813 | _("Interdiff against v%d:")); | |
126facf8 ES |
1814 | } |
1815 | ||
8631bf1c ES |
1816 | if (creation_factor < 0) |
1817 | creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT; | |
1818 | else if (!rdiff_prev) | |
1819 | die(_("--creation-factor requires --range-diff")); | |
1820 | ||
31e2617a | 1821 | if (rdiff_prev) { |
40ce4160 ES |
1822 | if (!cover_letter && total != 1) |
1823 | die(_("--range-diff requires --cover-letter or single patch")); | |
31e2617a | 1824 | |
2e6fd71a ES |
1825 | infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev, |
1826 | origin, list[0]); | |
31e2617a ES |
1827 | rev.rdiff1 = rdiff1.buf; |
1828 | rev.rdiff2 = rdiff2.buf; | |
8631bf1c | 1829 | rev.creation_factor = creation_factor; |
4ee99689 ES |
1830 | rev.rdiff_title = diff_title(&rdiff_title, reroll_count, |
1831 | _("Range-diff:"), | |
1832 | _("Range-diff against v%d:")); | |
31e2617a ES |
1833 | } |
1834 | ||
7022650f JM |
1835 | if (!signature) { |
1836 | ; /* --no-signature inhibits all signatures */ | |
1837 | } else if (signature && signature != git_version_string) { | |
1838 | ; /* non-default signature already set */ | |
1839 | } else if (signature_file) { | |
1840 | struct strbuf buf = STRBUF_INIT; | |
1841 | ||
1842 | if (strbuf_read_file(&buf, signature_file, 128) < 0) | |
1843 | die_errno(_("unable to read signature file '%s'"), signature_file); | |
1844 | signature = strbuf_detach(&buf, NULL); | |
1845 | } | |
1846 | ||
fa2ab86d | 1847 | memset(&bases, 0, sizeof(bases)); |
bb52995f | 1848 | if (base_commit || base_auto) { |
fa2ab86d XY |
1849 | struct commit *base = get_base_commit(base_commit, list, nr); |
1850 | reset_revision_walk(); | |
15b76c1f | 1851 | clear_object_flags(UNINTERESTING); |
fa2ab86d XY |
1852 | prepare_bases(&bases, base, list, nr); |
1853 | } | |
1854 | ||
b079c50e TR |
1855 | if (in_reply_to || thread || cover_letter) |
1856 | rev.ref_message_ids = xcalloc(1, sizeof(struct string_list)); | |
1857 | if (in_reply_to) { | |
1858 | const char *msgid = clean_message_id(in_reply_to); | |
1d2f80fa | 1859 | string_list_append(rev.ref_message_ids, msgid); |
b079c50e | 1860 | } |
ce37596c | 1861 | rev.numbered_files = just_numbers; |
108dab28 | 1862 | rev.patch_suffix = fmt_patch_suffix; |
a5a27c79 DB |
1863 | if (cover_letter) { |
1864 | if (thread) | |
1865 | gen_message_id(&rev, "cover"); | |
ce37596c | 1866 | make_cover_letter(&rev, use_stdout, |
d7ddad01 | 1867 | origin, nr, list, branch_name, quiet); |
95235f5b | 1868 | print_bases(&bases, rev.diffopt.file); |
480871e0 | 1869 | print_signature(rev.diffopt.file); |
a5a27c79 DB |
1870 | total++; |
1871 | start_number--; | |
40ce4160 | 1872 | /* interdiff/range-diff in cover-letter; omit from patches */ |
ee6cbf71 | 1873 | rev.idiff_oid1 = NULL; |
40ce4160 | 1874 | rev.rdiff1 = NULL; |
a5a27c79 | 1875 | } |
5289c56a | 1876 | rev.add_signoff = do_signoff; |
738e88a2 KW |
1877 | |
1878 | if (show_progress) | |
d33a4332 | 1879 | progress = start_delayed_progress(_("Generating patches"), total); |
91efcf60 JH |
1880 | while (0 <= --nr) { |
1881 | int shown; | |
738e88a2 | 1882 | display_progress(progress, total - nr); |
91efcf60 | 1883 | commit = list[nr]; |
add5c8a5 | 1884 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 1885 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 | 1886 | if (thread) { |
a5a27c79 | 1887 | /* Have we already had a message ID? */ |
e1a37346 | 1888 | if (rev.message_id) { |
a5a27c79 | 1889 | /* |
30984ed2 TR |
1890 | * For deep threading: make every mail |
1891 | * a reply to the previous one, no | |
1892 | * matter what other options are set. | |
1893 | * | |
1894 | * For shallow threading: | |
1895 | * | |
2175c10d TR |
1896 | * Without --cover-letter and |
1897 | * --in-reply-to, make every mail a | |
1898 | * reply to the one before. | |
1899 | * | |
1900 | * With --in-reply-to but no | |
1901 | * --cover-letter, make every mail a | |
1902 | * reply to the <reply-to>. | |
1903 | * | |
1904 | * With --cover-letter, make every | |
1905 | * mail but the cover letter a reply | |
1906 | * to the cover letter. The cover | |
1907 | * letter is a reply to the | |
1908 | * --in-reply-to, if specified. | |
a5a27c79 | 1909 | */ |
30984ed2 TR |
1910 | if (thread == THREAD_SHALLOW |
1911 | && rev.ref_message_ids->nr > 0 | |
2175c10d | 1912 | && (!cover_letter || rev.nr > 1)) |
e1a37346 DB |
1913 | free(rev.message_id); |
1914 | else | |
1d2f80fa JP |
1915 | string_list_append(rev.ref_message_ids, |
1916 | rev.message_id); | |
cc35de84 | 1917 | } |
f2fd0760 | 1918 | gen_message_id(&rev, oid_to_hex(&commit->object.oid)); |
d1566f78 | 1919 | } |
6df514af | 1920 | |
a21c2f94 | 1921 | if (!use_stdout && |
95235f5b | 1922 | open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet)) |
5c5f1d7c | 1923 | die(_("Failed to create output files")); |
91efcf60 | 1924 | shown = log_tree_commit(&rev, commit); |
0fb370da | 1925 | free_commit_buffer(commit); |
add5c8a5 JH |
1926 | |
1927 | /* We put one extra blank line between formatted | |
1928 | * patches and this flag is used by log-tree code | |
1929 | * to see if it needs to emit a LF before showing | |
1930 | * the log; when using one file per patch, we do | |
1931 | * not want the extra blank line. | |
1932 | */ | |
1933 | if (!use_stdout) | |
1934 | rev.shown_one = 0; | |
698ce6f8 | 1935 | if (shown) { |
480871e0 | 1936 | print_bases(&bases, rev.diffopt.file); |
698ce6f8 | 1937 | if (rev.mime_boundary) |
95235f5b | 1938 | fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n", |
698ce6f8 JS |
1939 | mime_boundary_leader, |
1940 | rev.mime_boundary); | |
1941 | else | |
95235f5b | 1942 | print_signature(rev.diffopt.file); |
698ce6f8 | 1943 | } |
0377db77 | 1944 | if (!use_stdout) |
95235f5b | 1945 | fclose(rev.diffopt.file); |
91efcf60 | 1946 | } |
738e88a2 | 1947 | stop_progress(&progress); |
91efcf60 | 1948 | free(list); |
739453a3 | 1949 | free(branch_name); |
ca9e0a1b SB |
1950 | string_list_clear(&extra_to, 0); |
1951 | string_list_clear(&extra_cc, 0); | |
1952 | string_list_clear(&extra_hdr, 0); | |
5d23e133 JH |
1953 | if (ignore_if_in_upstream) |
1954 | free_patch_ids(&ids); | |
126facf8 ES |
1955 | |
1956 | done: | |
1957 | oid_array_clear(&idiff_prev); | |
5ac290f9 | 1958 | strbuf_release(&idiff_title); |
31e2617a ES |
1959 | strbuf_release(&rdiff1); |
1960 | strbuf_release(&rdiff2); | |
4ee99689 | 1961 | strbuf_release(&rdiff_title); |
91efcf60 JH |
1962 | return 0; |
1963 | } | |
1964 | ||
e827633a RS |
1965 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
1966 | { | |
d801627b | 1967 | struct object_id oid; |
1968 | if (get_oid(arg, &oid) == 0) { | |
2122f675 SB |
1969 | struct commit *commit = lookup_commit_reference(the_repository, |
1970 | &oid); | |
e827633a RS |
1971 | if (commit) { |
1972 | commit->object.flags |= flags; | |
1973 | add_pending_object(revs, &commit->object, arg); | |
1974 | return 0; | |
1975 | } | |
1976 | } | |
1977 | return -1; | |
1978 | } | |
1979 | ||
28a53178 | 1980 | static const char * const cherry_usage[] = { |
986d1bb8 | 1981 | N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"), |
28a53178 EFL |
1982 | NULL |
1983 | }; | |
1984 | ||
a3a32e7f | 1985 | static void print_commit(char sign, struct commit *commit, int verbose, |
95235f5b | 1986 | int abbrev, FILE *file) |
a3a32e7f JN |
1987 | { |
1988 | if (!verbose) { | |
95235f5b | 1989 | fprintf(file, "%c %s\n", sign, |
aab9583f | 1990 | find_unique_abbrev(&commit->object.oid, abbrev)); |
a3a32e7f JN |
1991 | } else { |
1992 | struct strbuf buf = STRBUF_INIT; | |
f67d2e82 | 1993 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); |
95235f5b | 1994 | fprintf(file, "%c %s %s\n", sign, |
aab9583f | 1995 | find_unique_abbrev(&commit->object.oid, abbrev), |
a3a32e7f JN |
1996 | buf.buf); |
1997 | strbuf_release(&buf); | |
1998 | } | |
1999 | } | |
2000 | ||
e827633a RS |
2001 | int cmd_cherry(int argc, const char **argv, const char *prefix) |
2002 | { | |
2003 | struct rev_info revs; | |
5d23e133 | 2004 | struct patch_ids ids; |
e827633a RS |
2005 | struct commit *commit; |
2006 | struct commit_list *list = NULL; | |
f2968022 | 2007 | struct branch *current_branch; |
e827633a RS |
2008 | const char *upstream; |
2009 | const char *head = "HEAD"; | |
2010 | const char *limit = NULL; | |
28a53178 | 2011 | int verbose = 0, abbrev = 0; |
e827633a | 2012 | |
28a53178 EFL |
2013 | struct option options[] = { |
2014 | OPT__ABBREV(&abbrev), | |
986d1bb8 | 2015 | OPT__VERBOSE(&verbose, N_("be verbose")), |
28a53178 EFL |
2016 | OPT_END() |
2017 | }; | |
e827633a | 2018 | |
28a53178 | 2019 | argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); |
fef34270 | 2020 | |
e827633a | 2021 | switch (argc) { |
e827633a | 2022 | case 3: |
28a53178 | 2023 | limit = argv[2]; |
e827633a RS |
2024 | /* FALLTHROUGH */ |
2025 | case 2: | |
28a53178 EFL |
2026 | head = argv[1]; |
2027 | /* FALLTHROUGH */ | |
2028 | case 1: | |
2029 | upstream = argv[0]; | |
e827633a RS |
2030 | break; |
2031 | default: | |
f2968022 | 2032 | current_branch = branch_get(NULL); |
3a429d0a | 2033 | upstream = branch_get_upstream(current_branch, NULL); |
a9f9f8cc | 2034 | if (!upstream) { |
5c5f1d7c | 2035 | fprintf(stderr, _("Could not find a tracked" |
f2968022 | 2036 | " remote branch, please" |
5c5f1d7c | 2037 | " specify <upstream> manually.\n")); |
28a53178 | 2038 | usage_with_options(cherry_usage, options); |
f2968022 | 2039 | } |
e827633a RS |
2040 | } |
2041 | ||
2abf3503 | 2042 | repo_init_revisions(the_repository, &revs, prefix); |
51f4de3a | 2043 | revs.max_parents = 1; |
e827633a RS |
2044 | |
2045 | if (add_pending_commit(head, &revs, 0)) | |
5c5f1d7c | 2046 | die(_("Unknown commit %s"), head); |
e827633a | 2047 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) |
5c5f1d7c | 2048 | die(_("Unknown commit %s"), upstream); |
e827633a RS |
2049 | |
2050 | /* Don't say anything if head and upstream are the same. */ | |
2051 | if (revs.pending.nr == 2) { | |
2052 | struct object_array_entry *o = revs.pending.objects; | |
4a7e27e9 | 2053 | if (oideq(&o[0].item->oid, &o[1].item->oid)) |
e827633a RS |
2054 | return 0; |
2055 | } | |
2056 | ||
811929f1 | 2057 | get_patch_ids(&revs, &ids); |
e827633a RS |
2058 | |
2059 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
5c5f1d7c | 2060 | die(_("Unknown commit %s"), limit); |
e827633a RS |
2061 | |
2062 | /* reverse the list of commits */ | |
3d51e1b5 | 2063 | if (prepare_revision_walk(&revs)) |
5c5f1d7c | 2064 | die(_("revision walk setup failed")); |
e827633a | 2065 | while ((commit = get_revision(&revs)) != NULL) { |
e827633a RS |
2066 | commit_list_insert(commit, &list); |
2067 | } | |
2068 | ||
2069 | while (list) { | |
e827633a RS |
2070 | char sign = '+'; |
2071 | ||
2072 | commit = list->item; | |
5d23e133 | 2073 | if (has_commit_patch_id(commit, &ids)) |
e827633a | 2074 | sign = '-'; |
95235f5b | 2075 | print_commit(sign, commit, verbose, abbrev, revs.diffopt.file); |
e827633a RS |
2076 | list = list->next; |
2077 | } | |
2078 | ||
5d23e133 | 2079 | free_patch_ids(&ids); |
e827633a RS |
2080 | return 0; |
2081 | } |