Coverage Report

Created: 2024-09-16 06:12

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