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