blob: abbed750276e42339c67a1f341e47da66d7af25d [file] [log] [blame]
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001/*
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
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07008#define USE_THE_INDEX_COMPATIBILITY_MACROS
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05009#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020011#include "lockfile.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050012#include "cache-tree.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010013#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080014#include "dir.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050015#include "builtin.h"
16#include "diff.h"
17#include "diffcore.h"
18#include "commit.h"
19#include "revision.h"
20#include "wt-status.h"
21#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020022#include "hook.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050023#include "refs.h"
24#include "log-tree.h"
25#include "strbuf.h"
26#include "utf8.h"
27#include "parse-options.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010028#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020029#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080030#include "unpack-trees.h"
Junio C Hamano76e2f7c2009-08-07 23:31:57 -070031#include "quote.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020032#include "submodule.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070033#include "gpg-interface.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070034#include "column.h"
Miklos Vajna5ed75e22012-09-14 08:52:03 +020035#include "sequencer.h"
Antoine Pelisseea167942013-08-23 15:48:31 +020036#include "mailmap.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020037#include "help.h"
Derrick Stolee64043552018-07-20 16:33:04 +000038#include "commit-reach.h"
Derrick Stolee859fdc02018-08-29 05:49:04 -070039#include "commit-graph.h"
Ævar Arnfjörð Bjarmason88c7b4c2022-02-16 09:14:02 +010040#include "pretty.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050041
42static const char * const builtin_commit_usage[] = {
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020043 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
44 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
45 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
46 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
47 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
48 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
49 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
50 " [--] [<pathspec>...]"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050051 NULL
52};
53
Shawn Bohrer2f02b252007-12-02 23:02:09 -060054static const char * const builtin_status_usage[] = {
Ævar Arnfjörð Bjarmason463ea0c2022-10-13 17:39:21 +020055 N_("git status [<options>] [--] [<pathspec>...]"),
Shawn Bohrer2f02b252007-12-02 23:02:09 -060056 NULL
57};
58
Jeff Kingf197ed22010-06-06 20:41:46 -040059static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000060N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040061"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000062"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040063
Jay Soffian37f7a852011-02-19 23:12:29 -050064static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070065N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050066"If you wish to commit it anyway, use:\n"
67"\n"
68" git commit --allow-empty\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040069"\n");
70
Phillip Wood430b75f2019-12-06 16:06:12 +000071static const char empty_rebase_pick_advice[] =
72N_("Otherwise, please use 'git rebase --skip'\n");
73
Jeff Kingc17592a2013-07-26 19:39:28 -040074static const char empty_cherry_pick_advice_single[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053075N_("Otherwise, please use 'git cherry-pick --skip'\n");
Jeff Kingc17592a2013-07-26 19:39:28 -040076
77static const char empty_cherry_pick_advice_multi[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053078N_("and then use:\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050079"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053080" git cherry-pick --continue\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040081"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053082"to resume cherry-picking the remaining commits.\n"
83"If you wish to skip this commit, use:\n"
84"\n"
85" git cherry-pick --skip\n"
86"\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050087
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020088static const char *color_status_slots[] = {
89 [WT_STATUS_HEADER] = "header",
90 [WT_STATUS_UPDATED] = "updated",
91 [WT_STATUS_CHANGED] = "changed",
92 [WT_STATUS_UNTRACKED] = "untracked",
93 [WT_STATUS_NOBRANCH] = "noBranch",
94 [WT_STATUS_UNMERGED] = "unmerged",
95 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
96 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
97 [WT_STATUS_ONBRANCH] = "branch",
98};
99
Jay Soffian37f7a852011-02-19 23:12:29 -0500100static const char *use_message_buffer;
Junio C Hamano28886052007-11-18 01:52:55 -0800101static struct lock_file index_lock; /* real index */
102static struct lock_file false_lock; /* used only for partial commits */
103static enum {
104 COMMIT_AS_IS = 1,
105 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000106 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -0800107} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500108
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700109static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -0400110static const char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -0500111/*
112 * The _message variables are commit names from which to take
113 * the commit message and/or authorship.
114 */
115static const char *author_message, *author_message_buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500116static char *edit_message, *use_message;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530117static char *fixup_message, *fixup_commit, *squash_message;
118static const char *fixup_prefix;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800119static int all, also, interactive, patch_interactive, only, amend, signoff;
120static int edit_flag = -1; /* unspecified */
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200121static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Pranit Bauvaaaab8422016-05-05 15:20:02 +0530122static int config_commit_verbose = -1; /* unspecified */
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000123static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400124static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000125static char *sign_commit, *pathspec_from_file;
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000126static struct strvec trailer_args = STRVEC_INIT;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700127
Alex Riesen5f065732007-12-22 19:46:24 +0100128/*
129 * The default commit message cleanup mode will remove the lines
130 * beginning with # (shell comments) and leading and trailing
131 * whitespaces (empty lines or containing only whitespaces)
132 * if editor is used, and only the whitespaces if the message
133 * is specified explicitly.
134 */
Phillip Woodd0aaa462017-11-10 11:09:42 +0000135static enum commit_msg_cleanup_mode cleanup_mode;
Ralf Thielow51fb3a32013-01-10 18:45:59 +0100136static const char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500137
Jay Soffian37f7a852011-02-19 23:12:29 -0500138static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700139static int use_editor = 1, include_status = 1;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400140static int have_option_m;
Jeff King2c477892011-12-18 00:03:22 -0500141static struct strbuf message = STRBUF_INIT;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000142
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400143static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
Junio C Hamano84b42022013-06-24 11:41:40 -0700144
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000145static int opt_pass_trailer(const struct option *opt, const char *arg, int unset)
146{
147 BUG_ON_OPT_NEG(unset);
148
Jeff King116761b2022-10-06 09:11:31 -0400149 strvec_pushl(opt->value, "--trailer", arg, NULL);
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000150 return 0;
151}
152
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400153static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
154{
155 enum wt_status_format *value = (enum wt_status_format *)opt->value;
156 if (unset)
157 *value = STATUS_FORMAT_NONE;
158 else if (!arg)
159 *value = STATUS_FORMAT_PORCELAIN;
160 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
161 *value = STATUS_FORMAT_PORCELAIN;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400162 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
163 *value = STATUS_FORMAT_PORCELAIN_V2;
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400164 else
165 die("unsupported porcelain version '%s'", arg);
166
167 return 0;
168}
Jeff King7c9f7032009-09-05 04:59:56 -0400169
Johannes Schindelinf9568532007-11-11 17:36:39 +0000170static int opt_parse_m(const struct option *opt, const char *arg, int unset)
171{
172 struct strbuf *buf = opt->value;
René Scharfe25206772013-05-25 23:43:34 +0200173 if (unset) {
174 have_option_m = 0;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000175 strbuf_setlen(buf, 0);
René Scharfe25206772013-05-25 23:43:34 +0200176 } else {
177 have_option_m = 1;
Brandon Caseya24a41e2013-02-18 20:17:06 -0800178 if (buf->len)
179 strbuf_addch(buf, '\n');
Johannes Schindelinf9568532007-11-11 17:36:39 +0000180 strbuf_addstr(buf, arg);
Brandon Caseya24a41e2013-02-18 20:17:06 -0800181 strbuf_complete_line(buf);
Johannes Schindelinf9568532007-11-11 17:36:39 +0000182 }
183 return 0;
184}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500185
Ben Pearte8b2dc22018-05-11 15:38:58 +0000186static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
187{
188 const char **value = opt->value;
Jeff King517fe802018-11-05 01:45:42 -0500189
190 BUG_ON_OPT_NEG(unset);
191
Ben Pearte8b2dc22018-05-11 15:38:58 +0000192 if (arg != NULL && *arg == '=')
193 arg = arg + 1;
194
195 *value = arg;
196 return 0;
197}
198
Jay Soffian37f7a852011-02-19 23:12:29 -0500199static void determine_whence(struct wt_status *s)
200{
Stefan Beller102de882018-05-17 15:51:51 -0700201 if (file_exists(git_path_merge_head(the_repository)))
Jay Soffian37f7a852011-02-19 23:12:29 -0500202 whence = FROM_MERGE;
Phillip Wood901ba7b2019-12-06 16:06:11 +0000203 else if (!sequencer_determine_whence(the_repository, &whence))
Jay Soffian37f7a852011-02-19 23:12:29 -0500204 whence = FROM_COMMIT;
205 if (s)
206 s->whence = whence;
207}
208
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200209static void status_init_config(struct wt_status *s, config_fn_t fn)
210{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100211 wt_status_prepare(the_repository, s);
Eckhard S. Maaßdc6b1d92018-05-04 13:12:15 +0200212 init_diff_ui_defaults();
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200213 git_config(fn, s);
214 determine_whence(s);
Ben Boeckeled9bff02021-08-23 12:44:00 +0200215 s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200216}
217
Junio C Hamano28886052007-11-18 01:52:55 -0800218static void rollback_index_files(void)
219{
220 switch (commit_style) {
221 case COMMIT_AS_IS:
222 break; /* nothing to do */
223 case COMMIT_NORMAL:
224 rollback_lock_file(&index_lock);
225 break;
226 case COMMIT_PARTIAL:
227 rollback_lock_file(&index_lock);
228 rollback_lock_file(&false_lock);
229 break;
230 }
231}
232
Brandon Casey5a9dd392008-01-23 11:21:22 -0600233static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800234{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600235 int err = 0;
236
Junio C Hamano28886052007-11-18 01:52:55 -0800237 switch (commit_style) {
238 case COMMIT_AS_IS:
239 break; /* nothing to do */
240 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600241 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800242 break;
243 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600244 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800245 rollback_lock_file(&false_lock);
246 break;
247 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600248
249 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800250}
251
252/*
253 * Take a union of paths in the index and the named tree (typically, "HEAD"),
254 * and return the paths that match the given pattern in list.
255 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100256static int list_paths(struct string_list *list, const char *with_tree,
Jeff Kingc5c33502019-03-20 04:15:48 -0400257 const struct pathspec *pattern)
Junio C Hamano28886052007-11-18 01:52:55 -0800258{
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700259 int i, ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800260 char *m;
261
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700262 if (!pattern->nr)
Junio C Hamanob9a08012012-07-15 21:39:48 -0700263 return 0;
264
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700265 m = xcalloc(1, pattern->nr);
Junio C Hamano28886052007-11-18 01:52:55 -0800266
Clemens Buchacher8894d532011-07-30 19:13:47 +0200267 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200268 char *max_prefix = common_prefix(pattern);
Brandon Williams86238e02018-04-03 10:57:45 -0700269 overlay_tree_on_index(&the_index, with_tree, max_prefix);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200270 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200271 }
Junio C Hamano28886052007-11-18 01:52:55 -0800272
Derrick Stoleecb8388d2021-04-01 01:49:45 +0000273 /* TODO: audit for interaction with sparse-index. */
274 ensure_full_index(&the_index);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100275 for (i = 0; i < the_index.cache_nr; i++) {
276 const struct cache_entry *ce = the_index.cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700277 struct string_list_item *item;
278
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800279 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800280 continue;
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200281 if (!ce_path_match(&the_index, ce, pattern, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800282 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100283 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700284 if (ce_skip_worktree(ce))
285 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800286 }
287
Jeff Kingc5c33502019-03-20 04:15:48 -0400288 ret = report_path_error(m, pattern);
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700289 free(m);
290 return ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800291}
292
Johannes Schindelinc455c872008-07-21 19:03:49 +0100293static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800294{
295 int i;
296 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700297 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100298 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700299
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700300 /* p->util is skip-worktree */
301 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700302 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700303
Johannes Schindelinc455c872008-07-21 19:03:49 +0100304 if (!lstat(p->string, &st)) {
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +0100305 if (add_to_index(&the_index, p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000306 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200307 } else
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100308 remove_file_from_index(&the_index, p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800309 }
310}
311
Junio C Hamano06bb6432011-08-19 11:58:18 -0700312static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800313{
314 struct tree *tree;
315 struct unpack_trees_options opts;
316 struct tree_desc t;
317
Junio C Hamano06bb6432011-08-19 11:58:18 -0700318 if (!current_head) {
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800319 discard_cache();
320 return;
321 }
322
323 memset(&opts, 0, sizeof(opts));
324 opts.head_idx = 1;
325 opts.index_only = 1;
326 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800327 opts.src_index = &the_index;
328 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800329
330 opts.fn = oneway_merge;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000331 tree = parse_tree_indirect(&current_head->object.oid);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800332 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000333 die(_("failed to unpack HEAD tree object"));
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800334 parse_tree(tree);
335 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500336 if (unpack_trees(1, &t, &opts))
337 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800338}
339
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100340static 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 */
346 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
347 die_resolve_conflict("commit");
348}
349
Jeff Kinge885a842020-09-30 08:28:18 -0400350static const char *prepare_index(const char **argv, const char *prefix,
Michael Haggerty35ff08b2014-10-01 12:28:17 +0200351 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500352{
Martin Ågrendd1055e2017-09-23 01:34:49 +0200353 struct string_list partial = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700354 struct pathspec pathspec;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700355 int refresh_flags = REFRESH_QUIET;
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200356 const char *ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500357
Junio C Hamano50b7e702009-08-04 23:49:33 -0700358 if (is_status)
359 refresh_flags |= REFRESH_UNMERGED;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700360 parse_pathspec(&pathspec, 0,
361 PATHSPEC_PREFER_FULL,
362 prefix, argv);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500363
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000364 if (pathspec_from_file) {
365 if (interactive)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000366 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000367
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000368 if (all)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000369 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000370
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000371 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000372 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000373
374 parse_pathspec_file(&pathspec, 0,
375 PATHSPEC_PREFER_FULL,
376 prefix, pathspec_from_file, pathspec_file_nul);
377 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000378 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000379 }
380
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530381 if (!pathspec.nr && (also || (only && !allow_empty &&
382 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000383 die(_("No paths with --include/--only does not make sense."));
384
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700385 if (read_cache_preload(&pathspec) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000386 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800387
Conrad Irwin1020d082011-05-06 22:59:59 -0700388 if (interactive) {
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000389 char *old_index_env = NULL, *old_repo_index_file;
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800390 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700391
392 refresh_cache_or_die(refresh_flags);
393
Martin Ågren812d6b02017-10-06 22:12:12 +0200394 if (write_locked_index(&the_index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700395 die(_("unable to create temporary index"));
396
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000397 old_repo_index_file = the_repository->index_file;
398 the_repository->index_file =
399 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500400 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000401 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700402
Jeff Kinge885a842020-09-30 08:28:18 -0400403 if (interactive_add(argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700404 die(_("interactive add failed"));
405
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000406 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700407 if (old_index_env && *old_index_env)
408 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
409 else
410 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500411 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700412
413 discard_cache();
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200414 read_cache_from(get_lock_file_path(&index_lock));
David Turner9c4d6c02014-07-13 13:28:19 -0700415 if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700416 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700417 die(_("unable to write index file"));
Martin Ågren812d6b02017-10-06 22:12:12 +0200418 if (write_locked_index(&the_index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700419 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700420 } else
421 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700422
423 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200424 ret = get_lock_file_path(&index_lock);
425 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700426 }
427
Junio C Hamano28886052007-11-18 01:52:55 -0800428 /*
429 * Non partial, non as-is commit.
430 *
431 * (1) get the real index;
432 * (2) update the_index as necessary;
433 * (3) write the_index out to the real index (still locked);
434 * (4) return the name of the locked index file.
435 *
436 * The caller should run hooks on the locked real index, and
437 * (A) if all goes well, commit the real index;
438 * (B) on failure, rollback the real index.
439 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700440 if (all || (also && pathspec.nr)) {
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800441 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100442 add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100443 refresh_cache_or_die(refresh_flags);
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700444 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200445 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000446 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800447 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200448 ret = get_lock_file_path(&index_lock);
449 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500450 }
451
Junio C Hamano28886052007-11-18 01:52:55 -0800452 /*
453 * As-is commit.
454 *
455 * (1) return the name of the real index file.
456 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200457 * The caller should run hooks on the real index,
458 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800459 * We still need to refresh the index here.
460 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700461 if (!only && !pathspec.nr) {
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800462 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100463 refresh_cache_or_die(refresh_flags);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100464 if (the_index.cache_changed
465 || !cache_tree_fully_valid(the_index.cache_tree))
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700466 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren61000812018-03-01 21:40:20 +0100467 if (write_locked_index(&the_index, &index_lock,
468 COMMIT_LOCK | SKIP_IF_UNCHANGED))
469 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800470 commit_style = COMMIT_AS_IS;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200471 ret = get_index_file();
472 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500473 }
474
Junio C Hamano28886052007-11-18 01:52:55 -0800475 /*
476 * A partial commit.
477 *
478 * (0) find the set of affected paths;
479 * (1) get lock on the real index file;
480 * (2) update the_index with the given paths;
481 * (3) write the_index out to the real index (still locked);
482 * (4) get lock on the false index file;
483 * (5) reset the_index from HEAD;
484 * (6) update the_index the same way as (2);
485 * (7) write the_index out to the false index file;
486 * (8) return the name of the false index file (still locked);
487 *
488 * The caller should run hooks on the locked false index, and
489 * create commit from it. Then
490 * (A) if all goes well, commit the real index;
491 * (B) on failure, rollback the real index;
492 * In either case, rollback the false index.
493 */
494 commit_style = COMMIT_PARTIAL;
495
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000496 if (whence != FROM_COMMIT) {
497 if (whence == FROM_MERGE)
498 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000499 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000500 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000501 else if (is_from_rebase(whence))
502 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000503 }
Junio C Hamano28886052007-11-18 01:52:55 -0800504
Jeff Kingc5c33502019-03-20 04:15:48 -0400505 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800506 exit(1);
507
508 discard_cache();
509 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000510 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800511
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800512 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800513 add_remove_files(&partial);
Kristian Høgsbergef12b502007-11-12 15:48:22 -0500514 refresh_cache(REFRESH_QUIET);
David Turner9c4d6c02014-07-13 13:28:19 -0700515 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200516 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000517 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500518
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700519 hold_lock_file_for_update(&false_lock,
520 git_path("next-index-%"PRIuMAX,
521 (uintmax_t) getpid()),
522 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800523
Junio C Hamano06bb6432011-08-19 11:58:18 -0700524 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800525 add_remove_files(&partial);
Kristian Høgsbergd37d3202007-11-09 11:40:27 -0500526 refresh_cache(REFRESH_QUIET);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500527
Martin Ågren812d6b02017-10-06 22:12:12 +0200528 if (write_locked_index(&the_index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000529 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500530
531 discard_cache();
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200532 ret = get_lock_file_path(&false_lock);
533 read_cache_from(ret);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200534out:
535 string_list_clear(&partial, 0);
536 clear_pathspec(&pathspec);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200537 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500538}
539
Junio C Hamanod249b092009-08-09 21:59:30 -0700540static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
541 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500542{
brian m. carlson8066df42017-02-20 00:10:14 +0000543 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700544
Junio C Hamanod249b092009-08-09 21:59:30 -0700545 if (s->relative_paths)
546 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500547
548 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700549 s->amend = 1;
550 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500551 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700552 s->verbose = verbose;
553 s->index_file = index_file;
554 s->fp = fp;
555 s->nowarn = nowarn;
brian m. carlsone82caf32017-07-13 23:49:28 +0000556 s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400557 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000558 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400559 s->status_format = status_format;
560 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500561
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700562 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400563 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700564 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500565
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700566 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500567}
568
Junio C Hamano06bb6432011-08-19 11:58:18 -0700569static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100570{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700571 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100572}
573
Jeff Kingfac90832014-12-10 10:42:10 -0500574static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
575{
Jeff Kingc83a5092014-12-10 10:43:42 -0500576 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200577 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500578}
579
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700580static void export_one(const char *var, const char *s, const char *e, int hack)
581{
582 struct strbuf buf = STRBUF_INIT;
583 if (hack)
584 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100585 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700586 setenv(var, buf.buf, 1);
587 strbuf_release(&buf);
588}
589
Jeff Kingc33ddc22014-08-27 03:57:08 -0400590static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400591{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400592 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400593
Jeff Kingc33ddc22014-08-27 03:57:08 -0400594 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400595 int errors = 0;
596 unsigned long t = approxidate_careful(in, &errors);
597 if (errors)
598 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400599 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400600 }
601
602 return 0;
603}
604
Jeff Kingf4ef5172014-08-27 03:57:56 -0400605static void set_ident_var(char **buf, char *val)
606{
607 free(*buf);
608 *buf = val;
609}
610
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800611static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200612{
613 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700614 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200615
Jeff Kingeaa541e2015-01-12 20:58:33 -0500616 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
617 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
618 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200619
Jay Soffian37f7a852011-02-19 23:12:29 -0500620 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400621 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800622 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400623 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200624
Jeff Kingf0f96622014-08-27 03:57:28 -0400625 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200626 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400627 die(_("commit '%s' lacks author header"), author_message);
628 if (split_ident_line(&ident, a, len) < 0)
629 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200630
Jeff Kingf4ef5172014-08-27 03:57:56 -0400631 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
632 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200633
Jeff Kingf0f96622014-08-27 03:57:28 -0400634 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400635 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400636 strbuf_addch(&date_buf, '@');
637 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
638 strbuf_addch(&date_buf, ' ');
639 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400640 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400641 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200642 }
643
644 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400645 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200646
Jeff Kingf0f96622014-08-27 03:57:28 -0400647 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000648 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400649 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
650 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200651 }
652
Jeff King14ac2862014-05-01 21:12:42 -0400653 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400654 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400655 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400656 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400657 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400658 }
659
William Hubbs39ab4d02019-02-04 12:48:50 -0600660 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
661 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500662 assert_split_ident(&author, author_ident);
663 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
664 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
665 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400666 free(name);
667 free(email);
668 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200669}
670
Jeff Kingb7242b82014-05-01 21:10:01 -0400671static int author_date_is_interesting(void)
672{
673 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800674}
675
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700676static void adjust_comment_line_char(const struct strbuf *sb)
677{
678 char candidates[] = "#;@!$%^&|:";
679 char *candidate;
680 const char *p;
681
682 comment_line_char = candidates[0];
683 if (!memchr(sb->buf, comment_line_char, sb->len))
684 return;
685
686 p = sb->buf;
687 candidate = strchr(candidates, *p);
688 if (candidate)
689 *candidate = ' ';
690 for (p = sb->buf; *p; p++) {
691 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
692 candidate = strchr(candidates, p[1]);
693 if (candidate)
694 *candidate = ' ';
695 }
696 }
697
698 for (p = candidates; *p == ' '; p++)
699 ;
700 if (!*p)
701 die(_("unable to select a comment character that is not used\n"
702 "in the current commit message"));
703 comment_line_char = *p;
704}
705
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530706static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
707 struct pretty_print_context *ctx)
708{
709 const char *buffer, *subject, *fmt;
710
711 buffer = get_commit_buffer(commit, NULL);
712 find_commit_subject(buffer, &subject);
713 /*
714 * If we amend the 'amend!' commit then we don't want to
715 * duplicate the subject line.
716 */
717 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
718 format_commit_message(commit, fmt, sb, ctx);
719 unuse_commit_buffer(commit, buffer);
720}
721
Junio C Hamanod249b092009-08-09 21:59:30 -0700722static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700723 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800724 struct wt_status *s,
725 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500726{
727 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800728 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700729 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500730 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100731 const char *hook_arg1 = NULL;
732 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000733 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200734 int old_display_comment_prefix;
Denton Liu10559972019-04-17 11:23:28 +0100735 int merge_contains_scissors = 0;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100736 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500737
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700738 /* This checks and barfs if author is badly specified */
739 determine_author_info(author_ident);
740
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100741 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
742 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100743 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500744
Pat Notz89ac1222010-11-02 13:59:11 -0600745 if (squash_message) {
746 /*
747 * Insert the proper subject line before other commit
748 * message options add their content.
749 */
750 if (use_message && !strcmp(use_message, squash_message))
751 strbuf_addstr(&sb, "squash! ");
752 else {
753 struct pretty_print_context ctx = {0};
754 struct commit *c;
755 c = lookup_commit_reference_by_name(squash_message);
756 if (!c)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000757 die(_("could not lookup commit %s"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600758 ctx.output_encoding = get_commit_output_encoding();
759 format_commit_message(c, "squash! %s\n\n", &sb,
760 &ctx);
761 }
762 }
763
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000764 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000765 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100766 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500767 } else if (logfile && !strcmp(logfile, "-")) {
768 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000769 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500770 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000771 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100772 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500773 } else if (logfile) {
774 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000775 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200776 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100777 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500778 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800779 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500780 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400781 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200782 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100783 hook_arg1 = "commit";
784 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600785 } else if (fixup_message) {
786 struct pretty_print_context ctx = {0};
787 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530788 char *fmt;
789 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600790 if (!commit)
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530791 die(_("could not lookup commit %s"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600792 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530793 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
794 format_commit_message(commit, fmt, &sb, &ctx);
795 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600796 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530797
798 /*
799 * Only `-m` commit message option is checked here, as
800 * it supports `--fixup` to append the commit message.
801 *
802 * The other commit message options `-c`/`-C`/`-F` are
803 * incompatible with all the forms of `--fixup` and
804 * have already errored out while parsing the `git commit`
805 * options.
806 */
807 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
808 strbuf_addbuf(&sb, &message);
809
810 if (!strcmp(fixup_prefix, "amend")) {
811 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000812 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530813 prepare_amend_commit(commit, &sb, &ctx);
814 }
Stefan Beller102de882018-05-17 15:51:51 -0700815 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100816 size_t merge_msg_start;
817
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100818 /*
819 * prepend SQUASH_MSG here if it exists and a
820 * "merge --squash" was originally performed
821 */
Stefan Beller102de882018-05-17 15:51:51 -0700822 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
823 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100824 die_errno(_("could not read SQUASH_MSG"));
825 hook_arg1 = "squash";
826 } else
827 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100828
829 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700830 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000831 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100832
833 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
834 wt_status_locate_end(sb.buf + merge_msg_start,
835 sb.len - merge_msg_start) <
836 sb.len - merge_msg_start)
837 merge_contains_scissors = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700838 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
839 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000840 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100841 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600842 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500843 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000844 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100845 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200846 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500847 }
848
Paolo Bonzini8089c852008-02-05 08:04:18 +0100849 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500850 * The remaining cases don't modify the template message, but
851 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100852 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500853 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100854 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000855 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500856 hook_arg1 = "commit";
857 hook_arg2 = "CHERRY_PICK_HEAD";
858 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100859
Pat Notz89ac1222010-11-02 13:59:11 -0600860 if (squash_message) {
861 /*
862 * If squash_commit was used for the commit subject,
863 * then we're possibly hijacking other commit log options.
864 * Reset the hook args to tell the real story.
865 */
866 hook_arg1 = "message";
867 hook_arg2 = "";
868 }
869
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530870 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700871 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530872 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500873
Matthieu Moy2556b992013-09-06 19:43:07 +0200874 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
875 old_display_comment_prefix = s->display_comment_prefix;
876 s->display_comment_prefix = 1;
877
Matthieu Moyea9882b2013-09-12 12:50:06 +0200878 /*
879 * Most hints are counter-productive when the commit has
880 * already started.
881 */
882 s->hints = 0;
883
Boris Faure8b1ae672011-05-08 12:31:02 +0200884 if (clean_message_contents)
Tobias Klauser63af4a82015-10-16 17:16:42 +0200885 strbuf_stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000886
Junio C Hamano073bd752014-10-28 12:44:09 -0700887 if (signoff)
Jonathan Tan710714a2016-11-02 10:29:17 -0700888 append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000889
Jonathan Nieder37f30122011-02-25 23:10:49 -0600890 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000891 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500892
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700893 if (auto_comment_line_char)
894 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500895 strbuf_release(&sb);
896
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200897 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400898 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500899 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800900 int ident_shown = 0;
901 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400902 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800903 const char *hint_cleanup_all = allow_empty_message ?
904 _("Please enter the commit message for your changes."
905 " Lines starting\nwith '%c' will be ignored.\n") :
906 _("Please enter the commit message for your changes."
907 " Lines starting\nwith '%c' will be ignored, and an empty"
908 " message aborts the commit.\n");
909 const char *hint_cleanup_space = allow_empty_message ?
910 _("Please enter the commit message for your changes."
911 " Lines starting\n"
912 "with '%c' will be kept; you may remove them"
913 " yourself if you want to.\n") :
914 _("Please enter the commit message for your changes."
915 " Lines starting\n"
916 "with '%c' will be kept; you may remove them"
917 " yourself if you want to.\n"
918 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700919 if (whence != FROM_COMMIT) {
Denton Liu10559972019-04-17 11:23:28 +0100920 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
921 !merge_contains_scissors)
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700922 wt_status_add_cut_line(s->fp);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000923 status_printf_ln(
924 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400925 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000926 _("\n"
927 "It looks like you may be committing a merge.\n"
928 "If this is not correct, please run\n"
929 " git update-ref -d MERGE_HEAD\n"
930 "and try again.\n") :
931 _("\n"
932 "It looks like you may be committing a cherry-pick.\n"
933 "If this is not correct, please run\n"
934 " git update-ref -d CHERRY_PICK_HEAD\n"
935 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700936 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100937
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600938 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000939 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Hu Jialun54ba2f12021-07-10 02:07:31 +0800940 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
Denton Liu10559972019-04-17 11:23:28 +0100941 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
942 if (whence == FROM_COMMIT && !merge_contains_scissors)
943 wt_status_add_cut_line(s->fp);
944 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Hu Jialun54ba2f12021-07-10 02:07:31 +0800945 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100946
Jeff Kingfac90832014-12-10 10:42:10 -0500947 /*
948 * These should never fail because they come from our own
949 * fmt_ident. They may fail the sane_ident test, but we know
950 * that the name and mail pointers will at least be valid,
951 * which is enough for our tests and printing here.
952 */
953 assert_split_ident(&ai, author_ident);
954 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400955
956 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600957 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000958 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400959 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600960 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400961 (int)(ai.name_end - ai.name_begin), ai.name_begin,
962 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200963
Jeff Kingb7242b82014-05-01 21:10:01 -0400964 if (author_date_is_interesting())
965 status_printf_ln(s, GIT_COLOR_NORMAL,
966 _("%s"
967 "Date: %s"),
968 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400969 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100970
Jeff Kingd6991ce2012-11-14 16:34:13 -0800971 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600972 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000973 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400974 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600975 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400976 (int)(ci.name_end - ci.name_begin), ci.name_begin,
977 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200978
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +0530979 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200980
Junio C Hamanod249b092009-08-09 21:59:30 -0700981 saved_color_setting = s->use_color;
982 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700983 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700984 s->use_color = saved_color_setting;
Elijah Newren3e73cc62018-09-27 10:36:57 -0700985 string_list_clear(&s->change, 1);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100986 } else {
brian m. carlson8066df42017-02-20 00:10:14 +0000987 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800988 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100989
Alex Riesen71686242007-11-28 22:13:08 +0100990 if (!active_nr && read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000991 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +0100992
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800993 if (amend)
994 parent = "HEAD^1";
995
brian m. carlsone82caf32017-07-13 23:49:28 +0000996 if (get_oid(parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +0700997 int i, ita_nr = 0;
998
Derrick Stoleecb8388d2021-04-01 01:49:45 +0000999 /* TODO: audit for interaction with sparse-index. */
1000 ensure_full_index(&the_index);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001001 for (i = 0; i < the_index.cache_nr; i++)
1002 if (ce_intent_to_add(the_index.cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001003 ita_nr++;
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001004 committable = the_index.cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001005 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001006 /*
1007 * Unless the user did explicitly request a submodule
1008 * ignore mode by passing a command line option we do
1009 * not ignore any changed submodule SHA-1s when
1010 * comparing index and parent, no matter what is
1011 * configured. Otherwise we won't commit any
1012 * submodules which were manually staged, which would
1013 * be really confusing.
1014 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001015 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001016 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001017 if (ignore_submodule_arg &&
1018 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001019 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001020 committable = index_differs_from(the_repository,
1021 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001022 }
Alex Riesen71686242007-11-28 22:13:08 +01001023 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001024 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001025
Jonathan Nieder37f30122011-02-25 23:10:49 -06001026 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001027
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001028 if (trailer_args.nr) {
1029 struct child_process run_trailer = CHILD_PROCESS_INIT;
1030
1031 strvec_pushl(&run_trailer.args, "interpret-trailers",
1032 "--in-place", git_path_commit_editmsg(), NULL);
1033 strvec_pushv(&run_trailer.args, trailer_args.v);
1034 run_trailer.git_cmd = 1;
1035 if (run_command(&run_trailer))
1036 die(_("unable to pass trailers to --trailers"));
1037 strvec_clear(&trailer_args);
1038 }
1039
Jay Soffian37f7a852011-02-19 23:12:29 -05001040 /*
1041 * Reject an attempt to record a non-merge empty commit without
1042 * explicit --allow-empty. In the cherry-pick case, it may be
1043 * empty due to conflict resolution, which the user should okay.
1044 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001045 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001046 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001047 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001048 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001049 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001050 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001051 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001052 else if (is_from_cherry_pick(whence) ||
1053 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001054 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001055 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001056 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001057 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001058 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001059 else
1060 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001061 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001062 return 0;
1063 }
1064
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001065 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001066 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001067 * Re-read the index as the pre-commit-commit hook was invoked
1068 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001069 * the editor and after we invoke run_status above.
1070 */
1071 discard_cache();
1072 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001073 read_cache_from(index_file);
Kevin Willford680ee552017-08-14 15:54:25 -06001074
Thomas Rast996277c2011-12-06 18:43:37 +01001075 if (update_main_cache_tree(0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001076 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001077 return 0;
1078 }
1079
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001080 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301081 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001082 return 0;
1083
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001084 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001085 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001086
Jeff King22f9b7f2020-07-28 16:24:27 -04001087 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001088 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001089 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001090 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001091 exit(1);
1092 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001093 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001094 }
1095
1096 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001097 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1098 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001099 return 0;
1100 }
1101
1102 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001103}
1104
Junio C Hamano146ea062008-08-26 23:13:13 -07001105static const char *find_author_by_nickname(const char *name)
1106{
1107 struct rev_info revs;
1108 struct commit *commit;
1109 struct strbuf buf = STRBUF_INIT;
1110 const char *av[20];
1111 int ac = 0;
1112
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001113 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001114 strbuf_addf(&buf, "--author=%s", name);
1115 av[++ac] = "--all";
1116 av[++ac] = "-i";
1117 av[++ac] = buf.buf;
1118 av[++ac] = NULL;
1119 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001120 revs.mailmap = xmalloc(sizeof(struct string_list));
1121 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001122 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001123
Stefan Beller81c3ce32014-08-10 23:33:26 +02001124 if (prepare_revision_walk(&revs))
1125 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001126 commit = get_revision(&revs);
1127 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001128 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001129 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001130 strbuf_release(&buf);
Antoine Pelisseea167942013-08-23 15:48:31 +02001131 format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001132 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001133 return strbuf_detach(&buf, NULL);
1134 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001135 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001136}
1137
Jameson Millereec0f7f2017-10-30 13:21:37 -04001138static void handle_ignored_arg(struct wt_status *s)
1139{
1140 if (!ignored_arg)
1141 ; /* default already initialized */
1142 else if (!strcmp(ignored_arg, "traditional"))
1143 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1144 else if (!strcmp(ignored_arg, "no"))
1145 s->show_ignored_mode = SHOW_NO_IGNORED;
1146 else if (!strcmp(ignored_arg, "matching"))
1147 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1148 else
1149 die(_("Invalid ignored mode '%s'"), ignored_arg);
1150}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001151
1152static void handle_untracked_files_arg(struct wt_status *s)
1153{
1154 if (!untracked_files_arg)
1155 ; /* default already initialized */
1156 else if (!strcmp(untracked_files_arg, "no"))
1157 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1158 else if (!strcmp(untracked_files_arg, "normal"))
1159 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1160 else if (!strcmp(untracked_files_arg, "all"))
1161 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001162 /*
1163 * Please update $__git_untracked_file_modes in
1164 * git-completion.bash when you add new options
1165 */
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001166 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001167 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001168}
1169
Jay Soffian37f7a852011-02-19 23:12:29 -05001170static const char *read_commit_message(const char *name)
1171{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001172 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001173 struct commit *commit;
1174
1175 commit = lookup_commit_reference_by_name(name);
1176 if (!commit)
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001177 die(_("could not lookup commit %s"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001178 out_enc = get_commit_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10001179 return logmsg_reencode(commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001180}
1181
Junio C Hamano84b42022013-06-24 11:41:40 -07001182/*
1183 * Enumerate what needs to be propagated when --porcelain
1184 * is not in effect here.
1185 */
1186static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001187 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001188 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001189 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001190} status_deferred_config = {
1191 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001192 -1, /* unspecified */
1193 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001194};
1195
1196static void finalize_deferred_config(struct wt_status *s)
1197{
1198 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001199 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001200 !s->null_termination);
1201
1202 if (s->null_termination) {
1203 if (status_format == STATUS_FORMAT_NONE ||
1204 status_format == STATUS_FORMAT_UNSPECIFIED)
1205 status_format = STATUS_FORMAT_PORCELAIN;
1206 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001207 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001208 }
1209
1210 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1211 status_format = status_deferred_config.status_format;
1212 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1213 status_format = STATUS_FORMAT_NONE;
1214
1215 if (use_deferred_config && s->show_branch < 0)
1216 s->show_branch = status_deferred_config.show_branch;
1217 if (s->show_branch < 0)
1218 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001219
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001220 /*
1221 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001222 * line argument *AND* we will print in a human-readable format
1223 * (short, long etc.) then we inherit from the status.aheadbehind
1224 * config setting. In all other cases (and porcelain V[12] formats
1225 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001226 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001227 if (use_deferred_config &&
1228 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001229 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1230
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001231 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1232 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001233}
1234
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301235static void check_fixup_reword_options(int argc, const char *argv[]) {
1236 if (whence != FROM_COMMIT) {
1237 if (whence == FROM_MERGE)
1238 die(_("You are in the middle of a merge -- cannot reword."));
1239 else if (is_from_cherry_pick(whence))
1240 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1241 }
1242 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001243 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301244 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001245 die(_("reword option of '%s' and '%s' cannot be used together"),
1246 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301247}
1248
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001249static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001250 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001251 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001252 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001253 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001254 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001255{
Jeff King036dbbf2012-05-07 15:18:26 -04001256 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001257 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001258
Junio C Hamano146ea062008-08-26 23:13:13 -07001259 if (force_author && !strchr(force_author, '>'))
1260 force_author = find_author_by_nickname(force_author);
1261
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001262 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001263 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001264
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301265 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001266 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001267
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001268 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001269 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001270 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001271 if (amend && whence != FROM_COMMIT) {
1272 if (whence == FROM_MERGE)
1273 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001274 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001275 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001276 else if (whence == FROM_REBASE_PICK)
1277 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001278 }
Pat Notz89ac1222010-11-02 13:59:11 -06001279 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001280 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1281 die_for_incompatible_opt4(!!use_message, "-C",
1282 !!edit_message, "-c",
1283 !!logfile, "-F",
1284 !!fixup_message, "--fixup");
1285 die_for_incompatible_opt4(have_option_m, "-m",
1286 !!edit_message, "-c",
1287 !!use_message, "-C",
1288 !!logfile, "-F");
1289 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Junio C Hamano010c7db2012-03-30 11:30:59 -07001290 template_file = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001291 if (edit_message)
1292 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001293 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001294 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001295 if (!use_message && !is_from_cherry_pick(whence) &&
1296 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001297 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001298 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001299 use_message_buffer = read_commit_message(use_message);
1300 if (!renew_authorship) {
1301 author_message = use_message;
1302 author_message_buffer = use_message_buffer;
1303 }
1304 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001305 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1306 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001307 author_message = "CHERRY_PICK_HEAD";
1308 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001309 }
1310
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001311 if (patch_interactive)
1312 interactive = 1;
1313
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001314 die_for_incompatible_opt4(also, "-i/--include",
1315 only, "-o/--only",
1316 all, "-a/--all",
1317 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301318 if (fixup_message) {
1319 /*
1320 * We limit --fixup's suboptions to only alpha characters.
1321 * If the first character after a run of alpha is colon,
1322 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301323 * name like `amend` or `reword`, or a misspelt suboption
1324 * name. In either case, we treat it as
1325 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301326 *
1327 * Otherwise, we are dealing with --fixup=<commit>.
1328 */
1329 char *p = fixup_message;
1330 while (isalpha(*p))
1331 p++;
1332 if (p > fixup_message && *p == ':') {
1333 *p = '\0';
1334 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301335 if (!strcmp("amend", fixup_message) ||
1336 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301337 fixup_prefix = "amend";
1338 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301339 if (*fixup_message == 'r') {
1340 check_fixup_reword_options(argc, argv);
1341 only = 1;
1342 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301343 } else {
1344 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1345 }
1346 } else {
1347 fixup_commit = fixup_message;
1348 fixup_prefix = "fixup";
1349 use_editor = 0;
1350 }
1351 }
1352
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001353 if (0 <= edit_flag)
1354 use_editor = edit_flag;
1355
Denton Liuf29cd862019-04-17 11:23:25 +01001356 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001357
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001358 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001359
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001360 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001361 die(_("paths '%s ...' with -a does not make sense"),
1362 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001363
Jeff Kingf3f47a12012-10-18 21:15:50 +07001364 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001365 dry_run = 1;
1366
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001367 return argc;
1368}
1369
Jeff Kinge885a842020-09-30 08:28:18 -04001370static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001371 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001372{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001373 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001374 const char *index_file;
1375
Jeff Kinge885a842020-09-30 08:28:18 -04001376 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001377 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001378 rollback_index_files();
1379
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001380 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001381}
1382
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001383define_list_config_array_extra(color_status_slots, {"added"});
1384
Jonathan Nieder88521172014-10-07 15:16:57 -04001385static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001386{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001387 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001388 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001389
1390 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001391}
1392
1393static int git_status_config(const char *k, const char *v, void *cb)
1394{
1395 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001396 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001397
Christian Couder59556542013-11-30 21:55:40 +01001398 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001399 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001400 if (!strcmp(k, "status.submodulesummary")) {
1401 int is_bool;
1402 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1403 if (is_bool && s->submodule_summary)
1404 s->submodule_summary = -1;
1405 return 0;
1406 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001407 if (!strcmp(k, "status.short")) {
1408 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001409 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001410 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001411 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001412 return 0;
1413 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001414 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001415 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001416 return 0;
1417 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001418 if (!strcmp(k, "status.aheadbehind")) {
1419 status_deferred_config.ahead_behind = git_config_bool(k, v);
1420 return 0;
1421 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001422 if (!strcmp(k, "status.showstash")) {
1423 s->show_stash = git_config_bool(k, v);
1424 return 0;
1425 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001426 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001427 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001428 return 0;
1429 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001430 if (!strcmp(k, "status.displaycommentprefix")) {
1431 s->display_comment_prefix = git_config_bool(k, v);
1432 return 0;
1433 }
René Scharfee3f1da92014-10-04 20:54:50 +02001434 if (skip_prefix(k, "status.color.", &slot_name) ||
1435 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001436 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001437 if (slot < 0)
1438 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001439 if (!v)
1440 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001441 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001442 }
1443 if (!strcmp(k, "status.relativepaths")) {
1444 s->relative_paths = git_config_bool(k, v);
1445 return 0;
1446 }
1447 if (!strcmp(k, "status.showuntrackedfiles")) {
1448 if (!v)
1449 return config_error_nonbool(k);
1450 else if (!strcmp(v, "no"))
1451 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1452 else if (!strcmp(v, "normal"))
1453 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1454 else if (!strcmp(v, "all"))
1455 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1456 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001457 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001458 return 0;
1459 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001460 if (!strcmp(k, "diff.renamelimit")) {
1461 if (s->rename_limit == -1)
1462 s->rename_limit = git_config_int(k, v);
1463 return 0;
1464 }
1465 if (!strcmp(k, "status.renamelimit")) {
1466 s->rename_limit = git_config_int(k, v);
1467 return 0;
1468 }
1469 if (!strcmp(k, "diff.renames")) {
1470 if (s->detect_rename == -1)
1471 s->detect_rename = git_config_rename(k, v);
1472 return 0;
1473 }
1474 if (!strcmp(k, "status.renames")) {
1475 s->detect_rename = git_config_rename(k, v);
1476 return 0;
1477 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001478 return git_diff_ui_config(k, v, NULL);
1479}
1480
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001481int cmd_status(int argc, const char **argv, const char *prefix)
1482{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001483 static int no_renames = -1;
1484 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001485 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001486 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001487 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001488 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001489 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001490 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001491 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001492 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001493 OPT_BOOL('b', "branch", &s.show_branch,
1494 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001495 OPT_BOOL(0, "show-stash", &s.show_stash,
1496 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001497 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1498 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001499 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001500 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001501 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001502 OPT_SET_INT(0, "long", &status_format,
1503 N_("show status in long format (default)"),
1504 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001505 OPT_BOOL('z', "null", &s.null_termination,
1506 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001507 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001508 N_("mode"),
1509 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001510 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001511 { OPTION_STRING, 0, "ignored", &ignored_arg,
1512 N_("mode"),
1513 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1514 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001515 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1516 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001517 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001518 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001519 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001520 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001521 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001522 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001523 OPT_END(),
1524 };
1525
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001526 if (argc == 2 && !strcmp(argv[1], "-h"))
1527 usage_with_options(builtin_status_usage, builtin_status_options);
1528
Derrick Stoleed76723e2021-07-14 13:12:37 +00001529 prepare_repo_settings(the_repository);
1530 the_repository->settings.command_requires_full_index = 0;
1531
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001532 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001533 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001534 builtin_status_options,
1535 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001536 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001537 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001538
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001539 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001540 handle_ignored_arg(&s);
1541
1542 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1543 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1544 die(_("Unsupported combination of ignored and untracked-files arguments"));
1545
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001546 parse_pathspec(&s.pathspec, 0,
1547 PATHSPEC_PREFER_FULL,
1548 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001549
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001550 if (status_format != STATUS_FORMAT_PORCELAIN &&
1551 status_format != STATUS_FORMAT_PORCELAIN_V2)
1552 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001553 repo_read_index(the_repository);
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001554 refresh_index(&the_index,
1555 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1556 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001557
Jeff King27344d62017-09-27 02:54:30 -04001558 if (use_optional_locks())
1559 fd = hold_locked_index(&index_lock, 0);
1560 else
1561 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001562
brian m. carlsone82caf32017-07-13 23:49:28 +00001563 s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001564 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001565 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001566
Jens Lehmann46a958b2010-06-25 16:56:47 +02001567 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001568 s.status_format = status_format;
1569 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001570 if (no_renames != -1)
1571 s.detect_rename = !no_renames;
1572 if ((intptr_t)rename_score_arg != -1) {
1573 if (s.detect_rename < DIFF_DETECT_RENAME)
1574 s.detect_rename = DIFF_DETECT_RENAME;
1575 if (rename_score_arg)
1576 s.rename_score = parse_rename_score(&rename_score_arg);
1577 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001578
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001579 wt_status_collect(&s);
1580
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001581 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001582 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001583
Jeff King86617682009-12-07 00:26:25 -05001584 if (s.relative_paths)
1585 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001586
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001587 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001588 wt_status_collect_free_buffers(&s);
1589
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001590 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001591}
1592
Stephan Beyer186458b2008-07-24 01:09:35 +02001593static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001594{
Junio C Hamanod249b092009-08-09 21:59:30 -07001595 struct wt_status *s = cb;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001596 int status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001597
Brian Hetro984c6e72008-07-05 01:24:40 -04001598 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001599 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001600 if (!strcmp(k, "commit.status")) {
1601 include_status = git_config_bool(k, v);
1602 return 0;
1603 }
Ralf Thielow51fb3a32013-01-10 18:45:59 +01001604 if (!strcmp(k, "commit.cleanup"))
1605 return git_config_string(&cleanup_arg, k, v);
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001606 if (!strcmp(k, "commit.gpgsign")) {
1607 sign_commit = git_config_bool(k, v) ? "" : NULL;
1608 return 0;
1609 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301610 if (!strcmp(k, "commit.verbose")) {
1611 int is_bool;
1612 config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
1613 return 0;
1614 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001615
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001616 status = git_gpg_config(k, v, NULL);
1617 if (status)
1618 return status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001619 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001620}
1621
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001622int cmd_commit(int argc, const char **argv, const char *prefix)
1623{
Jeff King036dbbf2012-05-07 15:18:26 -04001624 static struct wt_status s;
1625 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001626 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1627 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001628
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001629 OPT_GROUP(N_("Commit message options")),
1630 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1631 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1632 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1633 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1634 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1635 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301636 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301637 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1638 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301639 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301640 OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001641 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001642 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
Jeff King116761b2022-10-06 09:11:31 -04001643 OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer),
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -07001644 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001645 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1646 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001647 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001648 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001649 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001650 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001651 /* end commit message options */
1652
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001653 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001654 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1655 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1656 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1657 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1658 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001659 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001660 OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001661 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001662 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001663 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001664 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1665 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001666 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001667 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001668 OPT_SET_INT(0, "long", &status_format,
1669 N_("show status in long format (default)"),
1670 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001671 OPT_BOOL('z', "null", &s.null_termination,
1672 N_("terminate entries with NUL")),
1673 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1674 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001675 { 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" },
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +00001676 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1677 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001678 /* end commit contents options */
1679
Stefan Beller4741edd2013-08-03 13:51:18 +02001680 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1681 N_("ok to record an empty change")),
1682 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1683 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001684
1685 OPT_END()
1686 };
1687
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001688 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001689 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001690 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001691 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001692 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001693 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001694 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001695 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001696 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001697 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001698
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001699 if (argc == 2 && !strcmp(argv[1], "-h"))
1700 usage_with_options(builtin_commit_usage, builtin_commit_options);
1701
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001702 prepare_repo_settings(the_repository);
1703 the_repository->settings.command_requires_full_index = 0;
1704
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001705 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301706 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301707 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001708 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001709
brian m. carlsone82caf32017-07-13 23:49:28 +00001710 if (get_oid("HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001711 current_head = NULL;
1712 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001713 current_head = lookup_commit_or_die(&oid, "HEAD");
Jeff King5e7d4d32013-10-24 04:53:19 -04001714 if (parse_commit(current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001715 die(_("could not parse HEAD commit"));
1716 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301717 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001718 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1719 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001720 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301721 if (verbose == -1)
1722 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1723
Jeff Kingc9bfb952011-08-17 22:05:35 -07001724 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001725 return dry_run_commit(argv, prefix, current_head, &s);
1726 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001727
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001728 /* Set up everything for writing the commit object. This includes
1729 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001730 if (!prepare_to_commit(index_file, prefix,
1731 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001732 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001733 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001734 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001735 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001736
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001737 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001738 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001739 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001740 if (!reflog_msg)
1741 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001742 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001743 if (!reflog_msg)
1744 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001745 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001746 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001747 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001748 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001749 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001750 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001751
Christian Couder643cb5f2010-06-12 18:05:12 +02001752 if (!reflog_msg)
1753 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001754 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001755 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001756 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001757 struct commit *parent;
1758
1759 parent = get_merge_parent(m.buf);
1760 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001761 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001762 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001763 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001764 fclose(fp);
1765 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001766 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1767 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001768 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001769 if (!strcmp(sb.buf, "no-ff"))
1770 allow_fast_forward = 0;
1771 }
1772 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001773 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001774 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001775 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001776 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001777 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001778 : is_from_rebase(whence)
1779 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001780 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001781 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001782 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001783
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001784 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001785 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301786 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001787 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001788 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001789 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001790 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001791
Denton Liuf29cd862019-04-17 11:23:25 +01001792 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301793
Phillip Woodd0aaa462017-11-10 11:09:42 +00001794 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001795 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001796 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001797 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001798 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001799 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301800 rollback_index_files();
1801 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1802 exit(1);
1803 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001804
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301805 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1806 !allow_empty_message) {
1807 struct strbuf body = STRBUF_INIT;
1808 size_t len = commit_subject_length(sb.buf);
1809 strbuf_addstr(&body, sb.buf + len);
1810 if (message_is_empty(&body, cleanup_mode)) {
1811 rollback_index_files();
1812 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1813 exit(1);
1814 }
1815 strbuf_release(&body);
1816 }
1817
Junio C Hamano0074d182011-12-20 13:20:56 -08001818 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001819 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001820 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001821 } else {
1822 struct commit_extra_header **tail = &extra;
1823 append_merge_tag_headers(parents, &tail);
1824 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001825
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001826 if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001827 parents, &oid, author_ident.buf, NULL,
1828 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001829 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001830 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001831 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001832 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001833
Phillip Wood0505d602017-11-17 11:34:47 +00001834 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1835 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001836 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001837 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001838 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001839
Junio C Hamanof496b062019-07-09 15:25:44 -07001840 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001841 unlink(git_path_merge_head(the_repository));
1842 unlink(git_path_merge_msg(the_repository));
1843 unlink(git_path_merge_mode(the_repository));
1844 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001845
Brandon Casey5a9dd392008-01-23 11:21:22 -06001846 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001847 die(_("repository has been updated, but unable to write\n"
1848 "new_index file. Check that disk is not full and quota is\n"
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +07001849 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001850
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001851 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001852
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001853 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001854 run_auto_maintenance(quiet);
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001855 run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1856 NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001857 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001858 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001859 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001860 if (!quiet) {
1861 unsigned int flags = 0;
1862
1863 if (!current_head)
1864 flags |= SUMMARY_INITIAL_COMMIT;
1865 if (author_date_is_interesting())
1866 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001867 print_commit_summary(the_repository, prefix,
1868 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001869 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001870
Denton Liua03b5552020-04-07 10:28:07 -04001871 apply_autostash(git_path_merge_autostash(the_repository));
1872
Junio C Hamano00d8c312022-05-12 15:51:07 -07001873cleanup:
1874 UNLEAK(author_ident);
Jeff King0e5bba52017-09-08 02:38:41 -04001875 UNLEAK(err);
1876 UNLEAK(sb);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001877 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001878}