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