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