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