blob: 2f459682221d6a12003918453a1f5bc17ea09a96 [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[] = {
Jean-Noël Avilad533c102025-01-15 20:23:45 +000047 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;
Patrick Steinhardt88dd3212025-02-07 12:03:37 +0100355 char *path = NULL;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500356
Junio C Hamano50b7e702009-08-04 23:49:33 -0700357 if (is_status)
358 refresh_flags |= REFRESH_UNMERGED;
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700359 parse_pathspec(&pathspec, 0,
360 PATHSPEC_PREFER_FULL,
361 prefix, argv);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500362
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000363 if (pathspec_from_file) {
364 if (interactive)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000365 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000366
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000367 if (all)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000368 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
Alexandr Miloslavskiy509efef2019-12-16 15:47:52 +0000369
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000370 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000371 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000372
373 parse_pathspec_file(&pathspec, 0,
374 PATHSPEC_PREFER_FULL,
375 prefix, pathspec_from_file, pathspec_file_nul);
376 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000377 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000378 }
379
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530380 if (!pathspec.nr && (also || (only && !allow_empty &&
381 (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
Alexandr Miloslavskiye440fc52019-11-19 16:48:55 +0000382 die(_("No paths with --include/--only does not make sense."));
383
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100384 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000385 die(_("index file corrupt"));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800386
Conrad Irwin1020d082011-05-06 22:59:59 -0700387 if (interactive) {
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000388 char *old_index_env = NULL, *old_repo_index_file;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100389 repo_hold_locked_index(the_repository, &index_lock,
390 LOCK_DIE_ON_ERROR);
Conrad Irwin1020d082011-05-06 22:59:59 -0700391
392 refresh_cache_or_die(refresh_flags);
393
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200394 if (write_locked_index(the_repository->index, &index_lock, 0))
Conrad Irwin1020d082011-05-06 22:59:59 -0700395 die(_("unable to create temporary index"));
396
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000397 old_repo_index_file = the_repository->index_file;
398 the_repository->index_file =
399 (char *)get_lock_file_path(&index_lock);
Jeff King406bab32019-01-11 17:15:40 -0500400 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000401 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
Conrad Irwin1020d082011-05-06 22:59:59 -0700402
John Cai83647452024-09-13 21:16:17 +0000403 if (interactive_add(the_repository, argv, prefix, patch_interactive) != 0)
Conrad Irwin1020d082011-05-06 22:59:59 -0700404 die(_("interactive add failed"));
405
Johannes Schindelinc480eeb2019-12-21 21:57:16 +0000406 the_repository->index_file = old_repo_index_file;
Conrad Irwin1020d082011-05-06 22:59:59 -0700407 if (old_index_env && *old_index_env)
408 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
409 else
410 unsetenv(INDEX_ENVIRONMENT);
Jeff King406bab32019-01-11 17:15:40 -0500411 FREE_AND_NULL(old_index_env);
Conrad Irwin1020d082011-05-06 22:59:59 -0700412
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200413 discard_index(the_repository->index);
414 read_index_from(the_repository->index, get_lock_file_path(&index_lock),
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200415 repo_get_git_dir(the_repository));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200416 if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700417 if (reopen_lock_file(&index_lock) < 0)
David Turner9c4d6c02014-07-13 13:28:19 -0700418 die(_("unable to write index file"));
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200419 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700420 die(_("unable to update temporary index"));
David Turner9c4d6c02014-07-13 13:28:19 -0700421 } else
422 warning(_("Failed to update main cache tree"));
Conrad Irwin1020d082011-05-06 22:59:59 -0700423
424 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200425 ret = get_lock_file_path(&index_lock);
426 goto out;
Conrad Irwin1020d082011-05-06 22:59:59 -0700427 }
428
Junio C Hamano28886052007-11-18 01:52:55 -0800429 /*
430 * Non partial, non as-is commit.
431 *
432 * (1) get the real index;
433 * (2) update the_index as necessary;
434 * (3) write the_index out to the real index (still locked);
435 * (4) return the name of the locked index file.
436 *
437 * The caller should run hooks on the locked real index, and
438 * (A) if all goes well, commit the real index;
439 * (B) on failure, rollback the real index.
440 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700441 if (all || (also && pathspec.nr)) {
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530442 char *ps_matched = xcalloc(pathspec.nr, 1);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100443 repo_hold_locked_index(the_repository, &index_lock,
444 LOCK_DIE_ON_ERROR);
Elijah Newren50c37ee2023-05-16 06:33:46 +0000445 add_files_to_cache(the_repository, also ? prefix : NULL,
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530446 &pathspec, ps_matched, 0, 0);
447 if (!all && report_path_error(ps_matched, &pathspec))
448 exit(128);
449
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100450 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200451 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
452 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700453 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800454 commit_style = COMMIT_NORMAL;
Martin Ågrendd1055e2017-09-23 01:34:49 +0200455 ret = get_lock_file_path(&index_lock);
Ghanshyam Thakkarac5946e2024-04-03 23:44:50 +0530456 free(ps_matched);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200457 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500458 }
459
Junio C Hamano28886052007-11-18 01:52:55 -0800460 /*
461 * As-is commit.
462 *
463 * (1) return the name of the real index file.
464 *
Markus Heidelberg73276232010-04-02 14:27:18 +0200465 * The caller should run hooks on the real index,
466 * and create commit from the_index.
Junio C Hamano28886052007-11-18 01:52:55 -0800467 * We still need to refresh the index here.
468 */
Nguyễn Thái Ngọc Duy6654c882013-07-14 15:35:38 +0700469 if (!only && !pathspec.nr) {
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100470 repo_hold_locked_index(the_repository, &index_lock,
471 LOCK_DIE_ON_ERROR);
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100472 refresh_cache_or_die(refresh_flags);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200473 if (the_repository->index->cache_changed
474 || !cache_tree_fully_valid(the_repository->index->cache_tree))
475 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
476 if (write_locked_index(the_repository->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100477 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700478 die(_("unable to write new index file"));
Junio C Hamano28886052007-11-18 01:52:55 -0800479 commit_style = COMMIT_AS_IS;
Patrick Steinhardt1dc4ec22024-09-12 13:29:32 +0200480 ret = repo_get_index_file(the_repository);
Martin Ågrendd1055e2017-09-23 01:34:49 +0200481 goto out;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500482 }
483
Junio C Hamano28886052007-11-18 01:52:55 -0800484 /*
485 * A partial commit.
486 *
487 * (0) find the set of affected paths;
488 * (1) get lock on the real index file;
489 * (2) update the_index with the given paths;
490 * (3) write the_index out to the real index (still locked);
491 * (4) get lock on the false index file;
492 * (5) reset the_index from HEAD;
493 * (6) update the_index the same way as (2);
494 * (7) write the_index out to the false index file;
495 * (8) return the name of the false index file (still locked);
496 *
497 * The caller should run hooks on the locked false index, and
498 * create commit from it. Then
499 * (A) if all goes well, commit the real index;
500 * (B) on failure, rollback the real index;
501 * In either case, rollback the false index.
502 */
503 commit_style = COMMIT_PARTIAL;
504
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000505 if (whence != FROM_COMMIT) {
506 if (whence == FROM_MERGE)
507 die(_("cannot do a partial commit during a merge."));
Phillip Wood8d57f752019-12-06 16:06:10 +0000508 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000509 die(_("cannot do a partial commit during a cherry-pick."));
Phillip Wood430b75f2019-12-06 16:06:12 +0000510 else if (is_from_rebase(whence))
511 die(_("cannot do a partial commit during a rebase."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +0000512 }
Junio C Hamano28886052007-11-18 01:52:55 -0800513
Jeff Kingc5c33502019-03-20 04:15:48 -0400514 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
Junio C Hamano28886052007-11-18 01:52:55 -0800515 exit(1);
516
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200517 discard_index(the_repository->index);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100518 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000519 die(_("cannot read the index"));
Junio C Hamano28886052007-11-18 01:52:55 -0800520
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100521 repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
Junio C Hamano28886052007-11-18 01:52:55 -0800522 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200523 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
524 cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
525 if (write_locked_index(the_repository->index, &index_lock, 0))
Junio C Hamanoa0607052023-10-17 20:08:51 -0700526 die(_("unable to write new index file"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500527
Patrick Steinhardt88dd3212025-02-07 12:03:37 +0100528 path = repo_git_path(the_repository, "next-index-%"PRIuMAX,
529 (uintmax_t) getpid());
530 hold_lock_file_for_update(&false_lock, path,
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700531 LOCK_DIE_ON_ERROR);
Linus Torvaldsfa9dcf82008-01-13 00:30:56 -0800532
Junio C Hamano06bb6432011-08-19 11:58:18 -0700533 create_base_index(current_head);
Junio C Hamano28886052007-11-18 01:52:55 -0800534 add_remove_files(&partial);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200535 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500536
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200537 if (write_locked_index(the_repository->index, &false_lock, 0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000538 die(_("unable to write temporary index file"));
Jeff King959ba672008-02-14 12:18:23 -0500539
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +0200540 discard_index(the_repository->index);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200541 ret = get_lock_file_path(&false_lock);
Patrick Steinhardt246deea2024-09-12 13:29:24 +0200542 read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
Martin Ågrendd1055e2017-09-23 01:34:49 +0200543out:
544 string_list_clear(&partial, 0);
545 clear_pathspec(&pathspec);
Patrick Steinhardt88dd3212025-02-07 12:03:37 +0100546 free(path);
Michael Haggertyb4fb09e2015-08-10 11:47:39 +0200547 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500548}
549
Junio C Hamanod249b092009-08-09 21:59:30 -0700550static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
551 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500552{
brian m. carlson8066df42017-02-20 00:10:14 +0000553 struct object_id oid;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700554
Junio C Hamanod249b092009-08-09 21:59:30 -0700555 if (s->relative_paths)
556 s->prefix = prefix;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500557
558 if (amend) {
Junio C Hamanod249b092009-08-09 21:59:30 -0700559 s->amend = 1;
560 s->reference = "HEAD^1";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500561 }
Junio C Hamanod249b092009-08-09 21:59:30 -0700562 s->verbose = verbose;
563 s->index_file = index_file;
564 s->fp = fp;
565 s->nowarn = nowarn;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200566 s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -0400567 if (!s->is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +0000568 oidcpy(&s->oid_commit, &oid);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400569 s->status_format = status_format;
570 s->ignore_submodule_arg = ignore_submodule_arg;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500571
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700572 wt_status_collect(s);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -0400573 wt_status_print(s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700574 wt_status_collect_free_buffers(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500575
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700576 return s->committable;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500577}
578
Junio C Hamano06bb6432011-08-19 11:58:18 -0700579static int is_a_merge(const struct commit *current_head)
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100580{
Junio C Hamano06bb6432011-08-19 11:58:18 -0700581 return !!(current_head->parents && current_head->parents->next);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100582}
583
Jeff Kingfac90832014-12-10 10:42:10 -0500584static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
585{
Jeff Kingc83a5092014-12-10 10:43:42 -0500586 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200587 BUG("unable to parse our own ident: %s", buf->buf);
Jeff Kingfac90832014-12-10 10:42:10 -0500588}
589
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700590static void export_one(const char *var, const char *s, const char *e, int hack)
591{
592 struct strbuf buf = STRBUF_INIT;
593 if (hack)
594 strbuf_addch(&buf, hack);
René Scharfe147ee352019-12-07 12:16:04 +0100595 strbuf_add(&buf, s, e - s);
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700596 setenv(var, buf.buf, 1);
597 strbuf_release(&buf);
598}
599
Jeff Kingc33ddc22014-08-27 03:57:08 -0400600static int parse_force_date(const char *in, struct strbuf *out)
Jeff King14ac2862014-05-01 21:12:42 -0400601{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400602 strbuf_addch(out, '@');
Jeff King14ac2862014-05-01 21:12:42 -0400603
Jeff Kingc33ddc22014-08-27 03:57:08 -0400604 if (parse_date(in, out) < 0) {
Jeff King14ac2862014-05-01 21:12:42 -0400605 int errors = 0;
606 unsigned long t = approxidate_careful(in, &errors);
607 if (errors)
608 return -1;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400609 strbuf_addf(out, "%lu", t);
Jeff King14ac2862014-05-01 21:12:42 -0400610 }
611
612 return 0;
613}
614
Jeff Kingf4ef5172014-08-27 03:57:56 -0400615static void set_ident_var(char **buf, char *val)
616{
617 free(*buf);
618 *buf = val;
619}
620
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800621static void determine_author_info(struct strbuf *author_ident)
Santi Béjara45d46b2008-05-04 18:04:49 +0200622{
623 char *name, *email, *date;
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700624 struct ident_split author;
Santi Béjara45d46b2008-05-04 18:04:49 +0200625
Jeff Kingeaa541e2015-01-12 20:58:33 -0500626 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
627 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
628 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
Santi Béjara45d46b2008-05-04 18:04:49 +0200629
Jay Soffian37f7a852011-02-19 23:12:29 -0500630 if (author_message) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400631 struct ident_split ident;
Junio C Hamano2c733fb2012-02-02 13:41:43 -0800632 size_t len;
Jeff Kingf0f96622014-08-27 03:57:28 -0400633 const char *a;
Santi Béjara45d46b2008-05-04 18:04:49 +0200634
Jeff Kingf0f96622014-08-27 03:57:28 -0400635 a = find_commit_header(author_message_buffer, "author", &len);
Santi Béjara45d46b2008-05-04 18:04:49 +0200636 if (!a)
Jeff Kingf0f96622014-08-27 03:57:28 -0400637 die(_("commit '%s' lacks author header"), author_message);
638 if (split_ident_line(&ident, a, len) < 0)
639 die(_("commit '%s' has malformed author line"), author_message);
Santi Béjara45d46b2008-05-04 18:04:49 +0200640
Jeff Kingf4ef5172014-08-27 03:57:56 -0400641 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
642 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200643
Jeff Kingf0f96622014-08-27 03:57:28 -0400644 if (ident.date_begin) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400645 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingf0f96622014-08-27 03:57:28 -0400646 strbuf_addch(&date_buf, '@');
647 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
648 strbuf_addch(&date_buf, ' ');
649 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400650 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff Kingf0f96622014-08-27 03:57:28 -0400651 }
Santi Béjara45d46b2008-05-04 18:04:49 +0200652 }
653
654 if (force_author) {
Jeff Kingf0f96622014-08-27 03:57:28 -0400655 struct ident_split ident;
Santi Béjara45d46b2008-05-04 18:04:49 +0200656
Jeff Kingf0f96622014-08-27 03:57:28 -0400657 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000658 die(_("malformed --author parameter"));
Jeff Kingf4ef5172014-08-27 03:57:56 -0400659 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
660 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
Santi Béjara45d46b2008-05-04 18:04:49 +0200661 }
662
Jeff King14ac2862014-05-01 21:12:42 -0400663 if (force_date) {
Jeff Kingf4ef5172014-08-27 03:57:56 -0400664 struct strbuf date_buf = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -0400665 if (parse_force_date(force_date, &date_buf))
Jeff King14ac2862014-05-01 21:12:42 -0400666 die(_("invalid date format: %s"), force_date);
Jeff Kingf4ef5172014-08-27 03:57:56 -0400667 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
Jeff King14ac2862014-05-01 21:12:42 -0400668 }
669
William Hubbs39ab4d02019-02-04 12:48:50 -0600670 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
671 IDENT_STRICT));
Jeff Kingc83a5092014-12-10 10:43:42 -0500672 assert_split_ident(&author, author_ident);
673 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
674 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
675 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
Jeff Kingf4ef5172014-08-27 03:57:56 -0400676 free(name);
677 free(email);
678 free(date);
Santi Béjara45d46b2008-05-04 18:04:49 +0200679}
680
Jeff Kingb7242b82014-05-01 21:10:01 -0400681static int author_date_is_interesting(void)
682{
683 return author_message || force_date;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800684}
685
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700686static void adjust_comment_line_char(const struct strbuf *sb)
687{
688 char candidates[] = "#;@!$%^&|:";
689 char *candidate;
690 const char *p;
691
Jeff King1751e582024-03-12 05:17:19 -0400692 if (!memchr(sb->buf, candidates[0], sb->len)) {
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200693 free(comment_line_str_to_free);
694 comment_line_str = comment_line_str_to_free =
695 xstrfmt("%c", candidates[0]);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700696 return;
Jeff King1751e582024-03-12 05:17:19 -0400697 }
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700698
699 p = sb->buf;
700 candidate = strchr(candidates, *p);
701 if (candidate)
702 *candidate = ' ';
703 for (p = sb->buf; *p; p++) {
704 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
705 candidate = strchr(candidates, p[1]);
706 if (candidate)
707 *candidate = ' ';
708 }
709 }
710
711 for (p = candidates; *p == ' '; p++)
712 ;
713 if (!*p)
714 die(_("unable to select a comment character that is not used\n"
715 "in the current commit message"));
Patrick Steinhardt648abbe2024-08-14 08:52:14 +0200716 free(comment_line_str_to_free);
717 comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p);
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700718}
719
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530720static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
721 struct pretty_print_context *ctx)
722{
723 const char *buffer, *subject, *fmt;
724
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200725 buffer = repo_get_commit_buffer(the_repository, commit, NULL);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530726 find_commit_subject(buffer, &subject);
727 /*
728 * If we amend the 'amend!' commit then we don't want to
729 * duplicate the subject line.
730 */
731 fmt = starts_with(subject, "amend!") ? "%b" : "%B";
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200732 repo_format_commit_message(the_repository, commit, fmt, sb, ctx);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200733 repo_unuse_commit_buffer(the_repository, commit, buffer);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530734}
735
Patrick Steinhardtd34b5cb2024-11-05 07:17:09 +0100736static void change_data_free(void *util, const char *str UNUSED)
737{
738 struct wt_status_change_data *d = util;
739 free(d->rename_source);
740 free(d);
741}
742
Junio C Hamanod249b092009-08-09 21:59:30 -0700743static int prepare_to_commit(const char *index_file, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -0700744 struct commit *current_head,
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800745 struct wt_status *s,
746 struct strbuf *author_ident)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500747{
748 struct stat statbuf;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -0800749 struct strbuf committer_ident = STRBUF_INIT;
Stephen P. Smith6fa90192018-09-05 17:53:27 -0700750 int committable;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500751 struct strbuf sb = STRBUF_INIT;
Paolo Bonzini8089c852008-02-05 08:04:18 +0100752 const char *hook_arg1 = NULL;
753 const char *hook_arg2 = NULL;
Phillip Woodd0aaa462017-11-10 11:09:42 +0000754 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
Matthieu Moy2556b992013-09-06 19:43:07 +0200755 int old_display_comment_prefix;
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100756 int invoked_hook;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500757
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700758 /* This checks and barfs if author is badly specified */
759 determine_author_info(author_ident);
760
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100761 if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
762 "pre-commit", NULL))
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100763 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500764
Pat Notz89ac1222010-11-02 13:59:11 -0600765 if (squash_message) {
766 /*
767 * Insert the proper subject line before other commit
768 * message options add their content.
769 */
770 if (use_message && !strcmp(use_message, squash_message))
771 strbuf_addstr(&sb, "squash! ");
772 else {
773 struct pretty_print_context ctx = {0};
774 struct commit *c;
775 c = lookup_commit_reference_by_name(squash_message);
776 if (!c)
Teng Longe4cf0132023-05-29 21:27:56 +0800777 die(_("could not lookup commit '%s'"), squash_message);
Pat Notz89ac1222010-11-02 13:59:11 -0600778 ctx.output_encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200779 repo_format_commit_message(the_repository, c,
780 "squash! %s\n\n", &sb,
781 &ctx);
Pat Notz89ac1222010-11-02 13:59:11 -0600782 }
783 }
784
Ævar Arnfjörð Bjarmason30884c92017-12-22 20:41:52 +0000785 if (have_option_m && !fixup_message) {
Johannes Schindelinf9568532007-11-11 17:36:39 +0000786 strbuf_addbuf(&sb, &message);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100787 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500788 } else if (logfile && !strcmp(logfile, "-")) {
789 if (isatty(0))
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000790 fprintf(stderr, _("(reading log message from standard input)\n"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500791 if (strbuf_read(&sb, 0, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000792 die_errno(_("could not read log from standard input"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100793 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500794 } else if (logfile) {
795 if (strbuf_read_file(&sb, logfile, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000796 die_errno(_("could not read log file '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200797 logfile);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100798 hook_arg1 = "message";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500799 } else if (use_message) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800800 char *buffer;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500801 buffer = strstr(use_message_buffer, "\n\n");
Jeff King076cbd62014-04-25 19:11:15 -0400802 if (buffer)
Johannes Schindelin84e213a2016-06-29 16:14:42 +0200803 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100804 hook_arg1 = "commit";
805 hook_arg2 = use_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -0600806 } else if (fixup_message) {
807 struct pretty_print_context ctx = {0};
808 struct commit *commit;
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530809 char *fmt;
810 commit = lookup_commit_reference_by_name(fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600811 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +0800812 die(_("could not lookup commit '%s'"), fixup_commit);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600813 ctx.output_encoding = get_commit_output_encoding();
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530814 fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200815 repo_format_commit_message(the_repository, commit, fmt, &sb,
816 &ctx);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530817 free(fmt);
Pat Notzd71b8ba2010-11-02 13:59:09 -0600818 hook_arg1 = "message";
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530819
820 /*
821 * Only `-m` commit message option is checked here, as
822 * it supports `--fixup` to append the commit message.
823 *
824 * The other commit message options `-c`/`-C`/`-F` are
825 * incompatible with all the forms of `--fixup` and
826 * have already errored out while parsing the `git commit`
827 * options.
828 */
829 if (have_option_m && !strcmp(fixup_prefix, "fixup"))
830 strbuf_addbuf(&sb, &message);
831
832 if (!strcmp(fixup_prefix, "amend")) {
833 if (have_option_m)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000834 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
Charvi Mendiratta494d3142021-03-15 13:24:32 +0530835 prepare_amend_commit(commit, &sb, &ctx);
836 }
Stefan Beller102de882018-05-17 15:51:51 -0700837 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
Denton Liu10559972019-04-17 11:23:28 +0100838 size_t merge_msg_start;
839
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100840 /*
841 * prepend SQUASH_MSG here if it exists and a
842 * "merge --squash" was originally performed
843 */
Stefan Beller102de882018-05-17 15:51:51 -0700844 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
845 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Sven Strickrothb64c1e02016-03-21 23:29:40 +0100846 die_errno(_("could not read SQUASH_MSG"));
847 hook_arg1 = "squash";
848 } else
849 hook_arg1 = "merge";
Denton Liu10559972019-04-17 11:23:28 +0100850
851 merge_msg_start = sb.len;
Stefan Beller102de882018-05-17 15:51:51 -0700852 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000853 die_errno(_("could not read MERGE_MSG"));
Denton Liu10559972019-04-17 11:23:28 +0100854
855 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
856 wt_status_locate_end(sb.buf + merge_msg_start,
857 sb.len - merge_msg_start) <
858 sb.len - merge_msg_start)
Josh Triplette90cc072024-02-27 01:17:36 -0800859 s->added_cut_line = 1;
Stefan Beller102de882018-05-17 15:51:51 -0700860 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
861 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000862 die_errno(_("could not read SQUASH_MSG"));
Paolo Bonzini8089c852008-02-05 08:04:18 +0100863 hook_arg1 = "squash";
Jonathan Nieder2140b142011-02-25 03:07:57 -0600864 } else if (template_file) {
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500865 if (strbuf_read_file(&sb, template_file, 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000866 die_errno(_("could not read '%s'"), template_file);
Paolo Bonzini8089c852008-02-05 08:04:18 +0100867 hook_arg1 = "template";
Boris Faure8b1ae672011-05-08 12:31:02 +0200868 clean_message_contents = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500869 }
870
Paolo Bonzini8089c852008-02-05 08:04:18 +0100871 /*
Jay Soffian37f7a852011-02-19 23:12:29 -0500872 * The remaining cases don't modify the template message, but
873 * just set the argument(s) to the prepare-commit-msg hook.
Paolo Bonzini8089c852008-02-05 08:04:18 +0100874 */
Jay Soffian37f7a852011-02-19 23:12:29 -0500875 else if (whence == FROM_MERGE)
Paolo Bonzini8089c852008-02-05 08:04:18 +0100876 hook_arg1 = "merge";
Phillip Wood430b75f2019-12-06 16:06:12 +0000877 else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
Jay Soffian37f7a852011-02-19 23:12:29 -0500878 hook_arg1 = "commit";
879 hook_arg2 = "CHERRY_PICK_HEAD";
880 }
Paolo Bonzini8089c852008-02-05 08:04:18 +0100881
Pat Notz89ac1222010-11-02 13:59:11 -0600882 if (squash_message) {
883 /*
884 * If squash_commit was used for the commit subject,
885 * then we're possibly hijacking other commit log options.
886 * Reset the hook args to tell the real story.
887 */
888 hook_arg1 = "message";
889 hook_arg2 = "";
890 }
891
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530892 s->fp = fopen_for_writing(git_path_commit_editmsg());
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700893 if (!s->fp)
Pranit Bauvae51b0df2016-05-25 00:49:50 +0530894 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500895
Matthieu Moy2556b992013-09-06 19:43:07 +0200896 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
897 old_display_comment_prefix = s->display_comment_prefix;
898 s->display_comment_prefix = 1;
899
Matthieu Moyea9882b2013-09-12 12:50:06 +0200900 /*
901 * Most hints are counter-productive when the commit has
902 * already started.
903 */
904 s->hints = 0;
905
Boris Faure8b1ae672011-05-08 12:31:02 +0200906 if (clean_message_contents)
Jeff King2982b652024-03-12 05:17:27 -0400907 strbuf_stripspace(&sb, NULL);
Johannes Schindelin13208572007-11-11 17:35:58 +0000908
Junio C Hamano073bd752014-10-28 12:44:09 -0700909 if (signoff)
Linus Arver7cb26a12023-10-20 19:01:33 +0000910 append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
Johannes Schindelin13208572007-11-11 17:35:58 +0000911
Jonathan Nieder37f30122011-02-25 23:10:49 -0600912 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +0000913 die_errno(_("could not write commit template"));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500914
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700915 if (auto_comment_line_char)
916 adjust_comment_line_char(&sb);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -0500917 strbuf_release(&sb);
918
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200919 /* This checks if committer ident is explicitly given */
Jeff Kingf20f3872012-07-23 14:50:35 -0400920 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
James P. Howard, IIbed575e2009-12-07 17:45:27 -0500921 if (use_editor && include_status) {
Elia Pintoe23fd152014-01-30 07:15:56 -0800922 int ident_shown = 0;
923 int saved_color_setting;
Jeff King47010262014-05-01 21:06:57 -0400924 struct ident_split ci, ai;
Hu Jialun6f70f002021-07-10 02:07:32 +0800925 const char *hint_cleanup_all = allow_empty_message ?
926 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400927 " Lines starting\nwith '%s' will be ignored.\n") :
Hu Jialun6f70f002021-07-10 02:07:32 +0800928 _("Please enter the commit message for your changes."
Jeff Kingf99e1d92024-03-12 05:17:34 -0400929 " Lines starting\nwith '%s' will be ignored, and an empty"
Hu Jialun6f70f002021-07-10 02:07:32 +0800930 " message aborts the commit.\n");
931 const char *hint_cleanup_space = allow_empty_message ?
932 _("Please enter the commit message for your changes."
933 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400934 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800935 " yourself if you want to.\n") :
936 _("Please enter the commit message for your changes."
937 " Lines starting\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400938 "with '%s' will be kept; you may remove them"
Hu Jialun6f70f002021-07-10 02:07:32 +0800939 " yourself if you want to.\n"
940 "An empty message aborts the commit.\n");
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700941 if (whence != FROM_COMMIT) {
Josh Triplette90cc072024-02-27 01:17:36 -0800942 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
Josh Triplett688a0a72024-02-27 01:16:09 -0800943 wt_status_add_cut_line(s);
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000944 status_printf_ln(
945 s, GIT_COLOR_NORMAL,
Jeff Kingca03e062017-04-20 17:08:54 -0400946 whence == FROM_MERGE ?
Han-Wen Nienhuysb6d25582020-08-21 16:59:36 +0000947 _("\n"
948 "It looks like you may be committing a merge.\n"
949 "If this is not correct, please run\n"
950 " git update-ref -d MERGE_HEAD\n"
951 "and try again.\n") :
952 _("\n"
953 "It looks like you may be committing a cherry-pick.\n"
954 "If this is not correct, please run\n"
955 " git update-ref -d CHERRY_PICK_HEAD\n"
956 "and try again.\n"));
Nguyễn Thái Ngọc Duy75df1f42014-02-17 19:15:32 +0700957 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100958
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600959 fprintf(s->fp, "\n");
Phillip Woodd0aaa462017-11-10 11:09:42 +0000960 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
Jeff Kingf99e1d92024-03-12 05:17:34 -0400961 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
Denton Liu10559972019-04-17 11:23:28 +0100962 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
Josh Triplette90cc072024-02-27 01:17:36 -0800963 if (whence == FROM_COMMIT)
Josh Triplett688a0a72024-02-27 01:16:09 -0800964 wt_status_add_cut_line(s);
Denton Liu10559972019-04-17 11:23:28 +0100965 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
Jeff Kingf99e1d92024-03-12 05:17:34 -0400966 status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100967
Jeff Kingfac90832014-12-10 10:42:10 -0500968 /*
969 * These should never fail because they come from our own
970 * fmt_ident. They may fail the sane_ident test, but we know
971 * that the name and mail pointers will at least be valid,
972 * which is enough for our tests and printing here.
973 */
974 assert_split_ident(&ai, author_ident);
975 assert_split_ident(&ci, &committer_ident);
Jeff King47010262014-05-01 21:06:57 -0400976
977 if (ident_cmp(&ai, &ci))
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600978 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000979 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400980 "Author: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600981 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400982 (int)(ai.name_end - ai.name_begin), ai.name_begin,
983 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
Santi Béjare83dbe82008-05-04 18:04:50 +0200984
Jeff Kingb7242b82014-05-01 21:10:01 -0400985 if (author_date_is_interesting())
986 status_printf_ln(s, GIT_COLOR_NORMAL,
987 _("%s"
988 "Date: %s"),
989 ident_shown++ ? "" : "\n",
Jeff Kinga5481a62015-06-25 12:55:02 -0400990 show_ident_date(&ai, DATE_MODE(NORMAL)));
Paolo Bonziniec84bd02008-02-05 11:01:46 +0100991
Jeff Kingd6991ce2012-11-14 16:34:13 -0800992 if (!committer_ident_sufficiently_given())
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600993 status_printf_ln(s, GIT_COLOR_NORMAL,
Ævar Arnfjörð Bjarmasonfe8165c2011-02-22 23:41:46 +0000994 _("%s"
Jeff King47010262014-05-01 21:06:57 -0400995 "Committer: %.*s <%.*s>"),
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600996 ident_shown++ ? "" : "\n",
Jeff King47010262014-05-01 21:06:57 -0400997 (int)(ci.name_end - ci.name_begin), ci.name_begin,
998 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
Santi Béjarbb1ae3f2008-05-04 18:04:51 +0200999
Kaartic Sivaraamb3cf1b72017-06-30 17:42:21 +05301000 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
Santi Béjarbb1ae3f2008-05-04 18:04:51 +02001001
Junio C Hamanod249b092009-08-09 21:59:30 -07001002 saved_color_setting = s->use_color;
1003 s->use_color = 0;
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001004 committable = run_status(s->fp, index_file, prefix, 1, s);
Junio C Hamanod249b092009-08-09 21:59:30 -07001005 s->use_color = saved_color_setting;
Patrick Steinhardtd34b5cb2024-11-05 07:17:09 +01001006 string_list_clear_func(&s->change, change_data_free);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001007 } else {
brian m. carlson8066df42017-02-20 00:10:14 +00001008 struct object_id oid;
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001009 const char *parent = "HEAD";
Alex Riesen71686242007-11-28 22:13:08 +01001010
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001011 if (!the_repository->index->initialized && repo_read_index(the_repository) < 0)
Johannes Schindelin2ee045e2023-06-29 13:23:10 +00001012 die(_("Cannot read index"));
Alex Riesen71686242007-11-28 22:13:08 +01001013
Junio C Hamanofbcf1182007-12-19 19:23:03 -08001014 if (amend)
1015 parent = "HEAD^1";
1016
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001017 if (repo_get_oid(the_repository, parent, &oid)) {
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001018 int i, ita_nr = 0;
1019
Derrick Stoleecb8388d2021-04-01 01:49:45 +00001020 /* TODO: audit for interaction with sparse-index. */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001021 ensure_full_index(the_repository->index);
1022 for (i = 0; i < the_repository->index->cache_nr; i++)
1023 if (ce_intent_to_add(the_repository->index->cache[i]))
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001024 ita_nr++;
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001025 committable = the_repository->index->cache_nr - ita_nr > 0;
Nguyễn Thái Ngọc Duy2c49f7f2016-10-24 17:42:22 +07001026 } else {
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001027 /*
1028 * Unless the user did explicitly request a submodule
1029 * ignore mode by passing a command line option we do
1030 * not ignore any changed submodule SHA-1s when
1031 * comparing index and parent, no matter what is
1032 * configured. Otherwise we won't commit any
1033 * submodules which were manually staged, which would
1034 * be really confusing.
1035 */
Brandon Williams02f2f562017-10-31 11:19:05 -07001036 struct diff_flags flags = DIFF_FLAGS_INIT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001037 flags.override_submodule_config = 1;
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001038 if (ignore_submodule_arg &&
1039 !strcmp(ignore_submodule_arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001040 flags.ignore_submodules = 1;
Nguyễn Thái Ngọc Duyffc00a42018-11-10 06:49:04 +01001041 committable = index_differs_from(the_repository,
1042 parent, &flags, 1);
Jens Lehmannc215d3d2014-04-05 18:59:36 +02001043 }
Alex Riesen71686242007-11-28 22:13:08 +01001044 }
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001045 strbuf_release(&committer_ident);
Alex Riesen71686242007-11-28 22:13:08 +01001046
Jonathan Nieder37f30122011-02-25 23:10:49 -06001047 fclose(s->fp);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001048
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001049 if (trailer_args.nr) {
John Passaro4a861872024-05-05 18:49:09 +00001050 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
ZheNing Hu2daae3d2021-03-23 13:55:57 +00001051 die(_("unable to pass trailers to --trailers"));
1052 strvec_clear(&trailer_args);
1053 }
1054
Jay Soffian37f7a852011-02-19 23:12:29 -05001055 /*
1056 * Reject an attempt to record a non-merge empty commit without
1057 * explicit --allow-empty. In the cherry-pick case, it may be
1058 * empty due to conflict resolution, which the user should okay.
1059 */
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001060 if (!committable && whence != FROM_MERGE && !allow_empty &&
Junio C Hamano06bb6432011-08-19 11:58:18 -07001061 !(amend && is_a_merge(current_head))) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001062 s->hints = advice_enabled(ADVICE_STATUS_HINTS);
Matthieu Moy2556b992013-09-06 19:43:07 +02001063 s->display_comment_prefix = old_display_comment_prefix;
Junio C Hamanod249b092009-08-09 21:59:30 -07001064 run_status(stdout, index_file, prefix, 0, s);
Jeff Kingf197ed22010-06-06 20:41:46 -04001065 if (amend)
Ævar Arnfjörð Bjarmasonfc88e312011-02-22 23:41:49 +00001066 fputs(_(empty_amend_advice), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001067 else if (is_from_cherry_pick(whence) ||
1068 whence == FROM_REBASE_PICK) {
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001069 fputs(_(empty_cherry_pick_advice), stderr);
Phillip Wood8d57f752019-12-06 16:06:10 +00001070 if (whence == FROM_CHERRY_PICK_SINGLE)
Jeff Kingc17592a2013-07-26 19:39:28 -04001071 fputs(_(empty_cherry_pick_advice_single), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001072 else if (whence == FROM_CHERRY_PICK_MULTI)
Jeff Kingc17592a2013-07-26 19:39:28 -04001073 fputs(_(empty_cherry_pick_advice_multi), stderr);
Phillip Wood430b75f2019-12-06 16:06:12 +00001074 else
1075 fputs(_(empty_rebase_pick_advice), stderr);
Jeff Kingc17592a2013-07-26 19:39:28 -04001076 }
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001077 return 0;
1078 }
1079
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001080 if (!no_verify && invoked_hook) {
Kevin Willford680ee552017-08-14 15:54:25 -06001081 /*
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001082 * Re-read the index as the pre-commit-commit hook was invoked
1083 * and could have updated it. We must do this before we invoke
Kevin Willford680ee552017-08-14 15:54:25 -06001084 * the editor and after we invoke run_status above.
1085 */
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001086 discard_index(the_repository->index);
Kevin Willford680ee552017-08-14 15:54:25 -06001087 }
Patrick Steinhardt246deea2024-09-12 13:29:24 +02001088 read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
Kevin Willford680ee552017-08-14 15:54:25 -06001089
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001090 if (cache_tree_update(the_repository->index, 0)) {
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001091 error(_("Error building trees"));
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001092 return 0;
1093 }
1094
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001095 if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301096 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
Paolo Bonzini8089c852008-02-05 08:04:18 +01001097 return 0;
1098
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001099 if (use_editor) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001100 struct strvec env = STRVEC_INIT;
Elia Pinto8d7aa4b2017-01-13 17:58:00 +00001101
Jeff King22f9b7f2020-07-28 16:24:27 -04001102 strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001103 if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
Stephan Beyer71982032008-07-25 18:28:42 +02001104 fprintf(stderr,
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001105 _("Please supply the message using either -m or -F option.\n"));
Stephan Beyer71982032008-07-25 18:28:42 +02001106 exit(1);
1107 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001108 strvec_clear(&env);
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001109 }
1110
1111 if (!no_verify &&
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001112 run_commit_hook(use_editor, index_file, NULL, "commit-msg",
1113 git_path_commit_editmsg(), NULL)) {
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001114 return 0;
1115 }
1116
1117 return 1;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001118}
1119
Junio C Hamano146ea062008-08-26 23:13:13 -07001120static const char *find_author_by_nickname(const char *name)
1121{
1122 struct rev_info revs;
1123 struct commit *commit;
1124 struct strbuf buf = STRBUF_INIT;
1125 const char *av[20];
1126 int ac = 0;
1127
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001128 repo_init_revisions(the_repository, &revs, NULL);
Junio C Hamano146ea062008-08-26 23:13:13 -07001129 strbuf_addf(&buf, "--author=%s", name);
1130 av[++ac] = "--all";
1131 av[++ac] = "-i";
1132 av[++ac] = buf.buf;
1133 av[++ac] = NULL;
1134 setup_revisions(ac, av, &revs, NULL);
Ævar Arnfjörð Bjarmasona52f07a2022-04-13 22:01:46 +02001135 revs.mailmap = xmalloc(sizeof(struct string_list));
1136 string_list_init_nodup(revs.mailmap);
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +01001137 read_mailmap(revs.mailmap);
Antoine Pelisseea167942013-08-23 15:48:31 +02001138
Stefan Beller81c3ce32014-08-10 23:33:26 +02001139 if (prepare_revision_walk(&revs))
1140 die(_("revision walk setup failed"));
Junio C Hamano146ea062008-08-26 23:13:13 -07001141 commit = get_revision(&revs);
1142 if (commit) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001143 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001144 ctx.date_mode.type = DATE_NORMAL;
Junio C Hamano146ea062008-08-26 23:13:13 -07001145 strbuf_release(&buf);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +02001146 repo_format_commit_message(the_repository, commit,
1147 "%aN <%aE>", &buf, &ctx);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001148 release_revisions(&revs);
Junio C Hamano146ea062008-08-26 23:13:13 -07001149 return strbuf_detach(&buf, NULL);
1150 }
Michael J Gruber1044b1f2015-01-26 16:48:33 +01001151 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
Junio C Hamano146ea062008-08-26 23:13:13 -07001152}
1153
Jameson Millereec0f7f2017-10-30 13:21:37 -04001154static void handle_ignored_arg(struct wt_status *s)
1155{
1156 if (!ignored_arg)
1157 ; /* default already initialized */
1158 else if (!strcmp(ignored_arg, "traditional"))
1159 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1160 else if (!strcmp(ignored_arg, "no"))
1161 s->show_ignored_mode = SHOW_NO_IGNORED;
1162 else if (!strcmp(ignored_arg, "matching"))
1163 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1164 else
1165 die(_("Invalid ignored mode '%s'"), ignored_arg);
1166}
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001167
Junio C Hamano63acdc42024-03-13 10:32:13 -07001168static enum untracked_status_type parse_untracked_setting_name(const char *u)
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001169{
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07001170 /*
1171 * Please update $__git_untracked_file_modes in
1172 * git-completion.bash when you add new options
1173 */
Junio C Hamanof66e1a02024-03-13 10:32:14 -07001174 switch (git_parse_maybe_bool(u)) {
1175 case 0:
1176 u = "no";
1177 break;
1178 case 1:
1179 u = "normal";
1180 break;
1181 default:
1182 break;
1183 }
1184
Junio C Hamano63acdc42024-03-13 10:32:13 -07001185 if (!strcmp(u, "no"))
1186 return SHOW_NO_UNTRACKED_FILES;
1187 else if (!strcmp(u, "normal"))
1188 return SHOW_NORMAL_UNTRACKED_FILES;
1189 else if (!strcmp(u, "all"))
1190 return SHOW_ALL_UNTRACKED_FILES;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001191 else
Junio C Hamano63acdc42024-03-13 10:32:13 -07001192 return SHOW_UNTRACKED_FILES_ERROR;
1193}
1194
1195static void handle_untracked_files_arg(struct wt_status *s)
1196{
1197 enum untracked_status_type u;
1198
1199 if (!untracked_files_arg)
1200 return; /* default already initialized */
1201
1202 u = parse_untracked_setting_name(untracked_files_arg);
1203 if (u == SHOW_UNTRACKED_FILES_ERROR)
1204 die(_("Invalid untracked files mode '%s'"),
1205 untracked_files_arg);
1206 s->show_untracked_files = u;
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001207}
1208
Jay Soffian37f7a852011-02-19 23:12:29 -05001209static const char *read_commit_message(const char *name)
1210{
Jeff Kingdd0d3882013-01-26 04:44:06 -05001211 const char *out_enc;
Jay Soffian37f7a852011-02-19 23:12:29 -05001212 struct commit *commit;
1213
1214 commit = lookup_commit_reference_by_name(name);
1215 if (!commit)
Teng Longe4cf0132023-05-29 21:27:56 +08001216 die(_("could not lookup commit '%s'"), name);
Jay Soffian37f7a852011-02-19 23:12:29 -05001217 out_enc = get_commit_output_encoding();
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001218 return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
Jay Soffian37f7a852011-02-19 23:12:29 -05001219}
1220
Junio C Hamano84b42022013-06-24 11:41:40 -07001221/*
1222 * Enumerate what needs to be propagated when --porcelain
1223 * is not in effect here.
1224 */
1225static struct status_deferred_config {
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001226 enum wt_status_format status_format;
Junio C Hamano84b42022013-06-24 11:41:40 -07001227 int show_branch;
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001228 enum ahead_behind_flags ahead_behind;
Junio C Hamano84b42022013-06-24 11:41:40 -07001229} status_deferred_config = {
1230 STATUS_FORMAT_UNSPECIFIED,
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001231 -1, /* unspecified */
1232 AHEAD_BEHIND_UNSPECIFIED,
Junio C Hamano84b42022013-06-24 11:41:40 -07001233};
1234
1235static void finalize_deferred_config(struct wt_status *s)
1236{
1237 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04001238 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
Junio C Hamano84b42022013-06-24 11:41:40 -07001239 !s->null_termination);
1240
1241 if (s->null_termination) {
1242 if (status_format == STATUS_FORMAT_NONE ||
1243 status_format == STATUS_FORMAT_UNSPECIFIED)
1244 status_format = STATUS_FORMAT_PORCELAIN;
1245 else if (status_format == STATUS_FORMAT_LONG)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001246 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
Junio C Hamano84b42022013-06-24 11:41:40 -07001247 }
1248
1249 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1250 status_format = status_deferred_config.status_format;
1251 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1252 status_format = STATUS_FORMAT_NONE;
1253
1254 if (use_deferred_config && s->show_branch < 0)
1255 s->show_branch = status_deferred_config.show_branch;
1256 if (s->show_branch < 0)
1257 s->show_branch = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001258
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001259 /*
1260 * If the user did not give a "--[no]-ahead-behind" command
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001261 * line argument *AND* we will print in a human-readable format
1262 * (short, long etc.) then we inherit from the status.aheadbehind
1263 * config setting. In all other cases (and porcelain V[12] formats
1264 * in particular), we inherit _FULL for backwards compatibility.
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001265 */
Jeff Hostetlerfb4db1a2019-06-18 13:21:28 -07001266 if (use_deferred_config &&
1267 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001268 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1269
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001270 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1271 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
Junio C Hamano84b42022013-06-24 11:41:40 -07001272}
1273
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301274static void check_fixup_reword_options(int argc, const char *argv[]) {
1275 if (whence != FROM_COMMIT) {
1276 if (whence == FROM_MERGE)
1277 die(_("You are in the middle of a merge -- cannot reword."));
1278 else if (is_from_cherry_pick(whence))
1279 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1280 }
1281 if (argc)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001282 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv);
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301283 if (patch_interactive || interactive || all || also || only)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001284 die(_("reword option of '%s' and '%s' cannot be used together"),
1285 "--fixup", "--patch/--interactive/--all/--include/--only");
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301286}
1287
Shawn Bohrer2f02b252007-12-02 23:02:09 -06001288static int parse_and_validate_options(int argc, const char *argv[],
Jeff King036dbbf2012-05-07 15:18:26 -04001289 const struct option *options,
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -07001290 const char * const usage[],
Junio C Hamanod249b092009-08-09 21:59:30 -07001291 const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001292 struct commit *current_head,
Junio C Hamanod249b092009-08-09 21:59:30 -07001293 struct wt_status *s)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001294{
Jeff King036dbbf2012-05-07 15:18:26 -04001295 argc = parse_options(argc, argv, prefix, options, usage, 0);
Junio C Hamano84b42022013-06-24 11:41:40 -07001296 finalize_deferred_config(s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001297
Junio C Hamano146ea062008-08-26 23:13:13 -07001298 if (force_author && !strchr(force_author, '>'))
1299 force_author = find_author_by_nickname(force_author);
1300
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001301 if (force_author && renew_authorship)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001302 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
Erick Mattosc51f6ce2009-11-04 01:20:11 -02001303
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301304 if (logfile || have_option_m || use_message)
Junio C Hamano48034662007-12-22 19:25:37 -08001305 use_editor = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001306
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001307 /* Sanity check options */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001308 if (amend && !current_head)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001309 die(_("You have nothing to amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001310 if (amend && whence != FROM_COMMIT) {
1311 if (whence == FROM_MERGE)
1312 die(_("You are in the middle of a merge -- cannot amend."));
Phillip Wood8d57f752019-12-06 16:06:10 +00001313 else if (is_from_cherry_pick(whence))
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001314 die(_("You are in the middle of a cherry-pick -- cannot amend."));
Phillip Wood430b75f2019-12-06 16:06:12 +00001315 else if (whence == FROM_REBASE_PICK)
1316 die(_("You are in the middle of a rebase -- cannot amend."));
Ævar Arnfjörð Bjarmasonb0cea472012-04-30 15:33:13 +00001317 }
Pat Notz89ac1222010-11-02 13:59:11 -06001318 if (fixup_message && squash_message)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001319 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1320 die_for_incompatible_opt4(!!use_message, "-C",
1321 !!edit_message, "-c",
1322 !!logfile, "-F",
1323 !!fixup_message, "--fixup");
1324 die_for_incompatible_opt4(have_option_m, "-m",
1325 !!edit_message, "-c",
1326 !!use_message, "-C",
1327 !!logfile, "-F");
1328 if (use_message || edit_message || logfile ||fixup_message || have_option_m)
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001329 FREE_AND_NULL(template_file);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001330 if (edit_message)
1331 use_message = edit_message;
Pat Notzd71b8ba2010-11-02 13:59:09 -06001332 if (amend && !use_message && !fixup_message)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001333 use_message = "HEAD";
Phillip Wood430b75f2019-12-06 16:06:12 +00001334 if (!use_message && !is_from_cherry_pick(whence) &&
1335 !is_from_rebase(whence) && renew_authorship)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001336 die(_("--reset-author can be used only with -C, -c or --amend."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001337 if (use_message) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001338 use_message_buffer = read_commit_message(use_message);
1339 if (!renew_authorship) {
1340 author_message = use_message;
1341 author_message_buffer = use_message_buffer;
1342 }
1343 }
Phillip Wood430b75f2019-12-06 16:06:12 +00001344 if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) &&
1345 !renew_authorship) {
Jay Soffian37f7a852011-02-19 23:12:29 -05001346 author_message = "CHERRY_PICK_HEAD";
1347 author_message_buffer = read_commit_message(author_message);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001348 }
1349
Conrad Irwinb4bd4662011-05-07 10:58:07 -07001350 if (patch_interactive)
1351 interactive = 1;
1352
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001353 die_for_incompatible_opt4(also, "-i/--include",
1354 only, "-o/--only",
1355 all, "-a/--all",
1356 interactive, "--interactive/-p/--patch");
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301357 if (fixup_message) {
1358 /*
1359 * We limit --fixup's suboptions to only alpha characters.
1360 * If the first character after a run of alpha is colon,
1361 * then the part before the colon may be a known suboption
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301362 * name like `amend` or `reword`, or a misspelt suboption
1363 * name. In either case, we treat it as
1364 * --fixup=<suboption>:<arg>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301365 *
1366 * Otherwise, we are dealing with --fixup=<commit>.
1367 */
1368 char *p = fixup_message;
1369 while (isalpha(*p))
1370 p++;
1371 if (p > fixup_message && *p == ':') {
1372 *p = '\0';
1373 fixup_commit = p + 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301374 if (!strcmp("amend", fixup_message) ||
1375 !strcmp("reword", fixup_message)) {
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301376 fixup_prefix = "amend";
1377 allow_empty = 1;
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301378 if (*fixup_message == 'r') {
1379 check_fixup_reword_options(argc, argv);
1380 only = 1;
1381 }
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301382 } else {
1383 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1384 }
1385 } else {
1386 fixup_commit = fixup_message;
1387 fixup_prefix = "fixup";
1388 use_editor = 0;
1389 }
1390 }
1391
Joel Klinghed8ef6aad2021-08-14 21:40:30 +00001392 if (0 <= edit_flag)
1393 use_editor = edit_flag;
1394
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001395 handle_untracked_files_arg(s);
Marius Storm-Olsen4bfee302008-06-05 10:31:19 +02001396
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001397 if (all && argc > 0)
Nguyễn Thái Ngọc Duy5a1dbd42019-03-20 17:29:06 +07001398 die(_("paths '%s ...' with -a does not make sense"),
1399 argv[0]);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001400
Jeff Kingf3f47a12012-10-18 21:15:50 +07001401 if (status_format != STATUS_FORMAT_NONE)
Jeff King7c9f7032009-09-05 04:59:56 -04001402 dry_run = 1;
1403
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001404 return argc;
1405}
1406
Jeff Kinge885a842020-09-30 08:28:18 -04001407static int dry_run_commit(const char **argv, const char *prefix,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001408 const struct commit *current_head, struct wt_status *s)
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001409{
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001410 int committable;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001411 const char *index_file;
1412
Jeff Kinge885a842020-09-30 08:28:18 -04001413 index_file = prepare_index(argv, prefix, current_head, 1);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001414 committable = run_status(stdout, index_file, prefix, 0, s);
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001415 rollback_index_files();
1416
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001417 return committable ? 0 : 1;
Junio C Hamano3a5d13a2009-08-07 23:03:36 -07001418}
1419
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02001420define_list_config_array_extra(color_status_slots, {"added"});
1421
Jonathan Nieder88521172014-10-07 15:16:57 -04001422static int parse_status_slot(const char *slot)
Junio C Hamanof766b362009-08-09 23:12:19 -07001423{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001424 if (!strcasecmp(slot, "added"))
Junio C Hamanof766b362009-08-09 23:12:19 -07001425 return WT_STATUS_UPDATED;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02001426
1427 return LOOKUP_CONFIG(color_status_slots, slot);
Junio C Hamanof766b362009-08-09 23:12:19 -07001428}
1429
Glen Chooa4e7e312023-06-28 19:26:22 +00001430static int git_status_config(const char *k, const char *v,
1431 const struct config_context *ctx, void *cb)
Junio C Hamanof766b362009-08-09 23:12:19 -07001432{
1433 struct wt_status *s = cb;
René Scharfee3f1da92014-10-04 20:54:50 +02001434 const char *slot_name;
Junio C Hamanof766b362009-08-09 23:12:19 -07001435
Christian Couder59556542013-11-30 21:55:40 +01001436 if (starts_with(k, "column."))
Jeff King4d2292e2012-05-07 15:35:03 -04001437 return git_column_config(k, v, "status", &s->colopts);
Junio C Hamanof766b362009-08-09 23:12:19 -07001438 if (!strcmp(k, "status.submodulesummary")) {
1439 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001440 s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi,
1441 &is_bool);
Junio C Hamanof766b362009-08-09 23:12:19 -07001442 if (is_bool && s->submodule_summary)
1443 s->submodule_summary = -1;
1444 return 0;
1445 }
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001446 if (!strcmp(k, "status.short")) {
1447 if (git_config_bool(k, v))
Junio C Hamano84b42022013-06-24 11:41:40 -07001448 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001449 else
Junio C Hamano84b42022013-06-24 11:41:40 -07001450 status_deferred_config.status_format = STATUS_FORMAT_NONE;
Jorge Juan Garcia Garcia4fb51662013-06-11 15:34:04 +02001451 return 0;
1452 }
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001453 if (!strcmp(k, "status.branch")) {
Junio C Hamano84b42022013-06-24 11:41:40 -07001454 status_deferred_config.show_branch = git_config_bool(k, v);
Jorge Juan Garcia Garciaec85d072013-06-11 15:34:05 +02001455 return 0;
1456 }
Jeff Hostetler06b324c2019-06-18 13:21:25 -07001457 if (!strcmp(k, "status.aheadbehind")) {
1458 status_deferred_config.ahead_behind = git_config_bool(k, v);
1459 return 0;
1460 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001461 if (!strcmp(k, "status.showstash")) {
1462 s->show_stash = git_config_bool(k, v);
1463 return 0;
1464 }
Junio C Hamanof766b362009-08-09 23:12:19 -07001465 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kinge269eb72011-08-17 22:03:48 -07001466 s->use_color = git_config_colorbool(k, v);
Junio C Hamanof766b362009-08-09 23:12:19 -07001467 return 0;
1468 }
Matthieu Moy2556b992013-09-06 19:43:07 +02001469 if (!strcmp(k, "status.displaycommentprefix")) {
1470 s->display_comment_prefix = git_config_bool(k, v);
1471 return 0;
1472 }
René Scharfee3f1da92014-10-04 20:54:50 +02001473 if (skip_prefix(k, "status.color.", &slot_name) ||
1474 skip_prefix(k, "color.status.", &slot_name)) {
Junio C Hamanob9465762014-10-20 12:23:48 -07001475 int slot = parse_status_slot(slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -05001476 if (slot < 0)
1477 return 0;
Junio C Hamanof766b362009-08-09 23:12:19 -07001478 if (!v)
1479 return config_error_nonbool(k);
Jeff Kingf6c5a292014-10-07 15:33:09 -04001480 return color_parse(v, s->color_palette[slot]);
Junio C Hamanof766b362009-08-09 23:12:19 -07001481 }
1482 if (!strcmp(k, "status.relativepaths")) {
1483 s->relative_paths = git_config_bool(k, v);
1484 return 0;
1485 }
1486 if (!strcmp(k, "status.showuntrackedfiles")) {
Junio C Hamano63acdc42024-03-13 10:32:13 -07001487 enum untracked_status_type u;
1488
Junio C Hamano63acdc42024-03-13 10:32:13 -07001489 u = parse_untracked_setting_name(v);
1490 if (u == SHOW_UNTRACKED_FILES_ERROR)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001491 return error(_("Invalid untracked files mode '%s'"), v);
Junio C Hamano63acdc42024-03-13 10:32:13 -07001492 s->show_untracked_files = u;
Junio C Hamanof766b362009-08-09 23:12:19 -07001493 return 0;
1494 }
Ben Pearte8b2dc22018-05-11 15:38:58 +00001495 if (!strcmp(k, "diff.renamelimit")) {
1496 if (s->rename_limit == -1)
Glen Choo8868b1e2023-06-28 19:26:27 +00001497 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001498 return 0;
1499 }
1500 if (!strcmp(k, "status.renamelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001501 s->rename_limit = git_config_int(k, v, ctx->kvi);
Ben Pearte8b2dc22018-05-11 15:38:58 +00001502 return 0;
1503 }
1504 if (!strcmp(k, "diff.renames")) {
1505 if (s->detect_rename == -1)
1506 s->detect_rename = git_config_rename(k, v);
1507 return 0;
1508 }
1509 if (!strcmp(k, "status.renames")) {
1510 s->detect_rename = git_config_rename(k, v);
1511 return 0;
1512 }
Glen Chooa4e7e312023-06-28 19:26:22 +00001513 return git_diff_ui_config(k, v, ctx, NULL);
Junio C Hamanof766b362009-08-09 23:12:19 -07001514}
1515
John Cai9b1cb502024-09-13 21:16:14 +00001516int cmd_status(int argc,
1517const char **argv,
1518const char *prefix,
1519struct repository *repo UNUSED)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001520{
Ben Pearte8b2dc22018-05-11 15:38:58 +00001521 static int no_renames = -1;
1522 static const char *rename_score_arg = (const char *)-1;
Jeff King036dbbf2012-05-07 15:18:26 -04001523 static struct wt_status s;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001524 unsigned int progress_flag = 0;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001525 int fd;
brian m. carlson8066df42017-02-20 00:10:14 +00001526 struct object_id oid;
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001527 static struct option builtin_status_options[] = {
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001528 OPT__VERBOSE(&verbose, N_("be verbose")),
Jeff Kingdd2be242009-09-05 04:54:14 -04001529 OPT_SET_INT('s', "short", &status_format,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001530 N_("show status concisely"), STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001531 OPT_BOOL('b', "branch", &s.show_branch,
1532 N_("show branch information")),
Liam Beguinc1b5d012017-06-17 18:30:51 -04001533 OPT_BOOL(0, "show-stash", &s.show_stash,
1534 N_("show stash information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001535 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1536 N_("compute full ahead/behind values")),
Denton Liu203c8532020-04-28 04:36:28 -04001537 OPT_CALLBACK_F(0, "porcelain", &status_format,
Jeff Hostetlerc4f596b2016-08-05 18:00:28 -04001538 N_("version"), N_("machine-readable output"),
Denton Liu203c8532020-04-28 04:36:28 -04001539 PARSE_OPT_OPTARG, opt_parse_porcelain),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001540 OPT_SET_INT(0, "long", &status_format,
1541 N_("show status in long format (default)"),
1542 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001543 OPT_BOOL('z', "null", &s.null_termination,
1544 N_("terminate entries with NUL")),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001545 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001546 N_("mode"),
1547 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001548 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Jameson Millereec0f7f2017-10-30 13:21:37 -04001549 { OPTION_STRING, 0, "ignored", &ignored_arg,
1550 N_("mode"),
1551 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1552 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
Nguyễn Thái Ngọc Duyf2276312012-08-20 19:32:37 +07001553 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1554 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
Jens Lehmann46a958b2010-06-25 16:56:47 +02001555 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001556 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
Ben Pearte8b2dc22018-05-11 15:38:58 +00001557 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
Denton Liu203c8532020-04-28 04:36:28 -04001558 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg,
Ben Pearte8b2dc22018-05-11 15:38:58 +00001559 N_("n"), N_("detect renames, optionally set similarity index"),
Denton Liu203c8532020-04-28 04:36:28 -04001560 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score),
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001561 OPT_END(),
1562 };
1563
Junio C Hamanob821c992025-01-16 13:35:51 -08001564 show_usage_with_options_if_asked(argc, argv,
1565 builtin_status_usage, builtin_status_options);
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001566
Derrick Stoleed76723e2021-07-14 13:12:37 +00001567 prepare_repo_settings(the_repository);
1568 the_repository->settings.command_requires_full_index = 0;
1569
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001570 status_init_config(&s, git_status_config);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001571 argc = parse_options(argc, argv, prefix,
Junio C Hamano9e4b7ab2009-08-15 02:27:39 -07001572 builtin_status_options,
1573 builtin_status_usage, 0);
Jeff King4d2292e2012-05-07 15:35:03 -04001574 finalize_colopts(&s.colopts, -1);
Junio C Hamano84b42022013-06-24 11:41:40 -07001575 finalize_deferred_config(&s);
Junio C Hamano6c929722011-06-06 11:40:08 -07001576
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001577 handle_untracked_files_arg(&s);
Jameson Millereec0f7f2017-10-30 13:21:37 -04001578 handle_ignored_arg(&s);
1579
1580 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1581 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1582 die(_("Unsupported combination of ignored and untracked-files arguments"));
1583
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +07001584 parse_pathspec(&s.pathspec, 0,
1585 PATHSPEC_PREFER_FULL,
1586 prefix, argv);
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001587
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001588 if (status_format != STATUS_FORMAT_PORCELAIN &&
1589 status_format != STATUS_FORMAT_PORCELAIN_V2)
1590 progress_flag = REFRESH_PROGRESS;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001591 repo_read_index(the_repository);
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001592 refresh_index(the_repository->index,
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001593 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1594 &s.pathspec, NULL, NULL);
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001595
Jeff King27344d62017-09-27 02:54:30 -04001596 if (use_optional_locks())
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001597 fd = repo_hold_locked_index(the_repository, &index_lock, 0);
Jeff King27344d62017-09-27 02:54:30 -04001598 else
1599 fd = -1;
Markus Heidelberg4bb66442010-04-02 23:44:21 +02001600
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001601 s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001602 if (!s.is_initial)
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00001603 oidcpy(&s.oid_commit, &oid);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001604
Jens Lehmann46a958b2010-06-25 16:56:47 +02001605 s.ignore_submodule_arg = ignore_submodule_arg;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001606 s.status_format = status_format;
1607 s.verbose = verbose;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001608 if (no_renames != -1)
1609 s.detect_rename = !no_renames;
1610 if ((intptr_t)rename_score_arg != -1) {
1611 if (s.detect_rename < DIFF_DETECT_RENAME)
1612 s.detect_rename = DIFF_DETECT_RENAME;
1613 if (rename_score_arg)
1614 s.rename_score = parse_rename_score(&rename_score_arg);
1615 }
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001616
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001617 wt_status_collect(&s);
1618
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001619 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07001620 repo_update_index_if_able(the_repository, &index_lock);
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +07001621
Jeff King86617682009-12-07 00:26:25 -05001622 if (s.relative_paths)
1623 s.prefix = prefix;
Markus Heidelberg38920dd2009-01-08 19:53:05 +01001624
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001625 wt_status_print(&s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001626 wt_status_collect_free_buffers(&s);
1627
Junio C Hamano76e2f7c2009-08-07 23:31:57 -07001628 return 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001629}
1630
Glen Chooa4e7e312023-06-28 19:26:22 +00001631static int git_commit_config(const char *k, const char *v,
1632 const struct config_context *ctx, void *cb)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001633{
Junio C Hamanod249b092009-08-09 21:59:30 -07001634 struct wt_status *s = cb;
1635
Brian Hetro984c6e72008-07-05 01:24:40 -04001636 if (!strcmp(k, "commit.template"))
Matthieu Moy395de252009-11-17 18:24:25 +01001637 return git_config_pathname(&template_file, k, v);
James P. Howard, IIbed575e2009-12-07 17:45:27 -05001638 if (!strcmp(k, "commit.status")) {
1639 include_status = git_config_bool(k, v);
1640 return 0;
1641 }
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001642 if (!strcmp(k, "commit.cleanup")) {
1643 FREE_AND_NULL(cleanup_config);
1644 return git_config_string(&cleanup_config, k, v);
1645 }
Nicolas Vigierd95bfb12013-11-05 00:14:41 +01001646 if (!strcmp(k, "commit.gpgsign")) {
1647 sign_commit = git_config_bool(k, v) ? "" : NULL;
1648 return 0;
1649 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301650 if (!strcmp(k, "commit.verbose")) {
1651 int is_bool;
Glen Choo8868b1e2023-06-28 19:26:27 +00001652 config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi,
1653 &is_bool);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301654 return 0;
1655 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001656
Glen Chooa4e7e312023-06-28 19:26:22 +00001657 return git_status_config(k, v, ctx, s);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001658}
1659
John Cai9b1cb502024-09-13 21:16:14 +00001660int cmd_commit(int argc,
1661 const char **argv,
1662 const char *prefix,
1663 struct repository *repo UNUSED)
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001664{
Jeff King036dbbf2012-05-07 15:18:26 -04001665 static struct wt_status s;
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001666 static const char *cleanup_arg = NULL;
Jeff King036dbbf2012-05-07 15:18:26 -04001667 static struct option builtin_commit_options[] = {
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001668 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1669 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
Jeff King036dbbf2012-05-07 15:18:26 -04001670
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001671 OPT_GROUP(N_("Commit message options")),
1672 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1673 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1674 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1675 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1676 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1677 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301678 /*
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301679 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1680 * and only translate <commit>.
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301681 */
Charvi Mendiratta3270ae82021-03-15 13:24:33 +05301682 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 +07001683 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001684 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 +00001685 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 -07001686 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001687 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1688 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
Denton Liuca04dc92019-04-17 11:23:26 +01001689 OPT_CLEANUP(&cleanup_arg),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001690 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
Junio C Hamanoe703d712014-03-23 15:58:12 -07001691 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001692 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King036dbbf2012-05-07 15:18:26 -04001693 /* end commit message options */
1694
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001695 OPT_GROUP(N_("Commit contents options")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001696 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1697 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1698 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1699 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1700 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
Orgad Shanehdef480f2016-07-26 17:00:15 +03001701 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001702 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 +07001703 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
Jeff King036dbbf2012-05-07 15:18:26 -04001704 STATUS_FORMAT_SHORT),
Junio C Hamano84b42022013-06-24 11:41:40 -07001705 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001706 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1707 N_("compute full ahead/behind values")),
Jeff King036dbbf2012-05-07 15:18:26 -04001708 OPT_SET_INT(0, "porcelain", &status_format,
Nguyễn Thái Ngọc Duy9c23f4c2012-08-20 19:32:04 +07001709 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
Jeff Kingf3f47a12012-10-18 21:15:50 +07001710 OPT_SET_INT(0, "long", &status_format,
1711 N_("show status in long format (default)"),
1712 STATUS_FORMAT_LONG),
Stefan Bellerd5d09d42013-08-03 13:51:19 +02001713 OPT_BOOL('z', "null", &s.null_termination,
1714 N_("terminate entries with NUL")),
1715 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1716 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 +07001717 { 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 +00001718 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1719 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Jeff King036dbbf2012-05-07 15:18:26 -04001720 /* end commit contents options */
1721
Stefan Beller4741edd2013-08-03 13:51:18 +02001722 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1723 N_("ok to record an empty change")),
1724 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1725 N_("ok to record a change with an empty message")),
Jeff King036dbbf2012-05-07 15:18:26 -04001726
1727 OPT_END()
1728 };
1729
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001730 struct strbuf sb = STRBUF_INIT;
Junio C Hamano4c28e4a2010-12-20 17:00:36 -08001731 struct strbuf author_ident = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001732 const char *index_file, *reflog_msg;
brian m. carlson8066df42017-02-20 00:10:14 +00001733 struct object_id oid;
René Scharfede9f7fa2016-10-29 14:55:36 +02001734 struct commit_list *parents = NULL;
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001735 struct stat statbuf;
Junio C Hamano06bb6432011-08-19 11:58:18 -07001736 struct commit *current_head = NULL;
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001737 struct commit_extra_header *extra = NULL;
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001738 struct strbuf err = STRBUF_INIT;
Junio C Hamano00d8c312022-05-12 15:51:07 -07001739 int ret = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001740
Junio C Hamanob821c992025-01-16 13:35:51 -08001741 show_usage_with_options_if_asked(argc, argv,
1742 builtin_commit_usage, builtin_commit_options);
Nguyễn Thái Ngọc Duy5d3dd912010-10-22 01:45:47 -05001743
Derrick Stoleedaa1ace2021-06-29 02:13:04 +00001744 prepare_repo_settings(the_repository);
1745 the_repository->settings.command_requires_full_index = 0;
1746
Matthieu Moy5c25dfa2013-09-12 12:50:04 +02001747 status_init_config(&s, git_commit_config);
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301748 s.commit_template = 1;
Ramkumar Ramachandraf0915cb2013-06-24 18:15:12 +05301749 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
Jeff King4d2292e2012-05-07 15:35:03 -04001750 s.colopts = 0;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001751
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001752 if (repo_get_oid(the_repository, "HEAD", &oid))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001753 current_head = NULL;
1754 else {
brian m. carlsonbc832662017-05-06 22:10:10 +00001755 current_head = lookup_commit_or_die(&oid, "HEAD");
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001756 if (repo_parse_commit(the_repository, current_head))
Junio C Hamano06bb6432011-08-19 11:58:18 -07001757 die(_("could not parse HEAD commit"));
1758 }
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301759 verbose = -1; /* unspecified */
Jeff King036dbbf2012-05-07 15:18:26 -04001760 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1761 builtin_commit_usage,
Junio C Hamano06bb6432011-08-19 11:58:18 -07001762 prefix, current_head, &s);
Pranit Bauvaaaab8422016-05-05 15:20:02 +05301763 if (verbose == -1)
1764 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1765
Patrick Steinhardt6ef9f772024-11-05 07:17:20 +01001766 if (cleanup_arg) {
1767 free(cleanup_config);
1768 cleanup_config = xstrdup(cleanup_arg);
1769 }
1770 cleanup_mode = get_cleanup_mode(cleanup_config, use_editor);
1771
Jeff Kingc9bfb952011-08-17 22:05:35 -07001772 if (dry_run)
Jeff Kinge885a842020-09-30 08:28:18 -04001773 return dry_run_commit(argv, prefix, current_head, &s);
1774 index_file = prepare_index(argv, prefix, current_head, 0);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001775
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001776 /* Set up everything for writing the commit object. This includes
1777 running hooks, writing the trees, and interacting with the user. */
Junio C Hamano06bb6432011-08-19 11:58:18 -07001778 if (!prepare_to_commit(index_file, prefix,
1779 current_head, &s, &author_ident)) {
Junio C Hamano00d8c312022-05-12 15:51:07 -07001780 ret = 1;
Junio C Hamano28886052007-11-18 01:52:55 -08001781 rollback_index_files();
Junio C Hamano00d8c312022-05-12 15:51:07 -07001782 goto cleanup;
Junio C Hamano28886052007-11-18 01:52:55 -08001783 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001784
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001785 /* Determine parents */
Christian Couder643cb5f2010-06-12 18:05:12 +02001786 reflog_msg = getenv("GIT_REFLOG_ACTION");
Junio C Hamano06bb6432011-08-19 11:58:18 -07001787 if (!current_head) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001788 if (!reflog_msg)
1789 reflog_msg = "commit (initial)";
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001790 } else if (amend) {
Christian Couder643cb5f2010-06-12 18:05:12 +02001791 if (!reflog_msg)
1792 reflog_msg = "commit (amend)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001793 parents = copy_commit_list(current_head->parents);
Jay Soffian37f7a852011-02-19 23:12:29 -05001794 } else if (whence == FROM_MERGE) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001795 struct strbuf m = STRBUF_INIT;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001796 FILE *fp;
Elia Pintoe23fd152014-01-30 07:15:56 -08001797 int allow_fast_forward = 1;
René Scharfede9f7fa2016-10-29 14:55:36 +02001798 struct commit_list **pptr = &parents;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001799
Christian Couder643cb5f2010-06-12 18:05:12 +02001800 if (!reflog_msg)
1801 reflog_msg = "commit (merge)";
René Scharfede9f7fa2016-10-29 14:55:36 +02001802 pptr = commit_list_append(current_head, pptr);
Stefan Beller102de882018-05-17 15:51:51 -07001803 fp = xfopen(git_path_merge_head(the_repository), "r");
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001804 while (strbuf_getline_lf(&m, fp) != EOF) {
Junio C Hamano5231c632011-11-07 16:21:32 -08001805 struct commit *parent;
1806
1807 parent = get_merge_parent(m.buf);
1808 if (!parent)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001809 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
René Scharfede9f7fa2016-10-29 14:55:36 +02001810 pptr = commit_list_append(parent, pptr);
Linus Torvalds7c3fd252008-01-15 16:12:33 -08001811 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001812 fclose(fp);
1813 strbuf_release(&m);
Stefan Beller102de882018-05-17 15:51:51 -07001814 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1815 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001816 die_errno(_("could not read MERGE_MODE"));
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001817 if (!strcmp(sb.buf, "no-ff"))
1818 allow_fast_forward = 0;
1819 }
1820 if (allow_fast_forward)
Martin Ågren4da72642017-11-07 21:39:45 +01001821 reduce_heads_replace(&parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001822 } else {
Christian Couder643cb5f2010-06-12 18:05:12 +02001823 if (!reflog_msg)
Phillip Wood8d57f752019-12-06 16:06:10 +00001824 reflog_msg = is_from_cherry_pick(whence)
Jay Soffian37f7a852011-02-19 23:12:29 -05001825 ? "commit (cherry-pick)"
Phillip Wood430b75f2019-12-06 16:06:12 +00001826 : is_from_rebase(whence)
1827 ? "commit (rebase)"
Jay Soffian37f7a852011-02-19 23:12:29 -05001828 : "commit";
René Scharfede9f7fa2016-10-29 14:55:36 +02001829 commit_list_insert(current_head, &parents);
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001830 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001831
Paolo Bonziniec84bd02008-02-05 11:01:46 +01001832 /* Finally, get the commit message */
Miklos Vajnacf10f9f2008-10-03 14:04:47 +02001833 strbuf_reset(&sb);
Pranit Bauvae51b0df2016-05-25 00:49:50 +05301834 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +02001835 int saved_errno = errno;
Junio C Hamano740001a2007-12-08 23:23:20 -08001836 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001837 die(_("could not read commit message: %s"), strerror(saved_errno));
Junio C Hamano740001a2007-12-08 23:23:20 -08001838 }
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001839
Denton Liuf29cd862019-04-17 11:23:25 +01001840 cleanup_message(&sb, cleanup_mode, verbose);
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301841
Phillip Woodd0aaa462017-11-10 11:09:42 +00001842 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
Junio C Hamano28886052007-11-18 01:52:55 -08001843 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001844 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
Jeff Kingfdc7c812008-07-31 03:36:09 -04001845 exit(1);
Junio C Hamano28886052007-11-18 01:52:55 -08001846 }
Phillip Woodd0aaa462017-11-10 11:09:42 +00001847 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
Kaartic Sivaraambc17f352017-07-17 21:06:15 +05301848 rollback_index_files();
1849 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1850 exit(1);
1851 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001852
Charvi Mendiratta494d3142021-03-15 13:24:32 +05301853 if (fixup_message && starts_with(sb.buf, "amend! ") &&
1854 !allow_empty_message) {
1855 struct strbuf body = STRBUF_INIT;
1856 size_t len = commit_subject_length(sb.buf);
1857 strbuf_addstr(&body, sb.buf + len);
1858 if (message_is_empty(&body, cleanup_mode)) {
1859 rollback_index_files();
1860 fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
1861 exit(1);
1862 }
1863 strbuf_release(&body);
1864 }
1865
Junio C Hamano0074d182011-12-20 13:20:56 -08001866 if (amend) {
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001867 const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001868 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Junio C Hamano0074d182011-12-20 13:20:56 -08001869 } else {
1870 struct commit_extra_header **tail = &extra;
1871 append_merge_tag_headers(parents, &tail);
1872 }
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001873
Patrick Steinhardtf59aa5e2024-04-18 14:14:14 +02001874 if (commit_tree_extended(sb.buf, sb.len, &the_repository->index->cache_tree->oid,
Phillip Woode8cbe212020-08-17 18:40:01 +01001875 parents, &oid, author_ident.buf, NULL,
1876 sign_commit, extra)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001877 rollback_index_files();
Ævar Arnfjörð Bjarmason8a6179b2011-02-22 23:41:44 +00001878 die(_("failed to write commit object"));
Junio C Hamano28886052007-11-18 01:52:55 -08001879 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001880
Phillip Wood0505d602017-11-17 11:34:47 +00001881 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1882 &err)) {
Junio C Hamano28886052007-11-18 01:52:55 -08001883 rollback_index_files();
Ronnie Sahlbergc0fe1ed2014-04-16 15:34:19 -07001884 die("%s", err.buf);
Junio C Hamano28886052007-11-18 01:52:55 -08001885 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001886
Junio C Hamanof496b062019-07-09 15:25:44 -07001887 sequencer_post_commit_cleanup(the_repository, 0);
Stefan Beller102de882018-05-17 15:51:51 -07001888 unlink(git_path_merge_head(the_repository));
1889 unlink(git_path_merge_msg(the_repository));
1890 unlink(git_path_merge_mode(the_repository));
1891 unlink(git_path_squash_msg(the_repository));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001892
Brandon Casey5a9dd392008-01-23 11:21:22 -06001893 if (commit_index_files())
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001894 die(_("repository has been updated, but unable to write\n"
Junio C Hamanoa0607052023-10-17 20:08:51 -07001895 "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 +07001896 "not exceeded, and then \"git restore --staged :/\" to recover."));
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001897
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001898 git_test_write_commit_graph_or_die();
Derrick Stolee859fdc02018-08-29 05:49:04 -07001899
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001900 repo_rerere(the_repository, 0);
Derrick Stoleea95ce122020-09-17 18:11:44 +00001901 run_auto_maintenance(quiet);
Patrick Steinhardt1dc4ec22024-09-12 13:29:32 +02001902 run_commit_hook(use_editor, repo_get_index_file(the_repository),
1903 NULL, "post-commit", NULL);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001904 if (amend && !no_post_rewrite) {
Nguyễn Thái Ngọc Duy1d18d752019-01-12 09:13:23 +07001905 commit_post_rewrite(the_repository, current_head, &oid);
Thomas Rast6f6bee32010-03-12 18:04:28 +01001906 }
Phillip Woode47c6ca2017-11-24 11:07:54 +00001907 if (!quiet) {
1908 unsigned int flags = 0;
1909
1910 if (!current_head)
1911 flags |= SUMMARY_INITIAL_COMMIT;
1912 if (author_date_is_interesting())
1913 flags |= SUMMARY_SHOW_AUTHOR_DATE;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +01001914 print_commit_summary(the_repository, prefix,
1915 &oid, flags);
Phillip Woode47c6ca2017-11-24 11:07:54 +00001916 }
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001917
Patrick Steinhardt3f921c72024-01-19 11:40:19 +01001918 apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
Denton Liua03b5552020-04-07 10:28:07 -04001919
Junio C Hamano00d8c312022-05-12 15:51:07 -07001920cleanup:
Patrick Steinhardt63c9bd32024-06-11 11:20:42 +02001921 free_commit_extra_headers(extra);
1922 free_commit_list(parents);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001923 strbuf_release(&author_ident);
1924 strbuf_release(&err);
1925 strbuf_release(&sb);
Patrick Steinhardt14da2622024-06-11 11:19:22 +02001926 free(logfile);
1927 free(template_file);
Junio C Hamano00d8c312022-05-12 15:51:07 -07001928 return ret;
Kristian Høgsbergf5bbc322007-11-08 11:59:00 -05001929}