blob: 7da5f924484d586c765796f8e411a042f2a3817a [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
Ævar Arnfjörð Bjarmasondfd0a892023-02-10 11:28:39 +01008#define USE_THE_INDEX_VARIABLE
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00009#include "builtin.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +000010#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070011#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020012#include "lockfile.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050013#include "cache-tree.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010014#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080015#include "dir.h"
Elijah Newren4e120822023-04-11 00:41:57 -070016#include "editor.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +000017#include "environment.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050018#include "diff.h"
19#include "diffcore.h"
20#include "commit.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000021#include "gettext.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050022#include "revision.h"
23#include "wt-status.h"
24#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020025#include "hook.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050026#include "refs.h"
27#include "log-tree.h"
28#include "strbuf.h"
29#include "utf8.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070030#include "object-name.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050031#include "parse-options.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000032#include "path.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000033#include "preload-index.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000034#include "read-cache.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010035#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020036#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080037#include "unpack-trees.h"
Junio C Hamano76e2f7c2009-08-07 23:31:57 -070038#include "quote.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +020039#include "submodule.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070040#include "gpg-interface.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070041#include "column.h"
Miklos Vajna5ed75e22012-09-14 08:52:03 +020042#include "sequencer.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000043#include "sparse-index.h"
Antoine Pelisseea167942013-08-23 15:48:31 +020044#include "mailmap.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020045#include "help.h"
Derrick Stolee64043552018-07-20 16:33:04 +000046#include "commit-reach.h"
Derrick Stolee859fdc02018-08-29 05:49:04 -070047#include "commit-graph.h"
Ævar Arnfjörð Bjarmason88c7b4c2022-02-16 09:14:02 +010048#include "pretty.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050049
50static const char * const builtin_commit_usage[] = {
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020051 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
52 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
53 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
54 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
55 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
56 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
57 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
58 " [--] [<pathspec>...]"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050059 NULL
60};
61
Shawn Bohrer2f02b252007-12-02 23:02:09 -060062static const char * const builtin_status_usage[] = {
Ævar Arnfjörð Bjarmason463ea0c2022-10-13 17:39:21 +020063 N_("git status [<options>] [--] [<pathspec>...]"),
Shawn Bohrer2f02b252007-12-02 23:02:09 -060064 NULL
65};
66
Jeff Kingf197ed22010-06-06 20:41:46 -040067static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000068N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040069"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000070"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040071
Jay Soffian37f7a852011-02-19 23:12:29 -050072static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070073N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050074"If you wish to commit it anyway, use:\n"
75"\n"
76" git commit --allow-empty\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040077"\n");
78
Phillip Wood430b75f2019-12-06 16:06:12 +000079static const char empty_rebase_pick_advice[] =
80N_("Otherwise, please use 'git rebase --skip'\n");
81
Jeff Kingc17592a2013-07-26 19:39:28 -040082static const char empty_cherry_pick_advice_single[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053083N_("Otherwise, please use 'git cherry-pick --skip'\n");
Jeff Kingc17592a2013-07-26 19:39:28 -040084
85static const char empty_cherry_pick_advice_multi[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053086N_("and then use:\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050087"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053088" git cherry-pick --continue\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040089"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053090"to resume cherry-picking the remaining commits.\n"
91"If you wish to skip this commit, use:\n"
92"\n"
93" git cherry-pick --skip\n"
94"\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050095
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020096static const char *color_status_slots[] = {
97 [WT_STATUS_HEADER] = "header",
98 [WT_STATUS_UPDATED] = "updated",
99 [WT_STATUS_CHANGED] = "changed",
100 [WT_STATUS_UNTRACKED] = "untracked",
101 [WT_STATUS_NOBRANCH] = "noBranch",
102 [WT_STATUS_UNMERGED] = "unmerged",
103 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
104 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
105 [WT_STATUS_ONBRANCH] = "branch",
106};
107
Jay Soffian37f7a852011-02-19 23:12:29 -0500108static const char *use_message_buffer;
Junio C Hamano28886052007-11-18 01:52:55 -0800109static struct lock_file index_lock; /* real index */
110static struct lock_file false_lock; /* used only for partial commits */
111static enum {
112 COMMIT_AS_IS = 1,
113 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000114 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -0800115} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500116
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700117static const char *logfile, *force_author;
Brian Hetro984c6e72008-07-05 01:24:40 -0400118static const char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -0500119/*
120 * The _message variables are commit names from which to take
121 * the commit message and/or authorship.
122 */
123static const char *author_message, *author_message_buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500124static char *edit_message, *use_message;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530125static char *fixup_message, *fixup_commit, *squash_message;
126static const char *fixup_prefix;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800127static int all, also, interactive, patch_interactive, only, amend, signoff;
128static int edit_flag = -1; /* unspecified */
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200129static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Pranit Bauvaaaab8422016-05-05 15:20:02 +0530130static int config_commit_verbose = -1; /* unspecified */
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000131static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400132static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000133static char *sign_commit, *pathspec_from_file;
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000134static struct strvec trailer_args = STRVEC_INIT;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700135
Alex Riesen5f065732007-12-22 19:46:24 +0100136/*
137 * The default commit message cleanup mode will remove the lines
138 * beginning with # (shell comments) and leading and trailing
139 * whitespaces (empty lines or containing only whitespaces)
140 * if editor is used, and only the whitespaces if the message
141 * is specified explicitly.
142 */
Phillip Woodd0aaa462017-11-10 11:09:42 +0000143static enum commit_msg_cleanup_mode cleanup_mode;
Ralf Thielow51fb3a32013-01-10 18:45:59 +0100144static const char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500145
Jay Soffian37f7a852011-02-19 23:12:29 -0500146static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700147static int use_editor = 1, include_status = 1;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400148static int have_option_m;
Jeff King2c477892011-12-18 00:03:22 -0500149static struct strbuf message = STRBUF_INIT;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000150
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400151static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
Junio C Hamano84b42022013-06-24 11:41:40 -0700152
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000153static int opt_pass_trailer(const struct option *opt, const char *arg, int unset)
154{
155 BUG_ON_OPT_NEG(unset);
156
Jeff King116761b2022-10-06 09:11:31 -0400157 strvec_pushl(opt->value, "--trailer", arg, NULL);
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000158 return 0;
159}
160
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400161static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
162{
163 enum wt_status_format *value = (enum wt_status_format *)opt->value;
164 if (unset)
165 *value = STATUS_FORMAT_NONE;
166 else if (!arg)
167 *value = STATUS_FORMAT_PORCELAIN;
168 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
169 *value = STATUS_FORMAT_PORCELAIN;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400170 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
171 *value = STATUS_FORMAT_PORCELAIN_V2;
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400172 else
173 die("unsupported porcelain version '%s'", arg);
174
175 return 0;
176}
Jeff King7c9f7032009-09-05 04:59:56 -0400177
Johannes Schindelinf9568532007-11-11 17:36:39 +0000178static int opt_parse_m(const struct option *opt, const char *arg, int unset)
179{
180 struct strbuf *buf = opt->value;
René Scharfe25206772013-05-25 23:43:34 +0200181 if (unset) {
182 have_option_m = 0;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000183 strbuf_setlen(buf, 0);
René Scharfe25206772013-05-25 23:43:34 +0200184 } else {
185 have_option_m = 1;
Brandon Caseya24a41e2013-02-18 20:17:06 -0800186 if (buf->len)
187 strbuf_addch(buf, '\n');
Johannes Schindelinf9568532007-11-11 17:36:39 +0000188 strbuf_addstr(buf, arg);
Brandon Caseya24a41e2013-02-18 20:17:06 -0800189 strbuf_complete_line(buf);
Johannes Schindelinf9568532007-11-11 17:36:39 +0000190 }
191 return 0;
192}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500193
Ben Pearte8b2dc22018-05-11 15:38:58 +0000194static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
195{
196 const char **value = opt->value;
Jeff King517fe802018-11-05 01:45:42 -0500197
198 BUG_ON_OPT_NEG(unset);
199
Ben Pearte8b2dc22018-05-11 15:38:58 +0000200 if (arg != NULL && *arg == '=')
201 arg = arg + 1;
202
203 *value = arg;
204 return 0;
205}
206
Jay Soffian37f7a852011-02-19 23:12:29 -0500207static void determine_whence(struct wt_status *s)
208{
Stefan Beller102de882018-05-17 15:51:51 -0700209 if (file_exists(git_path_merge_head(the_repository)))
Jay Soffian37f7a852011-02-19 23:12:29 -0500210 whence = FROM_MERGE;
Phillip Wood901ba7b2019-12-06 16:06:11 +0000211 else if (!sequencer_determine_whence(the_repository, &whence))
Jay Soffian37f7a852011-02-19 23:12:29 -0500212 whence = FROM_COMMIT;
213 if (s)
214 s->whence = whence;
215}
216
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200217static void status_init_config(struct wt_status *s, config_fn_t fn)
218{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100219 wt_status_prepare(the_repository, s);
Eckhard S. Maaßdc6b1d92018-05-04 13:12:15 +0200220 init_diff_ui_defaults();
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200221 git_config(fn, s);
222 determine_whence(s);
Ben Boeckeled9bff02021-08-23 12:44:00 +0200223 s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200224}
225
Junio C Hamano28886052007-11-18 01:52:55 -0800226static void rollback_index_files(void)
227{
228 switch (commit_style) {
229 case COMMIT_AS_IS:
230 break; /* nothing to do */
231 case COMMIT_NORMAL:
232 rollback_lock_file(&index_lock);
233 break;
234 case COMMIT_PARTIAL:
235 rollback_lock_file(&index_lock);
236 rollback_lock_file(&false_lock);
237 break;
238 }
239}
240
Brandon Casey5a9dd392008-01-23 11:21:22 -0600241static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800242{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600243 int err = 0;
244
Junio C Hamano28886052007-11-18 01:52:55 -0800245 switch (commit_style) {
246 case COMMIT_AS_IS:
247 break; /* nothing to do */
248 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600249 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800250 break;
251 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600252 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800253 rollback_lock_file(&false_lock);
254 break;
255 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600256
257 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800258}
259
260/*
261 * Take a union of paths in the index and the named tree (typically, "HEAD"),
262 * and return the paths that match the given pattern in list.
263 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100264static int list_paths(struct string_list *list, const char *with_tree,
Jeff Kingc5c33502019-03-20 04:15:48 -0400265 const struct pathspec *pattern)
Junio C Hamano28886052007-11-18 01:52:55 -0800266{
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700267 int i, ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800268 char *m;
269
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700270 if (!pattern->nr)
Junio C Hamanob9a08012012-07-15 21:39:48 -0700271 return 0;
272
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700273 m = xcalloc(1, pattern->nr);
Junio C Hamano28886052007-11-18 01:52:55 -0800274
Clemens Buchacher8894d532011-07-30 19:13:47 +0200275 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200276 char *max_prefix = common_prefix(pattern);
Brandon Williams86238e02018-04-03 10:57:45 -0700277 overlay_tree_on_index(&the_index, with_tree, max_prefix);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200278 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200279 }
Junio C Hamano28886052007-11-18 01:52:55 -0800280
Derrick Stoleecb8388d2021-04-01 01:49:45 +0000281 /* TODO: audit for interaction with sparse-index. */
282 ensure_full_index(&the_index);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100283 for (i = 0; i < the_index.cache_nr; i++) {
284 const struct cache_entry *ce = the_index.cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700285 struct string_list_item *item;
286
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800287 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800288 continue;
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200289 if (!ce_path_match(&the_index, ce, pattern, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800290 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100291 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700292 if (ce_skip_worktree(ce))
293 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800294 }
295
Jeff Kingc5c33502019-03-20 04:15:48 -0400296 ret = report_path_error(m, pattern);
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700297 free(m);
298 return ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800299}
300
Johannes Schindelinc455c872008-07-21 19:03:49 +0100301static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800302{
303 int i;
304 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700305 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100306 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700307
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700308 /* p->util is skip-worktree */
309 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700310 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700311
Johannes Schindelinc455c872008-07-21 19:03:49 +0100312 if (!lstat(p->string, &st)) {
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +0100313 if (add_to_index(&the_index, p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000314 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200315 } else
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100316 remove_file_from_index(&the_index, p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800317 }
318}
319
Junio C Hamano06bb6432011-08-19 11:58:18 -0700320static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800321{
322 struct tree *tree;
323 struct unpack_trees_options opts;
324 struct tree_desc t;
325
Junio C Hamano06bb6432011-08-19 11:58:18 -0700326 if (!current_head) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100327 discard_index(&the_index);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800328 return;
329 }
330
331 memset(&opts, 0, sizeof(opts));
332 opts.head_idx = 1;
333 opts.index_only = 1;
334 opts.merge = 1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800335 opts.src_index = &the_index;
336 opts.dst_index = &the_index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800337
338 opts.fn = oneway_merge;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000339 tree = parse_tree_indirect(&current_head->object.oid);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800340 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000341 die(_("failed to unpack HEAD tree object"));
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800342 parse_tree(tree);
343 init_tree_desc(&t, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500344 if (unpack_trees(1, &t, &opts))
345 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800346}
347
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100348static void refresh_cache_or_die(int refresh_flags)
349{
350 /*
351 * refresh_flags contains REFRESH_QUIET, so the only errors
352 * are for unmerged entries.
353 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100354 if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100355 die_resolve_conflict("commit");
356}
357
Jeff Kinge885a842020-09-30 08:28:18 -0400358static const char *prepare_index(const char **argv, const char *prefix,
Michael Haggerty35ff08b2014-10-01 12:28:17 +0200359 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500360{
Martin Ågrendd1055e2017-09-23 01:34:49 +0200361 struct string_list partial = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700362 struct pathspec pathspec;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700363 int refresh_flags = REFRESH_QUIET;
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200364 const char *ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500365
Junio C Hamano50b7e702009-08-04 23:49:33 -0700366 if (is_status)
367 refresh_flags |= REFRESH_UNMERGED;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700368 parse_pathspec(&pathspec, 0,
369 PATHSPEC_PREFER_FULL,
370 prefix, argv);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500371
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000372 if (pathspec_from_file) {
373 if (interactive)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000374 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000375
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000376 if (all)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000377 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000378
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000379 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000380 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000381
382 parse_pathspec_file(&pathspec, 0,
383 PATHSPEC_PREFER_FULL,
384 prefix, pathspec_from_file, pathspec_file_nul);
385 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000386 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000387 }
388
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530389 if (!pathspec.nr && (also || (only && !allow_empty &&
390 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000391 die(_("No paths with --include/--only does not make sense."));
392
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100393 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000394 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800395
Conrad Irwin1020d082011-05-06 22:59:59 -0700396 if (interactive) {
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000397 char *old_index_env = NULL, *old_repo_index_file;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100398 repo_hold_locked_index(the_repository, &index_lock,
399 LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700400
401 refresh_cache_or_die(refresh_flags);
402
Martin Ågren812d6b02017-10-06 22:12:12 +0200403 if (write_locked_index(&the_index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700404 die(_("unable to create temporary index"));
405
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000406 old_repo_index_file = the_repository->index_file;
407 the_repository->index_file =
408 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500409 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000410 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700411
Jeff Kinge885a842020-09-30 08:28:18 -0400412 if (interactive_add(argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700413 die(_("interactive add failed"));
414
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000415 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700416 if (old_index_env && *old_index_env)
417 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
418 else
419 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500420 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700421
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100422 discard_index(&the_index);
423 read_index_from(&the_index, get_lock_file_path(&index_lock),
424 get_git_dir());
Ævar Arnfjörð Bjarmasonfcb864b2023-02-10 11:28:38 +0100425 if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700426 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700427 die(_("unable to write index file"));
Martin Ågren812d6b02017-10-06 22:12:12 +0200428 if (write_locked_index(&the_index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700429 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700430 } else
431 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700432
433 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200434 ret = get_lock_file_path(&index_lock);
435 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700436 }
437
Junio C Hamano28886052007-11-18 01:52:55 -0800438 /*
439 * Non partial, non as-is commit.
440 *
441 * (1) get the real index;
442 * (2) update the_index as necessary;
443 * (3) write the_index out to the real index (still locked);
444 * (4) return the name of the locked index file.
445 *
446 * The caller should run hooks on the locked real index, and
447 * (A) if all goes well, commit the real index;
448 * (B) on failure, rollback the real index.
449 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700450 if (all || (also && pathspec.nr)) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100451 repo_hold_locked_index(the_repository, &index_lock,
452 LOCK_DIE_ON_ERROR);
Elijah Newren50c37ee2023-05-16 06:33:46 +0000453 add_files_to_cache(the_repository, also ? prefix : NULL,
454 &pathspec, 0, 0);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100455 refresh_cache_or_die(refresh_flags);
Ævar Arnfjörð Bjarmasonfcb864b2023-02-10 11:28:38 +0100456 cache_tree_update(&the_index, WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200457 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000458 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800459 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200460 ret = get_lock_file_path(&index_lock);
461 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500462 }
463
Junio C Hamano28886052007-11-18 01:52:55 -0800464 /*
465 * As-is commit.
466 *
467 * (1) return the name of the real index file.
468 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200469 * The caller should run hooks on the real index,
470 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800471 * We still need to refresh the index here.
472 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700473 if (!only && !pathspec.nr) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100474 repo_hold_locked_index(the_repository, &index_lock,
475 LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100476 refresh_cache_or_die(refresh_flags);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100477 if (the_index.cache_changed
478 || !cache_tree_fully_valid(the_index.cache_tree))
Ævar Arnfjörð Bjarmasonfcb864b2023-02-10 11:28:38 +0100479 cache_tree_update(&the_index, WRITE_TREE_SILENT);
Martin Ågren61000812018-03-01 21:40:20 +0100480 if (write_locked_index(&the_index, &index_lock,
481 COMMIT_LOCK | SKIP_IF_UNCHANGED))
482 die(_("unable to write new_index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800483 commit_style = COMMIT_AS_IS;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200484 ret = get_index_file();
485 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500486 }
487
Junio C Hamano28886052007-11-18 01:52:55 -0800488 /*
489 * A partial commit.
490 *
491 * (0) find the set of affected paths;
492 * (1) get lock on the real index file;
493 * (2) update the_index with the given paths;
494 * (3) write the_index out to the real index (still locked);
495 * (4) get lock on the false index file;
496 * (5) reset the_index from HEAD;
497 * (6) update the_index the same way as (2);
498 * (7) write the_index out to the false index file;
499 * (8) return the name of the false index file (still locked);
500 *
501 * The caller should run hooks on the locked false index, and
502 * create commit from it. Then
503 * (A) if all goes well, commit the real index;
504 * (B) on failure, rollback the real index;
505 * In either case, rollback the false index.
506 */
507 commit_style = COMMIT_PARTIAL;
508
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000509 if (whence != FROM_COMMIT) {
510 if (whence == FROM_MERGE)
511 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000512 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000513 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000514 else if (is_from_rebase(whence))
515 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000516 }
Junio C Hamano28886052007-11-18 01:52:55 -0800517
Jeff Kingc5c33502019-03-20 04:15:48 -0400518 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800519 exit(1);
520
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100521 discard_index(&the_index);
522 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000523 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800524
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100525 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800526 add_remove_files(&partial);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100527 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Ævar Arnfjörð Bjarmasonfcb864b2023-02-10 11:28:38 +0100528 cache_tree_update(&the_index, WRITE_TREE_SILENT);
Martin Ågren812d6b02017-10-06 22:12:12 +0200529 if (write_locked_index(&the_index, &index_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000530 die(_("unable to write new_index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500531
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700532 hold_lock_file_for_update(&false_lock,
533 git_path("next-index-%"PRIuMAX,
534 (uintmax_t) getpid()),
535 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800536
Junio C Hamano06bb6432011-08-19 11:58:18 -0700537 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800538 add_remove_files(&partial);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100539 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500540
Martin Ågren812d6b02017-10-06 22:12:12 +0200541 if (write_locked_index(&the_index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000542 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500543
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100544 discard_index(&the_index);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200545 ret = get_lock_file_path(&false_lock);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100546 read_index_from(&the_index, ret, get_git_dir());
Martin Ågrendd1055e2017-09-23 01:34:49 +0200547out:
548 string_list_clear(&partial, 0);
549 clear_pathspec(&pathspec);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200550 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500551}
552
Junio C Hamanod249b092009-08-09 21:59:30 -0700553static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
554 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500555{
brian m. carlson8066df42017-02-20 00:10:14 +0000556 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700557
Junio C Hamanod249b092009-08-09 21:59:30 -0700558 if (s->relative_paths)
559 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500560
561 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700562 s->amend = 1;
563 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500564 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700565 s->verbose = verbose;
566 s->index_file = index_file;
567 s->fp = fp;
568 s->nowarn = nowarn;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200569 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400570 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000571 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400572 s->status_format = status_format;
573 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500574
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700575 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400576 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700577 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500578
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700579 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500580}
581
Junio C Hamano06bb6432011-08-19 11:58:18 -0700582static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100583{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700584 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100585}
586
Jeff Kingfac90832014-12-10 10:42:10 -0500587static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
588{
Jeff Kingc83a5092014-12-10 10:43:42 -0500589 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200590 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500591}
592
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700593static void export_one(const char *var, const char *s, const char *e, int hack)
594{
595 struct strbuf buf = STRBUF_INIT;
596 if (hack)
597 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100598 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700599 setenv(var, buf.buf, 1);
600 strbuf_release(&buf);
601}
602
Jeff Kingc33ddc22014-08-27 03:57:08 -0400603static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400604{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400605 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400606
Jeff Kingc33ddc22014-08-27 03:57:08 -0400607 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400608 int errors = 0;
609 unsigned long t = approxidate_careful(in, &errors);
610 if (errors)
611 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400612 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400613 }
614
615 return 0;
616}
617
Jeff Kingf4ef5172014-08-27 03:57:56 -0400618static void set_ident_var(char **buf, char *val)
619{
620 free(*buf);
621 *buf = val;
622}
623
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800624static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200625{
626 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700627 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200628
Jeff Kingeaa541e2015-01-12 20:58:33 -0500629 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
630 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
631 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200632
Jay Soffian37f7a852011-02-19 23:12:29 -0500633 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400634 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800635 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400636 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200637
Jeff Kingf0f96622014-08-27 03:57:28 -0400638 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200639 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400640 die(_("commit '%s' lacks author header"), author_message);
641 if (split_ident_line(&ident, a, len) < 0)
642 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200643
Jeff Kingf4ef5172014-08-27 03:57:56 -0400644 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
645 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200646
Jeff Kingf0f96622014-08-27 03:57:28 -0400647 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400648 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400649 strbuf_addch(&date_buf, '@');
650 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
651 strbuf_addch(&date_buf, ' ');
652 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400653 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400654 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200655 }
656
657 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400658 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200659
Jeff Kingf0f96622014-08-27 03:57:28 -0400660 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000661 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400662 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
663 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200664 }
665
Jeff King14ac2862014-05-01 21:12:42 -0400666 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400667 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400668 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400669 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400670 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400671 }
672
William Hubbs39ab4d02019-02-04 12:48:50 -0600673 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
674 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500675 assert_split_ident(&author, author_ident);
676 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
677 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
678 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400679 free(name);
680 free(email);
681 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200682}
683
Jeff Kingb7242b82014-05-01 21:10:01 -0400684static int author_date_is_interesting(void)
685{
686 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800687}
688
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700689static void adjust_comment_line_char(const struct strbuf *sb)
690{
691 char candidates[] = "#;@!$%^&|:";
692 char *candidate;
693 const char *p;
694
695 comment_line_char = candidates[0];
696 if (!memchr(sb->buf, comment_line_char, sb->len))
697 return;
698
699 p = sb->buf;
700 candidate = strchr(candidates, *p);
701 if (candidate)
702 *candidate = ' ';
703 for (p = sb->buf; *p; p++) {
704 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
705 candidate = strchr(candidates, p[1]);
706 if (candidate)
707 *candidate = ' ';
708 }
709 }
710
711 for (p = candidates; *p == ' '; p++)
712 ;
713 if (!*p)
714 die(_("unable to select a comment character that is not used\n"
715 "in the current commit message"));
716 comment_line_char = *p;
717}
718
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530719static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
720 struct pretty_print_context *ctx)
721{
722 const char *buffer, *subject, *fmt;
723
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200724 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530725 find_commit_subject(buffer, &subject);
726 /*
727 * If we amend the 'amend!' commit then we don't want to
728 * duplicate the subject line.
729 */
730 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200731 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200732 repo_unuse_commit_buffer(the_repository, commit, buffer);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530733}
734
Junio C Hamanod249b092009-08-09 21:59:30 -0700735static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700736 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800737 struct wt_status *s,
738 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500739{
740 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800741 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700742 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500743 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100744 const char *hook_arg1 = NULL;
745 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000746 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200747 int old_display_comment_prefix;
Denton Liu10559972019-04-17 11:23:28 +0100748 int merge_contains_scissors = 0;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100749 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500750
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700751 /* This checks and barfs if author is badly specified */
752 determine_author_info(author_ident);
753
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100754 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
755 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100756 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500757
Pat Notz89ac1222010-11-02 13:59:11 -0600758 if (squash_message) {
759 /*
760 * Insert the proper subject line before other commit
761 * message options add their content.
762 */
763 if (use_message && !strcmp(use_message, squash_message))
764 strbuf_addstr(&sb, "squash! ");
765 else {
766 struct pretty_print_context ctx = {0};
767 struct commit *c;
768 c = lookup_commit_reference_by_name(squash_message);
769 if (!c)
Teng Longe4cf0132023-05-29 21:27:56 +0800770 die(_("could not lookup commit '%s'"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600771 ctx.output_encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200772 repo_format_commit_message(the_repository, c,
773 "squash! %s\n\n", &sb,
774 &ctx);
Pat Notz89ac1222010-11-02 13:59:11 -0600775 }
776 }
777
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000778 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000779 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100780 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500781 } else if (logfile && !strcmp(logfile, "-")) {
782 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000783 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500784 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000785 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100786 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500787 } else if (logfile) {
788 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000789 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200790 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100791 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500792 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800793 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500794 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400795 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200796 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100797 hook_arg1 = "commit";
798 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600799 } else if (fixup_message) {
800 struct pretty_print_context ctx = {0};
801 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530802 char *fmt;
803 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600804 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +0800805 die(_("could not lookup commit '%s'"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600806 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530807 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200808 repo_format_commit_message(the_repository, commit, fmt, &sb,
809 &ctx);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530810 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600811 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530812
813 /*
814 * Only `-m` commit message option is checked here, as
815 * it supports `--fixup` to append the commit message.
816 *
817 * The other commit message options `-c`/`-C`/`-F` are
818 * incompatible with all the forms of `--fixup` and
819 * have already errored out while parsing the `git commit`
820 * options.
821 */
822 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
823 strbuf_addbuf(&sb, &message);
824
825 if (!strcmp(fixup_prefix, "amend")) {
826 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000827 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530828 prepare_amend_commit(commit, &sb, &ctx);
829 }
Stefan Beller102de882018-05-17 15:51:51 -0700830 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100831 size_t merge_msg_start;
832
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100833 /*
834 * prepend SQUASH_MSG here if it exists and a
835 * "merge --squash" was originally performed
836 */
Stefan Beller102de882018-05-17 15:51:51 -0700837 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
838 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100839 die_errno(_("could not read SQUASH_MSG"));
840 hook_arg1 = "squash";
841 } else
842 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100843
844 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700845 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000846 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100847
848 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
849 wt_status_locate_end(sb.buf + merge_msg_start,
850 sb.len - merge_msg_start) <
851 sb.len - merge_msg_start)
852 merge_contains_scissors = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700853 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
854 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000855 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100856 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600857 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500858 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000859 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100860 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200861 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500862 }
863
Paolo Bonzini8089c852008-02-05 08:04:18 +0100864 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500865 * The remaining cases don't modify the template message, but
866 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100867 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500868 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100869 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000870 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500871 hook_arg1 = "commit";
872 hook_arg2 = "CHERRY_PICK_HEAD";
873 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100874
Pat Notz89ac1222010-11-02 13:59:11 -0600875 if (squash_message) {
876 /*
877 * If squash_commit was used for the commit subject,
878 * then we're possibly hijacking other commit log options.
879 * Reset the hook args to tell the real story.
880 */
881 hook_arg1 = "message";
882 hook_arg2 = "";
883 }
884
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530885 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700886 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530887 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500888
Matthieu Moy2556b992013-09-06 19:43:07 +0200889 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
890 old_display_comment_prefix = s->display_comment_prefix;
891 s->display_comment_prefix = 1;
892
Matthieu Moyea9882b2013-09-12 12:50:06 +0200893 /*
894 * Most hints are counter-productive when the commit has
895 * already started.
896 */
897 s->hints = 0;
898
Boris Faure8b1ae672011-05-08 12:31:02 +0200899 if (clean_message_contents)
Calvin Wan787cb8a2023-06-06 19:48:43 +0000900 strbuf_stripspace(&sb, '\0');
Johannes Schindelin13208572007-11-11 17:35:58 +0000901
Junio C Hamano073bd752014-10-28 12:44:09 -0700902 if (signoff)
Jonathan Tan710714a2016-11-02 10:29:17 -0700903 append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000904
Jonathan Nieder37f30122011-02-25 23:10:49 -0600905 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000906 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500907
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700908 if (auto_comment_line_char)
909 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500910 strbuf_release(&sb);
911
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200912 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400913 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500914 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800915 int ident_shown = 0;
916 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400917 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800918 const char *hint_cleanup_all = allow_empty_message ?
919 _("Please enter the commit message for your changes."
920 " Lines starting\nwith '%c' will be ignored.\n") :
921 _("Please enter the commit message for your changes."
922 " Lines starting\nwith '%c' will be ignored, and an empty"
923 " message aborts the commit.\n");
924 const char *hint_cleanup_space = allow_empty_message ?
925 _("Please enter the commit message for your changes."
926 " Lines starting\n"
927 "with '%c' will be kept; you may remove them"
928 " yourself if you want to.\n") :
929 _("Please enter the commit message for your changes."
930 " Lines starting\n"
931 "with '%c' will be kept; you may remove them"
932 " yourself if you want to.\n"
933 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700934 if (whence != FROM_COMMIT) {
Denton Liu10559972019-04-17 11:23:28 +0100935 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
936 !merge_contains_scissors)
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700937 wt_status_add_cut_line(s->fp);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000938 status_printf_ln(
939 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400940 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000941 _("\n"
942 "It looks like you may be committing a merge.\n"
943 "If this is not correct, please run\n"
944 " git update-ref -d MERGE_HEAD\n"
945 "and try again.\n") :
946 _("\n"
947 "It looks like you may be committing a cherry-pick.\n"
948 "If this is not correct, please run\n"
949 " git update-ref -d CHERRY_PICK_HEAD\n"
950 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700951 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100952
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600953 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000954 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Hu Jialun54ba2f12021-07-10 02:07:31 +0800955 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
Denton Liu10559972019-04-17 11:23:28 +0100956 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
957 if (whence == FROM_COMMIT && !merge_contains_scissors)
958 wt_status_add_cut_line(s->fp);
959 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Hu Jialun54ba2f12021-07-10 02:07:31 +0800960 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100961
Jeff Kingfac90832014-12-10 10:42:10 -0500962 /*
963 * These should never fail because they come from our own
964 * fmt_ident. They may fail the sane_ident test, but we know
965 * that the name and mail pointers will at least be valid,
966 * which is enough for our tests and printing here.
967 */
968 assert_split_ident(&ai, author_ident);
969 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400970
971 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600972 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000973 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400974 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600975 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400976 (int)(ai.name_end - ai.name_begin), ai.name_begin,
977 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200978
Jeff Kingb7242b82014-05-01 21:10:01 -0400979 if (author_date_is_interesting())
980 status_printf_ln(s, GIT_COLOR_NORMAL,
981 _("%s"
982 "Date: %s"),
983 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400984 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100985
Jeff Kingd6991ce2012-11-14 16:34:13 -0800986 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600987 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000988 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400989 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600990 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400991 (int)(ci.name_end - ci.name_begin), ci.name_begin,
992 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200993
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +0530994 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200995
Junio C Hamanod249b092009-08-09 21:59:30 -0700996 saved_color_setting = s->use_color;
997 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700998 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700999 s->use_color = saved_color_setting;
Elijah Newren3e73cc62018-09-27 10:36:57 -07001000 string_list_clear(&s->change, 1);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001001 } else {
brian m. carlson8066df42017-02-20 00:10:14 +00001002 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001003 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +01001004
Johannes Schindelin2ee045e2023-06-29 13:23:10 +00001005 if (!the_index.initialized && repo_read_index(the_repository) < 0)
1006 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +01001007
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001008 if (amend)
1009 parent = "HEAD^1";
1010
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001011 if (repo_get_oid(the_repository, parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001012 int i, ita_nr = 0;
1013
Derrick Stoleecb8388d2021-04-01 01:49:45 +00001014 /* TODO: audit for interaction with sparse-index. */
1015 ensure_full_index(&the_index);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001016 for (i = 0; i < the_index.cache_nr; i++)
1017 if (ce_intent_to_add(the_index.cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001018 ita_nr++;
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001019 committable = the_index.cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001020 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001021 /*
1022 * Unless the user did explicitly request a submodule
1023 * ignore mode by passing a command line option we do
1024 * not ignore any changed submodule SHA-1s when
1025 * comparing index and parent, no matter what is
1026 * configured. Otherwise we won't commit any
1027 * submodules which were manually staged, which would
1028 * be really confusing.
1029 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001030 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001031 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001032 if (ignore_submodule_arg &&
1033 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001034 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001035 committable = index_differs_from(the_repository,
1036 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001037 }
Alex Riesen71686242007-11-28 22:13:08 +01001038 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001039 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001040
Jonathan Nieder37f30122011-02-25 23:10:49 -06001041 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001042
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001043 if (trailer_args.nr) {
1044 struct child_process run_trailer = CHILD_PROCESS_INIT;
1045
1046 strvec_pushl(&run_trailer.args, "interpret-trailers",
Jeff Kingbe3d6542023-06-17 00:26:24 -04001047 "--in-place", "--no-divider",
1048 git_path_commit_editmsg(), NULL);
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001049 strvec_pushv(&run_trailer.args, trailer_args.v);
1050 run_trailer.git_cmd = 1;
1051 if (run_command(&run_trailer))
1052 die(_("unable to pass trailers to --trailers"));
1053 strvec_clear(&trailer_args);
1054 }
1055
Jay Soffian37f7a852011-02-19 23:12:29 -05001056 /*
1057 * Reject an attempt to record a non-merge empty commit without
1058 * explicit --allow-empty. In the cherry-pick case, it may be
1059 * empty due to conflict resolution, which the user should okay.
1060 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001061 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001062 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001063 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001064 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001065 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001066 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001067 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001068 else if (is_from_cherry_pick(whence) ||
1069 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001070 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001071 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001072 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001073 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001074 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001075 else
1076 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001077 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001078 return 0;
1079 }
1080
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001081 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001082 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001083 * Re-read the index as the pre-commit-commit hook was invoked
1084 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001085 * the editor and after we invoke run_status above.
1086 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001087 discard_index(&the_index);
Kevin Willford680ee552017-08-14 15:54:25 -06001088 }
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001089 read_index_from(&the_index, index_file, get_git_dir());
Kevin Willford680ee552017-08-14 15:54:25 -06001090
Ævar Arnfjörð Bjarmasonfcb864b2023-02-10 11:28:38 +01001091 if (cache_tree_update(&the_index, 0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001092 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001093 return 0;
1094 }
1095
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001096 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301097 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001098 return 0;
1099
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001100 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001101 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001102
Jeff King22f9b7f2020-07-28 16:24:27 -04001103 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001104 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001105 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001106 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001107 exit(1);
1108 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001109 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001110 }
1111
1112 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001113 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1114 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001115 return 0;
1116 }
1117
1118 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001119}
1120
Junio C Hamano146ea062008-08-26 23:13:13 -07001121static const char *find_author_by_nickname(const char *name)
1122{
1123 struct rev_info revs;
1124 struct commit *commit;
1125 struct strbuf buf = STRBUF_INIT;
1126 const char *av[20];
1127 int ac = 0;
1128
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001129 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001130 strbuf_addf(&buf, "--author=%s", name);
1131 av[++ac] = "--all";
1132 av[++ac] = "-i";
1133 av[++ac] = buf.buf;
1134 av[++ac] = NULL;
1135 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001136 revs.mailmap = xmalloc(sizeof(struct string_list));
1137 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001138 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001139
Stefan Beller81c3ce32014-08-10 23:33:26 +02001140 if (prepare_revision_walk(&revs))
1141 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001142 commit = get_revision(&revs);
1143 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001144 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001145 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001146 strbuf_release(&buf);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +02001147 repo_format_commit_message(the_repository, commit,
1148 "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001149 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001150 return strbuf_detach(&buf, NULL);
1151 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001152 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001153}
1154
Jameson Millereec0f7f2017-10-30 13:21:37 -04001155static void handle_ignored_arg(struct wt_status *s)
1156{
1157 if (!ignored_arg)
1158 ; /* default already initialized */
1159 else if (!strcmp(ignored_arg, "traditional"))
1160 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1161 else if (!strcmp(ignored_arg, "no"))
1162 s->show_ignored_mode = SHOW_NO_IGNORED;
1163 else if (!strcmp(ignored_arg, "matching"))
1164 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1165 else
1166 die(_("Invalid ignored mode '%s'"), ignored_arg);
1167}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001168
1169static void handle_untracked_files_arg(struct wt_status *s)
1170{
1171 if (!untracked_files_arg)
1172 ; /* default already initialized */
1173 else if (!strcmp(untracked_files_arg, "no"))
1174 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1175 else if (!strcmp(untracked_files_arg, "normal"))
1176 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1177 else if (!strcmp(untracked_files_arg, "all"))
1178 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001179 /*
1180 * Please update $__git_untracked_file_modes in
1181 * git-completion.bash when you add new options
1182 */
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001183 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001184 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001185}
1186
Jay Soffian37f7a852011-02-19 23:12:29 -05001187static const char *read_commit_message(const char *name)
1188{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001189 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001190 struct commit *commit;
1191
1192 commit = lookup_commit_reference_by_name(name);
1193 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +08001194 die(_("could not lookup commit '%s'"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001195 out_enc = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001196 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001197}
1198
Junio C Hamano84b42022013-06-24 11:41:40 -07001199/*
1200 * Enumerate what needs to be propagated when --porcelain
1201 * is not in effect here.
1202 */
1203static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001204 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001205 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001206 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001207} status_deferred_config = {
1208 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001209 -1, /* unspecified */
1210 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001211};
1212
1213static void finalize_deferred_config(struct wt_status *s)
1214{
1215 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001216 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001217 !s->null_termination);
1218
1219 if (s->null_termination) {
1220 if (status_format == STATUS_FORMAT_NONE ||
1221 status_format == STATUS_FORMAT_UNSPECIFIED)
1222 status_format = STATUS_FORMAT_PORCELAIN;
1223 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001224 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001225 }
1226
1227 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1228 status_format = status_deferred_config.status_format;
1229 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1230 status_format = STATUS_FORMAT_NONE;
1231
1232 if (use_deferred_config && s->show_branch < 0)
1233 s->show_branch = status_deferred_config.show_branch;
1234 if (s->show_branch < 0)
1235 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001236
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001237 /*
1238 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001239 * line argument *AND* we will print in a human-readable format
1240 * (short, long etc.) then we inherit from the status.aheadbehind
1241 * config setting. In all other cases (and porcelain V[12] formats
1242 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001243 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001244 if (use_deferred_config &&
1245 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001246 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1247
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001248 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1249 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001250}
1251
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301252static void check_fixup_reword_options(int argc, const char *argv[]) {
1253 if (whence != FROM_COMMIT) {
1254 if (whence == FROM_MERGE)
1255 die(_("You are in the middle of a merge -- cannot reword."));
1256 else if (is_from_cherry_pick(whence))
1257 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1258 }
1259 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001260 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301261 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001262 die(_("reword option of '%s' and '%s' cannot be used together"),
1263 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301264}
1265
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001266static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001267 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001268 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001269 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001270 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001271 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001272{
Jeff King036dbbf2012-05-07 15:18:26 -04001273 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001274 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001275
Junio C Hamano146ea062008-08-26 23:13:13 -07001276 if (force_author && !strchr(force_author, '>'))
1277 force_author = find_author_by_nickname(force_author);
1278
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001279 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001280 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001281
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301282 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001283 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001284
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001285 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001286 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001287 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001288 if (amend && whence != FROM_COMMIT) {
1289 if (whence == FROM_MERGE)
1290 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001291 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001292 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001293 else if (whence == FROM_REBASE_PICK)
1294 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001295 }
Pat Notz89ac1222010-11-02 13:59:11 -06001296 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001297 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1298 die_for_incompatible_opt4(!!use_message, "-C",
1299 !!edit_message, "-c",
1300 !!logfile, "-F",
1301 !!fixup_message, "--fixup");
1302 die_for_incompatible_opt4(have_option_m, "-m",
1303 !!edit_message, "-c",
1304 !!use_message, "-C",
1305 !!logfile, "-F");
1306 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Junio C Hamano010c7db2012-03-30 11:30:59 -07001307 template_file = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001308 if (edit_message)
1309 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001310 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001311 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001312 if (!use_message && !is_from_cherry_pick(whence) &&
1313 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001314 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001315 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001316 use_message_buffer = read_commit_message(use_message);
1317 if (!renew_authorship) {
1318 author_message = use_message;
1319 author_message_buffer = use_message_buffer;
1320 }
1321 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001322 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1323 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001324 author_message = "CHERRY_PICK_HEAD";
1325 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001326 }
1327
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001328 if (patch_interactive)
1329 interactive = 1;
1330
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001331 die_for_incompatible_opt4(also, "-i/--include",
1332 only, "-o/--only",
1333 all, "-a/--all",
1334 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301335 if (fixup_message) {
1336 /*
1337 * We limit --fixup's suboptions to only alpha characters.
1338 * If the first character after a run of alpha is colon,
1339 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301340 * name like `amend` or `reword`, or a misspelt suboption
1341 * name. In either case, we treat it as
1342 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301343 *
1344 * Otherwise, we are dealing with --fixup=<commit>.
1345 */
1346 char *p = fixup_message;
1347 while (isalpha(*p))
1348 p++;
1349 if (p > fixup_message && *p == ':') {
1350 *p = '\0';
1351 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301352 if (!strcmp("amend", fixup_message) ||
1353 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301354 fixup_prefix = "amend";
1355 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301356 if (*fixup_message == 'r') {
1357 check_fixup_reword_options(argc, argv);
1358 only = 1;
1359 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301360 } else {
1361 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1362 }
1363 } else {
1364 fixup_commit = fixup_message;
1365 fixup_prefix = "fixup";
1366 use_editor = 0;
1367 }
1368 }
1369
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001370 if (0 <= edit_flag)
1371 use_editor = edit_flag;
1372
Denton Liuf29cd862019-04-17 11:23:25 +01001373 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001374
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001375 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001376
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001377 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001378 die(_("paths '%s ...' with -a does not make sense"),
1379 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001380
Jeff Kingf3f47a12012-10-18 21:15:50 +07001381 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001382 dry_run = 1;
1383
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001384 return argc;
1385}
1386
Jeff Kinge885a842020-09-30 08:28:18 -04001387static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001388 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001389{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001390 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001391 const char *index_file;
1392
Jeff Kinge885a842020-09-30 08:28:18 -04001393 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001394 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001395 rollback_index_files();
1396
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001397 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001398}
1399
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001400define_list_config_array_extra(color_status_slots, {"added"});
1401
Jonathan Nieder88521172014-10-07 15:16:57 -04001402static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001403{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001404 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001405 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001406
1407 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001408}
1409
Glen Chooa4e7e312023-06-28 19:26:22 +00001410static int git_status_config(const char *k, const char *v,
1411 const struct config_context *ctx, void *cb)
Junio C Hamanof766b362009-08-09 23:12:19 -07001412{
1413 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001414 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001415
Christian Couder59556542013-11-30 21:55:40 +01001416 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001417 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001418 if (!strcmp(k, "status.submodulesummary")) {
1419 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001420 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1421 &is_bool);
Junio C Hamanof766b362009-08-09 23:12:19 -07001422 if (is_bool && s->submodule_summary)
1423 s->submodule_summary = -1;
1424 return 0;
1425 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001426 if (!strcmp(k, "status.short")) {
1427 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001428 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001429 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001430 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001431 return 0;
1432 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001433 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001434 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001435 return 0;
1436 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001437 if (!strcmp(k, "status.aheadbehind")) {
1438 status_deferred_config.ahead_behind = git_config_bool(k, v);
1439 return 0;
1440 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001441 if (!strcmp(k, "status.showstash")) {
1442 s->show_stash = git_config_bool(k, v);
1443 return 0;
1444 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001445 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001446 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001447 return 0;
1448 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001449 if (!strcmp(k, "status.displaycommentprefix")) {
1450 s->display_comment_prefix = git_config_bool(k, v);
1451 return 0;
1452 }
René Scharfee3f1da92014-10-04 20:54:50 +02001453 if (skip_prefix(k, "status.color.", &slot_name) ||
1454 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001455 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001456 if (slot < 0)
1457 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001458 if (!v)
1459 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001460 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001461 }
1462 if (!strcmp(k, "status.relativepaths")) {
1463 s->relative_paths = git_config_bool(k, v);
1464 return 0;
1465 }
1466 if (!strcmp(k, "status.showuntrackedfiles")) {
1467 if (!v)
1468 return config_error_nonbool(k);
1469 else if (!strcmp(v, "no"))
1470 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1471 else if (!strcmp(v, "normal"))
1472 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1473 else if (!strcmp(v, "all"))
1474 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1475 else
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001476 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001477 return 0;
1478 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001479 if (!strcmp(k, "diff.renamelimit")) {
1480 if (s->rename_limit == -1)
Glen Choo8868b1e2023-06-28 19:26:27 +00001481 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001482 return 0;
1483 }
1484 if (!strcmp(k, "status.renamelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001485 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001486 return 0;
1487 }
1488 if (!strcmp(k, "diff.renames")) {
1489 if (s->detect_rename == -1)
1490 s->detect_rename = git_config_rename(k, v);
1491 return 0;
1492 }
1493 if (!strcmp(k, "status.renames")) {
1494 s->detect_rename = git_config_rename(k, v);
1495 return 0;
1496 }
Glen Chooa4e7e312023-06-28 19:26:22 +00001497 return git_diff_ui_config(k, v, ctx, NULL);
Junio C Hamanof766b362009-08-09 23:12:19 -07001498}
1499
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001500int cmd_status(int argc, const char **argv, const char *prefix)
1501{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001502 static int no_renames = -1;
1503 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001504 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001505 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001506 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001507 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001508 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001509 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001510 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001511 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001512 OPT_BOOL('b', "branch", &s.show_branch,
1513 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001514 OPT_BOOL(0, "show-stash", &s.show_stash,
1515 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001516 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1517 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001518 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001519 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001520 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001521 OPT_SET_INT(0, "long", &status_format,
1522 N_("show status in long format (default)"),
1523 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001524 OPT_BOOL('z', "null", &s.null_termination,
1525 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001526 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001527 N_("mode"),
1528 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001529 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001530 { OPTION_STRING, 0, "ignored", &ignored_arg,
1531 N_("mode"),
1532 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1533 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001534 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1535 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001536 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001537 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001538 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001539 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001540 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001541 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001542 OPT_END(),
1543 };
1544
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001545 if (argc == 2 && !strcmp(argv[1], "-h"))
1546 usage_with_options(builtin_status_usage, builtin_status_options);
1547
Derrick Stoleed76723e2021-07-14 13:12:37 +00001548 prepare_repo_settings(the_repository);
1549 the_repository->settings.command_requires_full_index = 0;
1550
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001551 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001552 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001553 builtin_status_options,
1554 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001555 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001556 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001557
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001558 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001559 handle_ignored_arg(&s);
1560
1561 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1562 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1563 die(_("Unsupported combination of ignored and untracked-files arguments"));
1564
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001565 parse_pathspec(&s.pathspec, 0,
1566 PATHSPEC_PREFER_FULL,
1567 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001568
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001569 if (status_format != STATUS_FORMAT_PORCELAIN &&
1570 status_format != STATUS_FORMAT_PORCELAIN_V2)
1571 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001572 repo_read_index(the_repository);
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001573 refresh_index(&the_index,
1574 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1575 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001576
Jeff King27344d62017-09-27 02:54:30 -04001577 if (use_optional_locks())
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001578 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
Jeff King27344d62017-09-27 02:54:30 -04001579 else
1580 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001581
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001582 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001583 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001584 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001585
Jens Lehmann46a958b2010-06-25 16:56:47 +02001586 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001587 s.status_format = status_format;
1588 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001589 if (no_renames != -1)
1590 s.detect_rename = !no_renames;
1591 if ((intptr_t)rename_score_arg != -1) {
1592 if (s.detect_rename < DIFF_DETECT_RENAME)
1593 s.detect_rename = DIFF_DETECT_RENAME;
1594 if (rename_score_arg)
1595 s.rename_score = parse_rename_score(&rename_score_arg);
1596 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001597
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001598 wt_status_collect(&s);
1599
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001600 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001601 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001602
Jeff King86617682009-12-07 00:26:25 -05001603 if (s.relative_paths)
1604 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001605
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001606 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001607 wt_status_collect_free_buffers(&s);
1608
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001609 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001610}
1611
Glen Chooa4e7e312023-06-28 19:26:22 +00001612static int git_commit_config(const char *k, const char *v,
1613 const struct config_context *ctx, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001614{
Junio C Hamanod249b092009-08-09 21:59:30 -07001615 struct wt_status *s = cb;
1616
Brian Hetro984c6e72008-07-05 01:24:40 -04001617 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001618 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001619 if (!strcmp(k, "commit.status")) {
1620 include_status = git_config_bool(k, v);
1621 return 0;
1622 }
Ralf Thielow51fb3a32013-01-10 18:45:59 +01001623 if (!strcmp(k, "commit.cleanup"))
1624 return git_config_string(&cleanup_arg, k, v);
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001625 if (!strcmp(k, "commit.gpgsign")) {
1626 sign_commit = git_config_bool(k, v) ? "" : NULL;
1627 return 0;
1628 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301629 if (!strcmp(k, "commit.verbose")) {
1630 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001631 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1632 &is_bool);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301633 return 0;
1634 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001635
Glen Chooa4e7e312023-06-28 19:26:22 +00001636 return git_status_config(k, v, ctx, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001637}
1638
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001639int cmd_commit(int argc, const char **argv, const char *prefix)
1640{
Jeff King036dbbf2012-05-07 15:18:26 -04001641 static struct wt_status s;
1642 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001643 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1644 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001645
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001646 OPT_GROUP(N_("Commit message options")),
1647 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1648 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1649 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1650 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1651 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1652 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301653 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301654 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1655 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301656 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301657 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 +07001658 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001659 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 -04001660 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 -07001661 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001662 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1663 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001664 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001665 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001666 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001667 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001668 /* end commit message options */
1669
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001670 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001671 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1672 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1673 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1674 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1675 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001676 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001677 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 +07001678 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001679 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001680 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001681 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1682 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001683 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001684 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001685 OPT_SET_INT(0, "long", &status_format,
1686 N_("show status in long format (default)"),
1687 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001688 OPT_BOOL('z', "null", &s.null_termination,
1689 N_("terminate entries with NUL")),
1690 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1691 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001692 { 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 +00001693 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1694 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001695 /* end commit contents options */
1696
Stefan Beller4741edd2013-08-03 13:51:18 +02001697 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1698 N_("ok to record an empty change")),
1699 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1700 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001701
1702 OPT_END()
1703 };
1704
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001705 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001706 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001707 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001708 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001709 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001710 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001711 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001712 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001713 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001714 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001715
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001716 if (argc == 2 && !strcmp(argv[1], "-h"))
1717 usage_with_options(builtin_commit_usage, builtin_commit_options);
1718
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001719 prepare_repo_settings(the_repository);
1720 the_repository->settings.command_requires_full_index = 0;
1721
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001722 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301723 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301724 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001725 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001726
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001727 if (repo_get_oid(the_repository, "HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001728 current_head = NULL;
1729 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001730 current_head = lookup_commit_or_die(&oid, "HEAD");
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001731 if (repo_parse_commit(the_repository, current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001732 die(_("could not parse HEAD commit"));
1733 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301734 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001735 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1736 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001737 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301738 if (verbose == -1)
1739 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1740
Jeff Kingc9bfb952011-08-17 22:05:35 -07001741 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001742 return dry_run_commit(argv, prefix, current_head, &s);
1743 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001744
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001745 /* Set up everything for writing the commit object. This includes
1746 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001747 if (!prepare_to_commit(index_file, prefix,
1748 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001749 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001750 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001751 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001752 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001753
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001754 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001755 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001756 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001757 if (!reflog_msg)
1758 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001759 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001760 if (!reflog_msg)
1761 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001762 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001763 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001764 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001765 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001766 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001767 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001768
Christian Couder643cb5f2010-06-12 18:05:12 +02001769 if (!reflog_msg)
1770 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001771 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001772 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001773 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001774 struct commit *parent;
1775
1776 parent = get_merge_parent(m.buf);
1777 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001778 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001779 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001780 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001781 fclose(fp);
1782 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001783 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1784 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001785 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001786 if (!strcmp(sb.buf, "no-ff"))
1787 allow_fast_forward = 0;
1788 }
1789 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001790 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001791 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001792 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001793 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001794 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001795 : is_from_rebase(whence)
1796 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001797 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001798 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001799 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001800
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001801 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001802 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301803 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001804 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001805 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001806 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001807 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001808
Denton Liuf29cd862019-04-17 11:23:25 +01001809 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301810
Phillip Woodd0aaa462017-11-10 11:09:42 +00001811 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001812 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001813 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001814 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001815 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001816 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301817 rollback_index_files();
1818 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1819 exit(1);
1820 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001821
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301822 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1823 !allow_empty_message) {
1824 struct strbuf body = STRBUF_INIT;
1825 size_t len = commit_subject_length(sb.buf);
1826 strbuf_addstr(&body, sb.buf + len);
1827 if (message_is_empty(&body, cleanup_mode)) {
1828 rollback_index_files();
1829 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1830 exit(1);
1831 }
1832 strbuf_release(&body);
1833 }
1834
Junio C Hamano0074d182011-12-20 13:20:56 -08001835 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001836 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001837 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001838 } else {
1839 struct commit_extra_header **tail = &extra;
1840 append_merge_tag_headers(parents, &tail);
1841 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001842
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +01001843 if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001844 parents, &oid, author_ident.buf, NULL,
1845 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001846 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001847 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001848 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001849 free_commit_extra_headers(extra);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001850
Phillip Wood0505d602017-11-17 11:34:47 +00001851 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1852 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001853 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001854 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001855 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001856
Junio C Hamanof496b062019-07-09 15:25:44 -07001857 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001858 unlink(git_path_merge_head(the_repository));
1859 unlink(git_path_merge_msg(the_repository));
1860 unlink(git_path_merge_mode(the_repository));
1861 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001862
Brandon Casey5a9dd392008-01-23 11:21:22 -06001863 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001864 die(_("repository has been updated, but unable to write\n"
1865 "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 +07001866 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001867
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001868 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001869
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001870 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001871 run_auto_maintenance(quiet);
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001872 run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1873 NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001874 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001875 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001876 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001877 if (!quiet) {
1878 unsigned int flags = 0;
1879
1880 if (!current_head)
1881 flags |= SUMMARY_INITIAL_COMMIT;
1882 if (author_date_is_interesting())
1883 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001884 print_commit_summary(the_repository, prefix,
1885 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001886 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001887
Denton Liua03b5552020-04-07 10:28:07 -04001888 apply_autostash(git_path_merge_autostash(the_repository));
1889
Junio C Hamano00d8c312022-05-12 15:51:07 -07001890cleanup:
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001891 strbuf_release(&author_ident);
1892 strbuf_release(&err);
1893 strbuf_release(&sb);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001894 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001895}