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