blob: ef5e622c07746f86bb6d2fb2b909361bef1d1b10 [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 */
Patrick Steinhardt41f43b82024-12-06 11:27:19 +01007
John Cai03eae9a2024-09-13 21:16:15 +00008#define USE_THE_REPOSITORY_VARIABLE
Patrick Steinhardt41f43b82024-12-06 11:27:19 +01009#define DISABLE_SIGN_COMPARE_WARNINGS
10
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +000011#include "builtin.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +000012#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070013#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020014#include "lockfile.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050015#include "cache-tree.h"
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010016#include "color.h"
Junio C Hamano28886052007-11-18 01:52:55 -080017#include "dir.h"
Elijah Newren4e120822023-04-11 00:41:57 -070018#include "editor.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +000019#include "environment.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050020#include "diff.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050021#include "commit.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000022#include "gettext.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050023#include "revision.h"
24#include "wt-status.h"
25#include "run-command.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050026#include "strbuf.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070027#include "object-name.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050028#include "parse-options.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000029#include "path.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000030#include "preload-index.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000031#include "read-cache.h"
Patrick Steinhardt246deea2024-09-12 13:29:24 +020032#include "repository.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010033#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020034#include "rerere.h"
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -080035#include "unpack-trees.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070036#include "column.h"
Miklos Vajna5ed75e22012-09-14 08:52:03 +020037#include "sequencer.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000038#include "sparse-index.h"
Antoine Pelisseea167942013-08-23 15:48:31 +020039#include "mailmap.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020040#include "help.h"
Derrick Stolee64043552018-07-20 16:33:04 +000041#include "commit-reach.h"
Derrick Stolee859fdc02018-08-29 05:49:04 -070042#include "commit-graph.h"
Ævar Arnfjörð Bjarmason88c7b4c2022-02-16 09:14:02 +010043#include "pretty.h"
John Passaro4a861872024-05-05 18:49:09 +000044#include "trailer.h"
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050045
46static const char * const builtin_commit_usage[] = {
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020047 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
Tomas Nordin1c473dd2024-07-22 22:53:02 +000048 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>]\n"
Ævar Arnfjörð Bjarmason423be1f2022-10-13 17:39:23 +020049 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
50 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
51 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
52 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
53 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
54 " [--] [<pathspec>...]"),
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -050055 NULL
56};
57
Shawn Bohrer2f02b252007-12-02 23:02:09 -060058static const char * const builtin_status_usage[] = {
Ævar Arnfjörð Bjarmason463ea0c2022-10-13 17:39:21 +020059 N_("git status [<options>] [--] [<pathspec>...]"),
Shawn Bohrer2f02b252007-12-02 23:02:09 -060060 NULL
61};
62
Jeff Kingf197ed22010-06-06 20:41:46 -040063static const char empty_amend_advice[] =
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000064N_("You asked to amend the most recent commit, but doing so would make\n"
Jeff Kingf197ed22010-06-06 20:41:46 -040065"it empty. You can repeat your command with --allow-empty, or you can\n"
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +000066"remove the commit entirely with \"git reset HEAD^\".\n");
Jeff Kingf197ed22010-06-06 20:41:46 -040067
Jay Soffian37f7a852011-02-19 23:12:29 -050068static const char empty_cherry_pick_advice[] =
Junio C Hamano6c80cd22011-04-01 17:55:55 -070069N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050070"If you wish to commit it anyway, use:\n"
71"\n"
72" git commit --allow-empty\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040073"\n");
74
Phillip Wood430b75f2019-12-06 16:06:12 +000075static const char empty_rebase_pick_advice[] =
76N_("Otherwise, please use 'git rebase --skip'\n");
77
Jeff Kingc17592a2013-07-26 19:39:28 -040078static const char empty_cherry_pick_advice_single[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053079N_("Otherwise, please use 'git cherry-pick --skip'\n");
Jeff Kingc17592a2013-07-26 19:39:28 -040080
81static const char empty_cherry_pick_advice_multi[] =
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053082N_("and then use:\n"
Jay Soffian37f7a852011-02-19 23:12:29 -050083"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053084" git cherry-pick --continue\n"
Jeff Kingc17592a2013-07-26 19:39:28 -040085"\n"
Rohit Ashiwaldcb500d2019-07-02 14:41:29 +053086"to resume cherry-picking the remaining commits.\n"
87"If you wish to skip this commit, use:\n"
88"\n"
89" git cherry-pick --skip\n"
90"\n");
Jay Soffian37f7a852011-02-19 23:12:29 -050091
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020092static const char *color_status_slots[] = {
93 [WT_STATUS_HEADER] = "header",
94 [WT_STATUS_UPDATED] = "updated",
95 [WT_STATUS_CHANGED] = "changed",
96 [WT_STATUS_UNTRACKED] = "untracked",
97 [WT_STATUS_NOBRANCH] = "noBranch",
98 [WT_STATUS_UNMERGED] = "unmerged",
99 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
100 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
101 [WT_STATUS_ONBRANCH] = "branch",
102};
103
Jay Soffian37f7a852011-02-19 23:12:29 -0500104static const char *use_message_buffer;
Junio C Hamano28886052007-11-18 01:52:55 -0800105static struct lock_file index_lock; /* real index */
106static struct lock_file false_lock; /* used only for partial commits */
107static enum {
108 COMMIT_AS_IS = 1,
109 COMMIT_NORMAL,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000110 COMMIT_PARTIAL
Junio C Hamano28886052007-11-18 01:52:55 -0800111} commit_style;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500112
Patrick Steinhardt14da2622024-06-11 11:19:22 +0200113static const char *force_author;
114static char *logfile;
Patrick Steinhardt6073b3b2024-05-27 13:46:15 +0200115static char *template_file;
Jay Soffian37f7a852011-02-19 23:12:29 -0500116/*
117 * The _message variables are commit names from which to take
118 * the commit message and/or authorship.
119 */
120static const char *author_message, *author_message_buffer;
Patrick Steinhardtb5670042024-06-07 08:37:39 +0200121static const char *edit_message, *use_message;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530122static char *fixup_message, *fixup_commit, *squash_message;
123static const char *fixup_prefix;
Junio C Hamanoca1ba202011-12-06 13:09:55 -0800124static int all, also, interactive, patch_interactive, only, amend, signoff;
125static int edit_flag = -1; /* unspecified */
Erick Mattosc51f6ce2009-11-04 01:20:11 -0200126static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Pranit Bauvaaaab8422016-05-05 15:20:02 +0530127static int config_commit_verbose = -1; /* unspecified */
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000128static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
Patrick Steinhardtb5670042024-06-07 08:37:39 +0200129static const char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
130static const char *sign_commit, *pathspec_from_file;
ZheNing Hu2daae3d2021-03-23 13:55:57 +0000131static struct strvec trailer_args = STRVEC_INIT;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700132
Alex Riesen5f065732007-12-22 19:46:24 +0100133/*
134 * The default commit message cleanup mode will remove the lines
135 * beginning with # (shell comments) and leading and trailing
136 * whitespaces (empty lines or containing only whitespaces)
137 * if editor is used, and only the whitespaces if the message
138 * is specified explicitly.
139 */
Phillip Woodd0aaa462017-11-10 11:09:42 +0000140static enum commit_msg_cleanup_mode cleanup_mode;
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +0100141static char *cleanup_config;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500142
Jay Soffian37f7a852011-02-19 23:12:29 -0500143static enum commit_whence whence;
Junio C Hamano06bb6432011-08-19 11:58:18 -0700144static int use_editor = 1, include_status = 1;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400145static int have_option_m;
Jeff King2c477892011-12-18 00:03:22 -0500146static struct strbuf message = STRBUF_INIT;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000147
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400148static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
Junio C Hamano84b42022013-06-24 11:41:40 -0700149
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400150static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
151{
152 enum wt_status_format *value = (enum wt_status_format *)opt->value;
153 if (unset)
154 *value = STATUS_FORMAT_NONE;
155 else if (!arg)
156 *value = STATUS_FORMAT_PORCELAIN;
157 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
158 *value = STATUS_FORMAT_PORCELAIN;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400159 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
160 *value = STATUS_FORMAT_PORCELAIN_V2;
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -0400161 else
162 die("unsupported porcelain version '%s'", arg);
163
164 return 0;
165}
Jeff King7c9f7032009-09-05 04:59:56 -0400166
Johannes Schindelinf9568532007-11-11 17:36:39 +0000167static int opt_parse_m(const struct option *opt, const char *arg, int unset)
168{
169 struct strbuf *buf = opt->value;
René Scharfe25206772013-05-25 23:43:34 +0200170 if (unset) {
171 have_option_m = 0;
Johannes Schindelinf9568532007-11-11 17:36:39 +0000172 strbuf_setlen(buf, 0);
René Scharfe25206772013-05-25 23:43:34 +0200173 } else {
174 have_option_m = 1;
Brandon Caseya24a41e2013-02-18 20:17:06 -0800175 if (buf->len)
176 strbuf_addch(buf, '\n');
Johannes Schindelinf9568532007-11-11 17:36:39 +0000177 strbuf_addstr(buf, arg);
Brandon Caseya24a41e2013-02-18 20:17:06 -0800178 strbuf_complete_line(buf);
Johannes Schindelinf9568532007-11-11 17:36:39 +0000179 }
180 return 0;
181}
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500182
Ben Pearte8b2dc22018-05-11 15:38:58 +0000183static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
184{
185 const char **value = opt->value;
Jeff King517fe802018-11-05 01:45:42 -0500186
187 BUG_ON_OPT_NEG(unset);
188
Ben Pearte8b2dc22018-05-11 15:38:58 +0000189 if (arg != NULL && *arg == '=')
190 arg = arg + 1;
191
192 *value = arg;
193 return 0;
194}
195
Jay Soffian37f7a852011-02-19 23:12:29 -0500196static void determine_whence(struct wt_status *s)
197{
Stefan Beller102de882018-05-17 15:51:51 -0700198 if (file_exists(git_path_merge_head(the_repository)))
Jay Soffian37f7a852011-02-19 23:12:29 -0500199 whence = FROM_MERGE;
Phillip Wood901ba7b2019-12-06 16:06:11 +0000200 else if (!sequencer_determine_whence(the_repository, &whence))
Jay Soffian37f7a852011-02-19 23:12:29 -0500201 whence = FROM_COMMIT;
202 if (s)
203 s->whence = whence;
204}
205
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200206static void status_init_config(struct wt_status *s, config_fn_t fn)
207{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100208 wt_status_prepare(the_repository, s);
Eckhard S. Maaßdc6b1d92018-05-04 13:12:15 +0200209 init_diff_ui_defaults();
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200210 git_config(fn, s);
211 determine_whence(s);
Ben Boeckeled9bff02021-08-23 12:44:00 +0200212 s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */
Matthieu Moy5c25dfa2013-09-12 12:50:04 +0200213}
214
Junio C Hamano28886052007-11-18 01:52:55 -0800215static void rollback_index_files(void)
216{
217 switch (commit_style) {
218 case COMMIT_AS_IS:
219 break; /* nothing to do */
220 case COMMIT_NORMAL:
221 rollback_lock_file(&index_lock);
222 break;
223 case COMMIT_PARTIAL:
224 rollback_lock_file(&index_lock);
225 rollback_lock_file(&false_lock);
226 break;
227 }
228}
229
Brandon Casey5a9dd392008-01-23 11:21:22 -0600230static int commit_index_files(void)
Junio C Hamano28886052007-11-18 01:52:55 -0800231{
Brandon Casey5a9dd392008-01-23 11:21:22 -0600232 int err = 0;
233
Junio C Hamano28886052007-11-18 01:52:55 -0800234 switch (commit_style) {
235 case COMMIT_AS_IS:
236 break; /* nothing to do */
237 case COMMIT_NORMAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600238 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800239 break;
240 case COMMIT_PARTIAL:
Brandon Casey5a9dd392008-01-23 11:21:22 -0600241 err = commit_lock_file(&index_lock);
Junio C Hamano28886052007-11-18 01:52:55 -0800242 rollback_lock_file(&false_lock);
243 break;
244 }
Brandon Casey5a9dd392008-01-23 11:21:22 -0600245
246 return err;
Junio C Hamano28886052007-11-18 01:52:55 -0800247}
248
249/*
250 * Take a union of paths in the index and the named tree (typically, "HEAD"),
251 * and return the paths that match the given pattern in list.
252 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100253static int list_paths(struct string_list *list, const char *with_tree,
Jeff Kingc5c33502019-03-20 04:15:48 -0400254 const struct pathspec *pattern)
Junio C Hamano28886052007-11-18 01:52:55 -0800255{
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700256 int i, ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800257 char *m;
258
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700259 if (!pattern->nr)
Junio C Hamanob9a08012012-07-15 21:39:48 -0700260 return 0;
261
Nguyễn Thái Ngọc Duy17ddc662013-07-14 15:35:53 +0700262 m = xcalloc(1, pattern->nr);
Junio C Hamano28886052007-11-18 01:52:55 -0800263
Clemens Buchacher8894d532011-07-30 19:13:47 +0200264 if (with_tree) {
Clemens Buchacherf950eb92011-09-04 12:42:01 +0200265 char *max_prefix = common_prefix(pattern);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200266 overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
Clemens Buchacher5879f562011-09-04 12:41:59 +0200267 free(max_prefix);
Clemens Buchacher8894d532011-07-30 19:13:47 +0200268 }
Junio C Hamano28886052007-11-18 01:52:55 -0800269
Derrick Stoleecb8388d2021-04-01 01:49:45 +0000270 /* TODO: audit for interaction with sparse-index. */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200271 ensure_full_index(the_repository->index);
272 for (i = 0; i < the_repository->index->cache_nr; i++) {
273 const struct cache_entry *ce = the_repository->index->cache[i];
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700274 struct string_list_item *item;
275
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800276 if (ce->ce_flags & CE_UPDATE)
Junio C Hamanoe87e22d2008-01-14 13:54:24 -0800277 continue;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200278 if (!ce_path_match(the_repository->index, ce, pattern, m))
Junio C Hamano28886052007-11-18 01:52:55 -0800279 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100280 item = string_list_insert(list, ce->name);
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700281 if (ce_skip_worktree(ce))
282 item->util = item; /* better a valid pointer than a fake one */
Junio C Hamano28886052007-11-18 01:52:55 -0800283 }
284
Jeff Kingc5c33502019-03-20 04:15:48 -0400285 ret = report_path_error(m, pattern);
Stefan Beller5d0b9bf2015-03-20 17:28:07 -0700286 free(m);
287 return ret;
Junio C Hamano28886052007-11-18 01:52:55 -0800288}
289
Johannes Schindelinc455c872008-07-21 19:03:49 +0100290static void add_remove_files(struct string_list *list)
Junio C Hamano28886052007-11-18 01:52:55 -0800291{
292 int i;
293 for (i = 0; i < list->nr; i++) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700294 struct stat st;
Johannes Schindelinc455c872008-07-21 19:03:49 +0100295 struct string_list_item *p = &(list->items[i]);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700296
Nguyễn Thái Ngọc Duy7fce6e32009-12-14 18:43:59 +0700297 /* p->util is skip-worktree */
298 if (p->util)
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700299 continue;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700300
Johannes Schindelinc455c872008-07-21 19:03:49 +0100301 if (!lstat(p->string, &st)) {
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200302 if (add_to_index(the_repository->index, p->string, &st, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000303 die(_("updating files failed"));
Alex Riesen960b8ad2008-05-12 19:57:45 +0200304 } else
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200305 remove_file_from_index(the_repository->index, p->string);
Junio C Hamano28886052007-11-18 01:52:55 -0800306 }
307}
308
Junio C Hamano06bb6432011-08-19 11:58:18 -0700309static void create_base_index(const struct commit *current_head)
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800310{
311 struct tree *tree;
312 struct unpack_trees_options opts;
313 struct tree_desc t;
314
Junio C Hamano06bb6432011-08-19 11:58:18 -0700315 if (!current_head) {
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200316 discard_index(the_repository->index);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800317 return;
318 }
319
320 memset(&opts, 0, sizeof(opts));
321 opts.head_idx = 1;
322 opts.index_only = 1;
323 opts.merge = 1;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200324 opts.src_index = the_repository->index;
325 opts.dst_index = the_repository->index;
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800326
327 opts.fn = oneway_merge;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000328 tree = parse_tree_indirect(&current_head->object.oid);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800329 if (!tree)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000330 die(_("failed to unpack HEAD tree object"));
Johannes Schindelinaa9f6182024-02-23 08:34:23 +0000331 if (parse_tree(tree) < 0)
332 exit(128);
Eric W. Biedermanefed6872023-10-01 21:40:28 -0500333 init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500334 if (unpack_trees(1, &t, &opts))
335 exit(128); /* We've already reported the error, finish dying */
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800336}
337
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100338static void refresh_cache_or_die(int refresh_flags)
339{
340 /*
341 * refresh_flags contains REFRESH_QUIET, so the only errors
342 * are for unmerged entries.
343 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200344 if (refresh_index(the_repository->index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL))
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100345 die_resolve_conflict("commit");
346}
347
Jeff Kinge885a842020-09-30 08:28:18 -0400348static const char *prepare_index(const char **argv, const char *prefix,
Michael Haggerty35ff08b2014-10-01 12:28:17 +0200349 const struct commit *current_head, int is_status)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500350{
Martin Ågrendd1055e2017-09-23 01:34:49 +0200351 struct string_list partial = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700352 struct pathspec pathspec;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700353 int refresh_flags = REFRESH_QUIET;
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200354 const char *ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500355
Junio C Hamano50b7e702009-08-04 23:49:33 -0700356 if (is_status)
357 refresh_flags |= REFRESH_UNMERGED;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700358 parse_pathspec(&pathspec, 0,
359 PATHSPEC_PREFER_FULL,
360 prefix, argv);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500361
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000362 if (pathspec_from_file) {
363 if (interactive)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000364 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000365
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000366 if (all)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000367 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000368
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000369 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000370 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000371
372 parse_pathspec_file(&pathspec, 0,
373 PATHSPEC_PREFER_FULL,
374 prefix, pathspec_from_file, pathspec_file_nul);
375 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000376 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000377 }
378
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530379 if (!pathspec.nr && (also || (only && !allow_empty &&
380 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000381 die(_("No paths with --include/--only does not make sense."));
382
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100383 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000384 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800385
Conrad Irwin1020d082011-05-06 22:59:59 -0700386 if (interactive) {
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000387 char *old_index_env = NULL, *old_repo_index_file;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100388 repo_hold_locked_index(the_repository, &index_lock,
389 LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700390
391 refresh_cache_or_die(refresh_flags);
392
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200393 if (write_locked_index(the_repository->index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700394 die(_("unable to create temporary index"));
395
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000396 old_repo_index_file = the_repository->index_file;
397 the_repository->index_file =
398 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500399 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000400 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700401
John Cai83647452024-09-13 21:16:17 +0000402 if (interactive_add(the_repository, argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700403 die(_("interactive add failed"));
404
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000405 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700406 if (old_index_env && *old_index_env)
407 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
408 else
409 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500410 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700411
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200412 discard_index(the_repository->index);
413 read_index_from(the_repository->index, get_lock_file_path(&index_lock),
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200414 repo_get_git_dir(the_repository));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200415 if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700416 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700417 die(_("unable to write index file"));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200418 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700419 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700420 } else
421 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700422
423 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200424 ret = get_lock_file_path(&index_lock);
425 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700426 }
427
Junio C Hamano28886052007-11-18 01:52:55 -0800428 /*
429 * Non partial, non as-is commit.
430 *
431 * (1) get the real index;
432 * (2) update the_index as necessary;
433 * (3) write the_index out to the real index (still locked);
434 * (4) return the name of the locked index file.
435 *
436 * The caller should run hooks on the locked real index, and
437 * (A) if all goes well, commit the real index;
438 * (B) on failure, rollback the real index.
439 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700440 if (all || (also && pathspec.nr)) {
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530441 char *ps_matched = xcalloc(pathspec.nr, 1);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100442 repo_hold_locked_index(the_repository, &index_lock,
443 LOCK_DIE_ON_ERROR);
Elijah Newren50c37ee2023-05-16 06:33:46 +0000444 add_files_to_cache(the_repository, also ? prefix : NULL,
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530445 &pathspec, ps_matched, 0, 0);
446 if (!all && report_path_error(ps_matched, &pathspec))
447 exit(128);
448
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100449 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200450 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
451 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700452 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800453 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200454 ret = get_lock_file_path(&index_lock);
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530455 free(ps_matched);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200456 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500457 }
458
Junio C Hamano28886052007-11-18 01:52:55 -0800459 /*
460 * As-is commit.
461 *
462 * (1) return the name of the real index file.
463 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200464 * The caller should run hooks on the real index,
465 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800466 * We still need to refresh the index here.
467 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700468 if (!only && !pathspec.nr) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100469 repo_hold_locked_index(the_repository, &index_lock,
470 LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100471 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200472 if (the_repository->index->cache_changed
473 || !cache_tree_fully_valid(the_repository->index->cache_tree))
474 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
475 if (write_locked_index(the_repository->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100476 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700477 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800478 commit_style = COMMIT_AS_IS;
Patrick Steinhardt1dc4ec22024-09-12 13:29:32 +0200479 ret = repo_get_index_file(the_repository);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200480 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500481 }
482
Junio C Hamano28886052007-11-18 01:52:55 -0800483 /*
484 * A partial commit.
485 *
486 * (0) find the set of affected paths;
487 * (1) get lock on the real index file;
488 * (2) update the_index with the given paths;
489 * (3) write the_index out to the real index (still locked);
490 * (4) get lock on the false index file;
491 * (5) reset the_index from HEAD;
492 * (6) update the_index the same way as (2);
493 * (7) write the_index out to the false index file;
494 * (8) return the name of the false index file (still locked);
495 *
496 * The caller should run hooks on the locked false index, and
497 * create commit from it. Then
498 * (A) if all goes well, commit the real index;
499 * (B) on failure, rollback the real index;
500 * In either case, rollback the false index.
501 */
502 commit_style = COMMIT_PARTIAL;
503
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000504 if (whence != FROM_COMMIT) {
505 if (whence == FROM_MERGE)
506 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000507 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000508 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000509 else if (is_from_rebase(whence))
510 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000511 }
Junio C Hamano28886052007-11-18 01:52:55 -0800512
Jeff Kingc5c33502019-03-20 04:15:48 -0400513 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800514 exit(1);
515
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200516 discard_index(the_repository->index);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100517 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000518 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800519
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100520 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800521 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200522 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
523 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
524 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700525 die(_("unable to write new index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500526
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700527 hold_lock_file_for_update(&false_lock,
528 git_path("next-index-%"PRIuMAX,
529 (uintmax_t) getpid()),
530 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800531
Junio C Hamano06bb6432011-08-19 11:58:18 -0700532 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800533 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200534 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500535
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200536 if (write_locked_index(the_repository->index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000537 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500538
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200539 discard_index(the_repository->index);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200540 ret = get_lock_file_path(&false_lock);
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200541 read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
Martin Ågrendd1055e2017-09-23 01:34:49 +0200542out:
543 string_list_clear(&partial, 0);
544 clear_pathspec(&pathspec);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200545 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500546}
547
Junio C Hamanod249b092009-08-09 21:59:30 -0700548static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
549 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500550{
brian m. carlson8066df42017-02-20 00:10:14 +0000551 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700552
Junio C Hamanod249b092009-08-09 21:59:30 -0700553 if (s->relative_paths)
554 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500555
556 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700557 s->amend = 1;
558 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500559 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700560 s->verbose = verbose;
561 s->index_file = index_file;
562 s->fp = fp;
563 s->nowarn = nowarn;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200564 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400565 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000566 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400567 s->status_format = status_format;
568 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500569
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700570 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400571 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700572 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500573
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700574 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500575}
576
Junio C Hamano06bb6432011-08-19 11:58:18 -0700577static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100578{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700579 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100580}
581
Jeff Kingfac90832014-12-10 10:42:10 -0500582static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
583{
Jeff Kingc83a5092014-12-10 10:43:42 -0500584 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200585 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500586}
587
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700588static void export_one(const char *var, const char *s, const char *e, int hack)
589{
590 struct strbuf buf = STRBUF_INIT;
591 if (hack)
592 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100593 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700594 setenv(var, buf.buf, 1);
595 strbuf_release(&buf);
596}
597
Jeff Kingc33ddc22014-08-27 03:57:08 -0400598static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400599{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400600 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400601
Jeff Kingc33ddc22014-08-27 03:57:08 -0400602 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400603 int errors = 0;
604 unsigned long t = approxidate_careful(in, &errors);
605 if (errors)
606 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400607 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400608 }
609
610 return 0;
611}
612
Jeff Kingf4ef5172014-08-27 03:57:56 -0400613static void set_ident_var(char **buf, char *val)
614{
615 free(*buf);
616 *buf = val;
617}
618
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800619static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200620{
621 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700622 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200623
Jeff Kingeaa541e2015-01-12 20:58:33 -0500624 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
625 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
626 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200627
Jay Soffian37f7a852011-02-19 23:12:29 -0500628 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400629 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800630 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400631 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200632
Jeff Kingf0f96622014-08-27 03:57:28 -0400633 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200634 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400635 die(_("commit '%s' lacks author header"), author_message);
636 if (split_ident_line(&ident, a, len) < 0)
637 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200638
Jeff Kingf4ef5172014-08-27 03:57:56 -0400639 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
640 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200641
Jeff Kingf0f96622014-08-27 03:57:28 -0400642 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400643 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400644 strbuf_addch(&date_buf, '@');
645 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
646 strbuf_addch(&date_buf, ' ');
647 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400648 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400649 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200650 }
651
652 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400653 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200654
Jeff Kingf0f96622014-08-27 03:57:28 -0400655 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000656 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400657 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
658 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200659 }
660
Jeff King14ac2862014-05-01 21:12:42 -0400661 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400662 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400663 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400664 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400665 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400666 }
667
William Hubbs39ab4d02019-02-04 12:48:50 -0600668 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
669 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500670 assert_split_ident(&author, author_ident);
671 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
672 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
673 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400674 free(name);
675 free(email);
676 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200677}
678
Jeff Kingb7242b82014-05-01 21:10:01 -0400679static int author_date_is_interesting(void)
680{
681 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800682}
683
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700684static void adjust_comment_line_char(const struct strbuf *sb)
685{
686 char candidates[] = "#;@!$%^&|:";
687 char *candidate;
688 const char *p;
689
Jeff King1751e582024-03-12 05:17:19 -0400690 if (!memchr(sb->buf, candidates[0], sb->len)) {
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200691 free(comment_line_str_to_free);
692 comment_line_str = comment_line_str_to_free =
693 xstrfmt("%c", candidates[0]);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700694 return;
Jeff King1751e582024-03-12 05:17:19 -0400695 }
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700696
697 p = sb->buf;
698 candidate = strchr(candidates, *p);
699 if (candidate)
700 *candidate = ' ';
701 for (p = sb->buf; *p; p++) {
702 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
703 candidate = strchr(candidates, p[1]);
704 if (candidate)
705 *candidate = ' ';
706 }
707 }
708
709 for (p = candidates; *p == ' '; p++)
710 ;
711 if (!*p)
712 die(_("unable to select a comment character that is not used\n"
713 "in the current commit message"));
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200714 free(comment_line_str_to_free);
715 comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700716}
717
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530718static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
719 struct pretty_print_context *ctx)
720{
721 const char *buffer, *subject, *fmt;
722
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200723 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530724 find_commit_subject(buffer, &subject);
725 /*
726 * If we amend the 'amend!' commit then we don't want to
727 * duplicate the subject line.
728 */
729 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200730 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200731 repo_unuse_commit_buffer(the_repository, commit, buffer);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530732}
733
Patrick Steinhardtd34b5cb2024-11-05 07:17:09 +0100734static void change_data_free(void *util, const char *str UNUSED)
735{
736 struct wt_status_change_data *d = util;
737 free(d->rename_source);
738 free(d);
739}
740
Junio C Hamanod249b092009-08-09 21:59:30 -0700741static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700742 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800743 struct wt_status *s,
744 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500745{
746 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800747 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700748 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500749 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100750 const char *hook_arg1 = NULL;
751 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000752 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200753 int old_display_comment_prefix;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100754 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500755
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700756 /* This checks and barfs if author is badly specified */
757 determine_author_info(author_ident);
758
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100759 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
760 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100761 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500762
Pat Notz89ac1222010-11-02 13:59:11 -0600763 if (squash_message) {
764 /*
765 * Insert the proper subject line before other commit
766 * message options add their content.
767 */
768 if (use_message && !strcmp(use_message, squash_message))
769 strbuf_addstr(&sb, "squash! ");
770 else {
771 struct pretty_print_context ctx = {0};
772 struct commit *c;
773 c = lookup_commit_reference_by_name(squash_message);
774 if (!c)
Teng Longe4cf0132023-05-29 21:27:56 +0800775 die(_("could not lookup commit '%s'"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600776 ctx.output_encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200777 repo_format_commit_message(the_repository, c,
778 "squash! %s\n\n", &sb,
779 &ctx);
Pat Notz89ac1222010-11-02 13:59:11 -0600780 }
781 }
782
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000783 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000784 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100785 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500786 } else if (logfile && !strcmp(logfile, "-")) {
787 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000788 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500789 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000790 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100791 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500792 } else if (logfile) {
793 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000794 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200795 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100796 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500797 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800798 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500799 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400800 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200801 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100802 hook_arg1 = "commit";
803 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600804 } else if (fixup_message) {
805 struct pretty_print_context ctx = {0};
806 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530807 char *fmt;
808 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600809 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +0800810 die(_("could not lookup commit '%s'"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600811 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530812 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200813 repo_format_commit_message(the_repository, commit, fmt, &sb,
814 &ctx);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530815 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600816 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530817
818 /*
819 * Only `-m` commit message option is checked here, as
820 * it supports `--fixup` to append the commit message.
821 *
822 * The other commit message options `-c`/`-C`/`-F` are
823 * incompatible with all the forms of `--fixup` and
824 * have already errored out while parsing the `git commit`
825 * options.
826 */
827 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
828 strbuf_addbuf(&sb, &message);
829
830 if (!strcmp(fixup_prefix, "amend")) {
831 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000832 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530833 prepare_amend_commit(commit, &sb, &ctx);
834 }
Stefan Beller102de882018-05-17 15:51:51 -0700835 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100836 size_t merge_msg_start;
837
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100838 /*
839 * prepend SQUASH_MSG here if it exists and a
840 * "merge --squash" was originally performed
841 */
Stefan Beller102de882018-05-17 15:51:51 -0700842 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
843 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100844 die_errno(_("could not read SQUASH_MSG"));
845 hook_arg1 = "squash";
846 } else
847 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100848
849 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700850 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000851 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100852
853 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
854 wt_status_locate_end(sb.buf + merge_msg_start,
855 sb.len - merge_msg_start) <
856 sb.len - merge_msg_start)
Josh Triplette90cc072024-02-27 01:17:36 -0800857 s->added_cut_line = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700858 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
859 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000860 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100861 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600862 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500863 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000864 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100865 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200866 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500867 }
868
Paolo Bonzini8089c852008-02-05 08:04:18 +0100869 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500870 * The remaining cases don't modify the template message, but
871 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100872 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500873 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100874 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000875 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500876 hook_arg1 = "commit";
877 hook_arg2 = "CHERRY_PICK_HEAD";
878 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100879
Pat Notz89ac1222010-11-02 13:59:11 -0600880 if (squash_message) {
881 /*
882 * If squash_commit was used for the commit subject,
883 * then we're possibly hijacking other commit log options.
884 * Reset the hook args to tell the real story.
885 */
886 hook_arg1 = "message";
887 hook_arg2 = "";
888 }
889
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530890 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700891 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530892 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500893
Matthieu Moy2556b992013-09-06 19:43:07 +0200894 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
895 old_display_comment_prefix = s->display_comment_prefix;
896 s->display_comment_prefix = 1;
897
Matthieu Moyea9882b2013-09-12 12:50:06 +0200898 /*
899 * Most hints are counter-productive when the commit has
900 * already started.
901 */
902 s->hints = 0;
903
Boris Faure8b1ae672011-05-08 12:31:02 +0200904 if (clean_message_contents)
Jeff King2982b652024-03-12 05:17:27 -0400905 strbuf_stripspace(&sb, NULL);
Johannes Schindelin13208572007-11-11 17:35:58 +0000906
Junio C Hamano073bd752014-10-28 12:44:09 -0700907 if (signoff)
Linus Arver7cb26a12023-10-20 19:01:33 +0000908 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000909
Jonathan Nieder37f30122011-02-25 23:10:49 -0600910 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000911 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500912
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700913 if (auto_comment_line_char)
914 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500915 strbuf_release(&sb);
916
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200917 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400918 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500919 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800920 int ident_shown = 0;
921 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400922 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800923 const char *hint_cleanup_all = allow_empty_message ?
924 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400925 " Lines starting\nwith '%s' will be ignored.\n") :
Hu Jialun6f70f002021-07-10 02:07:32 +0800926 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400927 " Lines starting\nwith '%s' will be ignored, and an empty"
Hu Jialun6f70f002021-07-10 02:07:32 +0800928 " message aborts the commit.\n");
929 const char *hint_cleanup_space = allow_empty_message ?
930 _("Please enter the commit message for your changes."
931 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400932 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800933 " yourself if you want to.\n") :
934 _("Please enter the commit message for your changes."
935 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400936 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800937 " yourself if you want to.\n"
938 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700939 if (whence != FROM_COMMIT) {
Josh Triplette90cc072024-02-27 01:17:36 -0800940 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
Josh Triplett688a0a72024-02-27 01:16:09 -0800941 wt_status_add_cut_line(s);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000942 status_printf_ln(
943 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400944 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000945 _("\n"
946 "It looks like you may be committing a merge.\n"
947 "If this is not correct, please run\n"
948 " git update-ref -d MERGE_HEAD\n"
949 "and try again.\n") :
950 _("\n"
951 "It looks like you may be committing a cherry-pick.\n"
952 "If this is not correct, please run\n"
953 " git update-ref -d CHERRY_PICK_HEAD\n"
954 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700955 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100956
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600957 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000958 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Jeff Kingf99e1d92024-03-12 05:17:34 -0400959 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
Denton Liu10559972019-04-17 11:23:28 +0100960 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
Josh Triplette90cc072024-02-27 01:17:36 -0800961 if (whence == FROM_COMMIT)
Josh Triplett688a0a72024-02-27 01:16:09 -0800962 wt_status_add_cut_line(s);
Denton Liu10559972019-04-17 11:23:28 +0100963 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Jeff Kingf99e1d92024-03-12 05:17:34 -0400964 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100965
Jeff Kingfac90832014-12-10 10:42:10 -0500966 /*
967 * These should never fail because they come from our own
968 * fmt_ident. They may fail the sane_ident test, but we know
969 * that the name and mail pointers will at least be valid,
970 * which is enough for our tests and printing here.
971 */
972 assert_split_ident(&ai, author_ident);
973 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400974
975 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600976 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000977 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400978 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600979 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400980 (int)(ai.name_end - ai.name_begin), ai.name_begin,
981 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200982
Jeff Kingb7242b82014-05-01 21:10:01 -0400983 if (author_date_is_interesting())
984 status_printf_ln(s, GIT_COLOR_NORMAL,
985 _("%s"
986 "Date: %s"),
987 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400988 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100989
Jeff Kingd6991ce2012-11-14 16:34:13 -0800990 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600991 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000992 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400993 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600994 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400995 (int)(ci.name_end - ci.name_begin), ci.name_begin,
996 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200997
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +0530998 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200999
Junio C Hamanod249b092009-08-09 21:59:30 -07001000 saved_color_setting = s->use_color;
1001 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001002 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -07001003 s->use_color = saved_color_setting;
Patrick Steinhardtd34b5cb2024-11-05 07:17:09 +01001004 string_list_clear_func(&s->change, change_data_free);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001005 } else {
brian m. carlson8066df42017-02-20 00:10:14 +00001006 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001007 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +01001008
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001009 if (!the_repository->index->initialized && repo_read_index(the_repository) < 0)
Johannes Schindelin2ee045e2023-06-29 13:23:10 +00001010 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +01001011
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001012 if (amend)
1013 parent = "HEAD^1";
1014
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001015 if (repo_get_oid(the_repository, parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001016 int i, ita_nr = 0;
1017
Derrick Stoleecb8388d2021-04-01 01:49:45 +00001018 /* TODO: audit for interaction with sparse-index. */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001019 ensure_full_index(the_repository->index);
1020 for (i = 0; i < the_repository->index->cache_nr; i++)
1021 if (ce_intent_to_add(the_repository->index->cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001022 ita_nr++;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001023 committable = the_repository->index->cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001024 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001025 /*
1026 * Unless the user did explicitly request a submodule
1027 * ignore mode by passing a command line option we do
1028 * not ignore any changed submodule SHA-1s when
1029 * comparing index and parent, no matter what is
1030 * configured. Otherwise we won't commit any
1031 * submodules which were manually staged, which would
1032 * be really confusing.
1033 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001034 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001035 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001036 if (ignore_submodule_arg &&
1037 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001038 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001039 committable = index_differs_from(the_repository,
1040 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001041 }
Alex Riesen71686242007-11-28 22:13:08 +01001042 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001043 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001044
Jonathan Nieder37f30122011-02-25 23:10:49 -06001045 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001046
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001047 if (trailer_args.nr) {
John Passaro4a861872024-05-05 18:49:09 +00001048 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001049 die(_("unable to pass trailers to --trailers"));
1050 strvec_clear(&trailer_args);
1051 }
1052
Jay Soffian37f7a852011-02-19 23:12:29 -05001053 /*
1054 * Reject an attempt to record a non-merge empty commit without
1055 * explicit --allow-empty. In the cherry-pick case, it may be
1056 * empty due to conflict resolution, which the user should okay.
1057 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001058 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001059 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001060 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001061 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001062 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001063 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001064 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001065 else if (is_from_cherry_pick(whence) ||
1066 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001067 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001068 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001069 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001070 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001071 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001072 else
1073 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001074 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001075 return 0;
1076 }
1077
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001078 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001079 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001080 * Re-read the index as the pre-commit-commit hook was invoked
1081 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001082 * the editor and after we invoke run_status above.
1083 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001084 discard_index(the_repository->index);
Kevin Willford680ee552017-08-14 15:54:25 -06001085 }
Patrick Steinhardt246deea2024-09-12 13:29:24 +02001086 read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
Kevin Willford680ee552017-08-14 15:54:25 -06001087
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001088 if (cache_tree_update(the_repository->index, 0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001089 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001090 return 0;
1091 }
1092
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001093 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301094 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001095 return 0;
1096
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001097 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001098 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001099
Jeff King22f9b7f2020-07-28 16:24:27 -04001100 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001101 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001102 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001103 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001104 exit(1);
1105 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001106 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001107 }
1108
1109 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001110 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1111 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001112 return 0;
1113 }
1114
1115 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001116}
1117
Junio C Hamano146ea062008-08-26 23:13:13 -07001118static const char *find_author_by_nickname(const char *name)
1119{
1120 struct rev_info revs;
1121 struct commit *commit;
1122 struct strbuf buf = STRBUF_INIT;
1123 const char *av[20];
1124 int ac = 0;
1125
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001126 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001127 strbuf_addf(&buf, "--author=%s", name);
1128 av[++ac] = "--all";
1129 av[++ac] = "-i";
1130 av[++ac] = buf.buf;
1131 av[++ac] = NULL;
1132 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001133 revs.mailmap = xmalloc(sizeof(struct string_list));
1134 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001135 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001136
Stefan Beller81c3ce32014-08-10 23:33:26 +02001137 if (prepare_revision_walk(&revs))
1138 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001139 commit = get_revision(&revs);
1140 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001141 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001142 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001143 strbuf_release(&buf);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +02001144 repo_format_commit_message(the_repository, commit,
1145 "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001146 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001147 return strbuf_detach(&buf, NULL);
1148 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001149 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001150}
1151
Jameson Millereec0f7f2017-10-30 13:21:37 -04001152static void handle_ignored_arg(struct wt_status *s)
1153{
1154 if (!ignored_arg)
1155 ; /* default already initialized */
1156 else if (!strcmp(ignored_arg, "traditional"))
1157 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1158 else if (!strcmp(ignored_arg, "no"))
1159 s->show_ignored_mode = SHOW_NO_IGNORED;
1160 else if (!strcmp(ignored_arg, "matching"))
1161 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1162 else
1163 die(_("Invalid ignored mode '%s'"), ignored_arg);
1164}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001165
Junio C Hamano63acdc42024-03-13 10:32:13 -07001166static enum untracked_status_type parse_untracked_setting_name(const char *u)
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001167{
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001168 /*
1169 * Please update $__git_untracked_file_modes in
1170 * git-completion.bash when you add new options
1171 */
Junio C Hamanof66e1a02024-03-13 10:32:14 -07001172 switch (git_parse_maybe_bool(u)) {
1173 case 0:
1174 u = "no";
1175 break;
1176 case 1:
1177 u = "normal";
1178 break;
1179 default:
1180 break;
1181 }
1182
Junio C Hamano63acdc42024-03-13 10:32:13 -07001183 if (!strcmp(u, "no"))
1184 return SHOW_NO_UNTRACKED_FILES;
1185 else if (!strcmp(u, "normal"))
1186 return SHOW_NORMAL_UNTRACKED_FILES;
1187 else if (!strcmp(u, "all"))
1188 return SHOW_ALL_UNTRACKED_FILES;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001189 else
Junio C Hamano63acdc42024-03-13 10:32:13 -07001190 return SHOW_UNTRACKED_FILES_ERROR;
1191}
1192
1193static void handle_untracked_files_arg(struct wt_status *s)
1194{
1195 enum untracked_status_type u;
1196
1197 if (!untracked_files_arg)
1198 return; /* default already initialized */
1199
1200 u = parse_untracked_setting_name(untracked_files_arg);
1201 if (u == SHOW_UNTRACKED_FILES_ERROR)
1202 die(_("Invalid untracked files mode '%s'"),
1203 untracked_files_arg);
1204 s->show_untracked_files = u;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001205}
1206
Jay Soffian37f7a852011-02-19 23:12:29 -05001207static const char *read_commit_message(const char *name)
1208{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001209 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001210 struct commit *commit;
1211
1212 commit = lookup_commit_reference_by_name(name);
1213 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +08001214 die(_("could not lookup commit '%s'"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001215 out_enc = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001216 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001217}
1218
Junio C Hamano84b42022013-06-24 11:41:40 -07001219/*
1220 * Enumerate what needs to be propagated when --porcelain
1221 * is not in effect here.
1222 */
1223static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001224 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001225 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001226 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001227} status_deferred_config = {
1228 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001229 -1, /* unspecified */
1230 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001231};
1232
1233static void finalize_deferred_config(struct wt_status *s)
1234{
1235 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001236 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001237 !s->null_termination);
1238
1239 if (s->null_termination) {
1240 if (status_format == STATUS_FORMAT_NONE ||
1241 status_format == STATUS_FORMAT_UNSPECIFIED)
1242 status_format = STATUS_FORMAT_PORCELAIN;
1243 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001244 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001245 }
1246
1247 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1248 status_format = status_deferred_config.status_format;
1249 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1250 status_format = STATUS_FORMAT_NONE;
1251
1252 if (use_deferred_config && s->show_branch < 0)
1253 s->show_branch = status_deferred_config.show_branch;
1254 if (s->show_branch < 0)
1255 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001256
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001257 /*
1258 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001259 * line argument *AND* we will print in a human-readable format
1260 * (short, long etc.) then we inherit from the status.aheadbehind
1261 * config setting. In all other cases (and porcelain V[12] formats
1262 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001263 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001264 if (use_deferred_config &&
1265 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001266 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1267
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001268 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1269 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001270}
1271
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301272static void check_fixup_reword_options(int argc, const char *argv[]) {
1273 if (whence != FROM_COMMIT) {
1274 if (whence == FROM_MERGE)
1275 die(_("You are in the middle of a merge -- cannot reword."));
1276 else if (is_from_cherry_pick(whence))
1277 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1278 }
1279 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001280 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301281 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001282 die(_("reword option of '%s' and '%s' cannot be used together"),
1283 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301284}
1285
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001286static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001287 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001288 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001289 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001290 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001291 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001292{
Jeff King036dbbf2012-05-07 15:18:26 -04001293 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001294 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001295
Junio C Hamano146ea062008-08-26 23:13:13 -07001296 if (force_author && !strchr(force_author, '>'))
1297 force_author = find_author_by_nickname(force_author);
1298
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001299 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001300 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001301
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301302 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001303 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001304
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001305 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001306 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001307 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001308 if (amend && whence != FROM_COMMIT) {
1309 if (whence == FROM_MERGE)
1310 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001311 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001312 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001313 else if (whence == FROM_REBASE_PICK)
1314 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001315 }
Pat Notz89ac1222010-11-02 13:59:11 -06001316 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001317 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1318 die_for_incompatible_opt4(!!use_message, "-C",
1319 !!edit_message, "-c",
1320 !!logfile, "-F",
1321 !!fixup_message, "--fixup");
1322 die_for_incompatible_opt4(have_option_m, "-m",
1323 !!edit_message, "-c",
1324 !!use_message, "-C",
1325 !!logfile, "-F");
1326 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001327 FREE_AND_NULL(template_file);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001328 if (edit_message)
1329 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001330 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001331 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001332 if (!use_message && !is_from_cherry_pick(whence) &&
1333 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001334 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001335 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001336 use_message_buffer = read_commit_message(use_message);
1337 if (!renew_authorship) {
1338 author_message = use_message;
1339 author_message_buffer = use_message_buffer;
1340 }
1341 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001342 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1343 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001344 author_message = "CHERRY_PICK_HEAD";
1345 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001346 }
1347
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001348 if (patch_interactive)
1349 interactive = 1;
1350
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001351 die_for_incompatible_opt4(also, "-i/--include",
1352 only, "-o/--only",
1353 all, "-a/--all",
1354 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301355 if (fixup_message) {
1356 /*
1357 * We limit --fixup's suboptions to only alpha characters.
1358 * If the first character after a run of alpha is colon,
1359 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301360 * name like `amend` or `reword`, or a misspelt suboption
1361 * name. In either case, we treat it as
1362 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301363 *
1364 * Otherwise, we are dealing with --fixup=<commit>.
1365 */
1366 char *p = fixup_message;
1367 while (isalpha(*p))
1368 p++;
1369 if (p > fixup_message && *p == ':') {
1370 *p = '\0';
1371 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301372 if (!strcmp("amend", fixup_message) ||
1373 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301374 fixup_prefix = "amend";
1375 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301376 if (*fixup_message == 'r') {
1377 check_fixup_reword_options(argc, argv);
1378 only = 1;
1379 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301380 } else {
1381 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1382 }
1383 } else {
1384 fixup_commit = fixup_message;
1385 fixup_prefix = "fixup";
1386 use_editor = 0;
1387 }
1388 }
1389
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001390 if (0 <= edit_flag)
1391 use_editor = edit_flag;
1392
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001393 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001394
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001395 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001396 die(_("paths '%s ...' with -a does not make sense"),
1397 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001398
Jeff Kingf3f47a12012-10-18 21:15:50 +07001399 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001400 dry_run = 1;
1401
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001402 return argc;
1403}
1404
Jeff Kinge885a842020-09-30 08:28:18 -04001405static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001406 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001407{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001408 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001409 const char *index_file;
1410
Jeff Kinge885a842020-09-30 08:28:18 -04001411 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001412 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001413 rollback_index_files();
1414
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001415 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001416}
1417
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001418define_list_config_array_extra(color_status_slots, {"added"});
1419
Jonathan Nieder88521172014-10-07 15:16:57 -04001420static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001421{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001422 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001423 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001424
1425 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001426}
1427
Glen Chooa4e7e312023-06-28 19:26:22 +00001428static int git_status_config(const char *k, const char *v,
1429 const struct config_context *ctx, void *cb)
Junio C Hamanof766b362009-08-09 23:12:19 -07001430{
1431 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001432 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001433
Christian Couder59556542013-11-30 21:55:40 +01001434 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001435 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001436 if (!strcmp(k, "status.submodulesummary")) {
1437 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001438 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1439 &is_bool);
Junio C Hamanof766b362009-08-09 23:12:19 -07001440 if (is_bool && s->submodule_summary)
1441 s->submodule_summary = -1;
1442 return 0;
1443 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001444 if (!strcmp(k, "status.short")) {
1445 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001446 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001447 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001448 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001449 return 0;
1450 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001451 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001452 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001453 return 0;
1454 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001455 if (!strcmp(k, "status.aheadbehind")) {
1456 status_deferred_config.ahead_behind = git_config_bool(k, v);
1457 return 0;
1458 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001459 if (!strcmp(k, "status.showstash")) {
1460 s->show_stash = git_config_bool(k, v);
1461 return 0;
1462 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001463 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001464 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001465 return 0;
1466 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001467 if (!strcmp(k, "status.displaycommentprefix")) {
1468 s->display_comment_prefix = git_config_bool(k, v);
1469 return 0;
1470 }
René Scharfee3f1da92014-10-04 20:54:50 +02001471 if (skip_prefix(k, "status.color.", &slot_name) ||
1472 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001473 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001474 if (slot < 0)
1475 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001476 if (!v)
1477 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001478 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001479 }
1480 if (!strcmp(k, "status.relativepaths")) {
1481 s->relative_paths = git_config_bool(k, v);
1482 return 0;
1483 }
1484 if (!strcmp(k, "status.showuntrackedfiles")) {
Junio C Hamano63acdc42024-03-13 10:32:13 -07001485 enum untracked_status_type u;
1486
Junio C Hamano63acdc42024-03-13 10:32:13 -07001487 u = parse_untracked_setting_name(v);
1488 if (u == SHOW_UNTRACKED_FILES_ERROR)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001489 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamano63acdc42024-03-13 10:32:13 -07001490 s->show_untracked_files = u;
Junio C Hamanof766b362009-08-09 23:12:19 -07001491 return 0;
1492 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001493 if (!strcmp(k, "diff.renamelimit")) {
1494 if (s->rename_limit == -1)
Glen Choo8868b1e2023-06-28 19:26:27 +00001495 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001496 return 0;
1497 }
1498 if (!strcmp(k, "status.renamelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001499 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001500 return 0;
1501 }
1502 if (!strcmp(k, "diff.renames")) {
1503 if (s->detect_rename == -1)
1504 s->detect_rename = git_config_rename(k, v);
1505 return 0;
1506 }
1507 if (!strcmp(k, "status.renames")) {
1508 s->detect_rename = git_config_rename(k, v);
1509 return 0;
1510 }
Glen Chooa4e7e312023-06-28 19:26:22 +00001511 return git_diff_ui_config(k, v, ctx, NULL);
Junio C Hamanof766b362009-08-09 23:12:19 -07001512}
1513
John Cai9b1cb502024-09-13 21:16:14 +00001514int cmd_status(int argc,
1515const char **argv,
1516const char *prefix,
1517struct repository *repo UNUSED)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001518{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001519 static int no_renames = -1;
1520 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001521 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001522 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001523 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001524 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001525 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001526 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001527 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001528 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001529 OPT_BOOL('b', "branch", &s.show_branch,
1530 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001531 OPT_BOOL(0, "show-stash", &s.show_stash,
1532 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001533 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1534 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001535 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001536 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001537 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001538 OPT_SET_INT(0, "long", &status_format,
1539 N_("show status in long format (default)"),
1540 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001541 OPT_BOOL('z', "null", &s.null_termination,
1542 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001543 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001544 N_("mode"),
1545 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001546 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001547 { OPTION_STRING, 0, "ignored", &ignored_arg,
1548 N_("mode"),
1549 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1550 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001551 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1552 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001553 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001554 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001555 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001556 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001557 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001558 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001559 OPT_END(),
1560 };
1561
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001562 if (argc == 2 && !strcmp(argv[1], "-h"))
1563 usage_with_options(builtin_status_usage, builtin_status_options);
1564
Derrick Stoleed76723e2021-07-14 13:12:37 +00001565 prepare_repo_settings(the_repository);
1566 the_repository->settings.command_requires_full_index = 0;
1567
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001568 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001569 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001570 builtin_status_options,
1571 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001572 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001573 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001574
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001575 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001576 handle_ignored_arg(&s);
1577
1578 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1579 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1580 die(_("Unsupported combination of ignored and untracked-files arguments"));
1581
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001582 parse_pathspec(&s.pathspec, 0,
1583 PATHSPEC_PREFER_FULL,
1584 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001585
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001586 if (status_format != STATUS_FORMAT_PORCELAIN &&
1587 status_format != STATUS_FORMAT_PORCELAIN_V2)
1588 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001589 repo_read_index(the_repository);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001590 refresh_index(the_repository->index,
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001591 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1592 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001593
Jeff King27344d62017-09-27 02:54:30 -04001594 if (use_optional_locks())
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001595 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
Jeff King27344d62017-09-27 02:54:30 -04001596 else
1597 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001598
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001599 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001600 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001601 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001602
Jens Lehmann46a958b2010-06-25 16:56:47 +02001603 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001604 s.status_format = status_format;
1605 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001606 if (no_renames != -1)
1607 s.detect_rename = !no_renames;
1608 if ((intptr_t)rename_score_arg != -1) {
1609 if (s.detect_rename < DIFF_DETECT_RENAME)
1610 s.detect_rename = DIFF_DETECT_RENAME;
1611 if (rename_score_arg)
1612 s.rename_score = parse_rename_score(&rename_score_arg);
1613 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001614
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001615 wt_status_collect(&s);
1616
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001617 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001618 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001619
Jeff King86617682009-12-07 00:26:25 -05001620 if (s.relative_paths)
1621 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001622
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001623 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001624 wt_status_collect_free_buffers(&s);
1625
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001626 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001627}
1628
Glen Chooa4e7e312023-06-28 19:26:22 +00001629static int git_commit_config(const char *k, const char *v,
1630 const struct config_context *ctx, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001631{
Junio C Hamanod249b092009-08-09 21:59:30 -07001632 struct wt_status *s = cb;
1633
Brian Hetro984c6e72008-07-05 01:24:40 -04001634 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001635 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001636 if (!strcmp(k, "commit.status")) {
1637 include_status = git_config_bool(k, v);
1638 return 0;
1639 }
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001640 if (!strcmp(k, "commit.cleanup")) {
1641 FREE_AND_NULL(cleanup_config);
1642 return git_config_string(&cleanup_config, k, v);
1643 }
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001644 if (!strcmp(k, "commit.gpgsign")) {
1645 sign_commit = git_config_bool(k, v) ? "" : NULL;
1646 return 0;
1647 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301648 if (!strcmp(k, "commit.verbose")) {
1649 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001650 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1651 &is_bool);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301652 return 0;
1653 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001654
Glen Chooa4e7e312023-06-28 19:26:22 +00001655 return git_status_config(k, v, ctx, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001656}
1657
John Cai9b1cb502024-09-13 21:16:14 +00001658int cmd_commit(int argc,
1659 const char **argv,
1660 const char *prefix,
1661 struct repository *repo UNUSED)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001662{
Jeff King036dbbf2012-05-07 15:18:26 -04001663 static struct wt_status s;
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001664 static const char *cleanup_arg = NULL;
Jeff King036dbbf2012-05-07 15:18:26 -04001665 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001666 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1667 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001668
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001669 OPT_GROUP(N_("Commit message options")),
1670 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1671 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1672 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1673 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1674 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1675 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301676 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301677 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1678 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301679 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301680 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 +07001681 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001682 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 +00001683 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 -07001684 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001685 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1686 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001687 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001688 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001689 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001690 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001691 /* end commit message options */
1692
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001693 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001694 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1695 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1696 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1697 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1698 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001699 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001700 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 +07001701 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001702 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001703 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001704 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1705 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001706 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001707 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001708 OPT_SET_INT(0, "long", &status_format,
1709 N_("show status in long format (default)"),
1710 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001711 OPT_BOOL('z', "null", &s.null_termination,
1712 N_("terminate entries with NUL")),
1713 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1714 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 +07001715 { 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 +00001716 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1717 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001718 /* end commit contents options */
1719
Stefan Beller4741edd2013-08-03 13:51:18 +02001720 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1721 N_("ok to record an empty change")),
1722 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1723 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001724
1725 OPT_END()
1726 };
1727
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001728 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001729 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001730 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001731 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001732 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001733 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001734 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001735 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001736 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001737 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001738
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001739 if (argc == 2 && !strcmp(argv[1], "-h"))
1740 usage_with_options(builtin_commit_usage, builtin_commit_options);
1741
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001742 prepare_repo_settings(the_repository);
1743 the_repository->settings.command_requires_full_index = 0;
1744
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001745 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301746 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301747 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001748 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001749
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001750 if (repo_get_oid(the_repository, "HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001751 current_head = NULL;
1752 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001753 current_head = lookup_commit_or_die(&oid, "HEAD");
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001754 if (repo_parse_commit(the_repository, current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001755 die(_("could not parse HEAD commit"));
1756 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301757 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001758 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1759 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001760 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301761 if (verbose == -1)
1762 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1763
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001764 if (cleanup_arg) {
1765 free(cleanup_config);
1766 cleanup_config = xstrdup(cleanup_arg);
1767 }
1768 cleanup_mode = get_cleanup_mode(cleanup_config, use_editor);
1769
Jeff Kingc9bfb952011-08-17 22:05:35 -07001770 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001771 return dry_run_commit(argv, prefix, current_head, &s);
1772 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001773
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001774 /* Set up everything for writing the commit object. This includes
1775 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001776 if (!prepare_to_commit(index_file, prefix,
1777 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001778 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001779 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001780 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001781 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001782
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001783 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001784 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001785 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001786 if (!reflog_msg)
1787 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001788 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001789 if (!reflog_msg)
1790 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001791 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001792 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001793 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001794 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001795 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001796 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001797
Christian Couder643cb5f2010-06-12 18:05:12 +02001798 if (!reflog_msg)
1799 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001800 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001801 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001802 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001803 struct commit *parent;
1804
1805 parent = get_merge_parent(m.buf);
1806 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001807 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001808 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001809 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001810 fclose(fp);
1811 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001812 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1813 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001814 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001815 if (!strcmp(sb.buf, "no-ff"))
1816 allow_fast_forward = 0;
1817 }
1818 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001819 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001820 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001821 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001822 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001823 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001824 : is_from_rebase(whence)
1825 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001826 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001827 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001828 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001829
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001830 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001831 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301832 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001833 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001834 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001835 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001836 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001837
Denton Liuf29cd862019-04-17 11:23:25 +01001838 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301839
Phillip Woodd0aaa462017-11-10 11:09:42 +00001840 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001841 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001842 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001843 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001844 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001845 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301846 rollback_index_files();
1847 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1848 exit(1);
1849 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001850
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301851 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1852 !allow_empty_message) {
1853 struct strbuf body = STRBUF_INIT;
1854 size_t len = commit_subject_length(sb.buf);
1855 strbuf_addstr(&body, sb.buf + len);
1856 if (message_is_empty(&body, cleanup_mode)) {
1857 rollback_index_files();
1858 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1859 exit(1);
1860 }
1861 strbuf_release(&body);
1862 }
1863
Junio C Hamano0074d182011-12-20 13:20:56 -08001864 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001865 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001866 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001867 } else {
1868 struct commit_extra_header **tail = &extra;
1869 append_merge_tag_headers(parents, &tail);
1870 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001871
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001872 if (commit_tree_extended(sb.buf, sb.len, &the_repository->index->cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001873 parents, &oid, author_ident.buf, NULL,
1874 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001875 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001876 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001877 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001878
Phillip Wood0505d602017-11-17 11:34:47 +00001879 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1880 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001881 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001882 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001883 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001884
Junio C Hamanof496b062019-07-09 15:25:44 -07001885 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001886 unlink(git_path_merge_head(the_repository));
1887 unlink(git_path_merge_msg(the_repository));
1888 unlink(git_path_merge_mode(the_repository));
1889 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001890
Brandon Casey5a9dd392008-01-23 11:21:22 -06001891 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001892 die(_("repository has been updated, but unable to write\n"
Junio C Hamanoa0607052023-10-17 20:08:51 -07001893 "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 +07001894 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001895
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001896 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001897
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001898 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001899 run_auto_maintenance(quiet);
Patrick Steinhardt1dc4ec22024-09-12 13:29:32 +02001900 run_commit_hook(use_editor, repo_get_index_file(the_repository),
1901 NULL, "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001902 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001903 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001904 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001905 if (!quiet) {
1906 unsigned int flags = 0;
1907
1908 if (!current_head)
1909 flags |= SUMMARY_INITIAL_COMMIT;
1910 if (author_date_is_interesting())
1911 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001912 print_commit_summary(the_repository, prefix,
1913 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001914 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001915
Patrick Steinhardt3f921c72024-01-19 11:40:19 +01001916 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
Denton Liua03b5552020-04-07 10:28:07 -04001917
Junio C Hamano00d8c312022-05-12 15:51:07 -07001918cleanup:
Patrick Steinhardt63c9bd32024-06-11 11:20:42 +02001919 free_commit_extra_headers(extra);
1920 free_commit_list(parents);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001921 strbuf_release(&author_ident);
1922 strbuf_release(&err);
1923 strbuf_release(&sb);
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001924 free(logfile);
1925 free(template_file);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001926 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001927}