Coverage Report

Created: 2023-02-27 06:35

/src/git/builtin/commit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Builtin "git commit"
3
 *
4
 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5
 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
6
 */
7
8
#define USE_THE_INDEX_VARIABLE
9
#include "cache.h"
10
#include "config.h"
11
#include "lockfile.h"
12
#include "cache-tree.h"
13
#include "color.h"
14
#include "dir.h"
15
#include "builtin.h"
16
#include "diff.h"
17
#include "diffcore.h"
18
#include "commit.h"
19
#include "revision.h"
20
#include "wt-status.h"
21
#include "run-command.h"
22
#include "hook.h"
23
#include "refs.h"
24
#include "log-tree.h"
25
#include "strbuf.h"
26
#include "utf8.h"
27
#include "parse-options.h"
28
#include "string-list.h"
29
#include "rerere.h"
30
#include "unpack-trees.h"
31
#include "quote.h"
32
#include "submodule.h"
33
#include "gpg-interface.h"
34
#include "column.h"
35
#include "sequencer.h"
36
#include "mailmap.h"
37
#include "help.h"
38
#include "commit-reach.h"
39
#include "commit-graph.h"
40
#include "pretty.h"
41
42
static const char * const builtin_commit_usage[] = {
43
  N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
44
     "           [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
45
     "           [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
46
     "           [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
47
     "           [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
48
     "           [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
49
     "           [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
50
     "           [--] [<pathspec>...]"),
51
  NULL
52
};
53
54
static const char * const builtin_status_usage[] = {
55
  N_("git status [<options>] [--] [<pathspec>...]"),
56
  NULL
57
};
58
59
static const char empty_amend_advice[] =
60
N_("You asked to amend the most recent commit, but doing so would make\n"
61
"it empty. You can repeat your command with --allow-empty, or you can\n"
62
"remove the commit entirely with \"git reset HEAD^\".\n");
63
64
static const char empty_cherry_pick_advice[] =
65
N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
66
"If you wish to commit it anyway, use:\n"
67
"\n"
68
"    git commit --allow-empty\n"
69
"\n");
70
71
static const char empty_rebase_pick_advice[] =
72
N_("Otherwise, please use 'git rebase --skip'\n");
73
74
static const char empty_cherry_pick_advice_single[] =
75
N_("Otherwise, please use 'git cherry-pick --skip'\n");
76
77
static const char empty_cherry_pick_advice_multi[] =
78
N_("and then use:\n"
79
"\n"
80
"    git cherry-pick --continue\n"
81
"\n"
82
"to resume cherry-picking the remaining commits.\n"
83
"If you wish to skip this commit, use:\n"
84
"\n"
85
"    git cherry-pick --skip\n"
86
"\n");
87
88
static const char *color_status_slots[] = {
89
  [WT_STATUS_HEADER]    = "header",
90
  [WT_STATUS_UPDATED]   = "updated",
91
  [WT_STATUS_CHANGED]   = "changed",
92
  [WT_STATUS_UNTRACKED]   = "untracked",
93
  [WT_STATUS_NOBRANCH]    = "noBranch",
94
  [WT_STATUS_UNMERGED]    = "unmerged",
95
  [WT_STATUS_LOCAL_BRANCH]  = "localBranch",
96
  [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
97
  [WT_STATUS_ONBRANCH]    = "branch",
98
};
99
100
static const char *use_message_buffer;
101
static struct lock_file index_lock; /* real index */
102
static struct lock_file false_lock; /* used only for partial commits */
103
static enum {
104
  COMMIT_AS_IS = 1,
105
  COMMIT_NORMAL,
106
  COMMIT_PARTIAL
107
} commit_style;
108
109
static const char *logfile, *force_author;
110
static const char *template_file;
111
/*
112
 * The _message variables are commit names from which to take
113
 * the commit message and/or authorship.
114
 */
115
static const char *author_message, *author_message_buffer;
116
static char *edit_message, *use_message;
117
static char *fixup_message, *fixup_commit, *squash_message;
118
static const char *fixup_prefix;
119
static int all, also, interactive, patch_interactive, only, amend, signoff;
120
static int edit_flag = -1; /* unspecified */
121
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
122
static int config_commit_verbose = -1; /* unspecified */
123
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
124
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
125
static char *sign_commit, *pathspec_from_file;
126
static struct strvec trailer_args = STRVEC_INIT;
127
128
/*
129
 * The default commit message cleanup mode will remove the lines
130
 * beginning with # (shell comments) and leading and trailing
131
 * whitespaces (empty lines or containing only whitespaces)
132
 * if editor is used, and only the whitespaces if the message
133
 * is specified explicitly.
134
 */
135
static enum commit_msg_cleanup_mode cleanup_mode;
136
static const char *cleanup_arg;
137
138
static enum commit_whence whence;
139
static int use_editor = 1, include_status = 1;
140
static int have_option_m;
141
static struct strbuf message = STRBUF_INIT;
142
143
static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
144
145
static int opt_pass_trailer(const struct option *opt, const char *arg, int unset)
146
0
{
147
0
  BUG_ON_OPT_NEG(unset);
148
149
0
  strvec_pushl(opt->value, "--trailer", arg, NULL);
150
0
  return 0;
151
0
}
152
153
static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
154
0
{
155
0
  enum wt_status_format *value = (enum wt_status_format *)opt->value;
156
0
  if (unset)
157
0
    *value = STATUS_FORMAT_NONE;
158
0
  else if (!arg)
159
0
    *value = STATUS_FORMAT_PORCELAIN;
160
0
  else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
161
0
    *value = STATUS_FORMAT_PORCELAIN;
162
0
  else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
163
0
    *value = STATUS_FORMAT_PORCELAIN_V2;
164
0
  else
165
0
    die("unsupported porcelain version '%s'", arg);
166
167
0
  return 0;
168
0
}
169
170
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
171
7.59k
{
172
7.59k
  struct strbuf *buf = opt->value;
173
7.59k
  if (unset) {
174
0
    have_option_m = 0;
175
0
    strbuf_setlen(buf, 0);
176
7.59k
  } else {
177
7.59k
    have_option_m = 1;
178
7.59k
    if (buf->len)
179
7.59k
      strbuf_addch(buf, '\n');
180
7.59k
    strbuf_addstr(buf, arg);
181
7.59k
    strbuf_complete_line(buf);
182
7.59k
  }
183
7.59k
  return 0;
184
7.59k
}
185
186
static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
187
0
{
188
0
  const char **value = opt->value;
189
190
0
  BUG_ON_OPT_NEG(unset);
191
192
0
  if (arg != NULL && *arg == '=')
193
0
    arg = arg + 1;
194
195
0
  *value = arg;
196
0
  return 0;
197
0
}
198
199
static void determine_whence(struct wt_status *s)
200
7.59k
{
201
7.59k
  if (file_exists(git_path_merge_head(the_repository)))
202
0
    whence = FROM_MERGE;
203
7.59k
  else if (!sequencer_determine_whence(the_repository, &whence))
204
7.59k
    whence = FROM_COMMIT;
205
7.59k
  if (s)
206
7.59k
    s->whence = whence;
207
7.59k
}
208
209
static void status_init_config(struct wt_status *s, config_fn_t fn)
210
7.59k
{
211
7.59k
  wt_status_prepare(the_repository, s);
212
7.59k
  init_diff_ui_defaults();
213
7.59k
  git_config(fn, s);
214
7.59k
  determine_whence(s);
215
7.59k
  s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
216
7.59k
}
217
218
static void rollback_index_files(void)
219
100
{
220
100
  switch (commit_style) {
221
100
  case COMMIT_AS_IS:
222
100
    break; /* nothing to do */
223
0
  case COMMIT_NORMAL:
224
0
    rollback_lock_file(&index_lock);
225
0
    break;
226
0
  case COMMIT_PARTIAL:
227
0
    rollback_lock_file(&index_lock);
228
0
    rollback_lock_file(&false_lock);
229
0
    break;
230
100
  }
231
100
}
232
233
static int commit_index_files(void)
234
7.49k
{
235
7.49k
  int err = 0;
236
237
7.49k
  switch (commit_style) {
238
7.49k
  case COMMIT_AS_IS:
239
7.49k
    break; /* nothing to do */
240
0
  case COMMIT_NORMAL:
241
0
    err = commit_lock_file(&index_lock);
242
0
    break;
243
0
  case COMMIT_PARTIAL:
244
0
    err = commit_lock_file(&index_lock);
245
0
    rollback_lock_file(&false_lock);
246
0
    break;
247
7.49k
  }
248
249
7.49k
  return err;
250
7.49k
}
251
252
/*
253
 * Take a union of paths in the index and the named tree (typically, "HEAD"),
254
 * and return the paths that match the given pattern in list.
255
 */
256
static int list_paths(struct string_list *list, const char *with_tree,
257
          const struct pathspec *pattern)
258
0
{
259
0
  int i, ret;
260
0
  char *m;
261
262
0
  if (!pattern->nr)
263
0
    return 0;
264
265
0
  m = xcalloc(1, pattern->nr);
266
267
0
  if (with_tree) {
268
0
    char *max_prefix = common_prefix(pattern);
269
0
    overlay_tree_on_index(&the_index, with_tree, max_prefix);
270
0
    free(max_prefix);
271
0
  }
272
273
  /* TODO: audit for interaction with sparse-index. */
274
0
  ensure_full_index(&the_index);
275
0
  for (i = 0; i < the_index.cache_nr; i++) {
276
0
    const struct cache_entry *ce = the_index.cache[i];
277
0
    struct string_list_item *item;
278
279
0
    if (ce->ce_flags & CE_UPDATE)
280
0
      continue;
281
0
    if (!ce_path_match(&the_index, ce, pattern, m))
282
0
      continue;
283
0
    item = string_list_insert(list, ce->name);
284
0
    if (ce_skip_worktree(ce))
285
0
      item->util = item; /* better a valid pointer than a fake one */
286
0
  }
287
288
0
  ret = report_path_error(m, pattern);
289
0
  free(m);
290
0
  return ret;
291
0
}
292
293
static void add_remove_files(struct string_list *list)
294
0
{
295
0
  int i;
296
0
  for (i = 0; i < list->nr; i++) {
297
0
    struct stat st;
298
0
    struct string_list_item *p = &(list->items[i]);
299
300
    /* p->util is skip-worktree */
301
0
    if (p->util)
302
0
      continue;
303
304
0
    if (!lstat(p->string, &st)) {
305
0
      if (add_to_index(&the_index, p->string, &st, 0))
306
0
        die(_("updating files failed"));
307
0
    } else
308
0
      remove_file_from_index(&the_index, p->string);
309
0
  }
310
0
}
311
312
static void create_base_index(const struct commit *current_head)
313
0
{
314
0
  struct tree *tree;
315
0
  struct unpack_trees_options opts;
316
0
  struct tree_desc t;
317
318
0
  if (!current_head) {
319
0
    discard_index(&the_index);
320
0
    return;
321
0
  }
322
323
0
  memset(&opts, 0, sizeof(opts));
324
0
  opts.head_idx = 1;
325
0
  opts.index_only = 1;
326
0
  opts.merge = 1;
327
0
  opts.src_index = &the_index;
328
0
  opts.dst_index = &the_index;
329
330
0
  opts.fn = oneway_merge;
331
0
  tree = parse_tree_indirect(&current_head->object.oid);
332
0
  if (!tree)
333
0
    die(_("failed to unpack HEAD tree object"));
334
0
  parse_tree(tree);
335
0
  init_tree_desc(&t, tree->buffer, tree->size);
336
0
  if (unpack_trees(1, &t, &opts))
337
0
    exit(128); /* We've already reported the error, finish dying */
338
0
}
339
340
static void refresh_cache_or_die(int refresh_flags)
341
7.59k
{
342
  /*
343
   * refresh_flags contains REFRESH_QUIET, so the only errors
344
   * are for unmerged entries.
345
   */
346
7.59k
  if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
347
0
    die_resolve_conflict("commit");
348
7.59k
}
349
350
static const char *prepare_index(const char **argv, const char *prefix,
351
         const struct commit *current_head, int is_status)
352
7.59k
{
353
7.59k
  struct string_list partial = STRING_LIST_INIT_DUP;
354
7.59k
  struct pathspec pathspec;
355
7.59k
  int refresh_flags = REFRESH_QUIET;
356
7.59k
  const char *ret;
357
358
7.59k
  if (is_status)
359
0
    refresh_flags |= REFRESH_UNMERGED;
360
7.59k
  parse_pathspec(&pathspec, 0,
361
7.59k
           PATHSPEC_PREFER_FULL,
362
7.59k
           prefix, argv);
363
364
7.59k
  if (pathspec_from_file) {
365
0
    if (interactive)
366
0
      die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
367
368
0
    if (all)
369
0
      die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
370
371
0
    if (pathspec.nr)
372
0
      die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
373
374
0
    parse_pathspec_file(&pathspec, 0,
375
0
            PATHSPEC_PREFER_FULL,
376
0
            prefix, pathspec_from_file, pathspec_file_nul);
377
7.59k
  } else if (pathspec_file_nul) {
378
0
    die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
379
0
  }
380
381
7.59k
  if (!pathspec.nr && (also || (only && !allow_empty &&
382
7.59k
      (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
383
0
    die(_("No paths with --include/--only does not make sense."));
384
385
7.59k
  if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
386
0
    die(_("index file corrupt"));
387
388
7.59k
  if (interactive) {
389
0
    char *old_index_env = NULL, *old_repo_index_file;
390
0
    repo_hold_locked_index(the_repository, &index_lock,
391
0
               LOCK_DIE_ON_ERROR);
392
393
0
    refresh_cache_or_die(refresh_flags);
394
395
0
    if (write_locked_index(&the_index, &index_lock, 0))
396
0
      die(_("unable to create temporary index"));
397
398
0
    old_repo_index_file = the_repository->index_file;
399
0
    the_repository->index_file =
400
0
      (char *)get_lock_file_path(&index_lock);
401
0
    old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
402
0
    setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
403
404
0
    if (interactive_add(argv, prefix, patch_interactive) != 0)
405
0
      die(_("interactive add failed"));
406
407
0
    the_repository->index_file = old_repo_index_file;
408
0
    if (old_index_env && *old_index_env)
409
0
      setenv(INDEX_ENVIRONMENT, old_index_env, 1);
410
0
    else
411
0
      unsetenv(INDEX_ENVIRONMENT);
412
0
    FREE_AND_NULL(old_index_env);
413
414
0
    discard_index(&the_index);
415
0
    read_index_from(&the_index, get_lock_file_path(&index_lock),
416
0
        get_git_dir());
417
0
    if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) {
418
0
      if (reopen_lock_file(&index_lock) < 0)
419
0
        die(_("unable to write index file"));
420
0
      if (write_locked_index(&the_index, &index_lock, 0))
421
0
        die(_("unable to update temporary index"));
422
0
    } else
423
0
      warning(_("Failed to update main cache tree"));
424
425
0
    commit_style = COMMIT_NORMAL;
426
0
    ret = get_lock_file_path(&index_lock);
427
0
    goto out;
428
0
  }
429
430
  /*
431
   * Non partial, non as-is commit.
432
   *
433
   * (1) get the real index;
434
   * (2) update the_index as necessary;
435
   * (3) write the_index out to the real index (still locked);
436
   * (4) return the name of the locked index file.
437
   *
438
   * The caller should run hooks on the locked real index, and
439
   * (A) if all goes well, commit the real index;
440
   * (B) on failure, rollback the real index.
441
   */
442
7.59k
  if (all || (also && pathspec.nr)) {
443
0
    repo_hold_locked_index(the_repository, &index_lock,
444
0
               LOCK_DIE_ON_ERROR);
445
0
    add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
446
0
    refresh_cache_or_die(refresh_flags);
447
0
    cache_tree_update(&the_index, WRITE_TREE_SILENT);
448
0
    if (write_locked_index(&the_index, &index_lock, 0))
449
0
      die(_("unable to write new_index file"));
450
0
    commit_style = COMMIT_NORMAL;
451
0
    ret = get_lock_file_path(&index_lock);
452
0
    goto out;
453
0
  }
454
455
  /*
456
   * As-is commit.
457
   *
458
   * (1) return the name of the real index file.
459
   *
460
   * The caller should run hooks on the real index,
461
   * and create commit from the_index.
462
   * We still need to refresh the index here.
463
   */
464
7.59k
  if (!only && !pathspec.nr) {
465
7.59k
    repo_hold_locked_index(the_repository, &index_lock,
466
7.59k
               LOCK_DIE_ON_ERROR);
467
7.59k
    refresh_cache_or_die(refresh_flags);
468
7.59k
    if (the_index.cache_changed
469
7.59k
        || !cache_tree_fully_valid(the_index.cache_tree))
470
7.59k
      cache_tree_update(&the_index, WRITE_TREE_SILENT);
471
7.59k
    if (write_locked_index(&the_index, &index_lock,
472
7.59k
               COMMIT_LOCK | SKIP_IF_UNCHANGED))
473
0
      die(_("unable to write new_index file"));
474
7.59k
    commit_style = COMMIT_AS_IS;
475
7.59k
    ret = get_index_file();
476
7.59k
    goto out;
477
7.59k
  }
478
479
  /*
480
   * A partial commit.
481
   *
482
   * (0) find the set of affected paths;
483
   * (1) get lock on the real index file;
484
   * (2) update the_index with the given paths;
485
   * (3) write the_index out to the real index (still locked);
486
   * (4) get lock on the false index file;
487
   * (5) reset the_index from HEAD;
488
   * (6) update the_index the same way as (2);
489
   * (7) write the_index out to the false index file;
490
   * (8) return the name of the false index file (still locked);
491
   *
492
   * The caller should run hooks on the locked false index, and
493
   * create commit from it.  Then
494
   * (A) if all goes well, commit the real index;
495
   * (B) on failure, rollback the real index;
496
   * In either case, rollback the false index.
497
   */
498
0
  commit_style = COMMIT_PARTIAL;
499
500
0
  if (whence != FROM_COMMIT) {
501
0
    if (whence == FROM_MERGE)
502
0
      die(_("cannot do a partial commit during a merge."));
503
0
    else if (is_from_cherry_pick(whence))
504
0
      die(_("cannot do a partial commit during a cherry-pick."));
505
0
    else if (is_from_rebase(whence))
506
0
      die(_("cannot do a partial commit during a rebase."));
507
0
  }
508
509
0
  if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
510
0
    exit(1);
511
512
0
  discard_index(&the_index);
513
0
  if (repo_read_index(the_repository) < 0)
514
0
    die(_("cannot read the index"));
515
516
0
  repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
517
0
  add_remove_files(&partial);
518
0
  refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
519
0
  cache_tree_update(&the_index, WRITE_TREE_SILENT);
520
0
  if (write_locked_index(&the_index, &index_lock, 0))
521
0
    die(_("unable to write new_index file"));
522
523
0
  hold_lock_file_for_update(&false_lock,
524
0
          git_path("next-index-%"PRIuMAX,
525
0
             (uintmax_t) getpid()),
526
0
          LOCK_DIE_ON_ERROR);
527
528
0
  create_base_index(current_head);
529
0
  add_remove_files(&partial);
530
0
  refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
531
532
0
  if (write_locked_index(&the_index, &false_lock, 0))
533
0
    die(_("unable to write temporary index file"));
534
535
0
  discard_index(&the_index);
536
0
  ret = get_lock_file_path(&false_lock);
537
0
  read_index_from(&the_index, ret, get_git_dir());
538
7.59k
out:
539
7.59k
  string_list_clear(&partial, 0);
540
7.59k
  clear_pathspec(&pathspec);
541
7.59k
  return ret;
542
0
}
543
544
static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
545
          struct wt_status *s)
546
100
{
547
100
  struct object_id oid;
548
549
100
  if (s->relative_paths)
550
100
    s->prefix = prefix;
551
552
100
  if (amend) {
553
0
    s->amend = 1;
554
0
    s->reference = "HEAD^1";
555
0
  }
556
100
  s->verbose = verbose;
557
100
  s->index_file = index_file;
558
100
  s->fp = fp;
559
100
  s->nowarn = nowarn;
560
100
  s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
561
100
  if (!s->is_initial)
562
100
    oidcpy(&s->oid_commit, &oid);
563
100
  s->status_format = status_format;
564
100
  s->ignore_submodule_arg = ignore_submodule_arg;
565
566
100
  wt_status_collect(s);
567
100
  wt_status_print(s);
568
100
  wt_status_collect_free_buffers(s);
569
570
100
  return s->committable;
571
100
}
572
573
static int is_a_merge(const struct commit *current_head)
574
0
{
575
0
  return !!(current_head->parents && current_head->parents->next);
576
0
}
577
578
static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
579
7.59k
{
580
7.59k
  if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
581
0
    BUG("unable to parse our own ident: %s", buf->buf);
582
7.59k
}
583
584
static void export_one(const char *var, const char *s, const char *e, int hack)
585
22.7k
{
586
22.7k
  struct strbuf buf = STRBUF_INIT;
587
22.7k
  if (hack)
588
7.59k
    strbuf_addch(&buf, hack);
589
22.7k
  strbuf_add(&buf, s, e - s);
590
22.7k
  setenv(var, buf.buf, 1);
591
22.7k
  strbuf_release(&buf);
592
22.7k
}
593
594
static int parse_force_date(const char *in, struct strbuf *out)
595
0
{
596
0
  strbuf_addch(out, '@');
597
598
0
  if (parse_date(in, out) < 0) {
599
0
    int errors = 0;
600
0
    unsigned long t = approxidate_careful(in, &errors);
601
0
    if (errors)
602
0
      return -1;
603
0
    strbuf_addf(out, "%lu", t);
604
0
  }
605
606
0
  return 0;
607
0
}
608
609
static void set_ident_var(char **buf, char *val)
610
0
{
611
0
  free(*buf);
612
0
  *buf = val;
613
0
}
614
615
static void determine_author_info(struct strbuf *author_ident)
616
7.59k
{
617
7.59k
  char *name, *email, *date;
618
7.59k
  struct ident_split author;
619
620
7.59k
  name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
621
7.59k
  email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
622
7.59k
  date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
623
624
7.59k
  if (author_message) {
625
0
    struct ident_split ident;
626
0
    size_t len;
627
0
    const char *a;
628
629
0
    a = find_commit_header(author_message_buffer, "author", &len);
630
0
    if (!a)
631
0
      die(_("commit '%s' lacks author header"), author_message);
632
0
    if (split_ident_line(&ident, a, len) < 0)
633
0
      die(_("commit '%s' has malformed author line"), author_message);
634
635
0
    set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
636
0
    set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
637
638
0
    if (ident.date_begin) {
639
0
      struct strbuf date_buf = STRBUF_INIT;
640
0
      strbuf_addch(&date_buf, '@');
641
0
      strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
642
0
      strbuf_addch(&date_buf, ' ');
643
0
      strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
644
0
      set_ident_var(&date, strbuf_detach(&date_buf, NULL));
645
0
    }
646
0
  }
647
648
7.59k
  if (force_author) {
649
0
    struct ident_split ident;
650
651
0
    if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
652
0
      die(_("malformed --author parameter"));
653
0
    set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
654
0
    set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
655
0
  }
656
657
7.59k
  if (force_date) {
658
0
    struct strbuf date_buf = STRBUF_INIT;
659
0
    if (parse_force_date(force_date, &date_buf))
660
0
      die(_("invalid date format: %s"), force_date);
661
0
    set_ident_var(&date, strbuf_detach(&date_buf, NULL));
662
0
  }
663
664
7.59k
  strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
665
7.59k
        IDENT_STRICT));
666
7.59k
  assert_split_ident(&author, author_ident);
667
7.59k
  export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
668
7.59k
  export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
669
7.59k
  export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
670
7.59k
  free(name);
671
7.59k
  free(email);
672
7.59k
  free(date);
673
7.59k
}
674
675
static int author_date_is_interesting(void)
676
7.49k
{
677
7.49k
  return author_message || force_date;
678
7.49k
}
679
680
static void adjust_comment_line_char(const struct strbuf *sb)
681
0
{
682
0
  char candidates[] = "#;@!$%^&|:";
683
0
  char *candidate;
684
0
  const char *p;
685
686
0
  comment_line_char = candidates[0];
687
0
  if (!memchr(sb->buf, comment_line_char, sb->len))
688
0
    return;
689
690
0
  p = sb->buf;
691
0
  candidate = strchr(candidates, *p);
692
0
  if (candidate)
693
0
    *candidate = ' ';
694
0
  for (p = sb->buf; *p; p++) {
695
0
    if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
696
0
      candidate = strchr(candidates, p[1]);
697
0
      if (candidate)
698
0
        *candidate = ' ';
699
0
    }
700
0
  }
701
702
0
  for (p = candidates; *p == ' '; p++)
703
0
    ;
704
0
  if (!*p)
705
0
    die(_("unable to select a comment character that is not used\n"
706
0
          "in the current commit message"));
707
0
  comment_line_char = *p;
708
0
}
709
710
static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
711
        struct pretty_print_context *ctx)
712
0
{
713
0
  const char *buffer, *subject, *fmt;
714
715
0
  buffer = get_commit_buffer(commit, NULL);
716
0
  find_commit_subject(buffer, &subject);
717
  /*
718
   * If we amend the 'amend!' commit then we don't want to
719
   * duplicate the subject line.
720
   */
721
0
  fmt = starts_with(subject, "amend!") ? "%b" : "%B";
722
0
  format_commit_message(commit, fmt, sb, ctx);
723
0
  unuse_commit_buffer(commit, buffer);
724
0
}
725
726
static int prepare_to_commit(const char *index_file, const char *prefix,
727
           struct commit *current_head,
728
           struct wt_status *s,
729
           struct strbuf *author_ident)
730
7.59k
{
731
7.59k
  struct stat statbuf;
732
7.59k
  struct strbuf committer_ident = STRBUF_INIT;
733
7.59k
  int committable;
734
7.59k
  struct strbuf sb = STRBUF_INIT;
735
7.59k
  const char *hook_arg1 = NULL;
736
7.59k
  const char *hook_arg2 = NULL;
737
7.59k
  int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
738
7.59k
  int old_display_comment_prefix;
739
7.59k
  int merge_contains_scissors = 0;
740
7.59k
  int invoked_hook;
741
742
  /* This checks and barfs if author is badly specified */
743
7.59k
  determine_author_info(author_ident);
744
745
7.59k
  if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
746
7.59k
            "pre-commit", NULL))
747
0
    return 0;
748
749
7.59k
  if (squash_message) {
750
    /*
751
     * Insert the proper subject line before other commit
752
     * message options add their content.
753
     */
754
0
    if (use_message && !strcmp(use_message, squash_message))
755
0
      strbuf_addstr(&sb, "squash! ");
756
0
    else {
757
0
      struct pretty_print_context ctx = {0};
758
0
      struct commit *c;
759
0
      c = lookup_commit_reference_by_name(squash_message);
760
0
      if (!c)
761
0
        die(_("could not lookup commit %s"), squash_message);
762
0
      ctx.output_encoding = get_commit_output_encoding();
763
0
      format_commit_message(c, "squash! %s\n\n", &sb,
764
0
                &ctx);
765
0
    }
766
0
  }
767
768
7.59k
  if (have_option_m && !fixup_message) {
769
7.59k
    strbuf_addbuf(&sb, &message);
770
7.59k
    hook_arg1 = "message";
771
7.59k
  } else if (logfile && !strcmp(logfile, "-")) {
772
0
    if (isatty(0))
773
0
      fprintf(stderr, _("(reading log message from standard input)\n"));
774
0
    if (strbuf_read(&sb, 0, 0) < 0)
775
0
      die_errno(_("could not read log from standard input"));
776
0
    hook_arg1 = "message";
777
0
  } else if (logfile) {
778
0
    if (strbuf_read_file(&sb, logfile, 0) < 0)
779
0
      die_errno(_("could not read log file '%s'"),
780
0
          logfile);
781
0
    hook_arg1 = "message";
782
0
  } else if (use_message) {
783
0
    char *buffer;
784
0
    buffer = strstr(use_message_buffer, "\n\n");
785
0
    if (buffer)
786
0
      strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
787
0
    hook_arg1 = "commit";
788
0
    hook_arg2 = use_message;
789
0
  } else if (fixup_message) {
790
0
    struct pretty_print_context ctx = {0};
791
0
    struct commit *commit;
792
0
    char *fmt;
793
0
    commit = lookup_commit_reference_by_name(fixup_commit);
794
0
    if (!commit)
795
0
      die(_("could not lookup commit %s"), fixup_commit);
796
0
    ctx.output_encoding = get_commit_output_encoding();
797
0
    fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
798
0
    format_commit_message(commit, fmt, &sb, &ctx);
799
0
    free(fmt);
800
0
    hook_arg1 = "message";
801
802
    /*
803
     * Only `-m` commit message option is checked here, as
804
     * it supports `--fixup` to append the commit message.
805
     *
806
     * The other commit message options `-c`/`-C`/`-F` are
807
     * incompatible with all the forms of `--fixup` and
808
     * have already errored out while parsing the `git commit`
809
     * options.
810
     */
811
0
    if (have_option_m && !strcmp(fixup_prefix, "fixup"))
812
0
      strbuf_addbuf(&sb, &message);
813
814
0
    if (!strcmp(fixup_prefix, "amend")) {
815
0
      if (have_option_m)
816
0
        die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
817
0
      prepare_amend_commit(commit, &sb, &ctx);
818
0
    }
819
0
  } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
820
0
    size_t merge_msg_start;
821
822
    /*
823
     * prepend SQUASH_MSG here if it exists and a
824
     * "merge --squash" was originally performed
825
     */
826
0
    if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
827
0
      if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
828
0
        die_errno(_("could not read SQUASH_MSG"));
829
0
      hook_arg1 = "squash";
830
0
    } else
831
0
      hook_arg1 = "merge";
832
833
0
    merge_msg_start = sb.len;
834
0
    if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
835
0
      die_errno(_("could not read MERGE_MSG"));
836
837
0
    if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
838
0
        wt_status_locate_end(sb.buf + merge_msg_start,
839
0
           sb.len - merge_msg_start) <
840
0
        sb.len - merge_msg_start)
841
0
      merge_contains_scissors = 1;
842
0
  } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
843
0
    if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
844
0
      die_errno(_("could not read SQUASH_MSG"));
845
0
    hook_arg1 = "squash";
846
0
  } else if (template_file) {
847
0
    if (strbuf_read_file(&sb, template_file, 0) < 0)
848
0
      die_errno(_("could not read '%s'"), template_file);
849
0
    hook_arg1 = "template";
850
0
    clean_message_contents = 0;
851
0
  }
852
853
  /*
854
   * The remaining cases don't modify the template message, but
855
   * just set the argument(s) to the prepare-commit-msg hook.
856
   */
857
0
  else if (whence == FROM_MERGE)
858
0
    hook_arg1 = "merge";
859
0
  else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
860
0
    hook_arg1 = "commit";
861
0
    hook_arg2 = "CHERRY_PICK_HEAD";
862
0
  }
863
864
7.59k
  if (squash_message) {
865
    /*
866
     * If squash_commit was used for the commit subject,
867
     * then we're possibly hijacking other commit log options.
868
     * Reset the hook args to tell the real story.
869
     */
870
0
    hook_arg1 = "message";
871
0
    hook_arg2 = "";
872
0
  }
873
874
7.59k
  s->fp = fopen_for_writing(git_path_commit_editmsg());
875
7.59k
  if (!s->fp)
876
0
    die_errno(_("could not open '%s'"), git_path_commit_editmsg());
877
878
  /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
879
7.59k
  old_display_comment_prefix = s->display_comment_prefix;
880
7.59k
  s->display_comment_prefix = 1;
881
882
  /*
883
   * Most hints are counter-productive when the commit has
884
   * already started.
885
   */
886
7.59k
  s->hints = 0;
887
888
7.59k
  if (clean_message_contents)
889
7.59k
    strbuf_stripspace(&sb, 0);
890
891
7.59k
  if (signoff)
892
0
    append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
893
894
7.59k
  if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
895
0
    die_errno(_("could not write commit template"));
896
897
7.59k
  if (auto_comment_line_char)
898
0
    adjust_comment_line_char(&sb);
899
7.59k
  strbuf_release(&sb);
900
901
  /* This checks if committer ident is explicitly given */
902
7.59k
  strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
903
7.59k
  if (use_editor && include_status) {
904
0
    int ident_shown = 0;
905
0
    int saved_color_setting;
906
0
    struct ident_split ci, ai;
907
0
    const char *hint_cleanup_all = allow_empty_message ?
908
0
      _("Please enter the commit message for your changes."
909
0
        " Lines starting\nwith '%c' will be ignored.\n") :
910
0
      _("Please enter the commit message for your changes."
911
0
        " Lines starting\nwith '%c' will be ignored, and an empty"
912
0
        " message aborts the commit.\n");
913
0
    const char *hint_cleanup_space = allow_empty_message ?
914
0
      _("Please enter the commit message for your changes."
915
0
        " Lines starting\n"
916
0
        "with '%c' will be kept; you may remove them"
917
0
        " yourself if you want to.\n") :
918
0
      _("Please enter the commit message for your changes."
919
0
        " Lines starting\n"
920
0
        "with '%c' will be kept; you may remove them"
921
0
        " yourself if you want to.\n"
922
0
        "An empty message aborts the commit.\n");
923
0
    if (whence != FROM_COMMIT) {
924
0
      if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
925
0
        !merge_contains_scissors)
926
0
        wt_status_add_cut_line(s->fp);
927
0
      status_printf_ln(
928
0
        s, GIT_COLOR_NORMAL,
929
0
        whence == FROM_MERGE ?
930
0
                _("\n"
931
0
            "It looks like you may be committing a merge.\n"
932
0
            "If this is not correct, please run\n"
933
0
            " git update-ref -d MERGE_HEAD\n"
934
0
            "and try again.\n") :
935
0
                _("\n"
936
0
            "It looks like you may be committing a cherry-pick.\n"
937
0
            "If this is not correct, please run\n"
938
0
            " git update-ref -d CHERRY_PICK_HEAD\n"
939
0
            "and try again.\n"));
940
0
    }
941
942
0
    fprintf(s->fp, "\n");
943
0
    if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
944
0
      status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
945
0
    else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
946
0
      if (whence == FROM_COMMIT && !merge_contains_scissors)
947
0
        wt_status_add_cut_line(s->fp);
948
0
    } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
949
0
      status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
950
951
    /*
952
     * These should never fail because they come from our own
953
     * fmt_ident. They may fail the sane_ident test, but we know
954
     * that the name and mail pointers will at least be valid,
955
     * which is enough for our tests and printing here.
956
     */
957
0
    assert_split_ident(&ai, author_ident);
958
0
    assert_split_ident(&ci, &committer_ident);
959
960
0
    if (ident_cmp(&ai, &ci))
961
0
      status_printf_ln(s, GIT_COLOR_NORMAL,
962
0
        _("%s"
963
0
        "Author:    %.*s <%.*s>"),
964
0
        ident_shown++ ? "" : "\n",
965
0
        (int)(ai.name_end - ai.name_begin), ai.name_begin,
966
0
        (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
967
968
0
    if (author_date_is_interesting())
969
0
      status_printf_ln(s, GIT_COLOR_NORMAL,
970
0
        _("%s"
971
0
        "Date:      %s"),
972
0
        ident_shown++ ? "" : "\n",
973
0
        show_ident_date(&ai, DATE_MODE(NORMAL)));
974
975
0
    if (!committer_ident_sufficiently_given())
976
0
      status_printf_ln(s, GIT_COLOR_NORMAL,
977
0
        _("%s"
978
0
        "Committer: %.*s <%.*s>"),
979
0
        ident_shown++ ? "" : "\n",
980
0
        (int)(ci.name_end - ci.name_begin), ci.name_begin,
981
0
        (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
982
983
0
    status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
984
985
0
    saved_color_setting = s->use_color;
986
0
    s->use_color = 0;
987
0
    committable = run_status(s->fp, index_file, prefix, 1, s);
988
0
    s->use_color = saved_color_setting;
989
0
    string_list_clear(&s->change, 1);
990
7.59k
  } else {
991
7.59k
    struct object_id oid;
992
7.59k
    const char *parent = "HEAD";
993
994
7.59k
    if (!the_index.cache_nr) {
995
0
      discard_index(&the_index);
996
0
      if (repo_read_index(the_repository) < 0)
997
0
        die(_("Cannot read index"));
998
0
    }
999
1000
7.59k
    if (amend)
1001
0
      parent = "HEAD^1";
1002
1003
7.59k
    if (get_oid(parent, &oid)) {
1004
1.28k
      int i, ita_nr = 0;
1005
1006
      /* TODO: audit for interaction with sparse-index. */
1007
1.28k
      ensure_full_index(&the_index);
1008
3.85k
      for (i = 0; i < the_index.cache_nr; i++)
1009
2.57k
        if (ce_intent_to_add(the_index.cache[i]))
1010
0
          ita_nr++;
1011
1.28k
      committable = the_index.cache_nr - ita_nr > 0;
1012
6.30k
    } else {
1013
      /*
1014
       * Unless the user did explicitly request a submodule
1015
       * ignore mode by passing a command line option we do
1016
       * not ignore any changed submodule SHA-1s when
1017
       * comparing index and parent, no matter what is
1018
       * configured. Otherwise we won't commit any
1019
       * submodules which were manually staged, which would
1020
       * be really confusing.
1021
       */
1022
6.30k
      struct diff_flags flags = DIFF_FLAGS_INIT;
1023
6.30k
      flags.override_submodule_config = 1;
1024
6.30k
      if (ignore_submodule_arg &&
1025
6.30k
          !strcmp(ignore_submodule_arg, "all"))
1026
0
        flags.ignore_submodules = 1;
1027
6.30k
      committable = index_differs_from(the_repository,
1028
6.30k
               parent, &flags, 1);
1029
6.30k
    }
1030
7.59k
  }
1031
7.59k
  strbuf_release(&committer_ident);
1032
1033
7.59k
  fclose(s->fp);
1034
1035
7.59k
  if (trailer_args.nr) {
1036
0
    struct child_process run_trailer = CHILD_PROCESS_INIT;
1037
1038
0
    strvec_pushl(&run_trailer.args, "interpret-trailers",
1039
0
           "--in-place", git_path_commit_editmsg(), NULL);
1040
0
    strvec_pushv(&run_trailer.args, trailer_args.v);
1041
0
    run_trailer.git_cmd = 1;
1042
0
    if (run_command(&run_trailer))
1043
0
      die(_("unable to pass trailers to --trailers"));
1044
0
    strvec_clear(&trailer_args);
1045
0
  }
1046
1047
  /*
1048
   * Reject an attempt to record a non-merge empty commit without
1049
   * explicit --allow-empty. In the cherry-pick case, it may be
1050
   * empty due to conflict resolution, which the user should okay.
1051
   */
1052
7.59k
  if (!committable && whence != FROM_MERGE && !allow_empty &&
1053
7.59k
      !(amend && is_a_merge(current_head))) {
1054
100
    s->hints = advice_enabled(ADVICE_STATUS_HINTS);
1055
100
    s->display_comment_prefix = old_display_comment_prefix;
1056
100
    run_status(stdout, index_file, prefix, 0, s);
1057
100
    if (amend)
1058
0
      fputs(_(empty_amend_advice), stderr);
1059
100
    else if (is_from_cherry_pick(whence) ||
1060
100
       whence == FROM_REBASE_PICK) {
1061
0
      fputs(_(empty_cherry_pick_advice), stderr);
1062
0
      if (whence == FROM_CHERRY_PICK_SINGLE)
1063
0
        fputs(_(empty_cherry_pick_advice_single), stderr);
1064
0
      else if (whence == FROM_CHERRY_PICK_MULTI)
1065
0
        fputs(_(empty_cherry_pick_advice_multi), stderr);
1066
0
      else
1067
0
        fputs(_(empty_rebase_pick_advice), stderr);
1068
0
    }
1069
100
    return 0;
1070
100
  }
1071
1072
7.49k
  if (!no_verify && invoked_hook) {
1073
    /*
1074
     * Re-read the index as the pre-commit-commit hook was invoked
1075
     * and could have updated it. We must do this before we invoke
1076
     * the editor and after we invoke run_status above.
1077
     */
1078
0
    discard_index(&the_index);
1079
0
  }
1080
7.49k
  read_index_from(&the_index, index_file, get_git_dir());
1081
1082
7.49k
  if (cache_tree_update(&the_index, 0)) {
1083
0
    error(_("Error building trees"));
1084
0
    return 0;
1085
0
  }
1086
1087
7.49k
  if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
1088
7.49k
          git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
1089
0
    return 0;
1090
1091
7.49k
  if (use_editor) {
1092
0
    struct strvec env = STRVEC_INIT;
1093
1094
0
    strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
1095
0
    if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
1096
0
      fprintf(stderr,
1097
0
      _("Please supply the message using either -m or -F option.\n"));
1098
0
      exit(1);
1099
0
    }
1100
0
    strvec_clear(&env);
1101
0
  }
1102
1103
7.49k
  if (!no_verify &&
1104
7.49k
      run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1105
7.49k
          git_path_commit_editmsg(), NULL)) {
1106
0
    return 0;
1107
0
  }
1108
1109
7.49k
  return 1;
1110
7.49k
}
1111
1112
static const char *find_author_by_nickname(const char *name)
1113
0
{
1114
0
  struct rev_info revs;
1115
0
  struct commit *commit;
1116
0
  struct strbuf buf = STRBUF_INIT;
1117
0
  const char *av[20];
1118
0
  int ac = 0;
1119
1120
0
  repo_init_revisions(the_repository, &revs, NULL);
1121
0
  strbuf_addf(&buf, "--author=%s", name);
1122
0
  av[++ac] = "--all";
1123
0
  av[++ac] = "-i";
1124
0
  av[++ac] = buf.buf;
1125
0
  av[++ac] = NULL;
1126
0
  setup_revisions(ac, av, &revs, NULL);
1127
0
  revs.mailmap = xmalloc(sizeof(struct string_list));
1128
0
  string_list_init_nodup(revs.mailmap);
1129
0
  read_mailmap(revs.mailmap);
1130
1131
0
  if (prepare_revision_walk(&revs))
1132
0
    die(_("revision walk setup failed"));
1133
0
  commit = get_revision(&revs);
1134
0
  if (commit) {
1135
0
    struct pretty_print_context ctx = {0};
1136
0
    ctx.date_mode.type = DATE_NORMAL;
1137
0
    strbuf_release(&buf);
1138
0
    format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
1139
0
    release_revisions(&revs);
1140
0
    return strbuf_detach(&buf, NULL);
1141
0
  }
1142
0
  die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
1143
0
}
1144
1145
static void handle_ignored_arg(struct wt_status *s)
1146
0
{
1147
0
  if (!ignored_arg)
1148
0
    ; /* default already initialized */
1149
0
  else if (!strcmp(ignored_arg, "traditional"))
1150
0
    s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1151
0
  else if (!strcmp(ignored_arg, "no"))
1152
0
    s->show_ignored_mode = SHOW_NO_IGNORED;
1153
0
  else if (!strcmp(ignored_arg, "matching"))
1154
0
    s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1155
0
  else
1156
0
    die(_("Invalid ignored mode '%s'"), ignored_arg);
1157
0
}
1158
1159
static void handle_untracked_files_arg(struct wt_status *s)
1160
7.59k
{
1161
7.59k
  if (!untracked_files_arg)
1162
7.59k
    ; /* default already initialized */
1163
0
  else if (!strcmp(untracked_files_arg, "no"))
1164
0
    s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1165
0
  else if (!strcmp(untracked_files_arg, "normal"))
1166
0
    s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1167
0
  else if (!strcmp(untracked_files_arg, "all"))
1168
0
    s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1169
  /*
1170
   * Please update $__git_untracked_file_modes in
1171
   * git-completion.bash when you add new options
1172
   */
1173
0
  else
1174
0
    die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
1175
7.59k
}
1176
1177
static const char *read_commit_message(const char *name)
1178
0
{
1179
0
  const char *out_enc;
1180
0
  struct commit *commit;
1181
1182
0
  commit = lookup_commit_reference_by_name(name);
1183
0
  if (!commit)
1184
0
    die(_("could not lookup commit %s"), name);
1185
0
  out_enc = get_commit_output_encoding();
1186
0
  return logmsg_reencode(commit, NULL, out_enc);
1187
0
}
1188
1189
/*
1190
 * Enumerate what needs to be propagated when --porcelain
1191
 * is not in effect here.
1192
 */
1193
static struct status_deferred_config {
1194
  enum wt_status_format status_format;
1195
  int show_branch;
1196
  enum ahead_behind_flags ahead_behind;
1197
} status_deferred_config = {
1198
  STATUS_FORMAT_UNSPECIFIED,
1199
  -1, /* unspecified */
1200
  AHEAD_BEHIND_UNSPECIFIED,
1201
};
1202
1203
static void finalize_deferred_config(struct wt_status *s)
1204
7.59k
{
1205
7.59k
  int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1206
7.59k
           status_format != STATUS_FORMAT_PORCELAIN_V2 &&
1207
7.59k
           !s->null_termination);
1208
1209
7.59k
  if (s->null_termination) {
1210
0
    if (status_format == STATUS_FORMAT_NONE ||
1211
0
        status_format == STATUS_FORMAT_UNSPECIFIED)
1212
0
      status_format = STATUS_FORMAT_PORCELAIN;
1213
0
    else if (status_format == STATUS_FORMAT_LONG)
1214
0
      die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1215
0
  }
1216
1217
7.59k
  if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1218
0
    status_format = status_deferred_config.status_format;
1219
7.59k
  if (status_format == STATUS_FORMAT_UNSPECIFIED)
1220
0
    status_format = STATUS_FORMAT_NONE;
1221
1222
7.59k
  if (use_deferred_config && s->show_branch < 0)
1223
7.59k
    s->show_branch = status_deferred_config.show_branch;
1224
7.59k
  if (s->show_branch < 0)
1225
7.59k
    s->show_branch = 0;
1226
1227
  /*
1228
   * If the user did not give a "--[no]-ahead-behind" command
1229
   * line argument *AND* we will print in a human-readable format
1230
   * (short, long etc.) then we inherit from the status.aheadbehind
1231
   * config setting.  In all other cases (and porcelain V[12] formats
1232
   * in particular), we inherit _FULL for backwards compatibility.
1233
   */
1234
7.59k
  if (use_deferred_config &&
1235
7.59k
      s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1236
7.59k
    s->ahead_behind_flags = status_deferred_config.ahead_behind;
1237
1238
7.59k
  if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1239
7.59k
    s->ahead_behind_flags = AHEAD_BEHIND_FULL;
1240
7.59k
}
1241
1242
0
static void check_fixup_reword_options(int argc, const char *argv[]) {
1243
0
  if (whence != FROM_COMMIT) {
1244
0
    if (whence == FROM_MERGE)
1245
0
      die(_("You are in the middle of a merge -- cannot reword."));
1246
0
    else if (is_from_cherry_pick(whence))
1247
0
      die(_("You are in the middle of a cherry-pick -- cannot reword."));
1248
0
  }
1249
0
  if (argc)
1250
0
    die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
1251
0
  if (patch_interactive || interactive || all || also || only)
1252
0
    die(_("reword option of '%s' and '%s' cannot be used together"),
1253
0
      "--fixup", "--patch/--interactive/--all/--include/--only");
1254
0
}
1255
1256
static int parse_and_validate_options(int argc, const char *argv[],
1257
              const struct option *options,
1258
              const char * const usage[],
1259
              const char *prefix,
1260
              struct commit *current_head,
1261
              struct wt_status *s)
1262
7.59k
{
1263
7.59k
  argc = parse_options(argc, argv, prefix, options, usage, 0);
1264
7.59k
  finalize_deferred_config(s);
1265
1266
7.59k
  if (force_author && !strchr(force_author, '>'))
1267
0
    force_author = find_author_by_nickname(force_author);
1268
1269
7.59k
  if (force_author && renew_authorship)
1270
0
    die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1271
1272
7.59k
  if (logfile || have_option_m || use_message)
1273
7.59k
    use_editor = 0;
1274
1275
  /* Sanity check options */
1276
7.59k
  if (amend && !current_head)
1277
0
    die(_("You have nothing to amend."));
1278
7.59k
  if (amend && whence != FROM_COMMIT) {
1279
0
    if (whence == FROM_MERGE)
1280
0
      die(_("You are in the middle of a merge -- cannot amend."));
1281
0
    else if (is_from_cherry_pick(whence))
1282
0
      die(_("You are in the middle of a cherry-pick -- cannot amend."));
1283
0
    else if (whence == FROM_REBASE_PICK)
1284
0
      die(_("You are in the middle of a rebase -- cannot amend."));
1285
0
  }
1286
7.59k
  if (fixup_message && squash_message)
1287
0
    die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1288
7.59k
  die_for_incompatible_opt4(!!use_message, "-C",
1289
7.59k
          !!edit_message, "-c",
1290
7.59k
          !!logfile, "-F",
1291
7.59k
          !!fixup_message, "--fixup");
1292
7.59k
  die_for_incompatible_opt4(have_option_m, "-m",
1293
7.59k
          !!edit_message, "-c",
1294
7.59k
          !!use_message, "-C",
1295
7.59k
          !!logfile, "-F");
1296
7.59k
  if (use_message || edit_message || logfile ||fixup_message || have_option_m)
1297
7.59k
    template_file = NULL;
1298
7.59k
  if (edit_message)
1299
0
    use_message = edit_message;
1300
7.59k
  if (amend && !use_message && !fixup_message)
1301
0
    use_message = "HEAD";
1302
7.59k
  if (!use_message && !is_from_cherry_pick(whence) &&
1303
7.59k
      !is_from_rebase(whence) && renew_authorship)
1304
0
    die(_("--reset-author can be used only with -C, -c or --amend."));
1305
7.59k
  if (use_message) {
1306
0
    use_message_buffer = read_commit_message(use_message);
1307
0
    if (!renew_authorship) {
1308
0
      author_message = use_message;
1309
0
      author_message_buffer = use_message_buffer;
1310
0
    }
1311
0
  }
1312
7.59k
  if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1313
7.59k
      !renew_authorship) {
1314
0
    author_message = "CHERRY_PICK_HEAD";
1315
0
    author_message_buffer = read_commit_message(author_message);
1316
0
  }
1317
1318
7.59k
  if (patch_interactive)
1319
0
    interactive = 1;
1320
1321
7.59k
  die_for_incompatible_opt4(also, "-i/--include",
1322
7.59k
          only, "-o/--only",
1323
7.59k
          all, "-a/--all",
1324
7.59k
          interactive, "--interactive/-p/--patch");
1325
7.59k
  if (fixup_message) {
1326
    /*
1327
     * We limit --fixup's suboptions to only alpha characters.
1328
     * If the first character after a run of alpha is colon,
1329
     * then the part before the colon may be a known suboption
1330
     * name like `amend` or `reword`, or a misspelt suboption
1331
     * name. In either case, we treat it as
1332
     * --fixup=<suboption>:<arg>.
1333
     *
1334
     * Otherwise, we are dealing with --fixup=<commit>.
1335
     */
1336
0
    char *p = fixup_message;
1337
0
    while (isalpha(*p))
1338
0
      p++;
1339
0
    if (p > fixup_message && *p == ':') {
1340
0
      *p = '\0';
1341
0
      fixup_commit = p + 1;
1342
0
      if (!strcmp("amend", fixup_message) ||
1343
0
          !strcmp("reword", fixup_message)) {
1344
0
        fixup_prefix = "amend";
1345
0
        allow_empty = 1;
1346
0
        if (*fixup_message == 'r') {
1347
0
          check_fixup_reword_options(argc, argv);
1348
0
          only = 1;
1349
0
        }
1350
0
      } else {
1351
0
        die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1352
0
      }
1353
0
    } else {
1354
0
      fixup_commit = fixup_message;
1355
0
      fixup_prefix = "fixup";
1356
0
      use_editor = 0;
1357
0
    }
1358
0
  }
1359
1360
7.59k
  if (0 <= edit_flag)
1361
0
    use_editor = edit_flag;
1362
1363
7.59k
  cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
1364
1365
7.59k
  handle_untracked_files_arg(s);
1366
1367
7.59k
  if (all && argc > 0)
1368
0
    die(_("paths '%s ...' with -a does not make sense"),
1369
0
        argv[0]);
1370
1371
7.59k
  if (status_format != STATUS_FORMAT_NONE)
1372
0
    dry_run = 1;
1373
1374
7.59k
  return argc;
1375
7.59k
}
1376
1377
static int dry_run_commit(const char **argv, const char *prefix,
1378
        const struct commit *current_head, struct wt_status *s)
1379
0
{
1380
0
  int committable;
1381
0
  const char *index_file;
1382
1383
0
  index_file = prepare_index(argv, prefix, current_head, 1);
1384
0
  committable = run_status(stdout, index_file, prefix, 0, s);
1385
0
  rollback_index_files();
1386
1387
0
  return committable ? 0 : 1;
1388
0
}
1389
1390
define_list_config_array_extra(color_status_slots, {"added"});
1391
1392
static int parse_status_slot(const char *slot)
1393
0
{
1394
0
  if (!strcasecmp(slot, "added"))
1395
0
    return WT_STATUS_UPDATED;
1396
1397
0
  return LOOKUP_CONFIG(color_status_slots, slot);
1398
0
}
1399
1400
static int git_status_config(const char *k, const char *v, void *cb)
1401
22.7k
{
1402
22.7k
  struct wt_status *s = cb;
1403
22.7k
  const char *slot_name;
1404
1405
22.7k
  if (starts_with(k, "column."))
1406
0
    return git_column_config(k, v, "status", &s->colopts);
1407
22.7k
  if (!strcmp(k, "status.submodulesummary")) {
1408
0
    int is_bool;
1409
0
    s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1410
0
    if (is_bool && s->submodule_summary)
1411
0
      s->submodule_summary = -1;
1412
0
    return 0;
1413
0
  }
1414
22.7k
  if (!strcmp(k, "status.short")) {
1415
0
    if (git_config_bool(k, v))
1416
0
      status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1417
0
    else
1418
0
      status_deferred_config.status_format = STATUS_FORMAT_NONE;
1419
0
    return 0;
1420
0
  }
1421
22.7k
  if (!strcmp(k, "status.branch")) {
1422
0
    status_deferred_config.show_branch = git_config_bool(k, v);
1423
0
    return 0;
1424
0
  }
1425
22.7k
  if (!strcmp(k, "status.aheadbehind")) {
1426
0
    status_deferred_config.ahead_behind = git_config_bool(k, v);
1427
0
    return 0;
1428
0
  }
1429
22.7k
  if (!strcmp(k, "status.showstash")) {
1430
0
    s->show_stash = git_config_bool(k, v);
1431
0
    return 0;
1432
0
  }
1433
22.7k
  if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1434
0
    s->use_color = git_config_colorbool(k, v);
1435
0
    return 0;
1436
0
  }
1437
22.7k
  if (!strcmp(k, "status.displaycommentprefix")) {
1438
0
    s->display_comment_prefix = git_config_bool(k, v);
1439
0
    return 0;
1440
0
  }
1441
22.7k
  if (skip_prefix(k, "status.color.", &slot_name) ||
1442
22.7k
      skip_prefix(k, "color.status.", &slot_name)) {
1443
0
    int slot = parse_status_slot(slot_name);
1444
0
    if (slot < 0)
1445
0
      return 0;
1446
0
    if (!v)
1447
0
      return config_error_nonbool(k);
1448
0
    return color_parse(v, s->color_palette[slot]);
1449
0
  }
1450
22.7k
  if (!strcmp(k, "status.relativepaths")) {
1451
0
    s->relative_paths = git_config_bool(k, v);
1452
0
    return 0;
1453
0
  }
1454
22.7k
  if (!strcmp(k, "status.showuntrackedfiles")) {
1455
0
    if (!v)
1456
0
      return config_error_nonbool(k);
1457
0
    else if (!strcmp(v, "no"))
1458
0
      s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1459
0
    else if (!strcmp(v, "normal"))
1460
0
      s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1461
0
    else if (!strcmp(v, "all"))
1462
0
      s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1463
0
    else
1464
0
      return error(_("Invalid untracked files mode '%s'"), v);
1465
0
    return 0;
1466
0
  }
1467
22.7k
  if (!strcmp(k, "diff.renamelimit")) {
1468
0
    if (s->rename_limit == -1)
1469
0
      s->rename_limit = git_config_int(k, v);
1470
0
    return 0;
1471
0
  }
1472
22.7k
  if (!strcmp(k, "status.renamelimit")) {
1473
0
    s->rename_limit = git_config_int(k, v);
1474
0
    return 0;
1475
0
  }
1476
22.7k
  if (!strcmp(k, "diff.renames")) {
1477
0
    if (s->detect_rename == -1)
1478
0
      s->detect_rename = git_config_rename(k, v);
1479
0
    return 0;
1480
0
  }
1481
22.7k
  if (!strcmp(k, "status.renames")) {
1482
0
    s->detect_rename = git_config_rename(k, v);
1483
0
    return 0;
1484
0
  }
1485
22.7k
  return git_diff_ui_config(k, v, NULL);
1486
22.7k
}
1487
1488
int cmd_status(int argc, const char **argv, const char *prefix)
1489
0
{
1490
0
  static int no_renames = -1;
1491
0
  static const char *rename_score_arg = (const char *)-1;
1492
0
  static struct wt_status s;
1493
0
  unsigned int progress_flag = 0;
1494
0
  int fd;
1495
0
  struct object_id oid;
1496
0
  static struct option builtin_status_options[] = {
1497
0
    OPT__VERBOSE(&verbose, N_("be verbose")),
1498
0
    OPT_SET_INT('s', "short", &status_format,
1499
0
          N_("show status concisely"), STATUS_FORMAT_SHORT),
1500
0
    OPT_BOOL('b', "branch", &s.show_branch,
1501
0
       N_("show branch information")),
1502
0
    OPT_BOOL(0, "show-stash", &s.show_stash,
1503
0
       N_("show stash information")),
1504
0
    OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1505
0
       N_("compute full ahead/behind values")),
1506
0
    OPT_CALLBACK_F(0, "porcelain", &status_format,
1507
0
      N_("version"), N_("machine-readable output"),
1508
0
      PARSE_OPT_OPTARG, opt_parse_porcelain),
1509
0
    OPT_SET_INT(0, "long", &status_format,
1510
0
          N_("show status in long format (default)"),
1511
0
          STATUS_FORMAT_LONG),
1512
0
    OPT_BOOL('z', "null", &s.null_termination,
1513
0
       N_("terminate entries with NUL")),
1514
0
    { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1515
0
      N_("mode"),
1516
0
      N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1517
0
      PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1518
0
    { OPTION_STRING, 0, "ignored", &ignored_arg,
1519
0
      N_("mode"),
1520
0
      N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1521
0
      PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
1522
0
    { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1523
0
      N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1524
0
      PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1525
0
    OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1526
0
    OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
1527
0
    OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
1528
0
      N_("n"), N_("detect renames, optionally set similarity index"),
1529
0
      PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
1530
0
    OPT_END(),
1531
0
  };
1532
1533
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
1534
0
    usage_with_options(builtin_status_usage, builtin_status_options);
1535
1536
0
  prepare_repo_settings(the_repository);
1537
0
  the_repository->settings.command_requires_full_index = 0;
1538
1539
0
  status_init_config(&s, git_status_config);
1540
0
  argc = parse_options(argc, argv, prefix,
1541
0
           builtin_status_options,
1542
0
           builtin_status_usage, 0);
1543
0
  finalize_colopts(&s.colopts, -1);
1544
0
  finalize_deferred_config(&s);
1545
1546
0
  handle_untracked_files_arg(&s);
1547
0
  handle_ignored_arg(&s);
1548
1549
0
  if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1550
0
      s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1551
0
    die(_("Unsupported combination of ignored and untracked-files arguments"));
1552
1553
0
  parse_pathspec(&s.pathspec, 0,
1554
0
           PATHSPEC_PREFER_FULL,
1555
0
           prefix, argv);
1556
1557
0
  if (status_format != STATUS_FORMAT_PORCELAIN &&
1558
0
      status_format != STATUS_FORMAT_PORCELAIN_V2)
1559
0
    progress_flag = REFRESH_PROGRESS;
1560
0
  repo_read_index(the_repository);
1561
0
  refresh_index(&the_index,
1562
0
          REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1563
0
          &s.pathspec, NULL, NULL);
1564
1565
0
  if (use_optional_locks())
1566
0
    fd = repo_hold_locked_index(the_repository, &index_lock, 0);
1567
0
  else
1568
0
    fd = -1;
1569
1570
0
  s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
1571
0
  if (!s.is_initial)
1572
0
    oidcpy(&s.oid_commit, &oid);
1573
1574
0
  s.ignore_submodule_arg = ignore_submodule_arg;
1575
0
  s.status_format = status_format;
1576
0
  s.verbose = verbose;
1577
0
  if (no_renames != -1)
1578
0
    s.detect_rename = !no_renames;
1579
0
  if ((intptr_t)rename_score_arg != -1) {
1580
0
    if (s.detect_rename < DIFF_DETECT_RENAME)
1581
0
      s.detect_rename = DIFF_DETECT_RENAME;
1582
0
    if (rename_score_arg)
1583
0
      s.rename_score = parse_rename_score(&rename_score_arg);
1584
0
  }
1585
1586
0
  wt_status_collect(&s);
1587
1588
0
  if (0 <= fd)
1589
0
    repo_update_index_if_able(the_repository, &index_lock);
1590
1591
0
  if (s.relative_paths)
1592
0
    s.prefix = prefix;
1593
1594
0
  wt_status_print(&s);
1595
0
  wt_status_collect_free_buffers(&s);
1596
1597
0
  return 0;
1598
0
}
1599
1600
static int git_commit_config(const char *k, const char *v, void *cb)
1601
22.7k
{
1602
22.7k
  struct wt_status *s = cb;
1603
22.7k
  int status;
1604
1605
22.7k
  if (!strcmp(k, "commit.template"))
1606
0
    return git_config_pathname(&template_file, k, v);
1607
22.7k
  if (!strcmp(k, "commit.status")) {
1608
0
    include_status = git_config_bool(k, v);
1609
0
    return 0;
1610
0
  }
1611
22.7k
  if (!strcmp(k, "commit.cleanup"))
1612
0
    return git_config_string(&cleanup_arg, k, v);
1613
22.7k
  if (!strcmp(k, "commit.gpgsign")) {
1614
0
    sign_commit = git_config_bool(k, v) ? "" : NULL;
1615
0
    return 0;
1616
0
  }
1617
22.7k
  if (!strcmp(k, "commit.verbose")) {
1618
0
    int is_bool;
1619
0
    config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
1620
0
    return 0;
1621
0
  }
1622
1623
22.7k
  status = git_gpg_config(k, v, NULL);
1624
22.7k
  if (status)
1625
0
    return status;
1626
22.7k
  return git_status_config(k, v, s);
1627
22.7k
}
1628
1629
int cmd_commit(int argc, const char **argv, const char *prefix)
1630
7.59k
{
1631
7.59k
  static struct wt_status s;
1632
7.59k
  static struct option builtin_commit_options[] = {
1633
7.59k
    OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1634
7.59k
    OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1635
1636
7.59k
    OPT_GROUP(N_("Commit message options")),
1637
7.59k
    OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1638
7.59k
    OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1639
7.59k
    OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1640
7.59k
    OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1641
7.59k
    OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1642
7.59k
    OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1643
    /*
1644
     * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1645
     * and only translate <commit>.
1646
     */
1647
7.59k
    OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1648
7.59k
    OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1649
7.59k
    OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1650
7.59k
    OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
1651
7.59k
    OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
1652
7.59k
    OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1653
7.59k
    OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1654
7.59k
    OPT_CLEANUP(&cleanup_arg),
1655
7.59k
    OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1656
7.59k
    { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
1657
7.59k
      N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1658
    /* end commit message options */
1659
1660
7.59k
    OPT_GROUP(N_("Commit contents options")),
1661
7.59k
    OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1662
7.59k
    OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1663
7.59k
    OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1664
7.59k
    OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1665
7.59k
    OPT_BOOL('o', "only", &only, N_("commit only specified files")),
1666
7.59k
    OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
1667
7.59k
    OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
1668
7.59k
    OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1669
7.59k
          STATUS_FORMAT_SHORT),
1670
7.59k
    OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1671
7.59k
    OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1672
7.59k
       N_("compute full ahead/behind values")),
1673
7.59k
    OPT_SET_INT(0, "porcelain", &status_format,
1674
7.59k
          N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1675
7.59k
    OPT_SET_INT(0, "long", &status_format,
1676
7.59k
          N_("show status in long format (default)"),
1677
7.59k
          STATUS_FORMAT_LONG),
1678
7.59k
    OPT_BOOL('z', "null", &s.null_termination,
1679
7.59k
       N_("terminate entries with NUL")),
1680
7.59k
    OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1681
7.59k
    OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1682
7.59k
    { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1683
7.59k
    OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1684
7.59k
    OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1685
    /* end commit contents options */
1686
1687
7.59k
    OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1688
7.59k
        N_("ok to record an empty change")),
1689
7.59k
    OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1690
7.59k
        N_("ok to record a change with an empty message")),
1691
1692
7.59k
    OPT_END()
1693
7.59k
  };
1694
1695
7.59k
  struct strbuf sb = STRBUF_INIT;
1696
7.59k
  struct strbuf author_ident = STRBUF_INIT;
1697
7.59k
  const char *index_file, *reflog_msg;
1698
7.59k
  struct object_id oid;
1699
7.59k
  struct commit_list *parents = NULL;
1700
7.59k
  struct stat statbuf;
1701
7.59k
  struct commit *current_head = NULL;
1702
7.59k
  struct commit_extra_header *extra = NULL;
1703
7.59k
  struct strbuf err = STRBUF_INIT;
1704
7.59k
  int ret = 0;
1705
1706
7.59k
  if (argc == 2 && !strcmp(argv[1], "-h"))
1707
0
    usage_with_options(builtin_commit_usage, builtin_commit_options);
1708
1709
7.59k
  prepare_repo_settings(the_repository);
1710
7.59k
  the_repository->settings.command_requires_full_index = 0;
1711
1712
7.59k
  status_init_config(&s, git_commit_config);
1713
7.59k
  s.commit_template = 1;
1714
7.59k
  status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1715
7.59k
  s.colopts = 0;
1716
1717
7.59k
  if (get_oid("HEAD", &oid))
1718
1.28k
    current_head = NULL;
1719
6.30k
  else {
1720
6.30k
    current_head = lookup_commit_or_die(&oid, "HEAD");
1721
6.30k
    if (parse_commit(current_head))
1722
0
      die(_("could not parse HEAD commit"));
1723
6.30k
  }
1724
7.59k
  verbose = -1; /* unspecified */
1725
7.59k
  argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1726
7.59k
            builtin_commit_usage,
1727
7.59k
            prefix, current_head, &s);
1728
7.59k
  if (verbose == -1)
1729
7.59k
    verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1730
1731
7.59k
  if (dry_run)
1732
0
    return dry_run_commit(argv, prefix, current_head, &s);
1733
7.59k
  index_file = prepare_index(argv, prefix, current_head, 0);
1734
1735
  /* Set up everything for writing the commit object.  This includes
1736
     running hooks, writing the trees, and interacting with the user.  */
1737
7.59k
  if (!prepare_to_commit(index_file, prefix,
1738
7.59k
             current_head, &s, &author_ident)) {
1739
100
    ret = 1;
1740
100
    rollback_index_files();
1741
100
    goto cleanup;
1742
100
  }
1743
1744
  /* Determine parents */
1745
7.49k
  reflog_msg = getenv("GIT_REFLOG_ACTION");
1746
7.49k
  if (!current_head) {
1747
1.28k
    if (!reflog_msg)
1748
1.28k
      reflog_msg = "commit (initial)";
1749
6.20k
  } else if (amend) {
1750
0
    if (!reflog_msg)
1751
0
      reflog_msg = "commit (amend)";
1752
0
    parents = copy_commit_list(current_head->parents);
1753
6.20k
  } else if (whence == FROM_MERGE) {
1754
0
    struct strbuf m = STRBUF_INIT;
1755
0
    FILE *fp;
1756
0
    int allow_fast_forward = 1;
1757
0
    struct commit_list **pptr = &parents;
1758
1759
0
    if (!reflog_msg)
1760
0
      reflog_msg = "commit (merge)";
1761
0
    pptr = commit_list_append(current_head, pptr);
1762
0
    fp = xfopen(git_path_merge_head(the_repository), "r");
1763
0
    while (strbuf_getline_lf(&m, fp) != EOF) {
1764
0
      struct commit *parent;
1765
1766
0
      parent = get_merge_parent(m.buf);
1767
0
      if (!parent)
1768
0
        die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1769
0
      pptr = commit_list_append(parent, pptr);
1770
0
    }
1771
0
    fclose(fp);
1772
0
    strbuf_release(&m);
1773
0
    if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1774
0
      if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
1775
0
        die_errno(_("could not read MERGE_MODE"));
1776
0
      if (!strcmp(sb.buf, "no-ff"))
1777
0
        allow_fast_forward = 0;
1778
0
    }
1779
0
    if (allow_fast_forward)
1780
0
      reduce_heads_replace(&parents);
1781
6.20k
  } else {
1782
6.20k
    if (!reflog_msg)
1783
6.20k
      reflog_msg = is_from_cherry_pick(whence)
1784
6.20k
          ? "commit (cherry-pick)"
1785
6.20k
          : is_from_rebase(whence)
1786
6.20k
          ? "commit (rebase)"
1787
6.20k
          : "commit";
1788
6.20k
    commit_list_insert(current_head, &parents);
1789
6.20k
  }
1790
1791
  /* Finally, get the commit message */
1792
7.49k
  strbuf_reset(&sb);
1793
7.49k
  if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
1794
0
    int saved_errno = errno;
1795
0
    rollback_index_files();
1796
0
    die(_("could not read commit message: %s"), strerror(saved_errno));
1797
0
  }
1798
1799
7.49k
  cleanup_message(&sb, cleanup_mode, verbose);
1800
1801
7.49k
  if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
1802
0
    rollback_index_files();
1803
0
    fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1804
0
    exit(1);
1805
0
  }
1806
7.49k
  if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
1807
0
    rollback_index_files();
1808
0
    fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1809
0
    exit(1);
1810
0
  }
1811
1812
7.49k
  if (fixup_message && starts_with(sb.buf, "amend! ") &&
1813
7.49k
      !allow_empty_message) {
1814
0
    struct strbuf body = STRBUF_INIT;
1815
0
    size_t len = commit_subject_length(sb.buf);
1816
0
    strbuf_addstr(&body, sb.buf + len);
1817
0
    if (message_is_empty(&body, cleanup_mode)) {
1818
0
      rollback_index_files();
1819
0
      fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1820
0
      exit(1);
1821
0
    }
1822
0
    strbuf_release(&body);
1823
0
  }
1824
1825
7.49k
  if (amend) {
1826
0
    const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
1827
0
    extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1828
7.49k
  } else {
1829
7.49k
    struct commit_extra_header **tail = &extra;
1830
7.49k
    append_merge_tag_headers(parents, &tail);
1831
7.49k
  }
1832
1833
7.49k
  if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
1834
7.49k
         parents, &oid, author_ident.buf, NULL,
1835
7.49k
         sign_commit, extra)) {
1836
0
    rollback_index_files();
1837
0
    die(_("failed to write commit object"));
1838
0
  }
1839
7.49k
  free_commit_extra_headers(extra);
1840
1841
7.49k
  if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1842
7.49k
            &err)) {
1843
0
    rollback_index_files();
1844
0
    die("%s", err.buf);
1845
0
  }
1846
1847
7.49k
  sequencer_post_commit_cleanup(the_repository, 0);
1848
7.49k
  unlink(git_path_merge_head(the_repository));
1849
7.49k
  unlink(git_path_merge_msg(the_repository));
1850
7.49k
  unlink(git_path_merge_mode(the_repository));
1851
7.49k
  unlink(git_path_squash_msg(the_repository));
1852
1853
7.49k
  if (commit_index_files())
1854
0
    die(_("repository has been updated, but unable to write\n"
1855
0
          "new_index file. Check that disk is not full and quota is\n"
1856
0
          "not exceeded, and then \"git restore --staged :/\" to recover."));
1857
1858
7.49k
  git_test_write_commit_graph_or_die();
1859
1860
7.49k
  repo_rerere(the_repository, 0);
1861
7.49k
  run_auto_maintenance(quiet);
1862
7.49k
  run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1863
7.49k
      NULL);
1864
7.49k
  if (amend && !no_post_rewrite) {
1865
0
    commit_post_rewrite(the_repository, current_head, &oid);
1866
0
  }
1867
7.49k
  if (!quiet) {
1868
7.49k
    unsigned int flags = 0;
1869
1870
7.49k
    if (!current_head)
1871
1.28k
      flags |= SUMMARY_INITIAL_COMMIT;
1872
7.49k
    if (author_date_is_interesting())
1873
0
      flags |= SUMMARY_SHOW_AUTHOR_DATE;
1874
7.49k
    print_commit_summary(the_repository, prefix,
1875
7.49k
             &oid, flags);
1876
7.49k
  }
1877
1878
7.49k
  apply_autostash(git_path_merge_autostash(the_repository));
1879
1880
7.59k
cleanup:
1881
7.59k
  strbuf_release(&author_ident);
1882
7.59k
  strbuf_release(&err);
1883
7.59k
  strbuf_release(&sb);
1884
7.59k
  return ret;
1885
7.49k
}