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