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