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