Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1 | /* |
| 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 Steinhardt | 41f43b8 | 2024-12-06 11:27:19 +0100 | [diff] [blame] | 7 | |
John Cai | 03eae9a | 2024-09-13 21:16:15 +0000 | [diff] [blame] | 8 | #define USE_THE_REPOSITORY_VARIABLE |
Patrick Steinhardt | 41f43b8 | 2024-12-06 11:27:19 +0100 | [diff] [blame] | 9 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 10 | |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 11 | #include "builtin.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 12 | #include "advice.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 13 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 14 | #include "lockfile.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 15 | #include "cache-tree.h" |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 16 | #include "color.h" |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 17 | #include "dir.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 18 | #include "editor.h" |
Elijah Newren | 7ee24e1 | 2023-03-21 06:25:57 +0000 | [diff] [blame] | 19 | #include "environment.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 20 | #include "diff.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 21 | #include "commit.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 22 | #include "gettext.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 23 | #include "revision.h" |
| 24 | #include "wt-status.h" |
| 25 | #include "run-command.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 26 | #include "strbuf.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 27 | #include "object-name.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 28 | #include "parse-options.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 29 | #include "path.h" |
Elijah Newren | fbffdfb | 2023-05-16 06:33:52 +0000 | [diff] [blame] | 30 | #include "preload-index.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 31 | #include "read-cache.h" |
Patrick Steinhardt | 246deea | 2024-09-12 13:29:24 +0200 | [diff] [blame] | 32 | #include "repository.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 33 | #include "string-list.h" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 34 | #include "rerere.h" |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 35 | #include "unpack-trees.h" |
Nguyễn Thái Ngọc Duy | 323d053 | 2012-04-13 17:54:39 +0700 | [diff] [blame] | 36 | #include "column.h" |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 37 | #include "sequencer.h" |
Elijah Newren | baf889c | 2023-05-16 06:33:51 +0000 | [diff] [blame] | 38 | #include "sparse-index.h" |
Antoine Pelisse | ea16794 | 2013-08-23 15:48:31 +0200 | [diff] [blame] | 39 | #include "mailmap.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 40 | #include "help.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 41 | #include "commit-reach.h" |
Derrick Stolee | 859fdc0 | 2018-08-29 05:49:04 -0700 | [diff] [blame] | 42 | #include "commit-graph.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 43 | #include "pretty.h" |
John Passaro | 4a86187 | 2024-05-05 18:49:09 +0000 | [diff] [blame] | 44 | #include "trailer.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 45 | |
| 46 | static const char * const builtin_commit_usage[] = { |
Jean-Noël Avila | d533c10 | 2025-01-15 20:23:45 +0000 | [diff] [blame] | 47 | N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u[<mode>]] [--amend]\n" |
Tomas Nordin | 1c473dd | 2024-07-22 22:53:02 +0000 | [diff] [blame] | 48 | " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>]\n" |
Ævar Arnfjörð Bjarmason | 423be1f | 2022-10-13 17:39:23 +0200 | [diff] [blame] | 49 | " [-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øgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 55 | NULL |
| 56 | }; |
| 57 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 58 | static const char * const builtin_status_usage[] = { |
Ævar Arnfjörð Bjarmason | 463ea0c | 2022-10-13 17:39:21 +0200 | [diff] [blame] | 59 | N_("git status [<options>] [--] [<pathspec>...]"), |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 60 | NULL |
| 61 | }; |
| 62 | |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 63 | static const char empty_amend_advice[] = |
Ævar Arnfjörð Bjarmason | fc88e31 | 2011-02-22 23:41:49 +0000 | [diff] [blame] | 64 | N_("You asked to amend the most recent commit, but doing so would make\n" |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 65 | "it empty. You can repeat your command with --allow-empty, or you can\n" |
Ævar Arnfjörð Bjarmason | fc88e31 | 2011-02-22 23:41:49 +0000 | [diff] [blame] | 66 | "remove the commit entirely with \"git reset HEAD^\".\n"); |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 67 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 68 | static const char empty_cherry_pick_advice[] = |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 69 | N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n" |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 70 | "If you wish to commit it anyway, use:\n" |
| 71 | "\n" |
| 72 | " git commit --allow-empty\n" |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 73 | "\n"); |
| 74 | |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 75 | static const char empty_rebase_pick_advice[] = |
| 76 | N_("Otherwise, please use 'git rebase --skip'\n"); |
| 77 | |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 78 | static const char empty_cherry_pick_advice_single[] = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 79 | N_("Otherwise, please use 'git cherry-pick --skip'\n"); |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 80 | |
| 81 | static const char empty_cherry_pick_advice_multi[] = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 82 | N_("and then use:\n" |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 83 | "\n" |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 84 | " git cherry-pick --continue\n" |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 85 | "\n" |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 86 | "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 Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 91 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 92 | static 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 Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 104 | static const char *use_message_buffer; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 105 | static struct lock_file index_lock; /* real index */ |
| 106 | static struct lock_file false_lock; /* used only for partial commits */ |
| 107 | static enum { |
| 108 | COMMIT_AS_IS = 1, |
| 109 | COMMIT_NORMAL, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 110 | COMMIT_PARTIAL |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 111 | } commit_style; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 112 | |
Patrick Steinhardt | 14da262 | 2024-06-11 11:19:22 +0200 | [diff] [blame] | 113 | static const char *force_author; |
| 114 | static char *logfile; |
Patrick Steinhardt | 6073b3b | 2024-05-27 13:46:15 +0200 | [diff] [blame] | 115 | static char *template_file; |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 116 | /* |
| 117 | * The _message variables are commit names from which to take |
| 118 | * the commit message and/or authorship. |
| 119 | */ |
| 120 | static const char *author_message, *author_message_buffer; |
Patrick Steinhardt | b567004 | 2024-06-07 08:37:39 +0200 | [diff] [blame] | 121 | static const char *edit_message, *use_message; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 122 | static char *fixup_message, *fixup_commit, *squash_message; |
| 123 | static const char *fixup_prefix; |
Junio C Hamano | ca1ba20 | 2011-12-06 13:09:55 -0800 | [diff] [blame] | 124 | static int all, also, interactive, patch_interactive, only, amend, signoff; |
| 125 | static int edit_flag = -1; /* unspecified */ |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 126 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 127 | static int config_commit_verbose = -1; /* unspecified */ |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 128 | static int no_post_rewrite, allow_empty_message, pathspec_file_nul; |
Patrick Steinhardt | b567004 | 2024-06-07 08:37:39 +0200 | [diff] [blame] | 129 | static const char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg; |
| 130 | static const char *sign_commit, *pathspec_from_file; |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 131 | static struct strvec trailer_args = STRVEC_INIT; |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 132 | |
Alex Riesen | 5f06573 | 2007-12-22 19:46:24 +0100 | [diff] [blame] | 133 | /* |
| 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 Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 140 | static enum commit_msg_cleanup_mode cleanup_mode; |
Patrick Steinhardt | 6ef9f77 | 2024-11-05 07:17:20 +0100 | [diff] [blame] | 141 | static char *cleanup_config; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 142 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 143 | static enum commit_whence whence; |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 144 | static int use_editor = 1, include_status = 1; |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 145 | static int have_option_m; |
Jeff King | 2c47789 | 2011-12-18 00:03:22 -0500 | [diff] [blame] | 146 | static struct strbuf message = STRBUF_INIT; |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 147 | |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 148 | static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 149 | |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 150 | static 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 Hostetler | 1ecdecc | 2016-08-11 10:45:57 -0400 | [diff] [blame] | 159 | else if (!strcmp(arg, "v2") || !strcmp(arg, "2")) |
| 160 | *value = STATUS_FORMAT_PORCELAIN_V2; |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 161 | else |
| 162 | die("unsupported porcelain version '%s'", arg); |
| 163 | |
| 164 | return 0; |
| 165 | } |
Jeff King | 7c9f703 | 2009-09-05 04:59:56 -0400 | [diff] [blame] | 166 | |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 167 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
| 168 | { |
| 169 | struct strbuf *buf = opt->value; |
René Scharfe | 2520677 | 2013-05-25 23:43:34 +0200 | [diff] [blame] | 170 | if (unset) { |
| 171 | have_option_m = 0; |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 172 | strbuf_setlen(buf, 0); |
René Scharfe | 2520677 | 2013-05-25 23:43:34 +0200 | [diff] [blame] | 173 | } else { |
| 174 | have_option_m = 1; |
Brandon Casey | a24a41e | 2013-02-18 20:17:06 -0800 | [diff] [blame] | 175 | if (buf->len) |
| 176 | strbuf_addch(buf, '\n'); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 177 | strbuf_addstr(buf, arg); |
Brandon Casey | a24a41e | 2013-02-18 20:17:06 -0800 | [diff] [blame] | 178 | strbuf_complete_line(buf); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 179 | } |
| 180 | return 0; |
| 181 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 182 | |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 183 | static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset) |
| 184 | { |
| 185 | const char **value = opt->value; |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 186 | |
| 187 | BUG_ON_OPT_NEG(unset); |
| 188 | |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 189 | if (arg != NULL && *arg == '=') |
| 190 | arg = arg + 1; |
| 191 | |
| 192 | *value = arg; |
| 193 | return 0; |
| 194 | } |
| 195 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 196 | static void determine_whence(struct wt_status *s) |
| 197 | { |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 198 | if (file_exists(git_path_merge_head(the_repository))) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 199 | whence = FROM_MERGE; |
Phillip Wood | 901ba7b | 2019-12-06 16:06:11 +0000 | [diff] [blame] | 200 | else if (!sequencer_determine_whence(the_repository, &whence)) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 201 | whence = FROM_COMMIT; |
| 202 | if (s) |
| 203 | s->whence = whence; |
| 204 | } |
| 205 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 206 | static void status_init_config(struct wt_status *s, config_fn_t fn) |
| 207 | { |
Nguyễn Thái Ngọc Duy | 5b02ca3 | 2018-11-10 06:48:49 +0100 | [diff] [blame] | 208 | wt_status_prepare(the_repository, s); |
Eckhard S. Maaß | dc6b1d9 | 2018-05-04 13:12:15 +0200 | [diff] [blame] | 209 | init_diff_ui_defaults(); |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 210 | git_config(fn, s); |
| 211 | determine_whence(s); |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 212 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */ |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 215 | static 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 Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 230 | static int commit_index_files(void) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 231 | { |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 232 | int err = 0; |
| 233 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 234 | switch (commit_style) { |
| 235 | case COMMIT_AS_IS: |
| 236 | break; /* nothing to do */ |
| 237 | case COMMIT_NORMAL: |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 238 | err = commit_lock_file(&index_lock); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 239 | break; |
| 240 | case COMMIT_PARTIAL: |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 241 | err = commit_lock_file(&index_lock); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 242 | rollback_lock_file(&false_lock); |
| 243 | break; |
| 244 | } |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 245 | |
| 246 | return err; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 247 | } |
| 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 Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 253 | static int list_paths(struct string_list *list, const char *with_tree, |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 254 | const struct pathspec *pattern) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 255 | { |
Stefan Beller | 5d0b9bf | 2015-03-20 17:28:07 -0700 | [diff] [blame] | 256 | int i, ret; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 257 | char *m; |
| 258 | |
Nguyễn Thái Ngọc Duy | 17ddc66 | 2013-07-14 15:35:53 +0700 | [diff] [blame] | 259 | if (!pattern->nr) |
Junio C Hamano | b9a0801 | 2012-07-15 21:39:48 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | |
Nguyễn Thái Ngọc Duy | 17ddc66 | 2013-07-14 15:35:53 +0700 | [diff] [blame] | 262 | m = xcalloc(1, pattern->nr); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 263 | |
Clemens Buchacher | 8894d53 | 2011-07-30 19:13:47 +0200 | [diff] [blame] | 264 | if (with_tree) { |
Clemens Buchacher | f950eb9 | 2011-09-04 12:42:01 +0200 | [diff] [blame] | 265 | char *max_prefix = common_prefix(pattern); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 266 | overlay_tree_on_index(the_repository->index, with_tree, max_prefix); |
Clemens Buchacher | 5879f56 | 2011-09-04 12:41:59 +0200 | [diff] [blame] | 267 | free(max_prefix); |
Clemens Buchacher | 8894d53 | 2011-07-30 19:13:47 +0200 | [diff] [blame] | 268 | } |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 269 | |
Derrick Stolee | cb8388d | 2021-04-01 01:49:45 +0000 | [diff] [blame] | 270 | /* TODO: audit for interaction with sparse-index. */ |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 271 | 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 Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 274 | struct string_list_item *item; |
| 275 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 276 | if (ce->ce_flags & CE_UPDATE) |
Junio C Hamano | e87e22d | 2008-01-14 13:54:24 -0800 | [diff] [blame] | 277 | continue; |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 278 | if (!ce_path_match(the_repository->index, ce, pattern, m)) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 279 | continue; |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 280 | item = string_list_insert(list, ce->name); |
Nguyễn Thái Ngọc Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 281 | if (ce_skip_worktree(ce)) |
| 282 | item->util = item; /* better a valid pointer than a fake one */ |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 285 | ret = report_path_error(m, pattern); |
Stefan Beller | 5d0b9bf | 2015-03-20 17:28:07 -0700 | [diff] [blame] | 286 | free(m); |
| 287 | return ret; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 290 | static void add_remove_files(struct string_list *list) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 291 | { |
| 292 | int i; |
| 293 | for (i = 0; i < list->nr; i++) { |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 294 | struct stat st; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 295 | struct string_list_item *p = &(list->items[i]); |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 296 | |
Nguyễn Thái Ngọc Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 297 | /* p->util is skip-worktree */ |
| 298 | if (p->util) |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 299 | continue; |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 300 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 301 | if (!lstat(p->string, &st)) { |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 302 | if (add_to_index(the_repository->index, p->string, &st, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 303 | die(_("updating files failed")); |
Alex Riesen | 960b8ad | 2008-05-12 19:57:45 +0200 | [diff] [blame] | 304 | } else |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 305 | remove_file_from_index(the_repository->index, p->string); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 309 | static void create_base_index(const struct commit *current_head) |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 310 | { |
| 311 | struct tree *tree; |
| 312 | struct unpack_trees_options opts; |
| 313 | struct tree_desc t; |
| 314 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 315 | if (!current_head) { |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 316 | discard_index(the_repository->index); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | memset(&opts, 0, sizeof(opts)); |
| 321 | opts.head_idx = 1; |
| 322 | opts.index_only = 1; |
| 323 | opts.merge = 1; |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 324 | opts.src_index = the_repository->index; |
| 325 | opts.dst_index = the_repository->index; |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 326 | |
| 327 | opts.fn = oneway_merge; |
brian m. carlson | a9dbc17 | 2017-05-06 22:10:37 +0000 | [diff] [blame] | 328 | tree = parse_tree_indirect(¤t_head->object.oid); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 329 | if (!tree) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 330 | die(_("failed to unpack HEAD tree object")); |
Johannes Schindelin | aa9f618 | 2024-02-23 08:34:23 +0000 | [diff] [blame] | 331 | if (parse_tree(tree) < 0) |
| 332 | exit(128); |
Eric W. Biederman | efed687 | 2023-10-01 21:40:28 -0500 | [diff] [blame] | 333 | init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size); |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 334 | if (unpack_trees(1, &t, &opts)) |
| 335 | exit(128); /* We've already reported the error, finish dying */ |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 338 | static 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 Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 344 | if (refresh_index(the_repository->index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL)) |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 345 | die_resolve_conflict("commit"); |
| 346 | } |
| 347 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 348 | static const char *prepare_index(const char **argv, const char *prefix, |
Michael Haggerty | 35ff08b | 2014-10-01 12:28:17 +0200 | [diff] [blame] | 349 | const struct commit *current_head, int is_status) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 350 | { |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 351 | struct string_list partial = STRING_LIST_INIT_DUP; |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 352 | struct pathspec pathspec; |
Junio C Hamano | 50b7e70 | 2009-08-04 23:49:33 -0700 | [diff] [blame] | 353 | int refresh_flags = REFRESH_QUIET; |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 354 | const char *ret; |
Patrick Steinhardt | 88dd321 | 2025-02-07 12:03:37 +0100 | [diff] [blame] | 355 | char *path = NULL; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 356 | |
Junio C Hamano | 50b7e70 | 2009-08-04 23:49:33 -0700 | [diff] [blame] | 357 | if (is_status) |
| 358 | refresh_flags |= REFRESH_UNMERGED; |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 359 | parse_pathspec(&pathspec, 0, |
| 360 | PATHSPEC_PREFER_FULL, |
| 361 | prefix, argv); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 362 | |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 363 | if (pathspec_from_file) { |
| 364 | if (interactive) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 365 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch"); |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 366 | |
Alexandr Miloslavskiy | 509efef | 2019-12-16 15:47:52 +0000 | [diff] [blame] | 367 | if (all) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 368 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a"); |
Alexandr Miloslavskiy | 509efef | 2019-12-16 15:47:52 +0000 | [diff] [blame] | 369 | |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 370 | if (pathspec.nr) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 371 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 372 | |
| 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 Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 377 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 380 | if (!pathspec.nr && (also || (only && !allow_empty && |
| 381 | (!amend || (fixup_message && strcmp(fixup_prefix, "amend")))))) |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 382 | die(_("No paths with --include/--only does not make sense.")); |
| 383 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 384 | if (repo_read_index_preload(the_repository, &pathspec, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 385 | die(_("index file corrupt")); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 386 | |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 387 | if (interactive) { |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 388 | char *old_index_env = NULL, *old_repo_index_file; |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 389 | repo_hold_locked_index(the_repository, &index_lock, |
| 390 | LOCK_DIE_ON_ERROR); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 391 | |
| 392 | refresh_cache_or_die(refresh_flags); |
| 393 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 394 | if (write_locked_index(the_repository->index, &index_lock, 0)) |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 395 | die(_("unable to create temporary index")); |
| 396 | |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 397 | old_repo_index_file = the_repository->index_file; |
| 398 | the_repository->index_file = |
| 399 | (char *)get_lock_file_path(&index_lock); |
Jeff King | 406bab3 | 2019-01-11 17:15:40 -0500 | [diff] [blame] | 400 | old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT)); |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 401 | setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 402 | |
John Cai | 8364745 | 2024-09-13 21:16:17 +0000 | [diff] [blame] | 403 | if (interactive_add(the_repository, argv, prefix, patch_interactive) != 0) |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 404 | die(_("interactive add failed")); |
| 405 | |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 406 | the_repository->index_file = old_repo_index_file; |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 407 | if (old_index_env && *old_index_env) |
| 408 | setenv(INDEX_ENVIRONMENT, old_index_env, 1); |
| 409 | else |
| 410 | unsetenv(INDEX_ENVIRONMENT); |
Jeff King | 406bab3 | 2019-01-11 17:15:40 -0500 | [diff] [blame] | 411 | FREE_AND_NULL(old_index_env); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 412 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 413 | discard_index(the_repository->index); |
| 414 | read_index_from(the_repository->index, get_lock_file_path(&index_lock), |
Patrick Steinhardt | 246deea | 2024-09-12 13:29:24 +0200 | [diff] [blame] | 415 | repo_get_git_dir(the_repository)); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 416 | if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) { |
Junio C Hamano | 3fd13cb | 2014-09-11 10:33:32 -0700 | [diff] [blame] | 417 | if (reopen_lock_file(&index_lock) < 0) |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 418 | die(_("unable to write index file")); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 419 | if (write_locked_index(the_repository->index, &index_lock, 0)) |
Junio C Hamano | 3fd13cb | 2014-09-11 10:33:32 -0700 | [diff] [blame] | 420 | die(_("unable to update temporary index")); |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 421 | } else |
| 422 | warning(_("Failed to update main cache tree")); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 423 | |
| 424 | commit_style = COMMIT_NORMAL; |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 425 | ret = get_lock_file_path(&index_lock); |
| 426 | goto out; |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 429 | /* |
| 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 Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 441 | if (all || (also && pathspec.nr)) { |
Ghanshyam Thakkar | ac5946e | 2024-04-03 23:44:50 +0530 | [diff] [blame] | 442 | char *ps_matched = xcalloc(pathspec.nr, 1); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 443 | repo_hold_locked_index(the_repository, &index_lock, |
| 444 | LOCK_DIE_ON_ERROR); |
Elijah Newren | 50c37ee | 2023-05-16 06:33:46 +0000 | [diff] [blame] | 445 | add_files_to_cache(the_repository, also ? prefix : NULL, |
Ghanshyam Thakkar | ac5946e | 2024-04-03 23:44:50 +0530 | [diff] [blame] | 446 | &pathspec, ps_matched, 0, 0); |
| 447 | if (!all && report_path_error(ps_matched, &pathspec)) |
| 448 | exit(128); |
| 449 | |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 450 | refresh_cache_or_die(refresh_flags); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 451 | cache_tree_update(the_repository->index, WRITE_TREE_SILENT); |
| 452 | if (write_locked_index(the_repository->index, &index_lock, 0)) |
Junio C Hamano | a060705 | 2023-10-17 20:08:51 -0700 | [diff] [blame] | 453 | die(_("unable to write new index file")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 454 | commit_style = COMMIT_NORMAL; |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 455 | ret = get_lock_file_path(&index_lock); |
Ghanshyam Thakkar | ac5946e | 2024-04-03 23:44:50 +0530 | [diff] [blame] | 456 | free(ps_matched); |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 457 | goto out; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 460 | /* |
| 461 | * As-is commit. |
| 462 | * |
| 463 | * (1) return the name of the real index file. |
| 464 | * |
Markus Heidelberg | 7327623 | 2010-04-02 14:27:18 +0200 | [diff] [blame] | 465 | * The caller should run hooks on the real index, |
| 466 | * and create commit from the_index. |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 467 | * We still need to refresh the index here. |
| 468 | */ |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 469 | if (!only && !pathspec.nr) { |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 470 | repo_hold_locked_index(the_repository, &index_lock, |
| 471 | LOCK_DIE_ON_ERROR); |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 472 | refresh_cache_or_die(refresh_flags); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 473 | 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 Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 477 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
Junio C Hamano | a060705 | 2023-10-17 20:08:51 -0700 | [diff] [blame] | 478 | die(_("unable to write new index file")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 479 | commit_style = COMMIT_AS_IS; |
Patrick Steinhardt | 1dc4ec2 | 2024-09-12 13:29:32 +0200 | [diff] [blame] | 480 | ret = repo_get_index_file(the_repository); |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 481 | goto out; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 484 | /* |
| 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ð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 505 | if (whence != FROM_COMMIT) { |
| 506 | if (whence == FROM_MERGE) |
| 507 | die(_("cannot do a partial commit during a merge.")); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 508 | else if (is_from_cherry_pick(whence)) |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 509 | die(_("cannot do a partial commit during a cherry-pick.")); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 510 | else if (is_from_rebase(whence)) |
| 511 | die(_("cannot do a partial commit during a rebase.")); |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 512 | } |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 513 | |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 514 | if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec)) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 515 | exit(1); |
| 516 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 517 | discard_index(the_repository->index); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 518 | if (repo_read_index(the_repository) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 519 | die(_("cannot read the index")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 520 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 521 | repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 522 | add_remove_files(&partial); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 523 | 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 Hamano | a060705 | 2023-10-17 20:08:51 -0700 | [diff] [blame] | 526 | die(_("unable to write new index file")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 527 | |
Patrick Steinhardt | 88dd321 | 2025-02-07 12:03:37 +0100 | [diff] [blame] | 528 | 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 Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 531 | LOCK_DIE_ON_ERROR); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 532 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 533 | create_base_index(current_head); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 534 | add_remove_files(&partial); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 535 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 536 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 537 | if (write_locked_index(the_repository->index, &false_lock, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 538 | die(_("unable to write temporary index file")); |
Jeff King | 959ba67 | 2008-02-14 12:18:23 -0500 | [diff] [blame] | 539 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 540 | discard_index(the_repository->index); |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 541 | ret = get_lock_file_path(&false_lock); |
Patrick Steinhardt | 246deea | 2024-09-12 13:29:24 +0200 | [diff] [blame] | 542 | read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository)); |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 543 | out: |
| 544 | string_list_clear(&partial, 0); |
| 545 | clear_pathspec(&pathspec); |
Patrick Steinhardt | 88dd321 | 2025-02-07 12:03:37 +0100 | [diff] [blame] | 546 | free(path); |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 547 | return ret; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 548 | } |
| 549 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 550 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
| 551 | struct wt_status *s) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 552 | { |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 553 | struct object_id oid; |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 554 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 555 | if (s->relative_paths) |
| 556 | s->prefix = prefix; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 557 | |
| 558 | if (amend) { |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 559 | s->amend = 1; |
| 560 | s->reference = "HEAD^1"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 561 | } |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 562 | s->verbose = verbose; |
| 563 | s->index_file = index_file; |
| 564 | s->fp = fp; |
| 565 | s->nowarn = nowarn; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 566 | s->is_initial = repo_get_oid(the_repository, s->reference, &oid) ? 1 : 0; |
Jeff Hostetler | d9fc746 | 2016-08-11 10:45:59 -0400 | [diff] [blame] | 567 | if (!s->is_initial) |
brian m. carlson | e0cb7cd | 2019-08-18 20:04:21 +0000 | [diff] [blame] | 568 | oidcpy(&s->oid_commit, &oid); |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 569 | s->status_format = status_format; |
| 570 | s->ignore_submodule_arg = ignore_submodule_arg; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 571 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 572 | wt_status_collect(s); |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 573 | wt_status_print(s); |
Stephen P. Smith | 73ba5d7 | 2018-09-30 07:12:45 -0700 | [diff] [blame] | 574 | wt_status_collect_free_buffers(s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 575 | |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 576 | return s->committable; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 577 | } |
| 578 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 579 | static int is_a_merge(const struct commit *current_head) |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 580 | { |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 581 | return !!(current_head->parents && current_head->parents->next); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 584 | static void assert_split_ident(struct ident_split *id, const struct strbuf *buf) |
| 585 | { |
Jeff King | c83a509 | 2014-12-10 10:43:42 -0500 | [diff] [blame] | 586 | if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 587 | BUG("unable to parse our own ident: %s", buf->buf); |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 588 | } |
| 589 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 590 | static 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é Scharfe | 147ee35 | 2019-12-07 12:16:04 +0100 | [diff] [blame] | 595 | strbuf_add(&buf, s, e - s); |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 596 | setenv(var, buf.buf, 1); |
| 597 | strbuf_release(&buf); |
| 598 | } |
| 599 | |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 600 | static int parse_force_date(const char *in, struct strbuf *out) |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 601 | { |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 602 | strbuf_addch(out, '@'); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 603 | |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 604 | if (parse_date(in, out) < 0) { |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 605 | int errors = 0; |
| 606 | unsigned long t = approxidate_careful(in, &errors); |
| 607 | if (errors) |
| 608 | return -1; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 609 | strbuf_addf(out, "%lu", t); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 615 | static void set_ident_var(char **buf, char *val) |
| 616 | { |
| 617 | free(*buf); |
| 618 | *buf = val; |
| 619 | } |
| 620 | |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 621 | static void determine_author_info(struct strbuf *author_ident) |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 622 | { |
| 623 | char *name, *email, *date; |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 624 | struct ident_split author; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 625 | |
Jeff King | eaa541e | 2015-01-12 20:58:33 -0500 | [diff] [blame] | 626 | 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éjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 629 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 630 | if (author_message) { |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 631 | struct ident_split ident; |
Junio C Hamano | 2c733fb | 2012-02-02 13:41:43 -0800 | [diff] [blame] | 632 | size_t len; |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 633 | const char *a; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 634 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 635 | a = find_commit_header(author_message_buffer, "author", &len); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 636 | if (!a) |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 637 | 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éjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 640 | |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 641 | 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éjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 643 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 644 | if (ident.date_begin) { |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 645 | struct strbuf date_buf = STRBUF_INIT; |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 646 | 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 King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 650 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 651 | } |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (force_author) { |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 655 | struct ident_split ident; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 656 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 657 | if (split_ident_line(&ident, force_author, strlen(force_author)) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 658 | die(_("malformed --author parameter")); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 659 | 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éjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 663 | if (force_date) { |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 664 | struct strbuf date_buf = STRBUF_INIT; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 665 | if (parse_force_date(force_date, &date_buf)) |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 666 | die(_("invalid date format: %s"), force_date); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 667 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 668 | } |
| 669 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 670 | strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, |
| 671 | IDENT_STRICT)); |
Jeff King | c83a509 | 2014-12-10 10:43:42 -0500 | [diff] [blame] | 672 | 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 King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 676 | free(name); |
| 677 | free(email); |
| 678 | free(date); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Jeff King | b7242b8 | 2014-05-01 21:10:01 -0400 | [diff] [blame] | 681 | static int author_date_is_interesting(void) |
| 682 | { |
| 683 | return author_message || force_date; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 686 | static void adjust_comment_line_char(const struct strbuf *sb) |
| 687 | { |
| 688 | char candidates[] = "#;@!$%^&|:"; |
| 689 | char *candidate; |
| 690 | const char *p; |
| 691 | |
Jeff King | 1751e58 | 2024-03-12 05:17:19 -0400 | [diff] [blame] | 692 | if (!memchr(sb->buf, candidates[0], sb->len)) { |
Patrick Steinhardt | 648abbe | 2024-08-14 08:52:14 +0200 | [diff] [blame] | 693 | 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 Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 696 | return; |
Jeff King | 1751e58 | 2024-03-12 05:17:19 -0400 | [diff] [blame] | 697 | } |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 698 | |
| 699 | p = sb->buf; |
| 700 | candidate = strchr(candidates, *p); |
| 701 | if (candidate) |
| 702 | *candidate = ' '; |
| 703 | for (p = sb->buf; *p; p++) { |
| 704 | if ((p[0] == '\n' || p[0] == '\r') && p[1]) { |
| 705 | candidate = strchr(candidates, p[1]); |
| 706 | if (candidate) |
| 707 | *candidate = ' '; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | for (p = candidates; *p == ' '; p++) |
| 712 | ; |
| 713 | if (!*p) |
| 714 | die(_("unable to select a comment character that is not used\n" |
| 715 | "in the current commit message")); |
Patrick Steinhardt | 648abbe | 2024-08-14 08:52:14 +0200 | [diff] [blame] | 716 | free(comment_line_str_to_free); |
| 717 | comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p); |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 718 | } |
| 719 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 720 | static 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ð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 725 | buffer = repo_get_commit_buffer(the_repository, commit, NULL); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 726 | 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ð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 732 | repo_format_commit_message(the_repository, commit, fmt, sb, ctx); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 733 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 734 | } |
| 735 | |
Patrick Steinhardt | d34b5cb | 2024-11-05 07:17:09 +0100 | [diff] [blame] | 736 | static 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 Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 743 | static int prepare_to_commit(const char *index_file, const char *prefix, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 744 | struct commit *current_head, |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 745 | struct wt_status *s, |
| 746 | struct strbuf *author_ident) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 747 | { |
| 748 | struct stat statbuf; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 749 | struct strbuf committer_ident = STRBUF_INIT; |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 750 | int committable; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 751 | struct strbuf sb = STRBUF_INIT; |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 752 | const char *hook_arg1 = NULL; |
| 753 | const char *hook_arg2 = NULL; |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 754 | int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE); |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 755 | int old_display_comment_prefix; |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 756 | int invoked_hook; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 757 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 758 | /* This checks and barfs if author is badly specified */ |
| 759 | determine_author_info(author_ident); |
| 760 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 761 | if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook, |
| 762 | "pre-commit", NULL)) |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 763 | return 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 764 | |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 765 | 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 Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 777 | die(_("could not lookup commit '%s'"), squash_message); |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 778 | ctx.output_encoding = get_commit_output_encoding(); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 779 | repo_format_commit_message(the_repository, c, |
| 780 | "squash! %s\n\n", &sb, |
| 781 | &ctx); |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Ævar Arnfjörð Bjarmason | 30884c9 | 2017-12-22 20:41:52 +0000 | [diff] [blame] | 785 | if (have_option_m && !fixup_message) { |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 786 | strbuf_addbuf(&sb, &message); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 787 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 788 | } else if (logfile && !strcmp(logfile, "-")) { |
| 789 | if (isatty(0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 790 | fprintf(stderr, _("(reading log message from standard input)\n")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 791 | if (strbuf_read(&sb, 0, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 792 | die_errno(_("could not read log from standard input")); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 793 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 794 | } else if (logfile) { |
| 795 | if (strbuf_read_file(&sb, logfile, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 796 | die_errno(_("could not read log file '%s'"), |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 797 | logfile); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 798 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 799 | } else if (use_message) { |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 800 | char *buffer; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 801 | buffer = strstr(use_message_buffer, "\n\n"); |
Jeff King | 076cbd6 | 2014-04-25 19:11:15 -0400 | [diff] [blame] | 802 | if (buffer) |
Johannes Schindelin | 84e213a | 2016-06-29 16:14:42 +0200 | [diff] [blame] | 803 | strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 804 | hook_arg1 = "commit"; |
| 805 | hook_arg2 = use_message; |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 806 | } else if (fixup_message) { |
| 807 | struct pretty_print_context ctx = {0}; |
| 808 | struct commit *commit; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 809 | char *fmt; |
| 810 | commit = lookup_commit_reference_by_name(fixup_commit); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 811 | if (!commit) |
Teng Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 812 | die(_("could not lookup commit '%s'"), fixup_commit); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 813 | ctx.output_encoding = get_commit_output_encoding(); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 814 | fmt = xstrfmt("%s! %%s\n\n", fixup_prefix); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 815 | repo_format_commit_message(the_repository, commit, fmt, &sb, |
| 816 | &ctx); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 817 | free(fmt); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 818 | hook_arg1 = "message"; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 819 | |
| 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 Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 834 | die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 835 | prepare_amend_commit(commit, &sb, &ctx); |
| 836 | } |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 837 | } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) { |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 838 | size_t merge_msg_start; |
| 839 | |
Sven Strickroth | b64c1e0 | 2016-03-21 23:29:40 +0100 | [diff] [blame] | 840 | /* |
| 841 | * prepend SQUASH_MSG here if it exists and a |
| 842 | * "merge --squash" was originally performed |
| 843 | */ |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 844 | if (!stat(git_path_squash_msg(the_repository), &statbuf)) { |
| 845 | if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0) |
Sven Strickroth | b64c1e0 | 2016-03-21 23:29:40 +0100 | [diff] [blame] | 846 | die_errno(_("could not read SQUASH_MSG")); |
| 847 | hook_arg1 = "squash"; |
| 848 | } else |
| 849 | hook_arg1 = "merge"; |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 850 | |
| 851 | merge_msg_start = sb.len; |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 852 | if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 853 | die_errno(_("could not read MERGE_MSG")); |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 854 | |
| 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 Triplett | e90cc07 | 2024-02-27 01:17:36 -0800 | [diff] [blame] | 859 | s->added_cut_line = 1; |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 860 | } 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ð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 862 | die_errno(_("could not read SQUASH_MSG")); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 863 | hook_arg1 = "squash"; |
Jonathan Nieder | 2140b14 | 2011-02-25 03:07:57 -0600 | [diff] [blame] | 864 | } else if (template_file) { |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 865 | if (strbuf_read_file(&sb, template_file, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 866 | die_errno(_("could not read '%s'"), template_file); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 867 | hook_arg1 = "template"; |
Boris Faure | 8b1ae67 | 2011-05-08 12:31:02 +0200 | [diff] [blame] | 868 | clean_message_contents = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 869 | } |
| 870 | |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 871 | /* |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 872 | * The remaining cases don't modify the template message, but |
| 873 | * just set the argument(s) to the prepare-commit-msg hook. |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 874 | */ |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 875 | else if (whence == FROM_MERGE) |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 876 | hook_arg1 = "merge"; |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 877 | else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 878 | hook_arg1 = "commit"; |
| 879 | hook_arg2 = "CHERRY_PICK_HEAD"; |
| 880 | } |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 881 | |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 882 | 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 Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 892 | s->fp = fopen_for_writing(git_path_commit_editmsg()); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 893 | if (!s->fp) |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 894 | die_errno(_("could not open '%s'"), git_path_commit_editmsg()); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 895 | |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 896 | /* 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 Moy | ea9882b | 2013-09-12 12:50:06 +0200 | [diff] [blame] | 900 | /* |
| 901 | * Most hints are counter-productive when the commit has |
| 902 | * already started. |
| 903 | */ |
| 904 | s->hints = 0; |
| 905 | |
Boris Faure | 8b1ae67 | 2011-05-08 12:31:02 +0200 | [diff] [blame] | 906 | if (clean_message_contents) |
Jeff King | 2982b65 | 2024-03-12 05:17:27 -0400 | [diff] [blame] | 907 | strbuf_stripspace(&sb, NULL); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 908 | |
Junio C Hamano | 073bd75 | 2014-10-28 12:44:09 -0700 | [diff] [blame] | 909 | if (signoff) |
Linus Arver | 7cb26a1 | 2023-10-20 19:01:33 +0000 | [diff] [blame] | 910 | append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 911 | |
Jonathan Nieder | 37f3012 | 2011-02-25 23:10:49 -0600 | [diff] [blame] | 912 | if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 913 | die_errno(_("could not write commit template")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 914 | |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 915 | if (auto_comment_line_char) |
| 916 | adjust_comment_line_char(&sb); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 917 | strbuf_release(&sb); |
| 918 | |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 919 | /* This checks if committer ident is explicitly given */ |
Jeff King | f20f387 | 2012-07-23 14:50:35 -0400 | [diff] [blame] | 920 | strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); |
James P. Howard, II | bed575e | 2009-12-07 17:45:27 -0500 | [diff] [blame] | 921 | if (use_editor && include_status) { |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 922 | int ident_shown = 0; |
| 923 | int saved_color_setting; |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 924 | struct ident_split ci, ai; |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 925 | const char *hint_cleanup_all = allow_empty_message ? |
| 926 | _("Please enter the commit message for your changes." |
Jeff King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 927 | " Lines starting\nwith '%s' will be ignored.\n") : |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 928 | _("Please enter the commit message for your changes." |
Jeff King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 929 | " Lines starting\nwith '%s' will be ignored, and an empty" |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 930 | " 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 King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 934 | "with '%s' will be kept; you may remove them" |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 935 | " yourself if you want to.\n") : |
| 936 | _("Please enter the commit message for your changes." |
| 937 | " Lines starting\n" |
Jeff King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 938 | "with '%s' will be kept; you may remove them" |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 939 | " yourself if you want to.\n" |
| 940 | "An empty message aborts the commit.\n"); |
Nguyễn Thái Ngọc Duy | 75df1f4 | 2014-02-17 19:15:32 +0700 | [diff] [blame] | 941 | if (whence != FROM_COMMIT) { |
Josh Triplett | e90cc07 | 2024-02-27 01:17:36 -0800 | [diff] [blame] | 942 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
Josh Triplett | 688a0a7 | 2024-02-27 01:16:09 -0800 | [diff] [blame] | 943 | wt_status_add_cut_line(s); |
Han-Wen Nienhuys | b6d2558 | 2020-08-21 16:59:36 +0000 | [diff] [blame] | 944 | status_printf_ln( |
| 945 | s, GIT_COLOR_NORMAL, |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 946 | whence == FROM_MERGE ? |
Han-Wen Nienhuys | b6d2558 | 2020-08-21 16:59:36 +0000 | [diff] [blame] | 947 | _("\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 Duy | 75df1f4 | 2014-02-17 19:15:32 +0700 | [diff] [blame] | 957 | } |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 958 | |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 959 | fprintf(s->fp, "\n"); |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 960 | if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL) |
Jeff King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 961 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str); |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 962 | else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
Josh Triplett | e90cc07 | 2024-02-27 01:17:36 -0800 | [diff] [blame] | 963 | if (whence == FROM_COMMIT) |
Josh Triplett | 688a0a7 | 2024-02-27 01:16:09 -0800 | [diff] [blame] | 964 | wt_status_add_cut_line(s); |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 965 | } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */ |
Jeff King | f99e1d9 | 2024-03-12 05:17:34 -0400 | [diff] [blame] | 966 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 967 | |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 968 | /* |
| 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 King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 976 | |
| 977 | if (ident_cmp(&ai, &ci)) |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 978 | status_printf_ln(s, GIT_COLOR_NORMAL, |
Ævar Arnfjörð Bjarmason | fe8165c | 2011-02-22 23:41:46 +0000 | [diff] [blame] | 979 | _("%s" |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 980 | "Author: %.*s <%.*s>"), |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 981 | ident_shown++ ? "" : "\n", |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 982 | (int)(ai.name_end - ai.name_begin), ai.name_begin, |
| 983 | (int)(ai.mail_end - ai.mail_begin), ai.mail_begin); |
Santi Béjar | e83dbe8 | 2008-05-04 18:04:50 +0200 | [diff] [blame] | 984 | |
Jeff King | b7242b8 | 2014-05-01 21:10:01 -0400 | [diff] [blame] | 985 | if (author_date_is_interesting()) |
| 986 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 987 | _("%s" |
| 988 | "Date: %s"), |
| 989 | ident_shown++ ? "" : "\n", |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 990 | show_ident_date(&ai, DATE_MODE(NORMAL))); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 991 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 992 | if (!committer_ident_sufficiently_given()) |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 993 | status_printf_ln(s, GIT_COLOR_NORMAL, |
Ævar Arnfjörð Bjarmason | fe8165c | 2011-02-22 23:41:46 +0000 | [diff] [blame] | 994 | _("%s" |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 995 | "Committer: %.*s <%.*s>"), |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 996 | ident_shown++ ? "" : "\n", |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 997 | (int)(ci.name_end - ci.name_begin), ci.name_begin, |
| 998 | (int)(ci.mail_end - ci.mail_begin), ci.mail_begin); |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 999 | |
Kaartic Sivaraam | b3cf1b7 | 2017-06-30 17:42:21 +0530 | [diff] [blame] | 1000 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */ |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 1001 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1002 | saved_color_setting = s->use_color; |
| 1003 | s->use_color = 0; |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1004 | committable = run_status(s->fp, index_file, prefix, 1, s); |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1005 | s->use_color = saved_color_setting; |
Patrick Steinhardt | d34b5cb | 2024-11-05 07:17:09 +0100 | [diff] [blame] | 1006 | string_list_clear_func(&s->change, change_data_free); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1007 | } else { |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1008 | struct object_id oid; |
Junio C Hamano | fbcf118 | 2007-12-19 19:23:03 -0800 | [diff] [blame] | 1009 | const char *parent = "HEAD"; |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1010 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1011 | if (!the_repository->index->initialized && repo_read_index(the_repository) < 0) |
Johannes Schindelin | 2ee045e | 2023-06-29 13:23:10 +0000 | [diff] [blame] | 1012 | die(_("Cannot read index")); |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1013 | |
Junio C Hamano | fbcf118 | 2007-12-19 19:23:03 -0800 | [diff] [blame] | 1014 | if (amend) |
| 1015 | parent = "HEAD^1"; |
| 1016 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1017 | if (repo_get_oid(the_repository, parent, &oid)) { |
Nguyễn Thái Ngọc Duy | 2c49f7f | 2016-10-24 17:42:22 +0700 | [diff] [blame] | 1018 | int i, ita_nr = 0; |
| 1019 | |
Derrick Stolee | cb8388d | 2021-04-01 01:49:45 +0000 | [diff] [blame] | 1020 | /* TODO: audit for interaction with sparse-index. */ |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1021 | 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 Duy | 2c49f7f | 2016-10-24 17:42:22 +0700 | [diff] [blame] | 1024 | ita_nr++; |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1025 | committable = the_repository->index->cache_nr - ita_nr > 0; |
Nguyễn Thái Ngọc Duy | 2c49f7f | 2016-10-24 17:42:22 +0700 | [diff] [blame] | 1026 | } else { |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1027 | /* |
| 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 Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 1036 | struct diff_flags flags = DIFF_FLAGS_INIT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1037 | flags.override_submodule_config = 1; |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1038 | if (ignore_submodule_arg && |
| 1039 | !strcmp(ignore_submodule_arg, "all")) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1040 | flags.ignore_submodules = 1; |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 1041 | committable = index_differs_from(the_repository, |
| 1042 | parent, &flags, 1); |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1043 | } |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1044 | } |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 1045 | strbuf_release(&committer_ident); |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1046 | |
Jonathan Nieder | 37f3012 | 2011-02-25 23:10:49 -0600 | [diff] [blame] | 1047 | fclose(s->fp); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1048 | |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 1049 | if (trailer_args.nr) { |
John Passaro | 4a86187 | 2024-05-05 18:49:09 +0000 | [diff] [blame] | 1050 | if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args)) |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 1051 | die(_("unable to pass trailers to --trailers")); |
| 1052 | strvec_clear(&trailer_args); |
| 1053 | } |
| 1054 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1055 | /* |
| 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. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1060 | if (!committable && whence != FROM_MERGE && !allow_empty && |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1061 | !(amend && is_a_merge(current_head))) { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 1062 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 1063 | s->display_comment_prefix = old_display_comment_prefix; |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1064 | run_status(stdout, index_file, prefix, 0, s); |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 1065 | if (amend) |
Ævar Arnfjörð Bjarmason | fc88e31 | 2011-02-22 23:41:49 +0000 | [diff] [blame] | 1066 | fputs(_(empty_amend_advice), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1067 | else if (is_from_cherry_pick(whence) || |
| 1068 | whence == FROM_REBASE_PICK) { |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 1069 | fputs(_(empty_cherry_pick_advice), stderr); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1070 | if (whence == FROM_CHERRY_PICK_SINGLE) |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1071 | fputs(_(empty_cherry_pick_advice_single), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1072 | else if (whence == FROM_CHERRY_PICK_MULTI) |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1073 | fputs(_(empty_cherry_pick_advice_multi), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1074 | else |
| 1075 | fputs(_(empty_rebase_pick_advice), stderr); |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1076 | } |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1077 | return 0; |
| 1078 | } |
| 1079 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1080 | if (!no_verify && invoked_hook) { |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1081 | /* |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1082 | * 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 Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1084 | * the editor and after we invoke run_status above. |
| 1085 | */ |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1086 | discard_index(the_repository->index); |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1087 | } |
Patrick Steinhardt | 246deea | 2024-09-12 13:29:24 +0200 | [diff] [blame] | 1088 | read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository)); |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1089 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1090 | if (cache_tree_update(the_repository->index, 0)) { |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1091 | error(_("Error building trees")); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1095 | if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg", |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 1096 | git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL)) |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1099 | if (use_editor) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1100 | struct strvec env = STRVEC_INIT; |
Elia Pinto | 8d7aa4b | 2017-01-13 17:58:00 +0000 | [diff] [blame] | 1101 | |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1102 | strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1103 | if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) { |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 1104 | fprintf(stderr, |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1105 | _("Please supply the message using either -m or -F option.\n")); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 1106 | exit(1); |
| 1107 | } |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1108 | strvec_clear(&env); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | if (!no_verify && |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1112 | run_commit_hook(use_editor, index_file, NULL, "commit-msg", |
| 1113 | git_path_commit_editmsg(), NULL)) { |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | return 1; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1120 | static 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 Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 1128 | repo_init_revisions(the_repository, &revs, NULL); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1129 | 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ð Bjarmason | a52f07a | 2022-04-13 22:01:46 +0200 | [diff] [blame] | 1135 | revs.mailmap = xmalloc(sizeof(struct string_list)); |
| 1136 | string_list_init_nodup(revs.mailmap); |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 1137 | read_mailmap(revs.mailmap); |
Antoine Pelisse | ea16794 | 2013-08-23 15:48:31 +0200 | [diff] [blame] | 1138 | |
Stefan Beller | 81c3ce3 | 2014-08-10 23:33:26 +0200 | [diff] [blame] | 1139 | if (prepare_revision_walk(&revs)) |
| 1140 | die(_("revision walk setup failed")); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1141 | commit = get_revision(&revs); |
| 1142 | if (commit) { |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 1143 | struct pretty_print_context ctx = {0}; |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1144 | ctx.date_mode.type = DATE_NORMAL; |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1145 | strbuf_release(&buf); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 1146 | repo_format_commit_message(the_repository, commit, |
| 1147 | "%aN <%aE>", &buf, &ctx); |
Ævar Arnfjörð Bjarmason | 2108fe4 | 2022-04-13 22:01:36 +0200 | [diff] [blame] | 1148 | release_revisions(&revs); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1149 | return strbuf_detach(&buf, NULL); |
| 1150 | } |
Michael J Gruber | 1044b1f | 2015-01-26 16:48:33 +0100 | [diff] [blame] | 1151 | die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1154 | static 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 Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1167 | |
Junio C Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1168 | static enum untracked_status_type parse_untracked_setting_name(const char *u) |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1169 | { |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 1170 | /* |
| 1171 | * Please update $__git_untracked_file_modes in |
| 1172 | * git-completion.bash when you add new options |
| 1173 | */ |
Junio C Hamano | f66e1a0 | 2024-03-13 10:32:14 -0700 | [diff] [blame] | 1174 | 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 Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1185 | 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 Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1191 | else |
Junio C Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1192 | return SHOW_UNTRACKED_FILES_ERROR; |
| 1193 | } |
| 1194 | |
| 1195 | static 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 Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1209 | static const char *read_commit_message(const char *name) |
| 1210 | { |
Jeff King | dd0d388 | 2013-01-26 04:44:06 -0500 | [diff] [blame] | 1211 | const char *out_enc; |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1212 | struct commit *commit; |
| 1213 | |
| 1214 | commit = lookup_commit_reference_by_name(name); |
| 1215 | if (!commit) |
Teng Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 1216 | die(_("could not lookup commit '%s'"), name); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1217 | out_enc = get_commit_output_encoding(); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1218 | return repo_logmsg_reencode(the_repository, commit, NULL, out_enc); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1219 | } |
| 1220 | |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1221 | /* |
| 1222 | * Enumerate what needs to be propagated when --porcelain |
| 1223 | * is not in effect here. |
| 1224 | */ |
| 1225 | static struct status_deferred_config { |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1226 | enum wt_status_format status_format; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1227 | int show_branch; |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1228 | enum ahead_behind_flags ahead_behind; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1229 | } status_deferred_config = { |
| 1230 | STATUS_FORMAT_UNSPECIFIED, |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1231 | -1, /* unspecified */ |
| 1232 | AHEAD_BEHIND_UNSPECIFIED, |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1233 | }; |
| 1234 | |
| 1235 | static void finalize_deferred_config(struct wt_status *s) |
| 1236 | { |
| 1237 | int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN && |
Jeff Hostetler | 1ecdecc | 2016-08-11 10:45:57 -0400 | [diff] [blame] | 1238 | status_format != STATUS_FORMAT_PORCELAIN_V2 && |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1239 | !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 Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 1246 | die(_("options '%s' and '%s' cannot be used together"), "--long", "-z"); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1247 | } |
| 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 Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1258 | |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1259 | /* |
| 1260 | * If the user did not give a "--[no]-ahead-behind" command |
Jeff Hostetler | fb4db1a | 2019-06-18 13:21:28 -0700 | [diff] [blame] | 1261 | * 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 Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1265 | */ |
Jeff Hostetler | fb4db1a | 2019-06-18 13:21:28 -0700 | [diff] [blame] | 1266 | if (use_deferred_config && |
| 1267 | s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1268 | s->ahead_behind_flags = status_deferred_config.ahead_behind; |
| 1269 | |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1270 | if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) |
| 1271 | s->ahead_behind_flags = AHEAD_BEHIND_FULL; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1274 | static 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 Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1282 | die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv); |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1283 | if (patch_interactive || interactive || all || also || only) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1284 | die(_("reword option of '%s' and '%s' cannot be used together"), |
| 1285 | "--fixup", "--patch/--interactive/--all/--include/--only"); |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1286 | } |
| 1287 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 1288 | static int parse_and_validate_options(int argc, const char *argv[], |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1289 | const struct option *options, |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 1290 | const char * const usage[], |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1291 | const char *prefix, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1292 | struct commit *current_head, |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1293 | struct wt_status *s) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1294 | { |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1295 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1296 | finalize_deferred_config(s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1297 | |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1298 | if (force_author && !strchr(force_author, '>')) |
| 1299 | force_author = find_author_by_nickname(force_author); |
| 1300 | |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 1301 | if (force_author && renew_authorship) |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1302 | die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author"); |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 1303 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1304 | if (logfile || have_option_m || use_message) |
Junio C Hamano | 4803466 | 2007-12-22 19:25:37 -0800 | [diff] [blame] | 1305 | use_editor = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1306 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1307 | /* Sanity check options */ |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1308 | if (amend && !current_head) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1309 | die(_("You have nothing to amend.")); |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 1310 | if (amend && whence != FROM_COMMIT) { |
| 1311 | if (whence == FROM_MERGE) |
| 1312 | die(_("You are in the middle of a merge -- cannot amend.")); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1313 | else if (is_from_cherry_pick(whence)) |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 1314 | die(_("You are in the middle of a cherry-pick -- cannot amend.")); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1315 | else if (whence == FROM_REBASE_PICK) |
| 1316 | die(_("You are in the middle of a rebase -- cannot amend.")); |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 1317 | } |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 1318 | if (fixup_message && squash_message) |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1319 | 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 Steinhardt | 14da262 | 2024-06-11 11:19:22 +0200 | [diff] [blame] | 1329 | FREE_AND_NULL(template_file); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1330 | if (edit_message) |
| 1331 | use_message = edit_message; |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 1332 | if (amend && !use_message && !fixup_message) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1333 | use_message = "HEAD"; |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1334 | if (!use_message && !is_from_cherry_pick(whence) && |
| 1335 | !is_from_rebase(whence) && renew_authorship) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1336 | die(_("--reset-author can be used only with -C, -c or --amend.")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1337 | if (use_message) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1338 | 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 Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1344 | if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) && |
| 1345 | !renew_authorship) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1346 | author_message = "CHERRY_PICK_HEAD"; |
| 1347 | author_message_buffer = read_commit_message(author_message); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 1350 | if (patch_interactive) |
| 1351 | interactive = 1; |
| 1352 | |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1353 | die_for_incompatible_opt4(also, "-i/--include", |
| 1354 | only, "-o/--only", |
| 1355 | all, "-a/--all", |
| 1356 | interactive, "--interactive/-p/--patch"); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1357 | 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 Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1362 | * name like `amend` or `reword`, or a misspelt suboption |
| 1363 | * name. In either case, we treat it as |
| 1364 | * --fixup=<suboption>:<arg>. |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1365 | * |
| 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 Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1374 | if (!strcmp("amend", fixup_message) || |
| 1375 | !strcmp("reword", fixup_message)) { |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1376 | fixup_prefix = "amend"; |
| 1377 | allow_empty = 1; |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1378 | if (*fixup_message == 'r') { |
| 1379 | check_fixup_reword_options(argc, argv); |
| 1380 | only = 1; |
| 1381 | } |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1382 | } 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 Klinghed | 8ef6aad | 2021-08-14 21:40:30 +0000 | [diff] [blame] | 1392 | if (0 <= edit_flag) |
| 1393 | use_editor = edit_flag; |
| 1394 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1395 | handle_untracked_files_arg(s); |
Marius Storm-Olsen | 4bfee30 | 2008-06-05 10:31:19 +0200 | [diff] [blame] | 1396 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1397 | if (all && argc > 0) |
Nguyễn Thái Ngọc Duy | 5a1dbd4 | 2019-03-20 17:29:06 +0700 | [diff] [blame] | 1398 | die(_("paths '%s ...' with -a does not make sense"), |
| 1399 | argv[0]); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1400 | |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1401 | if (status_format != STATUS_FORMAT_NONE) |
Jeff King | 7c9f703 | 2009-09-05 04:59:56 -0400 | [diff] [blame] | 1402 | dry_run = 1; |
| 1403 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1404 | return argc; |
| 1405 | } |
| 1406 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1407 | static int dry_run_commit(const char **argv, const char *prefix, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1408 | const struct commit *current_head, struct wt_status *s) |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1409 | { |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1410 | int committable; |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1411 | const char *index_file; |
| 1412 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1413 | index_file = prepare_index(argv, prefix, current_head, 1); |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1414 | committable = run_status(stdout, index_file, prefix, 0, s); |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1415 | rollback_index_files(); |
| 1416 | |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1417 | return committable ? 0 : 1; |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 1420 | define_list_config_array_extra(color_status_slots, {"added"}); |
| 1421 | |
Jonathan Nieder | 8852117 | 2014-10-07 15:16:57 -0400 | [diff] [blame] | 1422 | static int parse_status_slot(const char *slot) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1423 | { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 1424 | if (!strcasecmp(slot, "added")) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1425 | return WT_STATUS_UPDATED; |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 1426 | |
| 1427 | return LOOKUP_CONFIG(color_status_slots, slot); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1430 | static int git_status_config(const char *k, const char *v, |
| 1431 | const struct config_context *ctx, void *cb) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1432 | { |
| 1433 | struct wt_status *s = cb; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1434 | const char *slot_name; |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1435 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1436 | if (starts_with(k, "column.")) |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1437 | return git_column_config(k, v, "status", &s->colopts); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1438 | if (!strcmp(k, "status.submodulesummary")) { |
| 1439 | int is_bool; |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1440 | s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi, |
| 1441 | &is_bool); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1442 | if (is_bool && s->submodule_summary) |
| 1443 | s->submodule_summary = -1; |
| 1444 | return 0; |
| 1445 | } |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1446 | if (!strcmp(k, "status.short")) { |
| 1447 | if (git_config_bool(k, v)) |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1448 | status_deferred_config.status_format = STATUS_FORMAT_SHORT; |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1449 | else |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1450 | status_deferred_config.status_format = STATUS_FORMAT_NONE; |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
Jorge Juan Garcia Garcia | ec85d07 | 2013-06-11 15:34:05 +0200 | [diff] [blame] | 1453 | if (!strcmp(k, "status.branch")) { |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1454 | status_deferred_config.show_branch = git_config_bool(k, v); |
Jorge Juan Garcia Garcia | ec85d07 | 2013-06-11 15:34:05 +0200 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1457 | if (!strcmp(k, "status.aheadbehind")) { |
| 1458 | status_deferred_config.ahead_behind = git_config_bool(k, v); |
| 1459 | return 0; |
| 1460 | } |
Liam Beguin | c1b5d01 | 2017-06-17 18:30:51 -0400 | [diff] [blame] | 1461 | if (!strcmp(k, "status.showstash")) { |
| 1462 | s->show_stash = git_config_bool(k, v); |
| 1463 | return 0; |
| 1464 | } |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1465 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 1466 | s->use_color = git_config_colorbool(k, v); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1467 | return 0; |
| 1468 | } |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 1469 | if (!strcmp(k, "status.displaycommentprefix")) { |
| 1470 | s->display_comment_prefix = git_config_bool(k, v); |
| 1471 | return 0; |
| 1472 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1473 | if (skip_prefix(k, "status.color.", &slot_name) || |
| 1474 | skip_prefix(k, "color.status.", &slot_name)) { |
Junio C Hamano | b946576 | 2014-10-20 12:23:48 -0700 | [diff] [blame] | 1475 | int slot = parse_status_slot(slot_name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 1476 | if (slot < 0) |
| 1477 | return 0; |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1478 | if (!v) |
| 1479 | return config_error_nonbool(k); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 1480 | return color_parse(v, s->color_palette[slot]); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1481 | } |
| 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 Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1487 | enum untracked_status_type u; |
| 1488 | |
Junio C Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1489 | u = parse_untracked_setting_name(v); |
| 1490 | if (u == SHOW_UNTRACKED_FILES_ERROR) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1491 | return error(_("Invalid untracked files mode '%s'"), v); |
Junio C Hamano | 63acdc4 | 2024-03-13 10:32:13 -0700 | [diff] [blame] | 1492 | s->show_untracked_files = u; |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1493 | return 0; |
| 1494 | } |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1495 | if (!strcmp(k, "diff.renamelimit")) { |
| 1496 | if (s->rename_limit == -1) |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1497 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | if (!strcmp(k, "status.renamelimit")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1501 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1502 | 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 Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1513 | return git_diff_ui_config(k, v, ctx, NULL); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
John Cai | 9b1cb50 | 2024-09-13 21:16:14 +0000 | [diff] [blame] | 1516 | int cmd_status(int argc, |
| 1517 | const char **argv, |
| 1518 | const char *prefix, |
| 1519 | struct repository *repo UNUSED) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1520 | { |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1521 | static int no_renames = -1; |
| 1522 | static const char *rename_score_arg = (const char *)-1; |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1523 | static struct wt_status s; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1524 | unsigned int progress_flag = 0; |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1525 | int fd; |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1526 | struct object_id oid; |
Junio C Hamano | 9e4b7ab | 2009-08-15 02:27:39 -0700 | [diff] [blame] | 1527 | static struct option builtin_status_options[] = { |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1528 | OPT__VERBOSE(&verbose, N_("be verbose")), |
Jeff King | dd2be24 | 2009-09-05 04:54:14 -0400 | [diff] [blame] | 1529 | OPT_SET_INT('s', "short", &status_format, |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1530 | N_("show status concisely"), STATUS_FORMAT_SHORT), |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1531 | OPT_BOOL('b', "branch", &s.show_branch, |
| 1532 | N_("show branch information")), |
Liam Beguin | c1b5d01 | 2017-06-17 18:30:51 -0400 | [diff] [blame] | 1533 | OPT_BOOL(0, "show-stash", &s.show_stash, |
| 1534 | N_("show stash information")), |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1535 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
| 1536 | N_("compute full ahead/behind values")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1537 | OPT_CALLBACK_F(0, "porcelain", &status_format, |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 1538 | N_("version"), N_("machine-readable output"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1539 | PARSE_OPT_OPTARG, opt_parse_porcelain), |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1540 | OPT_SET_INT(0, "long", &status_format, |
| 1541 | N_("show status in long format (default)"), |
| 1542 | STATUS_FORMAT_LONG), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1543 | OPT_BOOL('z', "null", &s.null_termination, |
| 1544 | N_("terminate entries with NUL")), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1545 | { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1546 | N_("mode"), |
| 1547 | N_("show untracked files, optional modes: all, normal, no. (Default: all)"), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1548 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1549 | { 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 Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1553 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"), |
| 1554 | N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 1555 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1556 | OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1557 | OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1558 | OPT_CALLBACK_F('M', "find-renames", &rename_score_arg, |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1559 | N_("n"), N_("detect renames, optionally set similarity index"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1560 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1561 | OPT_END(), |
| 1562 | }; |
| 1563 | |
Junio C Hamano | b821c99 | 2025-01-16 13:35:51 -0800 | [diff] [blame] | 1564 | show_usage_with_options_if_asked(argc, argv, |
| 1565 | builtin_status_usage, builtin_status_options); |
Nguyễn Thái Ngọc Duy | 5d3dd91 | 2010-10-22 01:45:47 -0500 | [diff] [blame] | 1566 | |
Derrick Stolee | d76723e | 2021-07-14 13:12:37 +0000 | [diff] [blame] | 1567 | prepare_repo_settings(the_repository); |
| 1568 | the_repository->settings.command_requires_full_index = 0; |
| 1569 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 1570 | status_init_config(&s, git_status_config); |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1571 | argc = parse_options(argc, argv, prefix, |
Junio C Hamano | 9e4b7ab | 2009-08-15 02:27:39 -0700 | [diff] [blame] | 1572 | builtin_status_options, |
| 1573 | builtin_status_usage, 0); |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1574 | finalize_colopts(&s.colopts, -1); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1575 | finalize_deferred_config(&s); |
Junio C Hamano | 6c92972 | 2011-06-06 11:40:08 -0700 | [diff] [blame] | 1576 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1577 | handle_untracked_files_arg(&s); |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1578 | 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 Duy | 15b55ae | 2013-07-14 15:35:39 +0700 | [diff] [blame] | 1584 | parse_pathspec(&s.pathspec, 0, |
| 1585 | PATHSPEC_PREFER_FULL, |
| 1586 | prefix, argv); |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1587 | |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1588 | if (status_format != STATUS_FORMAT_PORCELAIN && |
| 1589 | status_format != STATUS_FORMAT_PORCELAIN_V2) |
| 1590 | progress_flag = REFRESH_PROGRESS; |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 1591 | repo_read_index(the_repository); |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1592 | refresh_index(the_repository->index, |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1593 | REFRESH_QUIET|REFRESH_UNMERGED|progress_flag, |
| 1594 | &s.pathspec, NULL, NULL); |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1595 | |
Jeff King | 27344d6 | 2017-09-27 02:54:30 -0400 | [diff] [blame] | 1596 | if (use_optional_locks()) |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 1597 | fd = repo_hold_locked_index(the_repository, &index_lock, 0); |
Jeff King | 27344d6 | 2017-09-27 02:54:30 -0400 | [diff] [blame] | 1598 | else |
| 1599 | fd = -1; |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1600 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1601 | s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0; |
Jeff Hostetler | d9fc746 | 2016-08-11 10:45:59 -0400 | [diff] [blame] | 1602 | if (!s.is_initial) |
brian m. carlson | e0cb7cd | 2019-08-18 20:04:21 +0000 | [diff] [blame] | 1603 | oidcpy(&s.oid_commit, &oid); |
Jeff Hostetler | d9fc746 | 2016-08-11 10:45:59 -0400 | [diff] [blame] | 1604 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 1605 | s.ignore_submodule_arg = ignore_submodule_arg; |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1606 | s.status_format = status_format; |
| 1607 | s.verbose = verbose; |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1608 | 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 Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1616 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1617 | wt_status_collect(&s); |
| 1618 | |
Nguyễn Thái Ngọc Duy | 226c051 | 2015-03-08 17:12:41 +0700 | [diff] [blame] | 1619 | if (0 <= fd) |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 1620 | repo_update_index_if_able(the_repository, &index_lock); |
Nguyễn Thái Ngọc Duy | 226c051 | 2015-03-08 17:12:41 +0700 | [diff] [blame] | 1621 | |
Jeff King | 8661768 | 2009-12-07 00:26:25 -0500 | [diff] [blame] | 1622 | if (s.relative_paths) |
| 1623 | s.prefix = prefix; |
Markus Heidelberg | 38920dd | 2009-01-08 19:53:05 +0100 | [diff] [blame] | 1624 | |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1625 | wt_status_print(&s); |
Stephen P. Smith | 73ba5d7 | 2018-09-30 07:12:45 -0700 | [diff] [blame] | 1626 | wt_status_collect_free_buffers(&s); |
| 1627 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1628 | return 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1629 | } |
| 1630 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1631 | static int git_commit_config(const char *k, const char *v, |
| 1632 | const struct config_context *ctx, void *cb) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1633 | { |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1634 | struct wt_status *s = cb; |
| 1635 | |
Brian Hetro | 984c6e7 | 2008-07-05 01:24:40 -0400 | [diff] [blame] | 1636 | if (!strcmp(k, "commit.template")) |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 1637 | return git_config_pathname(&template_file, k, v); |
James P. Howard, II | bed575e | 2009-12-07 17:45:27 -0500 | [diff] [blame] | 1638 | if (!strcmp(k, "commit.status")) { |
| 1639 | include_status = git_config_bool(k, v); |
| 1640 | return 0; |
| 1641 | } |
Patrick Steinhardt | 6ef9f77 | 2024-11-05 07:17:20 +0100 | [diff] [blame] | 1642 | if (!strcmp(k, "commit.cleanup")) { |
| 1643 | FREE_AND_NULL(cleanup_config); |
| 1644 | return git_config_string(&cleanup_config, k, v); |
| 1645 | } |
Nicolas Vigier | d95bfb1 | 2013-11-05 00:14:41 +0100 | [diff] [blame] | 1646 | if (!strcmp(k, "commit.gpgsign")) { |
| 1647 | sign_commit = git_config_bool(k, v) ? "" : NULL; |
| 1648 | return 0; |
| 1649 | } |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1650 | if (!strcmp(k, "commit.verbose")) { |
| 1651 | int is_bool; |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1652 | config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi, |
| 1653 | &is_bool); |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1654 | return 0; |
| 1655 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1656 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1657 | return git_status_config(k, v, ctx, s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1658 | } |
| 1659 | |
John Cai | 9b1cb50 | 2024-09-13 21:16:14 +0000 | [diff] [blame] | 1660 | int cmd_commit(int argc, |
| 1661 | const char **argv, |
| 1662 | const char *prefix, |
| 1663 | struct repository *repo UNUSED) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1664 | { |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1665 | static struct wt_status s; |
Patrick Steinhardt | 6ef9f77 | 2024-11-05 07:17:20 +0100 | [diff] [blame] | 1666 | static const char *cleanup_arg = NULL; |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1667 | static struct option builtin_commit_options[] = { |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1668 | OPT__QUIET(&quiet, N_("suppress summary after successful commit")), |
| 1669 | OPT__VERBOSE(&verbose, N_("show diff in commit message template")), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1670 | |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1671 | 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 Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1678 | /* |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1679 | * TRANSLATORS: Leave "[(amend|reword):]" as-is, |
| 1680 | * and only translate <commit>. |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1681 | */ |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1682 | 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 Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1683 | OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1684 | OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), |
John Passaro | 56740f9 | 2024-05-05 18:49:08 +0000 | [diff] [blame] | 1685 | OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG), |
Bradley M. Kuhn | 3abd4a6 | 2020-10-19 18:03:55 -0700 | [diff] [blame] | 1686 | OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")), |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1687 | OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), |
| 1688 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), |
Denton Liu | ca04dc9 | 2019-04-17 11:23:26 +0100 | [diff] [blame] | 1689 | OPT_CLEANUP(&cleanup_arg), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1690 | OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")), |
Junio C Hamano | e703d71 | 2014-03-23 15:58:12 -0700 | [diff] [blame] | 1691 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1692 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1693 | /* end commit message options */ |
| 1694 | |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1695 | OPT_GROUP(N_("Commit contents options")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1696 | 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 Shaneh | def480f | 2016-07-26 17:00:15 +0300 | [diff] [blame] | 1701 | OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1702 | OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")), |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1703 | OPT_SET_INT(0, "short", &status_format, N_("show status concisely"), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1704 | STATUS_FORMAT_SHORT), |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1705 | OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")), |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1706 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
| 1707 | N_("compute full ahead/behind values")), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1708 | OPT_SET_INT(0, "porcelain", &status_format, |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1709 | N_("machine-readable output"), STATUS_FORMAT_PORCELAIN), |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1710 | OPT_SET_INT(0, "long", &status_format, |
| 1711 | N_("show status in long format (default)"), |
| 1712 | STATUS_FORMAT_LONG), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1713 | 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 Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1717 | { 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 Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 1718 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
| 1719 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1720 | /* end commit contents options */ |
| 1721 | |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 1722 | 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 King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1726 | |
| 1727 | OPT_END() |
| 1728 | }; |
| 1729 | |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1730 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 1731 | struct strbuf author_ident = STRBUF_INIT; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1732 | const char *index_file, *reflog_msg; |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1733 | struct object_id oid; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1734 | struct commit_list *parents = NULL; |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1735 | struct stat statbuf; |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1736 | struct commit *current_head = NULL; |
Junio C Hamano | ed7a42a | 2011-11-08 15:38:07 -0800 | [diff] [blame] | 1737 | struct commit_extra_header *extra = NULL; |
Ronnie Sahlberg | c0fe1ed | 2014-04-16 15:34:19 -0700 | [diff] [blame] | 1738 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1739 | int ret = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1740 | |
Junio C Hamano | b821c99 | 2025-01-16 13:35:51 -0800 | [diff] [blame] | 1741 | show_usage_with_options_if_asked(argc, argv, |
| 1742 | builtin_commit_usage, builtin_commit_options); |
Nguyễn Thái Ngọc Duy | 5d3dd91 | 2010-10-22 01:45:47 -0500 | [diff] [blame] | 1743 | |
Derrick Stolee | daa1ace | 2021-06-29 02:13:04 +0000 | [diff] [blame] | 1744 | prepare_repo_settings(the_repository); |
| 1745 | the_repository->settings.command_requires_full_index = 0; |
| 1746 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 1747 | status_init_config(&s, git_commit_config); |
Kaartic Sivaraam | 4ddb135 | 2017-06-21 23:46:14 +0530 | [diff] [blame] | 1748 | s.commit_template = 1; |
Ramkumar Ramachandra | f0915cb | 2013-06-24 18:15:12 +0530 | [diff] [blame] | 1749 | status_format = STATUS_FORMAT_NONE; /* Ignore status.short */ |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1750 | s.colopts = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1751 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1752 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1753 | current_head = NULL; |
| 1754 | else { |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 1755 | current_head = lookup_commit_or_die(&oid, "HEAD"); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1756 | if (repo_parse_commit(the_repository, current_head)) |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1757 | die(_("could not parse HEAD commit")); |
| 1758 | } |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1759 | verbose = -1; /* unspecified */ |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1760 | argc = parse_and_validate_options(argc, argv, builtin_commit_options, |
| 1761 | builtin_commit_usage, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1762 | prefix, current_head, &s); |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1763 | if (verbose == -1) |
| 1764 | verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose; |
| 1765 | |
Patrick Steinhardt | 6ef9f77 | 2024-11-05 07:17:20 +0100 | [diff] [blame] | 1766 | 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 King | c9bfb95 | 2011-08-17 22:05:35 -0700 | [diff] [blame] | 1772 | if (dry_run) |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1773 | return dry_run_commit(argv, prefix, current_head, &s); |
| 1774 | index_file = prepare_index(argv, prefix, current_head, 0); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1775 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1776 | /* Set up everything for writing the commit object. This includes |
| 1777 | running hooks, writing the trees, and interacting with the user. */ |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1778 | if (!prepare_to_commit(index_file, prefix, |
| 1779 | current_head, &s, &author_ident)) { |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1780 | ret = 1; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1781 | rollback_index_files(); |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1782 | goto cleanup; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1783 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1784 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1785 | /* Determine parents */ |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1786 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1787 | if (!current_head) { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1788 | if (!reflog_msg) |
| 1789 | reflog_msg = "commit (initial)"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1790 | } else if (amend) { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1791 | if (!reflog_msg) |
| 1792 | reflog_msg = "commit (amend)"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1793 | parents = copy_commit_list(current_head->parents); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1794 | } else if (whence == FROM_MERGE) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1795 | struct strbuf m = STRBUF_INIT; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1796 | FILE *fp; |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 1797 | int allow_fast_forward = 1; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1798 | struct commit_list **pptr = &parents; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1799 | |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1800 | if (!reflog_msg) |
| 1801 | reflog_msg = "commit (merge)"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1802 | pptr = commit_list_append(current_head, pptr); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1803 | fp = xfopen(git_path_merge_head(the_repository), "r"); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 1804 | while (strbuf_getline_lf(&m, fp) != EOF) { |
Junio C Hamano | 5231c63 | 2011-11-07 16:21:32 -0800 | [diff] [blame] | 1805 | struct commit *parent; |
| 1806 | |
| 1807 | parent = get_merge_parent(m.buf); |
| 1808 | if (!parent) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1809 | die(_("Corrupt MERGE_HEAD file (%s)"), m.buf); |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1810 | pptr = commit_list_append(parent, pptr); |
Linus Torvalds | 7c3fd25 | 2008-01-15 16:12:33 -0800 | [diff] [blame] | 1811 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1812 | fclose(fp); |
| 1813 | strbuf_release(&m); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1814 | 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ð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1816 | die_errno(_("could not read MERGE_MODE")); |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1817 | if (!strcmp(sb.buf, "no-ff")) |
| 1818 | allow_fast_forward = 0; |
| 1819 | } |
| 1820 | if (allow_fast_forward) |
Martin Ågren | 4da7264 | 2017-11-07 21:39:45 +0100 | [diff] [blame] | 1821 | reduce_heads_replace(&parents); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1822 | } else { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1823 | if (!reflog_msg) |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1824 | reflog_msg = is_from_cherry_pick(whence) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1825 | ? "commit (cherry-pick)" |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1826 | : is_from_rebase(whence) |
| 1827 | ? "commit (rebase)" |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1828 | : "commit"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1829 | commit_list_insert(current_head, &parents); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1830 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1831 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1832 | /* Finally, get the commit message */ |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1833 | strbuf_reset(&sb); |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 1834 | if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) { |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 1835 | int saved_errno = errno; |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 1836 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1837 | die(_("could not read commit message: %s"), strerror(saved_errno)); |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 1838 | } |
Kristian Høgsberg | 99a1269 | 2007-11-21 21:54:49 -0500 | [diff] [blame] | 1839 | |
Denton Liu | f29cd86 | 2019-04-17 11:23:25 +0100 | [diff] [blame] | 1840 | cleanup_message(&sb, cleanup_mode, verbose); |
Kaartic Sivaraam | bc17f35 | 2017-07-17 21:06:15 +0530 | [diff] [blame] | 1841 | |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 1842 | if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1843 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1844 | fprintf(stderr, _("Aborting commit due to empty commit message.\n")); |
Jeff King | fdc7c81 | 2008-07-31 03:36:09 -0400 | [diff] [blame] | 1845 | exit(1); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1846 | } |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 1847 | if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) { |
Kaartic Sivaraam | bc17f35 | 2017-07-17 21:06:15 +0530 | [diff] [blame] | 1848 | rollback_index_files(); |
| 1849 | fprintf(stderr, _("Aborting commit; you did not edit the message.\n")); |
| 1850 | exit(1); |
| 1851 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1852 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1853 | 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 Hamano | 0074d18 | 2011-12-20 13:20:56 -0800 | [diff] [blame] | 1866 | if (amend) { |
brian m. carlson | 42d4e1d | 2020-02-22 20:17:42 +0000 | [diff] [blame] | 1867 | const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL }; |
Junio C Hamano | c871a1d | 2012-01-05 10:54:14 -0800 | [diff] [blame] | 1868 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
Junio C Hamano | 0074d18 | 2011-12-20 13:20:56 -0800 | [diff] [blame] | 1869 | } else { |
| 1870 | struct commit_extra_header **tail = &extra; |
| 1871 | append_merge_tag_headers(parents, &tail); |
| 1872 | } |
Junio C Hamano | ed7a42a | 2011-11-08 15:38:07 -0800 | [diff] [blame] | 1873 | |
Patrick Steinhardt | f59aa5e | 2024-04-18 14:14:14 +0200 | [diff] [blame] | 1874 | if (commit_tree_extended(sb.buf, sb.len, &the_repository->index->cache_tree->oid, |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 1875 | parents, &oid, author_ident.buf, NULL, |
| 1876 | sign_commit, extra)) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1877 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1878 | die(_("failed to write commit object")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1879 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1880 | |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 1881 | if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb, |
| 1882 | &err)) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1883 | rollback_index_files(); |
Ronnie Sahlberg | c0fe1ed | 2014-04-16 15:34:19 -0700 | [diff] [blame] | 1884 | die("%s", err.buf); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1885 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1886 | |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 1887 | sequencer_post_commit_cleanup(the_repository, 0); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1888 | 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øgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1892 | |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 1893 | if (commit_index_files()) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 1894 | die(_("repository has been updated, but unable to write\n" |
Junio C Hamano | a060705 | 2023-10-17 20:08:51 -0700 | [diff] [blame] | 1895 | "new index file. Check that disk is not full and quota is\n" |
Nguyễn Thái Ngọc Duy | 80f537f | 2019-04-25 16:45:58 +0700 | [diff] [blame] | 1896 | "not exceeded, and then \"git restore --staged :/\" to recover.")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1897 | |
Derrick Stolee | b23ea97 | 2020-04-16 20:14:03 +0000 | [diff] [blame] | 1898 | git_test_write_commit_graph_or_die(); |
Derrick Stolee | 859fdc0 | 2018-08-29 05:49:04 -0700 | [diff] [blame] | 1899 | |
Nguyễn Thái Ngọc Duy | 35843b1 | 2018-09-21 17:57:32 +0200 | [diff] [blame] | 1900 | repo_rerere(the_repository, 0); |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1901 | run_auto_maintenance(quiet); |
Patrick Steinhardt | 1dc4ec2 | 2024-09-12 13:29:32 +0200 | [diff] [blame] | 1902 | run_commit_hook(use_editor, repo_get_index_file(the_repository), |
| 1903 | NULL, "post-commit", NULL); |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 1904 | if (amend && !no_post_rewrite) { |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 1905 | commit_post_rewrite(the_repository, current_head, &oid); |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 1906 | } |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1907 | 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 Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1914 | print_commit_summary(the_repository, prefix, |
| 1915 | &oid, flags); |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1916 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1917 | |
Patrick Steinhardt | 3f921c7 | 2024-01-19 11:40:19 +0100 | [diff] [blame] | 1918 | apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); |
Denton Liu | a03b555 | 2020-04-07 10:28:07 -0400 | [diff] [blame] | 1919 | |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1920 | cleanup: |
Patrick Steinhardt | 63c9bd3 | 2024-06-11 11:20:42 +0200 | [diff] [blame] | 1921 | free_commit_extra_headers(extra); |
| 1922 | free_commit_list(parents); |
Ævar Arnfjörð Bjarmason | ac95f5d | 2022-11-08 19:17:51 +0100 | [diff] [blame] | 1923 | strbuf_release(&author_ident); |
| 1924 | strbuf_release(&err); |
| 1925 | strbuf_release(&sb); |
Patrick Steinhardt | 14da262 | 2024-06-11 11:19:22 +0200 | [diff] [blame] | 1926 | free(logfile); |
| 1927 | free(template_file); |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1928 | return ret; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1929 | } |