blob: a1c1d16a099f635fc5797811d7e737122af7bcb5 [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
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00008#include "builtin.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +00009#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020011#include "lockfile.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050012#include "cache-tree.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010013#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080014#include "dir.h"
Elijah Newren4e120822023-04-11 00:41:57 -070015#include "editor.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +000016#include "environment.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050017#include "diff.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050018#include "commit.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000019#include "gettext.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050020#include "revision.h"
21#include "wt-status.h"
22#include "run-command.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050023#include "strbuf.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070024#include "object-name.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050025#include "parse-options.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000026#include "path.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000027#include "preload-index.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000028#include "read-cache.h"
Patrick Steinhardt246deea2024-09-12 13:29:24 +020029#include "repository.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010030#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020031#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080032#include "unpack-trees.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070033#include "column.h"
Miklos Vajna5ed75e22012-09-14 08:52:03 +020034#include "sequencer.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000035#include "sparse-index.h"
Antoine Pelisseea167942013-08-23 15:48:31 +020036#include "mailmap.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020037#include "help.h"
Derrick Stolee64043552018-07-20 16:33:04 +000038#include "commit-reach.h"
Derrick Stolee859fdc02018-08-29 05:49:04 -070039#include "commit-graph.h"
Ævar Arnfjörð Bjarmason88c7b4c2022-02-16 09:14:02 +010040#include "pretty.h"
John Passaro4a861872024-05-05 18:49:09 +000041#include "trailer.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050042
43static const char * const builtin_commit_usage[] = {
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020044 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
Tomas Nordin1c473dd2024-07-22 22:53:02 +000045 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>]\n"
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020046 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
47 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
48 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
49 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
50 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
51 " [--] [<pathspec>...]"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050052 NULL
53};
54
Shawn Bohrer2f02b252007-12-02 23:02:09 -060055static const char * const builtin_status_usage[] = {
Ævar Arnfjörð Bjarmason463ea0c2022-10-13 17:39:21 +020056 N_("git status [<options>] [--] [<pathspec>...]"),
Shawn Bohrer2f02b252007-12-02 23:02:09 -060057 NULL
58};
59
Jeff Kingf197ed22010-06-06 20:41:46 -040060static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000061N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040062"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000063"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040064
Jay Soffian37f7a852011-02-19 23:12:29 -050065static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070066N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050067"If you wish to commit it anyway, use:\n"
68"\n"
69" git commit --allow-empty\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040070"\n");
71
Phillip Wood430b75f2019-12-06 16:06:12 +000072static const char empty_rebase_pick_advice[] =
73N_("Otherwise, please use 'git rebase --skip'\n");
74
Jeff Kingc17592a2013-07-26 19:39:28 -040075static const char empty_cherry_pick_advice_single[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053076N_("Otherwise, please use 'git cherry-pick --skip'\n");
Jeff Kingc17592a2013-07-26 19:39:28 -040077
78static const char empty_cherry_pick_advice_multi[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053079N_("and then use:\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050080"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053081" git cherry-pick --continue\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040082"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053083"to resume cherry-picking the remaining commits.\n"
84"If you wish to skip this commit, use:\n"
85"\n"
86" git cherry-pick --skip\n"
87"\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050088
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020089static const char *color_status_slots[] = {
90 [WT_STATUS_HEADER] = "header",
91 [WT_STATUS_UPDATED] = "updated",
92 [WT_STATUS_CHANGED] = "changed",
93 [WT_STATUS_UNTRACKED] = "untracked",
94 [WT_STATUS_NOBRANCH] = "noBranch",
95 [WT_STATUS_UNMERGED] = "unmerged",
96 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
97 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
98 [WT_STATUS_ONBRANCH] = "branch",
99};
100
Jay Soffian37f7a852011-02-19 23:12:29 -0500101static const char *use_message_buffer;
Junio C Hamano28886052007-11-18 01:52:55 -0800102static struct lock_file index_lock; /* real index */
103static struct lock_file false_lock; /* used only for partial commits */
104static enum {
105 COMMIT_AS_IS = 1,
106 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000107 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -0800108} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500109
Patrick Steinhardt14da2622024-06-11 11:19:22 +0200110static const char *force_author;
111static char *logfile;
Patrick Steinhardt6073b3b2024-05-27 13:46:15 +0200112static char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -0500113/*
114 * The _message variables are commit names from which to take
115 * the commit message and/or authorship.
116 */
117static const char *author_message, *author_message_buffer;
Patrick Steinhardtb5670042024-06-07 08:37:39 +0200118static const char *edit_message, *use_message;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530119static char *fixup_message, *fixup_commit, *squash_message;
120static const char *fixup_prefix;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800121static int all, also, interactive, patch_interactive, only, amend, signoff;
122static int edit_flag = -1; /* unspecified */
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200123static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Pranit Bauvaaaab8422016-05-05 15:20:02 +0530124static int config_commit_verbose = -1; /* unspecified */
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000125static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
Patrick Steinhardtb5670042024-06-07 08:37:39 +0200126static const char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
127static const char *sign_commit, *pathspec_from_file;
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000128static struct strvec trailer_args = STRVEC_INIT;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700129
Alex Riesen5f065732007-12-22 19:46:24 +0100130/*
131 * The default commit message cleanup mode will remove the lines
132 * beginning with # (shell comments) and leading and trailing
133 * whitespaces (empty lines or containing only whitespaces)
134 * if editor is used, and only the whitespaces if the message
135 * is specified explicitly.
136 */
Phillip Woodd0aaa462017-11-10 11:09:42 +0000137static enum commit_msg_cleanup_mode cleanup_mode;
Patrick Steinhardt1b261c22024-05-27 13:46:39 +0200138static char *cleanup_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500139
Jay Soffian37f7a852011-02-19 23:12:29 -0500140static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700141static int use_editor = 1, include_status = 1;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400142static int have_option_m;
Jeff King2c477892011-12-18 00:03:22 -0500143static struct strbuf message = STRBUF_INIT;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000144
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400145static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
Junio C Hamano84b42022013-06-24 11:41:40 -0700146
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400147static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
148{
149 enum wt_status_format *value = (enum wt_status_format *)opt->value;
150 if (unset)
151 *value = STATUS_FORMAT_NONE;
152 else if (!arg)
153 *value = STATUS_FORMAT_PORCELAIN;
154 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
155 *value = STATUS_FORMAT_PORCELAIN;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400156 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
157 *value = STATUS_FORMAT_PORCELAIN_V2;
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400158 else
159 die("unsupported porcelain version '%s'", arg);
160
161 return 0;
162}
Jeff King7c9f7032009-09-05 04:59:56 -0400163
Johannes Schindelinf9568532007-11-11 17:36:39 +0000164static int opt_parse_m(const struct option *opt, const char *arg, int unset)
165{
166 struct strbuf *buf = opt->value;
René Scharfe25206772013-05-25 23:43:34 +0200167 if (unset) {
168 have_option_m = 0;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000169 strbuf_setlen(buf, 0);
René Scharfe25206772013-05-25 23:43:34 +0200170 } else {
171 have_option_m = 1;
Brandon Caseya24a41e2013-02-18 20:17:06 -0800172 if (buf->len)
173 strbuf_addch(buf, '\n');
Johannes Schindelinf9568532007-11-11 17:36:39 +0000174 strbuf_addstr(buf, arg);
Brandon Caseya24a41e2013-02-18 20:17:06 -0800175 strbuf_complete_line(buf);
Johannes Schindelinf9568532007-11-11 17:36:39 +0000176 }
177 return 0;
178}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500179
Ben Pearte8b2dc22018-05-11 15:38:58 +0000180static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
181{
182 const char **value = opt->value;
Jeff King517fe802018-11-05 01:45:42 -0500183
184 BUG_ON_OPT_NEG(unset);
185
Ben Pearte8b2dc22018-05-11 15:38:58 +0000186 if (arg != NULL && *arg == '=')
187 arg = arg + 1;
188
189 *value = arg;
190 return 0;
191}
192
Jay Soffian37f7a852011-02-19 23:12:29 -0500193static void determine_whence(struct wt_status *s)
194{
Stefan Beller102de882018-05-17 15:51:51 -0700195 if (file_exists(git_path_merge_head(the_repository)))
Jay Soffian37f7a852011-02-19 23:12:29 -0500196 whence = FROM_MERGE;
Phillip Wood901ba7b2019-12-06 16:06:11 +0000197 else if (!sequencer_determine_whence(the_repository, &whence))
Jay Soffian37f7a852011-02-19 23:12:29 -0500198 whence = FROM_COMMIT;
199 if (s)
200 s->whence = whence;
201}
202
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200203static void status_init_config(struct wt_status *s, config_fn_t fn)
204{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100205 wt_status_prepare(the_repository, s);
Eckhard S. Maaßdc6b1d92018-05-04 13:12:15 +0200206 init_diff_ui_defaults();
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200207 git_config(fn, s);
208 determine_whence(s);
Ben Boeckeled9bff02021-08-23 12:44:00 +0200209 s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200210}
211
Junio C Hamano28886052007-11-18 01:52:55 -0800212static void rollback_index_files(void)
213{
214 switch (commit_style) {
215 case COMMIT_AS_IS:
216 break; /* nothing to do */
217 case COMMIT_NORMAL:
218 rollback_lock_file(&index_lock);
219 break;
220 case COMMIT_PARTIAL:
221 rollback_lock_file(&index_lock);
222 rollback_lock_file(&false_lock);
223 break;
224 }
225}
226
Brandon Casey5a9dd392008-01-23 11:21:22 -0600227static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800228{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600229 int err = 0;
230
Junio C Hamano28886052007-11-18 01:52:55 -0800231 switch (commit_style) {
232 case COMMIT_AS_IS:
233 break; /* nothing to do */
234 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600235 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800236 break;
237 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600238 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800239 rollback_lock_file(&false_lock);
240 break;
241 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600242
243 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800244}
245
246/*
247 * Take a union of paths in the index and the named tree (typically, "HEAD"),
248 * and return the paths that match the given pattern in list.
249 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100250static int list_paths(struct string_list *list, const char *with_tree,
Jeff Kingc5c33502019-03-20 04:15:48 -0400251 const struct pathspec *pattern)
Junio C Hamano28886052007-11-18 01:52:55 -0800252{
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700253 int i, ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800254 char *m;
255
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700256 if (!pattern->nr)
Junio C Hamanob9a08012012-07-15 21:39:48 -0700257 return 0;
258
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700259 m = xcalloc(1, pattern->nr);
Junio C Hamano28886052007-11-18 01:52:55 -0800260
Clemens Buchacher8894d532011-07-30 19:13:47 +0200261 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200262 char *max_prefix = common_prefix(pattern);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200263 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200264 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200265 }
Junio C Hamano28886052007-11-18 01:52:55 -0800266
Derrick Stoleecb8388d2021-04-01 01:49:45 +0000267 /* TODO: audit for interaction with sparse-index. */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200268 ensure_full_index(the_repository->index);
269 for (i = 0; i < the_repository->index->cache_nr; i++) {
270 const struct cache_entry *ce = the_repository->index->cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700271 struct string_list_item *item;
272
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800273 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800274 continue;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200275 if (!ce_path_match(the_repository->index, ce, pattern, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800276 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100277 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700278 if (ce_skip_worktree(ce))
279 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800280 }
281
Jeff Kingc5c33502019-03-20 04:15:48 -0400282 ret = report_path_error(m, pattern);
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700283 free(m);
284 return ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800285}
286
Johannes Schindelinc455c872008-07-21 19:03:49 +0100287static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800288{
289 int i;
290 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700291 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100292 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700293
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700294 /* p->util is skip-worktree */
295 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700296 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700297
Johannes Schindelinc455c872008-07-21 19:03:49 +0100298 if (!lstat(p->string, &st)) {
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200299 if (add_to_index(the_repository->index, p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000300 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200301 } else
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200302 remove_file_from_index(the_repository->index, p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800303 }
304}
305
Junio C Hamano06bb6432011-08-19 11:58:18 -0700306static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800307{
308 struct tree *tree;
309 struct unpack_trees_options opts;
310 struct tree_desc t;
311
Junio C Hamano06bb6432011-08-19 11:58:18 -0700312 if (!current_head) {
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200313 discard_index(the_repository->index);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800314 return;
315 }
316
317 memset(&opts, 0, sizeof(opts));
318 opts.head_idx = 1;
319 opts.index_only = 1;
320 opts.merge = 1;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200321 opts.src_index = the_repository->index;
322 opts.dst_index = the_repository->index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800323
324 opts.fn = oneway_merge;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000325 tree = parse_tree_indirect(&current_head->object.oid);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800326 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000327 die(_("failed to unpack HEAD tree object"));
Johannes Schindelinaa9f6182024-02-23 08:34:23 +0000328 if (parse_tree(tree) < 0)
329 exit(128);
Eric W. Biedermanefed6872023-10-01 21:40:28 -0500330 init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500331 if (unpack_trees(1, &t, &opts))
332 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800333}
334
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100335static void refresh_cache_or_die(int refresh_flags)
336{
337 /*
338 * refresh_flags contains REFRESH_QUIET, so the only errors
339 * are for unmerged entries.
340 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200341 if (refresh_index(the_repository->index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100342 die_resolve_conflict("commit");
343}
344
Jeff Kinge885a842020-09-30 08:28:18 -0400345static const char *prepare_index(const char **argv, const char *prefix,
Michael Haggerty35ff08b2014-10-01 12:28:17 +0200346 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500347{
Martin Ågrendd1055e2017-09-23 01:34:49 +0200348 struct string_list partial = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700349 struct pathspec pathspec;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700350 int refresh_flags = REFRESH_QUIET;
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200351 const char *ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500352
Junio C Hamano50b7e702009-08-04 23:49:33 -0700353 if (is_status)
354 refresh_flags |= REFRESH_UNMERGED;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700355 parse_pathspec(&pathspec, 0,
356 PATHSPEC_PREFER_FULL,
357 prefix, argv);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500358
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000359 if (pathspec_from_file) {
360 if (interactive)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000361 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000362
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000363 if (all)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000364 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000365
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000366 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000367 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000368
369 parse_pathspec_file(&pathspec, 0,
370 PATHSPEC_PREFER_FULL,
371 prefix, pathspec_from_file, pathspec_file_nul);
372 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000373 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000374 }
375
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530376 if (!pathspec.nr && (also || (only && !allow_empty &&
377 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000378 die(_("No paths with --include/--only does not make sense."));
379
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100380 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000381 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800382
Conrad Irwin1020d082011-05-06 22:59:59 -0700383 if (interactive) {
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000384 char *old_index_env = NULL, *old_repo_index_file;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100385 repo_hold_locked_index(the_repository, &index_lock,
386 LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700387
388 refresh_cache_or_die(refresh_flags);
389
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200390 if (write_locked_index(the_repository->index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700391 die(_("unable to create temporary index"));
392
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000393 old_repo_index_file = the_repository->index_file;
394 the_repository->index_file =
395 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500396 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000397 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700398
Jeff Kinge885a842020-09-30 08:28:18 -0400399 if (interactive_add(argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700400 die(_("interactive add failed"));
401
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000402 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700403 if (old_index_env && *old_index_env)
404 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
405 else
406 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500407 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700408
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200409 discard_index(the_repository->index);
410 read_index_from(the_repository->index, get_lock_file_path(&index_lock),
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200411 repo_get_git_dir(the_repository));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200412 if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700413 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700414 die(_("unable to write index file"));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200415 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700416 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700417 } else
418 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700419
420 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200421 ret = get_lock_file_path(&index_lock);
422 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700423 }
424
Junio C Hamano28886052007-11-18 01:52:55 -0800425 /*
426 * Non partial, non as-is commit.
427 *
428 * (1) get the real index;
429 * (2) update the_index as necessary;
430 * (3) write the_index out to the real index (still locked);
431 * (4) return the name of the locked index file.
432 *
433 * The caller should run hooks on the locked real index, and
434 * (A) if all goes well, commit the real index;
435 * (B) on failure, rollback the real index.
436 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700437 if (all || (also && pathspec.nr)) {
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530438 char *ps_matched = xcalloc(pathspec.nr, 1);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100439 repo_hold_locked_index(the_repository, &index_lock,
440 LOCK_DIE_ON_ERROR);
Elijah Newren50c37ee2023-05-16 06:33:46 +0000441 add_files_to_cache(the_repository, also ? prefix : NULL,
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530442 &pathspec, ps_matched, 0, 0);
443 if (!all && report_path_error(ps_matched, &pathspec))
444 exit(128);
445
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100446 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200447 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
448 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700449 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800450 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200451 ret = get_lock_file_path(&index_lock);
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530452 free(ps_matched);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200453 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500454 }
455
Junio C Hamano28886052007-11-18 01:52:55 -0800456 /*
457 * As-is commit.
458 *
459 * (1) return the name of the real index file.
460 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200461 * The caller should run hooks on the real index,
462 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800463 * We still need to refresh the index here.
464 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700465 if (!only && !pathspec.nr) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100466 repo_hold_locked_index(the_repository, &index_lock,
467 LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100468 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200469 if (the_repository->index->cache_changed
470 || !cache_tree_fully_valid(the_repository->index->cache_tree))
471 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
472 if (write_locked_index(the_repository->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100473 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700474 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800475 commit_style = COMMIT_AS_IS;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200476 ret = get_index_file();
477 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500478 }
479
Junio C Hamano28886052007-11-18 01:52:55 -0800480 /*
481 * A partial commit.
482 *
483 * (0) find the set of affected paths;
484 * (1) get lock on the real index file;
485 * (2) update the_index with the given paths;
486 * (3) write the_index out to the real index (still locked);
487 * (4) get lock on the false index file;
488 * (5) reset the_index from HEAD;
489 * (6) update the_index the same way as (2);
490 * (7) write the_index out to the false index file;
491 * (8) return the name of the false index file (still locked);
492 *
493 * The caller should run hooks on the locked false index, and
494 * create commit from it. Then
495 * (A) if all goes well, commit the real index;
496 * (B) on failure, rollback the real index;
497 * In either case, rollback the false index.
498 */
499 commit_style = COMMIT_PARTIAL;
500
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000501 if (whence != FROM_COMMIT) {
502 if (whence == FROM_MERGE)
503 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000504 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000505 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000506 else if (is_from_rebase(whence))
507 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000508 }
Junio C Hamano28886052007-11-18 01:52:55 -0800509
Jeff Kingc5c33502019-03-20 04:15:48 -0400510 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800511 exit(1);
512
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200513 discard_index(the_repository->index);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100514 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000515 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800516
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100517 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800518 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200519 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
520 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
521 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700522 die(_("unable to write new index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500523
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700524 hold_lock_file_for_update(&false_lock,
525 git_path("next-index-%"PRIuMAX,
526 (uintmax_t) getpid()),
527 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800528
Junio C Hamano06bb6432011-08-19 11:58:18 -0700529 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800530 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200531 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500532
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200533 if (write_locked_index(the_repository->index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000534 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500535
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200536 discard_index(the_repository->index);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200537 ret = get_lock_file_path(&false_lock);
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200538 read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
Martin Ågrendd1055e2017-09-23 01:34:49 +0200539out:
540 string_list_clear(&partial, 0);
541 clear_pathspec(&pathspec);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200542 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500543}
544
Junio C Hamanod249b092009-08-09 21:59:30 -0700545static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
546 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500547{
brian m. carlson8066df42017-02-20 00:10:14 +0000548 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700549
Junio C Hamanod249b092009-08-09 21:59:30 -0700550 if (s->relative_paths)
551 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500552
553 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700554 s->amend = 1;
555 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500556 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700557 s->verbose = verbose;
558 s->index_file = index_file;
559 s->fp = fp;
560 s->nowarn = nowarn;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200561 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400562 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000563 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400564 s->status_format = status_format;
565 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500566
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700567 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400568 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700569 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500570
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700571 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500572}
573
Junio C Hamano06bb6432011-08-19 11:58:18 -0700574static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100575{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700576 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100577}
578
Jeff Kingfac90832014-12-10 10:42:10 -0500579static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
580{
Jeff Kingc83a5092014-12-10 10:43:42 -0500581 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200582 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500583}
584
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700585static void export_one(const char *var, const char *s, const char *e, int hack)
586{
587 struct strbuf buf = STRBUF_INIT;
588 if (hack)
589 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100590 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700591 setenv(var, buf.buf, 1);
592 strbuf_release(&buf);
593}
594
Jeff Kingc33ddc22014-08-27 03:57:08 -0400595static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400596{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400597 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400598
Jeff Kingc33ddc22014-08-27 03:57:08 -0400599 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400600 int errors = 0;
601 unsigned long t = approxidate_careful(in, &errors);
602 if (errors)
603 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400604 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400605 }
606
607 return 0;
608}
609
Jeff Kingf4ef5172014-08-27 03:57:56 -0400610static void set_ident_var(char **buf, char *val)
611{
612 free(*buf);
613 *buf = val;
614}
615
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800616static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200617{
618 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700619 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200620
Jeff Kingeaa541e2015-01-12 20:58:33 -0500621 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
622 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
623 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200624
Jay Soffian37f7a852011-02-19 23:12:29 -0500625 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400626 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800627 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400628 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200629
Jeff Kingf0f96622014-08-27 03:57:28 -0400630 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200631 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400632 die(_("commit '%s' lacks author header"), author_message);
633 if (split_ident_line(&ident, a, len) < 0)
634 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200635
Jeff Kingf4ef5172014-08-27 03:57:56 -0400636 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
637 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200638
Jeff Kingf0f96622014-08-27 03:57:28 -0400639 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400640 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400641 strbuf_addch(&date_buf, '@');
642 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
643 strbuf_addch(&date_buf, ' ');
644 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400645 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400646 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200647 }
648
649 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400650 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200651
Jeff Kingf0f96622014-08-27 03:57:28 -0400652 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000653 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400654 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
655 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200656 }
657
Jeff King14ac2862014-05-01 21:12:42 -0400658 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400659 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400660 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400661 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400662 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400663 }
664
William Hubbs39ab4d02019-02-04 12:48:50 -0600665 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
666 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500667 assert_split_ident(&author, author_ident);
668 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
669 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
670 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400671 free(name);
672 free(email);
673 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200674}
675
Jeff Kingb7242b82014-05-01 21:10:01 -0400676static int author_date_is_interesting(void)
677{
678 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800679}
680
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700681static void adjust_comment_line_char(const struct strbuf *sb)
682{
683 char candidates[] = "#;@!$%^&|:";
684 char *candidate;
685 const char *p;
686
Jeff King1751e582024-03-12 05:17:19 -0400687 if (!memchr(sb->buf, candidates[0], sb->len)) {
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200688 free(comment_line_str_to_free);
689 comment_line_str = comment_line_str_to_free =
690 xstrfmt("%c", candidates[0]);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700691 return;
Jeff King1751e582024-03-12 05:17:19 -0400692 }
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700693
694 p = sb->buf;
695 candidate = strchr(candidates, *p);
696 if (candidate)
697 *candidate = ' ';
698 for (p = sb->buf; *p; p++) {
699 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
700 candidate = strchr(candidates, p[1]);
701 if (candidate)
702 *candidate = ' ';
703 }
704 }
705
706 for (p = candidates; *p == ' '; p++)
707 ;
708 if (!*p)
709 die(_("unable to select a comment character that is not used\n"
710 "in the current commit message"));
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200711 free(comment_line_str_to_free);
712 comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700713}
714
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530715static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
716 struct pretty_print_context *ctx)
717{
718 const char *buffer, *subject, *fmt;
719
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200720 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530721 find_commit_subject(buffer, &subject);
722 /*
723 * If we amend the 'amend!' commit then we don't want to
724 * duplicate the subject line.
725 */
726 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200727 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200728 repo_unuse_commit_buffer(the_repository, commit, buffer);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530729}
730
Junio C Hamanod249b092009-08-09 21:59:30 -0700731static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700732 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800733 struct wt_status *s,
734 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500735{
736 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800737 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700738 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500739 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100740 const char *hook_arg1 = NULL;
741 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000742 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200743 int old_display_comment_prefix;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100744 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500745
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700746 /* This checks and barfs if author is badly specified */
747 determine_author_info(author_ident);
748
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100749 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
750 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100751 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500752
Pat Notz89ac1222010-11-02 13:59:11 -0600753 if (squash_message) {
754 /*
755 * Insert the proper subject line before other commit
756 * message options add their content.
757 */
758 if (use_message && !strcmp(use_message, squash_message))
759 strbuf_addstr(&sb, "squash! ");
760 else {
761 struct pretty_print_context ctx = {0};
762 struct commit *c;
763 c = lookup_commit_reference_by_name(squash_message);
764 if (!c)
Teng Longe4cf0132023-05-29 21:27:56 +0800765 die(_("could not lookup commit '%s'"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600766 ctx.output_encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200767 repo_format_commit_message(the_repository, c,
768 "squash! %s\n\n", &sb,
769 &ctx);
Pat Notz89ac1222010-11-02 13:59:11 -0600770 }
771 }
772
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000773 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000774 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100775 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500776 } else if (logfile && !strcmp(logfile, "-")) {
777 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000778 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500779 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000780 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100781 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500782 } else if (logfile) {
783 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000784 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200785 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100786 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500787 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800788 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500789 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400790 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200791 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100792 hook_arg1 = "commit";
793 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600794 } else if (fixup_message) {
795 struct pretty_print_context ctx = {0};
796 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530797 char *fmt;
798 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600799 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +0800800 die(_("could not lookup commit '%s'"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600801 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530802 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200803 repo_format_commit_message(the_repository, commit, fmt, &sb,
804 &ctx);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530805 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600806 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530807
808 /*
809 * Only `-m` commit message option is checked here, as
810 * it supports `--fixup` to append the commit message.
811 *
812 * The other commit message options `-c`/`-C`/`-F` are
813 * incompatible with all the forms of `--fixup` and
814 * have already errored out while parsing the `git commit`
815 * options.
816 */
817 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
818 strbuf_addbuf(&sb, &message);
819
820 if (!strcmp(fixup_prefix, "amend")) {
821 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000822 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530823 prepare_amend_commit(commit, &sb, &ctx);
824 }
Stefan Beller102de882018-05-17 15:51:51 -0700825 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100826 size_t merge_msg_start;
827
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100828 /*
829 * prepend SQUASH_MSG here if it exists and a
830 * "merge --squash" was originally performed
831 */
Stefan Beller102de882018-05-17 15:51:51 -0700832 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
833 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100834 die_errno(_("could not read SQUASH_MSG"));
835 hook_arg1 = "squash";
836 } else
837 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100838
839 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700840 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000841 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100842
843 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
844 wt_status_locate_end(sb.buf + merge_msg_start,
845 sb.len - merge_msg_start) <
846 sb.len - merge_msg_start)
Josh Triplette90cc072024-02-27 01:17:36 -0800847 s->added_cut_line = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700848 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
849 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000850 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100851 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600852 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500853 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000854 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100855 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200856 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500857 }
858
Paolo Bonzini8089c852008-02-05 08:04:18 +0100859 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500860 * The remaining cases don't modify the template message, but
861 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100862 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500863 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100864 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000865 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500866 hook_arg1 = "commit";
867 hook_arg2 = "CHERRY_PICK_HEAD";
868 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100869
Pat Notz89ac1222010-11-02 13:59:11 -0600870 if (squash_message) {
871 /*
872 * If squash_commit was used for the commit subject,
873 * then we're possibly hijacking other commit log options.
874 * Reset the hook args to tell the real story.
875 */
876 hook_arg1 = "message";
877 hook_arg2 = "";
878 }
879
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530880 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700881 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530882 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500883
Matthieu Moy2556b992013-09-06 19:43:07 +0200884 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
885 old_display_comment_prefix = s->display_comment_prefix;
886 s->display_comment_prefix = 1;
887
Matthieu Moyea9882b2013-09-12 12:50:06 +0200888 /*
889 * Most hints are counter-productive when the commit has
890 * already started.
891 */
892 s->hints = 0;
893
Boris Faure8b1ae672011-05-08 12:31:02 +0200894 if (clean_message_contents)
Jeff King2982b652024-03-12 05:17:27 -0400895 strbuf_stripspace(&sb, NULL);
Johannes Schindelin13208572007-11-11 17:35:58 +0000896
Junio C Hamano073bd752014-10-28 12:44:09 -0700897 if (signoff)
Linus Arver7cb26a12023-10-20 19:01:33 +0000898 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000899
Jonathan Nieder37f30122011-02-25 23:10:49 -0600900 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000901 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500902
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700903 if (auto_comment_line_char)
904 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500905 strbuf_release(&sb);
906
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200907 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400908 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500909 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800910 int ident_shown = 0;
911 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400912 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800913 const char *hint_cleanup_all = allow_empty_message ?
914 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400915 " Lines starting\nwith '%s' will be ignored.\n") :
Hu Jialun6f70f002021-07-10 02:07:32 +0800916 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400917 " Lines starting\nwith '%s' will be ignored, and an empty"
Hu Jialun6f70f002021-07-10 02:07:32 +0800918 " message aborts the commit.\n");
919 const char *hint_cleanup_space = allow_empty_message ?
920 _("Please enter the commit message for your changes."
921 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400922 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800923 " yourself if you want to.\n") :
924 _("Please enter the commit message for your changes."
925 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400926 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800927 " yourself if you want to.\n"
928 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700929 if (whence != FROM_COMMIT) {
Josh Triplette90cc072024-02-27 01:17:36 -0800930 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
Josh Triplett688a0a72024-02-27 01:16:09 -0800931 wt_status_add_cut_line(s);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000932 status_printf_ln(
933 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400934 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000935 _("\n"
936 "It looks like you may be committing a merge.\n"
937 "If this is not correct, please run\n"
938 " git update-ref -d MERGE_HEAD\n"
939 "and try again.\n") :
940 _("\n"
941 "It looks like you may be committing a cherry-pick.\n"
942 "If this is not correct, please run\n"
943 " git update-ref -d CHERRY_PICK_HEAD\n"
944 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700945 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100946
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600947 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000948 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Jeff Kingf99e1d92024-03-12 05:17:34 -0400949 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
Denton Liu10559972019-04-17 11:23:28 +0100950 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
Josh Triplette90cc072024-02-27 01:17:36 -0800951 if (whence == FROM_COMMIT)
Josh Triplett688a0a72024-02-27 01:16:09 -0800952 wt_status_add_cut_line(s);
Denton Liu10559972019-04-17 11:23:28 +0100953 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Jeff Kingf99e1d92024-03-12 05:17:34 -0400954 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100955
Jeff Kingfac90832014-12-10 10:42:10 -0500956 /*
957 * These should never fail because they come from our own
958 * fmt_ident. They may fail the sane_ident test, but we know
959 * that the name and mail pointers will at least be valid,
960 * which is enough for our tests and printing here.
961 */
962 assert_split_ident(&ai, author_ident);
963 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400964
965 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600966 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000967 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400968 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600969 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400970 (int)(ai.name_end - ai.name_begin), ai.name_begin,
971 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200972
Jeff Kingb7242b82014-05-01 21:10:01 -0400973 if (author_date_is_interesting())
974 status_printf_ln(s, GIT_COLOR_NORMAL,
975 _("%s"
976 "Date: %s"),
977 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400978 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100979
Jeff Kingd6991ce2012-11-14 16:34:13 -0800980 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600981 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000982 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400983 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600984 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400985 (int)(ci.name_end - ci.name_begin), ci.name_begin,
986 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200987
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +0530988 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200989
Junio C Hamanod249b092009-08-09 21:59:30 -0700990 saved_color_setting = s->use_color;
991 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700992 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -0700993 s->use_color = saved_color_setting;
Elijah Newren3e73cc62018-09-27 10:36:57 -0700994 string_list_clear(&s->change, 1);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100995 } else {
brian m. carlson8066df42017-02-20 00:10:14 +0000996 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -0800997 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +0100998
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200999 if (!the_repository->index->initialized && repo_read_index(the_repository) < 0)
Johannes Schindelin2ee045e2023-06-29 13:23:10 +00001000 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +01001001
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001002 if (amend)
1003 parent = "HEAD^1";
1004
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001005 if (repo_get_oid(the_repository, parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001006 int i, ita_nr = 0;
1007
Derrick Stoleecb8388d2021-04-01 01:49:45 +00001008 /* TODO: audit for interaction with sparse-index. */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001009 ensure_full_index(the_repository->index);
1010 for (i = 0; i < the_repository->index->cache_nr; i++)
1011 if (ce_intent_to_add(the_repository->index->cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001012 ita_nr++;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001013 committable = the_repository->index->cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001014 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001015 /*
1016 * Unless the user did explicitly request a submodule
1017 * ignore mode by passing a command line option we do
1018 * not ignore any changed submodule SHA-1s when
1019 * comparing index and parent, no matter what is
1020 * configured. Otherwise we won't commit any
1021 * submodules which were manually staged, which would
1022 * be really confusing.
1023 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001024 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001025 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001026 if (ignore_submodule_arg &&
1027 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001028 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001029 committable = index_differs_from(the_repository,
1030 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001031 }
Alex Riesen71686242007-11-28 22:13:08 +01001032 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001033 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001034
Jonathan Nieder37f30122011-02-25 23:10:49 -06001035 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001036
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001037 if (trailer_args.nr) {
John Passaro4a861872024-05-05 18:49:09 +00001038 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001039 die(_("unable to pass trailers to --trailers"));
1040 strvec_clear(&trailer_args);
1041 }
1042
Jay Soffian37f7a852011-02-19 23:12:29 -05001043 /*
1044 * Reject an attempt to record a non-merge empty commit without
1045 * explicit --allow-empty. In the cherry-pick case, it may be
1046 * empty due to conflict resolution, which the user should okay.
1047 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001048 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001049 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001050 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001051 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001052 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001053 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001054 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001055 else if (is_from_cherry_pick(whence) ||
1056 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001057 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001058 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001059 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001060 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001061 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001062 else
1063 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001064 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001065 return 0;
1066 }
1067
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001068 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001069 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001070 * Re-read the index as the pre-commit-commit hook was invoked
1071 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001072 * the editor and after we invoke run_status above.
1073 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001074 discard_index(the_repository->index);
Kevin Willford680ee552017-08-14 15:54:25 -06001075 }
Patrick Steinhardt246deea2024-09-12 13:29:24 +02001076 read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
Kevin Willford680ee552017-08-14 15:54:25 -06001077
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001078 if (cache_tree_update(the_repository->index, 0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001079 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001080 return 0;
1081 }
1082
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001083 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301084 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001085 return 0;
1086
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001087 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001088 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001089
Jeff King22f9b7f2020-07-28 16:24:27 -04001090 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001091 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001092 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001093 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001094 exit(1);
1095 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001096 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001097 }
1098
1099 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001100 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1101 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001102 return 0;
1103 }
1104
1105 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001106}
1107
Junio C Hamano146ea062008-08-26 23:13:13 -07001108static const char *find_author_by_nickname(const char *name)
1109{
1110 struct rev_info revs;
1111 struct commit *commit;
1112 struct strbuf buf = STRBUF_INIT;
1113 const char *av[20];
1114 int ac = 0;
1115
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001116 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001117 strbuf_addf(&buf, "--author=%s", name);
1118 av[++ac] = "--all";
1119 av[++ac] = "-i";
1120 av[++ac] = buf.buf;
1121 av[++ac] = NULL;
1122 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001123 revs.mailmap = xmalloc(sizeof(struct string_list));
1124 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001125 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001126
Stefan Beller81c3ce32014-08-10 23:33:26 +02001127 if (prepare_revision_walk(&revs))
1128 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001129 commit = get_revision(&revs);
1130 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001131 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001132 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001133 strbuf_release(&buf);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +02001134 repo_format_commit_message(the_repository, commit,
1135 "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001136 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001137 return strbuf_detach(&buf, NULL);
1138 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001139 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001140}
1141
Jameson Millereec0f7f2017-10-30 13:21:37 -04001142static void handle_ignored_arg(struct wt_status *s)
1143{
1144 if (!ignored_arg)
1145 ; /* default already initialized */
1146 else if (!strcmp(ignored_arg, "traditional"))
1147 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1148 else if (!strcmp(ignored_arg, "no"))
1149 s->show_ignored_mode = SHOW_NO_IGNORED;
1150 else if (!strcmp(ignored_arg, "matching"))
1151 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1152 else
1153 die(_("Invalid ignored mode '%s'"), ignored_arg);
1154}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001155
Junio C Hamano63acdc42024-03-13 10:32:13 -07001156static enum untracked_status_type parse_untracked_setting_name(const char *u)
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001157{
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001158 /*
1159 * Please update $__git_untracked_file_modes in
1160 * git-completion.bash when you add new options
1161 */
Junio C Hamanof66e1a02024-03-13 10:32:14 -07001162 switch (git_parse_maybe_bool(u)) {
1163 case 0:
1164 u = "no";
1165 break;
1166 case 1:
1167 u = "normal";
1168 break;
1169 default:
1170 break;
1171 }
1172
Junio C Hamano63acdc42024-03-13 10:32:13 -07001173 if (!strcmp(u, "no"))
1174 return SHOW_NO_UNTRACKED_FILES;
1175 else if (!strcmp(u, "normal"))
1176 return SHOW_NORMAL_UNTRACKED_FILES;
1177 else if (!strcmp(u, "all"))
1178 return SHOW_ALL_UNTRACKED_FILES;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001179 else
Junio C Hamano63acdc42024-03-13 10:32:13 -07001180 return SHOW_UNTRACKED_FILES_ERROR;
1181}
1182
1183static void handle_untracked_files_arg(struct wt_status *s)
1184{
1185 enum untracked_status_type u;
1186
1187 if (!untracked_files_arg)
1188 return; /* default already initialized */
1189
1190 u = parse_untracked_setting_name(untracked_files_arg);
1191 if (u == SHOW_UNTRACKED_FILES_ERROR)
1192 die(_("Invalid untracked files mode '%s'"),
1193 untracked_files_arg);
1194 s->show_untracked_files = u;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001195}
1196
Jay Soffian37f7a852011-02-19 23:12:29 -05001197static const char *read_commit_message(const char *name)
1198{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001199 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001200 struct commit *commit;
1201
1202 commit = lookup_commit_reference_by_name(name);
1203 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +08001204 die(_("could not lookup commit '%s'"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001205 out_enc = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001206 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001207}
1208
Junio C Hamano84b42022013-06-24 11:41:40 -07001209/*
1210 * Enumerate what needs to be propagated when --porcelain
1211 * is not in effect here.
1212 */
1213static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001214 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001215 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001216 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001217} status_deferred_config = {
1218 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001219 -1, /* unspecified */
1220 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001221};
1222
1223static void finalize_deferred_config(struct wt_status *s)
1224{
1225 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001226 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001227 !s->null_termination);
1228
1229 if (s->null_termination) {
1230 if (status_format == STATUS_FORMAT_NONE ||
1231 status_format == STATUS_FORMAT_UNSPECIFIED)
1232 status_format = STATUS_FORMAT_PORCELAIN;
1233 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001234 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001235 }
1236
1237 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1238 status_format = status_deferred_config.status_format;
1239 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1240 status_format = STATUS_FORMAT_NONE;
1241
1242 if (use_deferred_config && s->show_branch < 0)
1243 s->show_branch = status_deferred_config.show_branch;
1244 if (s->show_branch < 0)
1245 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001246
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001247 /*
1248 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001249 * line argument *AND* we will print in a human-readable format
1250 * (short, long etc.) then we inherit from the status.aheadbehind
1251 * config setting. In all other cases (and porcelain V[12] formats
1252 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001253 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001254 if (use_deferred_config &&
1255 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001256 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1257
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001258 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1259 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001260}
1261
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301262static void check_fixup_reword_options(int argc, const char *argv[]) {
1263 if (whence != FROM_COMMIT) {
1264 if (whence == FROM_MERGE)
1265 die(_("You are in the middle of a merge -- cannot reword."));
1266 else if (is_from_cherry_pick(whence))
1267 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1268 }
1269 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001270 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301271 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001272 die(_("reword option of '%s' and '%s' cannot be used together"),
1273 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301274}
1275
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001276static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001277 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001278 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001279 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001280 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001281 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001282{
Jeff King036dbbf2012-05-07 15:18:26 -04001283 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001284 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001285
Junio C Hamano146ea062008-08-26 23:13:13 -07001286 if (force_author && !strchr(force_author, '>'))
1287 force_author = find_author_by_nickname(force_author);
1288
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001289 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001290 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001291
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301292 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001293 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001294
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001295 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001296 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001297 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001298 if (amend && whence != FROM_COMMIT) {
1299 if (whence == FROM_MERGE)
1300 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001301 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001302 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001303 else if (whence == FROM_REBASE_PICK)
1304 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001305 }
Pat Notz89ac1222010-11-02 13:59:11 -06001306 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001307 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1308 die_for_incompatible_opt4(!!use_message, "-C",
1309 !!edit_message, "-c",
1310 !!logfile, "-F",
1311 !!fixup_message, "--fixup");
1312 die_for_incompatible_opt4(have_option_m, "-m",
1313 !!edit_message, "-c",
1314 !!use_message, "-C",
1315 !!logfile, "-F");
1316 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001317 FREE_AND_NULL(template_file);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001318 if (edit_message)
1319 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001320 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001321 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001322 if (!use_message && !is_from_cherry_pick(whence) &&
1323 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001324 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001325 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001326 use_message_buffer = read_commit_message(use_message);
1327 if (!renew_authorship) {
1328 author_message = use_message;
1329 author_message_buffer = use_message_buffer;
1330 }
1331 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001332 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1333 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001334 author_message = "CHERRY_PICK_HEAD";
1335 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001336 }
1337
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001338 if (patch_interactive)
1339 interactive = 1;
1340
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001341 die_for_incompatible_opt4(also, "-i/--include",
1342 only, "-o/--only",
1343 all, "-a/--all",
1344 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301345 if (fixup_message) {
1346 /*
1347 * We limit --fixup's suboptions to only alpha characters.
1348 * If the first character after a run of alpha is colon,
1349 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301350 * name like `amend` or `reword`, or a misspelt suboption
1351 * name. In either case, we treat it as
1352 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301353 *
1354 * Otherwise, we are dealing with --fixup=<commit>.
1355 */
1356 char *p = fixup_message;
1357 while (isalpha(*p))
1358 p++;
1359 if (p > fixup_message && *p == ':') {
1360 *p = '\0';
1361 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301362 if (!strcmp("amend", fixup_message) ||
1363 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301364 fixup_prefix = "amend";
1365 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301366 if (*fixup_message == 'r') {
1367 check_fixup_reword_options(argc, argv);
1368 only = 1;
1369 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301370 } else {
1371 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1372 }
1373 } else {
1374 fixup_commit = fixup_message;
1375 fixup_prefix = "fixup";
1376 use_editor = 0;
1377 }
1378 }
1379
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001380 if (0 <= edit_flag)
1381 use_editor = edit_flag;
1382
Denton Liuf29cd862019-04-17 11:23:25 +01001383 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001384
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001385 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001386
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001387 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001388 die(_("paths '%s ...' with -a does not make sense"),
1389 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001390
Jeff Kingf3f47a12012-10-18 21:15:50 +07001391 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001392 dry_run = 1;
1393
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001394 return argc;
1395}
1396
Jeff Kinge885a842020-09-30 08:28:18 -04001397static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001398 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001399{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001400 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001401 const char *index_file;
1402
Jeff Kinge885a842020-09-30 08:28:18 -04001403 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001404 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001405 rollback_index_files();
1406
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001407 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001408}
1409
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001410define_list_config_array_extra(color_status_slots, {"added"});
1411
Jonathan Nieder88521172014-10-07 15:16:57 -04001412static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001413{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001414 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001415 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001416
1417 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001418}
1419
Glen Chooa4e7e312023-06-28 19:26:22 +00001420static int git_status_config(const char *k, const char *v,
1421 const struct config_context *ctx, void *cb)
Junio C Hamanof766b362009-08-09 23:12:19 -07001422{
1423 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001424 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001425
Christian Couder59556542013-11-30 21:55:40 +01001426 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001427 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001428 if (!strcmp(k, "status.submodulesummary")) {
1429 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001430 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1431 &is_bool);
Junio C Hamanof766b362009-08-09 23:12:19 -07001432 if (is_bool && s->submodule_summary)
1433 s->submodule_summary = -1;
1434 return 0;
1435 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001436 if (!strcmp(k, "status.short")) {
1437 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001438 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001439 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001440 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001441 return 0;
1442 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001443 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001444 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001445 return 0;
1446 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001447 if (!strcmp(k, "status.aheadbehind")) {
1448 status_deferred_config.ahead_behind = git_config_bool(k, v);
1449 return 0;
1450 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001451 if (!strcmp(k, "status.showstash")) {
1452 s->show_stash = git_config_bool(k, v);
1453 return 0;
1454 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001455 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001456 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001457 return 0;
1458 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001459 if (!strcmp(k, "status.displaycommentprefix")) {
1460 s->display_comment_prefix = git_config_bool(k, v);
1461 return 0;
1462 }
René Scharfee3f1da92014-10-04 20:54:50 +02001463 if (skip_prefix(k, "status.color.", &slot_name) ||
1464 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001465 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001466 if (slot < 0)
1467 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001468 if (!v)
1469 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001470 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001471 }
1472 if (!strcmp(k, "status.relativepaths")) {
1473 s->relative_paths = git_config_bool(k, v);
1474 return 0;
1475 }
1476 if (!strcmp(k, "status.showuntrackedfiles")) {
Junio C Hamano63acdc42024-03-13 10:32:13 -07001477 enum untracked_status_type u;
1478
Junio C Hamano63acdc42024-03-13 10:32:13 -07001479 u = parse_untracked_setting_name(v);
1480 if (u == SHOW_UNTRACKED_FILES_ERROR)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001481 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamano63acdc42024-03-13 10:32:13 -07001482 s->show_untracked_files = u;
Junio C Hamanof766b362009-08-09 23:12:19 -07001483 return 0;
1484 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001485 if (!strcmp(k, "diff.renamelimit")) {
1486 if (s->rename_limit == -1)
Glen Choo8868b1e2023-06-28 19:26:27 +00001487 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001488 return 0;
1489 }
1490 if (!strcmp(k, "status.renamelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001491 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001492 return 0;
1493 }
1494 if (!strcmp(k, "diff.renames")) {
1495 if (s->detect_rename == -1)
1496 s->detect_rename = git_config_rename(k, v);
1497 return 0;
1498 }
1499 if (!strcmp(k, "status.renames")) {
1500 s->detect_rename = git_config_rename(k, v);
1501 return 0;
1502 }
Glen Chooa4e7e312023-06-28 19:26:22 +00001503 return git_diff_ui_config(k, v, ctx, NULL);
Junio C Hamanof766b362009-08-09 23:12:19 -07001504}
1505
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001506int cmd_status(int argc, const char **argv, const char *prefix)
1507{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001508 static int no_renames = -1;
1509 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001510 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001511 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001512 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001513 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001514 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001515 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001516 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001517 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001518 OPT_BOOL('b', "branch", &s.show_branch,
1519 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001520 OPT_BOOL(0, "show-stash", &s.show_stash,
1521 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001522 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1523 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001524 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001525 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001526 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001527 OPT_SET_INT(0, "long", &status_format,
1528 N_("show status in long format (default)"),
1529 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001530 OPT_BOOL('z', "null", &s.null_termination,
1531 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001532 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001533 N_("mode"),
1534 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001535 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001536 { OPTION_STRING, 0, "ignored", &ignored_arg,
1537 N_("mode"),
1538 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1539 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001540 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1541 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001542 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001543 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001544 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001545 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001546 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001547 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001548 OPT_END(),
1549 };
1550
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001551 if (argc == 2 && !strcmp(argv[1], "-h"))
1552 usage_with_options(builtin_status_usage, builtin_status_options);
1553
Derrick Stoleed76723e2021-07-14 13:12:37 +00001554 prepare_repo_settings(the_repository);
1555 the_repository->settings.command_requires_full_index = 0;
1556
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001557 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001558 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001559 builtin_status_options,
1560 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001561 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001562 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001563
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001564 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001565 handle_ignored_arg(&s);
1566
1567 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1568 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1569 die(_("Unsupported combination of ignored and untracked-files arguments"));
1570
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001571 parse_pathspec(&s.pathspec, 0,
1572 PATHSPEC_PREFER_FULL,
1573 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001574
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001575 if (status_format != STATUS_FORMAT_PORCELAIN &&
1576 status_format != STATUS_FORMAT_PORCELAIN_V2)
1577 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001578 repo_read_index(the_repository);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001579 refresh_index(the_repository->index,
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001580 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1581 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001582
Jeff King27344d62017-09-27 02:54:30 -04001583 if (use_optional_locks())
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001584 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
Jeff King27344d62017-09-27 02:54:30 -04001585 else
1586 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001587
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001588 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001589 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001590 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001591
Jens Lehmann46a958b2010-06-25 16:56:47 +02001592 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001593 s.status_format = status_format;
1594 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001595 if (no_renames != -1)
1596 s.detect_rename = !no_renames;
1597 if ((intptr_t)rename_score_arg != -1) {
1598 if (s.detect_rename < DIFF_DETECT_RENAME)
1599 s.detect_rename = DIFF_DETECT_RENAME;
1600 if (rename_score_arg)
1601 s.rename_score = parse_rename_score(&rename_score_arg);
1602 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001603
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001604 wt_status_collect(&s);
1605
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001606 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001607 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001608
Jeff King86617682009-12-07 00:26:25 -05001609 if (s.relative_paths)
1610 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001611
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001612 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001613 wt_status_collect_free_buffers(&s);
1614
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001615 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001616}
1617
Glen Chooa4e7e312023-06-28 19:26:22 +00001618static int git_commit_config(const char *k, const char *v,
1619 const struct config_context *ctx, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001620{
Junio C Hamanod249b092009-08-09 21:59:30 -07001621 struct wt_status *s = cb;
1622
Brian Hetro984c6e72008-07-05 01:24:40 -04001623 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001624 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001625 if (!strcmp(k, "commit.status")) {
1626 include_status = git_config_bool(k, v);
1627 return 0;
1628 }
Ralf Thielow51fb3a32013-01-10 18:45:59 +01001629 if (!strcmp(k, "commit.cleanup"))
1630 return git_config_string(&cleanup_arg, k, v);
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001631 if (!strcmp(k, "commit.gpgsign")) {
1632 sign_commit = git_config_bool(k, v) ? "" : NULL;
1633 return 0;
1634 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301635 if (!strcmp(k, "commit.verbose")) {
1636 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001637 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1638 &is_bool);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301639 return 0;
1640 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001641
Glen Chooa4e7e312023-06-28 19:26:22 +00001642 return git_status_config(k, v, ctx, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001643}
1644
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001645int cmd_commit(int argc, const char **argv, const char *prefix)
1646{
Jeff King036dbbf2012-05-07 15:18:26 -04001647 static struct wt_status s;
1648 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001649 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1650 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001651
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001652 OPT_GROUP(N_("Commit message options")),
1653 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1654 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1655 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1656 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1657 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1658 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301659 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301660 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1661 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301662 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301663 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 +07001664 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001665 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
John Passaro56740f92024-05-05 18:49:08 +00001666 OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG),
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -07001667 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001668 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1669 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001670 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001671 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001672 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001673 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001674 /* end commit message options */
1675
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001676 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001677 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1678 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1679 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1680 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1681 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001682 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001683 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 +07001684 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001685 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001686 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001687 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1688 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001689 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001690 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001691 OPT_SET_INT(0, "long", &status_format,
1692 N_("show status in long format (default)"),
1693 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001694 OPT_BOOL('z', "null", &s.null_termination,
1695 N_("terminate entries with NUL")),
1696 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1697 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 +07001698 { 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 +00001699 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1700 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001701 /* end commit contents options */
1702
Stefan Beller4741edd2013-08-03 13:51:18 +02001703 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1704 N_("ok to record an empty change")),
1705 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1706 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001707
1708 OPT_END()
1709 };
1710
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001711 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001712 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001713 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001714 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001715 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001716 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001717 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001718 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001719 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001720 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001721
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001722 if (argc == 2 && !strcmp(argv[1], "-h"))
1723 usage_with_options(builtin_commit_usage, builtin_commit_options);
1724
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001725 prepare_repo_settings(the_repository);
1726 the_repository->settings.command_requires_full_index = 0;
1727
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001728 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301729 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301730 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001731 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001732
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001733 if (repo_get_oid(the_repository, "HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001734 current_head = NULL;
1735 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001736 current_head = lookup_commit_or_die(&oid, "HEAD");
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001737 if (repo_parse_commit(the_repository, current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001738 die(_("could not parse HEAD commit"));
1739 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301740 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001741 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1742 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001743 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301744 if (verbose == -1)
1745 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1746
Jeff Kingc9bfb952011-08-17 22:05:35 -07001747 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001748 return dry_run_commit(argv, prefix, current_head, &s);
1749 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001750
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001751 /* Set up everything for writing the commit object. This includes
1752 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001753 if (!prepare_to_commit(index_file, prefix,
1754 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001755 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001756 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001757 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001758 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001759
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001760 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001761 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001762 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001763 if (!reflog_msg)
1764 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001765 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001766 if (!reflog_msg)
1767 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001768 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001769 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001770 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001771 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001772 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001773 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001774
Christian Couder643cb5f2010-06-12 18:05:12 +02001775 if (!reflog_msg)
1776 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001777 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001778 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001779 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001780 struct commit *parent;
1781
1782 parent = get_merge_parent(m.buf);
1783 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001784 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001785 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001786 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001787 fclose(fp);
1788 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001789 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1790 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001791 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001792 if (!strcmp(sb.buf, "no-ff"))
1793 allow_fast_forward = 0;
1794 }
1795 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001796 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001797 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001798 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001799 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001800 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001801 : is_from_rebase(whence)
1802 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001803 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001804 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001805 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001806
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001807 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001808 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301809 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001810 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001811 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001812 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001813 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001814
Denton Liuf29cd862019-04-17 11:23:25 +01001815 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301816
Phillip Woodd0aaa462017-11-10 11:09:42 +00001817 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001818 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001819 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001820 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001821 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001822 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301823 rollback_index_files();
1824 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1825 exit(1);
1826 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001827
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301828 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1829 !allow_empty_message) {
1830 struct strbuf body = STRBUF_INIT;
1831 size_t len = commit_subject_length(sb.buf);
1832 strbuf_addstr(&body, sb.buf + len);
1833 if (message_is_empty(&body, cleanup_mode)) {
1834 rollback_index_files();
1835 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1836 exit(1);
1837 }
1838 strbuf_release(&body);
1839 }
1840
Junio C Hamano0074d182011-12-20 13:20:56 -08001841 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001842 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001843 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001844 } else {
1845 struct commit_extra_header **tail = &extra;
1846 append_merge_tag_headers(parents, &tail);
1847 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001848
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001849 if (commit_tree_extended(sb.buf, sb.len, &the_repository->index->cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001850 parents, &oid, author_ident.buf, NULL,
1851 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001852 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001853 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001854 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001855
Phillip Wood0505d602017-11-17 11:34:47 +00001856 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1857 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001858 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001859 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001860 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001861
Junio C Hamanof496b062019-07-09 15:25:44 -07001862 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001863 unlink(git_path_merge_head(the_repository));
1864 unlink(git_path_merge_msg(the_repository));
1865 unlink(git_path_merge_mode(the_repository));
1866 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001867
Brandon Casey5a9dd392008-01-23 11:21:22 -06001868 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001869 die(_("repository has been updated, but unable to write\n"
Junio C Hamanoa0607052023-10-17 20:08:51 -07001870 "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 +07001871 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001872
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001873 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001874
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001875 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001876 run_auto_maintenance(quiet);
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001877 run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1878 NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001879 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001880 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001881 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001882 if (!quiet) {
1883 unsigned int flags = 0;
1884
1885 if (!current_head)
1886 flags |= SUMMARY_INITIAL_COMMIT;
1887 if (author_date_is_interesting())
1888 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001889 print_commit_summary(the_repository, prefix,
1890 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001891 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001892
Patrick Steinhardt3f921c72024-01-19 11:40:19 +01001893 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
Denton Liua03b5552020-04-07 10:28:07 -04001894
Junio C Hamano00d8c312022-05-12 15:51:07 -07001895cleanup:
Patrick Steinhardt63c9bd32024-06-11 11:20:42 +02001896 free_commit_extra_headers(extra);
1897 free_commit_list(parents);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001898 strbuf_release(&author_ident);
1899 strbuf_release(&err);
1900 strbuf_release(&sb);
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001901 free(logfile);
1902 free(template_file);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001903 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001904}