Coverage Report

Created: 2024-09-08 06:23

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