]>
Commit | Line | Data |
---|---|---|
70827b15 LT |
1 | /* |
2 | * Builtin "git log" and related commands (show, whatchanged) | |
3 | * | |
4 | * (C) Copyright 2006 Linus Torvalds | |
5 | * 2006 Junio Hamano | |
6 | */ | |
7 | #include "cache.h" | |
b2141fc1 | 8 | #include "config.h" |
fb58c8d5 | 9 | #include "refs.h" |
6b2f2d98 | 10 | #include "color.h" |
70827b15 LT |
11 | #include "commit.h" |
12 | #include "diff.h" | |
13 | #include "revision.h" | |
14 | #include "log-tree.h" | |
91efcf60 | 15 | #include "builtin.h" |
5d7eeee2 | 16 | #include "tag.h" |
cf39f54e | 17 | #include "reflog-walk.h" |
5d23e133 | 18 | #include "patch-ids.h" |
a5a27c79 | 19 | #include "run-command.h" |
2bda2cf4 | 20 | #include "shortlog.h" |
f2968022 | 21 | #include "remote.h" |
b079c50e | 22 | #include "string-list.h" |
fff02ee6 | 23 | #include "parse-options.h" |
12da1d1f | 24 | #include "line-log.h" |
739453a3 | 25 | #include "branch.h" |
74775a09 | 26 | #include "streaming.h" |
816fb46b | 27 | #include "version.h" |
ea57bc0d | 28 | #include "mailmap.h" |
6005dbb9 | 29 | #include "gpg-interface.h" |
70827b15 | 30 | |
dd0ffd5b HO |
31 | /* Set a default date-time format for git log ("log.date" config variable) */ |
32 | static const char *default_date_mode = NULL; | |
33 | ||
0c47695a | 34 | static int default_abbrev_commit; |
0f03ca94 | 35 | static int default_show_root = 1; |
076c9837 | 36 | static int default_follow; |
fce04c3c | 37 | static int default_show_signature; |
8a3d203b | 38 | static int decoration_style; |
1c40c36b | 39 | static int decoration_given; |
e6bb5f78 | 40 | static int use_mailmap_config; |
d54276f2 | 41 | static const char *fmt_patch_subject_prefix = "PATCH"; |
94c22a5e | 42 | static const char *fmt_pretty; |
0f03ca94 | 43 | |
1c40c36b | 44 | static const char * const builtin_log_usage[] = { |
ce41720c | 45 | N_("git log [<options>] [<revision-range>] [[--] <path>...]"), |
bb831db6 | 46 | N_("git show [<options>] <object>..."), |
1c40c36b CMN |
47 | NULL |
48 | }; | |
1c370ea4 | 49 | |
12da1d1f TR |
50 | struct line_opt_callback_data { |
51 | struct rev_info *rev; | |
52 | const char *prefix; | |
53 | struct string_list args; | |
54 | }; | |
55 | ||
940a911f AH |
56 | static int auto_decoration_style(void) |
57 | { | |
58 | return (isatty(1) || pager_in_use()) ? DECORATE_SHORT_REFS : 0; | |
59 | } | |
60 | ||
8a3d203b JH |
61 | static int parse_decoration_style(const char *var, const char *value) |
62 | { | |
63 | switch (git_config_maybe_bool(var, value)) { | |
64 | case 1: | |
65 | return DECORATE_SHORT_REFS; | |
66 | case 0: | |
67 | return 0; | |
68 | default: | |
69 | break; | |
70 | } | |
71 | if (!strcmp(value, "full")) | |
72 | return DECORATE_FULL_REFS; | |
73 | else if (!strcmp(value, "short")) | |
74 | return DECORATE_SHORT_REFS; | |
15715866 | 75 | else if (!strcmp(value, "auto")) |
940a911f | 76 | return auto_decoration_style(); |
8a3d203b JH |
77 | return -1; |
78 | } | |
79 | ||
1c40c36b CMN |
80 | static int decorate_callback(const struct option *opt, const char *arg, int unset) |
81 | { | |
82 | if (unset) | |
83 | decoration_style = 0; | |
84 | else if (arg) | |
85 | decoration_style = parse_decoration_style("command line", arg); | |
86 | else | |
87 | decoration_style = DECORATE_SHORT_REFS; | |
88 | ||
89 | if (decoration_style < 0) | |
e4a590ef | 90 | die(_("invalid --decorate option: %s"), arg); |
1c40c36b CMN |
91 | |
92 | decoration_given = 1; | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
12da1d1f TR |
97 | static int log_line_range_callback(const struct option *option, const char *arg, int unset) |
98 | { | |
99 | struct line_opt_callback_data *data = option->value; | |
100 | ||
101 | if (!arg) | |
102 | return -1; | |
103 | ||
104 | data->rev->line_level_traverse = 1; | |
105 | string_list_append(&data->args, arg); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
9501d191 MM |
110 | static void init_log_defaults(void) |
111 | { | |
112 | init_grep_defaults(); | |
5404c116 | 113 | init_diff_ui_defaults(); |
c74271aa | 114 | |
115 | decoration_style = auto_decoration_style(); | |
9501d191 MM |
116 | } |
117 | ||
ef803fd4 | 118 | static void cmd_log_init_defaults(struct rev_info *rev) |
70827b15 | 119 | { |
94c22a5e | 120 | if (fmt_pretty) |
4da45bef | 121 | get_commit_format(fmt_pretty, rev); |
076c9837 DT |
122 | if (default_follow) |
123 | DIFF_OPT_SET(&rev->diffopt, DEFAULT_FOLLOW_RENAMES); | |
70827b15 | 124 | rev->verbose_header = 1; |
8f67f8ae | 125 | DIFF_OPT_SET(&rev->diffopt, RECURSIVE); |
5e0ec15e | 126 | rev->diffopt.stat_width = -1; /* use full terminal width */ |
df44483a | 127 | rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */ |
0c47695a | 128 | rev->abbrev_commit = default_abbrev_commit; |
0f03ca94 | 129 | rev->show_root_diff = default_show_root; |
d54276f2 | 130 | rev->subject_prefix = fmt_patch_subject_prefix; |
fce04c3c | 131 | rev->show_signature = default_show_signature; |
5ec11af6 | 132 | DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV); |
dd0ffd5b HO |
133 | |
134 | if (default_date_mode) | |
a5481a6c | 135 | parse_date_format(default_date_mode, &rev->date_mode); |
6c374008 | 136 | rev->diffopt.touched_flags = 0; |
ef803fd4 | 137 | } |
dd0ffd5b | 138 | |
ef803fd4 MG |
139 | static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, |
140 | struct rev_info *rev, struct setup_revision_opt *opt) | |
141 | { | |
ef803fd4 | 142 | struct userformat_want w; |
ea57bc0d | 143 | int quiet = 0, source = 0, mailmap = 0; |
12da1d1f | 144 | static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP}; |
1c40c36b CMN |
145 | |
146 | const struct option builtin_log_options[] = { | |
b7df098c | 147 | OPT__QUIET(&quiet, N_("suppress diff output")), |
aa089cd9 FC |
148 | OPT_BOOL(0, "source", &source, N_("show source")), |
149 | OPT_BOOL(0, "use-mailmap", &mailmap, N_("Use mail map file")), | |
e62cd35a | 150 | { OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"), |
1c40c36b | 151 | PARSE_OPT_OPTARG, decorate_callback}, |
12da1d1f | 152 | OPT_CALLBACK('L', NULL, &line_cb, "n,m:file", |
e4a590ef | 153 | N_("Process line range n,m in file, counting from 1"), |
12da1d1f | 154 | log_line_range_callback), |
1c40c36b CMN |
155 | OPT_END() |
156 | }; | |
157 | ||
12da1d1f TR |
158 | line_cb.rev = rev; |
159 | line_cb.prefix = prefix; | |
160 | ||
e6bb5f78 | 161 | mailmap = use_mailmap_config; |
1c40c36b CMN |
162 | argc = parse_options(argc, argv, prefix, |
163 | builtin_log_options, builtin_log_usage, | |
164 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | | |
165 | PARSE_OPT_KEEP_DASHDASH); | |
52883fbd | 166 | |
01771a8e JH |
167 | if (quiet) |
168 | rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT; | |
f9c75d85 | 169 | argc = setup_revisions(argc, argv, rev, opt); |
dd0ffd5b | 170 | |
1c40c36b CMN |
171 | /* Any arguments at this point are not recognized */ |
172 | if (argc > 1) | |
e4a590ef | 173 | die(_("unrecognized argument: %s"), argv[1]); |
1c40c36b | 174 | |
5b163603 JG |
175 | memset(&w, 0, sizeof(w)); |
176 | userformat_find_requirements(NULL, &w); | |
177 | ||
178 | if (!rev->show_notes_given && (!rev->pretty_given || w.notes)) | |
66b2ed09 | 179 | rev->show_notes = 1; |
894a9d33 TR |
180 | if (rev->show_notes) |
181 | init_display_notes(&rev->notes_opt); | |
66b2ed09 | 182 | |
dd63f169 JK |
183 | if (rev->diffopt.pickaxe || rev->diffopt.filter || |
184 | DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) | |
9dafea26 | 185 | rev->always_show_header = 0; |
1c40c36b CMN |
186 | |
187 | if (source) | |
188 | rev->show_source = 1; | |
635530a2 | 189 | |
ea57bc0d AP |
190 | if (mailmap) { |
191 | rev->mailmap = xcalloc(1, sizeof(struct string_list)); | |
192 | read_mailmap(rev->mailmap, NULL); | |
193 | } | |
194 | ||
0c47695a JS |
195 | if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) { |
196 | /* | |
197 | * "log --pretty=raw" is special; ignore UI oriented | |
198 | * configuration variables such as decoration. | |
199 | */ | |
200 | if (!decoration_given) | |
201 | decoration_style = 0; | |
202 | if (!rev->abbrev_commit_given) | |
203 | rev->abbrev_commit = 0; | |
204 | } | |
635530a2 | 205 | |
33e7018c LH |
206 | if (decoration_style) { |
207 | rev->show_decorations = 1; | |
208 | load_ref_decorations(decoration_style); | |
209 | } | |
12da1d1f TR |
210 | |
211 | if (rev->line_level_traverse) | |
212 | line_log_init(rev, line_cb.prefix, &line_cb.args); | |
213 | ||
1fda91b5 | 214 | setup_pager(); |
9dafea26 TH |
215 | } |
216 | ||
ef803fd4 MG |
217 | static void cmd_log_init(int argc, const char **argv, const char *prefix, |
218 | struct rev_info *rev, struct setup_revision_opt *opt) | |
219 | { | |
220 | cmd_log_init_defaults(rev); | |
221 | cmd_log_init_finish(argc, argv, prefix, rev, opt); | |
222 | } | |
223 | ||
252a7c02 LT |
224 | /* |
225 | * This gives a rough estimate for how many commits we | |
226 | * will print out in the list. | |
227 | */ | |
228 | static int estimate_commit_count(struct rev_info *rev, struct commit_list *list) | |
229 | { | |
230 | int n = 0; | |
231 | ||
232 | while (list) { | |
233 | struct commit *commit = list->item; | |
234 | unsigned int flags = commit->object.flags; | |
252a7c02 | 235 | list = list->next; |
7dc0fe3b | 236 | if (!(flags & (TREESAME | UNINTERESTING))) |
53b2c823 | 237 | n++; |
252a7c02 LT |
238 | } |
239 | return n; | |
240 | } | |
241 | ||
242 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
243 | { | |
244 | if (rev->shown_one) { | |
245 | rev->shown_one = 0; | |
246 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
247 | putchar(rev->diffopt.line_termination); | |
248 | } | |
95235f5b | 249 | fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage); |
252a7c02 LT |
250 | } |
251 | ||
2af202be | 252 | static struct itimerval early_output_timer; |
252a7c02 | 253 | |
cdcefbc9 LT |
254 | static void log_show_early(struct rev_info *revs, struct commit_list *list) |
255 | { | |
6ea57703 | 256 | int i = revs->early_output, close_file = revs->diffopt.close_file; |
252a7c02 | 257 | int show_header = 1; |
cdcefbc9 | 258 | |
6ea57703 | 259 | revs->diffopt.close_file = 0; |
08f704f2 | 260 | sort_in_topological_order(&list, revs->sort_order); |
cdcefbc9 LT |
261 | while (list && i) { |
262 | struct commit *commit = list->item; | |
252a7c02 LT |
263 | switch (simplify_commit(revs, commit)) { |
264 | case commit_show: | |
265 | if (show_header) { | |
266 | int n = estimate_commit_count(revs, list); | |
267 | show_early_header(revs, "incomplete", n); | |
268 | show_header = 0; | |
269 | } | |
270 | log_tree_commit(revs, commit); | |
271 | i--; | |
272 | break; | |
273 | case commit_ignore: | |
274 | break; | |
275 | case commit_error: | |
6ea57703 JS |
276 | if (close_file) |
277 | fclose(revs->diffopt.file); | |
252a7c02 LT |
278 | return; |
279 | } | |
cdcefbc9 | 280 | list = list->next; |
cdcefbc9 | 281 | } |
252a7c02 LT |
282 | |
283 | /* Did we already get enough commits for the early output? */ | |
6ea57703 JS |
284 | if (!i) { |
285 | if (close_file) | |
286 | fclose(revs->diffopt.file); | |
252a7c02 | 287 | return; |
6ea57703 | 288 | } |
252a7c02 LT |
289 | |
290 | /* | |
291 | * ..if no, then repeat it twice a second until we | |
292 | * do. | |
293 | * | |
294 | * NOTE! We don't use "it_interval", because if the | |
295 | * reader isn't listening, we want our output to be | |
296 | * throttled by the writing, and not have the timer | |
297 | * trigger every second even if we're blocked on a | |
298 | * reader! | |
299 | */ | |
300 | early_output_timer.it_value.tv_sec = 0; | |
301 | early_output_timer.it_value.tv_usec = 500000; | |
302 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
303 | } |
304 | ||
305 | static void early_output(int signal) | |
306 | { | |
307 | show_early_output = log_show_early; | |
308 | } | |
309 | ||
310 | static void setup_early_output(struct rev_info *rev) | |
311 | { | |
312 | struct sigaction sa; | |
cdcefbc9 LT |
313 | |
314 | /* | |
315 | * Set up the signal handler, minimally intrusively: | |
316 | * we only set a single volatile integer word (not | |
317 | * using sigatomic_t - trying to avoid unnecessary | |
318 | * system dependencies and headers), and using | |
319 | * SA_RESTART. | |
320 | */ | |
321 | memset(&sa, 0, sizeof(sa)); | |
322 | sa.sa_handler = early_output; | |
323 | sigemptyset(&sa.sa_mask); | |
324 | sa.sa_flags = SA_RESTART; | |
325 | sigaction(SIGALRM, &sa, NULL); | |
326 | ||
327 | /* | |
328 | * If we can get the whole output in less than a | |
329 | * tenth of a second, don't even bother doing the | |
330 | * early-output thing.. | |
331 | * | |
332 | * This is a one-time-only trigger. | |
333 | */ | |
252a7c02 LT |
334 | early_output_timer.it_value.tv_sec = 0; |
335 | early_output_timer.it_value.tv_usec = 100000; | |
336 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
cdcefbc9 LT |
337 | } |
338 | ||
339 | static void finish_early_output(struct rev_info *rev) | |
340 | { | |
252a7c02 | 341 | int n = estimate_commit_count(rev, rev->commits); |
cdcefbc9 | 342 | signal(SIGALRM, SIG_IGN); |
252a7c02 | 343 | show_early_header(rev, "done", n); |
cdcefbc9 LT |
344 | } |
345 | ||
9dafea26 TH |
346 | static int cmd_log_walk(struct rev_info *rev) |
347 | { | |
348 | struct commit *commit; | |
f31027c9 | 349 | int saved_nrl = 0; |
6ea57703 | 350 | int saved_dcctc = 0, close_file = rev->diffopt.close_file; |
70827b15 | 351 | |
cdcefbc9 LT |
352 | if (rev->early_output) |
353 | setup_early_output(rev); | |
354 | ||
3d51e1b5 | 355 | if (prepare_revision_walk(rev)) |
5c5f1d7c | 356 | die(_("revision walk setup failed")); |
cdcefbc9 LT |
357 | |
358 | if (rev->early_output) | |
359 | finish_early_output(rev); | |
360 | ||
036d17fe | 361 | /* |
84102a33 PVM |
362 | * For --check and --exit-code, the exit code is based on CHECK_FAILED |
363 | * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to | |
364 | * retain that state information if replacing rev->diffopt in this loop | |
036d17fe | 365 | */ |
6ea57703 | 366 | rev->diffopt.close_file = 0; |
70827b15 | 367 | while ((commit = get_revision(rev)) != NULL) { |
3acf8dd8 | 368 | if (!log_tree_commit(rev, commit) && rev->max_count >= 0) |
251df09b MM |
369 | /* |
370 | * We decremented max_count in get_revision, | |
371 | * but we didn't actually show the commit. | |
372 | */ | |
373 | rev->max_count++; | |
a6c73064 JS |
374 | if (!rev->reflog_info) { |
375 | /* we allow cycles in reflog ancestry */ | |
0fb370da | 376 | free_commit_buffer(commit); |
a6c73064 | 377 | } |
cb115748 LT |
378 | free_commit_list(commit->parents); |
379 | commit->parents = NULL; | |
f31027c9 JH |
380 | if (saved_nrl < rev->diffopt.needed_rename_limit) |
381 | saved_nrl = rev->diffopt.needed_rename_limit; | |
382 | if (rev->diffopt.degraded_cc_to_c) | |
383 | saved_dcctc = 1; | |
70827b15 | 384 | } |
f31027c9 JH |
385 | rev->diffopt.degraded_cc_to_c = saved_dcctc; |
386 | rev->diffopt.needed_rename_limit = saved_nrl; | |
6ea57703 JS |
387 | if (close_file) |
388 | fclose(rev->diffopt.file); | |
f31027c9 | 389 | |
036d17fe PVM |
390 | if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && |
391 | DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) { | |
392 | return 02; | |
393 | } | |
84102a33 | 394 | return diff_result_code(&rev->diffopt, 0); |
70827b15 LT |
395 | } |
396 | ||
ef90d6d4 | 397 | static int git_log_config(const char *var, const char *value, void *cb) |
0f03ca94 | 398 | { |
e3f1da98 RS |
399 | const char *slot_name; |
400 | ||
94c22a5e CR |
401 | if (!strcmp(var, "format.pretty")) |
402 | return git_config_string(&fmt_pretty, var, value); | |
70cff3ac BH |
403 | if (!strcmp(var, "format.subjectprefix")) |
404 | return git_config_string(&fmt_patch_subject_prefix, var, value); | |
0c47695a JS |
405 | if (!strcmp(var, "log.abbrevcommit")) { |
406 | default_abbrev_commit = git_config_bool(var, value); | |
407 | return 0; | |
408 | } | |
dd0ffd5b HO |
409 | if (!strcmp(var, "log.date")) |
410 | return git_config_string(&default_date_mode, var, value); | |
eb734454 | 411 | if (!strcmp(var, "log.decorate")) { |
8a3d203b JH |
412 | decoration_style = parse_decoration_style(var, value); |
413 | if (decoration_style < 0) | |
414 | decoration_style = 0; /* maybe warn? */ | |
eb734454 SD |
415 | return 0; |
416 | } | |
0f03ca94 PB |
417 | if (!strcmp(var, "log.showroot")) { |
418 | default_show_root = git_config_bool(var, value); | |
419 | return 0; | |
420 | } | |
076c9837 DT |
421 | if (!strcmp(var, "log.follow")) { |
422 | default_follow = git_config_bool(var, value); | |
423 | return 0; | |
424 | } | |
e3f1da98 | 425 | if (skip_prefix(var, "color.decorate.", &slot_name)) |
b9465768 | 426 | return parse_decorate_color_config(var, slot_name, value); |
e6bb5f78 AP |
427 | if (!strcmp(var, "log.mailmap")) { |
428 | use_mailmap_config = git_config_bool(var, value); | |
429 | return 0; | |
430 | } | |
fce04c3c MJ |
431 | if (!strcmp(var, "log.showsignature")) { |
432 | default_show_signature = git_config_bool(var, value); | |
433 | return 0; | |
434 | } | |
e6bb5f78 | 435 | |
0657bcbf JH |
436 | if (grep_config(var, value, cb) < 0) |
437 | return -1; | |
6005dbb9 | 438 | if (git_gpg_config(var, value, cb) < 0) |
0657bcbf | 439 | return -1; |
ef90d6d4 | 440 | return git_diff_ui_config(var, value, cb); |
0f03ca94 PB |
441 | } |
442 | ||
a633fca0 | 443 | int cmd_whatchanged(int argc, const char **argv, const char *prefix) |
70827b15 LT |
444 | { |
445 | struct rev_info rev; | |
32962c9b | 446 | struct setup_revision_opt opt; |
70827b15 | 447 | |
9501d191 | 448 | init_log_defaults(); |
ef90d6d4 | 449 | git_config(git_log_config, NULL); |
6b2f2d98 | 450 | |
db6296a5 | 451 | init_revisions(&rev, prefix); |
70827b15 | 452 | rev.diff = 1; |
9202434c | 453 | rev.simplify_history = 0; |
32962c9b JH |
454 | memset(&opt, 0, sizeof(opt)); |
455 | opt.def = "HEAD"; | |
d5f6b1d7 | 456 | opt.revarg_opt = REVARG_COMMITTISH; |
32962c9b | 457 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
9dafea26 TH |
458 | if (!rev.diffopt.output_format) |
459 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
460 | return cmd_log_walk(&rev); | |
70827b15 LT |
461 | } |
462 | ||
56122ed8 JS |
463 | static void show_tagger(char *buf, int len, struct rev_info *rev) |
464 | { | |
ea718e65 | 465 | struct strbuf out = STRBUF_INIT; |
6bf13944 | 466 | struct pretty_print_context pp = {0}; |
56122ed8 | 467 | |
6bf13944 JK |
468 | pp.fmt = rev->commit_format; |
469 | pp.date_mode = rev->date_mode; | |
470 | pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding()); | |
95235f5b | 471 | fprintf(rev->diffopt.file, "%s", out.buf); |
ea718e65 | 472 | strbuf_release(&out); |
56122ed8 JS |
473 | } |
474 | ||
d801627b | 475 | static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name) |
74775a09 | 476 | { |
d801627b | 477 | struct object_id oidc; |
083b9931 MG |
478 | struct object_context obj_context; |
479 | char *buf; | |
480 | unsigned long size; | |
481 | ||
95235f5b | 482 | fflush(rev->diffopt.file); |
083b9931 MG |
483 | if (!DIFF_OPT_TOUCHED(&rev->diffopt, ALLOW_TEXTCONV) || |
484 | !DIFF_OPT_TST(&rev->diffopt, ALLOW_TEXTCONV)) | |
7eda0e4f | 485 | return stream_blob_to_fd(1, oid, NULL, 0); |
083b9931 | 486 | |
dc944b65 JK |
487 | if (get_sha1_with_context(obj_name, GET_SHA1_RECORD_PATH, |
488 | oidc.hash, &obj_context)) | |
e4a590ef | 489 | die(_("Not a valid object name %s"), obj_name); |
dc944b65 JK |
490 | if (!obj_context.path || |
491 | !textconv_object(obj_context.path, obj_context.mode, &oidc, 1, &buf, &size)) { | |
492 | free(obj_context.path); | |
7eda0e4f | 493 | return stream_blob_to_fd(1, oid, NULL, 0); |
dc944b65 | 494 | } |
083b9931 MG |
495 | |
496 | if (!buf) | |
e4a590ef | 497 | die(_("git show %s: bad file"), obj_name); |
083b9931 MG |
498 | |
499 | write_or_die(1, buf, size); | |
dc944b65 | 500 | free(obj_context.path); |
083b9931 | 501 | return 0; |
74775a09 NTND |
502 | } |
503 | ||
d801627b | 504 | static int show_tag_object(const struct object_id *oid, struct rev_info *rev) |
5d7eeee2 JS |
505 | { |
506 | unsigned long size; | |
21666f1a | 507 | enum object_type type; |
d801627b | 508 | char *buf = read_sha1_file(oid->hash, &type, &size); |
5d7eeee2 JS |
509 | int offset = 0; |
510 | ||
511 | if (!buf) | |
d801627b | 512 | return error(_("Could not read object %s"), oid_to_hex(oid)); |
5d7eeee2 | 513 | |
74775a09 NTND |
514 | assert(type == OBJ_TAG); |
515 | while (offset < size && buf[offset] != '\n') { | |
516 | int new_offset = offset + 1; | |
517 | while (new_offset < size && buf[new_offset++] != '\n') | |
518 | ; /* do nothing */ | |
59556548 | 519 | if (starts_with(buf + offset, "tagger ")) |
74775a09 NTND |
520 | show_tagger(buf + offset + 7, |
521 | new_offset - offset - 7, rev); | |
522 | offset = new_offset; | |
523 | } | |
5d7eeee2 JS |
524 | |
525 | if (offset < size) | |
95235f5b | 526 | fwrite(buf + offset, size - offset, 1, rev->diffopt.file); |
5d7eeee2 JS |
527 | free(buf); |
528 | return 0; | |
529 | } | |
530 | ||
531 | static int show_tree_object(const unsigned char *sha1, | |
6a0b0b6d | 532 | struct strbuf *base, |
671f0707 | 533 | const char *pathname, unsigned mode, int stage, void *context) |
5d7eeee2 | 534 | { |
95235f5b JS |
535 | FILE *file = context; |
536 | fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
5d7eeee2 JS |
537 | return 0; |
538 | } | |
539 | ||
b130c706 JH |
540 | static void show_setup_revisions_tweak(struct rev_info *rev, |
541 | struct setup_revision_opt *opt) | |
b4490059 | 542 | { |
2bf65873 JH |
543 | if (rev->ignore_merges) { |
544 | /* There was no "-m" on the command line */ | |
545 | rev->ignore_merges = 0; | |
546 | if (!rev->first_parent_only && !rev->combine_merges) { | |
01689909 | 547 | /* No "--first-parent", "-c", or "--cc" */ |
2bf65873 JH |
548 | rev->combine_merges = 1; |
549 | rev->dense_combined_merges = 1; | |
550 | } | |
551 | } | |
b4490059 JH |
552 | if (!rev->diffopt.output_format) |
553 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
554 | } | |
555 | ||
a633fca0 | 556 | int cmd_show(int argc, const char **argv, const char *prefix) |
70827b15 LT |
557 | { |
558 | struct rev_info rev; | |
5d7eeee2 | 559 | struct object_array_entry *objects; |
32962c9b | 560 | struct setup_revision_opt opt; |
f0096c06 | 561 | struct pathspec match_all; |
5d7eeee2 | 562 | int i, count, ret = 0; |
70827b15 | 563 | |
9501d191 | 564 | init_log_defaults(); |
ef90d6d4 | 565 | git_config(git_log_config, NULL); |
6b2f2d98 | 566 | |
9a087274 | 567 | memset(&match_all, 0, sizeof(match_all)); |
db6296a5 | 568 | init_revisions(&rev, prefix); |
70827b15 | 569 | rev.diff = 1; |
70827b15 | 570 | rev.always_show_header = 1; |
ca92e59e | 571 | rev.no_walk = REVISION_WALK_NO_WALK_SORTED; |
666c92a2 ZJS |
572 | rev.diffopt.stat_width = -1; /* Scale to real terminal size */ |
573 | ||
32962c9b JH |
574 | memset(&opt, 0, sizeof(opt)); |
575 | opt.def = "HEAD"; | |
b130c706 | 576 | opt.tweak = show_setup_revisions_tweak; |
32962c9b | 577 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
5d7eeee2 | 578 | |
c5941f1a JH |
579 | if (!rev.no_walk) |
580 | return cmd_log_walk(&rev); | |
581 | ||
5d7eeee2 JS |
582 | count = rev.pending.nr; |
583 | objects = rev.pending.objects; | |
584 | for (i = 0; i < count && !ret; i++) { | |
585 | struct object *o = objects[i].item; | |
586 | const char *name = objects[i].name; | |
587 | switch (o->type) { | |
588 | case OBJ_BLOB: | |
d801627b | 589 | ret = show_blob_object(&o->oid, &rev, name); |
5d7eeee2 JS |
590 | break; |
591 | case OBJ_TAG: { | |
592 | struct tag *t = (struct tag *)o; | |
593 | ||
ae03ee64 JK |
594 | if (rev.shown_one) |
595 | putchar('\n'); | |
95235f5b | 596 | fprintf(rev.diffopt.file, "%stag %s%s\n", |
8f67f8ae | 597 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 598 | t->tag, |
8f67f8ae | 599 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
d801627b | 600 | ret = show_tag_object(&o->oid, &rev); |
ae03ee64 | 601 | rev.shown_one = 1; |
d2dadfe8 JH |
602 | if (ret) |
603 | break; | |
c251c83d | 604 | o = parse_object(&t->tagged->oid); |
d2dadfe8 | 605 | if (!o) |
5c5f1d7c | 606 | ret = error(_("Could not read object %s"), |
f2fd0760 | 607 | oid_to_hex(&t->tagged->oid)); |
d2dadfe8 | 608 | objects[i].item = o; |
5d7eeee2 JS |
609 | i--; |
610 | break; | |
611 | } | |
612 | case OBJ_TREE: | |
ae03ee64 JK |
613 | if (rev.shown_one) |
614 | putchar('\n'); | |
95235f5b | 615 | fprintf(rev.diffopt.file, "%stree %s%s\n\n", |
8f67f8ae | 616 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), |
5d7eeee2 | 617 | name, |
8f67f8ae | 618 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); |
f0096c06 | 619 | read_tree_recursive((struct tree *)o, "", 0, 0, &match_all, |
95235f5b | 620 | show_tree_object, rev.diffopt.file); |
ae03ee64 | 621 | rev.shown_one = 1; |
5d7eeee2 JS |
622 | break; |
623 | case OBJ_COMMIT: | |
624 | rev.pending.nr = rev.pending.alloc = 0; | |
625 | rev.pending.objects = NULL; | |
626 | add_object_array(o, name, &rev.pending); | |
627 | ret = cmd_log_walk(&rev); | |
628 | break; | |
629 | default: | |
5c5f1d7c | 630 | ret = error(_("Unknown type: %d"), o->type); |
5d7eeee2 JS |
631 | } |
632 | } | |
633 | free(objects); | |
634 | return ret; | |
70827b15 LT |
635 | } |
636 | ||
cf39f54e LT |
637 | /* |
638 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
639 | */ | |
640 | int cmd_log_reflog(int argc, const char **argv, const char *prefix) | |
641 | { | |
642 | struct rev_info rev; | |
32962c9b | 643 | struct setup_revision_opt opt; |
cf39f54e | 644 | |
9501d191 | 645 | init_log_defaults(); |
ef90d6d4 | 646 | git_config(git_log_config, NULL); |
6b2f2d98 | 647 | |
cf39f54e LT |
648 | init_revisions(&rev, prefix); |
649 | init_reflog_walk(&rev.reflog_info); | |
cf39f54e | 650 | rev.verbose_header = 1; |
32962c9b JH |
651 | memset(&opt, 0, sizeof(opt)); |
652 | opt.def = "HEAD"; | |
4b56cf58 | 653 | cmd_log_init_defaults(&rev); |
0c47695a | 654 | rev.abbrev_commit = 1; |
cf39f54e | 655 | rev.commit_format = CMIT_FMT_ONELINE; |
4da45bef | 656 | rev.use_terminator = 1; |
cf39f54e | 657 | rev.always_show_header = 1; |
4b56cf58 | 658 | cmd_log_init_finish(argc, argv, prefix, &rev, &opt); |
cf39f54e | 659 | |
cf39f54e LT |
660 | return cmd_log_walk(&rev); |
661 | } | |
662 | ||
b130c706 JH |
663 | static void log_setup_revisions_tweak(struct rev_info *rev, |
664 | struct setup_revision_opt *opt) | |
076c9837 DT |
665 | { |
666 | if (DIFF_OPT_TST(&rev->diffopt, DEFAULT_FOLLOW_RENAMES) && | |
667 | rev->prune_data.nr == 1) | |
668 | DIFF_OPT_SET(&rev->diffopt, FOLLOW_RENAMES); | |
c7eaf8b4 JH |
669 | |
670 | /* Turn --cc/-c into -p --cc/-c when -p was not given */ | |
671 | if (!rev->diffopt.output_format && rev->combine_merges) | |
672 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
82dee416 JH |
673 | |
674 | /* Turn -m on when --cc/-c was given */ | |
675 | if (rev->combine_merges) | |
676 | rev->ignore_merges = 0; | |
076c9837 DT |
677 | } |
678 | ||
a633fca0 | 679 | int cmd_log(int argc, const char **argv, const char *prefix) |
70827b15 LT |
680 | { |
681 | struct rev_info rev; | |
32962c9b | 682 | struct setup_revision_opt opt; |
70827b15 | 683 | |
9501d191 | 684 | init_log_defaults(); |
ef90d6d4 | 685 | git_config(git_log_config, NULL); |
6b2f2d98 | 686 | |
db6296a5 | 687 | init_revisions(&rev, prefix); |
70827b15 | 688 | rev.always_show_header = 1; |
32962c9b JH |
689 | memset(&opt, 0, sizeof(opt)); |
690 | opt.def = "HEAD"; | |
d5f6b1d7 | 691 | opt.revarg_opt = REVARG_COMMITTISH; |
b130c706 | 692 | opt.tweak = log_setup_revisions_tweak; |
32962c9b | 693 | cmd_log_init(argc, argv, prefix, &rev, &opt); |
9dafea26 | 694 | return cmd_log_walk(&rev); |
70827b15 | 695 | } |
91efcf60 | 696 | |
c06d2daa | 697 | /* format-patch */ |
0377db77 | 698 | |
917a8f89 | 699 | static const char *fmt_patch_suffix = ".patch"; |
49604a4d | 700 | static int numbered = 0; |
a567fdcb | 701 | static int auto_number = 1; |
20ff0680 | 702 | |
0db5260b JW |
703 | static char *default_attach = NULL; |
704 | ||
2721ce21 JK |
705 | static struct string_list extra_hdr = STRING_LIST_INIT_NODUP; |
706 | static struct string_list extra_to = STRING_LIST_INIT_NODUP; | |
707 | static struct string_list extra_cc = STRING_LIST_INIT_NODUP; | |
3ee79d9f DB |
708 | |
709 | static void add_header(const char *value) | |
710 | { | |
ca9e0a1b | 711 | struct string_list_item *item; |
3ee79d9f | 712 | int len = strlen(value); |
c8c4450e | 713 | while (len && value[len - 1] == '\n') |
3ee79d9f | 714 | len--; |
ca9e0a1b | 715 | |
3ee79d9f | 716 | if (!strncasecmp(value, "to: ", 4)) { |
1d2f80fa | 717 | item = string_list_append(&extra_to, value + 4); |
ca9e0a1b SB |
718 | len -= 4; |
719 | } else if (!strncasecmp(value, "cc: ", 4)) { | |
1d2f80fa | 720 | item = string_list_append(&extra_cc, value + 4); |
ca9e0a1b SB |
721 | len -= 4; |
722 | } else { | |
1d2f80fa | 723 | item = string_list_append(&extra_hdr, value); |
3ee79d9f | 724 | } |
ca9e0a1b SB |
725 | |
726 | item->string[len] = '\0'; | |
3ee79d9f DB |
727 | } |
728 | ||
30984ed2 TR |
729 | #define THREAD_SHALLOW 1 |
730 | #define THREAD_DEEP 2 | |
6622d9c7 SB |
731 | static int thread; |
732 | static int do_signoff; | |
bb52995f | 733 | static int base_auto; |
6bc6b6c0 | 734 | static char *from; |
6622d9c7 | 735 | static const char *signature = git_version_string; |
7022650f | 736 | static const char *signature_file; |
2a4c2607 | 737 | static int config_cover_letter; |
bc6bf2d7 | 738 | static const char *config_output_directory; |
2a4c2607 FC |
739 | |
740 | enum { | |
741 | COVER_UNSET, | |
742 | COVER_OFF, | |
743 | COVER_ON, | |
744 | COVER_AUTO | |
745 | }; | |
30984ed2 | 746 | |
ef90d6d4 | 747 | static int git_format_config(const char *var, const char *value, void *cb) |
20ff0680 JS |
748 | { |
749 | if (!strcmp(var, "format.headers")) { | |
d7fb91c6 | 750 | if (!value) |
5c5f1d7c | 751 | die(_("format.headers without value")); |
3ee79d9f | 752 | add_header(value); |
20ff0680 JS |
753 | return 0; |
754 | } | |
70cff3ac BH |
755 | if (!strcmp(var, "format.suffix")) |
756 | return git_config_string(&fmt_patch_suffix, var, value); | |
ae6c098f SD |
757 | if (!strcmp(var, "format.to")) { |
758 | if (!value) | |
759 | return config_error_nonbool(var); | |
1d2f80fa | 760 | string_list_append(&extra_to, value); |
ae6c098f SD |
761 | return 0; |
762 | } | |
fe8928e6 MV |
763 | if (!strcmp(var, "format.cc")) { |
764 | if (!value) | |
765 | return config_error_nonbool(var); | |
1d2f80fa | 766 | string_list_append(&extra_cc, value); |
fe8928e6 MV |
767 | return 0; |
768 | } | |
787570c7 | 769 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") || |
339de508 | 770 | !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) { |
f3aafa4d RA |
771 | return 0; |
772 | } | |
49604a4d | 773 | if (!strcmp(var, "format.numbered")) { |
90f5c186 | 774 | if (value && !strcasecmp(value, "auto")) { |
49604a4d BG |
775 | auto_number = 1; |
776 | return 0; | |
777 | } | |
49604a4d | 778 | numbered = git_config_bool(var, value); |
a567fdcb | 779 | auto_number = auto_number && numbered; |
49604a4d BG |
780 | return 0; |
781 | } | |
0db5260b JW |
782 | if (!strcmp(var, "format.attach")) { |
783 | if (value && *value) | |
784 | default_attach = xstrdup(value); | |
785 | else | |
786 | default_attach = xstrdup(git_version_string); | |
787 | return 0; | |
788 | } | |
30984ed2 TR |
789 | if (!strcmp(var, "format.thread")) { |
790 | if (value && !strcasecmp(value, "deep")) { | |
791 | thread = THREAD_DEEP; | |
792 | return 0; | |
793 | } | |
794 | if (value && !strcasecmp(value, "shallow")) { | |
795 | thread = THREAD_SHALLOW; | |
796 | return 0; | |
797 | } | |
798 | thread = git_config_bool(var, value) && THREAD_SHALLOW; | |
799 | return 0; | |
800 | } | |
1d1876e9 HV |
801 | if (!strcmp(var, "format.signoff")) { |
802 | do_signoff = git_config_bool(var, value); | |
803 | return 0; | |
804 | } | |
6622d9c7 SB |
805 | if (!strcmp(var, "format.signature")) |
806 | return git_config_string(&signature, var, value); | |
7022650f JM |
807 | if (!strcmp(var, "format.signaturefile")) |
808 | return git_config_pathname(&signature_file, var, value); | |
2a4c2607 FC |
809 | if (!strcmp(var, "format.coverletter")) { |
810 | if (value && !strcasecmp(value, "auto")) { | |
811 | config_cover_letter = COVER_AUTO; | |
812 | return 0; | |
813 | } | |
814 | config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF; | |
815 | return 0; | |
816 | } | |
bc6bf2d7 AK |
817 | if (!strcmp(var, "format.outputdirectory")) |
818 | return git_config_string(&config_output_directory, var, value); | |
bb52995f XY |
819 | if (!strcmp(var, "format.useautobase")) { |
820 | base_auto = git_config_bool(var, value); | |
821 | return 0; | |
822 | } | |
6bc6b6c0 JT |
823 | if (!strcmp(var, "format.from")) { |
824 | int b = git_config_maybe_bool(var, value); | |
825 | free(from); | |
826 | if (b < 0) | |
827 | from = xstrdup(value); | |
828 | else if (b) | |
829 | from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
830 | else | |
831 | from = NULL; | |
832 | return 0; | |
833 | } | |
dbd21447 | 834 | |
ef90d6d4 | 835 | return git_log_config(var, value, cb); |
20ff0680 JS |
836 | } |
837 | ||
efd02016 | 838 | static const char *output_directory = NULL; |
9800a754 | 839 | static int outdir_offset; |
81f3a188 | 840 | |
95235f5b | 841 | static int open_next_file(struct commit *commit, const char *subject, |
a21c2f94 | 842 | struct rev_info *rev, int quiet) |
0377db77 | 843 | { |
cd2ef591 | 844 | struct strbuf filename = STRBUF_INIT; |
68cb7b6f | 845 | int suffix_len = strlen(rev->patch_suffix) + 1; |
0377db77 | 846 | |
2448482b | 847 | if (output_directory) { |
cd2ef591 SB |
848 | strbuf_addstr(&filename, output_directory); |
849 | if (filename.len >= | |
15d980a7 NTND |
850 | PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) { |
851 | strbuf_release(&filename); | |
5c5f1d7c | 852 | return error(_("name of output directory is too long")); |
15d980a7 | 853 | } |
00b6c178 | 854 | strbuf_complete(&filename, '/'); |
2448482b | 855 | } |
0377db77 | 856 | |
38ec23ac JH |
857 | if (rev->numbered_files) |
858 | strbuf_addf(&filename, "%d", rev->nr); | |
d28b5d47 JH |
859 | else if (commit) |
860 | fmt_output_commit(&filename, commit, rev); | |
38ec23ac | 861 | else |
d28b5d47 | 862 | fmt_output_subject(&filename, subject, rev); |
e6ff0f42 | 863 | |
250087f2 | 864 | if (!quiet) |
36a4d905 | 865 | printf("%s\n", filename.buf + outdir_offset); |
ec2956df | 866 | |
15d980a7 NTND |
867 | if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) { |
868 | error_errno(_("Cannot open patch file %s"), filename.buf); | |
869 | strbuf_release(&filename); | |
870 | return -1; | |
871 | } | |
c06d2daa | 872 | |
cd2ef591 | 873 | strbuf_release(&filename); |
e6ff0f42 | 874 | return 0; |
0377db77 JS |
875 | } |
876 | ||
811929f1 | 877 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids) |
9c6efa36 JS |
878 | { |
879 | struct rev_info check_rev; | |
9b7a61d7 | 880 | struct commit *commit, *c1, *c2; |
9c6efa36 JS |
881 | struct object *o1, *o2; |
882 | unsigned flags1, flags2; | |
9c6efa36 JS |
883 | |
884 | if (rev->pending.nr != 2) | |
5c5f1d7c | 885 | die(_("Need exactly one range.")); |
9c6efa36 JS |
886 | |
887 | o1 = rev->pending.objects[0].item; | |
9c6efa36 | 888 | o2 = rev->pending.objects[1].item; |
9b7a61d7 | 889 | flags1 = o1->flags; |
9c6efa36 | 890 | flags2 = o2->flags; |
bc83266a | 891 | c1 = lookup_commit_reference(&o1->oid); |
892 | c2 = lookup_commit_reference(&o2->oid); | |
9c6efa36 JS |
893 | |
894 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
5c5f1d7c | 895 | die(_("Not a range.")); |
9c6efa36 | 896 | |
5d23e133 | 897 | init_patch_ids(ids); |
9c6efa36 JS |
898 | |
899 | /* given a range a..b get all patch ids for b..a */ | |
811929f1 | 900 | init_revisions(&check_rev, rev->prefix); |
51f4de3a | 901 | check_rev.max_parents = 1; |
9c6efa36 JS |
902 | o1->flags ^= UNINTERESTING; |
903 | o2->flags ^= UNINTERESTING; | |
904 | add_pending_object(&check_rev, o1, "o1"); | |
905 | add_pending_object(&check_rev, o2, "o2"); | |
3d51e1b5 | 906 | if (prepare_revision_walk(&check_rev)) |
5c5f1d7c | 907 | die(_("revision walk setup failed")); |
9c6efa36 JS |
908 | |
909 | while ((commit = get_revision(&check_rev)) != NULL) { | |
5d23e133 | 910 | add_commit_patch_id(commit, ids); |
9c6efa36 JS |
911 | } |
912 | ||
913 | /* reset for next revision walk */ | |
9b7a61d7 JH |
914 | clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED); |
915 | clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED); | |
9c6efa36 JS |
916 | o1->flags = flags1; |
917 | o2->flags = flags2; | |
918 | } | |
919 | ||
e1a37346 | 920 | static void gen_message_id(struct rev_info *info, char *base) |
d1566f78 | 921 | { |
f285a2d7 | 922 | struct strbuf buf = STRBUF_INIT; |
cb71f8bd | 923 | strbuf_addf(&buf, "%s.%"PRItime".git.%s", base, |
dddbad72 | 924 | (timestamp_t) time(NULL), |
59f9b8a9 | 925 | git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT)); |
e1a37346 | 926 | info->message_id = strbuf_detach(&buf, NULL); |
d1566f78 JT |
927 | } |
928 | ||
95235f5b | 929 | static void print_signature(FILE *file) |
6622d9c7 | 930 | { |
c6076e2b JK |
931 | if (!signature || !*signature) |
932 | return; | |
933 | ||
95235f5b | 934 | fprintf(file, "-- \n%s", signature); |
c6076e2b | 935 | if (signature[strlen(signature)-1] != '\n') |
95235f5b JS |
936 | putc('\n', file); |
937 | putc('\n', file); | |
6622d9c7 SB |
938 | } |
939 | ||
739453a3 JH |
940 | static void add_branch_description(struct strbuf *buf, const char *branch_name) |
941 | { | |
942 | struct strbuf desc = STRBUF_INIT; | |
943 | if (!branch_name || !*branch_name) | |
944 | return; | |
945 | read_branch_desc(&desc, branch_name); | |
946 | if (desc.len) { | |
947 | strbuf_addch(buf, '\n'); | |
e992d1eb | 948 | strbuf_addbuf(buf, &desc); |
739453a3 JH |
949 | strbuf_addch(buf, '\n'); |
950 | } | |
94204bf5 | 951 | strbuf_release(&desc); |
739453a3 JH |
952 | } |
953 | ||
d7ddad01 FC |
954 | static char *find_branch_name(struct rev_info *rev) |
955 | { | |
956 | int i, positive = -1; | |
f2fd0760 | 957 | struct object_id branch_oid; |
958 | const struct object_id *tip_oid; | |
95b567c7 | 959 | const char *ref, *v; |
d7ddad01 FC |
960 | char *full_ref, *branch = NULL; |
961 | ||
962 | for (i = 0; i < rev->cmdline.nr; i++) { | |
963 | if (rev->cmdline.rev[i].flags & UNINTERESTING) | |
964 | continue; | |
965 | if (positive < 0) | |
966 | positive = i; | |
967 | else | |
968 | return NULL; | |
969 | } | |
970 | if (positive < 0) | |
971 | return NULL; | |
972 | ref = rev->cmdline.rev[positive].name; | |
f2fd0760 | 973 | tip_oid = &rev->cmdline.rev[positive].item->oid; |
974 | if (dwim_ref(ref, strlen(ref), branch_oid.hash, &full_ref) && | |
95b567c7 | 975 | skip_prefix(full_ref, "refs/heads/", &v) && |
f2fd0760 | 976 | !oidcmp(tip_oid, &branch_oid)) |
95b567c7 | 977 | branch = xstrdup(v); |
d7ddad01 FC |
978 | free(full_ref); |
979 | return branch; | |
980 | } | |
981 | ||
2bda2cf4 | 982 | static void make_cover_letter(struct rev_info *rev, int use_stdout, |
2bda2cf4 | 983 | struct commit *origin, |
d7ddad01 | 984 | int nr, struct commit **list, |
739453a3 | 985 | const char *branch_name, |
250087f2 | 986 | int quiet) |
a5a27c79 DB |
987 | { |
988 | const char *committer; | |
a5a27c79 DB |
989 | const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; |
990 | const char *msg; | |
2bda2cf4 | 991 | struct shortlog log; |
f285a2d7 | 992 | struct strbuf sb = STRBUF_INIT; |
2bda2cf4 | 993 | int i; |
330db18c | 994 | const char *encoding = "UTF-8"; |
39fe578b | 995 | struct diff_options opts; |
267123b4 | 996 | int need_8bit_cte = 0; |
6bf13944 | 997 | struct pretty_print_context pp = {0}; |
d7ddad01 | 998 | struct commit *head = list[0]; |
a5a27c79 | 999 | |
9f23e040 | 1000 | if (!cmit_fmt_is_mail(rev->commit_format)) |
5c5f1d7c | 1001 | die(_("Cover letter needs email format")); |
a5a27c79 | 1002 | |
cd2ef591 | 1003 | committer = git_committer_info(0); |
6df514af | 1004 | |
a21c2f94 | 1005 | if (!use_stdout && |
95235f5b | 1006 | open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet)) |
a5a27c79 DB |
1007 | return; |
1008 | ||
6d167fd7 | 1009 | log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte); |
a5a27c79 | 1010 | |
bc6b8fc1 | 1011 | for (i = 0; !need_8bit_cte && i < nr; i++) { |
8597ea3a | 1012 | const char *buf = get_commit_buffer(list[i], NULL); |
bc6b8fc1 | 1013 | if (has_non_ascii(buf)) |
0a7f4483 | 1014 | need_8bit_cte = 1; |
bc6b8fc1 JK |
1015 | unuse_commit_buffer(list[i], buf); |
1016 | } | |
0a7f4483 | 1017 | |
d7ddad01 FC |
1018 | if (!branch_name) |
1019 | branch_name = find_branch_name(rev); | |
1020 | ||
a5a27c79 | 1021 | msg = body; |
6bf13944 | 1022 | pp.fmt = CMIT_FMT_EMAIL; |
a5481a6c | 1023 | pp.date_mode.type = DATE_RFC2822; |
6d167fd7 RS |
1024 | pp.rev = rev; |
1025 | pp.print_email_subject = 1; | |
6bf13944 JK |
1026 | pp_user_info(&pp, NULL, &sb, committer, encoding); |
1027 | pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte); | |
1028 | pp_remainder(&pp, &msg, &sb, 0); | |
739453a3 | 1029 | add_branch_description(&sb, branch_name); |
95235f5b | 1030 | fprintf(rev->diffopt.file, "%s\n", sb.buf); |
a5a27c79 DB |
1031 | |
1032 | strbuf_release(&sb); | |
1033 | ||
2bda2cf4 | 1034 | shortlog_init(&log); |
859c4fbe JS |
1035 | log.wrap_lines = 1; |
1036 | log.wrap = 72; | |
1037 | log.in1 = 2; | |
1038 | log.in2 = 4; | |
95235f5b | 1039 | log.file = rev->diffopt.file; |
2bda2cf4 DB |
1040 | for (i = 0; i < nr; i++) |
1041 | shortlog_add_commit(&log, list[i]); | |
1042 | ||
1043 | shortlog_output(&log); | |
1044 | ||
a5a27c79 | 1045 | /* |
2bda2cf4 | 1046 | * We can only do diffstat with a unique reference point |
a5a27c79 DB |
1047 | */ |
1048 | if (!origin) | |
1049 | return; | |
1050 | ||
5d02294c JS |
1051 | memcpy(&opts, &rev->diffopt, sizeof(opts)); |
1052 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
a5a27c79 | 1053 | |
39fe578b DB |
1054 | diff_setup_done(&opts); |
1055 | ||
66f414f8 BW |
1056 | diff_tree_oid(&origin->tree->object.oid, |
1057 | &head->tree->object.oid, | |
1058 | "", &opts); | |
39fe578b DB |
1059 | diffcore_std(&opts); |
1060 | diff_flush(&opts); | |
a5a27c79 | 1061 | |
95235f5b | 1062 | fprintf(rev->diffopt.file, "\n"); |
d1566f78 JT |
1063 | } |
1064 | ||
8419d2ee JH |
1065 | static const char *clean_message_id(const char *msg_id) |
1066 | { | |
1067 | char ch; | |
1068 | const char *a, *z, *m; | |
8419d2ee JH |
1069 | |
1070 | m = msg_id; | |
1071 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
1072 | m++; | |
1073 | a = m; | |
1074 | z = NULL; | |
1075 | while ((ch = *m)) { | |
1076 | if (!isspace(ch) && (ch != '>')) | |
1077 | z = m; | |
1078 | m++; | |
1079 | } | |
1080 | if (!z) | |
5c5f1d7c | 1081 | die(_("insane in-reply-to: %s"), msg_id); |
8419d2ee JH |
1082 | if (++z == m) |
1083 | return a; | |
182af834 | 1084 | return xmemdupz(a, z - a); |
8419d2ee JH |
1085 | } |
1086 | ||
9800a754 JH |
1087 | static const char *set_outdir(const char *prefix, const char *output_directory) |
1088 | { | |
1089 | if (output_directory && is_absolute_path(output_directory)) | |
1090 | return output_directory; | |
1091 | ||
1092 | if (!prefix || !*prefix) { | |
1093 | if (output_directory) | |
1094 | return output_directory; | |
1095 | /* The user did not explicitly ask for "./" */ | |
1096 | outdir_offset = 2; | |
1097 | return "./"; | |
1098 | } | |
1099 | ||
1100 | outdir_offset = strlen(prefix); | |
1101 | if (!output_directory) | |
1102 | return prefix; | |
1103 | ||
e4da43b1 | 1104 | return prefix_filename(prefix, output_directory); |
9800a754 JH |
1105 | } |
1106 | ||
fff02ee6 | 1107 | static const char * const builtin_format_patch_usage[] = { |
9c9b4f2f | 1108 | N_("git format-patch [<options>] [<since> | <revision-range>]"), |
fff02ee6 SB |
1109 | NULL |
1110 | }; | |
1111 | ||
1112 | static int keep_subject = 0; | |
1113 | ||
1114 | static int keep_callback(const struct option *opt, const char *arg, int unset) | |
1115 | { | |
1116 | ((struct rev_info *)opt->value)->total = -1; | |
1117 | keep_subject = 1; | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | static int subject_prefix = 0; | |
1122 | ||
1123 | static int subject_prefix_callback(const struct option *opt, const char *arg, | |
1124 | int unset) | |
1125 | { | |
1126 | subject_prefix = 1; | |
1127 | ((struct rev_info *)opt->value)->subject_prefix = arg; | |
1128 | return 0; | |
1129 | } | |
1130 | ||
68e83a5b JT |
1131 | static int rfc_callback(const struct option *opt, const char *arg, int unset) |
1132 | { | |
1133 | return subject_prefix_callback(opt, "RFC PATCH", unset); | |
1134 | } | |
1135 | ||
1af4731b JH |
1136 | static int numbered_cmdline_opt = 0; |
1137 | ||
fff02ee6 SB |
1138 | static int numbered_callback(const struct option *opt, const char *arg, |
1139 | int unset) | |
1140 | { | |
1af4731b | 1141 | *(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1; |
fff02ee6 SB |
1142 | if (unset) |
1143 | auto_number = 0; | |
1144 | return 0; | |
1145 | } | |
1146 | ||
1147 | static int no_numbered_callback(const struct option *opt, const char *arg, | |
1148 | int unset) | |
1149 | { | |
1150 | return numbered_callback(opt, arg, 1); | |
1151 | } | |
1152 | ||
1153 | static int output_directory_callback(const struct option *opt, const char *arg, | |
1154 | int unset) | |
1155 | { | |
1156 | const char **dir = (const char **)opt->value; | |
1157 | if (*dir) | |
5c5f1d7c | 1158 | die(_("Two output directories?")); |
fff02ee6 SB |
1159 | *dir = arg; |
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | static int thread_callback(const struct option *opt, const char *arg, int unset) | |
1164 | { | |
1165 | int *thread = (int *)opt->value; | |
1166 | if (unset) | |
1167 | *thread = 0; | |
1168 | else if (!arg || !strcmp(arg, "shallow")) | |
1169 | *thread = THREAD_SHALLOW; | |
1170 | else if (!strcmp(arg, "deep")) | |
1171 | *thread = THREAD_DEEP; | |
1172 | else | |
1173 | return 1; | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static int attach_callback(const struct option *opt, const char *arg, int unset) | |
1178 | { | |
1179 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1180 | if (unset) | |
1181 | rev->mime_boundary = NULL; | |
1182 | else if (arg) | |
1183 | rev->mime_boundary = arg; | |
1184 | else | |
1185 | rev->mime_boundary = git_version_string; | |
1186 | rev->no_inline = unset ? 0 : 1; | |
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | static int inline_callback(const struct option *opt, const char *arg, int unset) | |
1191 | { | |
1192 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1193 | if (unset) | |
1194 | rev->mime_boundary = NULL; | |
1195 | else if (arg) | |
1196 | rev->mime_boundary = arg; | |
1197 | else | |
1198 | rev->mime_boundary = git_version_string; | |
1199 | rev->no_inline = 0; | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | static int header_callback(const struct option *opt, const char *arg, int unset) | |
1204 | { | |
c4260034 SB |
1205 | if (unset) { |
1206 | string_list_clear(&extra_hdr, 0); | |
1207 | string_list_clear(&extra_to, 0); | |
1208 | string_list_clear(&extra_cc, 0); | |
1209 | } else { | |
1210 | add_header(arg); | |
1211 | } | |
fff02ee6 SB |
1212 | return 0; |
1213 | } | |
1214 | ||
ae6c098f SD |
1215 | static int to_callback(const struct option *opt, const char *arg, int unset) |
1216 | { | |
c4260034 SB |
1217 | if (unset) |
1218 | string_list_clear(&extra_to, 0); | |
1219 | else | |
1d2f80fa | 1220 | string_list_append(&extra_to, arg); |
fff02ee6 SB |
1221 | return 0; |
1222 | } | |
1223 | ||
1224 | static int cc_callback(const struct option *opt, const char *arg, int unset) | |
1225 | { | |
c4260034 SB |
1226 | if (unset) |
1227 | string_list_clear(&extra_cc, 0); | |
1228 | else | |
1d2f80fa | 1229 | string_list_append(&extra_cc, arg); |
fff02ee6 SB |
1230 | return 0; |
1231 | } | |
1232 | ||
a9080475 JK |
1233 | static int from_callback(const struct option *opt, const char *arg, int unset) |
1234 | { | |
1235 | char **from = opt->value; | |
1236 | ||
1237 | free(*from); | |
1238 | ||
1239 | if (unset) | |
1240 | *from = NULL; | |
1241 | else if (arg) | |
1242 | *from = xstrdup(arg); | |
1243 | else | |
1244 | *from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
1245 | return 0; | |
1246 | } | |
1247 | ||
fa2ab86d XY |
1248 | struct base_tree_info { |
1249 | struct object_id base_commit; | |
1250 | int nr_patch_id, alloc_patch_id; | |
1251 | struct object_id *patch_id; | |
1252 | }; | |
1253 | ||
1254 | static struct commit *get_base_commit(const char *base_commit, | |
1255 | struct commit **list, | |
1256 | int total) | |
1257 | { | |
1258 | struct commit *base = NULL; | |
1259 | struct commit **rev; | |
1260 | int i = 0, rev_nr = 0; | |
1261 | ||
bb52995f XY |
1262 | if (base_commit && strcmp(base_commit, "auto")) { |
1263 | base = lookup_commit_reference_by_name(base_commit); | |
1264 | if (!base) | |
1265 | die(_("Unknown commit %s"), base_commit); | |
1266 | } else if ((base_commit && !strcmp(base_commit, "auto")) || base_auto) { | |
3de66517 XY |
1267 | struct branch *curr_branch = branch_get(NULL); |
1268 | const char *upstream = branch_get_upstream(curr_branch, NULL); | |
1269 | if (upstream) { | |
1270 | struct commit_list *base_list; | |
1271 | struct commit *commit; | |
d801627b | 1272 | struct object_id oid; |
3de66517 | 1273 | |
d801627b | 1274 | if (get_oid(upstream, &oid)) |
3de66517 | 1275 | die(_("Failed to resolve '%s' as a valid ref."), upstream); |
bc83266a | 1276 | commit = lookup_commit_or_die(&oid, "upstream base"); |
3de66517 XY |
1277 | base_list = get_merge_bases_many(commit, total, list); |
1278 | /* There should be one and only one merge base. */ | |
1279 | if (!base_list || base_list->next) | |
1280 | die(_("Could not find exact merge base.")); | |
1281 | base = base_list->item; | |
1282 | free_commit_list(base_list); | |
1283 | } else { | |
1284 | die(_("Failed to get upstream, if you want to record base commit automatically,\n" | |
1285 | "please use git branch --set-upstream-to to track a remote branch.\n" | |
1286 | "Or you could specify base commit by --base=<base-commit-id> manually.")); | |
1287 | } | |
3de66517 | 1288 | } |
fa2ab86d XY |
1289 | |
1290 | ALLOC_ARRAY(rev, total); | |
1291 | for (i = 0; i < total; i++) | |
1292 | rev[i] = list[i]; | |
1293 | ||
1294 | rev_nr = total; | |
1295 | /* | |
1296 | * Get merge base through pair-wise computations | |
1297 | * and store it in rev[0]. | |
1298 | */ | |
1299 | while (rev_nr > 1) { | |
1300 | for (i = 0; i < rev_nr / 2; i++) { | |
1301 | struct commit_list *merge_base; | |
1302 | merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]); | |
1303 | if (!merge_base || merge_base->next) | |
1304 | die(_("Failed to find exact merge base")); | |
1305 | ||
1306 | rev[i] = merge_base->item; | |
1307 | } | |
1308 | ||
1309 | if (rev_nr % 2) | |
1310 | rev[i] = rev[2 * i]; | |
42c78a21 | 1311 | rev_nr = DIV_ROUND_UP(rev_nr, 2); |
fa2ab86d XY |
1312 | } |
1313 | ||
1314 | if (!in_merge_bases(base, rev[0])) | |
1315 | die(_("base commit should be the ancestor of revision list")); | |
1316 | ||
1317 | for (i = 0; i < total; i++) { | |
1318 | if (base == list[i]) | |
1319 | die(_("base commit shouldn't be in revision list")); | |
1320 | } | |
1321 | ||
1322 | free(rev); | |
1323 | return base; | |
1324 | } | |
1325 | ||
1326 | static void prepare_bases(struct base_tree_info *bases, | |
1327 | struct commit *base, | |
1328 | struct commit **list, | |
1329 | int total) | |
1330 | { | |
1331 | struct commit *commit; | |
1332 | struct rev_info revs; | |
1333 | struct diff_options diffopt; | |
1334 | int i; | |
1335 | ||
1336 | if (!base) | |
1337 | return; | |
1338 | ||
1339 | diff_setup(&diffopt); | |
1340 | DIFF_OPT_SET(&diffopt, RECURSIVE); | |
1341 | diff_setup_done(&diffopt); | |
1342 | ||
1343 | oidcpy(&bases->base_commit, &base->object.oid); | |
1344 | ||
1345 | init_revisions(&revs, NULL); | |
1346 | revs.max_parents = 1; | |
1347 | revs.topo_order = 1; | |
1348 | for (i = 0; i < total; i++) { | |
1349 | list[i]->object.flags &= ~UNINTERESTING; | |
1350 | add_pending_object(&revs, &list[i]->object, "rev_list"); | |
1351 | list[i]->util = (void *)1; | |
1352 | } | |
1353 | base->object.flags |= UNINTERESTING; | |
1354 | add_pending_object(&revs, &base->object, "base"); | |
1355 | ||
1356 | if (prepare_revision_walk(&revs)) | |
1357 | die(_("revision walk setup failed")); | |
1358 | /* | |
1359 | * Traverse the commits list, get prerequisite patch ids | |
1360 | * and stuff them in bases structure. | |
1361 | */ | |
1362 | while ((commit = get_revision(&revs)) != NULL) { | |
d801627b | 1363 | struct object_id oid; |
fa2ab86d XY |
1364 | struct object_id *patch_id; |
1365 | if (commit->util) | |
1366 | continue; | |
34f3c0eb | 1367 | if (commit_patch_id(commit, &diffopt, &oid, 0)) |
fa2ab86d XY |
1368 | die(_("cannot get patch id")); |
1369 | ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id); | |
1370 | patch_id = bases->patch_id + bases->nr_patch_id; | |
d801627b | 1371 | oidcpy(patch_id, &oid); |
fa2ab86d XY |
1372 | bases->nr_patch_id++; |
1373 | } | |
1374 | } | |
1375 | ||
95235f5b | 1376 | static void print_bases(struct base_tree_info *bases, FILE *file) |
fa2ab86d XY |
1377 | { |
1378 | int i; | |
1379 | ||
1380 | /* Only do this once, either for the cover or for the first one */ | |
1381 | if (is_null_oid(&bases->base_commit)) | |
1382 | return; | |
1383 | ||
1384 | /* Show the base commit */ | |
480871e0 | 1385 | fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit)); |
fa2ab86d XY |
1386 | |
1387 | /* Show the prerequisite patches */ | |
1388 | for (i = bases->nr_patch_id - 1; i >= 0; i--) | |
95235f5b | 1389 | fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i])); |
fa2ab86d XY |
1390 | |
1391 | free(bases->patch_id); | |
1392 | bases->nr_patch_id = 0; | |
1393 | bases->alloc_patch_id = 0; | |
1394 | oidclr(&bases->base_commit); | |
1395 | } | |
1396 | ||
a633fca0 | 1397 | int cmd_format_patch(int argc, const char **argv, const char *prefix) |
91efcf60 JH |
1398 | { |
1399 | struct commit *commit; | |
1400 | struct commit **list = NULL; | |
1401 | struct rev_info rev; | |
32962c9b | 1402 | struct setup_revision_opt s_r_opt; |
fff02ee6 | 1403 | int nr = 0, total, i; |
0377db77 | 1404 | int use_stdout = 0; |
fa0f02df | 1405 | int start_number = -1; |
ce37596c | 1406 | int just_numbers = 0; |
9c6efa36 | 1407 | int ignore_if_in_upstream = 0; |
2a4c2607 | 1408 | int cover_letter = -1; |
2bda2cf4 | 1409 | int boundary_count = 0; |
37c22a4b | 1410 | int no_binary_diff = 0; |
3a30aa17 | 1411 | int zero_commit = 0; |
d7ddad01 | 1412 | struct commit *origin = NULL; |
76af0734 | 1413 | const char *in_reply_to = NULL; |
5d23e133 | 1414 | struct patch_ids ids; |
f285a2d7 | 1415 | struct strbuf buf = STRBUF_INIT; |
1d46f2ea | 1416 | int use_patch_format = 0; |
250087f2 | 1417 | int quiet = 0; |
5fe10fe8 | 1418 | int reroll_count = -1; |
739453a3 | 1419 | char *branch_name = NULL; |
fa2ab86d XY |
1420 | char *base_commit = NULL; |
1421 | struct base_tree_info bases; | |
1422 | ||
fff02ee6 SB |
1423 | const struct option builtin_format_patch_options[] = { |
1424 | { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL, | |
61007a58 | 1425 | N_("use [PATCH n/m] even with a single patch"), |
fff02ee6 SB |
1426 | PARSE_OPT_NOARG, numbered_callback }, |
1427 | { OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL, | |
61007a58 | 1428 | N_("use [PATCH] even with multiple patches"), |
fff02ee6 | 1429 | PARSE_OPT_NOARG, no_numbered_callback }, |
aa089cd9 FC |
1430 | OPT_BOOL('s', "signoff", &do_signoff, N_("add Signed-off-by:")), |
1431 | OPT_BOOL(0, "stdout", &use_stdout, | |
61007a58 | 1432 | N_("print patches to standard out")), |
aa089cd9 | 1433 | OPT_BOOL(0, "cover-letter", &cover_letter, |
61007a58 | 1434 | N_("generate a cover letter")), |
aa089cd9 | 1435 | OPT_BOOL(0, "numbered-files", &just_numbers, |
61007a58 NTND |
1436 | N_("use simple number sequence for output file names")), |
1437 | OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"), | |
1438 | N_("use <sfx> instead of '.patch'")), | |
fff02ee6 | 1439 | OPT_INTEGER(0, "start-number", &start_number, |
61007a58 | 1440 | N_("start numbering patches at <n> instead of 1")), |
7952ea66 | 1441 | OPT_INTEGER('v', "reroll-count", &reroll_count, |
5fe10fe8 | 1442 | N_("mark the series as Nth re-roll")), |
68e83a5b JT |
1443 | { OPTION_CALLBACK, 0, "rfc", &rev, NULL, |
1444 | N_("Use [RFC PATCH] instead of [PATCH]"), | |
1445 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback }, | |
61007a58 NTND |
1446 | { OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"), |
1447 | N_("Use [<prefix>] instead of [PATCH]"), | |
fff02ee6 SB |
1448 | PARSE_OPT_NONEG, subject_prefix_callback }, |
1449 | { OPTION_CALLBACK, 'o', "output-directory", &output_directory, | |
61007a58 | 1450 | N_("dir"), N_("store resulting files in <dir>"), |
fff02ee6 SB |
1451 | PARSE_OPT_NONEG, output_directory_callback }, |
1452 | { OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL, | |
61007a58 | 1453 | N_("don't strip/add [PATCH]"), |
fff02ee6 | 1454 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback }, |
d5d09d47 SB |
1455 | OPT_BOOL(0, "no-binary", &no_binary_diff, |
1456 | N_("don't output binary diffs")), | |
3a30aa17 | 1457 | OPT_BOOL(0, "zero-commit", &zero_commit, |
1458 | N_("output all-zero hash in From header")), | |
d5d09d47 SB |
1459 | OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream, |
1460 | N_("don't include a patch matching a commit upstream")), | |
4741edd5 | 1461 | { OPTION_SET_INT, 'p', "no-stat", &use_patch_format, NULL, |
61007a58 | 1462 | N_("show patch format instead of default (patch + stat)"), |
4741edd5 | 1463 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1}, |
61007a58 NTND |
1464 | OPT_GROUP(N_("Messaging")), |
1465 | { OPTION_CALLBACK, 0, "add-header", NULL, N_("header"), | |
1466 | N_("add email header"), 0, header_callback }, | |
1467 | { OPTION_CALLBACK, 0, "to", NULL, N_("email"), N_("add To: header"), | |
c4260034 | 1468 | 0, to_callback }, |
61007a58 | 1469 | { OPTION_CALLBACK, 0, "cc", NULL, N_("email"), N_("add Cc: header"), |
c4260034 | 1470 | 0, cc_callback }, |
a9080475 JK |
1471 | { OPTION_CALLBACK, 0, "from", &from, N_("ident"), |
1472 | N_("set From address to <ident> (or committer ident if absent)"), | |
1473 | PARSE_OPT_OPTARG, from_callback }, | |
61007a58 NTND |
1474 | OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"), |
1475 | N_("make first mail a reply to <message-id>")), | |
1476 | { OPTION_CALLBACK, 0, "attach", &rev, N_("boundary"), | |
1477 | N_("attach the patch"), PARSE_OPT_OPTARG, | |
fff02ee6 | 1478 | attach_callback }, |
61007a58 NTND |
1479 | { OPTION_CALLBACK, 0, "inline", &rev, N_("boundary"), |
1480 | N_("inline the patch"), | |
fff02ee6 SB |
1481 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, |
1482 | inline_callback }, | |
61007a58 NTND |
1483 | { OPTION_CALLBACK, 0, "thread", &thread, N_("style"), |
1484 | N_("enable message threading, styles: shallow, deep"), | |
fff02ee6 | 1485 | PARSE_OPT_OPTARG, thread_callback }, |
61007a58 NTND |
1486 | OPT_STRING(0, "signature", &signature, N_("signature"), |
1487 | N_("add a signature")), | |
fa2ab86d XY |
1488 | OPT_STRING(0, "base", &base_commit, N_("base-commit"), |
1489 | N_("add prerequisite tree info to the patch series")), | |
7022650f JM |
1490 | OPT_FILENAME(0, "signature-file", &signature_file, |
1491 | N_("add a signature from a file")), | |
b7df098c | 1492 | OPT__QUIET(&quiet, N_("don't print the patch filenames")), |
fff02ee6 SB |
1493 | OPT_END() |
1494 | }; | |
91efcf60 | 1495 | |
ca9e0a1b SB |
1496 | extra_hdr.strdup_strings = 1; |
1497 | extra_to.strdup_strings = 1; | |
1498 | extra_cc.strdup_strings = 1; | |
9501d191 | 1499 | init_log_defaults(); |
ef90d6d4 | 1500 | git_config(git_format_config, NULL); |
db6296a5 | 1501 | init_revisions(&rev, prefix); |
91efcf60 | 1502 | rev.commit_format = CMIT_FMT_EMAIL; |
0893eec8 | 1503 | rev.expand_tabs_in_log_default = 0; |
91efcf60 JH |
1504 | rev.verbose_header = 1; |
1505 | rev.diff = 1; | |
ad5aeede | 1506 | rev.max_parents = 1; |
8f67f8ae | 1507 | DIFF_OPT_SET(&rev.diffopt, RECURSIVE); |
dbd21447 | 1508 | rev.subject_prefix = fmt_patch_subject_prefix; |
32962c9b JH |
1509 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
1510 | s_r_opt.def = "HEAD"; | |
d5f6b1d7 | 1511 | s_r_opt.revarg_opt = REVARG_COMMITTISH; |
20ff0680 | 1512 | |
0db5260b JW |
1513 | if (default_attach) { |
1514 | rev.mime_boundary = default_attach; | |
1515 | rev.no_inline = 1; | |
1516 | } | |
1517 | ||
2448482b JS |
1518 | /* |
1519 | * Parse the arguments before setup_revisions(), or something | |
2dc189a3 | 1520 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is |
2448482b JS |
1521 | * possibly a valid SHA1. |
1522 | */ | |
37782920 | 1523 | argc = parse_options(argc, argv, prefix, builtin_format_patch_options, |
fff02ee6 | 1524 | builtin_format_patch_usage, |
382da402 FC |
1525 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | |
1526 | PARSE_OPT_KEEP_DASHDASH); | |
2448482b | 1527 | |
5fe10fe8 JH |
1528 | if (0 < reroll_count) { |
1529 | struct strbuf sprefix = STRBUF_INIT; | |
1530 | strbuf_addf(&sprefix, "%s v%d", | |
1531 | rev.subject_prefix, reroll_count); | |
1532 | rev.reroll_count = reroll_count; | |
1533 | rev.subject_prefix = strbuf_detach(&sprefix, NULL); | |
1534 | } | |
1535 | ||
ca9e0a1b SB |
1536 | for (i = 0; i < extra_hdr.nr; i++) { |
1537 | strbuf_addstr(&buf, extra_hdr.items[i].string); | |
3ee79d9f DB |
1538 | strbuf_addch(&buf, '\n'); |
1539 | } | |
1540 | ||
ca9e0a1b | 1541 | if (extra_to.nr) |
3ee79d9f | 1542 | strbuf_addstr(&buf, "To: "); |
ca9e0a1b | 1543 | for (i = 0; i < extra_to.nr; i++) { |
3ee79d9f DB |
1544 | if (i) |
1545 | strbuf_addstr(&buf, " "); | |
ca9e0a1b SB |
1546 | strbuf_addstr(&buf, extra_to.items[i].string); |
1547 | if (i + 1 < extra_to.nr) | |
3ee79d9f DB |
1548 | strbuf_addch(&buf, ','); |
1549 | strbuf_addch(&buf, '\n'); | |
1550 | } | |
1551 | ||
ca9e0a1b | 1552 | if (extra_cc.nr) |
3ee79d9f | 1553 | strbuf_addstr(&buf, "Cc: "); |
ca9e0a1b | 1554 | for (i = 0; i < extra_cc.nr; i++) { |
3ee79d9f DB |
1555 | if (i) |
1556 | strbuf_addstr(&buf, " "); | |
ca9e0a1b SB |
1557 | strbuf_addstr(&buf, extra_cc.items[i].string); |
1558 | if (i + 1 < extra_cc.nr) | |
3ee79d9f DB |
1559 | strbuf_addch(&buf, ','); |
1560 | strbuf_addch(&buf, '\n'); | |
1561 | } | |
1562 | ||
2af202be | 1563 | rev.extra_headers = strbuf_detach(&buf, NULL); |
3ee79d9f | 1564 | |
a9080475 JK |
1565 | if (from) { |
1566 | if (split_ident_line(&rev.from_ident, from, strlen(from))) | |
1567 | die(_("invalid ident line: %s"), from); | |
1568 | } | |
1569 | ||
add5c8a5 | 1570 | if (start_number < 0) |
fa0f02df | 1571 | start_number = 1; |
ca6b91d2 JM |
1572 | |
1573 | /* | |
1574 | * If numbered is set solely due to format.numbered in config, | |
1575 | * and it would conflict with --keep-subject (-k) from the | |
1576 | * command line, reset "numbered". | |
1577 | */ | |
1578 | if (numbered && keep_subject && !numbered_cmdline_opt) | |
1579 | numbered = 0; | |
1580 | ||
63b398a4 | 1581 | if (numbered && keep_subject) |
5c5f1d7c | 1582 | die (_("-n and -k are mutually exclusive.")); |
2d9e4a47 | 1583 | if (keep_subject && subject_prefix) |
68e83a5b | 1584 | die (_("--subject-prefix/--rfc and -k are mutually exclusive.")); |
9553d2b2 | 1585 | rev.preserve_subject = keep_subject; |
8ac80a57 | 1586 | |
32962c9b | 1587 | argc = setup_revisions(argc, argv, &rev, &s_r_opt); |
2448482b | 1588 | if (argc > 1) |
5c5f1d7c | 1589 | die (_("unrecognized argument: %s"), argv[1]); |
0377db77 | 1590 | |
02bc5b03 | 1591 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME) |
f338cb83 | 1592 | die(_("--name-only does not make sense")); |
02bc5b03 | 1593 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) |
f338cb83 | 1594 | die(_("--name-status does not make sense")); |
02bc5b03 | 1595 | if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) |
f338cb83 | 1596 | die(_("--check does not make sense")); |
02bc5b03 BG |
1597 | |
1598 | if (!use_patch_format && | |
1599 | (!rev.diffopt.output_format || | |
1600 | rev.diffopt.output_format == DIFF_FORMAT_PATCH)) | |
1601 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; | |
1602 | ||
1603 | /* Always generate a patch */ | |
1604 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
c9b5ef99 | 1605 | |
3a30aa17 | 1606 | rev.zero_commit = zero_commit; |
1607 | ||
37c22a4b | 1608 | if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff) |
8f67f8ae | 1609 | DIFF_OPT_SET(&rev.diffopt, BINARY); |
e47f306d | 1610 | |
894a9d33 TR |
1611 | if (rev.show_notes) |
1612 | init_display_notes(&rev.notes_opt); | |
1613 | ||
bc6bf2d7 AK |
1614 | if (!output_directory && !use_stdout) |
1615 | output_directory = config_output_directory; | |
1616 | ||
9800a754 JH |
1617 | if (!use_stdout) |
1618 | output_directory = set_outdir(prefix, output_directory); | |
38a94bb6 TRC |
1619 | else |
1620 | setup_pager(); | |
77e565d8 | 1621 | |
efd02016 | 1622 | if (output_directory) { |
11f4eb19 JS |
1623 | if (rev.diffopt.use_color != GIT_COLOR_ALWAYS) |
1624 | rev.diffopt.use_color = GIT_COLOR_NEVER; | |
efd02016 | 1625 | if (use_stdout) |
5c5f1d7c | 1626 | die(_("standard output, or directory, which one?")); |
efd02016 | 1627 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) |
5c5f1d7c | 1628 | die_errno(_("Could not create directory '%s'"), |
0721c314 | 1629 | output_directory); |
efd02016 JH |
1630 | } |
1631 | ||
1f1e895f | 1632 | if (rev.pending.nr == 1) { |
427a8ec5 FC |
1633 | int check_head = 0; |
1634 | ||
8a1d076e JH |
1635 | if (rev.max_count < 0 && !rev.show_root_diff) { |
1636 | /* | |
1637 | * This is traditional behaviour of "git format-patch | |
1638 | * origin" that prepares what the origin side still | |
1639 | * does not have. | |
1640 | */ | |
7c496280 | 1641 | rev.pending.objects[0].item->flags |= UNINTERESTING; |
3384a2df | 1642 | add_head_to_pending(&rev); |
427a8ec5 | 1643 | check_head = 1; |
7c496280 | 1644 | } |
8a1d076e JH |
1645 | /* |
1646 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
1647 | * "format-patch --root HEAD". The user wants | |
1648 | * get_revision() to do the usual traversal. | |
7c496280 | 1649 | */ |
427a8ec5 FC |
1650 | |
1651 | if (!strcmp(rev.pending.objects[0].name, "HEAD")) | |
1652 | check_head = 1; | |
1653 | ||
1654 | if (check_head) { | |
d801627b | 1655 | struct object_id oid; |
95b567c7 | 1656 | const char *ref, *v; |
7695d118 | 1657 | ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
d801627b | 1658 | oid.hash, NULL); |
95b567c7 JK |
1659 | if (ref && skip_prefix(ref, "refs/heads/", &v)) |
1660 | branch_name = xstrdup(v); | |
427a8ec5 FC |
1661 | else |
1662 | branch_name = xstrdup(""); /* no branch */ | |
1663 | } | |
e686eb98 | 1664 | } |
68c2ec7f JH |
1665 | |
1666 | /* | |
1667 | * We cannot move this anywhere earlier because we do want to | |
9517e6b8 | 1668 | * know if --root was given explicitly from the command line. |
68c2ec7f JH |
1669 | */ |
1670 | rev.show_root_diff = 1; | |
1671 | ||
657ab61e KB |
1672 | if (ignore_if_in_upstream) { |
1673 | /* Don't say anything if head and upstream are the same. */ | |
1674 | if (rev.pending.nr == 2) { | |
1675 | struct object_array_entry *o = rev.pending.objects; | |
f2fd0760 | 1676 | if (oidcmp(&o[0].item->oid, &o[1].item->oid) == 0) |
657ab61e KB |
1677 | return 0; |
1678 | } | |
811929f1 | 1679 | get_patch_ids(&rev, &ids); |
657ab61e | 1680 | } |
9c6efa36 | 1681 | |
3d51e1b5 | 1682 | if (prepare_revision_walk(&rev)) |
5c5f1d7c | 1683 | die(_("revision walk setup failed")); |
2bda2cf4 | 1684 | rev.boundary = 1; |
91efcf60 | 1685 | while ((commit = get_revision(&rev)) != NULL) { |
2bda2cf4 | 1686 | if (commit->object.flags & BOUNDARY) { |
2bda2cf4 DB |
1687 | boundary_count++; |
1688 | origin = (boundary_count == 1) ? commit : NULL; | |
1689 | continue; | |
1690 | } | |
1691 | ||
3acf8dd8 | 1692 | if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids)) |
9c6efa36 JS |
1693 | continue; |
1694 | ||
91efcf60 | 1695 | nr++; |
2756ca43 | 1696 | REALLOC_ARRAY(list, nr); |
91efcf60 JH |
1697 | list[nr - 1] = commit; |
1698 | } | |
80d35ca0 FC |
1699 | if (nr == 0) |
1700 | /* nothing to do */ | |
1701 | return 0; | |
0377db77 | 1702 | total = nr; |
2a4c2607 FC |
1703 | if (cover_letter == -1) { |
1704 | if (config_cover_letter == COVER_AUTO) | |
1705 | cover_letter = (total > 1); | |
1706 | else | |
1707 | cover_letter = (config_cover_letter == COVER_ON); | |
1708 | } | |
957ed3a5 JK |
1709 | if (!keep_subject && auto_number && (total > 1 || cover_letter)) |
1710 | numbered = 1; | |
1711 | if (numbered) | |
1712 | rev.total = total + start_number - 1; | |
2a4c2607 | 1713 | |
7022650f JM |
1714 | if (!signature) { |
1715 | ; /* --no-signature inhibits all signatures */ | |
1716 | } else if (signature && signature != git_version_string) { | |
1717 | ; /* non-default signature already set */ | |
1718 | } else if (signature_file) { | |
1719 | struct strbuf buf = STRBUF_INIT; | |
1720 | ||
1721 | if (strbuf_read_file(&buf, signature_file, 128) < 0) | |
1722 | die_errno(_("unable to read signature file '%s'"), signature_file); | |
1723 | signature = strbuf_detach(&buf, NULL); | |
1724 | } | |
1725 | ||
fa2ab86d | 1726 | memset(&bases, 0, sizeof(bases)); |
bb52995f | 1727 | if (base_commit || base_auto) { |
fa2ab86d XY |
1728 | struct commit *base = get_base_commit(base_commit, list, nr); |
1729 | reset_revision_walk(); | |
1730 | prepare_bases(&bases, base, list, nr); | |
1731 | } | |
1732 | ||
b079c50e TR |
1733 | if (in_reply_to || thread || cover_letter) |
1734 | rev.ref_message_ids = xcalloc(1, sizeof(struct string_list)); | |
1735 | if (in_reply_to) { | |
1736 | const char *msgid = clean_message_id(in_reply_to); | |
1d2f80fa | 1737 | string_list_append(rev.ref_message_ids, msgid); |
b079c50e | 1738 | } |
ce37596c | 1739 | rev.numbered_files = just_numbers; |
108dab28 | 1740 | rev.patch_suffix = fmt_patch_suffix; |
a5a27c79 DB |
1741 | if (cover_letter) { |
1742 | if (thread) | |
1743 | gen_message_id(&rev, "cover"); | |
ce37596c | 1744 | make_cover_letter(&rev, use_stdout, |
d7ddad01 | 1745 | origin, nr, list, branch_name, quiet); |
95235f5b | 1746 | print_bases(&bases, rev.diffopt.file); |
480871e0 | 1747 | print_signature(rev.diffopt.file); |
a5a27c79 DB |
1748 | total++; |
1749 | start_number--; | |
1750 | } | |
5289c56a | 1751 | rev.add_signoff = do_signoff; |
91efcf60 JH |
1752 | while (0 <= --nr) { |
1753 | int shown; | |
1754 | commit = list[nr]; | |
add5c8a5 | 1755 | rev.nr = total - nr + (start_number - 1); |
d1566f78 | 1756 | /* Make the second and subsequent mails replies to the first */ |
cc35de84 | 1757 | if (thread) { |
a5a27c79 | 1758 | /* Have we already had a message ID? */ |
e1a37346 | 1759 | if (rev.message_id) { |
a5a27c79 | 1760 | /* |
30984ed2 TR |
1761 | * For deep threading: make every mail |
1762 | * a reply to the previous one, no | |
1763 | * matter what other options are set. | |
1764 | * | |
1765 | * For shallow threading: | |
1766 | * | |
2175c10d TR |
1767 | * Without --cover-letter and |
1768 | * --in-reply-to, make every mail a | |
1769 | * reply to the one before. | |
1770 | * | |
1771 | * With --in-reply-to but no | |
1772 | * --cover-letter, make every mail a | |
1773 | * reply to the <reply-to>. | |
1774 | * | |
1775 | * With --cover-letter, make every | |
1776 | * mail but the cover letter a reply | |
1777 | * to the cover letter. The cover | |
1778 | * letter is a reply to the | |
1779 | * --in-reply-to, if specified. | |
a5a27c79 | 1780 | */ |
30984ed2 TR |
1781 | if (thread == THREAD_SHALLOW |
1782 | && rev.ref_message_ids->nr > 0 | |
2175c10d | 1783 | && (!cover_letter || rev.nr > 1)) |
e1a37346 DB |
1784 | free(rev.message_id); |
1785 | else | |
1d2f80fa JP |
1786 | string_list_append(rev.ref_message_ids, |
1787 | rev.message_id); | |
cc35de84 | 1788 | } |
f2fd0760 | 1789 | gen_message_id(&rev, oid_to_hex(&commit->object.oid)); |
d1566f78 | 1790 | } |
6df514af | 1791 | |
a21c2f94 | 1792 | if (!use_stdout && |
95235f5b | 1793 | open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet)) |
5c5f1d7c | 1794 | die(_("Failed to create output files")); |
91efcf60 | 1795 | shown = log_tree_commit(&rev, commit); |
0fb370da | 1796 | free_commit_buffer(commit); |
add5c8a5 JH |
1797 | |
1798 | /* We put one extra blank line between formatted | |
1799 | * patches and this flag is used by log-tree code | |
1800 | * to see if it needs to emit a LF before showing | |
1801 | * the log; when using one file per patch, we do | |
1802 | * not want the extra blank line. | |
1803 | */ | |
1804 | if (!use_stdout) | |
1805 | rev.shown_one = 0; | |
698ce6f8 | 1806 | if (shown) { |
480871e0 | 1807 | print_bases(&bases, rev.diffopt.file); |
698ce6f8 | 1808 | if (rev.mime_boundary) |
95235f5b | 1809 | fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n", |
698ce6f8 JS |
1810 | mime_boundary_leader, |
1811 | rev.mime_boundary); | |
1812 | else | |
95235f5b | 1813 | print_signature(rev.diffopt.file); |
698ce6f8 | 1814 | } |
0377db77 | 1815 | if (!use_stdout) |
95235f5b | 1816 | fclose(rev.diffopt.file); |
91efcf60 JH |
1817 | } |
1818 | free(list); | |
739453a3 | 1819 | free(branch_name); |
ca9e0a1b SB |
1820 | string_list_clear(&extra_to, 0); |
1821 | string_list_clear(&extra_cc, 0); | |
1822 | string_list_clear(&extra_hdr, 0); | |
5d23e133 JH |
1823 | if (ignore_if_in_upstream) |
1824 | free_patch_ids(&ids); | |
91efcf60 JH |
1825 | return 0; |
1826 | } | |
1827 | ||
e827633a RS |
1828 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) |
1829 | { | |
d801627b | 1830 | struct object_id oid; |
1831 | if (get_oid(arg, &oid) == 0) { | |
bc83266a | 1832 | struct commit *commit = lookup_commit_reference(&oid); |
e827633a RS |
1833 | if (commit) { |
1834 | commit->object.flags |= flags; | |
1835 | add_pending_object(revs, &commit->object, arg); | |
1836 | return 0; | |
1837 | } | |
1838 | } | |
1839 | return -1; | |
1840 | } | |
1841 | ||
28a53178 | 1842 | static const char * const cherry_usage[] = { |
986d1bb8 | 1843 | N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"), |
28a53178 EFL |
1844 | NULL |
1845 | }; | |
1846 | ||
a3a32e7f | 1847 | static void print_commit(char sign, struct commit *commit, int verbose, |
95235f5b | 1848 | int abbrev, FILE *file) |
a3a32e7f JN |
1849 | { |
1850 | if (!verbose) { | |
95235f5b | 1851 | fprintf(file, "%c %s\n", sign, |
ed1c9977 | 1852 | find_unique_abbrev(commit->object.oid.hash, abbrev)); |
a3a32e7f JN |
1853 | } else { |
1854 | struct strbuf buf = STRBUF_INIT; | |
f67d2e82 | 1855 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); |
95235f5b | 1856 | fprintf(file, "%c %s %s\n", sign, |
ed1c9977 | 1857 | find_unique_abbrev(commit->object.oid.hash, abbrev), |
a3a32e7f JN |
1858 | buf.buf); |
1859 | strbuf_release(&buf); | |
1860 | } | |
1861 | } | |
1862 | ||
e827633a RS |
1863 | int cmd_cherry(int argc, const char **argv, const char *prefix) |
1864 | { | |
1865 | struct rev_info revs; | |
5d23e133 | 1866 | struct patch_ids ids; |
e827633a RS |
1867 | struct commit *commit; |
1868 | struct commit_list *list = NULL; | |
f2968022 | 1869 | struct branch *current_branch; |
e827633a RS |
1870 | const char *upstream; |
1871 | const char *head = "HEAD"; | |
1872 | const char *limit = NULL; | |
28a53178 | 1873 | int verbose = 0, abbrev = 0; |
e827633a | 1874 | |
28a53178 EFL |
1875 | struct option options[] = { |
1876 | OPT__ABBREV(&abbrev), | |
986d1bb8 | 1877 | OPT__VERBOSE(&verbose, N_("be verbose")), |
28a53178 EFL |
1878 | OPT_END() |
1879 | }; | |
e827633a | 1880 | |
28a53178 | 1881 | argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); |
fef34270 | 1882 | |
e827633a | 1883 | switch (argc) { |
e827633a | 1884 | case 3: |
28a53178 | 1885 | limit = argv[2]; |
e827633a RS |
1886 | /* FALLTHROUGH */ |
1887 | case 2: | |
28a53178 EFL |
1888 | head = argv[1]; |
1889 | /* FALLTHROUGH */ | |
1890 | case 1: | |
1891 | upstream = argv[0]; | |
e827633a RS |
1892 | break; |
1893 | default: | |
f2968022 | 1894 | current_branch = branch_get(NULL); |
3a429d0a | 1895 | upstream = branch_get_upstream(current_branch, NULL); |
a9f9f8cc | 1896 | if (!upstream) { |
5c5f1d7c | 1897 | fprintf(stderr, _("Could not find a tracked" |
f2968022 | 1898 | " remote branch, please" |
5c5f1d7c | 1899 | " specify <upstream> manually.\n")); |
28a53178 | 1900 | usage_with_options(cherry_usage, options); |
f2968022 | 1901 | } |
e827633a RS |
1902 | } |
1903 | ||
1904 | init_revisions(&revs, prefix); | |
51f4de3a | 1905 | revs.max_parents = 1; |
e827633a RS |
1906 | |
1907 | if (add_pending_commit(head, &revs, 0)) | |
5c5f1d7c | 1908 | die(_("Unknown commit %s"), head); |
e827633a | 1909 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) |
5c5f1d7c | 1910 | die(_("Unknown commit %s"), upstream); |
e827633a RS |
1911 | |
1912 | /* Don't say anything if head and upstream are the same. */ | |
1913 | if (revs.pending.nr == 2) { | |
1914 | struct object_array_entry *o = revs.pending.objects; | |
f2fd0760 | 1915 | if (oidcmp(&o[0].item->oid, &o[1].item->oid) == 0) |
e827633a RS |
1916 | return 0; |
1917 | } | |
1918 | ||
811929f1 | 1919 | get_patch_ids(&revs, &ids); |
e827633a RS |
1920 | |
1921 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
5c5f1d7c | 1922 | die(_("Unknown commit %s"), limit); |
e827633a RS |
1923 | |
1924 | /* reverse the list of commits */ | |
3d51e1b5 | 1925 | if (prepare_revision_walk(&revs)) |
5c5f1d7c | 1926 | die(_("revision walk setup failed")); |
e827633a | 1927 | while ((commit = get_revision(&revs)) != NULL) { |
e827633a RS |
1928 | commit_list_insert(commit, &list); |
1929 | } | |
1930 | ||
1931 | while (list) { | |
e827633a RS |
1932 | char sign = '+'; |
1933 | ||
1934 | commit = list->item; | |
5d23e133 | 1935 | if (has_commit_patch_id(commit, &ids)) |
e827633a | 1936 | sign = '-'; |
95235f5b | 1937 | print_commit(sign, commit, verbose, abbrev, revs.diffopt.file); |
e827633a RS |
1938 | list = list->next; |
1939 | } | |
1940 | ||
5d23e133 | 1941 | free_patch_ids(&ids); |
e827633a RS |
1942 | return 0; |
1943 | } |