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