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