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 | */ |
| 7 | |
Ævar Arnfjörð Bjarmason | dfd0a89 | 2023-02-10 11:28:39 +0100 | [diff] [blame] | 8 | #define USE_THE_INDEX_VARIABLE |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 9 | #include "builtin.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 10 | #include "advice.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 11 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 12 | #include "lockfile.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 13 | #include "cache-tree.h" |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 14 | #include "color.h" |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 15 | #include "dir.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 16 | #include "editor.h" |
Elijah Newren | 7ee24e1 | 2023-03-21 06:25:57 +0000 | [diff] [blame] | 17 | #include "environment.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 18 | #include "diff.h" |
| 19 | #include "diffcore.h" |
| 20 | #include "commit.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 21 | #include "gettext.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 22 | #include "revision.h" |
| 23 | #include "wt-status.h" |
| 24 | #include "run-command.h" |
Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 25 | #include "hook.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 26 | #include "refs.h" |
| 27 | #include "log-tree.h" |
| 28 | #include "strbuf.h" |
| 29 | #include "utf8.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 30 | #include "object-name.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 31 | #include "parse-options.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 32 | #include "path.h" |
Elijah Newren | fbffdfb | 2023-05-16 06:33:52 +0000 | [diff] [blame] | 33 | #include "preload-index.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 34 | #include "read-cache.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 35 | #include "string-list.h" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 36 | #include "rerere.h" |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 37 | #include "unpack-trees.h" |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 38 | #include "quote.h" |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 39 | #include "submodule.h" |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 40 | #include "gpg-interface.h" |
Nguyễn Thái Ngọc Duy | 323d053 | 2012-04-13 17:54:39 +0700 | [diff] [blame] | 41 | #include "column.h" |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 42 | #include "sequencer.h" |
Elijah Newren | baf889c | 2023-05-16 06:33:51 +0000 | [diff] [blame] | 43 | #include "sparse-index.h" |
Antoine Pelisse | ea16794 | 2013-08-23 15:48:31 +0200 | [diff] [blame] | 44 | #include "mailmap.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 45 | #include "help.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 46 | #include "commit-reach.h" |
Derrick Stolee | 859fdc0 | 2018-08-29 05:49:04 -0700 | [diff] [blame] | 47 | #include "commit-graph.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 48 | #include "pretty.h" |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 49 | |
| 50 | static const char * const builtin_commit_usage[] = { |
Ævar Arnfjörð Bjarmason | 423be1f | 2022-10-13 17:39:23 +0200 | [diff] [blame] | 51 | N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n" |
| 52 | " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n" |
| 53 | " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n" |
| 54 | " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n" |
| 55 | " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n" |
| 56 | " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" |
| 57 | " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n" |
| 58 | " [--] [<pathspec>...]"), |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 59 | NULL |
| 60 | }; |
| 61 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 62 | static const char * const builtin_status_usage[] = { |
Ævar Arnfjörð Bjarmason | 463ea0c | 2022-10-13 17:39:21 +0200 | [diff] [blame] | 63 | N_("git status [<options>] [--] [<pathspec>...]"), |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 64 | NULL |
| 65 | }; |
| 66 | |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 67 | static const char empty_amend_advice[] = |
Ævar Arnfjörð Bjarmason | fc88e31 | 2011-02-22 23:41:49 +0000 | [diff] [blame] | 68 | 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] | 69 | "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] | 70 | "remove the commit entirely with \"git reset HEAD^\".\n"); |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 71 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 72 | static const char empty_cherry_pick_advice[] = |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 73 | 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] | 74 | "If you wish to commit it anyway, use:\n" |
| 75 | "\n" |
| 76 | " git commit --allow-empty\n" |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 77 | "\n"); |
| 78 | |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 79 | static const char empty_rebase_pick_advice[] = |
| 80 | N_("Otherwise, please use 'git rebase --skip'\n"); |
| 81 | |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 82 | static const char empty_cherry_pick_advice_single[] = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 83 | N_("Otherwise, please use 'git cherry-pick --skip'\n"); |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 84 | |
| 85 | static const char empty_cherry_pick_advice_multi[] = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 86 | N_("and then use:\n" |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 87 | "\n" |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 88 | " git cherry-pick --continue\n" |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 89 | "\n" |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 90 | "to resume cherry-picking the remaining commits.\n" |
| 91 | "If you wish to skip this commit, use:\n" |
| 92 | "\n" |
| 93 | " git cherry-pick --skip\n" |
| 94 | "\n"); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 95 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 96 | static const char *color_status_slots[] = { |
| 97 | [WT_STATUS_HEADER] = "header", |
| 98 | [WT_STATUS_UPDATED] = "updated", |
| 99 | [WT_STATUS_CHANGED] = "changed", |
| 100 | [WT_STATUS_UNTRACKED] = "untracked", |
| 101 | [WT_STATUS_NOBRANCH] = "noBranch", |
| 102 | [WT_STATUS_UNMERGED] = "unmerged", |
| 103 | [WT_STATUS_LOCAL_BRANCH] = "localBranch", |
| 104 | [WT_STATUS_REMOTE_BRANCH] = "remoteBranch", |
| 105 | [WT_STATUS_ONBRANCH] = "branch", |
| 106 | }; |
| 107 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 108 | static const char *use_message_buffer; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 109 | static struct lock_file index_lock; /* real index */ |
| 110 | static struct lock_file false_lock; /* used only for partial commits */ |
| 111 | static enum { |
| 112 | COMMIT_AS_IS = 1, |
| 113 | COMMIT_NORMAL, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 114 | COMMIT_PARTIAL |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 115 | } commit_style; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 116 | |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 117 | static const char *logfile, *force_author; |
Brian Hetro | 984c6e7 | 2008-07-05 01:24:40 -0400 | [diff] [blame] | 118 | static const char *template_file; |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 119 | /* |
| 120 | * The _message variables are commit names from which to take |
| 121 | * the commit message and/or authorship. |
| 122 | */ |
| 123 | static const char *author_message, *author_message_buffer; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 124 | static char *edit_message, *use_message; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 125 | static char *fixup_message, *fixup_commit, *squash_message; |
| 126 | static const char *fixup_prefix; |
Junio C Hamano | ca1ba20 | 2011-12-06 13:09:55 -0800 | [diff] [blame] | 127 | static int all, also, interactive, patch_interactive, only, amend, signoff; |
| 128 | static int edit_flag = -1; /* unspecified */ |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 129 | static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 130 | static int config_commit_verbose = -1; /* unspecified */ |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 131 | static int no_post_rewrite, allow_empty_message, pathspec_file_nul; |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 132 | static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg; |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 133 | static char *sign_commit, *pathspec_from_file; |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 134 | static struct strvec trailer_args = STRVEC_INIT; |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 135 | |
Alex Riesen | 5f06573 | 2007-12-22 19:46:24 +0100 | [diff] [blame] | 136 | /* |
| 137 | * The default commit message cleanup mode will remove the lines |
| 138 | * beginning with # (shell comments) and leading and trailing |
| 139 | * whitespaces (empty lines or containing only whitespaces) |
| 140 | * if editor is used, and only the whitespaces if the message |
| 141 | * is specified explicitly. |
| 142 | */ |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 143 | static enum commit_msg_cleanup_mode cleanup_mode; |
Ralf Thielow | 51fb3a3 | 2013-01-10 18:45:59 +0100 | [diff] [blame] | 144 | static const char *cleanup_arg; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 145 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 146 | static enum commit_whence whence; |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 147 | static int use_editor = 1, include_status = 1; |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 148 | static int have_option_m; |
Jeff King | 2c47789 | 2011-12-18 00:03:22 -0500 | [diff] [blame] | 149 | static struct strbuf message = STRBUF_INIT; |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 150 | |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 151 | static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 152 | |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 153 | static int opt_pass_trailer(const struct option *opt, const char *arg, int unset) |
| 154 | { |
| 155 | BUG_ON_OPT_NEG(unset); |
| 156 | |
Jeff King | 116761b | 2022-10-06 09:11:31 -0400 | [diff] [blame] | 157 | strvec_pushl(opt->value, "--trailer", arg, NULL); |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 161 | static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset) |
| 162 | { |
| 163 | enum wt_status_format *value = (enum wt_status_format *)opt->value; |
| 164 | if (unset) |
| 165 | *value = STATUS_FORMAT_NONE; |
| 166 | else if (!arg) |
| 167 | *value = STATUS_FORMAT_PORCELAIN; |
| 168 | else if (!strcmp(arg, "v1") || !strcmp(arg, "1")) |
| 169 | *value = STATUS_FORMAT_PORCELAIN; |
Jeff Hostetler | 1ecdecc | 2016-08-11 10:45:57 -0400 | [diff] [blame] | 170 | else if (!strcmp(arg, "v2") || !strcmp(arg, "2")) |
| 171 | *value = STATUS_FORMAT_PORCELAIN_V2; |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 172 | else |
| 173 | die("unsupported porcelain version '%s'", arg); |
| 174 | |
| 175 | return 0; |
| 176 | } |
Jeff King | 7c9f703 | 2009-09-05 04:59:56 -0400 | [diff] [blame] | 177 | |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 178 | static int opt_parse_m(const struct option *opt, const char *arg, int unset) |
| 179 | { |
| 180 | struct strbuf *buf = opt->value; |
René Scharfe | 2520677 | 2013-05-25 23:43:34 +0200 | [diff] [blame] | 181 | if (unset) { |
| 182 | have_option_m = 0; |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 183 | strbuf_setlen(buf, 0); |
René Scharfe | 2520677 | 2013-05-25 23:43:34 +0200 | [diff] [blame] | 184 | } else { |
| 185 | have_option_m = 1; |
Brandon Casey | a24a41e | 2013-02-18 20:17:06 -0800 | [diff] [blame] | 186 | if (buf->len) |
| 187 | strbuf_addch(buf, '\n'); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 188 | strbuf_addstr(buf, arg); |
Brandon Casey | a24a41e | 2013-02-18 20:17:06 -0800 | [diff] [blame] | 189 | strbuf_complete_line(buf); |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 190 | } |
| 191 | return 0; |
| 192 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 193 | |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 194 | static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset) |
| 195 | { |
| 196 | const char **value = opt->value; |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 197 | |
| 198 | BUG_ON_OPT_NEG(unset); |
| 199 | |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 200 | if (arg != NULL && *arg == '=') |
| 201 | arg = arg + 1; |
| 202 | |
| 203 | *value = arg; |
| 204 | return 0; |
| 205 | } |
| 206 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 207 | static void determine_whence(struct wt_status *s) |
| 208 | { |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 209 | if (file_exists(git_path_merge_head(the_repository))) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 210 | whence = FROM_MERGE; |
Phillip Wood | 901ba7b | 2019-12-06 16:06:11 +0000 | [diff] [blame] | 211 | else if (!sequencer_determine_whence(the_repository, &whence)) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 212 | whence = FROM_COMMIT; |
| 213 | if (s) |
| 214 | s->whence = whence; |
| 215 | } |
| 216 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 217 | static void status_init_config(struct wt_status *s, config_fn_t fn) |
| 218 | { |
Nguyễn Thái Ngọc Duy | 5b02ca3 | 2018-11-10 06:48:49 +0100 | [diff] [blame] | 219 | wt_status_prepare(the_repository, s); |
Eckhard S. Maaß | dc6b1d9 | 2018-05-04 13:12:15 +0200 | [diff] [blame] | 220 | init_diff_ui_defaults(); |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 221 | git_config(fn, s); |
| 222 | determine_whence(s); |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 223 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); /* must come after git_config() */ |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 226 | static void rollback_index_files(void) |
| 227 | { |
| 228 | switch (commit_style) { |
| 229 | case COMMIT_AS_IS: |
| 230 | break; /* nothing to do */ |
| 231 | case COMMIT_NORMAL: |
| 232 | rollback_lock_file(&index_lock); |
| 233 | break; |
| 234 | case COMMIT_PARTIAL: |
| 235 | rollback_lock_file(&index_lock); |
| 236 | rollback_lock_file(&false_lock); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 241 | static int commit_index_files(void) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 242 | { |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 243 | int err = 0; |
| 244 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 245 | switch (commit_style) { |
| 246 | case COMMIT_AS_IS: |
| 247 | break; /* nothing to do */ |
| 248 | case COMMIT_NORMAL: |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 249 | err = commit_lock_file(&index_lock); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 250 | break; |
| 251 | case COMMIT_PARTIAL: |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 252 | err = commit_lock_file(&index_lock); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 253 | rollback_lock_file(&false_lock); |
| 254 | break; |
| 255 | } |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 256 | |
| 257 | return err; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Take a union of paths in the index and the named tree (typically, "HEAD"), |
| 262 | * and return the paths that match the given pattern in list. |
| 263 | */ |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 264 | static int list_paths(struct string_list *list, const char *with_tree, |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 265 | const struct pathspec *pattern) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 266 | { |
Stefan Beller | 5d0b9bf | 2015-03-20 17:28:07 -0700 | [diff] [blame] | 267 | int i, ret; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 268 | char *m; |
| 269 | |
Nguyễn Thái Ngọc Duy | 17ddc66 | 2013-07-14 15:35:53 +0700 | [diff] [blame] | 270 | if (!pattern->nr) |
Junio C Hamano | b9a0801 | 2012-07-15 21:39:48 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | |
Nguyễn Thái Ngọc Duy | 17ddc66 | 2013-07-14 15:35:53 +0700 | [diff] [blame] | 273 | m = xcalloc(1, pattern->nr); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 274 | |
Clemens Buchacher | 8894d53 | 2011-07-30 19:13:47 +0200 | [diff] [blame] | 275 | if (with_tree) { |
Clemens Buchacher | f950eb9 | 2011-09-04 12:42:01 +0200 | [diff] [blame] | 276 | char *max_prefix = common_prefix(pattern); |
Brandon Williams | 86238e0 | 2018-04-03 10:57:45 -0700 | [diff] [blame] | 277 | overlay_tree_on_index(&the_index, with_tree, max_prefix); |
Clemens Buchacher | 5879f56 | 2011-09-04 12:41:59 +0200 | [diff] [blame] | 278 | free(max_prefix); |
Clemens Buchacher | 8894d53 | 2011-07-30 19:13:47 +0200 | [diff] [blame] | 279 | } |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 280 | |
Derrick Stolee | cb8388d | 2021-04-01 01:49:45 +0000 | [diff] [blame] | 281 | /* TODO: audit for interaction with sparse-index. */ |
| 282 | ensure_full_index(&the_index); |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 283 | for (i = 0; i < the_index.cache_nr; i++) { |
| 284 | const struct cache_entry *ce = the_index.cache[i]; |
Nguyễn Thái Ngọc Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 285 | struct string_list_item *item; |
| 286 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 287 | if (ce->ce_flags & CE_UPDATE) |
Junio C Hamano | e87e22d | 2008-01-14 13:54:24 -0800 | [diff] [blame] | 288 | continue; |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 289 | if (!ce_path_match(&the_index, ce, pattern, m)) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 290 | continue; |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 291 | item = string_list_insert(list, ce->name); |
Nguyễn Thái Ngọc Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 292 | if (ce_skip_worktree(ce)) |
| 293 | item->util = item; /* better a valid pointer than a fake one */ |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 296 | ret = report_path_error(m, pattern); |
Stefan Beller | 5d0b9bf | 2015-03-20 17:28:07 -0700 | [diff] [blame] | 297 | free(m); |
| 298 | return ret; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 301 | static void add_remove_files(struct string_list *list) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 302 | { |
| 303 | int i; |
| 304 | for (i = 0; i < list->nr; i++) { |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 305 | struct stat st; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 306 | struct string_list_item *p = &(list->items[i]); |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 307 | |
Nguyễn Thái Ngọc Duy | 7fce6e3 | 2009-12-14 18:43:59 +0700 | [diff] [blame] | 308 | /* p->util is skip-worktree */ |
| 309 | if (p->util) |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 310 | continue; |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 311 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 312 | if (!lstat(p->string, &st)) { |
Ævar Arnfjörð Bjarmason | fbc1ed6 | 2022-11-19 14:07:30 +0100 | [diff] [blame] | 313 | if (add_to_index(&the_index, p->string, &st, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 314 | die(_("updating files failed")); |
Alex Riesen | 960b8ad | 2008-05-12 19:57:45 +0200 | [diff] [blame] | 315 | } else |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 316 | remove_file_from_index(&the_index, p->string); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 320 | static void create_base_index(const struct commit *current_head) |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 321 | { |
| 322 | struct tree *tree; |
| 323 | struct unpack_trees_options opts; |
| 324 | struct tree_desc t; |
| 325 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 326 | if (!current_head) { |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 327 | discard_index(&the_index); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
| 331 | memset(&opts, 0, sizeof(opts)); |
| 332 | opts.head_idx = 1; |
| 333 | opts.index_only = 1; |
| 334 | opts.merge = 1; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 335 | opts.src_index = &the_index; |
| 336 | opts.dst_index = &the_index; |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 337 | |
| 338 | opts.fn = oneway_merge; |
brian m. carlson | a9dbc17 | 2017-05-06 22:10:37 +0000 | [diff] [blame] | 339 | tree = parse_tree_indirect(¤t_head->object.oid); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 340 | if (!tree) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 341 | die(_("failed to unpack HEAD tree object")); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 342 | parse_tree(tree); |
| 343 | init_tree_desc(&t, tree->buffer, tree->size); |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 344 | if (unpack_trees(1, &t, &opts)) |
| 345 | exit(128); /* We've already reported the error, finish dying */ |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 348 | static void refresh_cache_or_die(int refresh_flags) |
| 349 | { |
| 350 | /* |
| 351 | * refresh_flags contains REFRESH_QUIET, so the only errors |
| 352 | * are for unmerged entries. |
| 353 | */ |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 354 | if (refresh_index(&the_index, refresh_flags | REFRESH_IN_PORCELAIN, NULL, NULL, NULL)) |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 355 | die_resolve_conflict("commit"); |
| 356 | } |
| 357 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 358 | static const char *prepare_index(const char **argv, const char *prefix, |
Michael Haggerty | 35ff08b | 2014-10-01 12:28:17 +0200 | [diff] [blame] | 359 | const struct commit *current_head, int is_status) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 360 | { |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 361 | 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] | 362 | struct pathspec pathspec; |
Junio C Hamano | 50b7e70 | 2009-08-04 23:49:33 -0700 | [diff] [blame] | 363 | int refresh_flags = REFRESH_QUIET; |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 364 | const char *ret; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 365 | |
Junio C Hamano | 50b7e70 | 2009-08-04 23:49:33 -0700 | [diff] [blame] | 366 | if (is_status) |
| 367 | refresh_flags |= REFRESH_UNMERGED; |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 368 | parse_pathspec(&pathspec, 0, |
| 369 | PATHSPEC_PREFER_FULL, |
| 370 | prefix, argv); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 371 | |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 372 | if (pathspec_from_file) { |
| 373 | if (interactive) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 374 | 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] | 375 | |
Alexandr Miloslavskiy | 509efef | 2019-12-16 15:47:52 +0000 | [diff] [blame] | 376 | if (all) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 377 | 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] | 378 | |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 379 | if (pathspec.nr) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 380 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 381 | |
| 382 | parse_pathspec_file(&pathspec, 0, |
| 383 | PATHSPEC_PREFER_FULL, |
| 384 | prefix, pathspec_from_file, pathspec_file_nul); |
| 385 | } else if (pathspec_file_nul) { |
Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 386 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 389 | if (!pathspec.nr && (also || (only && !allow_empty && |
| 390 | (!amend || (fixup_message && strcmp(fixup_prefix, "amend")))))) |
Alexandr Miloslavskiy | e440fc5 | 2019-11-19 16:48:55 +0000 | [diff] [blame] | 391 | die(_("No paths with --include/--only does not make sense.")); |
| 392 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 393 | if (repo_read_index_preload(the_repository, &pathspec, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 394 | die(_("index file corrupt")); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 395 | |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 396 | if (interactive) { |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 397 | char *old_index_env = NULL, *old_repo_index_file; |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 398 | repo_hold_locked_index(the_repository, &index_lock, |
| 399 | LOCK_DIE_ON_ERROR); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 400 | |
| 401 | refresh_cache_or_die(refresh_flags); |
| 402 | |
Martin Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 403 | if (write_locked_index(&the_index, &index_lock, 0)) |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 404 | die(_("unable to create temporary index")); |
| 405 | |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 406 | old_repo_index_file = the_repository->index_file; |
| 407 | the_repository->index_file = |
| 408 | (char *)get_lock_file_path(&index_lock); |
Jeff King | 406bab3 | 2019-01-11 17:15:40 -0500 | [diff] [blame] | 409 | old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT)); |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 410 | setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 411 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 412 | if (interactive_add(argv, prefix, patch_interactive) != 0) |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 413 | die(_("interactive add failed")); |
| 414 | |
Johannes Schindelin | c480eeb | 2019-12-21 21:57:16 +0000 | [diff] [blame] | 415 | the_repository->index_file = old_repo_index_file; |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 416 | if (old_index_env && *old_index_env) |
| 417 | setenv(INDEX_ENVIRONMENT, old_index_env, 1); |
| 418 | else |
| 419 | unsetenv(INDEX_ENVIRONMENT); |
Jeff King | 406bab3 | 2019-01-11 17:15:40 -0500 | [diff] [blame] | 420 | FREE_AND_NULL(old_index_env); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 421 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 422 | discard_index(&the_index); |
| 423 | read_index_from(&the_index, get_lock_file_path(&index_lock), |
| 424 | get_git_dir()); |
Ævar Arnfjörð Bjarmason | fcb864b | 2023-02-10 11:28:38 +0100 | [diff] [blame] | 425 | if (cache_tree_update(&the_index, WRITE_TREE_SILENT) == 0) { |
Junio C Hamano | 3fd13cb | 2014-09-11 10:33:32 -0700 | [diff] [blame] | 426 | if (reopen_lock_file(&index_lock) < 0) |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 427 | die(_("unable to write index file")); |
Martin Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 428 | if (write_locked_index(&the_index, &index_lock, 0)) |
Junio C Hamano | 3fd13cb | 2014-09-11 10:33:32 -0700 | [diff] [blame] | 429 | die(_("unable to update temporary index")); |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 430 | } else |
| 431 | warning(_("Failed to update main cache tree")); |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 432 | |
| 433 | commit_style = COMMIT_NORMAL; |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 434 | ret = get_lock_file_path(&index_lock); |
| 435 | goto out; |
Conrad Irwin | 1020d08 | 2011-05-06 22:59:59 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 438 | /* |
| 439 | * Non partial, non as-is commit. |
| 440 | * |
| 441 | * (1) get the real index; |
| 442 | * (2) update the_index as necessary; |
| 443 | * (3) write the_index out to the real index (still locked); |
| 444 | * (4) return the name of the locked index file. |
| 445 | * |
| 446 | * The caller should run hooks on the locked real index, and |
| 447 | * (A) if all goes well, commit the real index; |
| 448 | * (B) on failure, rollback the real index. |
| 449 | */ |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 450 | if (all || (also && pathspec.nr)) { |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 451 | repo_hold_locked_index(the_repository, &index_lock, |
| 452 | LOCK_DIE_ON_ERROR); |
Elijah Newren | 50c37ee | 2023-05-16 06:33:46 +0000 | [diff] [blame] | 453 | add_files_to_cache(the_repository, also ? prefix : NULL, |
| 454 | &pathspec, 0, 0); |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 455 | refresh_cache_or_die(refresh_flags); |
Ævar Arnfjörð Bjarmason | fcb864b | 2023-02-10 11:28:38 +0100 | [diff] [blame] | 456 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
Martin Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 457 | if (write_locked_index(&the_index, &index_lock, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 458 | die(_("unable to write new_index file")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 459 | commit_style = COMMIT_NORMAL; |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 460 | ret = get_lock_file_path(&index_lock); |
| 461 | goto out; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 464 | /* |
| 465 | * As-is commit. |
| 466 | * |
| 467 | * (1) return the name of the real index file. |
| 468 | * |
Markus Heidelberg | 7327623 | 2010-04-02 14:27:18 +0200 | [diff] [blame] | 469 | * The caller should run hooks on the real index, |
| 470 | * and create commit from the_index. |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 471 | * We still need to refresh the index here. |
| 472 | */ |
Nguyễn Thái Ngọc Duy | 6654c88 | 2013-07-14 15:35:38 +0700 | [diff] [blame] | 473 | if (!only && !pathspec.nr) { |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 474 | repo_hold_locked_index(the_repository, &index_lock, |
| 475 | LOCK_DIE_ON_ERROR); |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 476 | refresh_cache_or_die(refresh_flags); |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 477 | if (the_index.cache_changed |
| 478 | || !cache_tree_fully_valid(the_index.cache_tree)) |
Ævar Arnfjörð Bjarmason | fcb864b | 2023-02-10 11:28:38 +0100 | [diff] [blame] | 479 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 480 | if (write_locked_index(&the_index, &index_lock, |
| 481 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 482 | die(_("unable to write new_index file")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 483 | commit_style = COMMIT_AS_IS; |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 484 | ret = get_index_file(); |
| 485 | goto out; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 486 | } |
| 487 | |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 488 | /* |
| 489 | * A partial commit. |
| 490 | * |
| 491 | * (0) find the set of affected paths; |
| 492 | * (1) get lock on the real index file; |
| 493 | * (2) update the_index with the given paths; |
| 494 | * (3) write the_index out to the real index (still locked); |
| 495 | * (4) get lock on the false index file; |
| 496 | * (5) reset the_index from HEAD; |
| 497 | * (6) update the_index the same way as (2); |
| 498 | * (7) write the_index out to the false index file; |
| 499 | * (8) return the name of the false index file (still locked); |
| 500 | * |
| 501 | * The caller should run hooks on the locked false index, and |
| 502 | * create commit from it. Then |
| 503 | * (A) if all goes well, commit the real index; |
| 504 | * (B) on failure, rollback the real index; |
| 505 | * In either case, rollback the false index. |
| 506 | */ |
| 507 | commit_style = COMMIT_PARTIAL; |
| 508 | |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 509 | if (whence != FROM_COMMIT) { |
| 510 | if (whence == FROM_MERGE) |
| 511 | die(_("cannot do a partial commit during a merge.")); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 512 | else if (is_from_cherry_pick(whence)) |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 513 | die(_("cannot do a partial commit during a cherry-pick.")); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 514 | else if (is_from_rebase(whence)) |
| 515 | die(_("cannot do a partial commit during a rebase.")); |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 516 | } |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 517 | |
Jeff King | c5c3350 | 2019-03-20 04:15:48 -0400 | [diff] [blame] | 518 | if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec)) |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 519 | exit(1); |
| 520 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 521 | discard_index(&the_index); |
| 522 | if (repo_read_index(the_repository) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 523 | die(_("cannot read the index")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 524 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 525 | 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] | 526 | add_remove_files(&partial); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 527 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
Ævar Arnfjörð Bjarmason | fcb864b | 2023-02-10 11:28:38 +0100 | [diff] [blame] | 528 | cache_tree_update(&the_index, WRITE_TREE_SILENT); |
Martin Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 529 | if (write_locked_index(&the_index, &index_lock, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 530 | die(_("unable to write new_index file")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 531 | |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 532 | hold_lock_file_for_update(&false_lock, |
| 533 | git_path("next-index-%"PRIuMAX, |
| 534 | (uintmax_t) getpid()), |
| 535 | LOCK_DIE_ON_ERROR); |
Linus Torvalds | fa9dcf8 | 2008-01-13 00:30:56 -0800 | [diff] [blame] | 536 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 537 | create_base_index(current_head); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 538 | add_remove_files(&partial); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 539 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 540 | |
Martin Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 541 | if (write_locked_index(&the_index, &false_lock, 0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 542 | die(_("unable to write temporary index file")); |
Jeff King | 959ba67 | 2008-02-14 12:18:23 -0500 | [diff] [blame] | 543 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 544 | discard_index(&the_index); |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 545 | ret = get_lock_file_path(&false_lock); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 546 | read_index_from(&the_index, ret, get_git_dir()); |
Martin Ågren | dd1055e | 2017-09-23 01:34:49 +0200 | [diff] [blame] | 547 | out: |
| 548 | string_list_clear(&partial, 0); |
| 549 | clear_pathspec(&pathspec); |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 550 | return ret; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 553 | static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn, |
| 554 | struct wt_status *s) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 555 | { |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 556 | struct object_id oid; |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 557 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 558 | if (s->relative_paths) |
| 559 | s->prefix = prefix; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 560 | |
| 561 | if (amend) { |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 562 | s->amend = 1; |
| 563 | s->reference = "HEAD^1"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 564 | } |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 565 | s->verbose = verbose; |
| 566 | s->index_file = index_file; |
| 567 | s->fp = fp; |
| 568 | s->nowarn = nowarn; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 569 | 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] | 570 | if (!s->is_initial) |
brian m. carlson | e0cb7cd | 2019-08-18 20:04:21 +0000 | [diff] [blame] | 571 | oidcpy(&s->oid_commit, &oid); |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 572 | s->status_format = status_format; |
| 573 | s->ignore_submodule_arg = ignore_submodule_arg; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 574 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 575 | wt_status_collect(s); |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 576 | wt_status_print(s); |
Stephen P. Smith | 73ba5d7 | 2018-09-30 07:12:45 -0700 | [diff] [blame] | 577 | wt_status_collect_free_buffers(s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 578 | |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 579 | return s->committable; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 580 | } |
| 581 | |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 582 | static int is_a_merge(const struct commit *current_head) |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 583 | { |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 584 | return !!(current_head->parents && current_head->parents->next); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 587 | static void assert_split_ident(struct ident_split *id, const struct strbuf *buf) |
| 588 | { |
Jeff King | c83a509 | 2014-12-10 10:43:42 -0500 | [diff] [blame] | 589 | if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 590 | BUG("unable to parse our own ident: %s", buf->buf); |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 591 | } |
| 592 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 593 | static void export_one(const char *var, const char *s, const char *e, int hack) |
| 594 | { |
| 595 | struct strbuf buf = STRBUF_INIT; |
| 596 | if (hack) |
| 597 | strbuf_addch(&buf, hack); |
René Scharfe | 147ee35 | 2019-12-07 12:16:04 +0100 | [diff] [blame] | 598 | strbuf_add(&buf, s, e - s); |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 599 | setenv(var, buf.buf, 1); |
| 600 | strbuf_release(&buf); |
| 601 | } |
| 602 | |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 603 | static int parse_force_date(const char *in, struct strbuf *out) |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 604 | { |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 605 | strbuf_addch(out, '@'); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 606 | |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 607 | if (parse_date(in, out) < 0) { |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 608 | int errors = 0; |
| 609 | unsigned long t = approxidate_careful(in, &errors); |
| 610 | if (errors) |
| 611 | return -1; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 612 | strbuf_addf(out, "%lu", t); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 618 | static void set_ident_var(char **buf, char *val) |
| 619 | { |
| 620 | free(*buf); |
| 621 | *buf = val; |
| 622 | } |
| 623 | |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 624 | static void determine_author_info(struct strbuf *author_ident) |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 625 | { |
| 626 | char *name, *email, *date; |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 627 | struct ident_split author; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 628 | |
Jeff King | eaa541e | 2015-01-12 20:58:33 -0500 | [diff] [blame] | 629 | name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME")); |
| 630 | email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL")); |
| 631 | date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE")); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 632 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 633 | if (author_message) { |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 634 | struct ident_split ident; |
Junio C Hamano | 2c733fb | 2012-02-02 13:41:43 -0800 | [diff] [blame] | 635 | size_t len; |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 636 | const char *a; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 637 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 638 | a = find_commit_header(author_message_buffer, "author", &len); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 639 | if (!a) |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 640 | die(_("commit '%s' lacks author header"), author_message); |
| 641 | if (split_ident_line(&ident, a, len) < 0) |
| 642 | die(_("commit '%s' has malformed author line"), author_message); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 643 | |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 644 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
| 645 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 646 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 647 | if (ident.date_begin) { |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 648 | struct strbuf date_buf = STRBUF_INIT; |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 649 | strbuf_addch(&date_buf, '@'); |
| 650 | strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin); |
| 651 | strbuf_addch(&date_buf, ' '); |
| 652 | strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 653 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 654 | } |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | if (force_author) { |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 658 | struct ident_split ident; |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 659 | |
Jeff King | f0f9662 | 2014-08-27 03:57:28 -0400 | [diff] [blame] | 660 | 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] | 661 | die(_("malformed --author parameter")); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 662 | set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin)); |
| 663 | set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin)); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 666 | if (force_date) { |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 667 | struct strbuf date_buf = STRBUF_INIT; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 668 | if (parse_force_date(force_date, &date_buf)) |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 669 | die(_("invalid date format: %s"), force_date); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 670 | set_ident_var(&date, strbuf_detach(&date_buf, NULL)); |
Jeff King | 14ac286 | 2014-05-01 21:12:42 -0400 | [diff] [blame] | 671 | } |
| 672 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 673 | strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date, |
| 674 | IDENT_STRICT)); |
Jeff King | c83a509 | 2014-12-10 10:43:42 -0500 | [diff] [blame] | 675 | assert_split_ident(&author, author_ident); |
| 676 | export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0); |
| 677 | export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0); |
| 678 | export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@'); |
Jeff King | f4ef517 | 2014-08-27 03:57:56 -0400 | [diff] [blame] | 679 | free(name); |
| 680 | free(email); |
| 681 | free(date); |
Santi Béjar | a45d46b | 2008-05-04 18:04:49 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Jeff King | b7242b8 | 2014-05-01 21:10:01 -0400 | [diff] [blame] | 684 | static int author_date_is_interesting(void) |
| 685 | { |
| 686 | return author_message || force_date; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 689 | static void adjust_comment_line_char(const struct strbuf *sb) |
| 690 | { |
| 691 | char candidates[] = "#;@!$%^&|:"; |
| 692 | char *candidate; |
| 693 | const char *p; |
| 694 | |
| 695 | comment_line_char = candidates[0]; |
| 696 | if (!memchr(sb->buf, comment_line_char, sb->len)) |
| 697 | return; |
| 698 | |
| 699 | p = sb->buf; |
| 700 | candidate = strchr(candidates, *p); |
| 701 | if (candidate) |
| 702 | *candidate = ' '; |
| 703 | for (p = sb->buf; *p; p++) { |
| 704 | if ((p[0] == '\n' || p[0] == '\r') && p[1]) { |
| 705 | candidate = strchr(candidates, p[1]); |
| 706 | if (candidate) |
| 707 | *candidate = ' '; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | for (p = candidates; *p == ' '; p++) |
| 712 | ; |
| 713 | if (!*p) |
| 714 | die(_("unable to select a comment character that is not used\n" |
| 715 | "in the current commit message")); |
| 716 | comment_line_char = *p; |
| 717 | } |
| 718 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 719 | static void prepare_amend_commit(struct commit *commit, struct strbuf *sb, |
| 720 | struct pretty_print_context *ctx) |
| 721 | { |
| 722 | const char *buffer, *subject, *fmt; |
| 723 | |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 724 | buffer = repo_get_commit_buffer(the_repository, commit, NULL); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 725 | find_commit_subject(buffer, &subject); |
| 726 | /* |
| 727 | * If we amend the 'amend!' commit then we don't want to |
| 728 | * duplicate the subject line. |
| 729 | */ |
| 730 | fmt = starts_with(subject, "amend!") ? "%b" : "%B"; |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 731 | repo_format_commit_message(the_repository, commit, fmt, sb, ctx); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 732 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 733 | } |
| 734 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 735 | 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] | 736 | struct commit *current_head, |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 737 | struct wt_status *s, |
| 738 | struct strbuf *author_ident) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 739 | { |
| 740 | struct stat statbuf; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 741 | struct strbuf committer_ident = STRBUF_INIT; |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 742 | int committable; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 743 | struct strbuf sb = STRBUF_INIT; |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 744 | const char *hook_arg1 = NULL; |
| 745 | const char *hook_arg2 = NULL; |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 746 | int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE); |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 747 | int old_display_comment_prefix; |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 748 | int merge_contains_scissors = 0; |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 749 | int invoked_hook; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 750 | |
Junio C Hamano | 7dfe8ad | 2012-03-11 03:12:10 -0700 | [diff] [blame] | 751 | /* This checks and barfs if author is badly specified */ |
| 752 | determine_author_info(author_ident); |
| 753 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 754 | if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook, |
| 755 | "pre-commit", NULL)) |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 756 | return 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 757 | |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 758 | if (squash_message) { |
| 759 | /* |
| 760 | * Insert the proper subject line before other commit |
| 761 | * message options add their content. |
| 762 | */ |
| 763 | if (use_message && !strcmp(use_message, squash_message)) |
| 764 | strbuf_addstr(&sb, "squash! "); |
| 765 | else { |
| 766 | struct pretty_print_context ctx = {0}; |
| 767 | struct commit *c; |
| 768 | c = lookup_commit_reference_by_name(squash_message); |
| 769 | if (!c) |
Teng Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 770 | die(_("could not lookup commit '%s'"), squash_message); |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 771 | ctx.output_encoding = get_commit_output_encoding(); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 772 | repo_format_commit_message(the_repository, c, |
| 773 | "squash! %s\n\n", &sb, |
| 774 | &ctx); |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Ævar Arnfjörð Bjarmason | 30884c9 | 2017-12-22 20:41:52 +0000 | [diff] [blame] | 778 | if (have_option_m && !fixup_message) { |
Johannes Schindelin | f956853 | 2007-11-11 17:36:39 +0000 | [diff] [blame] | 779 | strbuf_addbuf(&sb, &message); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 780 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 781 | } else if (logfile && !strcmp(logfile, "-")) { |
| 782 | if (isatty(0)) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 783 | fprintf(stderr, _("(reading log message from standard input)\n")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 784 | if (strbuf_read(&sb, 0, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 785 | die_errno(_("could not read log from standard input")); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 786 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 787 | } else if (logfile) { |
| 788 | if (strbuf_read_file(&sb, logfile, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 789 | die_errno(_("could not read log file '%s'"), |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 790 | logfile); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 791 | hook_arg1 = "message"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 792 | } else if (use_message) { |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 793 | char *buffer; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 794 | buffer = strstr(use_message_buffer, "\n\n"); |
Jeff King | 076cbd6 | 2014-04-25 19:11:15 -0400 | [diff] [blame] | 795 | if (buffer) |
Johannes Schindelin | 84e213a | 2016-06-29 16:14:42 +0200 | [diff] [blame] | 796 | strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 797 | hook_arg1 = "commit"; |
| 798 | hook_arg2 = use_message; |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 799 | } else if (fixup_message) { |
| 800 | struct pretty_print_context ctx = {0}; |
| 801 | struct commit *commit; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 802 | char *fmt; |
| 803 | commit = lookup_commit_reference_by_name(fixup_commit); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 804 | if (!commit) |
Teng Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 805 | die(_("could not lookup commit '%s'"), fixup_commit); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 806 | ctx.output_encoding = get_commit_output_encoding(); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 807 | fmt = xstrfmt("%s! %%s\n\n", fixup_prefix); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 808 | repo_format_commit_message(the_repository, commit, fmt, &sb, |
| 809 | &ctx); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 810 | free(fmt); |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 811 | hook_arg1 = "message"; |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 812 | |
| 813 | /* |
| 814 | * Only `-m` commit message option is checked here, as |
| 815 | * it supports `--fixup` to append the commit message. |
| 816 | * |
| 817 | * The other commit message options `-c`/`-C`/`-F` are |
| 818 | * incompatible with all the forms of `--fixup` and |
| 819 | * have already errored out while parsing the `git commit` |
| 820 | * options. |
| 821 | */ |
| 822 | if (have_option_m && !strcmp(fixup_prefix, "fixup")) |
| 823 | strbuf_addbuf(&sb, &message); |
| 824 | |
| 825 | if (!strcmp(fixup_prefix, "amend")) { |
| 826 | if (have_option_m) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 827 | 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] | 828 | prepare_amend_commit(commit, &sb, &ctx); |
| 829 | } |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 830 | } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) { |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 831 | size_t merge_msg_start; |
| 832 | |
Sven Strickroth | b64c1e0 | 2016-03-21 23:29:40 +0100 | [diff] [blame] | 833 | /* |
| 834 | * prepend SQUASH_MSG here if it exists and a |
| 835 | * "merge --squash" was originally performed |
| 836 | */ |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 837 | if (!stat(git_path_squash_msg(the_repository), &statbuf)) { |
| 838 | 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] | 839 | die_errno(_("could not read SQUASH_MSG")); |
| 840 | hook_arg1 = "squash"; |
| 841 | } else |
| 842 | hook_arg1 = "merge"; |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 843 | |
| 844 | merge_msg_start = sb.len; |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 845 | 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] | 846 | die_errno(_("could not read MERGE_MSG")); |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 847 | |
| 848 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS && |
| 849 | wt_status_locate_end(sb.buf + merge_msg_start, |
| 850 | sb.len - merge_msg_start) < |
| 851 | sb.len - merge_msg_start) |
| 852 | merge_contains_scissors = 1; |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 853 | } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) { |
| 854 | if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 855 | die_errno(_("could not read SQUASH_MSG")); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 856 | hook_arg1 = "squash"; |
Jonathan Nieder | 2140b14 | 2011-02-25 03:07:57 -0600 | [diff] [blame] | 857 | } else if (template_file) { |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 858 | if (strbuf_read_file(&sb, template_file, 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 859 | die_errno(_("could not read '%s'"), template_file); |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 860 | hook_arg1 = "template"; |
Boris Faure | 8b1ae67 | 2011-05-08 12:31:02 +0200 | [diff] [blame] | 861 | clean_message_contents = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 862 | } |
| 863 | |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 864 | /* |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 865 | * The remaining cases don't modify the template message, but |
| 866 | * just set the argument(s) to the prepare-commit-msg hook. |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 867 | */ |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 868 | else if (whence == FROM_MERGE) |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 869 | hook_arg1 = "merge"; |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 870 | else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 871 | hook_arg1 = "commit"; |
| 872 | hook_arg2 = "CHERRY_PICK_HEAD"; |
| 873 | } |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 874 | |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 875 | if (squash_message) { |
| 876 | /* |
| 877 | * If squash_commit was used for the commit subject, |
| 878 | * then we're possibly hijacking other commit log options. |
| 879 | * Reset the hook args to tell the real story. |
| 880 | */ |
| 881 | hook_arg1 = "message"; |
| 882 | hook_arg2 = ""; |
| 883 | } |
| 884 | |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 885 | s->fp = fopen_for_writing(git_path_commit_editmsg()); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 886 | if (!s->fp) |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 887 | die_errno(_("could not open '%s'"), git_path_commit_editmsg()); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 888 | |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 889 | /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */ |
| 890 | old_display_comment_prefix = s->display_comment_prefix; |
| 891 | s->display_comment_prefix = 1; |
| 892 | |
Matthieu Moy | ea9882b | 2013-09-12 12:50:06 +0200 | [diff] [blame] | 893 | /* |
| 894 | * Most hints are counter-productive when the commit has |
| 895 | * already started. |
| 896 | */ |
| 897 | s->hints = 0; |
| 898 | |
Boris Faure | 8b1ae67 | 2011-05-08 12:31:02 +0200 | [diff] [blame] | 899 | if (clean_message_contents) |
Calvin Wan | 787cb8a | 2023-06-06 19:48:43 +0000 | [diff] [blame] | 900 | strbuf_stripspace(&sb, '\0'); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 901 | |
Junio C Hamano | 073bd75 | 2014-10-28 12:44:09 -0700 | [diff] [blame] | 902 | if (signoff) |
Jonathan Tan | 710714a | 2016-11-02 10:29:17 -0700 | [diff] [blame] | 903 | append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0); |
Johannes Schindelin | 1320857 | 2007-11-11 17:35:58 +0000 | [diff] [blame] | 904 | |
Jonathan Nieder | 37f3012 | 2011-02-25 23:10:49 -0600 | [diff] [blame] | 905 | 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] | 906 | die_errno(_("could not write commit template")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 907 | |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 908 | if (auto_comment_line_char) |
| 909 | adjust_comment_line_char(&sb); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 910 | strbuf_release(&sb); |
| 911 | |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 912 | /* This checks if committer ident is explicitly given */ |
Jeff King | f20f387 | 2012-07-23 14:50:35 -0400 | [diff] [blame] | 913 | strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT)); |
James P. Howard, II | bed575e | 2009-12-07 17:45:27 -0500 | [diff] [blame] | 914 | if (use_editor && include_status) { |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 915 | int ident_shown = 0; |
| 916 | int saved_color_setting; |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 917 | struct ident_split ci, ai; |
Hu Jialun | 6f70f00 | 2021-07-10 02:07:32 +0800 | [diff] [blame] | 918 | const char *hint_cleanup_all = allow_empty_message ? |
| 919 | _("Please enter the commit message for your changes." |
| 920 | " Lines starting\nwith '%c' will be ignored.\n") : |
| 921 | _("Please enter the commit message for your changes." |
| 922 | " Lines starting\nwith '%c' will be ignored, and an empty" |
| 923 | " message aborts the commit.\n"); |
| 924 | const char *hint_cleanup_space = allow_empty_message ? |
| 925 | _("Please enter the commit message for your changes." |
| 926 | " Lines starting\n" |
| 927 | "with '%c' will be kept; you may remove them" |
| 928 | " yourself if you want to.\n") : |
| 929 | _("Please enter the commit message for your changes." |
| 930 | " Lines starting\n" |
| 931 | "with '%c' will be kept; you may remove them" |
| 932 | " yourself if you want to.\n" |
| 933 | "An empty message aborts the commit.\n"); |
Nguyễn Thái Ngọc Duy | 75df1f4 | 2014-02-17 19:15:32 +0700 | [diff] [blame] | 934 | if (whence != FROM_COMMIT) { |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 935 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS && |
| 936 | !merge_contains_scissors) |
Nguyễn Thái Ngọc Duy | 75df1f4 | 2014-02-17 19:15:32 +0700 | [diff] [blame] | 937 | wt_status_add_cut_line(s->fp); |
Han-Wen Nienhuys | b6d2558 | 2020-08-21 16:59:36 +0000 | [diff] [blame] | 938 | status_printf_ln( |
| 939 | s, GIT_COLOR_NORMAL, |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 940 | whence == FROM_MERGE ? |
Han-Wen Nienhuys | b6d2558 | 2020-08-21 16:59:36 +0000 | [diff] [blame] | 941 | _("\n" |
| 942 | "It looks like you may be committing a merge.\n" |
| 943 | "If this is not correct, please run\n" |
| 944 | " git update-ref -d MERGE_HEAD\n" |
| 945 | "and try again.\n") : |
| 946 | _("\n" |
| 947 | "It looks like you may be committing a cherry-pick.\n" |
| 948 | "If this is not correct, please run\n" |
| 949 | " git update-ref -d CHERRY_PICK_HEAD\n" |
| 950 | "and try again.\n")); |
Nguyễn Thái Ngọc Duy | 75df1f4 | 2014-02-17 19:15:32 +0700 | [diff] [blame] | 951 | } |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 952 | |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 953 | fprintf(s->fp, "\n"); |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 954 | if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL) |
Hu Jialun | 54ba2f1 | 2021-07-10 02:07:31 +0800 | [diff] [blame] | 955 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char); |
Denton Liu | 1055997 | 2019-04-17 11:23:28 +0100 | [diff] [blame] | 956 | else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
| 957 | if (whence == FROM_COMMIT && !merge_contains_scissors) |
| 958 | wt_status_add_cut_line(s->fp); |
| 959 | } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */ |
Hu Jialun | 54ba2f1 | 2021-07-10 02:07:31 +0800 | [diff] [blame] | 960 | status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 961 | |
Jeff King | fac9083 | 2014-12-10 10:42:10 -0500 | [diff] [blame] | 962 | /* |
| 963 | * These should never fail because they come from our own |
| 964 | * fmt_ident. They may fail the sane_ident test, but we know |
| 965 | * that the name and mail pointers will at least be valid, |
| 966 | * which is enough for our tests and printing here. |
| 967 | */ |
| 968 | assert_split_ident(&ai, author_ident); |
| 969 | assert_split_ident(&ci, &committer_ident); |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 970 | |
| 971 | if (ident_cmp(&ai, &ci)) |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 972 | status_printf_ln(s, GIT_COLOR_NORMAL, |
Ævar Arnfjörð Bjarmason | fe8165c | 2011-02-22 23:41:46 +0000 | [diff] [blame] | 973 | _("%s" |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 974 | "Author: %.*s <%.*s>"), |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 975 | ident_shown++ ? "" : "\n", |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 976 | (int)(ai.name_end - ai.name_begin), ai.name_begin, |
| 977 | (int)(ai.mail_end - ai.mail_begin), ai.mail_begin); |
Santi Béjar | e83dbe8 | 2008-05-04 18:04:50 +0200 | [diff] [blame] | 978 | |
Jeff King | b7242b8 | 2014-05-01 21:10:01 -0400 | [diff] [blame] | 979 | if (author_date_is_interesting()) |
| 980 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 981 | _("%s" |
| 982 | "Date: %s"), |
| 983 | ident_shown++ ? "" : "\n", |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 984 | show_ident_date(&ai, DATE_MODE(NORMAL))); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 985 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 986 | if (!committer_ident_sufficiently_given()) |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 987 | status_printf_ln(s, GIT_COLOR_NORMAL, |
Ævar Arnfjörð Bjarmason | fe8165c | 2011-02-22 23:41:46 +0000 | [diff] [blame] | 988 | _("%s" |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 989 | "Committer: %.*s <%.*s>"), |
Jonathan Nieder | b926c0d | 2011-02-25 23:11:37 -0600 | [diff] [blame] | 990 | ident_shown++ ? "" : "\n", |
Jeff King | 4701026 | 2014-05-01 21:06:57 -0400 | [diff] [blame] | 991 | (int)(ci.name_end - ci.name_begin), ci.name_begin, |
| 992 | (int)(ci.mail_end - ci.mail_begin), ci.mail_begin); |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 993 | |
Kaartic Sivaraam | b3cf1b7 | 2017-06-30 17:42:21 +0530 | [diff] [blame] | 994 | 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] | 995 | |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 996 | saved_color_setting = s->use_color; |
| 997 | s->use_color = 0; |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 998 | committable = run_status(s->fp, index_file, prefix, 1, s); |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 999 | s->use_color = saved_color_setting; |
Elijah Newren | 3e73cc6 | 2018-09-27 10:36:57 -0700 | [diff] [blame] | 1000 | string_list_clear(&s->change, 1); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1001 | } else { |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1002 | struct object_id oid; |
Junio C Hamano | fbcf118 | 2007-12-19 19:23:03 -0800 | [diff] [blame] | 1003 | const char *parent = "HEAD"; |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1004 | |
Johannes Schindelin | 2ee045e | 2023-06-29 13:23:10 +0000 | [diff] [blame] | 1005 | if (!the_index.initialized && repo_read_index(the_repository) < 0) |
| 1006 | die(_("Cannot read index")); |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1007 | |
Junio C Hamano | fbcf118 | 2007-12-19 19:23:03 -0800 | [diff] [blame] | 1008 | if (amend) |
| 1009 | parent = "HEAD^1"; |
| 1010 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1011 | 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] | 1012 | int i, ita_nr = 0; |
| 1013 | |
Derrick Stolee | cb8388d | 2021-04-01 01:49:45 +0000 | [diff] [blame] | 1014 | /* TODO: audit for interaction with sparse-index. */ |
| 1015 | ensure_full_index(&the_index); |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 1016 | for (i = 0; i < the_index.cache_nr; i++) |
| 1017 | if (ce_intent_to_add(the_index.cache[i])) |
Nguyễn Thái Ngọc Duy | 2c49f7f | 2016-10-24 17:42:22 +0700 | [diff] [blame] | 1018 | ita_nr++; |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 1019 | committable = the_index.cache_nr - ita_nr > 0; |
Nguyễn Thái Ngọc Duy | 2c49f7f | 2016-10-24 17:42:22 +0700 | [diff] [blame] | 1020 | } else { |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1021 | /* |
| 1022 | * Unless the user did explicitly request a submodule |
| 1023 | * ignore mode by passing a command line option we do |
| 1024 | * not ignore any changed submodule SHA-1s when |
| 1025 | * comparing index and parent, no matter what is |
| 1026 | * configured. Otherwise we won't commit any |
| 1027 | * submodules which were manually staged, which would |
| 1028 | * be really confusing. |
| 1029 | */ |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 1030 | struct diff_flags flags = DIFF_FLAGS_INIT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1031 | flags.override_submodule_config = 1; |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1032 | if (ignore_submodule_arg && |
| 1033 | !strcmp(ignore_submodule_arg, "all")) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1034 | flags.ignore_submodules = 1; |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 1035 | committable = index_differs_from(the_repository, |
| 1036 | parent, &flags, 1); |
Jens Lehmann | c215d3d | 2014-04-05 18:59:36 +0200 | [diff] [blame] | 1037 | } |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1038 | } |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 1039 | strbuf_release(&committer_ident); |
Alex Riesen | 7168624 | 2007-11-28 22:13:08 +0100 | [diff] [blame] | 1040 | |
Jonathan Nieder | 37f3012 | 2011-02-25 23:10:49 -0600 | [diff] [blame] | 1041 | fclose(s->fp); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1042 | |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 1043 | if (trailer_args.nr) { |
| 1044 | struct child_process run_trailer = CHILD_PROCESS_INIT; |
| 1045 | |
| 1046 | strvec_pushl(&run_trailer.args, "interpret-trailers", |
Jeff King | be3d654 | 2023-06-17 00:26:24 -0400 | [diff] [blame] | 1047 | "--in-place", "--no-divider", |
| 1048 | git_path_commit_editmsg(), NULL); |
ZheNing Hu | 2daae3d | 2021-03-23 13:55:57 +0000 | [diff] [blame] | 1049 | strvec_pushv(&run_trailer.args, trailer_args.v); |
| 1050 | run_trailer.git_cmd = 1; |
| 1051 | if (run_command(&run_trailer)) |
| 1052 | die(_("unable to pass trailers to --trailers")); |
| 1053 | strvec_clear(&trailer_args); |
| 1054 | } |
| 1055 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1056 | /* |
| 1057 | * Reject an attempt to record a non-merge empty commit without |
| 1058 | * explicit --allow-empty. In the cherry-pick case, it may be |
| 1059 | * empty due to conflict resolution, which the user should okay. |
| 1060 | */ |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1061 | if (!committable && whence != FROM_MERGE && !allow_empty && |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1062 | !(amend && is_a_merge(current_head))) { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 1063 | s->hints = advice_enabled(ADVICE_STATUS_HINTS); |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 1064 | s->display_comment_prefix = old_display_comment_prefix; |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1065 | run_status(stdout, index_file, prefix, 0, s); |
Jeff King | f197ed2 | 2010-06-06 20:41:46 -0400 | [diff] [blame] | 1066 | if (amend) |
Ævar Arnfjörð Bjarmason | fc88e31 | 2011-02-22 23:41:49 +0000 | [diff] [blame] | 1067 | fputs(_(empty_amend_advice), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1068 | else if (is_from_cherry_pick(whence) || |
| 1069 | whence == FROM_REBASE_PICK) { |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 1070 | fputs(_(empty_cherry_pick_advice), stderr); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1071 | if (whence == FROM_CHERRY_PICK_SINGLE) |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1072 | fputs(_(empty_cherry_pick_advice_single), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1073 | else if (whence == FROM_CHERRY_PICK_MULTI) |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1074 | fputs(_(empty_cherry_pick_advice_multi), stderr); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1075 | else |
| 1076 | fputs(_(empty_rebase_pick_advice), stderr); |
Jeff King | c17592a | 2013-07-26 19:39:28 -0400 | [diff] [blame] | 1077 | } |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1078 | return 0; |
| 1079 | } |
| 1080 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1081 | if (!no_verify && invoked_hook) { |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1082 | /* |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1083 | * Re-read the index as the pre-commit-commit hook was invoked |
| 1084 | * and could have updated it. We must do this before we invoke |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1085 | * the editor and after we invoke run_status above. |
| 1086 | */ |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 1087 | discard_index(&the_index); |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1088 | } |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 1089 | read_index_from(&the_index, index_file, get_git_dir()); |
Kevin Willford | 680ee55 | 2017-08-14 15:54:25 -0600 | [diff] [blame] | 1090 | |
Ævar Arnfjörð Bjarmason | fcb864b | 2023-02-10 11:28:38 +0100 | [diff] [blame] | 1091 | if (cache_tree_update(&the_index, 0)) { |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1092 | error(_("Error building trees")); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1096 | if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg", |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 1097 | git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL)) |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 1098 | return 0; |
| 1099 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1100 | if (use_editor) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1101 | struct strvec env = STRVEC_INIT; |
Elia Pinto | 8d7aa4b | 2017-01-13 17:58:00 +0000 | [diff] [blame] | 1102 | |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1103 | strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1104 | if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) { |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 1105 | fprintf(stderr, |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1106 | _("Please supply the message using either -m or -F option.\n")); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 1107 | exit(1); |
| 1108 | } |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1109 | strvec_clear(&env); |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | if (!no_verify && |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1113 | run_commit_hook(use_editor, index_file, NULL, "commit-msg", |
| 1114 | git_path_commit_editmsg(), NULL)) { |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | return 1; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1119 | } |
| 1120 | |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1121 | static const char *find_author_by_nickname(const char *name) |
| 1122 | { |
| 1123 | struct rev_info revs; |
| 1124 | struct commit *commit; |
| 1125 | struct strbuf buf = STRBUF_INIT; |
| 1126 | const char *av[20]; |
| 1127 | int ac = 0; |
| 1128 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 1129 | repo_init_revisions(the_repository, &revs, NULL); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1130 | strbuf_addf(&buf, "--author=%s", name); |
| 1131 | av[++ac] = "--all"; |
| 1132 | av[++ac] = "-i"; |
| 1133 | av[++ac] = buf.buf; |
| 1134 | av[++ac] = NULL; |
| 1135 | setup_revisions(ac, av, &revs, NULL); |
Ævar Arnfjörð Bjarmason | a52f07a | 2022-04-13 22:01:46 +0200 | [diff] [blame] | 1136 | revs.mailmap = xmalloc(sizeof(struct string_list)); |
| 1137 | string_list_init_nodup(revs.mailmap); |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 1138 | read_mailmap(revs.mailmap); |
Antoine Pelisse | ea16794 | 2013-08-23 15:48:31 +0200 | [diff] [blame] | 1139 | |
Stefan Beller | 81c3ce3 | 2014-08-10 23:33:26 +0200 | [diff] [blame] | 1140 | if (prepare_revision_walk(&revs)) |
| 1141 | die(_("revision walk setup failed")); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1142 | commit = get_revision(&revs); |
| 1143 | if (commit) { |
Thomas Rast | dd2e794 | 2009-10-19 17:48:08 +0200 | [diff] [blame] | 1144 | struct pretty_print_context ctx = {0}; |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1145 | ctx.date_mode.type = DATE_NORMAL; |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1146 | strbuf_release(&buf); |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 1147 | repo_format_commit_message(the_repository, commit, |
| 1148 | "%aN <%aE>", &buf, &ctx); |
Ævar Arnfjörð Bjarmason | 2108fe4 | 2022-04-13 22:01:36 +0200 | [diff] [blame] | 1149 | release_revisions(&revs); |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1150 | return strbuf_detach(&buf, NULL); |
| 1151 | } |
Michael J Gruber | 1044b1f | 2015-01-26 16:48:33 +0100 | [diff] [blame] | 1152 | 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] | 1153 | } |
| 1154 | |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1155 | static void handle_ignored_arg(struct wt_status *s) |
| 1156 | { |
| 1157 | if (!ignored_arg) |
| 1158 | ; /* default already initialized */ |
| 1159 | else if (!strcmp(ignored_arg, "traditional")) |
| 1160 | s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED; |
| 1161 | else if (!strcmp(ignored_arg, "no")) |
| 1162 | s->show_ignored_mode = SHOW_NO_IGNORED; |
| 1163 | else if (!strcmp(ignored_arg, "matching")) |
| 1164 | s->show_ignored_mode = SHOW_MATCHING_IGNORED; |
| 1165 | else |
| 1166 | die(_("Invalid ignored mode '%s'"), ignored_arg); |
| 1167 | } |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1168 | |
| 1169 | static void handle_untracked_files_arg(struct wt_status *s) |
| 1170 | { |
| 1171 | if (!untracked_files_arg) |
| 1172 | ; /* default already initialized */ |
| 1173 | else if (!strcmp(untracked_files_arg, "no")) |
| 1174 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; |
| 1175 | else if (!strcmp(untracked_files_arg, "normal")) |
| 1176 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
| 1177 | else if (!strcmp(untracked_files_arg, "all")) |
| 1178 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 1179 | /* |
| 1180 | * Please update $__git_untracked_file_modes in |
| 1181 | * git-completion.bash when you add new options |
| 1182 | */ |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1183 | else |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1184 | die(_("Invalid untracked files mode '%s'"), untracked_files_arg); |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1187 | static const char *read_commit_message(const char *name) |
| 1188 | { |
Jeff King | dd0d388 | 2013-01-26 04:44:06 -0500 | [diff] [blame] | 1189 | const char *out_enc; |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1190 | struct commit *commit; |
| 1191 | |
| 1192 | commit = lookup_commit_reference_by_name(name); |
| 1193 | if (!commit) |
Teng Long | e4cf013 | 2023-05-29 21:27:56 +0800 | [diff] [blame] | 1194 | die(_("could not lookup commit '%s'"), name); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1195 | out_enc = get_commit_output_encoding(); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1196 | return repo_logmsg_reencode(the_repository, commit, NULL, out_enc); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1197 | } |
| 1198 | |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1199 | /* |
| 1200 | * Enumerate what needs to be propagated when --porcelain |
| 1201 | * is not in effect here. |
| 1202 | */ |
| 1203 | static struct status_deferred_config { |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1204 | enum wt_status_format status_format; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1205 | int show_branch; |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1206 | enum ahead_behind_flags ahead_behind; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1207 | } status_deferred_config = { |
| 1208 | STATUS_FORMAT_UNSPECIFIED, |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1209 | -1, /* unspecified */ |
| 1210 | AHEAD_BEHIND_UNSPECIFIED, |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1211 | }; |
| 1212 | |
| 1213 | static void finalize_deferred_config(struct wt_status *s) |
| 1214 | { |
| 1215 | int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN && |
Jeff Hostetler | 1ecdecc | 2016-08-11 10:45:57 -0400 | [diff] [blame] | 1216 | status_format != STATUS_FORMAT_PORCELAIN_V2 && |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1217 | !s->null_termination); |
| 1218 | |
| 1219 | if (s->null_termination) { |
| 1220 | if (status_format == STATUS_FORMAT_NONE || |
| 1221 | status_format == STATUS_FORMAT_UNSPECIFIED) |
| 1222 | status_format = STATUS_FORMAT_PORCELAIN; |
| 1223 | else if (status_format == STATUS_FORMAT_LONG) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 1224 | die(_("options '%s' and '%s' cannot be used together"), "--long", "-z"); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED) |
| 1228 | status_format = status_deferred_config.status_format; |
| 1229 | if (status_format == STATUS_FORMAT_UNSPECIFIED) |
| 1230 | status_format = STATUS_FORMAT_NONE; |
| 1231 | |
| 1232 | if (use_deferred_config && s->show_branch < 0) |
| 1233 | s->show_branch = status_deferred_config.show_branch; |
| 1234 | if (s->show_branch < 0) |
| 1235 | s->show_branch = 0; |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1236 | |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * If the user did not give a "--[no]-ahead-behind" command |
Jeff Hostetler | fb4db1a | 2019-06-18 13:21:28 -0700 | [diff] [blame] | 1239 | * line argument *AND* we will print in a human-readable format |
| 1240 | * (short, long etc.) then we inherit from the status.aheadbehind |
| 1241 | * config setting. In all other cases (and porcelain V[12] formats |
| 1242 | * in particular), we inherit _FULL for backwards compatibility. |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1243 | */ |
Jeff Hostetler | fb4db1a | 2019-06-18 13:21:28 -0700 | [diff] [blame] | 1244 | if (use_deferred_config && |
| 1245 | s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1246 | s->ahead_behind_flags = status_deferred_config.ahead_behind; |
| 1247 | |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1248 | if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED) |
| 1249 | s->ahead_behind_flags = AHEAD_BEHIND_FULL; |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1252 | static void check_fixup_reword_options(int argc, const char *argv[]) { |
| 1253 | if (whence != FROM_COMMIT) { |
| 1254 | if (whence == FROM_MERGE) |
| 1255 | die(_("You are in the middle of a merge -- cannot reword.")); |
| 1256 | else if (is_from_cherry_pick(whence)) |
| 1257 | die(_("You are in the middle of a cherry-pick -- cannot reword.")); |
| 1258 | } |
| 1259 | if (argc) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1260 | 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] | 1261 | if (patch_interactive || interactive || all || also || only) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1262 | die(_("reword option of '%s' and '%s' cannot be used together"), |
| 1263 | "--fixup", "--patch/--interactive/--all/--include/--only"); |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1264 | } |
| 1265 | |
Shawn Bohrer | 2f02b25 | 2007-12-02 23:02:09 -0600 | [diff] [blame] | 1266 | static int parse_and_validate_options(int argc, const char *argv[], |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1267 | const struct option *options, |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 1268 | const char * const usage[], |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1269 | const char *prefix, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1270 | struct commit *current_head, |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1271 | struct wt_status *s) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1272 | { |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1273 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1274 | finalize_deferred_config(s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1275 | |
Junio C Hamano | 146ea06 | 2008-08-26 23:13:13 -0700 | [diff] [blame] | 1276 | if (force_author && !strchr(force_author, '>')) |
| 1277 | force_author = find_author_by_nickname(force_author); |
| 1278 | |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 1279 | if (force_author && renew_authorship) |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1280 | die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author"); |
Erick Mattos | c51f6ce | 2009-11-04 01:20:11 -0200 | [diff] [blame] | 1281 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1282 | if (logfile || have_option_m || use_message) |
Junio C Hamano | 4803466 | 2007-12-22 19:25:37 -0800 | [diff] [blame] | 1283 | use_editor = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1284 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1285 | /* Sanity check options */ |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1286 | if (amend && !current_head) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1287 | die(_("You have nothing to amend.")); |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 1288 | if (amend && whence != FROM_COMMIT) { |
| 1289 | if (whence == FROM_MERGE) |
| 1290 | die(_("You are in the middle of a merge -- cannot amend.")); |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1291 | else if (is_from_cherry_pick(whence)) |
Ævar Arnfjörð Bjarmason | b0cea47 | 2012-04-30 15:33:13 +0000 | [diff] [blame] | 1292 | die(_("You are in the middle of a cherry-pick -- cannot amend.")); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1293 | else if (whence == FROM_REBASE_PICK) |
| 1294 | 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] | 1295 | } |
Pat Notz | 89ac122 | 2010-11-02 13:59:11 -0600 | [diff] [blame] | 1296 | if (fixup_message && squash_message) |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1297 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup"); |
| 1298 | die_for_incompatible_opt4(!!use_message, "-C", |
| 1299 | !!edit_message, "-c", |
| 1300 | !!logfile, "-F", |
| 1301 | !!fixup_message, "--fixup"); |
| 1302 | die_for_incompatible_opt4(have_option_m, "-m", |
| 1303 | !!edit_message, "-c", |
| 1304 | !!use_message, "-C", |
| 1305 | !!logfile, "-F"); |
| 1306 | if (use_message || edit_message || logfile ||fixup_message || have_option_m) |
Junio C Hamano | 010c7db | 2012-03-30 11:30:59 -0700 | [diff] [blame] | 1307 | template_file = NULL; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1308 | if (edit_message) |
| 1309 | use_message = edit_message; |
Pat Notz | d71b8ba | 2010-11-02 13:59:09 -0600 | [diff] [blame] | 1310 | if (amend && !use_message && !fixup_message) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1311 | use_message = "HEAD"; |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1312 | if (!use_message && !is_from_cherry_pick(whence) && |
| 1313 | !is_from_rebase(whence) && renew_authorship) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1314 | 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] | 1315 | if (use_message) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1316 | use_message_buffer = read_commit_message(use_message); |
| 1317 | if (!renew_authorship) { |
| 1318 | author_message = use_message; |
| 1319 | author_message_buffer = use_message_buffer; |
| 1320 | } |
| 1321 | } |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1322 | if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) && |
| 1323 | !renew_authorship) { |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1324 | author_message = "CHERRY_PICK_HEAD"; |
| 1325 | author_message_buffer = read_commit_message(author_message); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1326 | } |
| 1327 | |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 1328 | if (patch_interactive) |
| 1329 | interactive = 1; |
| 1330 | |
Jean-Noël Avila | a699367 | 2022-01-31 22:07:46 +0000 | [diff] [blame] | 1331 | die_for_incompatible_opt4(also, "-i/--include", |
| 1332 | only, "-o/--only", |
| 1333 | all, "-a/--all", |
| 1334 | interactive, "--interactive/-p/--patch"); |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1335 | if (fixup_message) { |
| 1336 | /* |
| 1337 | * We limit --fixup's suboptions to only alpha characters. |
| 1338 | * If the first character after a run of alpha is colon, |
| 1339 | * then the part before the colon may be a known suboption |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1340 | * name like `amend` or `reword`, or a misspelt suboption |
| 1341 | * name. In either case, we treat it as |
| 1342 | * --fixup=<suboption>:<arg>. |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1343 | * |
| 1344 | * Otherwise, we are dealing with --fixup=<commit>. |
| 1345 | */ |
| 1346 | char *p = fixup_message; |
| 1347 | while (isalpha(*p)) |
| 1348 | p++; |
| 1349 | if (p > fixup_message && *p == ':') { |
| 1350 | *p = '\0'; |
| 1351 | fixup_commit = p + 1; |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1352 | if (!strcmp("amend", fixup_message) || |
| 1353 | !strcmp("reword", fixup_message)) { |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1354 | fixup_prefix = "amend"; |
| 1355 | allow_empty = 1; |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1356 | if (*fixup_message == 'r') { |
| 1357 | check_fixup_reword_options(argc, argv); |
| 1358 | only = 1; |
| 1359 | } |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1360 | } else { |
| 1361 | die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit); |
| 1362 | } |
| 1363 | } else { |
| 1364 | fixup_commit = fixup_message; |
| 1365 | fixup_prefix = "fixup"; |
| 1366 | use_editor = 0; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Joel Klinghed | 8ef6aad | 2021-08-14 21:40:30 +0000 | [diff] [blame] | 1370 | if (0 <= edit_flag) |
| 1371 | use_editor = edit_flag; |
| 1372 | |
Denton Liu | f29cd86 | 2019-04-17 11:23:25 +0100 | [diff] [blame] | 1373 | cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1374 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1375 | handle_untracked_files_arg(s); |
Marius Storm-Olsen | 4bfee30 | 2008-06-05 10:31:19 +0200 | [diff] [blame] | 1376 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1377 | if (all && argc > 0) |
Nguyễn Thái Ngọc Duy | 5a1dbd4 | 2019-03-20 17:29:06 +0700 | [diff] [blame] | 1378 | die(_("paths '%s ...' with -a does not make sense"), |
| 1379 | argv[0]); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1380 | |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1381 | if (status_format != STATUS_FORMAT_NONE) |
Jeff King | 7c9f703 | 2009-09-05 04:59:56 -0400 | [diff] [blame] | 1382 | dry_run = 1; |
| 1383 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1384 | return argc; |
| 1385 | } |
| 1386 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1387 | static int dry_run_commit(const char **argv, const char *prefix, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1388 | const struct commit *current_head, struct wt_status *s) |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1389 | { |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1390 | int committable; |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1391 | const char *index_file; |
| 1392 | |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1393 | index_file = prepare_index(argv, prefix, current_head, 1); |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1394 | committable = run_status(stdout, index_file, prefix, 0, s); |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1395 | rollback_index_files(); |
| 1396 | |
Stephen P. Smith | 6fa9019 | 2018-09-05 17:53:27 -0700 | [diff] [blame] | 1397 | return committable ? 0 : 1; |
Junio C Hamano | 3a5d13a | 2009-08-07 23:03:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 1400 | define_list_config_array_extra(color_status_slots, {"added"}); |
| 1401 | |
Jonathan Nieder | 8852117 | 2014-10-07 15:16:57 -0400 | [diff] [blame] | 1402 | static int parse_status_slot(const char *slot) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1403 | { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 1404 | if (!strcasecmp(slot, "added")) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1405 | return WT_STATUS_UPDATED; |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 1406 | |
| 1407 | return LOOKUP_CONFIG(color_status_slots, slot); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1410 | static int git_status_config(const char *k, const char *v, |
| 1411 | const struct config_context *ctx, void *cb) |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1412 | { |
| 1413 | struct wt_status *s = cb; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1414 | const char *slot_name; |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1415 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1416 | if (starts_with(k, "column.")) |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1417 | return git_column_config(k, v, "status", &s->colopts); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1418 | if (!strcmp(k, "status.submodulesummary")) { |
| 1419 | int is_bool; |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1420 | s->submodule_summary = git_config_bool_or_int(k, v, ctx->kvi, |
| 1421 | &is_bool); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1422 | if (is_bool && s->submodule_summary) |
| 1423 | s->submodule_summary = -1; |
| 1424 | return 0; |
| 1425 | } |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1426 | if (!strcmp(k, "status.short")) { |
| 1427 | if (git_config_bool(k, v)) |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1428 | status_deferred_config.status_format = STATUS_FORMAT_SHORT; |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1429 | else |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1430 | status_deferred_config.status_format = STATUS_FORMAT_NONE; |
Jorge Juan Garcia Garcia | 4fb5166 | 2013-06-11 15:34:04 +0200 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
Jorge Juan Garcia Garcia | ec85d07 | 2013-06-11 15:34:05 +0200 | [diff] [blame] | 1433 | if (!strcmp(k, "status.branch")) { |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1434 | status_deferred_config.show_branch = git_config_bool(k, v); |
Jorge Juan Garcia Garcia | ec85d07 | 2013-06-11 15:34:05 +0200 | [diff] [blame] | 1435 | return 0; |
| 1436 | } |
Jeff Hostetler | 06b324c | 2019-06-18 13:21:25 -0700 | [diff] [blame] | 1437 | if (!strcmp(k, "status.aheadbehind")) { |
| 1438 | status_deferred_config.ahead_behind = git_config_bool(k, v); |
| 1439 | return 0; |
| 1440 | } |
Liam Beguin | c1b5d01 | 2017-06-17 18:30:51 -0400 | [diff] [blame] | 1441 | if (!strcmp(k, "status.showstash")) { |
| 1442 | s->show_stash = git_config_bool(k, v); |
| 1443 | return 0; |
| 1444 | } |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1445 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 1446 | s->use_color = git_config_colorbool(k, v); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1447 | return 0; |
| 1448 | } |
Matthieu Moy | 2556b99 | 2013-09-06 19:43:07 +0200 | [diff] [blame] | 1449 | if (!strcmp(k, "status.displaycommentprefix")) { |
| 1450 | s->display_comment_prefix = git_config_bool(k, v); |
| 1451 | return 0; |
| 1452 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1453 | if (skip_prefix(k, "status.color.", &slot_name) || |
| 1454 | skip_prefix(k, "color.status.", &slot_name)) { |
Junio C Hamano | b946576 | 2014-10-20 12:23:48 -0700 | [diff] [blame] | 1455 | int slot = parse_status_slot(slot_name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 1456 | if (slot < 0) |
| 1457 | return 0; |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1458 | if (!v) |
| 1459 | return config_error_nonbool(k); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 1460 | return color_parse(v, s->color_palette[slot]); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1461 | } |
| 1462 | if (!strcmp(k, "status.relativepaths")) { |
| 1463 | s->relative_paths = git_config_bool(k, v); |
| 1464 | return 0; |
| 1465 | } |
| 1466 | if (!strcmp(k, "status.showuntrackedfiles")) { |
| 1467 | if (!v) |
| 1468 | return config_error_nonbool(k); |
| 1469 | else if (!strcmp(v, "no")) |
| 1470 | s->show_untracked_files = SHOW_NO_UNTRACKED_FILES; |
| 1471 | else if (!strcmp(v, "normal")) |
| 1472 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
| 1473 | else if (!strcmp(v, "all")) |
| 1474 | s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES; |
| 1475 | else |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1476 | return error(_("Invalid untracked files mode '%s'"), v); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1479 | if (!strcmp(k, "diff.renamelimit")) { |
| 1480 | if (s->rename_limit == -1) |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1481 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
| 1484 | if (!strcmp(k, "status.renamelimit")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1485 | s->rename_limit = git_config_int(k, v, ctx->kvi); |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | if (!strcmp(k, "diff.renames")) { |
| 1489 | if (s->detect_rename == -1) |
| 1490 | s->detect_rename = git_config_rename(k, v); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | if (!strcmp(k, "status.renames")) { |
| 1494 | s->detect_rename = git_config_rename(k, v); |
| 1495 | return 0; |
| 1496 | } |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1497 | return git_diff_ui_config(k, v, ctx, NULL); |
Junio C Hamano | f766b36 | 2009-08-09 23:12:19 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1500 | int cmd_status(int argc, const char **argv, const char *prefix) |
| 1501 | { |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1502 | static int no_renames = -1; |
| 1503 | static const char *rename_score_arg = (const char *)-1; |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1504 | static struct wt_status s; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1505 | unsigned int progress_flag = 0; |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1506 | int fd; |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1507 | struct object_id oid; |
Junio C Hamano | 9e4b7ab | 2009-08-15 02:27:39 -0700 | [diff] [blame] | 1508 | static struct option builtin_status_options[] = { |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1509 | OPT__VERBOSE(&verbose, N_("be verbose")), |
Jeff King | dd2be24 | 2009-09-05 04:54:14 -0400 | [diff] [blame] | 1510 | OPT_SET_INT('s', "short", &status_format, |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1511 | N_("show status concisely"), STATUS_FORMAT_SHORT), |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1512 | OPT_BOOL('b', "branch", &s.show_branch, |
| 1513 | N_("show branch information")), |
Liam Beguin | c1b5d01 | 2017-06-17 18:30:51 -0400 | [diff] [blame] | 1514 | OPT_BOOL(0, "show-stash", &s.show_stash, |
| 1515 | N_("show stash information")), |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1516 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
| 1517 | N_("compute full ahead/behind values")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1518 | OPT_CALLBACK_F(0, "porcelain", &status_format, |
Jeff Hostetler | c4f596b | 2016-08-05 18:00:28 -0400 | [diff] [blame] | 1519 | N_("version"), N_("machine-readable output"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1520 | PARSE_OPT_OPTARG, opt_parse_porcelain), |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1521 | OPT_SET_INT(0, "long", &status_format, |
| 1522 | N_("show status in long format (default)"), |
| 1523 | STATUS_FORMAT_LONG), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1524 | OPT_BOOL('z', "null", &s.null_termination, |
| 1525 | N_("terminate entries with NUL")), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1526 | { 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] | 1527 | N_("mode"), |
| 1528 | N_("show untracked files, optional modes: all, normal, no. (Default: all)"), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1529 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1530 | { OPTION_STRING, 0, "ignored", &ignored_arg, |
| 1531 | N_("mode"), |
| 1532 | N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"), |
| 1533 | PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" }, |
Nguyễn Thái Ngọc Duy | f227631 | 2012-08-20 19:32:37 +0700 | [diff] [blame] | 1534 | { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"), |
| 1535 | N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 1536 | PARSE_OPT_OPTARG, NULL, (intptr_t)"all" }, |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1537 | OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1538 | OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1539 | OPT_CALLBACK_F('M', "find-renames", &rename_score_arg, |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1540 | N_("n"), N_("detect renames, optionally set similarity index"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1541 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score), |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1542 | OPT_END(), |
| 1543 | }; |
| 1544 | |
Nguyễn Thái Ngọc Duy | 5d3dd91 | 2010-10-22 01:45:47 -0500 | [diff] [blame] | 1545 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 1546 | usage_with_options(builtin_status_usage, builtin_status_options); |
| 1547 | |
Derrick Stolee | d76723e | 2021-07-14 13:12:37 +0000 | [diff] [blame] | 1548 | prepare_repo_settings(the_repository); |
| 1549 | the_repository->settings.command_requires_full_index = 0; |
| 1550 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 1551 | status_init_config(&s, git_status_config); |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1552 | argc = parse_options(argc, argv, prefix, |
Junio C Hamano | 9e4b7ab | 2009-08-15 02:27:39 -0700 | [diff] [blame] | 1553 | builtin_status_options, |
| 1554 | builtin_status_usage, 0); |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1555 | finalize_colopts(&s.colopts, -1); |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1556 | finalize_deferred_config(&s); |
Junio C Hamano | 6c92972 | 2011-06-06 11:40:08 -0700 | [diff] [blame] | 1557 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1558 | handle_untracked_files_arg(&s); |
Jameson Miller | eec0f7f | 2017-10-30 13:21:37 -0400 | [diff] [blame] | 1559 | handle_ignored_arg(&s); |
| 1560 | |
| 1561 | if (s.show_ignored_mode == SHOW_MATCHING_IGNORED && |
| 1562 | s.show_untracked_files == SHOW_NO_UNTRACKED_FILES) |
| 1563 | die(_("Unsupported combination of ignored and untracked-files arguments")); |
| 1564 | |
Nguyễn Thái Ngọc Duy | 15b55ae | 2013-07-14 15:35:39 +0700 | [diff] [blame] | 1565 | parse_pathspec(&s.pathspec, 0, |
| 1566 | PATHSPEC_PREFER_FULL, |
| 1567 | prefix, argv); |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1568 | |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1569 | if (status_format != STATUS_FORMAT_PORCELAIN && |
| 1570 | status_format != STATUS_FORMAT_PORCELAIN_V2) |
| 1571 | progress_flag = REFRESH_PROGRESS; |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 1572 | repo_read_index(the_repository); |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1573 | refresh_index(&the_index, |
| 1574 | REFRESH_QUIET|REFRESH_UNMERGED|progress_flag, |
| 1575 | &s.pathspec, NULL, NULL); |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1576 | |
Jeff King | 27344d6 | 2017-09-27 02:54:30 -0400 | [diff] [blame] | 1577 | if (use_optional_locks()) |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 1578 | fd = repo_hold_locked_index(the_repository, &index_lock, 0); |
Jeff King | 27344d6 | 2017-09-27 02:54:30 -0400 | [diff] [blame] | 1579 | else |
| 1580 | fd = -1; |
Markus Heidelberg | 4bb6644 | 2010-04-02 23:44:21 +0200 | [diff] [blame] | 1581 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1582 | 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] | 1583 | if (!s.is_initial) |
brian m. carlson | e0cb7cd | 2019-08-18 20:04:21 +0000 | [diff] [blame] | 1584 | oidcpy(&s.oid_commit, &oid); |
Jeff Hostetler | d9fc746 | 2016-08-11 10:45:59 -0400 | [diff] [blame] | 1585 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 1586 | s.ignore_submodule_arg = ignore_submodule_arg; |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1587 | s.status_format = status_format; |
| 1588 | s.verbose = verbose; |
Ben Peart | e8b2dc2 | 2018-05-11 15:38:58 +0000 | [diff] [blame] | 1589 | if (no_renames != -1) |
| 1590 | s.detect_rename = !no_renames; |
| 1591 | if ((intptr_t)rename_score_arg != -1) { |
| 1592 | if (s.detect_rename < DIFF_DETECT_RENAME) |
| 1593 | s.detect_rename = DIFF_DETECT_RENAME; |
| 1594 | if (rename_score_arg) |
| 1595 | s.rename_score = parse_rename_score(&rename_score_arg); |
| 1596 | } |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1597 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1598 | wt_status_collect(&s); |
| 1599 | |
Nguyễn Thái Ngọc Duy | 226c051 | 2015-03-08 17:12:41 +0700 | [diff] [blame] | 1600 | if (0 <= fd) |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 1601 | 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] | 1602 | |
Jeff King | 8661768 | 2009-12-07 00:26:25 -0500 | [diff] [blame] | 1603 | if (s.relative_paths) |
| 1604 | s.prefix = prefix; |
Markus Heidelberg | 38920dd | 2009-01-08 19:53:05 +0100 | [diff] [blame] | 1605 | |
Jeff Hostetler | be7e795 | 2016-08-05 18:00:27 -0400 | [diff] [blame] | 1606 | wt_status_print(&s); |
Stephen P. Smith | 73ba5d7 | 2018-09-30 07:12:45 -0700 | [diff] [blame] | 1607 | wt_status_collect_free_buffers(&s); |
| 1608 | |
Junio C Hamano | 76e2f7c | 2009-08-07 23:31:57 -0700 | [diff] [blame] | 1609 | return 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1610 | } |
| 1611 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1612 | static int git_commit_config(const char *k, const char *v, |
| 1613 | const struct config_context *ctx, void *cb) |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1614 | { |
Junio C Hamano | d249b09 | 2009-08-09 21:59:30 -0700 | [diff] [blame] | 1615 | struct wt_status *s = cb; |
| 1616 | |
Brian Hetro | 984c6e7 | 2008-07-05 01:24:40 -0400 | [diff] [blame] | 1617 | if (!strcmp(k, "commit.template")) |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 1618 | return git_config_pathname(&template_file, k, v); |
James P. Howard, II | bed575e | 2009-12-07 17:45:27 -0500 | [diff] [blame] | 1619 | if (!strcmp(k, "commit.status")) { |
| 1620 | include_status = git_config_bool(k, v); |
| 1621 | return 0; |
| 1622 | } |
Ralf Thielow | 51fb3a3 | 2013-01-10 18:45:59 +0100 | [diff] [blame] | 1623 | if (!strcmp(k, "commit.cleanup")) |
| 1624 | return git_config_string(&cleanup_arg, k, v); |
Nicolas Vigier | d95bfb1 | 2013-11-05 00:14:41 +0100 | [diff] [blame] | 1625 | if (!strcmp(k, "commit.gpgsign")) { |
| 1626 | sign_commit = git_config_bool(k, v) ? "" : NULL; |
| 1627 | return 0; |
| 1628 | } |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1629 | if (!strcmp(k, "commit.verbose")) { |
| 1630 | int is_bool; |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1631 | config_commit_verbose = git_config_bool_or_int(k, v, ctx->kvi, |
| 1632 | &is_bool); |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1635 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1636 | return git_status_config(k, v, ctx, s); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1637 | } |
| 1638 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1639 | int cmd_commit(int argc, const char **argv, const char *prefix) |
| 1640 | { |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1641 | static struct wt_status s; |
| 1642 | static struct option builtin_commit_options[] = { |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1643 | OPT__QUIET(&quiet, N_("suppress summary after successful commit")), |
| 1644 | OPT__VERBOSE(&verbose, N_("show diff in commit message template")), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1645 | |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1646 | OPT_GROUP(N_("Commit message options")), |
| 1647 | OPT_FILENAME('F', "file", &logfile, N_("read message from file")), |
| 1648 | OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")), |
| 1649 | OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")), |
| 1650 | OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m), |
| 1651 | OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")), |
| 1652 | OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")), |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1653 | /* |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1654 | * TRANSLATORS: Leave "[(amend|reword):]" as-is, |
| 1655 | * and only translate <commit>. |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1656 | */ |
Charvi Mendiratta | 3270ae8 | 2021-03-15 13:24:33 +0530 | [diff] [blame] | 1657 | 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] | 1658 | 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] | 1659 | OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), |
Jeff King | 116761b | 2022-10-06 09:11:31 -0400 | [diff] [blame] | 1660 | OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer), |
Bradley M. Kuhn | 3abd4a6 | 2020-10-19 18:03:55 -0700 | [diff] [blame] | 1661 | 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] | 1662 | OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), |
| 1663 | OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), |
Denton Liu | ca04dc9 | 2019-04-17 11:23:26 +0100 | [diff] [blame] | 1664 | OPT_CLEANUP(&cleanup_arg), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1665 | 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] | 1666 | { 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] | 1667 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1668 | /* end commit message options */ |
| 1669 | |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1670 | OPT_GROUP(N_("Commit contents options")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1671 | OPT_BOOL('a', "all", &all, N_("commit all changed files")), |
| 1672 | OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")), |
| 1673 | OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")), |
| 1674 | OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")), |
| 1675 | OPT_BOOL('o', "only", &only, N_("commit only specified files")), |
Orgad Shaneh | def480f | 2016-07-26 17:00:15 +0300 | [diff] [blame] | 1676 | 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] | 1677 | 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] | 1678 | OPT_SET_INT(0, "short", &status_format, N_("show status concisely"), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1679 | STATUS_FORMAT_SHORT), |
Junio C Hamano | 84b4202 | 2013-06-24 11:41:40 -0700 | [diff] [blame] | 1680 | OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")), |
Jeff Hostetler | fd9b544 | 2018-01-09 18:50:16 +0000 | [diff] [blame] | 1681 | OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags, |
| 1682 | N_("compute full ahead/behind values")), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1683 | OPT_SET_INT(0, "porcelain", &status_format, |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1684 | N_("machine-readable output"), STATUS_FORMAT_PORCELAIN), |
Jeff King | f3f47a1 | 2012-10-18 21:15:50 +0700 | [diff] [blame] | 1685 | OPT_SET_INT(0, "long", &status_format, |
| 1686 | N_("show status in long format (default)"), |
| 1687 | STATUS_FORMAT_LONG), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1688 | OPT_BOOL('z', "null", &s.null_termination, |
| 1689 | N_("terminate entries with NUL")), |
| 1690 | OPT_BOOL(0, "amend", &amend, N_("amend previous commit")), |
| 1691 | OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")), |
Nguyễn Thái Ngọc Duy | 9c23f4c | 2012-08-20 19:32:04 +0700 | [diff] [blame] | 1692 | { 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] | 1693 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
| 1694 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1695 | /* end commit contents options */ |
| 1696 | |
Stefan Beller | 4741edd | 2013-08-03 13:51:18 +0200 | [diff] [blame] | 1697 | OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty, |
| 1698 | N_("ok to record an empty change")), |
| 1699 | OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message, |
| 1700 | N_("ok to record a change with an empty message")), |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1701 | |
| 1702 | OPT_END() |
| 1703 | }; |
| 1704 | |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1705 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | 4c28e4a | 2010-12-20 17:00:36 -0800 | [diff] [blame] | 1706 | struct strbuf author_ident = STRBUF_INIT; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1707 | const char *index_file, *reflog_msg; |
brian m. carlson | 8066df4 | 2017-02-20 00:10:14 +0000 | [diff] [blame] | 1708 | struct object_id oid; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1709 | struct commit_list *parents = NULL; |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1710 | struct stat statbuf; |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1711 | struct commit *current_head = NULL; |
Junio C Hamano | ed7a42a | 2011-11-08 15:38:07 -0800 | [diff] [blame] | 1712 | struct commit_extra_header *extra = NULL; |
Ronnie Sahlberg | c0fe1ed | 2014-04-16 15:34:19 -0700 | [diff] [blame] | 1713 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1714 | int ret = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1715 | |
Nguyễn Thái Ngọc Duy | 5d3dd91 | 2010-10-22 01:45:47 -0500 | [diff] [blame] | 1716 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 1717 | usage_with_options(builtin_commit_usage, builtin_commit_options); |
| 1718 | |
Derrick Stolee | daa1ace | 2021-06-29 02:13:04 +0000 | [diff] [blame] | 1719 | prepare_repo_settings(the_repository); |
| 1720 | the_repository->settings.command_requires_full_index = 0; |
| 1721 | |
Matthieu Moy | 5c25dfa | 2013-09-12 12:50:04 +0200 | [diff] [blame] | 1722 | status_init_config(&s, git_commit_config); |
Kaartic Sivaraam | 4ddb135 | 2017-06-21 23:46:14 +0530 | [diff] [blame] | 1723 | s.commit_template = 1; |
Ramkumar Ramachandra | f0915cb | 2013-06-24 18:15:12 +0530 | [diff] [blame] | 1724 | status_format = STATUS_FORMAT_NONE; /* Ignore status.short */ |
Jeff King | 4d2292e | 2012-05-07 15:35:03 -0400 | [diff] [blame] | 1725 | s.colopts = 0; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1726 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1727 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1728 | current_head = NULL; |
| 1729 | else { |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 1730 | current_head = lookup_commit_or_die(&oid, "HEAD"); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1731 | if (repo_parse_commit(the_repository, current_head)) |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1732 | die(_("could not parse HEAD commit")); |
| 1733 | } |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1734 | verbose = -1; /* unspecified */ |
Jeff King | 036dbbf | 2012-05-07 15:18:26 -0400 | [diff] [blame] | 1735 | argc = parse_and_validate_options(argc, argv, builtin_commit_options, |
| 1736 | builtin_commit_usage, |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1737 | prefix, current_head, &s); |
Pranit Bauva | aaab842 | 2016-05-05 15:20:02 +0530 | [diff] [blame] | 1738 | if (verbose == -1) |
| 1739 | verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose; |
| 1740 | |
Jeff King | c9bfb95 | 2011-08-17 22:05:35 -0700 | [diff] [blame] | 1741 | if (dry_run) |
Jeff King | e885a84 | 2020-09-30 08:28:18 -0400 | [diff] [blame] | 1742 | return dry_run_commit(argv, prefix, current_head, &s); |
| 1743 | index_file = prepare_index(argv, prefix, current_head, 0); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1744 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1745 | /* Set up everything for writing the commit object. This includes |
| 1746 | running hooks, writing the trees, and interacting with the user. */ |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1747 | if (!prepare_to_commit(index_file, prefix, |
| 1748 | current_head, &s, &author_ident)) { |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1749 | ret = 1; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1750 | rollback_index_files(); |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1751 | goto cleanup; |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1752 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1753 | |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1754 | /* Determine parents */ |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1755 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
Junio C Hamano | 06bb643 | 2011-08-19 11:58:18 -0700 | [diff] [blame] | 1756 | if (!current_head) { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1757 | if (!reflog_msg) |
| 1758 | reflog_msg = "commit (initial)"; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1759 | } else if (amend) { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1760 | if (!reflog_msg) |
| 1761 | reflog_msg = "commit (amend)"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1762 | parents = copy_commit_list(current_head->parents); |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1763 | } else if (whence == FROM_MERGE) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1764 | struct strbuf m = STRBUF_INIT; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1765 | FILE *fp; |
Elia Pinto | e23fd15 | 2014-01-30 07:15:56 -0800 | [diff] [blame] | 1766 | int allow_fast_forward = 1; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1767 | struct commit_list **pptr = &parents; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1768 | |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1769 | if (!reflog_msg) |
| 1770 | reflog_msg = "commit (merge)"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1771 | pptr = commit_list_append(current_head, pptr); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1772 | fp = xfopen(git_path_merge_head(the_repository), "r"); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 1773 | while (strbuf_getline_lf(&m, fp) != EOF) { |
Junio C Hamano | 5231c63 | 2011-11-07 16:21:32 -0800 | [diff] [blame] | 1774 | struct commit *parent; |
| 1775 | |
| 1776 | parent = get_merge_parent(m.buf); |
| 1777 | if (!parent) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1778 | die(_("Corrupt MERGE_HEAD file (%s)"), m.buf); |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1779 | pptr = commit_list_append(parent, pptr); |
Linus Torvalds | 7c3fd25 | 2008-01-15 16:12:33 -0800 | [diff] [blame] | 1780 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1781 | fclose(fp); |
| 1782 | strbuf_release(&m); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1783 | if (!stat(git_path_merge_mode(the_repository), &statbuf)) { |
| 1784 | if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0) |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1785 | die_errno(_("could not read MERGE_MODE")); |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1786 | if (!strcmp(sb.buf, "no-ff")) |
| 1787 | allow_fast_forward = 0; |
| 1788 | } |
| 1789 | if (allow_fast_forward) |
Martin Ågren | 4da7264 | 2017-11-07 21:39:45 +0100 | [diff] [blame] | 1790 | reduce_heads_replace(&parents); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1791 | } else { |
Christian Couder | 643cb5f | 2010-06-12 18:05:12 +0200 | [diff] [blame] | 1792 | if (!reflog_msg) |
Phillip Wood | 8d57f75 | 2019-12-06 16:06:10 +0000 | [diff] [blame] | 1793 | reflog_msg = is_from_cherry_pick(whence) |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1794 | ? "commit (cherry-pick)" |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1795 | : is_from_rebase(whence) |
| 1796 | ? "commit (rebase)" |
Jay Soffian | 37f7a85 | 2011-02-19 23:12:29 -0500 | [diff] [blame] | 1797 | : "commit"; |
René Scharfe | de9f7fa | 2016-10-29 14:55:36 +0200 | [diff] [blame] | 1798 | commit_list_insert(current_head, &parents); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1799 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1800 | |
Paolo Bonzini | ec84bd0 | 2008-02-05 11:01:46 +0100 | [diff] [blame] | 1801 | /* Finally, get the commit message */ |
Miklos Vajna | cf10f9f | 2008-10-03 14:04:47 +0200 | [diff] [blame] | 1802 | strbuf_reset(&sb); |
Pranit Bauva | e51b0df | 2016-05-25 00:49:50 +0530 | [diff] [blame] | 1803 | if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) { |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 1804 | int saved_errno = errno; |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 1805 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1806 | die(_("could not read commit message: %s"), strerror(saved_errno)); |
Junio C Hamano | 740001a | 2007-12-08 23:23:20 -0800 | [diff] [blame] | 1807 | } |
Kristian Høgsberg | 99a1269 | 2007-11-21 21:54:49 -0500 | [diff] [blame] | 1808 | |
Denton Liu | f29cd86 | 2019-04-17 11:23:25 +0100 | [diff] [blame] | 1809 | cleanup_message(&sb, cleanup_mode, verbose); |
Kaartic Sivaraam | bc17f35 | 2017-07-17 21:06:15 +0530 | [diff] [blame] | 1810 | |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 1811 | if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1812 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1813 | fprintf(stderr, _("Aborting commit due to empty commit message.\n")); |
Jeff King | fdc7c81 | 2008-07-31 03:36:09 -0400 | [diff] [blame] | 1814 | exit(1); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1815 | } |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 1816 | if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) { |
Kaartic Sivaraam | bc17f35 | 2017-07-17 21:06:15 +0530 | [diff] [blame] | 1817 | rollback_index_files(); |
| 1818 | fprintf(stderr, _("Aborting commit; you did not edit the message.\n")); |
| 1819 | exit(1); |
| 1820 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1821 | |
Charvi Mendiratta | 494d314 | 2021-03-15 13:24:32 +0530 | [diff] [blame] | 1822 | if (fixup_message && starts_with(sb.buf, "amend! ") && |
| 1823 | !allow_empty_message) { |
| 1824 | struct strbuf body = STRBUF_INIT; |
| 1825 | size_t len = commit_subject_length(sb.buf); |
| 1826 | strbuf_addstr(&body, sb.buf + len); |
| 1827 | if (message_is_empty(&body, cleanup_mode)) { |
| 1828 | rollback_index_files(); |
| 1829 | fprintf(stderr, _("Aborting commit due to empty commit message body.\n")); |
| 1830 | exit(1); |
| 1831 | } |
| 1832 | strbuf_release(&body); |
| 1833 | } |
| 1834 | |
Junio C Hamano | 0074d18 | 2011-12-20 13:20:56 -0800 | [diff] [blame] | 1835 | if (amend) { |
brian m. carlson | 42d4e1d | 2020-02-22 20:17:42 +0000 | [diff] [blame] | 1836 | const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL }; |
Junio C Hamano | c871a1d | 2012-01-05 10:54:14 -0800 | [diff] [blame] | 1837 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
Junio C Hamano | 0074d18 | 2011-12-20 13:20:56 -0800 | [diff] [blame] | 1838 | } else { |
| 1839 | struct commit_extra_header **tail = &extra; |
| 1840 | append_merge_tag_headers(parents, &tail); |
| 1841 | } |
Junio C Hamano | ed7a42a | 2011-11-08 15:38:07 -0800 | [diff] [blame] | 1842 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 1843 | if (commit_tree_extended(sb.buf, sb.len, &the_index.cache_tree->oid, |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 1844 | parents, &oid, author_ident.buf, NULL, |
| 1845 | sign_commit, extra)) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1846 | rollback_index_files(); |
Ævar Arnfjörð Bjarmason | 8a6179b | 2011-02-22 23:41:44 +0000 | [diff] [blame] | 1847 | die(_("failed to write commit object")); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1848 | } |
Junio C Hamano | ed7a42a | 2011-11-08 15:38:07 -0800 | [diff] [blame] | 1849 | free_commit_extra_headers(extra); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1850 | |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 1851 | if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb, |
| 1852 | &err)) { |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1853 | rollback_index_files(); |
Ronnie Sahlberg | c0fe1ed | 2014-04-16 15:34:19 -0700 | [diff] [blame] | 1854 | die("%s", err.buf); |
Junio C Hamano | 2888605 | 2007-11-18 01:52:55 -0800 | [diff] [blame] | 1855 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1856 | |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 1857 | sequencer_post_commit_cleanup(the_repository, 0); |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1858 | unlink(git_path_merge_head(the_repository)); |
| 1859 | unlink(git_path_merge_msg(the_repository)); |
| 1860 | unlink(git_path_merge_mode(the_repository)); |
| 1861 | unlink(git_path_squash_msg(the_repository)); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1862 | |
Brandon Casey | 5a9dd39 | 2008-01-23 11:21:22 -0600 | [diff] [blame] | 1863 | if (commit_index_files()) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 1864 | die(_("repository has been updated, but unable to write\n" |
| 1865 | "new_index file. Check that disk is not full and quota is\n" |
Nguyễn Thái Ngọc Duy | 80f537f | 2019-04-25 16:45:58 +0700 | [diff] [blame] | 1866 | "not exceeded, and then \"git restore --staged :/\" to recover.")); |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1867 | |
Derrick Stolee | b23ea97 | 2020-04-16 20:14:03 +0000 | [diff] [blame] | 1868 | git_test_write_commit_graph_or_die(); |
Derrick Stolee | 859fdc0 | 2018-08-29 05:49:04 -0700 | [diff] [blame] | 1869 | |
Nguyễn Thái Ngọc Duy | 35843b1 | 2018-09-21 17:57:32 +0200 | [diff] [blame] | 1870 | repo_rerere(the_repository, 0); |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 1871 | run_auto_maintenance(quiet); |
Ævar Arnfjörð Bjarmason | a8cc594 | 2022-03-07 13:33:46 +0100 | [diff] [blame] | 1872 | run_commit_hook(use_editor, get_index_file(), NULL, "post-commit", |
| 1873 | NULL); |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 1874 | if (amend && !no_post_rewrite) { |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 1875 | commit_post_rewrite(the_repository, current_head, &oid); |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 1876 | } |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1877 | if (!quiet) { |
| 1878 | unsigned int flags = 0; |
| 1879 | |
| 1880 | if (!current_head) |
| 1881 | flags |= SUMMARY_INITIAL_COMMIT; |
| 1882 | if (author_date_is_interesting()) |
| 1883 | flags |= SUMMARY_SHOW_AUTHOR_DATE; |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1884 | print_commit_summary(the_repository, prefix, |
| 1885 | &oid, flags); |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1886 | } |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1887 | |
Denton Liu | a03b555 | 2020-04-07 10:28:07 -0400 | [diff] [blame] | 1888 | apply_autostash(git_path_merge_autostash(the_repository)); |
| 1889 | |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1890 | cleanup: |
Ævar Arnfjörð Bjarmason | ac95f5d | 2022-11-08 19:17:51 +0100 | [diff] [blame] | 1891 | strbuf_release(&author_ident); |
| 1892 | strbuf_release(&err); |
| 1893 | strbuf_release(&sb); |
Junio C Hamano | 00d8c31 | 2022-05-12 15:51:07 -0700 | [diff] [blame] | 1894 | return ret; |
Kristian Høgsberg | f5bbc32 | 2007-11-08 11:59:00 -0500 | [diff] [blame] | 1895 | } |