]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Builtin "git log" and related commands (show, whatchanged) | |
3 | * | |
4 | * (C) Copyright 2006 Linus Torvalds | |
5 | * 2006 Junio Hamano | |
6 | */ | |
7 | ||
8 | #define USE_THE_REPOSITORY_VARIABLE | |
9 | ||
10 | #include "builtin.h" | |
11 | #include "abspath.h" | |
12 | #include "config.h" | |
13 | #include "environment.h" | |
14 | #include "gettext.h" | |
15 | #include "hex.h" | |
16 | #include "refs.h" | |
17 | #include "object-name.h" | |
18 | #include "object-store.h" | |
19 | #include "pager.h" | |
20 | #include "color.h" | |
21 | #include "commit.h" | |
22 | #include "diff.h" | |
23 | #include "diff-merges.h" | |
24 | #include "revision.h" | |
25 | #include "log-tree.h" | |
26 | #include "builtin.h" | |
27 | #include "oid-array.h" | |
28 | #include "tag.h" | |
29 | #include "reflog-walk.h" | |
30 | #include "patch-ids.h" | |
31 | #include "path.h" | |
32 | #include "shortlog.h" | |
33 | #include "remote.h" | |
34 | #include "string-list.h" | |
35 | #include "parse-options.h" | |
36 | #include "line-log.h" | |
37 | #include "branch.h" | |
38 | #include "streaming.h" | |
39 | #include "version.h" | |
40 | #include "mailmap.h" | |
41 | #include "progress.h" | |
42 | #include "commit-slab.h" | |
43 | ||
44 | #include "commit-reach.h" | |
45 | #include "range-diff.h" | |
46 | #include "tmp-objdir.h" | |
47 | #include "tree.h" | |
48 | #include "write-or-die.h" | |
49 | ||
50 | #define MAIL_DEFAULT_WRAP 72 | |
51 | #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100 | |
52 | #define FORMAT_PATCH_NAME_MAX_DEFAULT 64 | |
53 | ||
54 | static unsigned int force_in_body_from; | |
55 | static int stdout_mboxrd; | |
56 | static int format_no_prefix; | |
57 | ||
58 | static const char * const builtin_log_usage[] = { | |
59 | N_("git log [<options>] [<revision-range>] [[--] <path>...]"), | |
60 | N_("git show [<options>] <object>..."), | |
61 | NULL | |
62 | }; | |
63 | ||
64 | struct line_opt_callback_data { | |
65 | struct rev_info *rev; | |
66 | const char *prefix; | |
67 | struct string_list args; | |
68 | }; | |
69 | ||
70 | static int session_is_interactive(void) | |
71 | { | |
72 | return isatty(1) || pager_in_use(); | |
73 | } | |
74 | ||
75 | static int auto_decoration_style(void) | |
76 | { | |
77 | return session_is_interactive() ? DECORATE_SHORT_REFS : 0; | |
78 | } | |
79 | ||
80 | static int parse_decoration_style(const char *value) | |
81 | { | |
82 | switch (git_parse_maybe_bool(value)) { | |
83 | case 1: | |
84 | return DECORATE_SHORT_REFS; | |
85 | case 0: | |
86 | return 0; | |
87 | default: | |
88 | break; | |
89 | } | |
90 | if (!strcmp(value, "full")) | |
91 | return DECORATE_FULL_REFS; | |
92 | else if (!strcmp(value, "short")) | |
93 | return DECORATE_SHORT_REFS; | |
94 | else if (!strcmp(value, "auto")) | |
95 | return auto_decoration_style(); | |
96 | /* | |
97 | * Please update _git_log() in git-completion.bash when you | |
98 | * add new decoration styles. | |
99 | */ | |
100 | return -1; | |
101 | } | |
102 | ||
103 | struct log_config { | |
104 | int default_abbrev_commit; | |
105 | int default_show_root; | |
106 | int default_follow; | |
107 | int default_show_signature; | |
108 | int default_encode_email_headers; | |
109 | int decoration_style; | |
110 | int decoration_given; | |
111 | int use_mailmap_config; | |
112 | char *fmt_patch_subject_prefix; | |
113 | int fmt_patch_name_max; | |
114 | char *fmt_pretty; | |
115 | char *default_date_mode; | |
116 | ||
117 | #ifndef WITH_BREAKING_CHANGES | |
118 | /* | |
119 | * Note: git_log_config() does not touch this member and that | |
120 | * is very deliberate. This member is only to be used to | |
121 | * resurrect whatchanged that is deprecated. | |
122 | */ | |
123 | int i_still_use_this; | |
124 | #endif | |
125 | }; | |
126 | ||
127 | static void log_config_init(struct log_config *cfg) | |
128 | { | |
129 | memset(cfg, 0, sizeof(*cfg)); | |
130 | cfg->default_show_root = 1; | |
131 | cfg->default_encode_email_headers = 1; | |
132 | cfg->use_mailmap_config = 1; | |
133 | cfg->fmt_patch_subject_prefix = xstrdup("PATCH"); | |
134 | cfg->fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT; | |
135 | cfg->decoration_style = auto_decoration_style(); | |
136 | } | |
137 | ||
138 | static void log_config_release(struct log_config *cfg) | |
139 | { | |
140 | free(cfg->default_date_mode); | |
141 | free(cfg->fmt_pretty); | |
142 | free(cfg->fmt_patch_subject_prefix); | |
143 | } | |
144 | ||
145 | static int use_default_decoration_filter = 1; | |
146 | static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP; | |
147 | static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP; | |
148 | static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP; | |
149 | ||
150 | static int clear_decorations_callback(const struct option *opt UNUSED, | |
151 | const char *arg, int unset) | |
152 | { | |
153 | BUG_ON_OPT_NEG(unset); | |
154 | BUG_ON_OPT_ARG(arg); | |
155 | string_list_clear(&decorate_refs_include, 0); | |
156 | string_list_clear(&decorate_refs_exclude, 0); | |
157 | use_default_decoration_filter = 0; | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static int decorate_callback(const struct option *opt, const char *arg, | |
162 | int unset) | |
163 | { | |
164 | struct log_config *cfg = opt->value; | |
165 | ||
166 | if (unset) | |
167 | cfg->decoration_style = 0; | |
168 | else if (arg) | |
169 | cfg->decoration_style = parse_decoration_style(arg); | |
170 | else | |
171 | cfg->decoration_style = DECORATE_SHORT_REFS; | |
172 | ||
173 | if (cfg->decoration_style < 0) | |
174 | die(_("invalid --decorate option: %s"), arg); | |
175 | ||
176 | cfg->decoration_given = 1; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static int log_line_range_callback(const struct option *option, const char *arg, int unset) | |
182 | { | |
183 | struct line_opt_callback_data *data = option->value; | |
184 | ||
185 | BUG_ON_OPT_NEG(unset); | |
186 | ||
187 | if (!arg) | |
188 | return -1; | |
189 | ||
190 | data->rev->line_level_traverse = 1; | |
191 | string_list_append(&data->args, arg); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static void cmd_log_init_defaults(struct rev_info *rev, | |
197 | struct log_config *cfg) | |
198 | { | |
199 | if (cfg->fmt_pretty) | |
200 | get_commit_format(cfg->fmt_pretty, rev); | |
201 | if (cfg->default_follow) | |
202 | rev->diffopt.flags.default_follow_renames = 1; | |
203 | rev->verbose_header = 1; | |
204 | init_diffstat_widths(&rev->diffopt); | |
205 | rev->diffopt.flags.recursive = 1; | |
206 | rev->diffopt.flags.allow_textconv = 1; | |
207 | rev->abbrev_commit = cfg->default_abbrev_commit; | |
208 | rev->show_root_diff = cfg->default_show_root; | |
209 | rev->subject_prefix = cfg->fmt_patch_subject_prefix; | |
210 | rev->patch_name_max = cfg->fmt_patch_name_max; | |
211 | rev->show_signature = cfg->default_show_signature; | |
212 | rev->encode_email_headers = cfg->default_encode_email_headers; | |
213 | ||
214 | if (cfg->default_date_mode) | |
215 | parse_date_format(cfg->default_date_mode, &rev->date_mode); | |
216 | } | |
217 | ||
218 | static void set_default_decoration_filter(struct decoration_filter *decoration_filter) | |
219 | { | |
220 | char *value = NULL; | |
221 | struct string_list *include = decoration_filter->include_ref_pattern; | |
222 | const struct string_list *config_exclude; | |
223 | ||
224 | if (!git_config_get_string_multi("log.excludeDecoration", | |
225 | &config_exclude)) { | |
226 | struct string_list_item *item; | |
227 | for_each_string_list_item(item, config_exclude) | |
228 | string_list_append(decoration_filter->exclude_ref_config_pattern, | |
229 | item->string); | |
230 | } | |
231 | ||
232 | /* | |
233 | * By default, decorate_all is disabled. Enable it if | |
234 | * log.initialDecorationSet=all. Don't ever disable it by config, | |
235 | * since the command-line takes precedent. | |
236 | */ | |
237 | if (use_default_decoration_filter && | |
238 | !git_config_get_string("log.initialdecorationset", &value) && | |
239 | !strcmp("all", value)) | |
240 | use_default_decoration_filter = 0; | |
241 | free(value); | |
242 | ||
243 | if (!use_default_decoration_filter || | |
244 | decoration_filter->exclude_ref_pattern->nr || | |
245 | decoration_filter->include_ref_pattern->nr || | |
246 | decoration_filter->exclude_ref_config_pattern->nr) | |
247 | return; | |
248 | ||
249 | /* | |
250 | * No command-line or config options were given, so | |
251 | * populate with sensible defaults. | |
252 | */ | |
253 | for (size_t i = 0; i < ARRAY_SIZE(ref_namespace); i++) { | |
254 | if (!ref_namespace[i].decoration) | |
255 | continue; | |
256 | ||
257 | string_list_append(include, ref_namespace[i].ref); | |
258 | } | |
259 | } | |
260 | ||
261 | static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, | |
262 | struct rev_info *rev, struct setup_revision_opt *opt, | |
263 | struct log_config *cfg) | |
264 | { | |
265 | struct userformat_want w; | |
266 | int quiet = 0, source = 0, mailmap; | |
267 | static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP}; | |
268 | struct decoration_filter decoration_filter = { | |
269 | .exclude_ref_pattern = &decorate_refs_exclude, | |
270 | .include_ref_pattern = &decorate_refs_include, | |
271 | .exclude_ref_config_pattern = &decorate_refs_exclude_config, | |
272 | }; | |
273 | static struct revision_sources revision_sources; | |
274 | ||
275 | const struct option builtin_log_options[] = { | |
276 | OPT__QUIET(&quiet, N_("suppress diff output")), | |
277 | OPT_BOOL(0, "source", &source, N_("show source")), | |
278 | OPT_BOOL(0, "use-mailmap", &mailmap, N_("use mail map file")), | |
279 | #ifndef WITH_BREAKING_CHANGES | |
280 | OPT_HIDDEN_BOOL(0, "i-still-use-this", &cfg->i_still_use_this, | |
281 | "<use this deprecated command>"), | |
282 | #endif | |
283 | OPT_ALIAS(0, "mailmap", "use-mailmap"), | |
284 | OPT_CALLBACK_F(0, "clear-decorations", NULL, NULL, | |
285 | N_("clear all previously-defined decoration filters"), | |
286 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
287 | clear_decorations_callback), | |
288 | OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include, | |
289 | N_("pattern"), N_("only decorate refs that match <pattern>")), | |
290 | OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude, | |
291 | N_("pattern"), N_("do not decorate refs that match <pattern>")), | |
292 | OPT_CALLBACK_F(0, "decorate", cfg, NULL, N_("decorate options"), | |
293 | PARSE_OPT_OPTARG, decorate_callback), | |
294 | OPT_CALLBACK('L', NULL, &line_cb, "range:file", | |
295 | N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"), | |
296 | log_line_range_callback), | |
297 | OPT_END() | |
298 | }; | |
299 | ||
300 | line_cb.rev = rev; | |
301 | line_cb.prefix = prefix; | |
302 | ||
303 | mailmap = cfg->use_mailmap_config; | |
304 | argc = parse_options(argc, argv, prefix, | |
305 | builtin_log_options, builtin_log_usage, | |
306 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | | |
307 | PARSE_OPT_KEEP_DASHDASH); | |
308 | ||
309 | if (quiet) | |
310 | rev->diffopt.output_format |= DIFF_FORMAT_NO_OUTPUT; | |
311 | argc = setup_revisions(argc, argv, rev, opt); | |
312 | ||
313 | /* Any arguments at this point are not recognized */ | |
314 | if (argc > 1) | |
315 | die(_("unrecognized argument: %s"), argv[1]); | |
316 | ||
317 | if (rev->line_level_traverse && rev->prune_data.nr) | |
318 | die(_("-L<range>:<file> cannot be used with pathspec")); | |
319 | ||
320 | memset(&w, 0, sizeof(w)); | |
321 | userformat_find_requirements(NULL, &w); | |
322 | ||
323 | if (!rev->show_notes_given && (!rev->pretty_given || w.notes)) | |
324 | rev->show_notes = 1; | |
325 | if (rev->show_notes) | |
326 | load_display_notes(&rev->notes_opt); | |
327 | ||
328 | if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) || | |
329 | rev->diffopt.filter || rev->diffopt.flags.follow_renames) | |
330 | rev->always_show_header = 0; | |
331 | ||
332 | if (source || w.source) { | |
333 | init_revision_sources(&revision_sources); | |
334 | rev->sources = &revision_sources; | |
335 | } | |
336 | ||
337 | if (mailmap) { | |
338 | rev->mailmap = xmalloc(sizeof(struct string_list)); | |
339 | string_list_init_nodup(rev->mailmap); | |
340 | read_mailmap(rev->mailmap); | |
341 | } | |
342 | ||
343 | if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) { | |
344 | /* | |
345 | * "log --pretty=raw" is special; ignore UI oriented | |
346 | * configuration variables such as decoration. | |
347 | */ | |
348 | if (!cfg->decoration_given) | |
349 | cfg->decoration_style = 0; | |
350 | if (!rev->abbrev_commit_given) | |
351 | rev->abbrev_commit = 0; | |
352 | } | |
353 | ||
354 | if (rev->commit_format == CMIT_FMT_USERFORMAT) { | |
355 | if (!w.decorate) { | |
356 | /* | |
357 | * Disable decoration loading if the format will not | |
358 | * show them anyway. | |
359 | */ | |
360 | cfg->decoration_style = 0; | |
361 | } else if (!cfg->decoration_style) { | |
362 | /* | |
363 | * If we are going to show them, make sure we do load | |
364 | * them here, but taking care not to override a | |
365 | * specific style set by config or --decorate. | |
366 | */ | |
367 | cfg->decoration_style = DECORATE_SHORT_REFS; | |
368 | } | |
369 | } | |
370 | ||
371 | if (cfg->decoration_style || rev->simplify_by_decoration) { | |
372 | set_default_decoration_filter(&decoration_filter); | |
373 | ||
374 | if (cfg->decoration_style) | |
375 | rev->show_decorations = 1; | |
376 | ||
377 | load_ref_decorations(&decoration_filter, cfg->decoration_style); | |
378 | } | |
379 | ||
380 | if (rev->line_level_traverse) | |
381 | line_log_init(rev, line_cb.prefix, &line_cb.args); | |
382 | ||
383 | setup_pager(the_repository); | |
384 | } | |
385 | ||
386 | static void cmd_log_init(int argc, const char **argv, const char *prefix, | |
387 | struct rev_info *rev, struct setup_revision_opt *opt, | |
388 | struct log_config *cfg) | |
389 | { | |
390 | cmd_log_init_defaults(rev, cfg); | |
391 | cmd_log_init_finish(argc, argv, prefix, rev, opt, cfg); | |
392 | } | |
393 | ||
394 | /* | |
395 | * This gives a rough estimate for how many commits we | |
396 | * will print out in the list. | |
397 | */ | |
398 | static int estimate_commit_count(struct commit_list *list) | |
399 | { | |
400 | int n = 0; | |
401 | ||
402 | while (list) { | |
403 | struct commit *commit = list->item; | |
404 | unsigned int flags = commit->object.flags; | |
405 | list = list->next; | |
406 | if (!(flags & (TREESAME | UNINTERESTING))) | |
407 | n++; | |
408 | } | |
409 | return n; | |
410 | } | |
411 | ||
412 | static void show_early_header(struct rev_info *rev, const char *stage, int nr) | |
413 | { | |
414 | if (rev->shown_one) { | |
415 | rev->shown_one = 0; | |
416 | if (rev->commit_format != CMIT_FMT_ONELINE) | |
417 | putchar(rev->diffopt.line_termination); | |
418 | } | |
419 | fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage); | |
420 | } | |
421 | ||
422 | static struct itimerval early_output_timer; | |
423 | ||
424 | static void log_show_early(struct rev_info *revs, struct commit_list *list) | |
425 | { | |
426 | int i = revs->early_output; | |
427 | int show_header = 1; | |
428 | int no_free = revs->diffopt.no_free; | |
429 | ||
430 | revs->diffopt.no_free = 0; | |
431 | sort_in_topological_order(&list, revs->sort_order); | |
432 | while (list && i) { | |
433 | struct commit *commit = list->item; | |
434 | switch (simplify_commit(revs, commit)) { | |
435 | case commit_show: | |
436 | if (show_header) { | |
437 | int n = estimate_commit_count(list); | |
438 | show_early_header(revs, "incomplete", n); | |
439 | show_header = 0; | |
440 | } | |
441 | log_tree_commit(revs, commit); | |
442 | i--; | |
443 | break; | |
444 | case commit_ignore: | |
445 | break; | |
446 | case commit_error: | |
447 | revs->diffopt.no_free = no_free; | |
448 | diff_free(&revs->diffopt); | |
449 | return; | |
450 | } | |
451 | list = list->next; | |
452 | } | |
453 | ||
454 | /* Did we already get enough commits for the early output? */ | |
455 | if (!i) { | |
456 | revs->diffopt.no_free = 0; | |
457 | diff_free(&revs->diffopt); | |
458 | return; | |
459 | } | |
460 | ||
461 | /* | |
462 | * ..if no, then repeat it twice a second until we | |
463 | * do. | |
464 | * | |
465 | * NOTE! We don't use "it_interval", because if the | |
466 | * reader isn't listening, we want our output to be | |
467 | * throttled by the writing, and not have the timer | |
468 | * trigger every second even if we're blocked on a | |
469 | * reader! | |
470 | */ | |
471 | early_output_timer.it_value.tv_sec = 0; | |
472 | early_output_timer.it_value.tv_usec = 500000; | |
473 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
474 | } | |
475 | ||
476 | static void early_output(int signal UNUSED) | |
477 | { | |
478 | show_early_output = log_show_early; | |
479 | } | |
480 | ||
481 | static void setup_early_output(void) | |
482 | { | |
483 | struct sigaction sa; | |
484 | ||
485 | /* | |
486 | * Set up the signal handler, minimally intrusively: | |
487 | * we only set a single volatile integer word (not | |
488 | * using sigatomic_t - trying to avoid unnecessary | |
489 | * system dependencies and headers), and using | |
490 | * SA_RESTART. | |
491 | */ | |
492 | memset(&sa, 0, sizeof(sa)); | |
493 | sa.sa_handler = early_output; | |
494 | sigemptyset(&sa.sa_mask); | |
495 | sa.sa_flags = SA_RESTART; | |
496 | sigaction(SIGALRM, &sa, NULL); | |
497 | ||
498 | /* | |
499 | * If we can get the whole output in less than a | |
500 | * tenth of a second, don't even bother doing the | |
501 | * early-output thing.. | |
502 | * | |
503 | * This is a one-time-only trigger. | |
504 | */ | |
505 | early_output_timer.it_value.tv_sec = 0; | |
506 | early_output_timer.it_value.tv_usec = 100000; | |
507 | setitimer(ITIMER_REAL, &early_output_timer, NULL); | |
508 | } | |
509 | ||
510 | static void finish_early_output(struct rev_info *rev) | |
511 | { | |
512 | int n = estimate_commit_count(rev->commits); | |
513 | signal(SIGALRM, SIG_IGN); | |
514 | show_early_header(rev, "done", n); | |
515 | } | |
516 | ||
517 | static int cmd_log_walk_no_free(struct rev_info *rev) | |
518 | { | |
519 | struct commit *commit; | |
520 | int saved_nrl = 0; | |
521 | int saved_dcctc = 0; | |
522 | int result; | |
523 | ||
524 | if (rev->early_output) | |
525 | setup_early_output(); | |
526 | ||
527 | if (prepare_revision_walk(rev)) | |
528 | die(_("revision walk setup failed")); | |
529 | ||
530 | if (rev->early_output) | |
531 | finish_early_output(rev); | |
532 | ||
533 | /* | |
534 | * For --check and --exit-code, the exit code is based on CHECK_FAILED | |
535 | * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to | |
536 | * retain that state information if replacing rev->diffopt in this loop | |
537 | */ | |
538 | while ((commit = get_revision(rev)) != NULL) { | |
539 | if (!log_tree_commit(rev, commit) && rev->max_count >= 0) | |
540 | /* | |
541 | * We decremented max_count in get_revision, | |
542 | * but we didn't actually show the commit. | |
543 | */ | |
544 | rev->max_count++; | |
545 | if (!rev->reflog_info && !rev->remerge_diff) { | |
546 | /* | |
547 | * We may show a given commit multiple times when | |
548 | * walking the reflogs. Therefore we still need it. | |
549 | * | |
550 | * Likewise, we potentially still need the parents | |
551 | * of * already shown commits to determine merge | |
552 | * bases when showing remerge diffs. | |
553 | */ | |
554 | free_commit_buffer(the_repository->parsed_objects, | |
555 | commit); | |
556 | free_commit_list(commit->parents); | |
557 | commit->parents = NULL; | |
558 | } | |
559 | if (saved_nrl < rev->diffopt.needed_rename_limit) | |
560 | saved_nrl = rev->diffopt.needed_rename_limit; | |
561 | if (rev->diffopt.degraded_cc_to_c) | |
562 | saved_dcctc = 1; | |
563 | } | |
564 | rev->diffopt.degraded_cc_to_c = saved_dcctc; | |
565 | rev->diffopt.needed_rename_limit = saved_nrl; | |
566 | ||
567 | result = diff_result_code(rev); | |
568 | if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF && | |
569 | rev->diffopt.flags.check_failed) { | |
570 | result = 02; | |
571 | } | |
572 | return result; | |
573 | } | |
574 | ||
575 | static int cmd_log_walk(struct rev_info *rev) | |
576 | { | |
577 | int retval; | |
578 | ||
579 | rev->diffopt.no_free = 1; | |
580 | retval = cmd_log_walk_no_free(rev); | |
581 | rev->diffopt.no_free = 0; | |
582 | diff_free(&rev->diffopt); | |
583 | return retval; | |
584 | } | |
585 | ||
586 | static int git_log_config(const char *var, const char *value, | |
587 | const struct config_context *ctx, void *cb) | |
588 | { | |
589 | struct log_config *cfg = cb; | |
590 | const char *slot_name; | |
591 | ||
592 | if (!strcmp(var, "format.pretty")) { | |
593 | FREE_AND_NULL(cfg->fmt_pretty); | |
594 | return git_config_string(&cfg->fmt_pretty, var, value); | |
595 | } | |
596 | if (!strcmp(var, "format.subjectprefix")) { | |
597 | FREE_AND_NULL(cfg->fmt_patch_subject_prefix); | |
598 | return git_config_string(&cfg->fmt_patch_subject_prefix, var, value); | |
599 | } | |
600 | if (!strcmp(var, "format.filenamemaxlength")) { | |
601 | cfg->fmt_patch_name_max = git_config_int(var, value, ctx->kvi); | |
602 | return 0; | |
603 | } | |
604 | if (!strcmp(var, "format.encodeemailheaders")) { | |
605 | cfg->default_encode_email_headers = git_config_bool(var, value); | |
606 | return 0; | |
607 | } | |
608 | if (!strcmp(var, "log.abbrevcommit")) { | |
609 | cfg->default_abbrev_commit = git_config_bool(var, value); | |
610 | return 0; | |
611 | } | |
612 | if (!strcmp(var, "log.date")) { | |
613 | FREE_AND_NULL(cfg->default_date_mode); | |
614 | return git_config_string(&cfg->default_date_mode, var, value); | |
615 | } | |
616 | if (!strcmp(var, "log.decorate")) { | |
617 | cfg->decoration_style = parse_decoration_style(value); | |
618 | if (cfg->decoration_style < 0) | |
619 | cfg->decoration_style = 0; /* maybe warn? */ | |
620 | return 0; | |
621 | } | |
622 | if (!strcmp(var, "log.diffmerges")) { | |
623 | if (!value) | |
624 | return config_error_nonbool(var); | |
625 | return diff_merges_config(value); | |
626 | } | |
627 | if (!strcmp(var, "log.showroot")) { | |
628 | cfg->default_show_root = git_config_bool(var, value); | |
629 | return 0; | |
630 | } | |
631 | if (!strcmp(var, "log.follow")) { | |
632 | cfg->default_follow = git_config_bool(var, value); | |
633 | return 0; | |
634 | } | |
635 | if (skip_prefix(var, "color.decorate.", &slot_name)) | |
636 | return parse_decorate_color_config(var, slot_name, value); | |
637 | if (!strcmp(var, "log.mailmap")) { | |
638 | cfg->use_mailmap_config = git_config_bool(var, value); | |
639 | return 0; | |
640 | } | |
641 | if (!strcmp(var, "log.showsignature")) { | |
642 | cfg->default_show_signature = git_config_bool(var, value); | |
643 | return 0; | |
644 | } | |
645 | ||
646 | return git_diff_ui_config(var, value, ctx, cb); | |
647 | } | |
648 | ||
649 | #ifndef WITH_BREAKING_CHANGES | |
650 | int cmd_whatchanged(int argc, | |
651 | const char **argv, | |
652 | const char *prefix, | |
653 | struct repository *repo UNUSED) | |
654 | { | |
655 | struct log_config cfg; | |
656 | struct rev_info rev; | |
657 | struct setup_revision_opt opt; | |
658 | int ret; | |
659 | ||
660 | log_config_init(&cfg); | |
661 | init_diff_ui_defaults(); | |
662 | git_config(git_log_config, &cfg); | |
663 | ||
664 | repo_init_revisions(the_repository, &rev, prefix); | |
665 | git_config(grep_config, &rev.grep_filter); | |
666 | ||
667 | rev.diff = 1; | |
668 | rev.simplify_history = 0; | |
669 | memset(&opt, 0, sizeof(opt)); | |
670 | opt.def = "HEAD"; | |
671 | opt.revarg_opt = REVARG_COMMITTISH; | |
672 | cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); | |
673 | ||
674 | if (!cfg.i_still_use_this) | |
675 | you_still_use_that("git whatchanged"); | |
676 | ||
677 | if (!rev.diffopt.output_format) | |
678 | rev.diffopt.output_format = DIFF_FORMAT_RAW; | |
679 | ||
680 | ret = cmd_log_walk(&rev); | |
681 | ||
682 | release_revisions(&rev); | |
683 | log_config_release(&cfg); | |
684 | return ret; | |
685 | } | |
686 | #endif | |
687 | ||
688 | static void show_tagger(const char *buf, struct rev_info *rev) | |
689 | { | |
690 | struct strbuf out = STRBUF_INIT; | |
691 | struct pretty_print_context pp = {0}; | |
692 | ||
693 | pp.fmt = rev->commit_format; | |
694 | pp.date_mode = rev->date_mode; | |
695 | pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding()); | |
696 | fprintf(rev->diffopt.file, "%s", out.buf); | |
697 | strbuf_release(&out); | |
698 | } | |
699 | ||
700 | static int show_blob_object(const struct object_id *oid, struct rev_info *rev, const char *obj_name) | |
701 | { | |
702 | struct object_id oidc; | |
703 | struct object_context obj_context = {0}; | |
704 | char *buf; | |
705 | unsigned long size; | |
706 | ||
707 | fflush(rev->diffopt.file); | |
708 | if (!rev->diffopt.flags.textconv_set_via_cmdline || | |
709 | !rev->diffopt.flags.allow_textconv) | |
710 | return stream_blob_to_fd(1, oid, NULL, 0); | |
711 | ||
712 | if (get_oid_with_context(the_repository, obj_name, | |
713 | GET_OID_RECORD_PATH, | |
714 | &oidc, &obj_context)) | |
715 | die(_("not a valid object name %s"), obj_name); | |
716 | if (!obj_context.path || | |
717 | !textconv_object(the_repository, obj_context.path, | |
718 | obj_context.mode, &oidc, 1, &buf, &size)) { | |
719 | object_context_release(&obj_context); | |
720 | return stream_blob_to_fd(1, oid, NULL, 0); | |
721 | } | |
722 | ||
723 | if (!buf) | |
724 | die(_("git show %s: bad file"), obj_name); | |
725 | ||
726 | write_or_die(1, buf, size); | |
727 | object_context_release(&obj_context); | |
728 | free(buf); | |
729 | return 0; | |
730 | } | |
731 | ||
732 | static int show_tag_object(const struct object_id *oid, struct rev_info *rev) | |
733 | { | |
734 | unsigned long size; | |
735 | enum object_type type; | |
736 | char *buf = repo_read_object_file(the_repository, oid, &type, &size); | |
737 | unsigned long offset = 0; | |
738 | ||
739 | if (!buf) | |
740 | return error(_("could not read object %s"), oid_to_hex(oid)); | |
741 | ||
742 | assert(type == OBJ_TAG); | |
743 | while (offset < size && buf[offset] != '\n') { | |
744 | unsigned long new_offset = offset + 1; | |
745 | const char *ident; | |
746 | while (new_offset < size && buf[new_offset++] != '\n') | |
747 | ; /* do nothing */ | |
748 | if (skip_prefix(buf + offset, "tagger ", &ident)) | |
749 | show_tagger(ident, rev); | |
750 | offset = new_offset; | |
751 | } | |
752 | ||
753 | if (offset < size) | |
754 | fwrite(buf + offset, size - offset, 1, rev->diffopt.file); | |
755 | free(buf); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | static int show_tree_object(const struct object_id *oid UNUSED, | |
760 | struct strbuf *base UNUSED, | |
761 | const char *pathname, unsigned mode, | |
762 | void *context) | |
763 | { | |
764 | FILE *file = context; | |
765 | fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : ""); | |
766 | return 0; | |
767 | } | |
768 | ||
769 | static void show_setup_revisions_tweak(struct rev_info *rev) | |
770 | { | |
771 | if (rev->first_parent_only) | |
772 | diff_merges_default_to_first_parent(rev); | |
773 | else | |
774 | diff_merges_default_to_dense_combined(rev); | |
775 | if (!rev->diffopt.output_format) | |
776 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
777 | } | |
778 | ||
779 | int cmd_show(int argc, | |
780 | const char **argv, | |
781 | const char *prefix, | |
782 | struct repository *repo UNUSED) | |
783 | { | |
784 | struct log_config cfg; | |
785 | struct rev_info rev; | |
786 | unsigned int i; | |
787 | struct setup_revision_opt opt; | |
788 | struct pathspec match_all; | |
789 | int ret = 0; | |
790 | ||
791 | log_config_init(&cfg); | |
792 | init_diff_ui_defaults(); | |
793 | git_config(git_log_config, &cfg); | |
794 | ||
795 | if (the_repository->gitdir) { | |
796 | prepare_repo_settings(the_repository); | |
797 | the_repository->settings.command_requires_full_index = 0; | |
798 | } | |
799 | ||
800 | memset(&match_all, 0, sizeof(match_all)); | |
801 | repo_init_revisions(the_repository, &rev, prefix); | |
802 | git_config(grep_config, &rev.grep_filter); | |
803 | ||
804 | rev.diff = 1; | |
805 | rev.always_show_header = 1; | |
806 | rev.no_walk = 1; | |
807 | rev.diffopt.stat_width = -1; /* Scale to real terminal size */ | |
808 | ||
809 | memset(&opt, 0, sizeof(opt)); | |
810 | opt.def = "HEAD"; | |
811 | opt.tweak = show_setup_revisions_tweak; | |
812 | cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); | |
813 | ||
814 | if (!rev.no_walk) { | |
815 | ret = cmd_log_walk(&rev); | |
816 | release_revisions(&rev); | |
817 | log_config_release(&cfg); | |
818 | return ret; | |
819 | } | |
820 | ||
821 | rev.diffopt.no_free = 1; | |
822 | for (i = 0; i < rev.pending.nr && !ret; i++) { | |
823 | struct object *o = rev.pending.objects[i].item; | |
824 | const char *name = rev.pending.objects[i].name; | |
825 | switch (o->type) { | |
826 | case OBJ_BLOB: | |
827 | ret = show_blob_object(&o->oid, &rev, name); | |
828 | break; | |
829 | case OBJ_TAG: { | |
830 | struct tag *t = (struct tag *)o; | |
831 | struct object_id *oid = get_tagged_oid(t); | |
832 | ||
833 | if (rev.shown_one) | |
834 | putchar('\n'); | |
835 | fprintf(rev.diffopt.file, "%stag %s%s\n", | |
836 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), | |
837 | t->tag, | |
838 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); | |
839 | ret = show_tag_object(&o->oid, &rev); | |
840 | rev.shown_one = 1; | |
841 | if (ret) | |
842 | break; | |
843 | o = parse_object(the_repository, oid); | |
844 | if (!o) | |
845 | ret = error(_("could not read object %s"), | |
846 | oid_to_hex(oid)); | |
847 | rev.pending.objects[i].item = o; | |
848 | i--; | |
849 | break; | |
850 | } | |
851 | case OBJ_TREE: | |
852 | if (rev.shown_one) | |
853 | putchar('\n'); | |
854 | fprintf(rev.diffopt.file, "%stree %s%s\n\n", | |
855 | diff_get_color_opt(&rev.diffopt, DIFF_COMMIT), | |
856 | name, | |
857 | diff_get_color_opt(&rev.diffopt, DIFF_RESET)); | |
858 | read_tree(the_repository, (struct tree *)o, | |
859 | &match_all, show_tree_object, | |
860 | rev.diffopt.file); | |
861 | rev.shown_one = 1; | |
862 | break; | |
863 | case OBJ_COMMIT: | |
864 | { | |
865 | struct object_array old; | |
866 | struct object_array blank = OBJECT_ARRAY_INIT; | |
867 | ||
868 | memcpy(&old, &rev.pending, sizeof(old)); | |
869 | memcpy(&rev.pending, &blank, sizeof(rev.pending)); | |
870 | ||
871 | add_object_array(o, name, &rev.pending); | |
872 | ret = cmd_log_walk_no_free(&rev); | |
873 | ||
874 | /* | |
875 | * No need for | |
876 | * object_array_clear(&pending). It was | |
877 | * cleared already in prepare_revision_walk() | |
878 | */ | |
879 | memcpy(&rev.pending, &old, sizeof(rev.pending)); | |
880 | break; | |
881 | } | |
882 | default: | |
883 | ret = error(_("unknown type: %d"), o->type); | |
884 | } | |
885 | } | |
886 | ||
887 | rev.diffopt.no_free = 0; | |
888 | diff_free(&rev.diffopt); | |
889 | release_revisions(&rev); | |
890 | log_config_release(&cfg); | |
891 | ||
892 | return ret; | |
893 | } | |
894 | ||
895 | /* | |
896 | * This is equivalent to "git log -g --abbrev-commit --pretty=oneline" | |
897 | */ | |
898 | int cmd_log_reflog(int argc, | |
899 | const char **argv, | |
900 | const char *prefix, | |
901 | struct repository *repo UNUSED) | |
902 | { | |
903 | struct log_config cfg; | |
904 | struct rev_info rev; | |
905 | struct setup_revision_opt opt; | |
906 | int ret; | |
907 | ||
908 | log_config_init(&cfg); | |
909 | init_diff_ui_defaults(); | |
910 | git_config(git_log_config, &cfg); | |
911 | ||
912 | repo_init_revisions(the_repository, &rev, prefix); | |
913 | init_reflog_walk(&rev.reflog_info); | |
914 | git_config(grep_config, &rev.grep_filter); | |
915 | ||
916 | rev.verbose_header = 1; | |
917 | memset(&opt, 0, sizeof(opt)); | |
918 | opt.def = "HEAD"; | |
919 | cmd_log_init_defaults(&rev, &cfg); | |
920 | rev.abbrev_commit = 1; | |
921 | rev.commit_format = CMIT_FMT_ONELINE; | |
922 | rev.use_terminator = 1; | |
923 | rev.always_show_header = 1; | |
924 | cmd_log_init_finish(argc, argv, prefix, &rev, &opt, &cfg); | |
925 | ||
926 | ret = cmd_log_walk(&rev); | |
927 | ||
928 | release_revisions(&rev); | |
929 | log_config_release(&cfg); | |
930 | return ret; | |
931 | } | |
932 | ||
933 | static void log_setup_revisions_tweak(struct rev_info *rev) | |
934 | { | |
935 | if (rev->diffopt.flags.default_follow_renames && | |
936 | diff_check_follow_pathspec(&rev->prune_data, 0)) | |
937 | rev->diffopt.flags.follow_renames = 1; | |
938 | ||
939 | if (rev->first_parent_only) | |
940 | diff_merges_default_to_first_parent(rev); | |
941 | } | |
942 | ||
943 | int cmd_log(int argc, | |
944 | const char **argv, | |
945 | const char *prefix, | |
946 | struct repository *repo UNUSED) | |
947 | { | |
948 | struct log_config cfg; | |
949 | struct rev_info rev; | |
950 | struct setup_revision_opt opt; | |
951 | int ret; | |
952 | ||
953 | log_config_init(&cfg); | |
954 | init_diff_ui_defaults(); | |
955 | git_config(git_log_config, &cfg); | |
956 | ||
957 | repo_init_revisions(the_repository, &rev, prefix); | |
958 | git_config(grep_config, &rev.grep_filter); | |
959 | ||
960 | rev.always_show_header = 1; | |
961 | memset(&opt, 0, sizeof(opt)); | |
962 | opt.def = "HEAD"; | |
963 | opt.revarg_opt = REVARG_COMMITTISH; | |
964 | opt.tweak = log_setup_revisions_tweak; | |
965 | cmd_log_init(argc, argv, prefix, &rev, &opt, &cfg); | |
966 | ||
967 | ret = cmd_log_walk(&rev); | |
968 | ||
969 | release_revisions(&rev); | |
970 | log_config_release(&cfg); | |
971 | return ret; | |
972 | } | |
973 | ||
974 | /* format-patch */ | |
975 | ||
976 | enum cover_setting { | |
977 | COVER_UNSET, | |
978 | COVER_OFF, | |
979 | COVER_ON, | |
980 | COVER_AUTO | |
981 | }; | |
982 | ||
983 | enum thread_level { | |
984 | THREAD_UNSET, | |
985 | THREAD_SHALLOW, | |
986 | THREAD_DEEP | |
987 | }; | |
988 | ||
989 | enum cover_from_description { | |
990 | COVER_FROM_NONE, | |
991 | COVER_FROM_MESSAGE, | |
992 | COVER_FROM_SUBJECT, | |
993 | COVER_FROM_AUTO | |
994 | }; | |
995 | ||
996 | enum auto_base_setting { | |
997 | AUTO_BASE_NEVER, | |
998 | AUTO_BASE_ALWAYS, | |
999 | AUTO_BASE_WHEN_ABLE | |
1000 | }; | |
1001 | ||
1002 | struct format_config { | |
1003 | struct log_config log; | |
1004 | enum thread_level thread; | |
1005 | int do_signoff; | |
1006 | enum auto_base_setting auto_base; | |
1007 | char *base_commit; | |
1008 | char *from; | |
1009 | char *signature; | |
1010 | char *signature_file; | |
1011 | enum cover_setting config_cover_letter; | |
1012 | char *config_output_directory; | |
1013 | enum cover_from_description cover_from_description_mode; | |
1014 | int show_notes; | |
1015 | struct display_notes_opt notes_opt; | |
1016 | int numbered_cmdline_opt; | |
1017 | int numbered; | |
1018 | int auto_number; | |
1019 | char *default_attach; | |
1020 | struct string_list extra_hdr; | |
1021 | struct string_list extra_to; | |
1022 | struct string_list extra_cc; | |
1023 | int keep_subject; | |
1024 | int subject_prefix; | |
1025 | struct strbuf sprefix; | |
1026 | char *fmt_patch_suffix; | |
1027 | }; | |
1028 | ||
1029 | static void format_config_init(struct format_config *cfg) | |
1030 | { | |
1031 | memset(cfg, 0, sizeof(*cfg)); | |
1032 | log_config_init(&cfg->log); | |
1033 | cfg->cover_from_description_mode = COVER_FROM_MESSAGE; | |
1034 | cfg->auto_number = 1; | |
1035 | string_list_init_dup(&cfg->extra_hdr); | |
1036 | string_list_init_dup(&cfg->extra_to); | |
1037 | string_list_init_dup(&cfg->extra_cc); | |
1038 | strbuf_init(&cfg->sprefix, 0); | |
1039 | cfg->fmt_patch_suffix = xstrdup(".patch"); | |
1040 | } | |
1041 | ||
1042 | static void format_config_release(struct format_config *cfg) | |
1043 | { | |
1044 | log_config_release(&cfg->log); | |
1045 | free(cfg->base_commit); | |
1046 | free(cfg->from); | |
1047 | free(cfg->signature); | |
1048 | free(cfg->signature_file); | |
1049 | free(cfg->config_output_directory); | |
1050 | free(cfg->default_attach); | |
1051 | string_list_clear(&cfg->extra_hdr, 0); | |
1052 | string_list_clear(&cfg->extra_to, 0); | |
1053 | string_list_clear(&cfg->extra_cc, 0); | |
1054 | strbuf_release(&cfg->sprefix); | |
1055 | free(cfg->fmt_patch_suffix); | |
1056 | } | |
1057 | ||
1058 | static enum cover_from_description parse_cover_from_description(const char *arg) | |
1059 | { | |
1060 | if (!arg || !strcmp(arg, "default")) | |
1061 | return COVER_FROM_MESSAGE; | |
1062 | else if (!strcmp(arg, "none")) | |
1063 | return COVER_FROM_NONE; | |
1064 | else if (!strcmp(arg, "message")) | |
1065 | return COVER_FROM_MESSAGE; | |
1066 | else if (!strcmp(arg, "subject")) | |
1067 | return COVER_FROM_SUBJECT; | |
1068 | else if (!strcmp(arg, "auto")) | |
1069 | return COVER_FROM_AUTO; | |
1070 | else | |
1071 | die(_("%s: invalid cover from description mode"), arg); | |
1072 | } | |
1073 | ||
1074 | static void add_header(struct format_config *cfg, const char *value) | |
1075 | { | |
1076 | struct string_list_item *item; | |
1077 | int len = strlen(value); | |
1078 | while (len && value[len - 1] == '\n') | |
1079 | len--; | |
1080 | ||
1081 | if (!strncasecmp(value, "to: ", 4)) { | |
1082 | item = string_list_append(&cfg->extra_to, value + 4); | |
1083 | len -= 4; | |
1084 | } else if (!strncasecmp(value, "cc: ", 4)) { | |
1085 | item = string_list_append(&cfg->extra_cc, value + 4); | |
1086 | len -= 4; | |
1087 | } else { | |
1088 | item = string_list_append(&cfg->extra_hdr, value); | |
1089 | } | |
1090 | ||
1091 | item->string[len] = '\0'; | |
1092 | } | |
1093 | ||
1094 | static int git_format_config(const char *var, const char *value, | |
1095 | const struct config_context *ctx, void *cb) | |
1096 | { | |
1097 | struct format_config *cfg = cb; | |
1098 | ||
1099 | if (!strcmp(var, "format.headers")) { | |
1100 | if (!value) | |
1101 | die(_("format.headers without value")); | |
1102 | add_header(cfg, value); | |
1103 | return 0; | |
1104 | } | |
1105 | if (!strcmp(var, "format.suffix")) { | |
1106 | FREE_AND_NULL(cfg->fmt_patch_suffix); | |
1107 | return git_config_string(&cfg->fmt_patch_suffix, var, value); | |
1108 | } | |
1109 | if (!strcmp(var, "format.to")) { | |
1110 | if (!value) | |
1111 | return config_error_nonbool(var); | |
1112 | string_list_append(&cfg->extra_to, value); | |
1113 | return 0; | |
1114 | } | |
1115 | if (!strcmp(var, "format.cc")) { | |
1116 | if (!value) | |
1117 | return config_error_nonbool(var); | |
1118 | string_list_append(&cfg->extra_cc, value); | |
1119 | return 0; | |
1120 | } | |
1121 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff") || | |
1122 | !strcmp(var, "color.ui") || !strcmp(var, "diff.submodule")) { | |
1123 | return 0; | |
1124 | } | |
1125 | if (!strcmp(var, "format.numbered")) { | |
1126 | if (value && !strcasecmp(value, "auto")) { | |
1127 | cfg->auto_number = 1; | |
1128 | return 0; | |
1129 | } | |
1130 | cfg->numbered = git_config_bool(var, value); | |
1131 | cfg->auto_number = cfg->auto_number && cfg->numbered; | |
1132 | return 0; | |
1133 | } | |
1134 | if (!strcmp(var, "format.attach")) { | |
1135 | if (value && *value) { | |
1136 | FREE_AND_NULL(cfg->default_attach); | |
1137 | cfg->default_attach = xstrdup(value); | |
1138 | } else if (value && !*value) { | |
1139 | FREE_AND_NULL(cfg->default_attach); | |
1140 | } else { | |
1141 | FREE_AND_NULL(cfg->default_attach); | |
1142 | cfg->default_attach = xstrdup(git_version_string); | |
1143 | } | |
1144 | return 0; | |
1145 | } | |
1146 | if (!strcmp(var, "format.thread")) { | |
1147 | if (value && !strcasecmp(value, "deep")) { | |
1148 | cfg->thread = THREAD_DEEP; | |
1149 | return 0; | |
1150 | } | |
1151 | if (value && !strcasecmp(value, "shallow")) { | |
1152 | cfg->thread = THREAD_SHALLOW; | |
1153 | return 0; | |
1154 | } | |
1155 | cfg->thread = git_config_bool(var, value) ? THREAD_SHALLOW : THREAD_UNSET; | |
1156 | return 0; | |
1157 | } | |
1158 | if (!strcmp(var, "format.signoff")) { | |
1159 | cfg->do_signoff = git_config_bool(var, value); | |
1160 | return 0; | |
1161 | } | |
1162 | if (!strcmp(var, "format.signature")) { | |
1163 | FREE_AND_NULL(cfg->signature); | |
1164 | return git_config_string(&cfg->signature, var, value); | |
1165 | } | |
1166 | if (!strcmp(var, "format.signaturefile")) { | |
1167 | FREE_AND_NULL(cfg->signature_file); | |
1168 | return git_config_pathname(&cfg->signature_file, var, value); | |
1169 | } | |
1170 | if (!strcmp(var, "format.coverletter")) { | |
1171 | if (value && !strcasecmp(value, "auto")) { | |
1172 | cfg->config_cover_letter = COVER_AUTO; | |
1173 | return 0; | |
1174 | } | |
1175 | cfg->config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF; | |
1176 | return 0; | |
1177 | } | |
1178 | if (!strcmp(var, "format.outputdirectory")) { | |
1179 | FREE_AND_NULL(cfg->config_output_directory); | |
1180 | return git_config_string(&cfg->config_output_directory, var, value); | |
1181 | } | |
1182 | if (!strcmp(var, "format.useautobase")) { | |
1183 | if (value && !strcasecmp(value, "whenAble")) { | |
1184 | cfg->auto_base = AUTO_BASE_WHEN_ABLE; | |
1185 | return 0; | |
1186 | } | |
1187 | cfg->auto_base = git_config_bool(var, value) ? AUTO_BASE_ALWAYS : AUTO_BASE_NEVER; | |
1188 | return 0; | |
1189 | } | |
1190 | if (!strcmp(var, "format.from")) { | |
1191 | int b = git_parse_maybe_bool(value); | |
1192 | FREE_AND_NULL(cfg->from); | |
1193 | if (b < 0) | |
1194 | cfg->from = xstrdup(value); | |
1195 | else if (b) | |
1196 | cfg->from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
1197 | return 0; | |
1198 | } | |
1199 | if (!strcmp(var, "format.forceinbodyfrom")) { | |
1200 | force_in_body_from = git_config_bool(var, value); | |
1201 | return 0; | |
1202 | } | |
1203 | if (!strcmp(var, "format.notes")) { | |
1204 | int b = git_parse_maybe_bool(value); | |
1205 | if (b < 0) | |
1206 | enable_ref_display_notes(&cfg->notes_opt, &cfg->show_notes, value); | |
1207 | else if (b) | |
1208 | enable_default_display_notes(&cfg->notes_opt, &cfg->show_notes); | |
1209 | else | |
1210 | disable_display_notes(&cfg->notes_opt, &cfg->show_notes); | |
1211 | return 0; | |
1212 | } | |
1213 | if (!strcmp(var, "format.coverfromdescription")) { | |
1214 | cfg->cover_from_description_mode = parse_cover_from_description(value); | |
1215 | return 0; | |
1216 | } | |
1217 | if (!strcmp(var, "format.mboxrd")) { | |
1218 | stdout_mboxrd = git_config_bool(var, value); | |
1219 | return 0; | |
1220 | } | |
1221 | if (!strcmp(var, "format.noprefix")) { | |
1222 | format_no_prefix = 1; | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * ignore some porcelain config which would otherwise be parsed by | |
1228 | * git_diff_ui_config(), via git_log_config(); we can't just avoid | |
1229 | * diff_ui_config completely, because we do care about some ui options | |
1230 | * like color. | |
1231 | */ | |
1232 | if (!strcmp(var, "diff.noprefix")) | |
1233 | return 0; | |
1234 | ||
1235 | return git_log_config(var, value, ctx, &cfg->log); | |
1236 | } | |
1237 | ||
1238 | static const char *output_directory = NULL; | |
1239 | static int outdir_offset; | |
1240 | ||
1241 | static int open_next_file(struct commit *commit, const char *subject, | |
1242 | struct rev_info *rev, int quiet) | |
1243 | { | |
1244 | struct strbuf filename = STRBUF_INIT; | |
1245 | ||
1246 | if (output_directory) { | |
1247 | strbuf_addstr(&filename, output_directory); | |
1248 | strbuf_complete(&filename, '/'); | |
1249 | } | |
1250 | ||
1251 | if (rev->numbered_files) | |
1252 | strbuf_addf(&filename, "%d", rev->nr); | |
1253 | else if (commit) | |
1254 | fmt_output_commit(&filename, commit, rev); | |
1255 | else | |
1256 | fmt_output_subject(&filename, subject, rev); | |
1257 | ||
1258 | if (!quiet) | |
1259 | printf("%s\n", filename.buf + outdir_offset); | |
1260 | ||
1261 | if (!(rev->diffopt.file = fopen(filename.buf, "w"))) { | |
1262 | error_errno(_("cannot open patch file %s"), filename.buf); | |
1263 | strbuf_release(&filename); | |
1264 | return -1; | |
1265 | } | |
1266 | ||
1267 | strbuf_release(&filename); | |
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids) | |
1272 | { | |
1273 | struct rev_info check_rev; | |
1274 | struct commit *commit, *c1, *c2; | |
1275 | struct object *o1, *o2; | |
1276 | unsigned flags1, flags2; | |
1277 | ||
1278 | if (rev->pending.nr != 2) | |
1279 | die(_("need exactly one range")); | |
1280 | ||
1281 | o1 = rev->pending.objects[0].item; | |
1282 | o2 = rev->pending.objects[1].item; | |
1283 | flags1 = o1->flags; | |
1284 | flags2 = o2->flags; | |
1285 | c1 = lookup_commit_reference(the_repository, &o1->oid); | |
1286 | c2 = lookup_commit_reference(the_repository, &o2->oid); | |
1287 | ||
1288 | if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING)) | |
1289 | die(_("not a range")); | |
1290 | ||
1291 | init_patch_ids(the_repository, ids); | |
1292 | ||
1293 | /* given a range a..b get all patch ids for b..a */ | |
1294 | repo_init_revisions(the_repository, &check_rev, rev->prefix); | |
1295 | check_rev.max_parents = 1; | |
1296 | o1->flags ^= UNINTERESTING; | |
1297 | o2->flags ^= UNINTERESTING; | |
1298 | add_pending_object(&check_rev, o1, "o1"); | |
1299 | add_pending_object(&check_rev, o2, "o2"); | |
1300 | if (prepare_revision_walk(&check_rev)) | |
1301 | die(_("revision walk setup failed")); | |
1302 | ||
1303 | while ((commit = get_revision(&check_rev)) != NULL) { | |
1304 | add_commit_patch_id(commit, ids); | |
1305 | } | |
1306 | ||
1307 | /* reset for next revision walk */ | |
1308 | clear_commit_marks(c1, SEEN | UNINTERESTING | SHOWN | ADDED); | |
1309 | clear_commit_marks(c2, SEEN | UNINTERESTING | SHOWN | ADDED); | |
1310 | o1->flags = flags1; | |
1311 | o2->flags = flags2; | |
1312 | } | |
1313 | ||
1314 | static void gen_message_id(struct rev_info *info, const char *base) | |
1315 | { | |
1316 | struct strbuf buf = STRBUF_INIT; | |
1317 | strbuf_addf(&buf, "%s.%"PRItime".git.%s", base, | |
1318 | (timestamp_t) time(NULL), | |
1319 | git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT)); | |
1320 | info->message_id = strbuf_detach(&buf, NULL); | |
1321 | } | |
1322 | ||
1323 | static void print_signature(const char *signature, FILE *file) | |
1324 | { | |
1325 | if (!signature || !*signature) | |
1326 | return; | |
1327 | ||
1328 | fprintf(file, "-- \n%s", signature); | |
1329 | if (signature[strlen(signature)-1] != '\n') | |
1330 | putc('\n', file); | |
1331 | putc('\n', file); | |
1332 | } | |
1333 | ||
1334 | static char *find_branch_name(struct rev_info *rev) | |
1335 | { | |
1336 | struct object_id branch_oid; | |
1337 | const struct object_id *tip_oid; | |
1338 | const char *ref, *v; | |
1339 | char *full_ref, *branch = NULL; | |
1340 | int interesting_found = 0; | |
1341 | size_t idx; | |
1342 | ||
1343 | for (size_t i = 0; i < rev->cmdline.nr; i++) { | |
1344 | if (rev->cmdline.rev[i].flags & UNINTERESTING) | |
1345 | continue; | |
1346 | if (interesting_found) | |
1347 | return NULL; | |
1348 | interesting_found = 1; | |
1349 | idx = i; | |
1350 | } | |
1351 | if (!interesting_found) | |
1352 | return NULL; | |
1353 | ref = rev->cmdline.rev[idx].name; | |
1354 | tip_oid = &rev->cmdline.rev[idx].item->oid; | |
1355 | if (repo_dwim_ref(the_repository, ref, strlen(ref), &branch_oid, | |
1356 | &full_ref, 0) && | |
1357 | skip_prefix(full_ref, "refs/heads/", &v) && | |
1358 | oideq(tip_oid, &branch_oid)) | |
1359 | branch = xstrdup(v); | |
1360 | free(full_ref); | |
1361 | return branch; | |
1362 | } | |
1363 | ||
1364 | static void show_diffstat(struct rev_info *rev, | |
1365 | struct commit *origin, struct commit *head) | |
1366 | { | |
1367 | struct diff_options opts; | |
1368 | ||
1369 | memcpy(&opts, &rev->diffopt, sizeof(opts)); | |
1370 | opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
1371 | diff_setup_done(&opts); | |
1372 | ||
1373 | diff_tree_oid(get_commit_tree_oid(origin), | |
1374 | get_commit_tree_oid(head), | |
1375 | "", &opts); | |
1376 | diffcore_std(&opts); | |
1377 | diff_flush(&opts); | |
1378 | ||
1379 | fprintf(rev->diffopt.file, "\n"); | |
1380 | } | |
1381 | ||
1382 | static void read_desc_file(struct strbuf *buf, const char *desc_file) | |
1383 | { | |
1384 | if (strbuf_read_file(buf, desc_file, 0) < 0) | |
1385 | die_errno(_("unable to read branch description file '%s'"), | |
1386 | desc_file); | |
1387 | } | |
1388 | ||
1389 | static void prepare_cover_text(struct pretty_print_context *pp, | |
1390 | const char *description_file, | |
1391 | const char *branch_name, | |
1392 | struct strbuf *sb, | |
1393 | const char *encoding, | |
1394 | int need_8bit_cte, | |
1395 | const struct format_config *cfg) | |
1396 | { | |
1397 | const char *subject = "*** SUBJECT HERE ***"; | |
1398 | const char *body = "*** BLURB HERE ***"; | |
1399 | struct strbuf description_sb = STRBUF_INIT; | |
1400 | struct strbuf subject_sb = STRBUF_INIT; | |
1401 | ||
1402 | if (cfg->cover_from_description_mode == COVER_FROM_NONE) | |
1403 | goto do_pp; | |
1404 | ||
1405 | if (description_file && *description_file) | |
1406 | read_desc_file(&description_sb, description_file); | |
1407 | else if (branch_name && *branch_name) | |
1408 | read_branch_desc(&description_sb, branch_name); | |
1409 | if (!description_sb.len) | |
1410 | goto do_pp; | |
1411 | ||
1412 | if (cfg->cover_from_description_mode == COVER_FROM_SUBJECT || | |
1413 | cfg->cover_from_description_mode == COVER_FROM_AUTO) | |
1414 | body = format_subject(&subject_sb, description_sb.buf, " "); | |
1415 | ||
1416 | if (cfg->cover_from_description_mode == COVER_FROM_MESSAGE || | |
1417 | (cfg->cover_from_description_mode == COVER_FROM_AUTO && | |
1418 | subject_sb.len > COVER_FROM_AUTO_MAX_SUBJECT_LEN)) | |
1419 | body = description_sb.buf; | |
1420 | else | |
1421 | subject = subject_sb.buf; | |
1422 | ||
1423 | do_pp: | |
1424 | pp_email_subject(pp, &subject, sb, encoding, need_8bit_cte); | |
1425 | pp_remainder(pp, &body, sb, 0); | |
1426 | ||
1427 | strbuf_release(&description_sb); | |
1428 | strbuf_release(&subject_sb); | |
1429 | } | |
1430 | ||
1431 | static int get_notes_refs(struct string_list_item *item, void *arg) | |
1432 | { | |
1433 | strvec_pushf(arg, "--notes=%s", item->string); | |
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | static void get_notes_args(struct strvec *arg, struct rev_info *rev) | |
1438 | { | |
1439 | if (!rev->show_notes) { | |
1440 | strvec_push(arg, "--no-notes"); | |
1441 | } else if (rev->notes_opt.use_default_notes > 0 || | |
1442 | (rev->notes_opt.use_default_notes == -1 && | |
1443 | !rev->notes_opt.extra_notes_refs.nr)) { | |
1444 | strvec_push(arg, "--notes"); | |
1445 | } else { | |
1446 | for_each_string_list(&rev->notes_opt.extra_notes_refs, get_notes_refs, arg); | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | static void make_cover_letter(struct rev_info *rev, int use_separate_file, | |
1451 | struct commit *origin, | |
1452 | int nr, struct commit **list, | |
1453 | const char *description_file, | |
1454 | const char *branch_name, | |
1455 | int quiet, | |
1456 | const struct format_config *cfg) | |
1457 | { | |
1458 | const char *committer; | |
1459 | struct shortlog log; | |
1460 | struct strbuf sb = STRBUF_INIT; | |
1461 | int i; | |
1462 | const char *encoding = "UTF-8"; | |
1463 | int need_8bit_cte = 0; | |
1464 | struct pretty_print_context pp = {0}; | |
1465 | struct commit *head = list[0]; | |
1466 | char *to_free = NULL; | |
1467 | ||
1468 | if (!cmit_fmt_is_mail(rev->commit_format)) | |
1469 | die(_("cover letter needs email format")); | |
1470 | ||
1471 | committer = git_committer_info(0); | |
1472 | ||
1473 | if (use_separate_file && | |
1474 | open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet)) | |
1475 | die(_("failed to create cover-letter file")); | |
1476 | ||
1477 | log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0); | |
1478 | ||
1479 | for (i = 0; !need_8bit_cte && i < nr; i++) { | |
1480 | const char *buf = repo_get_commit_buffer(the_repository, | |
1481 | list[i], NULL); | |
1482 | if (has_non_ascii(buf)) | |
1483 | need_8bit_cte = 1; | |
1484 | repo_unuse_commit_buffer(the_repository, list[i], buf); | |
1485 | } | |
1486 | ||
1487 | if (!branch_name) | |
1488 | branch_name = to_free = find_branch_name(rev); | |
1489 | ||
1490 | pp.fmt = CMIT_FMT_EMAIL; | |
1491 | pp.date_mode.type = DATE_RFC2822; | |
1492 | pp.rev = rev; | |
1493 | pp.encode_email_headers = rev->encode_email_headers; | |
1494 | pp_user_info(&pp, NULL, &sb, committer, encoding); | |
1495 | prepare_cover_text(&pp, description_file, branch_name, &sb, | |
1496 | encoding, need_8bit_cte, cfg); | |
1497 | fprintf(rev->diffopt.file, "%s\n", sb.buf); | |
1498 | ||
1499 | free(to_free); | |
1500 | free(pp.after_subject); | |
1501 | strbuf_release(&sb); | |
1502 | ||
1503 | shortlog_init(&log); | |
1504 | log.wrap_lines = 1; | |
1505 | log.wrap = MAIL_DEFAULT_WRAP; | |
1506 | log.in1 = 2; | |
1507 | log.in2 = 4; | |
1508 | log.file = rev->diffopt.file; | |
1509 | log.groups = SHORTLOG_GROUP_AUTHOR; | |
1510 | shortlog_finish_setup(&log); | |
1511 | for (i = 0; i < nr; i++) | |
1512 | shortlog_add_commit(&log, list[i]); | |
1513 | ||
1514 | shortlog_output(&log); | |
1515 | ||
1516 | /* We can only do diffstat with a unique reference point */ | |
1517 | if (origin) | |
1518 | show_diffstat(rev, origin, head); | |
1519 | ||
1520 | if (rev->idiff_oid1) { | |
1521 | fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title); | |
1522 | show_interdiff(rev->idiff_oid1, rev->idiff_oid2, 0, | |
1523 | &rev->diffopt); | |
1524 | } | |
1525 | ||
1526 | if (rev->rdiff1) { | |
1527 | /* | |
1528 | * Pass minimum required diff-options to range-diff; others | |
1529 | * can be added later if deemed desirable. | |
1530 | */ | |
1531 | struct diff_options opts; | |
1532 | struct strvec other_arg = STRVEC_INIT; | |
1533 | struct range_diff_options range_diff_opts = { | |
1534 | .creation_factor = rev->creation_factor, | |
1535 | .dual_color = 1, | |
1536 | .diffopt = &opts, | |
1537 | .other_arg = &other_arg | |
1538 | }; | |
1539 | ||
1540 | repo_diff_setup(the_repository, &opts); | |
1541 | opts.file = rev->diffopt.file; | |
1542 | opts.use_color = rev->diffopt.use_color; | |
1543 | diff_setup_done(&opts); | |
1544 | fprintf_ln(rev->diffopt.file, "%s", rev->rdiff_title); | |
1545 | get_notes_args(&other_arg, rev); | |
1546 | show_range_diff(rev->rdiff1, rev->rdiff2, &range_diff_opts); | |
1547 | strvec_clear(&other_arg); | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | static char *clean_message_id(const char *msg_id) | |
1552 | { | |
1553 | char ch; | |
1554 | const char *a, *z, *m; | |
1555 | ||
1556 | m = msg_id; | |
1557 | while ((ch = *m) && (isspace(ch) || (ch == '<'))) | |
1558 | m++; | |
1559 | a = m; | |
1560 | z = NULL; | |
1561 | while ((ch = *m)) { | |
1562 | if (!isspace(ch) && (ch != '>')) | |
1563 | z = m; | |
1564 | m++; | |
1565 | } | |
1566 | if (!z) | |
1567 | die(_("insane in-reply-to: %s"), msg_id); | |
1568 | if (++z == m) | |
1569 | return xstrdup(a); | |
1570 | return xmemdupz(a, z - a); | |
1571 | } | |
1572 | ||
1573 | static const char *set_outdir(const char *prefix, const char *output_directory) | |
1574 | { | |
1575 | if (output_directory && is_absolute_path(output_directory)) | |
1576 | return output_directory; | |
1577 | ||
1578 | if (!prefix || !*prefix) { | |
1579 | if (output_directory) | |
1580 | return output_directory; | |
1581 | /* The user did not explicitly ask for "./" */ | |
1582 | outdir_offset = 2; | |
1583 | return "./"; | |
1584 | } | |
1585 | ||
1586 | outdir_offset = strlen(prefix); | |
1587 | if (!output_directory) | |
1588 | return prefix; | |
1589 | ||
1590 | return prefix_filename(prefix, output_directory); | |
1591 | } | |
1592 | ||
1593 | static const char * const builtin_format_patch_usage[] = { | |
1594 | N_("git format-patch [<options>] [<since> | <revision-range>]"), | |
1595 | NULL | |
1596 | }; | |
1597 | ||
1598 | struct keep_callback_data { | |
1599 | struct format_config *cfg; | |
1600 | struct rev_info *revs; | |
1601 | }; | |
1602 | ||
1603 | static int keep_callback(const struct option *opt, const char *arg, int unset) | |
1604 | { | |
1605 | struct keep_callback_data *data = opt->value; | |
1606 | BUG_ON_OPT_NEG(unset); | |
1607 | BUG_ON_OPT_ARG(arg); | |
1608 | data->revs->total = -1; | |
1609 | data->cfg->keep_subject = 1; | |
1610 | return 0; | |
1611 | } | |
1612 | ||
1613 | static int subject_prefix_callback(const struct option *opt, const char *arg, | |
1614 | int unset) | |
1615 | { | |
1616 | struct format_config *cfg = opt->value; | |
1617 | ||
1618 | BUG_ON_OPT_NEG(unset); | |
1619 | cfg->subject_prefix = 1; | |
1620 | strbuf_reset(&cfg->sprefix); | |
1621 | strbuf_addstr(&cfg->sprefix, arg); | |
1622 | return 0; | |
1623 | } | |
1624 | ||
1625 | static int rfc_callback(const struct option *opt, const char *arg, | |
1626 | int unset) | |
1627 | { | |
1628 | const char **rfc = opt->value; | |
1629 | ||
1630 | *rfc = opt->value; | |
1631 | if (unset) | |
1632 | *rfc = NULL; | |
1633 | else | |
1634 | *rfc = arg ? arg : "RFC"; | |
1635 | return 0; | |
1636 | } | |
1637 | ||
1638 | static int numbered_callback(const struct option *opt, const char *arg, | |
1639 | int unset) | |
1640 | { | |
1641 | struct format_config *cfg = opt->value; | |
1642 | BUG_ON_OPT_ARG(arg); | |
1643 | cfg->numbered = cfg->numbered_cmdline_opt = unset ? 0 : 1; | |
1644 | if (unset) | |
1645 | cfg->auto_number = 0; | |
1646 | return 0; | |
1647 | } | |
1648 | ||
1649 | static int no_numbered_callback(const struct option *opt, const char *arg, | |
1650 | int unset) | |
1651 | { | |
1652 | BUG_ON_OPT_NEG(unset); | |
1653 | return numbered_callback(opt, arg, 1); | |
1654 | } | |
1655 | ||
1656 | static int output_directory_callback(const struct option *opt, const char *arg, | |
1657 | int unset) | |
1658 | { | |
1659 | const char **dir = (const char **)opt->value; | |
1660 | BUG_ON_OPT_NEG(unset); | |
1661 | if (*dir) | |
1662 | die(_("two output directories?")); | |
1663 | *dir = arg; | |
1664 | return 0; | |
1665 | } | |
1666 | ||
1667 | static int thread_callback(const struct option *opt, const char *arg, int unset) | |
1668 | { | |
1669 | struct format_config *cfg = opt->value; | |
1670 | ||
1671 | if (unset) | |
1672 | cfg->thread = THREAD_UNSET; | |
1673 | else if (!arg || !strcmp(arg, "shallow")) | |
1674 | cfg->thread = THREAD_SHALLOW; | |
1675 | else if (!strcmp(arg, "deep")) | |
1676 | cfg->thread = THREAD_DEEP; | |
1677 | /* | |
1678 | * Please update _git_formatpatch() in git-completion.bash | |
1679 | * when you add new options. | |
1680 | */ | |
1681 | else | |
1682 | return 1; | |
1683 | return 0; | |
1684 | } | |
1685 | ||
1686 | static int attach_callback(const struct option *opt, const char *arg, int unset) | |
1687 | { | |
1688 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1689 | if (unset) | |
1690 | rev->mime_boundary = NULL; | |
1691 | else if (arg) | |
1692 | rev->mime_boundary = arg; | |
1693 | else | |
1694 | rev->mime_boundary = git_version_string; | |
1695 | rev->no_inline = unset ? 0 : 1; | |
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | static int inline_callback(const struct option *opt, const char *arg, int unset) | |
1700 | { | |
1701 | struct rev_info *rev = (struct rev_info *)opt->value; | |
1702 | if (unset) | |
1703 | rev->mime_boundary = NULL; | |
1704 | else if (arg) | |
1705 | rev->mime_boundary = arg; | |
1706 | else | |
1707 | rev->mime_boundary = git_version_string; | |
1708 | rev->no_inline = 0; | |
1709 | return 0; | |
1710 | } | |
1711 | ||
1712 | static int header_callback(const struct option *opt, const char *arg, | |
1713 | int unset) | |
1714 | { | |
1715 | struct format_config *cfg = opt->value; | |
1716 | ||
1717 | if (unset) { | |
1718 | string_list_clear(&cfg->extra_hdr, 0); | |
1719 | string_list_clear(&cfg->extra_to, 0); | |
1720 | string_list_clear(&cfg->extra_cc, 0); | |
1721 | } else { | |
1722 | add_header(cfg, arg); | |
1723 | } | |
1724 | return 0; | |
1725 | } | |
1726 | ||
1727 | static int from_callback(const struct option *opt, const char *arg, int unset) | |
1728 | { | |
1729 | char **from = opt->value; | |
1730 | ||
1731 | free(*from); | |
1732 | ||
1733 | if (unset) | |
1734 | *from = NULL; | |
1735 | else if (arg) | |
1736 | *from = xstrdup(arg); | |
1737 | else | |
1738 | *from = xstrdup(git_committer_info(IDENT_NO_DATE)); | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | static int base_callback(const struct option *opt, const char *arg, int unset) | |
1743 | { | |
1744 | struct format_config *cfg = opt->value; | |
1745 | ||
1746 | if (unset) { | |
1747 | cfg->auto_base = AUTO_BASE_NEVER; | |
1748 | FREE_AND_NULL(cfg->base_commit); | |
1749 | } else if (!strcmp(arg, "auto")) { | |
1750 | cfg->auto_base = AUTO_BASE_ALWAYS; | |
1751 | FREE_AND_NULL(cfg->base_commit); | |
1752 | } else { | |
1753 | cfg->auto_base = AUTO_BASE_NEVER; | |
1754 | cfg->base_commit = xstrdup(arg); | |
1755 | } | |
1756 | return 0; | |
1757 | } | |
1758 | ||
1759 | struct base_tree_info { | |
1760 | struct object_id base_commit; | |
1761 | int nr_patch_id, alloc_patch_id; | |
1762 | struct object_id *patch_id; | |
1763 | }; | |
1764 | ||
1765 | static struct commit *get_base_commit(const struct format_config *cfg, | |
1766 | struct commit **list, | |
1767 | size_t total) | |
1768 | { | |
1769 | struct commit *base = NULL; | |
1770 | struct commit **rev; | |
1771 | int auto_select, die_on_failure, ret; | |
1772 | size_t i = 0, rev_nr = 0; | |
1773 | ||
1774 | switch (cfg->auto_base) { | |
1775 | case AUTO_BASE_NEVER: | |
1776 | if (cfg->base_commit) { | |
1777 | auto_select = 0; | |
1778 | die_on_failure = 1; | |
1779 | } else { | |
1780 | /* no base information is requested */ | |
1781 | return NULL; | |
1782 | } | |
1783 | break; | |
1784 | case AUTO_BASE_ALWAYS: | |
1785 | case AUTO_BASE_WHEN_ABLE: | |
1786 | if (cfg->base_commit) { | |
1787 | BUG("requested automatic base selection but a commit was provided"); | |
1788 | } else { | |
1789 | auto_select = 1; | |
1790 | die_on_failure = cfg->auto_base == AUTO_BASE_ALWAYS; | |
1791 | } | |
1792 | break; | |
1793 | default: | |
1794 | BUG("unexpected automatic base selection method"); | |
1795 | } | |
1796 | ||
1797 | if (!auto_select) { | |
1798 | base = lookup_commit_reference_by_name(cfg->base_commit); | |
1799 | if (!base) | |
1800 | die(_("unknown commit %s"), cfg->base_commit); | |
1801 | } else { | |
1802 | struct branch *curr_branch = branch_get(NULL); | |
1803 | const char *upstream = branch_get_upstream(curr_branch, NULL); | |
1804 | if (upstream) { | |
1805 | struct commit_list *base_list = NULL; | |
1806 | struct commit *commit; | |
1807 | struct object_id oid; | |
1808 | ||
1809 | if (repo_get_oid(the_repository, upstream, &oid)) { | |
1810 | if (die_on_failure) | |
1811 | die(_("failed to resolve '%s' as a valid ref"), upstream); | |
1812 | else | |
1813 | return NULL; | |
1814 | } | |
1815 | commit = lookup_commit_or_die(&oid, "upstream base"); | |
1816 | if (repo_get_merge_bases_many(the_repository, | |
1817 | commit, total, | |
1818 | list, | |
1819 | &base_list) < 0 || | |
1820 | /* There should be one and only one merge base. */ | |
1821 | !base_list || base_list->next) { | |
1822 | if (die_on_failure) { | |
1823 | die(_("could not find exact merge base")); | |
1824 | } else { | |
1825 | free_commit_list(base_list); | |
1826 | return NULL; | |
1827 | } | |
1828 | } | |
1829 | base = base_list->item; | |
1830 | free_commit_list(base_list); | |
1831 | } else { | |
1832 | if (die_on_failure) | |
1833 | die(_("failed to get upstream, if you want to record base commit automatically,\n" | |
1834 | "please use git branch --set-upstream-to to track a remote branch.\n" | |
1835 | "Or you could specify base commit by --base=<base-commit-id> manually")); | |
1836 | else | |
1837 | return NULL; | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | ALLOC_ARRAY(rev, total); | |
1842 | for (i = 0; i < total; i++) | |
1843 | rev[i] = list[i]; | |
1844 | ||
1845 | rev_nr = total; | |
1846 | /* | |
1847 | * Get merge base through pair-wise computations | |
1848 | * and store it in rev[0]. | |
1849 | */ | |
1850 | while (rev_nr > 1) { | |
1851 | for (i = 0; i < rev_nr / 2; i++) { | |
1852 | struct commit_list *merge_base = NULL; | |
1853 | if (repo_get_merge_bases(the_repository, | |
1854 | rev[2 * i], | |
1855 | rev[2 * i + 1], &merge_base) < 0 || | |
1856 | !merge_base || merge_base->next) { | |
1857 | if (die_on_failure) { | |
1858 | die(_("failed to find exact merge base")); | |
1859 | } else { | |
1860 | free_commit_list(merge_base); | |
1861 | free(rev); | |
1862 | return NULL; | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | rev[i] = merge_base->item; | |
1867 | free_commit_list(merge_base); | |
1868 | } | |
1869 | ||
1870 | if (rev_nr % 2) | |
1871 | rev[i] = rev[2 * i]; | |
1872 | rev_nr = DIV_ROUND_UP(rev_nr, 2); | |
1873 | } | |
1874 | ||
1875 | ret = repo_in_merge_bases(the_repository, base, rev[0]); | |
1876 | if (ret < 0) | |
1877 | exit(128); | |
1878 | if (!ret) { | |
1879 | if (die_on_failure) { | |
1880 | die(_("base commit should be the ancestor of revision list")); | |
1881 | } else { | |
1882 | free(rev); | |
1883 | return NULL; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | for (i = 0; i < total; i++) { | |
1888 | if (base == list[i]) { | |
1889 | if (die_on_failure) { | |
1890 | die(_("base commit shouldn't be in revision list")); | |
1891 | } else { | |
1892 | free(rev); | |
1893 | return NULL; | |
1894 | } | |
1895 | } | |
1896 | } | |
1897 | ||
1898 | free(rev); | |
1899 | return base; | |
1900 | } | |
1901 | ||
1902 | define_commit_slab(commit_base, int); | |
1903 | ||
1904 | static void prepare_bases(struct base_tree_info *bases, | |
1905 | struct commit *base, | |
1906 | struct commit **list, | |
1907 | size_t total) | |
1908 | { | |
1909 | struct commit *commit; | |
1910 | struct rev_info revs; | |
1911 | struct diff_options diffopt; | |
1912 | struct commit_base commit_base; | |
1913 | ||
1914 | if (!base) | |
1915 | return; | |
1916 | ||
1917 | init_commit_base(&commit_base); | |
1918 | repo_diff_setup(the_repository, &diffopt); | |
1919 | diffopt.flags.recursive = 1; | |
1920 | diff_setup_done(&diffopt); | |
1921 | ||
1922 | oidcpy(&bases->base_commit, &base->object.oid); | |
1923 | ||
1924 | repo_init_revisions(the_repository, &revs, NULL); | |
1925 | revs.max_parents = 1; | |
1926 | revs.topo_order = 1; | |
1927 | for (size_t i = 0; i < total; i++) { | |
1928 | list[i]->object.flags &= ~UNINTERESTING; | |
1929 | add_pending_object(&revs, &list[i]->object, "rev_list"); | |
1930 | *commit_base_at(&commit_base, list[i]) = 1; | |
1931 | } | |
1932 | base->object.flags |= UNINTERESTING; | |
1933 | add_pending_object(&revs, &base->object, "base"); | |
1934 | ||
1935 | if (prepare_revision_walk(&revs)) | |
1936 | die(_("revision walk setup failed")); | |
1937 | /* | |
1938 | * Traverse the commits list, get prerequisite patch ids | |
1939 | * and stuff them in bases structure. | |
1940 | */ | |
1941 | while ((commit = get_revision(&revs)) != NULL) { | |
1942 | struct object_id oid; | |
1943 | struct object_id *patch_id; | |
1944 | if (*commit_base_at(&commit_base, commit)) | |
1945 | continue; | |
1946 | if (commit_patch_id(commit, &diffopt, &oid, 0)) | |
1947 | die(_("cannot get patch id")); | |
1948 | ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id); | |
1949 | patch_id = bases->patch_id + bases->nr_patch_id; | |
1950 | oidcpy(patch_id, &oid); | |
1951 | bases->nr_patch_id++; | |
1952 | } | |
1953 | clear_commit_base(&commit_base); | |
1954 | } | |
1955 | ||
1956 | static void print_bases(struct base_tree_info *bases, FILE *file) | |
1957 | { | |
1958 | int i; | |
1959 | ||
1960 | /* Only do this once, either for the cover or for the first one */ | |
1961 | if (is_null_oid(&bases->base_commit)) | |
1962 | return; | |
1963 | ||
1964 | /* Show the base commit */ | |
1965 | fprintf(file, "\nbase-commit: %s\n", oid_to_hex(&bases->base_commit)); | |
1966 | ||
1967 | /* Show the prerequisite patches */ | |
1968 | for (i = bases->nr_patch_id - 1; i >= 0; i--) | |
1969 | fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i])); | |
1970 | ||
1971 | free(bases->patch_id); | |
1972 | bases->nr_patch_id = 0; | |
1973 | bases->alloc_patch_id = 0; | |
1974 | oidclr(&bases->base_commit, the_repository->hash_algo); | |
1975 | } | |
1976 | ||
1977 | static const char *diff_title(struct strbuf *sb, | |
1978 | const char *reroll_count, | |
1979 | const char *generic, | |
1980 | const char *rerolled) | |
1981 | { | |
1982 | int v; | |
1983 | ||
1984 | /* RFC may be v0, so allow -v1 to diff against v0 */ | |
1985 | if (reroll_count && !strtol_i(reroll_count, 10, &v) && | |
1986 | v >= 1) | |
1987 | strbuf_addf(sb, rerolled, v - 1); | |
1988 | else | |
1989 | strbuf_addstr(sb, generic); | |
1990 | return sb->buf; | |
1991 | } | |
1992 | ||
1993 | static void infer_range_diff_ranges(struct strbuf *r1, | |
1994 | struct strbuf *r2, | |
1995 | const char *prev, | |
1996 | struct commit *origin, | |
1997 | struct commit *head) | |
1998 | { | |
1999 | const char *head_oid = oid_to_hex(&head->object.oid); | |
2000 | int prev_is_range = is_range_diff_range(prev); | |
2001 | ||
2002 | if (prev_is_range) | |
2003 | strbuf_addstr(r1, prev); | |
2004 | else | |
2005 | strbuf_addf(r1, "%s..%s", head_oid, prev); | |
2006 | ||
2007 | if (origin) | |
2008 | strbuf_addf(r2, "%s..%s", oid_to_hex(&origin->object.oid), head_oid); | |
2009 | else if (prev_is_range) | |
2010 | die(_("failed to infer range-diff origin of current series")); | |
2011 | else { | |
2012 | warning(_("using '%s' as range-diff origin of current series"), prev); | |
2013 | strbuf_addf(r2, "%s..%s", prev, head_oid); | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | int cmd_format_patch(int argc, | |
2018 | const char **argv, | |
2019 | const char *prefix, | |
2020 | struct repository *repo UNUSED) | |
2021 | { | |
2022 | struct format_config cfg; | |
2023 | struct commit *commit; | |
2024 | struct commit **list = NULL; | |
2025 | struct rev_info rev; | |
2026 | char *to_free = NULL; | |
2027 | struct setup_revision_opt s_r_opt; | |
2028 | size_t nr = 0, total, i; | |
2029 | int use_stdout = 0; | |
2030 | int start_number = -1; | |
2031 | int just_numbers = 0; | |
2032 | int ignore_if_in_upstream = 0; | |
2033 | int cover_letter = -1; | |
2034 | int boundary_count = 0; | |
2035 | int no_binary_diff = 0; | |
2036 | int zero_commit = 0; | |
2037 | struct commit *origin = NULL; | |
2038 | const char *in_reply_to = NULL; | |
2039 | struct patch_ids ids; | |
2040 | struct strbuf buf = STRBUF_INIT; | |
2041 | int use_patch_format = 0; | |
2042 | int quiet = 0; | |
2043 | const char *reroll_count = NULL; | |
2044 | char *cover_from_description_arg = NULL; | |
2045 | char *description_file = NULL; | |
2046 | char *branch_name = NULL; | |
2047 | struct base_tree_info bases; | |
2048 | struct commit *base; | |
2049 | int show_progress = 0; | |
2050 | struct progress *progress = NULL; | |
2051 | struct oid_array idiff_prev = OID_ARRAY_INIT; | |
2052 | struct strbuf idiff_title = STRBUF_INIT; | |
2053 | const char *rdiff_prev = NULL; | |
2054 | struct strbuf rdiff1 = STRBUF_INIT; | |
2055 | struct strbuf rdiff2 = STRBUF_INIT; | |
2056 | struct strbuf rdiff_title = STRBUF_INIT; | |
2057 | const char *rfc = NULL; | |
2058 | int creation_factor = -1; | |
2059 | const char *signature = git_version_string; | |
2060 | char *signature_to_free = NULL; | |
2061 | char *signature_file_arg = NULL; | |
2062 | struct keep_callback_data keep_callback_data = { | |
2063 | .cfg = &cfg, | |
2064 | .revs = &rev, | |
2065 | }; | |
2066 | const char *fmt_patch_suffix = NULL; | |
2067 | ||
2068 | const struct option builtin_format_patch_options[] = { | |
2069 | OPT_CALLBACK_F('n', "numbered", &cfg, NULL, | |
2070 | N_("use [PATCH n/m] even with a single patch"), | |
2071 | PARSE_OPT_NOARG, numbered_callback), | |
2072 | OPT_CALLBACK_F('N', "no-numbered", &cfg, NULL, | |
2073 | N_("use [PATCH] even with multiple patches"), | |
2074 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, no_numbered_callback), | |
2075 | OPT_BOOL('s', "signoff", &cfg.do_signoff, N_("add a Signed-off-by trailer")), | |
2076 | OPT_BOOL(0, "stdout", &use_stdout, | |
2077 | N_("print patches to standard out")), | |
2078 | OPT_BOOL(0, "cover-letter", &cover_letter, | |
2079 | N_("generate a cover letter")), | |
2080 | OPT_BOOL(0, "numbered-files", &just_numbers, | |
2081 | N_("use simple number sequence for output file names")), | |
2082 | OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"), | |
2083 | N_("use <sfx> instead of '.patch'")), | |
2084 | OPT_INTEGER(0, "start-number", &start_number, | |
2085 | N_("start numbering patches at <n> instead of 1")), | |
2086 | OPT_STRING('v', "reroll-count", &reroll_count, N_("reroll-count"), | |
2087 | N_("mark the series as Nth re-roll")), | |
2088 | OPT_INTEGER(0, "filename-max-length", &cfg.log.fmt_patch_name_max, | |
2089 | N_("max length of output filename")), | |
2090 | OPT_CALLBACK_F(0, "rfc", &rfc, N_("rfc"), | |
2091 | N_("add <rfc> (default 'RFC') before 'PATCH'"), | |
2092 | PARSE_OPT_OPTARG, rfc_callback), | |
2093 | OPT_STRING(0, "cover-from-description", &cover_from_description_arg, | |
2094 | N_("cover-from-description-mode"), | |
2095 | N_("generate parts of a cover letter based on a branch's description")), | |
2096 | OPT_FILENAME(0, "description-file", &description_file, | |
2097 | N_("use branch description from file")), | |
2098 | OPT_CALLBACK_F(0, "subject-prefix", &cfg, N_("prefix"), | |
2099 | N_("use [<prefix>] instead of [PATCH]"), | |
2100 | PARSE_OPT_NONEG, subject_prefix_callback), | |
2101 | OPT_CALLBACK_F('o', "output-directory", &output_directory, | |
2102 | N_("dir"), N_("store resulting files in <dir>"), | |
2103 | PARSE_OPT_NONEG, output_directory_callback), | |
2104 | OPT_CALLBACK_F('k', "keep-subject", &keep_callback_data, NULL, | |
2105 | N_("don't strip/add [PATCH]"), | |
2106 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback), | |
2107 | OPT_BOOL(0, "no-binary", &no_binary_diff, | |
2108 | N_("don't output binary diffs")), | |
2109 | OPT_BOOL(0, "zero-commit", &zero_commit, | |
2110 | N_("output all-zero hash in From header")), | |
2111 | OPT_BOOL(0, "ignore-if-in-upstream", &ignore_if_in_upstream, | |
2112 | N_("don't include a patch matching a commit upstream")), | |
2113 | OPT_SET_INT_F('p', "no-stat", &use_patch_format, | |
2114 | N_("show patch format instead of default (patch + stat)"), | |
2115 | 1, PARSE_OPT_NONEG), | |
2116 | OPT_GROUP(N_("Messaging")), | |
2117 | OPT_CALLBACK(0, "add-header", &cfg, N_("header"), | |
2118 | N_("add email header"), header_callback), | |
2119 | OPT_STRING_LIST(0, "to", &cfg.extra_to, N_("email"), N_("add To: header")), | |
2120 | OPT_STRING_LIST(0, "cc", &cfg.extra_cc, N_("email"), N_("add Cc: header")), | |
2121 | OPT_CALLBACK_F(0, "from", &cfg.from, N_("ident"), | |
2122 | N_("set From address to <ident> (or committer ident if absent)"), | |
2123 | PARSE_OPT_OPTARG, from_callback), | |
2124 | OPT_STRING(0, "in-reply-to", &in_reply_to, N_("message-id"), | |
2125 | N_("make first mail a reply to <message-id>")), | |
2126 | OPT_CALLBACK_F(0, "attach", &rev, N_("boundary"), | |
2127 | N_("attach the patch"), PARSE_OPT_OPTARG, | |
2128 | attach_callback), | |
2129 | OPT_CALLBACK_F(0, "inline", &rev, N_("boundary"), | |
2130 | N_("inline the patch"), | |
2131 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, | |
2132 | inline_callback), | |
2133 | OPT_CALLBACK_F(0, "thread", &cfg, N_("style"), | |
2134 | N_("enable message threading, styles: shallow, deep"), | |
2135 | PARSE_OPT_OPTARG, thread_callback), | |
2136 | OPT_STRING(0, "signature", &signature, N_("signature"), | |
2137 | N_("add a signature")), | |
2138 | OPT_CALLBACK_F(0, "base", &cfg, N_("base-commit"), | |
2139 | N_("add prerequisite tree info to the patch series"), | |
2140 | 0, base_callback), | |
2141 | OPT_FILENAME(0, "signature-file", &signature_file_arg, | |
2142 | N_("add a signature from a file")), | |
2143 | OPT__QUIET(&quiet, N_("don't print the patch filenames")), | |
2144 | OPT_BOOL(0, "progress", &show_progress, | |
2145 | N_("show progress while generating patches")), | |
2146 | OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"), | |
2147 | N_("show changes against <rev> in cover letter or single patch"), | |
2148 | parse_opt_object_name), | |
2149 | OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"), | |
2150 | N_("show changes against <refspec> in cover letter or single patch")), | |
2151 | OPT_INTEGER(0, "creation-factor", &creation_factor, | |
2152 | N_("percentage by which creation is weighted")), | |
2153 | OPT_BOOL(0, "force-in-body-from", &force_in_body_from, | |
2154 | N_("show in-body From: even if identical to the e-mail header")), | |
2155 | OPT_END() | |
2156 | }; | |
2157 | ||
2158 | format_config_init(&cfg); | |
2159 | init_diff_ui_defaults(); | |
2160 | init_display_notes(&cfg.notes_opt); | |
2161 | git_config(git_format_config, &cfg); | |
2162 | repo_init_revisions(the_repository, &rev, prefix); | |
2163 | git_config(grep_config, &rev.grep_filter); | |
2164 | ||
2165 | rev.show_notes = cfg.show_notes; | |
2166 | memcpy(&rev.notes_opt, &cfg.notes_opt, sizeof(cfg.notes_opt)); | |
2167 | rev.commit_format = CMIT_FMT_EMAIL; | |
2168 | rev.encode_email_headers = cfg.log.default_encode_email_headers; | |
2169 | rev.expand_tabs_in_log_default = 0; | |
2170 | rev.verbose_header = 1; | |
2171 | rev.diff = 1; | |
2172 | rev.max_parents = 1; | |
2173 | rev.diffopt.flags.recursive = 1; | |
2174 | rev.diffopt.no_free = 1; | |
2175 | memset(&s_r_opt, 0, sizeof(s_r_opt)); | |
2176 | s_r_opt.def = "HEAD"; | |
2177 | s_r_opt.revarg_opt = REVARG_COMMITTISH; | |
2178 | ||
2179 | strbuf_addstr(&cfg.sprefix, cfg.log.fmt_patch_subject_prefix); | |
2180 | if (format_no_prefix) | |
2181 | diff_set_noprefix(&rev.diffopt); | |
2182 | ||
2183 | if (cfg.default_attach) { | |
2184 | rev.mime_boundary = cfg.default_attach; | |
2185 | rev.no_inline = 1; | |
2186 | } | |
2187 | ||
2188 | /* | |
2189 | * Parse the arguments before setup_revisions(), or something | |
2190 | * like "git format-patch -o a123 HEAD^.." may fail; a123 is | |
2191 | * possibly a valid SHA1. | |
2192 | */ | |
2193 | argc = parse_options(argc, argv, prefix, builtin_format_patch_options, | |
2194 | builtin_format_patch_usage, | |
2195 | PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | | |
2196 | PARSE_OPT_KEEP_DASHDASH); | |
2197 | ||
2198 | rev.force_in_body_from = force_in_body_from; | |
2199 | ||
2200 | if (!fmt_patch_suffix) | |
2201 | fmt_patch_suffix = cfg.fmt_patch_suffix; | |
2202 | ||
2203 | /* Make sure "0000-$sub.patch" gives non-negative length for $sub */ | |
2204 | if (cfg.log.fmt_patch_name_max <= cast_size_t_to_int(strlen("0000-") + strlen(fmt_patch_suffix))) | |
2205 | cfg.log.fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix); | |
2206 | ||
2207 | if (cover_from_description_arg) | |
2208 | cfg.cover_from_description_mode = parse_cover_from_description(cover_from_description_arg); | |
2209 | ||
2210 | if (rfc && rfc[0]) { | |
2211 | cfg.subject_prefix = 1; | |
2212 | if (rfc[0] == '-') | |
2213 | strbuf_addf(&cfg.sprefix, " %s", rfc + 1); | |
2214 | else | |
2215 | strbuf_insertf(&cfg.sprefix, 0, "%s ", rfc); | |
2216 | } | |
2217 | ||
2218 | if (reroll_count) { | |
2219 | strbuf_addf(&cfg.sprefix, " v%s", reroll_count); | |
2220 | rev.reroll_count = reroll_count; | |
2221 | } | |
2222 | ||
2223 | rev.subject_prefix = cfg.sprefix.buf; | |
2224 | ||
2225 | for (i = 0; i < cfg.extra_hdr.nr; i++) { | |
2226 | strbuf_addstr(&buf, cfg.extra_hdr.items[i].string); | |
2227 | strbuf_addch(&buf, '\n'); | |
2228 | } | |
2229 | ||
2230 | if (cfg.extra_to.nr) | |
2231 | strbuf_addstr(&buf, "To: "); | |
2232 | for (i = 0; i < cfg.extra_to.nr; i++) { | |
2233 | if (i) | |
2234 | strbuf_addstr(&buf, " "); | |
2235 | strbuf_addstr(&buf, cfg.extra_to.items[i].string); | |
2236 | if (i + 1 < cfg.extra_to.nr) | |
2237 | strbuf_addch(&buf, ','); | |
2238 | strbuf_addch(&buf, '\n'); | |
2239 | } | |
2240 | ||
2241 | if (cfg.extra_cc.nr) | |
2242 | strbuf_addstr(&buf, "Cc: "); | |
2243 | for (i = 0; i < cfg.extra_cc.nr; i++) { | |
2244 | if (i) | |
2245 | strbuf_addstr(&buf, " "); | |
2246 | strbuf_addstr(&buf, cfg.extra_cc.items[i].string); | |
2247 | if (i + 1 < cfg.extra_cc.nr) | |
2248 | strbuf_addch(&buf, ','); | |
2249 | strbuf_addch(&buf, '\n'); | |
2250 | } | |
2251 | ||
2252 | rev.extra_headers = to_free = strbuf_detach(&buf, NULL); | |
2253 | ||
2254 | if (cfg.from) { | |
2255 | if (split_ident_line(&rev.from_ident, cfg.from, strlen(cfg.from))) | |
2256 | die(_("invalid ident line: %s"), cfg.from); | |
2257 | } | |
2258 | ||
2259 | if (start_number < 0) | |
2260 | start_number = 1; | |
2261 | ||
2262 | /* | |
2263 | * If numbered is set solely due to format.numbered in config, | |
2264 | * and it would conflict with --keep-subject (-k) from the | |
2265 | * command line, reset "numbered". | |
2266 | */ | |
2267 | if (cfg.numbered && cfg.keep_subject && !cfg.numbered_cmdline_opt) | |
2268 | cfg.numbered = 0; | |
2269 | ||
2270 | if (cfg.numbered && cfg.keep_subject) | |
2271 | die(_("options '%s' and '%s' cannot be used together"), "-n", "-k"); | |
2272 | if (cfg.keep_subject && cfg.subject_prefix) | |
2273 | die(_("options '%s' and '%s' cannot be used together"), "--subject-prefix/--rfc", "-k"); | |
2274 | rev.preserve_subject = cfg.keep_subject; | |
2275 | ||
2276 | argc = setup_revisions(argc, argv, &rev, &s_r_opt); | |
2277 | if (argc > 1) | |
2278 | die(_("unrecognized argument: %s"), argv[1]); | |
2279 | ||
2280 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME) | |
2281 | die(_("--name-only does not make sense")); | |
2282 | if (rev.diffopt.output_format & DIFF_FORMAT_NAME_STATUS) | |
2283 | die(_("--name-status does not make sense")); | |
2284 | if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF) | |
2285 | die(_("--check does not make sense")); | |
2286 | if (rev.remerge_diff) | |
2287 | die(_("--remerge-diff does not make sense")); | |
2288 | ||
2289 | if (!use_patch_format && | |
2290 | (!rev.diffopt.output_format || | |
2291 | rev.diffopt.output_format == DIFF_FORMAT_PATCH)) | |
2292 | rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY; | |
2293 | if (!rev.diffopt.stat_width) | |
2294 | rev.diffopt.stat_width = MAIL_DEFAULT_WRAP; | |
2295 | ||
2296 | /* Always generate a patch */ | |
2297 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
2298 | rev.always_show_header = 1; | |
2299 | ||
2300 | rev.zero_commit = zero_commit; | |
2301 | rev.patch_name_max = cfg.log.fmt_patch_name_max; | |
2302 | ||
2303 | if (!rev.diffopt.flags.text && !no_binary_diff) | |
2304 | rev.diffopt.flags.binary = 1; | |
2305 | ||
2306 | if (rev.show_notes) | |
2307 | load_display_notes(&rev.notes_opt); | |
2308 | ||
2309 | die_for_incompatible_opt3(use_stdout, "--stdout", | |
2310 | rev.diffopt.close_file, "--output", | |
2311 | !!output_directory, "--output-directory"); | |
2312 | ||
2313 | if (use_stdout && stdout_mboxrd) | |
2314 | rev.commit_format = CMIT_FMT_MBOXRD; | |
2315 | ||
2316 | if (use_stdout) { | |
2317 | setup_pager(the_repository); | |
2318 | } else if (!rev.diffopt.close_file) { | |
2319 | int saved; | |
2320 | ||
2321 | if (!output_directory) | |
2322 | output_directory = cfg.config_output_directory; | |
2323 | output_directory = set_outdir(prefix, output_directory); | |
2324 | ||
2325 | if (rev.diffopt.use_color != GIT_COLOR_ALWAYS) | |
2326 | rev.diffopt.use_color = GIT_COLOR_NEVER; | |
2327 | /* | |
2328 | * We consider <outdir> as 'outside of gitdir', therefore avoid | |
2329 | * applying adjust_shared_perm in s-c-l-d. | |
2330 | */ | |
2331 | saved = repo_settings_get_shared_repository(the_repository); | |
2332 | repo_settings_set_shared_repository(the_repository, 0); | |
2333 | switch (safe_create_leading_directories_const(the_repository, output_directory)) { | |
2334 | case SCLD_OK: | |
2335 | case SCLD_EXISTS: | |
2336 | break; | |
2337 | default: | |
2338 | die(_("could not create leading directories " | |
2339 | "of '%s'"), output_directory); | |
2340 | } | |
2341 | repo_settings_set_shared_repository(the_repository, saved); | |
2342 | if (mkdir(output_directory, 0777) < 0 && errno != EEXIST) | |
2343 | die_errno(_("could not create directory '%s'"), | |
2344 | output_directory); | |
2345 | } | |
2346 | ||
2347 | if (rev.pending.nr == 1) { | |
2348 | int check_head = 0; | |
2349 | ||
2350 | if (rev.max_count < 0 && !rev.show_root_diff) { | |
2351 | /* | |
2352 | * This is traditional behaviour of "git format-patch | |
2353 | * origin" that prepares what the origin side still | |
2354 | * does not have. | |
2355 | */ | |
2356 | rev.pending.objects[0].item->flags |= UNINTERESTING; | |
2357 | add_head_to_pending(&rev); | |
2358 | check_head = 1; | |
2359 | } | |
2360 | /* | |
2361 | * Otherwise, it is "format-patch -22 HEAD", and/or | |
2362 | * "format-patch --root HEAD". The user wants | |
2363 | * get_revision() to do the usual traversal. | |
2364 | */ | |
2365 | ||
2366 | if (!strcmp(rev.pending.objects[0].name, "HEAD")) | |
2367 | check_head = 1; | |
2368 | ||
2369 | if (check_head) { | |
2370 | const char *ref, *v; | |
2371 | ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), | |
2372 | "HEAD", | |
2373 | RESOLVE_REF_READING, | |
2374 | NULL, NULL); | |
2375 | if (ref && skip_prefix(ref, "refs/heads/", &v)) | |
2376 | branch_name = xstrdup(v); | |
2377 | else | |
2378 | branch_name = xstrdup(""); /* no branch */ | |
2379 | } | |
2380 | } | |
2381 | ||
2382 | /* | |
2383 | * We cannot move this anywhere earlier because we do want to | |
2384 | * know if --root was given explicitly from the command line. | |
2385 | */ | |
2386 | rev.show_root_diff = 1; | |
2387 | ||
2388 | if (ignore_if_in_upstream) { | |
2389 | /* Don't say anything if head and upstream are the same. */ | |
2390 | if (rev.pending.nr == 2) { | |
2391 | struct object_array_entry *o = rev.pending.objects; | |
2392 | if (oideq(&o[0].item->oid, &o[1].item->oid)) | |
2393 | goto done; | |
2394 | } | |
2395 | get_patch_ids(&rev, &ids); | |
2396 | } | |
2397 | ||
2398 | if (prepare_revision_walk(&rev)) | |
2399 | die(_("revision walk setup failed")); | |
2400 | rev.boundary = 1; | |
2401 | while ((commit = get_revision(&rev)) != NULL) { | |
2402 | if (commit->object.flags & BOUNDARY) { | |
2403 | boundary_count++; | |
2404 | origin = (boundary_count == 1) ? commit : NULL; | |
2405 | continue; | |
2406 | } | |
2407 | ||
2408 | if (ignore_if_in_upstream && has_commit_patch_id(commit, &ids)) | |
2409 | continue; | |
2410 | ||
2411 | nr++; | |
2412 | REALLOC_ARRAY(list, nr); | |
2413 | list[nr - 1] = commit; | |
2414 | } | |
2415 | if (nr == 0) | |
2416 | /* nothing to do */ | |
2417 | goto done; | |
2418 | total = nr; | |
2419 | if (cover_letter == -1) { | |
2420 | if (cfg.config_cover_letter == COVER_AUTO) | |
2421 | cover_letter = (total > 1); | |
2422 | else if ((idiff_prev.nr || rdiff_prev) && (total > 1)) | |
2423 | cover_letter = (cfg.config_cover_letter != COVER_OFF); | |
2424 | else | |
2425 | cover_letter = (cfg.config_cover_letter == COVER_ON); | |
2426 | } | |
2427 | if (!cfg.keep_subject && cfg.auto_number && (total > 1 || cover_letter)) | |
2428 | cfg.numbered = 1; | |
2429 | if (cfg.numbered) | |
2430 | rev.total = total + start_number - 1; | |
2431 | ||
2432 | if (idiff_prev.nr) { | |
2433 | if (!cover_letter && total != 1) | |
2434 | die(_("--interdiff requires --cover-letter or single patch")); | |
2435 | rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1]; | |
2436 | rev.idiff_oid2 = get_commit_tree_oid(list[0]); | |
2437 | rev.idiff_title = diff_title(&idiff_title, reroll_count, | |
2438 | _("Interdiff:"), | |
2439 | _("Interdiff against v%d:")); | |
2440 | } | |
2441 | ||
2442 | if (creation_factor < 0) | |
2443 | creation_factor = CREATION_FACTOR_FOR_THE_SAME_SERIES; | |
2444 | else if (!rdiff_prev) | |
2445 | die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff"); | |
2446 | ||
2447 | if (rdiff_prev) { | |
2448 | if (!cover_letter && total != 1) | |
2449 | die(_("--range-diff requires --cover-letter or single patch")); | |
2450 | ||
2451 | infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev, | |
2452 | origin, list[0]); | |
2453 | rev.rdiff1 = rdiff1.buf; | |
2454 | rev.rdiff2 = rdiff2.buf; | |
2455 | rev.creation_factor = creation_factor; | |
2456 | rev.rdiff_title = diff_title(&rdiff_title, reroll_count, | |
2457 | _("Range-diff:"), | |
2458 | _("Range-diff against v%d:")); | |
2459 | } | |
2460 | ||
2461 | /* | |
2462 | * The order of precedence is: | |
2463 | * | |
2464 | * 1. The `--signature` and `--no-signature` options. | |
2465 | * 2. The `--signature-file` option. | |
2466 | * 3. The `format.signature` config. | |
2467 | * 4. The `format.signatureFile` config. | |
2468 | * 5. Default `git_version_string`. | |
2469 | */ | |
2470 | if (!signature) { | |
2471 | ; /* --no-signature inhibits all signatures */ | |
2472 | } else if (signature && signature != git_version_string) { | |
2473 | ; /* non-default signature already set */ | |
2474 | } else if (signature_file_arg || (cfg.signature_file && !cfg.signature)) { | |
2475 | struct strbuf buf = STRBUF_INIT; | |
2476 | const char *signature_file = signature_file_arg ? | |
2477 | signature_file_arg : cfg.signature_file; | |
2478 | ||
2479 | if (strbuf_read_file(&buf, signature_file, 128) < 0) | |
2480 | die_errno(_("unable to read signature file '%s'"), signature_file); | |
2481 | signature = signature_to_free = strbuf_detach(&buf, NULL); | |
2482 | } else if (cfg.signature) { | |
2483 | signature = cfg.signature; | |
2484 | } | |
2485 | ||
2486 | memset(&bases, 0, sizeof(bases)); | |
2487 | base = get_base_commit(&cfg, list, nr); | |
2488 | if (base) { | |
2489 | reset_revision_walk(); | |
2490 | clear_object_flags(the_repository, UNINTERESTING); | |
2491 | prepare_bases(&bases, base, list, nr); | |
2492 | } | |
2493 | ||
2494 | if (in_reply_to || cfg.thread || cover_letter) { | |
2495 | rev.ref_message_ids = xmalloc(sizeof(*rev.ref_message_ids)); | |
2496 | string_list_init_dup(rev.ref_message_ids); | |
2497 | } | |
2498 | if (in_reply_to) { | |
2499 | char *msgid = clean_message_id(in_reply_to); | |
2500 | string_list_append_nodup(rev.ref_message_ids, msgid); | |
2501 | } | |
2502 | rev.numbered_files = just_numbers; | |
2503 | rev.patch_suffix = fmt_patch_suffix; | |
2504 | if (cover_letter) { | |
2505 | if (cfg.thread) | |
2506 | gen_message_id(&rev, "cover"); | |
2507 | make_cover_letter(&rev, !!output_directory, | |
2508 | origin, nr, list, description_file, branch_name, quiet, &cfg); | |
2509 | print_bases(&bases, rev.diffopt.file); | |
2510 | print_signature(signature, rev.diffopt.file); | |
2511 | total++; | |
2512 | start_number--; | |
2513 | /* interdiff/range-diff in cover-letter; omit from patches */ | |
2514 | rev.idiff_oid1 = NULL; | |
2515 | rev.rdiff1 = NULL; | |
2516 | } | |
2517 | rev.add_signoff = cfg.do_signoff; | |
2518 | ||
2519 | if (show_progress) | |
2520 | progress = start_delayed_progress(the_repository, | |
2521 | _("Generating patches"), total); | |
2522 | for (i = 0; i < nr; i++) { | |
2523 | size_t idx = nr - i - 1; | |
2524 | int shown; | |
2525 | ||
2526 | display_progress(progress, total - idx); | |
2527 | commit = list[idx]; | |
2528 | rev.nr = total - idx + (start_number - 1); | |
2529 | ||
2530 | /* Make the second and subsequent mails replies to the first */ | |
2531 | if (cfg.thread) { | |
2532 | /* Have we already had a message ID? */ | |
2533 | if (rev.message_id) { | |
2534 | /* | |
2535 | * For deep threading: make every mail | |
2536 | * a reply to the previous one, no | |
2537 | * matter what other options are set. | |
2538 | * | |
2539 | * For shallow threading: | |
2540 | * | |
2541 | * Without --cover-letter and | |
2542 | * --in-reply-to, make every mail a | |
2543 | * reply to the one before. | |
2544 | * | |
2545 | * With --in-reply-to but no | |
2546 | * --cover-letter, make every mail a | |
2547 | * reply to the <reply-to>. | |
2548 | * | |
2549 | * With --cover-letter, make every | |
2550 | * mail but the cover letter a reply | |
2551 | * to the cover letter. The cover | |
2552 | * letter is a reply to the | |
2553 | * --in-reply-to, if specified. | |
2554 | */ | |
2555 | if (cfg.thread == THREAD_SHALLOW | |
2556 | && rev.ref_message_ids->nr > 0 | |
2557 | && (!cover_letter || rev.nr > 1)) | |
2558 | free(rev.message_id); | |
2559 | else | |
2560 | string_list_append_nodup(rev.ref_message_ids, | |
2561 | rev.message_id); | |
2562 | } | |
2563 | gen_message_id(&rev, oid_to_hex(&commit->object.oid)); | |
2564 | } | |
2565 | ||
2566 | if (output_directory && | |
2567 | open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet)) | |
2568 | die(_("failed to create output files")); | |
2569 | shown = log_tree_commit(&rev, commit); | |
2570 | free_commit_buffer(the_repository->parsed_objects, | |
2571 | commit); | |
2572 | ||
2573 | /* We put one extra blank line between formatted | |
2574 | * patches and this flag is used by log-tree code | |
2575 | * to see if it needs to emit a LF before showing | |
2576 | * the log; when using one file per patch, we do | |
2577 | * not want the extra blank line. | |
2578 | */ | |
2579 | if (output_directory) | |
2580 | rev.shown_one = 0; | |
2581 | if (shown) { | |
2582 | print_bases(&bases, rev.diffopt.file); | |
2583 | if (rev.mime_boundary) | |
2584 | fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n", | |
2585 | mime_boundary_leader, | |
2586 | rev.mime_boundary); | |
2587 | else | |
2588 | print_signature(signature, rev.diffopt.file); | |
2589 | } | |
2590 | if (output_directory) { | |
2591 | fclose(rev.diffopt.file); | |
2592 | rev.diffopt.file = NULL; | |
2593 | } | |
2594 | } | |
2595 | stop_progress(&progress); | |
2596 | free(list); | |
2597 | if (ignore_if_in_upstream) | |
2598 | free_patch_ids(&ids); | |
2599 | ||
2600 | done: | |
2601 | oid_array_clear(&idiff_prev); | |
2602 | strbuf_release(&idiff_title); | |
2603 | strbuf_release(&rdiff1); | |
2604 | strbuf_release(&rdiff2); | |
2605 | strbuf_release(&rdiff_title); | |
2606 | free(description_file); | |
2607 | free(signature_file_arg); | |
2608 | free(signature_to_free); | |
2609 | free(branch_name); | |
2610 | free(to_free); | |
2611 | free(rev.message_id); | |
2612 | if (rev.ref_message_ids) | |
2613 | string_list_clear(rev.ref_message_ids, 0); | |
2614 | free(rev.ref_message_ids); | |
2615 | rev.diffopt.no_free = 0; | |
2616 | release_revisions(&rev); | |
2617 | format_config_release(&cfg); | |
2618 | return 0; | |
2619 | } | |
2620 | ||
2621 | static int add_pending_commit(const char *arg, struct rev_info *revs, int flags) | |
2622 | { | |
2623 | struct object_id oid; | |
2624 | if (repo_get_oid(the_repository, arg, &oid) == 0) { | |
2625 | struct commit *commit = lookup_commit_reference(the_repository, | |
2626 | &oid); | |
2627 | if (commit) { | |
2628 | commit->object.flags |= flags; | |
2629 | add_pending_object(revs, &commit->object, arg); | |
2630 | return 0; | |
2631 | } | |
2632 | } | |
2633 | return -1; | |
2634 | } | |
2635 | ||
2636 | static const char * const cherry_usage[] = { | |
2637 | N_("git cherry [-v] [<upstream> [<head> [<limit>]]]"), | |
2638 | NULL | |
2639 | }; | |
2640 | ||
2641 | static void print_commit(char sign, struct commit *commit, int verbose, | |
2642 | int abbrev, FILE *file) | |
2643 | { | |
2644 | if (!verbose) { | |
2645 | fprintf(file, "%c %s\n", sign, | |
2646 | repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev)); | |
2647 | } else { | |
2648 | struct strbuf buf = STRBUF_INIT; | |
2649 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); | |
2650 | fprintf(file, "%c %s %s\n", sign, | |
2651 | repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev), | |
2652 | buf.buf); | |
2653 | strbuf_release(&buf); | |
2654 | } | |
2655 | } | |
2656 | ||
2657 | int cmd_cherry(int argc, | |
2658 | const char **argv, | |
2659 | const char *prefix, | |
2660 | struct repository *repo UNUSED) | |
2661 | { | |
2662 | struct rev_info revs; | |
2663 | struct patch_ids ids; | |
2664 | struct commit *commit; | |
2665 | struct commit_list *list = NULL; | |
2666 | struct branch *current_branch; | |
2667 | const char *upstream; | |
2668 | const char *head = "HEAD"; | |
2669 | const char *limit = NULL; | |
2670 | int verbose = 0, abbrev = 0; | |
2671 | ||
2672 | struct option options[] = { | |
2673 | OPT__ABBREV(&abbrev), | |
2674 | OPT__VERBOSE(&verbose, N_("be verbose")), | |
2675 | OPT_END() | |
2676 | }; | |
2677 | ||
2678 | argc = parse_options(argc, argv, prefix, options, cherry_usage, 0); | |
2679 | ||
2680 | switch (argc) { | |
2681 | case 3: | |
2682 | limit = argv[2]; | |
2683 | /* FALLTHROUGH */ | |
2684 | case 2: | |
2685 | head = argv[1]; | |
2686 | /* FALLTHROUGH */ | |
2687 | case 1: | |
2688 | upstream = argv[0]; | |
2689 | break; | |
2690 | default: | |
2691 | current_branch = branch_get(NULL); | |
2692 | upstream = branch_get_upstream(current_branch, NULL); | |
2693 | if (!upstream) { | |
2694 | fprintf(stderr, _("Could not find a tracked" | |
2695 | " remote branch, please" | |
2696 | " specify <upstream> manually.\n")); | |
2697 | usage_with_options(cherry_usage, options); | |
2698 | } | |
2699 | } | |
2700 | ||
2701 | repo_init_revisions(the_repository, &revs, prefix); | |
2702 | revs.max_parents = 1; | |
2703 | ||
2704 | if (add_pending_commit(head, &revs, 0)) | |
2705 | die(_("unknown commit %s"), head); | |
2706 | if (add_pending_commit(upstream, &revs, UNINTERESTING)) | |
2707 | die(_("unknown commit %s"), upstream); | |
2708 | ||
2709 | /* Don't say anything if head and upstream are the same. */ | |
2710 | if (revs.pending.nr == 2) { | |
2711 | struct object_array_entry *o = revs.pending.objects; | |
2712 | if (oideq(&o[0].item->oid, &o[1].item->oid)) | |
2713 | return 0; | |
2714 | } | |
2715 | ||
2716 | get_patch_ids(&revs, &ids); | |
2717 | ||
2718 | if (limit && add_pending_commit(limit, &revs, UNINTERESTING)) | |
2719 | die(_("unknown commit %s"), limit); | |
2720 | ||
2721 | /* reverse the list of commits */ | |
2722 | if (prepare_revision_walk(&revs)) | |
2723 | die(_("revision walk setup failed")); | |
2724 | while ((commit = get_revision(&revs)) != NULL) { | |
2725 | commit_list_insert(commit, &list); | |
2726 | } | |
2727 | ||
2728 | for (struct commit_list *l = list; l; l = l->next) { | |
2729 | char sign = '+'; | |
2730 | ||
2731 | commit = l->item; | |
2732 | if (has_commit_patch_id(commit, &ids)) | |
2733 | sign = '-'; | |
2734 | print_commit(sign, commit, verbose, abbrev, revs.diffopt.file); | |
2735 | } | |
2736 | ||
2737 | free_commit_list(list); | |
2738 | free_patch_ids(&ids); | |
2739 | return 0; | |
2740 | } |