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