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