blob: 06b1330346f895af5103fdb4b52243b57e6f4f18 [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) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100319 discard_index(&the_index);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800320 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 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100346 if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100347 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
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100385 if (repo_read_index_preload(the_repository, &pathspec, 0) < 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;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100390 repo_hold_locked_index(the_repository, &index_lock,
391 LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700392
393 refresh_cache_or_die(refresh_flags);
394
Martin Ågren812d6b02017-10-06 22:12:12 +0200395 if (write_locked_index(&the_index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700396 die(_("unable to create temporary index"));
397
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000398 old_repo_index_file = the_repository->index_file;
399 the_repository->index_file =
400 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500401 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000402 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700403
Jeff Kinge885a842020-09-30 08:28:18 -0400404 if (interactive_add(argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700405 die(_("interactive add failed"));
406
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000407 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700408 if (old_index_env && *old_index_env)
409 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
410 else
411 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500412 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700413
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100414 discard_index(&the_index);
415 read_index_from(&the_index, get_lock_file_path(&index_lock),
416 get_git_dir());
David Turner9c4d6c02014-07-13 13:28:19 -0700417 if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700418 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700419 die(_("unable to write index file"));
Martin Ågren812d6b02017-10-06 22:12:12 +0200420 if (write_locked_index(&the_index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700421 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700422 } else
423 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700424
425 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200426 ret = get_lock_file_path(&index_lock);
427 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700428 }
429
Junio C Hamano28886052007-11-18 01:52:55 -0800430 /*
431 * Non partial, non as-is commit.
432 *
433 * (1) get the real index;
434 * (2) update the_index as necessary;
435 * (3) write the_index out to the real index (still locked);
436 * (4) return the name of the locked index file.
437 *
438 * The caller should run hooks on the locked real index, and
439 * (A) if all goes well, commit the real index;
440 * (B) on failure, rollback the real index.
441 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700442 if (all || (also && pathspec.nr)) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100443 repo_hold_locked_index(the_repository, &index_lock,
444 LOCK_DIE_ON_ERROR);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100445 add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100446 refresh_cache_or_die(refresh_flags);
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700447 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200448 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000449 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800450 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200451 ret = get_lock_file_path(&index_lock);
452 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500453 }
454
Junio C Hamano28886052007-11-18 01:52:55 -0800455 /*
456 * As-is commit.
457 *
458 * (1) return the name of the real index file.
459 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200460 * The caller should run hooks on the real index,
461 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800462 * We still need to refresh the index here.
463 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700464 if (!only && !pathspec.nr) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100465 repo_hold_locked_index(the_repository, &index_lock,
466 LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100467 refresh_cache_or_die(refresh_flags);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100468 if (the_index.cache_changed
469 || !cache_tree_fully_valid(the_index.cache_tree))
Nguyễn Thái Ngọc Duye859c692012-01-16 09:36:46 +0700470 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren61000812018-03-01 21:40:20 +0100471 if (write_locked_index(&the_index, &index_lock,
472 COMMIT_LOCK | SKIP_IF_UNCHANGED))
473 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800474 commit_style = COMMIT_AS_IS;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200475 ret = get_index_file();
476 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500477 }
478
Junio C Hamano28886052007-11-18 01:52:55 -0800479 /*
480 * A partial commit.
481 *
482 * (0) find the set of affected paths;
483 * (1) get lock on the real index file;
484 * (2) update the_index with the given paths;
485 * (3) write the_index out to the real index (still locked);
486 * (4) get lock on the false index file;
487 * (5) reset the_index from HEAD;
488 * (6) update the_index the same way as (2);
489 * (7) write the_index out to the false index file;
490 * (8) return the name of the false index file (still locked);
491 *
492 * The caller should run hooks on the locked false index, and
493 * create commit from it. Then
494 * (A) if all goes well, commit the real index;
495 * (B) on failure, rollback the real index;
496 * In either case, rollback the false index.
497 */
498 commit_style = COMMIT_PARTIAL;
499
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000500 if (whence != FROM_COMMIT) {
501 if (whence == FROM_MERGE)
502 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000503 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000504 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000505 else if (is_from_rebase(whence))
506 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000507 }
Junio C Hamano28886052007-11-18 01:52:55 -0800508
Jeff Kingc5c33502019-03-20 04:15:48 -0400509 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800510 exit(1);
511
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100512 discard_index(&the_index);
513 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000514 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800515
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100516 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800517 add_remove_files(&partial);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100518 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
David Turner9c4d6c02014-07-13 13:28:19 -0700519 update_main_cache_tree(WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200520 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000521 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500522
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700523 hold_lock_file_for_update(&false_lock,
524 git_path("next-index-%"PRIuMAX,
525 (uintmax_t) getpid()),
526 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800527
Junio C Hamano06bb6432011-08-19 11:58:18 -0700528 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800529 add_remove_files(&partial);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100530 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500531
Martin Ågren812d6b02017-10-06 22:12:12 +0200532 if (write_locked_index(&the_index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000533 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500534
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100535 discard_index(&the_index);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200536 ret = get_lock_file_path(&false_lock);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100537 read_index_from(&the_index, ret, get_git_dir());
Martin Ågrendd1055e2017-09-23 01:34:49 +0200538out:
539 string_list_clear(&partial, 0);
540 clear_pathspec(&pathspec);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200541 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500542}
543
Junio C Hamanod249b092009-08-09 21:59:30 -0700544static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
545 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500546{
brian m. carlson8066df42017-02-20 00:10:14 +0000547 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700548
Junio C Hamanod249b092009-08-09 21:59:30 -0700549 if (s->relative_paths)
550 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500551
552 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700553 s->amend = 1;
554 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500555 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700556 s->verbose = verbose;
557 s->index_file = index_file;
558 s->fp = fp;
559 s->nowarn = nowarn;
brian m. carlsone82caf32017-07-13 23:49:28 +0000560 s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400561 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000562 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400563 s->status_format = status_format;
564 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500565
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700566 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400567 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700568 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500569
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700570 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500571}
572
Junio C Hamano06bb6432011-08-19 11:58:18 -0700573static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100574{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700575 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100576}
577
Jeff Kingfac90832014-12-10 10:42:10 -0500578static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
579{
Jeff Kingc83a5092014-12-10 10:43:42 -0500580 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200581 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500582}
583
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700584static void export_one(const char *var, const char *s, const char *e, int hack)
585{
586 struct strbuf buf = STRBUF_INIT;
587 if (hack)
588 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100589 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700590 setenv(var, buf.buf, 1);
591 strbuf_release(&buf);
592}
593
Jeff Kingc33ddc22014-08-27 03:57:08 -0400594static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400595{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400596 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400597
Jeff Kingc33ddc22014-08-27 03:57:08 -0400598 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400599 int errors = 0;
600 unsigned long t = approxidate_careful(in, &errors);
601 if (errors)
602 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400603 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400604 }
605
606 return 0;
607}
608
Jeff Kingf4ef5172014-08-27 03:57:56 -0400609static void set_ident_var(char **buf, char *val)
610{
611 free(*buf);
612 *buf = val;
613}
614
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800615static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200616{
617 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700618 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200619
Jeff Kingeaa541e2015-01-12 20:58:33 -0500620 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
621 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
622 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200623
Jay Soffian37f7a852011-02-19 23:12:29 -0500624 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400625 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800626 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400627 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200628
Jeff Kingf0f96622014-08-27 03:57:28 -0400629 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200630 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400631 die(_("commit '%s' lacks author header"), author_message);
632 if (split_ident_line(&ident, a, len) < 0)
633 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200634
Jeff Kingf4ef5172014-08-27 03:57:56 -0400635 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
636 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200637
Jeff Kingf0f96622014-08-27 03:57:28 -0400638 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400639 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400640 strbuf_addch(&date_buf, '@');
641 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
642 strbuf_addch(&date_buf, ' ');
643 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400644 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400645 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200646 }
647
648 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400649 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200650
Jeff Kingf0f96622014-08-27 03:57:28 -0400651 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000652 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400653 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
654 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200655 }
656
Jeff King14ac2862014-05-01 21:12:42 -0400657 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400658 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400659 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400660 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400661 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400662 }
663
William Hubbs39ab4d02019-02-04 12:48:50 -0600664 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
665 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500666 assert_split_ident(&author, author_ident);
667 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
668 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
669 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400670 free(name);
671 free(email);
672 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200673}
674
Jeff Kingb7242b82014-05-01 21:10:01 -0400675static int author_date_is_interesting(void)
676{
677 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800678}
679
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700680static void adjust_comment_line_char(const struct strbuf *sb)
681{
682 char candidates[] = "#;@!$%^&|:";
683 char *candidate;
684 const char *p;
685
686 comment_line_char = candidates[0];
687 if (!memchr(sb->buf, comment_line_char, sb->len))
688 return;
689
690 p = sb->buf;
691 candidate = strchr(candidates, *p);
692 if (candidate)
693 *candidate = ' ';
694 for (p = sb->buf; *p; p++) {
695 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
696 candidate = strchr(candidates, p[1]);
697 if (candidate)
698 *candidate = ' ';
699 }
700 }
701
702 for (p = candidates; *p == ' '; p++)
703 ;
704 if (!*p)
705 die(_("unable to select a comment character that is not used\n"
706 "in the current commit message"));
707 comment_line_char = *p;
708}
709
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530710static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
711 struct pretty_print_context *ctx)
712{
713 const char *buffer, *subject, *fmt;
714
715 buffer = get_commit_buffer(commit, NULL);
716 find_commit_subject(buffer, &subject);
717 /*
718 * If we amend the 'amend!' commit then we don't want to
719 * duplicate the subject line.
720 */
721 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
722 format_commit_message(commit, fmt, sb, ctx);
723 unuse_commit_buffer(commit, buffer);
724}
725
Junio C Hamanod249b092009-08-09 21:59:30 -0700726static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700727 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800728 struct wt_status *s,
729 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500730{
731 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800732 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700733 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500734 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100735 const char *hook_arg1 = NULL;
736 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000737 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200738 int old_display_comment_prefix;
Denton Liu10559972019-04-17 11:23:28 +0100739 int merge_contains_scissors = 0;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100740 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500741
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700742 /* This checks and barfs if author is badly specified */
743 determine_author_info(author_ident);
744
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100745 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
746 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100747 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500748
Pat Notz89ac1222010-11-02 13:59:11 -0600749 if (squash_message) {
750 /*
751 * Insert the proper subject line before other commit
752 * message options add their content.
753 */
754 if (use_message && !strcmp(use_message, squash_message))
755 strbuf_addstr(&sb, "squash! ");
756 else {
757 struct pretty_print_context ctx = {0};
758 struct commit *c;
759 c = lookup_commit_reference_by_name(squash_message);
760 if (!c)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000761 die(_("could not lookup commit %s"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600762 ctx.output_encoding = get_commit_output_encoding();
763 format_commit_message(c, "squash! %s\n\n", &sb,
764 &ctx);
765 }
766 }
767
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000768 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000769 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100770 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500771 } else if (logfile && !strcmp(logfile, "-")) {
772 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000773 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500774 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000775 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100776 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500777 } else if (logfile) {
778 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000779 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200780 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100781 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500782 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800783 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500784 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400785 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200786 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100787 hook_arg1 = "commit";
788 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600789 } else if (fixup_message) {
790 struct pretty_print_context ctx = {0};
791 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530792 char *fmt;
793 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600794 if (!commit)
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530795 die(_("could not lookup commit %s"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600796 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530797 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
798 format_commit_message(commit, fmt, &sb, &ctx);
799 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600800 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530801
802 /*
803 * Only `-m` commit message option is checked here, as
804 * it supports `--fixup` to append the commit message.
805 *
806 * The other commit message options `-c`/`-C`/`-F` are
807 * incompatible with all the forms of `--fixup` and
808 * have already errored out while parsing the `git commit`
809 * options.
810 */
811 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
812 strbuf_addbuf(&sb, &message);
813
814 if (!strcmp(fixup_prefix, "amend")) {
815 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000816 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530817 prepare_amend_commit(commit, &sb, &ctx);
818 }
Stefan Beller102de882018-05-17 15:51:51 -0700819 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100820 size_t merge_msg_start;
821
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100822 /*
823 * prepend SQUASH_MSG here if it exists and a
824 * "merge --squash" was originally performed
825 */
Stefan Beller102de882018-05-17 15:51:51 -0700826 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
827 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100828 die_errno(_("could not read SQUASH_MSG"));
829 hook_arg1 = "squash";
830 } else
831 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100832
833 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700834 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000835 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100836
837 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
838 wt_status_locate_end(sb.buf + merge_msg_start,
839 sb.len - merge_msg_start) <
840 sb.len - merge_msg_start)
841 merge_contains_scissors = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700842 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
843 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000844 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100845 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600846 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500847 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000848 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100849 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200850 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500851 }
852
Paolo Bonzini8089c852008-02-05 08:04:18 +0100853 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500854 * The remaining cases don't modify the template message, but
855 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100856 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500857 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100858 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000859 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500860 hook_arg1 = "commit";
861 hook_arg2 = "CHERRY_PICK_HEAD";
862 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100863
Pat Notz89ac1222010-11-02 13:59:11 -0600864 if (squash_message) {
865 /*
866 * If squash_commit was used for the commit subject,
867 * then we're possibly hijacking other commit log options.
868 * Reset the hook args to tell the real story.
869 */
870 hook_arg1 = "message";
871 hook_arg2 = "";
872 }
873
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530874 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700875 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530876 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500877
Matthieu Moy2556b992013-09-06 19:43:07 +0200878 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
879 old_display_comment_prefix = s->display_comment_prefix;
880 s->display_comment_prefix = 1;
881
Matthieu Moyea9882b2013-09-12 12:50:06 +0200882 /*
883 * Most hints are counter-productive when the commit has
884 * already started.
885 */
886 s->hints = 0;
887
Boris Faure8b1ae672011-05-08 12:31:02 +0200888 if (clean_message_contents)
Tobias Klauser63af4a82015-10-16 17:16:42 +0200889 strbuf_stripspace(&sb, 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000890
Junio C Hamano073bd752014-10-28 12:44:09 -0700891 if (signoff)
Jonathan Tan710714a2016-11-02 10:29:17 -0700892 append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000893
Jonathan Nieder37f30122011-02-25 23:10:49 -0600894 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000895 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500896
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700897 if (auto_comment_line_char)
898 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500899 strbuf_release(&sb);
900
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200901 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400902 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500903 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800904 int ident_shown = 0;
905 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400906 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800907 const char *hint_cleanup_all = allow_empty_message ?
908 _("Please enter the commit message for your changes."
909 " Lines starting\nwith '%c' will be ignored.\n") :
910 _("Please enter the commit message for your changes."
911 " Lines starting\nwith '%c' will be ignored, and an empty"
912 " message aborts the commit.\n");
913 const char *hint_cleanup_space = allow_empty_message ?
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 _("Please enter the commit message for your changes."
919 " Lines starting\n"
920 "with '%c' will be kept; you may remove them"
921 " yourself if you want to.\n"
922 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700923 if (whence != FROM_COMMIT) {
Denton Liu10559972019-04-17 11:23:28 +0100924 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
925 !merge_contains_scissors)
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700926 wt_status_add_cut_line(s->fp);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000927 status_printf_ln(
928 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400929 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000930 _("\n"
931 "It looks like you may be committing a merge.\n"
932 "If this is not correct, please run\n"
933 " git update-ref -d MERGE_HEAD\n"
934 "and try again.\n") :
935 _("\n"
936 "It looks like you may be committing a cherry-pick.\n"
937 "If this is not correct, please run\n"
938 " git update-ref -d CHERRY_PICK_HEAD\n"
939 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700940 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100941
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600942 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000943 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Hu Jialun54ba2f12021-07-10 02:07:31 +0800944 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
Denton Liu10559972019-04-17 11:23:28 +0100945 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
946 if (whence == FROM_COMMIT && !merge_contains_scissors)
947 wt_status_add_cut_line(s->fp);
948 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Hu Jialun54ba2f12021-07-10 02:07:31 +0800949 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100950
Jeff Kingfac90832014-12-10 10:42:10 -0500951 /*
952 * These should never fail because they come from our own
953 * fmt_ident. They may fail the sane_ident test, but we know
954 * that the name and mail pointers will at least be valid,
955 * which is enough for our tests and printing here.
956 */
957 assert_split_ident(&ai, author_ident);
958 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400959
960 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600961 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000962 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400963 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600964 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400965 (int)(ai.name_end - ai.name_begin), ai.name_begin,
966 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200967
Jeff Kingb7242b82014-05-01 21:10:01 -0400968 if (author_date_is_interesting())
969 status_printf_ln(s, GIT_COLOR_NORMAL,
970 _("%s"
971 "Date: %s"),
972 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400973 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100974
Jeff Kingd6991ce2012-11-14 16:34:13 -0800975 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600976 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000977 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400978 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600979 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400980 (int)(ci.name_end - ci.name_begin), ci.name_begin,
981 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200982
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +0530983 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200984
Junio C Hamanod249b092009-08-09 21:59:30 -0700985 saved_color_setting = s->use_color;
986 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700987 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700988 s->use_color = saved_color_setting;
Elijah Newren3e73cc62018-09-27 10:36:57 -0700989 string_list_clear(&s->change, 1);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100990 } else {
brian m. carlson8066df42017-02-20 00:10:14 +0000991 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800992 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100993
Alex Riesen71686242007-11-28 22:13:08 +0100994 if (!active_nr && read_cache() < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000995 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +0100996
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800997 if (amend)
998 parent = "HEAD^1";
999
brian m. carlsone82caf32017-07-13 23:49:28 +00001000 if (get_oid(parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001001 int i, ita_nr = 0;
1002
Derrick Stoleecb8388d2021-04-01 01:49:45 +00001003 /* TODO: audit for interaction with sparse-index. */
1004 ensure_full_index(&the_index);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001005 for (i = 0; i < the_index.cache_nr; i++)
1006 if (ce_intent_to_add(the_index.cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001007 ita_nr++;
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001008 committable = the_index.cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001009 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001010 /*
1011 * Unless the user did explicitly request a submodule
1012 * ignore mode by passing a command line option we do
1013 * not ignore any changed submodule SHA-1s when
1014 * comparing index and parent, no matter what is
1015 * configured. Otherwise we won't commit any
1016 * submodules which were manually staged, which would
1017 * be really confusing.
1018 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001019 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001020 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001021 if (ignore_submodule_arg &&
1022 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001023 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001024 committable = index_differs_from(the_repository,
1025 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001026 }
Alex Riesen71686242007-11-28 22:13:08 +01001027 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001028 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001029
Jonathan Nieder37f30122011-02-25 23:10:49 -06001030 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001031
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001032 if (trailer_args.nr) {
1033 struct child_process run_trailer = CHILD_PROCESS_INIT;
1034
1035 strvec_pushl(&run_trailer.args, "interpret-trailers",
1036 "--in-place", git_path_commit_editmsg(), NULL);
1037 strvec_pushv(&run_trailer.args, trailer_args.v);
1038 run_trailer.git_cmd = 1;
1039 if (run_command(&run_trailer))
1040 die(_("unable to pass trailers to --trailers"));
1041 strvec_clear(&trailer_args);
1042 }
1043
Jay Soffian37f7a852011-02-19 23:12:29 -05001044 /*
1045 * Reject an attempt to record a non-merge empty commit without
1046 * explicit --allow-empty. In the cherry-pick case, it may be
1047 * empty due to conflict resolution, which the user should okay.
1048 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001049 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001050 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001051 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001052 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001053 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001054 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001055 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001056 else if (is_from_cherry_pick(whence) ||
1057 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001058 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001059 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001060 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001061 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001062 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001063 else
1064 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001065 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001066 return 0;
1067 }
1068
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001069 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001070 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001071 * Re-read the index as the pre-commit-commit hook was invoked
1072 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001073 * the editor and after we invoke run_status above.
1074 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001075 discard_index(&the_index);
Kevin Willford680ee552017-08-14 15:54:25 -06001076 }
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001077 read_index_from(&the_index, index_file, get_git_dir());
Kevin Willford680ee552017-08-14 15:54:25 -06001078
Thomas Rast996277c2011-12-06 18:43:37 +01001079 if (update_main_cache_tree(0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001080 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001081 return 0;
1082 }
1083
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001084 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301085 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001086 return 0;
1087
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001088 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001089 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001090
Jeff King22f9b7f2020-07-28 16:24:27 -04001091 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001092 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001093 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001094 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001095 exit(1);
1096 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001097 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001098 }
1099
1100 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001101 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1102 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001103 return 0;
1104 }
1105
1106 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001107}
1108
Junio C Hamano146ea062008-08-26 23:13:13 -07001109static const char *find_author_by_nickname(const char *name)
1110{
1111 struct rev_info revs;
1112 struct commit *commit;
1113 struct strbuf buf = STRBUF_INIT;
1114 const char *av[20];
1115 int ac = 0;
1116
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001117 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001118 strbuf_addf(&buf, "--author=%s", name);
1119 av[++ac] = "--all";
1120 av[++ac] = "-i";
1121 av[++ac] = buf.buf;
1122 av[++ac] = NULL;
1123 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001124 revs.mailmap = xmalloc(sizeof(struct string_list));
1125 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001126 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001127
Stefan Beller81c3ce32014-08-10 23:33:26 +02001128 if (prepare_revision_walk(&revs))
1129 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001130 commit = get_revision(&revs);
1131 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001132 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001133 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001134 strbuf_release(&buf);
Antoine Pelisseea167942013-08-23 15:48:31 +02001135 format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001136 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001137 return strbuf_detach(&buf, NULL);
1138 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001139 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001140}
1141
Jameson Millereec0f7f2017-10-30 13:21:37 -04001142static void handle_ignored_arg(struct wt_status *s)
1143{
1144 if (!ignored_arg)
1145 ; /* default already initialized */
1146 else if (!strcmp(ignored_arg, "traditional"))
1147 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1148 else if (!strcmp(ignored_arg, "no"))
1149 s->show_ignored_mode = SHOW_NO_IGNORED;
1150 else if (!strcmp(ignored_arg, "matching"))
1151 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1152 else
1153 die(_("Invalid ignored mode '%s'"), ignored_arg);
1154}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001155
1156static void handle_untracked_files_arg(struct wt_status *s)
1157{
1158 if (!untracked_files_arg)
1159 ; /* default already initialized */
1160 else if (!strcmp(untracked_files_arg, "no"))
1161 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1162 else if (!strcmp(untracked_files_arg, "normal"))
1163 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1164 else if (!strcmp(untracked_files_arg, "all"))
1165 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001166 /*
1167 * Please update $__git_untracked_file_modes in
1168 * git-completion.bash when you add new options
1169 */
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001170 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001171 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001172}
1173
Jay Soffian37f7a852011-02-19 23:12:29 -05001174static const char *read_commit_message(const char *name)
1175{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001176 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001177 struct commit *commit;
1178
1179 commit = lookup_commit_reference_by_name(name);
1180 if (!commit)
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001181 die(_("could not lookup commit %s"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001182 out_enc = get_commit_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10001183 return logmsg_reencode(commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001184}
1185
Junio C Hamano84b42022013-06-24 11:41:40 -07001186/*
1187 * Enumerate what needs to be propagated when --porcelain
1188 * is not in effect here.
1189 */
1190static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001191 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001192 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001193 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001194} status_deferred_config = {
1195 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001196 -1, /* unspecified */
1197 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001198};
1199
1200static void finalize_deferred_config(struct wt_status *s)
1201{
1202 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001203 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001204 !s->null_termination);
1205
1206 if (s->null_termination) {
1207 if (status_format == STATUS_FORMAT_NONE ||
1208 status_format == STATUS_FORMAT_UNSPECIFIED)
1209 status_format = STATUS_FORMAT_PORCELAIN;
1210 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001211 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001212 }
1213
1214 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1215 status_format = status_deferred_config.status_format;
1216 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1217 status_format = STATUS_FORMAT_NONE;
1218
1219 if (use_deferred_config && s->show_branch < 0)
1220 s->show_branch = status_deferred_config.show_branch;
1221 if (s->show_branch < 0)
1222 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001223
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001224 /*
1225 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001226 * line argument *AND* we will print in a human-readable format
1227 * (short, long etc.) then we inherit from the status.aheadbehind
1228 * config setting. In all other cases (and porcelain V[12] formats
1229 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001230 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001231 if (use_deferred_config &&
1232 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001233 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1234
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001235 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1236 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001237}
1238
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301239static void check_fixup_reword_options(int argc, const char *argv[]) {
1240 if (whence != FROM_COMMIT) {
1241 if (whence == FROM_MERGE)
1242 die(_("You are in the middle of a merge -- cannot reword."));
1243 else if (is_from_cherry_pick(whence))
1244 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1245 }
1246 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001247 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301248 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001249 die(_("reword option of '%s' and '%s' cannot be used together"),
1250 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301251}
1252
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001253static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001254 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001255 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001256 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001257 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001258 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001259{
Jeff King036dbbf2012-05-07 15:18:26 -04001260 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001261 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001262
Junio C Hamano146ea062008-08-26 23:13:13 -07001263 if (force_author && !strchr(force_author, '>'))
1264 force_author = find_author_by_nickname(force_author);
1265
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001266 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001267 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001268
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301269 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001270 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001271
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001272 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001273 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001274 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001275 if (amend && whence != FROM_COMMIT) {
1276 if (whence == FROM_MERGE)
1277 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001278 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001279 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001280 else if (whence == FROM_REBASE_PICK)
1281 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001282 }
Pat Notz89ac1222010-11-02 13:59:11 -06001283 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001284 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1285 die_for_incompatible_opt4(!!use_message, "-C",
1286 !!edit_message, "-c",
1287 !!logfile, "-F",
1288 !!fixup_message, "--fixup");
1289 die_for_incompatible_opt4(have_option_m, "-m",
1290 !!edit_message, "-c",
1291 !!use_message, "-C",
1292 !!logfile, "-F");
1293 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Junio C Hamano010c7db2012-03-30 11:30:59 -07001294 template_file = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001295 if (edit_message)
1296 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001297 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001298 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001299 if (!use_message && !is_from_cherry_pick(whence) &&
1300 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001301 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001302 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001303 use_message_buffer = read_commit_message(use_message);
1304 if (!renew_authorship) {
1305 author_message = use_message;
1306 author_message_buffer = use_message_buffer;
1307 }
1308 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001309 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1310 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001311 author_message = "CHERRY_PICK_HEAD";
1312 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001313 }
1314
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001315 if (patch_interactive)
1316 interactive = 1;
1317
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001318 die_for_incompatible_opt4(also, "-i/--include",
1319 only, "-o/--only",
1320 all, "-a/--all",
1321 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301322 if (fixup_message) {
1323 /*
1324 * We limit --fixup's suboptions to only alpha characters.
1325 * If the first character after a run of alpha is colon,
1326 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301327 * name like `amend` or `reword`, or a misspelt suboption
1328 * name. In either case, we treat it as
1329 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301330 *
1331 * Otherwise, we are dealing with --fixup=<commit>.
1332 */
1333 char *p = fixup_message;
1334 while (isalpha(*p))
1335 p++;
1336 if (p > fixup_message && *p == ':') {
1337 *p = '\0';
1338 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301339 if (!strcmp("amend", fixup_message) ||
1340 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301341 fixup_prefix = "amend";
1342 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301343 if (*fixup_message == 'r') {
1344 check_fixup_reword_options(argc, argv);
1345 only = 1;
1346 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301347 } else {
1348 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1349 }
1350 } else {
1351 fixup_commit = fixup_message;
1352 fixup_prefix = "fixup";
1353 use_editor = 0;
1354 }
1355 }
1356
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001357 if (0 <= edit_flag)
1358 use_editor = edit_flag;
1359
Denton Liuf29cd862019-04-17 11:23:25 +01001360 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001361
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001362 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001363
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001364 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001365 die(_("paths '%s ...' with -a does not make sense"),
1366 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001367
Jeff Kingf3f47a12012-10-18 21:15:50 +07001368 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001369 dry_run = 1;
1370
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001371 return argc;
1372}
1373
Jeff Kinge885a842020-09-30 08:28:18 -04001374static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001375 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001376{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001377 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001378 const char *index_file;
1379
Jeff Kinge885a842020-09-30 08:28:18 -04001380 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001381 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001382 rollback_index_files();
1383
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001384 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001385}
1386
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001387define_list_config_array_extra(color_status_slots, {"added"});
1388
Jonathan Nieder88521172014-10-07 15:16:57 -04001389static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001390{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001391 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001392 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001393
1394 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001395}
1396
1397static int git_status_config(const char *k, const char *v, void *cb)
1398{
1399 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001400 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001401
Christian Couder59556542013-11-30 21:55:40 +01001402 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001403 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001404 if (!strcmp(k, "status.submodulesummary")) {
1405 int is_bool;
1406 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1407 if (is_bool && s->submodule_summary)
1408 s->submodule_summary = -1;
1409 return 0;
1410 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001411 if (!strcmp(k, "status.short")) {
1412 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001413 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001414 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001415 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001416 return 0;
1417 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001418 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001419 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001420 return 0;
1421 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001422 if (!strcmp(k, "status.aheadbehind")) {
1423 status_deferred_config.ahead_behind = git_config_bool(k, v);
1424 return 0;
1425 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001426 if (!strcmp(k, "status.showstash")) {
1427 s->show_stash = git_config_bool(k, v);
1428 return 0;
1429 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001430 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001431 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001432 return 0;
1433 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001434 if (!strcmp(k, "status.displaycommentprefix")) {
1435 s->display_comment_prefix = git_config_bool(k, v);
1436 return 0;
1437 }
René Scharfee3f1da92014-10-04 20:54:50 +02001438 if (skip_prefix(k, "status.color.", &slot_name) ||
1439 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001440 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001441 if (slot < 0)
1442 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001443 if (!v)
1444 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001445 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001446 }
1447 if (!strcmp(k, "status.relativepaths")) {
1448 s->relative_paths = git_config_bool(k, v);
1449 return 0;
1450 }
1451 if (!strcmp(k, "status.showuntrackedfiles")) {
1452 if (!v)
1453 return config_error_nonbool(k);
1454 else if (!strcmp(v, "no"))
1455 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1456 else if (!strcmp(v, "normal"))
1457 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1458 else if (!strcmp(v, "all"))
1459 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1460 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001461 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001462 return 0;
1463 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001464 if (!strcmp(k, "diff.renamelimit")) {
1465 if (s->rename_limit == -1)
1466 s->rename_limit = git_config_int(k, v);
1467 return 0;
1468 }
1469 if (!strcmp(k, "status.renamelimit")) {
1470 s->rename_limit = git_config_int(k, v);
1471 return 0;
1472 }
1473 if (!strcmp(k, "diff.renames")) {
1474 if (s->detect_rename == -1)
1475 s->detect_rename = git_config_rename(k, v);
1476 return 0;
1477 }
1478 if (!strcmp(k, "status.renames")) {
1479 s->detect_rename = git_config_rename(k, v);
1480 return 0;
1481 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001482 return git_diff_ui_config(k, v, NULL);
1483}
1484
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001485int cmd_status(int argc, const char **argv, const char *prefix)
1486{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001487 static int no_renames = -1;
1488 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001489 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001490 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001491 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001492 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001493 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001494 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001495 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001496 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001497 OPT_BOOL('b', "branch", &s.show_branch,
1498 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001499 OPT_BOOL(0, "show-stash", &s.show_stash,
1500 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001501 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1502 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001503 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001504 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001505 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001506 OPT_SET_INT(0, "long", &status_format,
1507 N_("show status in long format (default)"),
1508 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001509 OPT_BOOL('z', "null", &s.null_termination,
1510 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001511 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001512 N_("mode"),
1513 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001514 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001515 { OPTION_STRING, 0, "ignored", &ignored_arg,
1516 N_("mode"),
1517 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1518 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001519 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1520 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001521 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001522 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001523 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001524 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001525 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001526 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001527 OPT_END(),
1528 };
1529
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001530 if (argc == 2 && !strcmp(argv[1], "-h"))
1531 usage_with_options(builtin_status_usage, builtin_status_options);
1532
Derrick Stoleed76723e2021-07-14 13:12:37 +00001533 prepare_repo_settings(the_repository);
1534 the_repository->settings.command_requires_full_index = 0;
1535
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001536 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001537 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001538 builtin_status_options,
1539 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001540 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001541 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001542
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001543 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001544 handle_ignored_arg(&s);
1545
1546 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1547 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1548 die(_("Unsupported combination of ignored and untracked-files arguments"));
1549
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001550 parse_pathspec(&s.pathspec, 0,
1551 PATHSPEC_PREFER_FULL,
1552 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001553
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001554 if (status_format != STATUS_FORMAT_PORCELAIN &&
1555 status_format != STATUS_FORMAT_PORCELAIN_V2)
1556 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001557 repo_read_index(the_repository);
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001558 refresh_index(&the_index,
1559 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1560 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001561
Jeff King27344d62017-09-27 02:54:30 -04001562 if (use_optional_locks())
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001563 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
Jeff King27344d62017-09-27 02:54:30 -04001564 else
1565 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001566
brian m. carlsone82caf32017-07-13 23:49:28 +00001567 s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001568 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001569 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001570
Jens Lehmann46a958b2010-06-25 16:56:47 +02001571 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001572 s.status_format = status_format;
1573 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001574 if (no_renames != -1)
1575 s.detect_rename = !no_renames;
1576 if ((intptr_t)rename_score_arg != -1) {
1577 if (s.detect_rename < DIFF_DETECT_RENAME)
1578 s.detect_rename = DIFF_DETECT_RENAME;
1579 if (rename_score_arg)
1580 s.rename_score = parse_rename_score(&rename_score_arg);
1581 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001582
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001583 wt_status_collect(&s);
1584
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001585 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001586 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001587
Jeff King86617682009-12-07 00:26:25 -05001588 if (s.relative_paths)
1589 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001590
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001591 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001592 wt_status_collect_free_buffers(&s);
1593
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001594 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001595}
1596
Stephan Beyer186458b2008-07-24 01:09:35 +02001597static int git_commit_config(const char *k, const char *v, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001598{
Junio C Hamanod249b092009-08-09 21:59:30 -07001599 struct wt_status *s = cb;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001600 int status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001601
Brian Hetro984c6e72008-07-05 01:24:40 -04001602 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001603 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001604 if (!strcmp(k, "commit.status")) {
1605 include_status = git_config_bool(k, v);
1606 return 0;
1607 }
Ralf Thielow51fb3a32013-01-10 18:45:59 +01001608 if (!strcmp(k, "commit.cleanup"))
1609 return git_config_string(&cleanup_arg, k, v);
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001610 if (!strcmp(k, "commit.gpgsign")) {
1611 sign_commit = git_config_bool(k, v) ? "" : NULL;
1612 return 0;
1613 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301614 if (!strcmp(k, "commit.verbose")) {
1615 int is_bool;
1616 config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
1617 return 0;
1618 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001619
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001620 status = git_gpg_config(k, v, NULL);
1621 if (status)
1622 return status;
Junio C Hamanod249b092009-08-09 21:59:30 -07001623 return git_status_config(k, v, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001624}
1625
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001626int cmd_commit(int argc, const char **argv, const char *prefix)
1627{
Jeff King036dbbf2012-05-07 15:18:26 -04001628 static struct wt_status s;
1629 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001630 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1631 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001632
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001633 OPT_GROUP(N_("Commit message options")),
1634 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1635 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1636 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1637 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1638 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1639 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301640 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301641 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1642 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301643 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301644 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 +07001645 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001646 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 -04001647 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 -07001648 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001649 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1650 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001651 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001652 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001653 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001654 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001655 /* end commit message options */
1656
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001657 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001658 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1659 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1660 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1661 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1662 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001663 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001664 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 +07001665 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001666 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001667 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001668 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1669 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001670 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001671 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001672 OPT_SET_INT(0, "long", &status_format,
1673 N_("show status in long format (default)"),
1674 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001675 OPT_BOOL('z', "null", &s.null_termination,
1676 N_("terminate entries with NUL")),
1677 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1678 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 +07001679 { 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 +00001680 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1681 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001682 /* end commit contents options */
1683
Stefan Beller4741edd2013-08-03 13:51:18 +02001684 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1685 N_("ok to record an empty change")),
1686 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1687 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001688
1689 OPT_END()
1690 };
1691
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001692 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001693 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001694 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001695 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001696 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001697 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001698 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001699 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001700 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001701 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001702
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001703 if (argc == 2 && !strcmp(argv[1], "-h"))
1704 usage_with_options(builtin_commit_usage, builtin_commit_options);
1705
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001706 prepare_repo_settings(the_repository);
1707 the_repository->settings.command_requires_full_index = 0;
1708
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001709 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301710 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301711 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001712 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001713
brian m. carlsone82caf32017-07-13 23:49:28 +00001714 if (get_oid("HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001715 current_head = NULL;
1716 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001717 current_head = lookup_commit_or_die(&oid, "HEAD");
Jeff King5e7d4d32013-10-24 04:53:19 -04001718 if (parse_commit(current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001719 die(_("could not parse HEAD commit"));
1720 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301721 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001722 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1723 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001724 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301725 if (verbose == -1)
1726 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1727
Jeff Kingc9bfb952011-08-17 22:05:35 -07001728 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001729 return dry_run_commit(argv, prefix, current_head, &s);
1730 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001731
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001732 /* Set up everything for writing the commit object. This includes
1733 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001734 if (!prepare_to_commit(index_file, prefix,
1735 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001736 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001737 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001738 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001739 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001740
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001741 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001742 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001743 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001744 if (!reflog_msg)
1745 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001746 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001747 if (!reflog_msg)
1748 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001749 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001750 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001751 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001752 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001753 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001754 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001755
Christian Couder643cb5f2010-06-12 18:05:12 +02001756 if (!reflog_msg)
1757 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001758 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001759 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001760 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001761 struct commit *parent;
1762
1763 parent = get_merge_parent(m.buf);
1764 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001765 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001766 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001767 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001768 fclose(fp);
1769 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001770 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1771 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001772 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001773 if (!strcmp(sb.buf, "no-ff"))
1774 allow_fast_forward = 0;
1775 }
1776 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001777 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001778 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001779 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001780 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001781 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001782 : is_from_rebase(whence)
1783 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001784 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001785 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001786 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001787
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001788 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001789 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301790 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001791 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001792 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001793 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001794 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001795
Denton Liuf29cd862019-04-17 11:23:25 +01001796 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301797
Phillip Woodd0aaa462017-11-10 11:09:42 +00001798 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001799 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001800 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001801 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001802 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001803 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301804 rollback_index_files();
1805 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1806 exit(1);
1807 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001808
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301809 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1810 !allow_empty_message) {
1811 struct strbuf body = STRBUF_INIT;
1812 size_t len = commit_subject_length(sb.buf);
1813 strbuf_addstr(&body, sb.buf + len);
1814 if (message_is_empty(&body, cleanup_mode)) {
1815 rollback_index_files();
1816 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1817 exit(1);
1818 }
1819 strbuf_release(&body);
1820 }
1821
Junio C Hamano0074d182011-12-20 13:20:56 -08001822 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001823 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001824 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001825 } else {
1826 struct commit_extra_header **tail = &extra;
1827 append_merge_tag_headers(parents, &tail);
1828 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001829
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001830 if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001831 parents, &oid, author_ident.buf, NULL,
1832 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001833 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001834 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001835 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001836 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001837
Phillip Wood0505d602017-11-17 11:34:47 +00001838 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1839 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001840 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001841 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001842 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001843
Junio C Hamanof496b062019-07-09 15:25:44 -07001844 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001845 unlink(git_path_merge_head(the_repository));
1846 unlink(git_path_merge_msg(the_repository));
1847 unlink(git_path_merge_mode(the_repository));
1848 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001849
Brandon Casey5a9dd392008-01-23 11:21:22 -06001850 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001851 die(_("repository has been updated, but unable to write\n"
1852 "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 +07001853 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001854
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001855 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001856
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001857 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001858 run_auto_maintenance(quiet);
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001859 run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1860 NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001861 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001862 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001863 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001864 if (!quiet) {
1865 unsigned int flags = 0;
1866
1867 if (!current_head)
1868 flags |= SUMMARY_INITIAL_COMMIT;
1869 if (author_date_is_interesting())
1870 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001871 print_commit_summary(the_repository, prefix,
1872 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001873 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001874
Denton Liua03b5552020-04-07 10:28:07 -04001875 apply_autostash(git_path_merge_autostash(the_repository));
1876
Junio C Hamano00d8c312022-05-12 15:51:07 -07001877cleanup:
1878 UNLEAK(author_ident);
Jeff King0e5bba52017-09-08 02:38:41 -04001879 UNLEAK(err);
1880 UNLEAK(sb);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001881 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001882}