Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 3 | #include "lockfile.h" |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 4 | #include "dir.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 5 | #include "object-store.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 6 | #include "object.h" |
| 7 | #include "commit.h" |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 8 | #include "sequencer.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 9 | #include "tag.h" |
| 10 | #include "run-command.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 11 | #include "exec-cmd.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 12 | #include "utf8.h" |
| 13 | #include "cache-tree.h" |
| 14 | #include "diff.h" |
| 15 | #include "revision.h" |
| 16 | #include "rerere.h" |
| 17 | #include "merge-recursive.h" |
| 18 | #include "refs.h" |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 19 | #include "argv-array.h" |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 20 | #include "quote.h" |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 21 | #include "trailer.h" |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 22 | #include "log-tree.h" |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 23 | #include "wt-status.h" |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 24 | #include "hashmap.h" |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 25 | #include "notes-utils.h" |
| 26 | #include "sigchain.h" |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 27 | #include "unpack-trees.h" |
| 28 | #include "worktree.h" |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 29 | #include "oidmap.h" |
| 30 | #include "oidset.h" |
Nguyễn Thái Ngọc Duy | 8315bd2 | 2018-05-19 07:28:22 +0200 | [diff] [blame] | 31 | #include "commit-slab.h" |
Nguyễn Thái Ngọc Duy | 65b5f94 | 2018-05-20 20:40:06 +0200 | [diff] [blame] | 32 | #include "alias.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 33 | #include "commit-reach.h" |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 34 | #include "rebase-interactive.h" |
Denton Liu | 0816f1d | 2020-04-07 10:28:03 -0400 | [diff] [blame] | 35 | #include "reset.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 36 | |
| 37 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 38 | |
Ramsay Jones | 5fe8143 | 2019-02-11 17:16:58 +0000 | [diff] [blame] | 39 | static const char sign_off_header[] = "Signed-off-by: "; |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 40 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 41 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 42 | GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
| 43 | |
Phillip Wood | 901ba7b | 2019-12-06 16:06:11 +0000 | [diff] [blame] | 44 | static GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
Johannes Schindelin | 8a2a0f5 | 2016-10-14 15:17:12 +0200 | [diff] [blame] | 45 | |
| 46 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") |
| 47 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") |
| 48 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 49 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 50 | |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 51 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
| 52 | /* |
| 53 | * The file containing rebase commands, comments, and empty lines. |
| 54 | * This file is created by "git rebase -i" then edited by the user. As |
| 55 | * the lines are processed, they are removed from the front of this |
| 56 | * file and written to the tail of 'done'. |
| 57 | */ |
Alban Gruin | 44b776c | 2018-08-10 18:51:28 +0200 | [diff] [blame] | 58 | GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 59 | GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup") |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 60 | |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 61 | GIT_PATH_FUNC(rebase_path_dropped, "rebase-merge/dropped") |
| 62 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 63 | /* |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 64 | * The rebase command lines that have already been processed. A line |
| 65 | * is moved here when it is first handled, before any associated user |
| 66 | * actions. |
| 67 | */ |
| 68 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") |
| 69 | /* |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 70 | * The file to keep track of how many commands were already processed (e.g. |
| 71 | * for the prompt). |
| 72 | */ |
Beat Bolli | 9ad3635 | 2018-07-09 21:25:35 +0200 | [diff] [blame] | 73 | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum") |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 74 | /* |
| 75 | * The file to keep track of how many commands are to be processed in total |
| 76 | * (e.g. for the prompt). |
| 77 | */ |
Beat Bolli | 9ad3635 | 2018-07-09 21:25:35 +0200 | [diff] [blame] | 78 | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end") |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 79 | /* |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 80 | * The commit message that is planned to be used for any changes that |
| 81 | * need to be committed following a user interaction. |
| 82 | */ |
| 83 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") |
| 84 | /* |
| 85 | * The file into which is accumulated the suggested commit message for |
| 86 | * squash/fixup commands. When the first of a series of squash/fixups |
| 87 | * is seen, the file is created and the commit message from the |
| 88 | * previous commit and from the first squash/fixup commit are written |
| 89 | * to it. The commit message for each subsequent squash/fixup commit |
| 90 | * is appended to the file as it is processed. |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 91 | */ |
| 92 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") |
| 93 | /* |
| 94 | * If the current series of squash/fixups has not yet included a squash |
| 95 | * command, then this file exists and holds the commit message of the |
| 96 | * original "pick" commit. (If the series ends without a "squash" |
| 97 | * command, then this can be used as the commit message of the combined |
| 98 | * commit without opening the editor.) |
| 99 | */ |
| 100 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") |
| 101 | /* |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 102 | * This file contains the list fixup/squash commands that have been |
| 103 | * accumulated into message-fixup or message-squash so far. |
| 104 | */ |
| 105 | static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups") |
| 106 | /* |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 107 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
| 108 | * GIT_AUTHOR_DATE that will be used for the commit that is currently |
| 109 | * being rebased. |
| 110 | */ |
| 111 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 112 | /* |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 113 | * When an "edit" rebase command is being processed, the SHA1 of the |
| 114 | * commit to be edited is recorded in this file. When "git rebase |
| 115 | * --continue" is executed, if there are any staged changes then they |
| 116 | * will be amended to the HEAD commit, but only provided the HEAD |
| 117 | * commit is still the commit to be edited. When any other rebase |
| 118 | * command is processed, this file is deleted. |
| 119 | */ |
| 120 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") |
| 121 | /* |
| 122 | * When we stop at a given patch via the "edit" command, this file contains |
| 123 | * the abbreviated commit name of the corresponding patch. |
| 124 | */ |
| 125 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") |
| 126 | /* |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 127 | * For the post-rewrite hook, we make a list of rewritten commits and |
| 128 | * their new sha1s. The rewritten-pending list keeps the sha1s of |
| 129 | * commits that have been processed, but not committed yet, |
| 130 | * e.g. because they are waiting for a 'squash' command. |
| 131 | */ |
| 132 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") |
| 133 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, |
| 134 | "rebase-merge/rewritten-pending") |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 135 | |
| 136 | /* |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 137 | * The path of the file containing the OID of the "squash onto" commit, i.e. |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 138 | * the dummy commit used for `reset [new root]`. |
| 139 | */ |
| 140 | static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto") |
| 141 | |
| 142 | /* |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 143 | * The path of the file listing refs that need to be deleted after the rebase |
| 144 | * finishes. This is used by the `label` command to record the need for cleanup. |
| 145 | */ |
| 146 | static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") |
| 147 | |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 148 | /* |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 149 | * The following files are written by git-rebase just after parsing the |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 150 | * command-line. |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 151 | */ |
| 152 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 153 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
| 154 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 155 | static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet") |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 156 | static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff") |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 157 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
| 158 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 159 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 160 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
| 161 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") |
Phillip Wood | 9b6d7a6 | 2017-08-02 11:44:17 +0100 | [diff] [blame] | 162 | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 163 | static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec") |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 164 | static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits") |
| 165 | static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits") |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 166 | |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 167 | static int git_sequencer_config(const char *k, const char *v, void *cb) |
| 168 | { |
| 169 | struct replay_opts *opts = cb; |
| 170 | int status; |
| 171 | |
| 172 | if (!strcmp(k, "commit.cleanup")) { |
| 173 | const char *s; |
| 174 | |
| 175 | status = git_config_string(&s, k, v); |
| 176 | if (status) |
| 177 | return status; |
| 178 | |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 179 | if (!strcmp(s, "verbatim")) { |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 180 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 181 | opts->explicit_cleanup = 1; |
| 182 | } else if (!strcmp(s, "whitespace")) { |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 183 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 184 | opts->explicit_cleanup = 1; |
| 185 | } else if (!strcmp(s, "strip")) { |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 186 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 187 | opts->explicit_cleanup = 1; |
| 188 | } else if (!strcmp(s, "scissors")) { |
Denton Liu | 1a2b985 | 2019-04-17 11:23:30 +0100 | [diff] [blame] | 189 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS; |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 190 | opts->explicit_cleanup = 1; |
| 191 | } else { |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 192 | warning(_("invalid commit message cleanup mode '%s'"), |
| 193 | s); |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 194 | } |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 195 | |
Stefan Beller | f40f3c1 | 2018-06-01 13:01:46 -0700 | [diff] [blame] | 196 | free((char *)s); |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 197 | return status; |
| 198 | } |
| 199 | |
| 200 | if (!strcmp(k, "commit.gpgsign")) { |
Johannes Schindelin | ed1e528 | 2017-12-22 12:50:50 +0100 | [diff] [blame] | 201 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | status = git_gpg_config(k, v, NULL); |
| 206 | if (status) |
| 207 | return status; |
| 208 | |
| 209 | return git_diff_basic_config(k, v, NULL); |
| 210 | } |
| 211 | |
| 212 | void sequencer_init_config(struct replay_opts *opts) |
| 213 | { |
| 214 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
| 215 | git_config(git_sequencer_config, opts); |
| 216 | } |
| 217 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 218 | static inline int is_rebase_i(const struct replay_opts *opts) |
| 219 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 220 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Johannes Schindelin | 285abf5 | 2016-10-14 15:17:20 +0200 | [diff] [blame] | 223 | static const char *get_dir(const struct replay_opts *opts) |
| 224 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 225 | if (is_rebase_i(opts)) |
| 226 | return rebase_path(); |
Johannes Schindelin | 285abf5 | 2016-10-14 15:17:20 +0200 | [diff] [blame] | 227 | return git_path_seq_dir(); |
| 228 | } |
| 229 | |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 230 | static const char *get_todo_path(const struct replay_opts *opts) |
| 231 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 232 | if (is_rebase_i(opts)) |
| 233 | return rebase_path_todo(); |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 234 | return git_path_todo_file(); |
| 235 | } |
| 236 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 237 | /* |
| 238 | * Returns 0 for non-conforming footer |
| 239 | * Returns 1 for conforming footer |
| 240 | * Returns 2 when sob exists within conforming footer |
| 241 | * Returns 3 when sob exists within conforming footer as last entry |
| 242 | */ |
| 243 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
Jeff King | 66e83d9 | 2018-08-22 20:50:51 -0400 | [diff] [blame] | 244 | size_t ignore_footer) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 245 | { |
Jeff King | 00a21f5 | 2018-08-22 20:46:23 -0400 | [diff] [blame] | 246 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 247 | struct trailer_info info; |
Jeff King | a3b636e | 2018-08-22 20:45:44 -0400 | [diff] [blame] | 248 | size_t i; |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 249 | int found_sob = 0, found_sob_last = 0; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 250 | |
Jeff King | ffce7f5 | 2018-08-22 20:50:37 -0400 | [diff] [blame] | 251 | opts.no_divider = 1; |
| 252 | |
Jeff King | 00a21f5 | 2018-08-22 20:46:23 -0400 | [diff] [blame] | 253 | trailer_info_get(&info, sb->buf, &opts); |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 254 | |
| 255 | if (info.trailer_start == info.trailer_end) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 256 | return 0; |
| 257 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 258 | for (i = 0; i < info.trailer_nr; i++) |
| 259 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { |
| 260 | found_sob = 1; |
| 261 | if (i == info.trailer_nr - 1) |
| 262 | found_sob_last = 1; |
| 263 | } |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 264 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 265 | trailer_info_release(&info); |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 266 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 267 | if (found_sob_last) |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 268 | return 3; |
| 269 | if (found_sob) |
| 270 | return 2; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 271 | return 1; |
| 272 | } |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 273 | |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 274 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
| 275 | { |
| 276 | static struct strbuf buf = STRBUF_INIT; |
| 277 | |
| 278 | strbuf_reset(&buf); |
| 279 | if (opts->gpg_sign) |
| 280 | sq_quotef(&buf, "-S%s", opts->gpg_sign); |
| 281 | return buf.buf; |
| 282 | } |
| 283 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 284 | int sequencer_remove_state(struct replay_opts *opts) |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 285 | { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 286 | struct strbuf buf = STRBUF_INIT; |
Phillip Wood | 37e9ee5 | 2019-05-14 19:03:48 +0100 | [diff] [blame] | 287 | int i, ret = 0; |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 288 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 289 | if (is_rebase_i(opts) && |
| 290 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { |
| 291 | char *p = buf.buf; |
| 292 | while (*p) { |
| 293 | char *eol = strchr(p, '\n'); |
| 294 | if (eol) |
| 295 | *eol = '\0'; |
Elijah Newren | c2417d3 | 2020-02-15 21:36:36 +0000 | [diff] [blame] | 296 | if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 297 | warning(_("could not delete '%s'"), p); |
Phillip Wood | 37e9ee5 | 2019-05-14 19:03:48 +0100 | [diff] [blame] | 298 | ret = -1; |
| 299 | } |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 300 | if (!eol) |
| 301 | break; |
| 302 | p = eol + 1; |
| 303 | } |
| 304 | } |
| 305 | |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 306 | free(opts->gpg_sign); |
| 307 | free(opts->strategy); |
| 308 | for (i = 0; i < opts->xopts_nr; i++) |
| 309 | free(opts->xopts[i]); |
| 310 | free(opts->xopts); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 311 | strbuf_release(&opts->current_fixups); |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 312 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 313 | strbuf_reset(&buf); |
| 314 | strbuf_addstr(&buf, get_dir(opts)); |
Phillip Wood | 37e9ee5 | 2019-05-14 19:03:48 +0100 | [diff] [blame] | 315 | if (remove_dir_recursively(&buf, 0)) |
| 316 | ret = error(_("could not remove '%s'"), buf.buf); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 317 | strbuf_release(&buf); |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 318 | |
Phillip Wood | 37e9ee5 | 2019-05-14 19:03:48 +0100 | [diff] [blame] | 319 | return ret; |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 320 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 321 | |
| 322 | static const char *action_name(const struct replay_opts *opts) |
| 323 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 324 | switch (opts->action) { |
| 325 | case REPLAY_REVERT: |
| 326 | return N_("revert"); |
| 327 | case REPLAY_PICK: |
| 328 | return N_("cherry-pick"); |
| 329 | case REPLAY_INTERACTIVE_REBASE: |
Elijah Newren | c2417d3 | 2020-02-15 21:36:36 +0000 | [diff] [blame] | 330 | return N_("rebase"); |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 331 | } |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 332 | die(_("unknown action: %d"), opts->action); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 333 | } |
| 334 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 335 | struct commit_message { |
| 336 | char *parent_label; |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 337 | char *label; |
| 338 | char *subject; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 339 | const char *message; |
| 340 | }; |
| 341 | |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 342 | static const char *short_commit_name(struct commit *commit) |
| 343 | { |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 344 | return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV); |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 347 | static int get_message(struct commit *commit, struct commit_message *out) |
| 348 | { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 349 | const char *abbrev, *subject; |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 350 | int subject_len; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 351 | |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 352 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 353 | abbrev = short_commit_name(commit); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 354 | |
| 355 | subject_len = find_commit_subject(out->message, &subject); |
| 356 | |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 357 | out->subject = xmemdupz(subject, subject_len); |
| 358 | out->label = xstrfmt("%s... %s", abbrev, out->subject); |
| 359 | out->parent_label = xstrfmt("parent of %s", out->label); |
| 360 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 364 | static void free_message(struct commit *commit, struct commit_message *msg) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 365 | { |
| 366 | free(msg->parent_label); |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 367 | free(msg->label); |
| 368 | free(msg->subject); |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 369 | unuse_commit_buffer(commit, msg->message); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 370 | } |
| 371 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 372 | static void print_advice(struct repository *r, int show_hint, |
| 373 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 374 | { |
| 375 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 376 | |
| 377 | if (msg) { |
| 378 | fprintf(stderr, "%s\n", msg); |
| 379 | /* |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 380 | * A conflict has occurred but the porcelain |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 381 | * (typically rebase --interactive) wants to take care |
| 382 | * of the commit itself so remove CHERRY_PICK_HEAD |
| 383 | */ |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 384 | unlink(git_path_cherry_pick_head(r)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 388 | if (show_hint) { |
| 389 | if (opts->no_commit) |
| 390 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 391 | "with 'git add <paths>' or 'git rm <paths>'")); |
| 392 | else |
| 393 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 394 | "with 'git add <paths>' or 'git rm <paths>'\n" |
| 395 | "and commit the result with 'git commit'")); |
| 396 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 397 | } |
| 398 | |
Alban Gruin | ddb81e5 | 2019-03-05 20:18:01 +0100 | [diff] [blame] | 399 | static int write_message(const void *buf, size_t len, const char *filename, |
| 400 | int append_eol) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 401 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 402 | struct lock_file msg_file = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 403 | |
Johannes Schindelin | 4ef3d8f | 2016-09-09 16:37:05 +0200 | [diff] [blame] | 404 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
| 405 | if (msg_fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 406 | return error_errno(_("could not lock '%s'"), filename); |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 407 | if (write_in_full(msg_fd, buf, len) < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 408 | error_errno(_("could not write to '%s'"), filename); |
Johannes Schindelin | 4f66c83 | 2016-10-21 14:26:00 +0200 | [diff] [blame] | 409 | rollback_lock_file(&msg_file); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 410 | return -1; |
Johannes Schindelin | 4f66c83 | 2016-10-21 14:26:00 +0200 | [diff] [blame] | 411 | } |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 412 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 413 | error_errno(_("could not write eol to '%s'"), filename); |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 414 | rollback_lock_file(&msg_file); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 415 | return -1; |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 416 | } |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 417 | if (commit_lock_file(&msg_file) < 0) |
| 418 | return error(_("failed to finalize '%s'"), filename); |
Johannes Schindelin | 4ef3d8f | 2016-09-09 16:37:05 +0200 | [diff] [blame] | 419 | |
| 420 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 421 | } |
| 422 | |
Denton Liu | c20de8b | 2020-04-07 10:27:54 -0400 | [diff] [blame] | 423 | int read_oneliner(struct strbuf *buf, |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 424 | const char *path, unsigned flags) |
Johannes Schindelin | 1dfc84e | 2016-10-21 14:25:08 +0200 | [diff] [blame] | 425 | { |
| 426 | int orig_len = buf->len; |
| 427 | |
Johannes Schindelin | 1dfc84e | 2016-10-21 14:25:08 +0200 | [diff] [blame] | 428 | if (strbuf_read_file(buf, path, 0) < 0) { |
Denton Liu | bfa50c2 | 2020-04-07 10:27:53 -0400 | [diff] [blame] | 429 | if ((flags & READ_ONELINER_WARN_MISSING) || |
| 430 | (errno != ENOENT && errno != ENOTDIR)) |
Denton Liu | 5b2f6d9 | 2020-04-07 10:27:51 -0400 | [diff] [blame] | 431 | warning_errno(_("could not read '%s'"), path); |
Johannes Schindelin | 1dfc84e | 2016-10-21 14:25:08 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { |
| 436 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') |
| 437 | --buf->len; |
| 438 | buf->buf[buf->len] = '\0'; |
| 439 | } |
| 440 | |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 441 | if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len) |
Johannes Schindelin | 1dfc84e | 2016-10-21 14:25:08 +0200 | [diff] [blame] | 442 | return 0; |
| 443 | |
| 444 | return 1; |
| 445 | } |
| 446 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 447 | static struct tree *empty_tree(struct repository *r) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 448 | { |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 449 | return lookup_tree(r, the_hash_algo->empty_tree); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 450 | } |
| 451 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 452 | static int error_dirty_index(struct repository *repo, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 453 | { |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 454 | if (repo_read_index_unmerged(repo)) |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 455 | return error_resolve_conflict(_(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 456 | |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 457 | error(_("your local changes would be overwritten by %s."), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 458 | _(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 459 | |
| 460 | if (advice_commit_before_merge) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 461 | advise(_("commit your changes or stash them to proceed.")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 462 | return -1; |
| 463 | } |
| 464 | |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 465 | static void update_abort_safety_file(void) |
| 466 | { |
| 467 | struct object_id head; |
| 468 | |
| 469 | /* Do nothing on a single-pick */ |
| 470 | if (!file_exists(git_path_seq_dir())) |
| 471 | return; |
| 472 | |
| 473 | if (!get_oid("HEAD", &head)) |
| 474 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); |
| 475 | else |
| 476 | write_file(git_path_abort_safety_file(), "%s", ""); |
| 477 | } |
| 478 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 479 | static int fast_forward_to(struct repository *r, |
| 480 | const struct object_id *to, |
| 481 | const struct object_id *from, |
| 482 | int unborn, |
| 483 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 484 | { |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 485 | struct ref_transaction *transaction; |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 486 | struct strbuf sb = STRBUF_INIT; |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 487 | struct strbuf err = STRBUF_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 488 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 489 | repo_read_index(r); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 490 | if (checkout_fast_forward(r, from, to, 1)) |
Johannes Schindelin | 0e408fc | 2016-09-09 16:37:55 +0200 | [diff] [blame] | 491 | return -1; /* the callee should have complained already */ |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 492 | |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 493 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 494 | |
| 495 | transaction = ref_transaction_begin(&err); |
| 496 | if (!transaction || |
| 497 | ref_transaction_update(transaction, "HEAD", |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 498 | to, unborn && !is_rebase_i(opts) ? |
| 499 | &null_oid : from, |
Michael Haggerty | 1d147bd | 2015-02-17 18:00:15 +0100 | [diff] [blame] | 500 | 0, sb.buf, &err) || |
Ronnie Sahlberg | db7516a | 2014-04-30 12:22:42 -0700 | [diff] [blame] | 501 | ref_transaction_commit(transaction, &err)) { |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 502 | ref_transaction_free(transaction); |
| 503 | error("%s", err.buf); |
| 504 | strbuf_release(&sb); |
| 505 | strbuf_release(&err); |
| 506 | return -1; |
| 507 | } |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 508 | |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 509 | strbuf_release(&sb); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 510 | strbuf_release(&err); |
| 511 | ref_transaction_free(transaction); |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 512 | update_abort_safety_file(); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 513 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 514 | } |
| 515 | |
Denton Liu | f29cd86 | 2019-04-17 11:23:25 +0100 | [diff] [blame] | 516 | enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, |
| 517 | int use_editor) |
| 518 | { |
| 519 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) |
| 520 | return use_editor ? COMMIT_MSG_CLEANUP_ALL : |
| 521 | COMMIT_MSG_CLEANUP_SPACE; |
| 522 | else if (!strcmp(cleanup_arg, "verbatim")) |
| 523 | return COMMIT_MSG_CLEANUP_NONE; |
| 524 | else if (!strcmp(cleanup_arg, "whitespace")) |
| 525 | return COMMIT_MSG_CLEANUP_SPACE; |
| 526 | else if (!strcmp(cleanup_arg, "strip")) |
| 527 | return COMMIT_MSG_CLEANUP_ALL; |
| 528 | else if (!strcmp(cleanup_arg, "scissors")) |
| 529 | return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : |
| 530 | COMMIT_MSG_CLEANUP_SPACE; |
| 531 | else |
| 532 | die(_("Invalid cleanup mode %s"), cleanup_arg); |
| 533 | } |
| 534 | |
Phillip Wood | dc42e9a | 2019-04-17 11:23:29 +0100 | [diff] [blame] | 535 | /* |
| 536 | * NB using int rather than enum cleanup_mode to stop clang's |
| 537 | * -Wtautological-constant-out-of-range-compare complaining that the comparison |
| 538 | * is always true. |
| 539 | */ |
| 540 | static const char *describe_cleanup_mode(int cleanup_mode) |
| 541 | { |
| 542 | static const char *modes[] = { "whitespace", |
| 543 | "verbatim", |
| 544 | "scissors", |
| 545 | "strip" }; |
| 546 | |
| 547 | if (cleanup_mode < ARRAY_SIZE(modes)) |
| 548 | return modes[cleanup_mode]; |
| 549 | |
| 550 | BUG("invalid cleanup_mode provided (%d)", cleanup_mode); |
| 551 | } |
| 552 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 553 | void append_conflicts_hint(struct index_state *istate, |
Denton Liu | 1a2b985 | 2019-04-17 11:23:30 +0100 | [diff] [blame] | 554 | struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode) |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 555 | { |
| 556 | int i; |
| 557 | |
Denton Liu | 1a2b985 | 2019-04-17 11:23:30 +0100 | [diff] [blame] | 558 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
| 559 | strbuf_addch(msgbuf, '\n'); |
| 560 | wt_status_append_cut_line(msgbuf); |
| 561 | strbuf_addch(msgbuf, comment_line_char); |
| 562 | } |
| 563 | |
Junio C Hamano | 261f315 | 2014-10-28 13:04:38 -0700 | [diff] [blame] | 564 | strbuf_addch(msgbuf, '\n'); |
| 565 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 566 | for (i = 0; i < istate->cache_nr;) { |
| 567 | const struct cache_entry *ce = istate->cache[i++]; |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 568 | if (ce_stage(ce)) { |
Junio C Hamano | 261f315 | 2014-10-28 13:04:38 -0700 | [diff] [blame] | 569 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 570 | while (i < istate->cache_nr && |
| 571 | !strcmp(ce->name, istate->cache[i]->name)) |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 572 | i++; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 577 | static int do_recursive_merge(struct repository *r, |
| 578 | struct commit *base, struct commit *next, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 579 | const char *base_label, const char *next_label, |
brian m. carlson | 48be4c6 | 2017-05-06 22:10:36 +0000 | [diff] [blame] | 580 | struct object_id *head, struct strbuf *msgbuf, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 581 | struct replay_opts *opts) |
| 582 | { |
| 583 | struct merge_options o; |
Elijah Newren | b4db8a2 | 2019-08-17 11:41:30 -0700 | [diff] [blame] | 584 | struct tree *next_tree, *base_tree, *head_tree; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 585 | int clean; |
Jeff King | d20bc01 | 2020-01-29 00:46:47 -0500 | [diff] [blame] | 586 | int i; |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 587 | struct lock_file index_lock = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 588 | |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 589 | if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0) |
Phillip Wood | bd58886 | 2017-11-15 10:41:25 +0000 | [diff] [blame] | 590 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 591 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 592 | repo_read_index(r); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 593 | |
Nguyễn Thái Ngọc Duy | 0d6caa2 | 2019-01-12 09:13:29 +0700 | [diff] [blame] | 594 | init_merge_options(&o, r); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 595 | o.ancestor = base ? base_label : "(empty tree)"; |
| 596 | o.branch1 = "HEAD"; |
| 597 | o.branch2 = next ? next_label : "(empty tree)"; |
Johannes Schindelin | 62fdb65 | 2017-01-02 16:35:39 +0100 | [diff] [blame] | 598 | if (is_rebase_i(opts)) |
| 599 | o.buffer_output = 2; |
Elijah Newren | 9268cf4 | 2017-11-13 12:16:00 -0800 | [diff] [blame] | 600 | o.show_rename_progress = 1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 601 | |
| 602 | head_tree = parse_tree_indirect(head); |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 603 | next_tree = next ? get_commit_tree(next) : empty_tree(r); |
| 604 | base_tree = base ? get_commit_tree(base) : empty_tree(r); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 605 | |
Jeff King | d20bc01 | 2020-01-29 00:46:47 -0500 | [diff] [blame] | 606 | for (i = 0; i < opts->xopts_nr; i++) |
| 607 | parse_merge_opt(&o, opts->xopts[i]); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 608 | |
| 609 | clean = merge_trees(&o, |
| 610 | head_tree, |
Elijah Newren | b4db8a2 | 2019-08-17 11:41:30 -0700 | [diff] [blame] | 611 | next_tree, base_tree); |
Johannes Schindelin | 62fdb65 | 2017-01-02 16:35:39 +0100 | [diff] [blame] | 612 | if (is_rebase_i(opts) && clean <= 0) |
| 613 | fputs(o.obuf.buf, stdout); |
Johannes Schindelin | 548009c | 2016-08-01 13:44:53 +0200 | [diff] [blame] | 614 | strbuf_release(&o.obuf); |
Martin Ågren | 6481652 | 2018-02-27 22:30:10 +0100 | [diff] [blame] | 615 | if (clean < 0) { |
| 616 | rollback_lock_file(&index_lock); |
Johannes Schindelin | f241ff0 | 2016-07-26 18:06:02 +0200 | [diff] [blame] | 617 | return clean; |
Martin Ågren | 6481652 | 2018-02-27 22:30:10 +0100 | [diff] [blame] | 618 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 619 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 620 | if (write_locked_index(r->index, &index_lock, |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 621 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 622 | /* |
| 623 | * TRANSLATORS: %s will be "revert", "cherry-pick" or |
Elijah Newren | c2417d3 | 2020-02-15 21:36:36 +0000 | [diff] [blame] | 624 | * "rebase". |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 625 | */ |
Johannes Schindelin | c527b55 | 2016-09-09 16:37:10 +0200 | [diff] [blame] | 626 | return error(_("%s: Unable to write new index file"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 627 | _(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 628 | |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 629 | if (!clean) |
Denton Liu | 1a2b985 | 2019-04-17 11:23:30 +0100 | [diff] [blame] | 630 | append_conflicts_hint(r->index, msgbuf, |
| 631 | opts->default_msg_cleanup); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 632 | |
| 633 | return !clean; |
| 634 | } |
| 635 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 636 | static struct object_id *get_cache_tree_oid(struct index_state *istate) |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 637 | { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 638 | if (!istate->cache_tree) |
| 639 | istate->cache_tree = cache_tree(); |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 640 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 641 | if (!cache_tree_fully_valid(istate->cache_tree)) |
| 642 | if (cache_tree_update(istate, 0)) { |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 643 | error(_("unable to update cache tree")); |
| 644 | return NULL; |
| 645 | } |
| 646 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 647 | return &istate->cache_tree->oid; |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 650 | static int is_index_unchanged(struct repository *r) |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 651 | { |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 652 | struct object_id head_oid, *cache_tree_oid; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 653 | struct commit *head_commit; |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 654 | struct index_state *istate = r->index; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 655 | |
brian m. carlson | 49e6147 | 2017-10-15 22:07:09 +0000 | [diff] [blame] | 656 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 657 | return error(_("could not resolve HEAD commit")); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 658 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 659 | head_commit = lookup_commit(r, &head_oid); |
Neil Horman | 4b58006 | 2012-05-03 08:10:22 -0400 | [diff] [blame] | 660 | |
| 661 | /* |
| 662 | * If head_commit is NULL, check_commit, called from |
| 663 | * lookup_commit, would have indicated that head_commit is not |
| 664 | * a commit object already. parse_commit() will return failure |
| 665 | * without further complaints in such a case. Otherwise, if |
| 666 | * the commit is invalid, parse_commit() will complain. So |
| 667 | * there is nothing for us to say here. Just return failure. |
| 668 | */ |
| 669 | if (parse_commit(head_commit)) |
| 670 | return -1; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 671 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 672 | if (!(cache_tree_oid = get_cache_tree_oid(istate))) |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 673 | return -1; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 674 | |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 675 | return oideq(cache_tree_oid, get_commit_tree_oid(head_commit)); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 678 | static int write_author_script(const char *message) |
| 679 | { |
| 680 | struct strbuf buf = STRBUF_INIT; |
| 681 | const char *eol; |
| 682 | int res; |
| 683 | |
| 684 | for (;;) |
| 685 | if (!*message || starts_with(message, "\n")) { |
| 686 | missing_author: |
| 687 | /* Missing 'author' line? */ |
| 688 | unlink(rebase_path_author_script()); |
| 689 | return 0; |
| 690 | } else if (skip_prefix(message, "author ", &message)) |
| 691 | break; |
| 692 | else if ((eol = strchr(message, '\n'))) |
| 693 | message = eol + 1; |
| 694 | else |
| 695 | goto missing_author; |
| 696 | |
| 697 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); |
| 698 | while (*message && *message != '\n' && *message != '\r') |
| 699 | if (skip_prefix(message, " <", &message)) |
| 700 | break; |
| 701 | else if (*message != '\'') |
| 702 | strbuf_addch(&buf, *(message++)); |
| 703 | else |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 704 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 705 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); |
| 706 | while (*message && *message != '\n' && *message != '\r') |
| 707 | if (skip_prefix(message, "> ", &message)) |
| 708 | break; |
| 709 | else if (*message != '\'') |
| 710 | strbuf_addch(&buf, *(message++)); |
| 711 | else |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 712 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 713 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); |
| 714 | while (*message && *message != '\n' && *message != '\r') |
| 715 | if (*message != '\'') |
| 716 | strbuf_addch(&buf, *(message++)); |
| 717 | else |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 718 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
Eric Sunshine | 0f16c09 | 2018-07-31 03:33:29 -0400 | [diff] [blame] | 719 | strbuf_addch(&buf, '\''); |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 720 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); |
| 721 | strbuf_release(&buf); |
| 722 | return res; |
| 723 | } |
| 724 | |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 725 | /** |
| 726 | * Take a series of KEY='VALUE' lines where VALUE part is |
| 727 | * sq-quoted, and append <KEY, VALUE> at the end of the string list |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 728 | */ |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 729 | static int parse_key_value_squoted(char *buf, struct string_list *list) |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 730 | { |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 731 | while (*buf) { |
| 732 | struct string_list_item *item; |
| 733 | char *np; |
| 734 | char *cp = strchr(buf, '='); |
| 735 | if (!cp) { |
| 736 | np = strchrnul(buf, '\n'); |
| 737 | return error(_("no key present in '%.*s'"), |
| 738 | (int) (np - buf), buf); |
| 739 | } |
| 740 | np = strchrnul(cp, '\n'); |
| 741 | *cp++ = '\0'; |
| 742 | item = string_list_append(list, buf); |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 743 | |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 744 | buf = np + (*np == '\n'); |
| 745 | *np = '\0'; |
| 746 | cp = sq_dequote(cp); |
| 747 | if (!cp) |
| 748 | return error(_("unable to dequote value of '%s'"), |
| 749 | item->string); |
| 750 | item->util = xstrdup(cp); |
| 751 | } |
Phillip Wood | 4aa5ff9 | 2018-08-07 10:34:52 +0100 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 755 | /** |
| 756 | * Reads and parses the state directory's "author-script" file, and sets name, |
| 757 | * email and date accordingly. |
| 758 | * Returns 0 on success, -1 if the file could not be parsed. |
| 759 | * |
| 760 | * The author script is of the format: |
| 761 | * |
| 762 | * GIT_AUTHOR_NAME='$author_name' |
| 763 | * GIT_AUTHOR_EMAIL='$author_email' |
| 764 | * GIT_AUTHOR_DATE='$author_date' |
| 765 | * |
| 766 | * where $author_name, $author_email and $author_date are quoted. We are strict |
Johannes Schindelin | c3c003e | 2019-05-14 04:22:33 -0700 | [diff] [blame] | 767 | * with our parsing, as the file was meant to be eval'd in the now-removed |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 768 | * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs |
| 769 | * from what this function expects, it is better to bail out than to do |
| 770 | * something that the user does not expect. |
| 771 | */ |
| 772 | int read_author_script(const char *path, char **name, char **email, char **date, |
| 773 | int allow_missing) |
| 774 | { |
| 775 | struct strbuf buf = STRBUF_INIT; |
| 776 | struct string_list kv = STRING_LIST_INIT_DUP; |
| 777 | int retval = -1; /* assume failure */ |
| 778 | int i, name_i = -2, email_i = -2, date_i = -2, err = 0; |
| 779 | |
| 780 | if (strbuf_read_file(&buf, path, 256) <= 0) { |
| 781 | strbuf_release(&buf); |
| 782 | if (errno == ENOENT && allow_missing) |
| 783 | return 0; |
| 784 | else |
| 785 | return error_errno(_("could not open '%s' for reading"), |
| 786 | path); |
| 787 | } |
| 788 | |
| 789 | if (parse_key_value_squoted(buf.buf, &kv)) |
| 790 | goto finish; |
| 791 | |
| 792 | for (i = 0; i < kv.nr; i++) { |
| 793 | if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) { |
| 794 | if (name_i != -2) |
| 795 | name_i = error(_("'GIT_AUTHOR_NAME' already given")); |
| 796 | else |
| 797 | name_i = i; |
| 798 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) { |
| 799 | if (email_i != -2) |
| 800 | email_i = error(_("'GIT_AUTHOR_EMAIL' already given")); |
| 801 | else |
| 802 | email_i = i; |
| 803 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) { |
| 804 | if (date_i != -2) |
| 805 | date_i = error(_("'GIT_AUTHOR_DATE' already given")); |
| 806 | else |
| 807 | date_i = i; |
| 808 | } else { |
| 809 | err = error(_("unknown variable '%s'"), |
| 810 | kv.items[i].string); |
| 811 | } |
| 812 | } |
| 813 | if (name_i == -2) |
| 814 | error(_("missing 'GIT_AUTHOR_NAME'")); |
| 815 | if (email_i == -2) |
| 816 | error(_("missing 'GIT_AUTHOR_EMAIL'")); |
| 817 | if (date_i == -2) |
| 818 | error(_("missing 'GIT_AUTHOR_DATE'")); |
| 819 | if (date_i < 0 || email_i < 0 || date_i < 0 || err) |
| 820 | goto finish; |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 821 | *name = kv.items[name_i].util; |
| 822 | *email = kv.items[email_i].util; |
| 823 | *date = kv.items[date_i].util; |
Phillip Wood | bcd33ec | 2018-10-31 10:15:55 +0000 | [diff] [blame] | 824 | retval = 0; |
| 825 | finish: |
| 826 | string_list_clear(&kv, !!retval); |
| 827 | strbuf_release(&buf); |
| 828 | return retval; |
| 829 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 830 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 831 | /* |
Phillip Wood | 4d010a7 | 2018-10-31 10:15:56 +0000 | [diff] [blame] | 832 | * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a |
| 833 | * file with shell quoting into struct argv_array. Returns -1 on |
| 834 | * error, 0 otherwise. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 835 | */ |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 836 | static int read_env_script(struct argv_array *env) |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 837 | { |
Phillip Wood | 4d010a7 | 2018-10-31 10:15:56 +0000 | [diff] [blame] | 838 | char *name, *email, *date; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 839 | |
Phillip Wood | 4d010a7 | 2018-10-31 10:15:56 +0000 | [diff] [blame] | 840 | if (read_author_script(rebase_path_author_script(), |
| 841 | &name, &email, &date, 0)) |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 842 | return -1; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 843 | |
Phillip Wood | 4d010a7 | 2018-10-31 10:15:56 +0000 | [diff] [blame] | 844 | argv_array_pushf(env, "GIT_AUTHOR_NAME=%s", name); |
| 845 | argv_array_pushf(env, "GIT_AUTHOR_EMAIL=%s", email); |
| 846 | argv_array_pushf(env, "GIT_AUTHOR_DATE=%s", date); |
| 847 | free(name); |
| 848 | free(email); |
| 849 | free(date); |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 850 | |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 851 | return 0; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 854 | static char *get_author(const char *message) |
| 855 | { |
| 856 | size_t len; |
| 857 | const char *a; |
| 858 | |
| 859 | a = find_commit_header(message, "author", &len); |
| 860 | if (a) |
| 861 | return xmemdupz(a, len); |
| 862 | |
| 863 | return NULL; |
| 864 | } |
| 865 | |
Johannes Schindelin | 791eb87 | 2016-10-21 14:26:32 +0200 | [diff] [blame] | 866 | static const char staged_changes_advice[] = |
| 867 | N_("you have staged changes in your working tree\n" |
| 868 | "If these changes are meant to be squashed into the previous commit, run:\n" |
| 869 | "\n" |
| 870 | " git commit --amend %s\n" |
| 871 | "\n" |
| 872 | "If they are meant to go into a new commit, run:\n" |
| 873 | "\n" |
| 874 | " git commit %s\n" |
| 875 | "\n" |
| 876 | "In both cases, once you're done, continue with:\n" |
| 877 | "\n" |
| 878 | " git rebase --continue\n"); |
| 879 | |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 880 | #define ALLOW_EMPTY (1<<0) |
| 881 | #define EDIT_MSG (1<<1) |
| 882 | #define AMEND_MSG (1<<2) |
| 883 | #define CLEANUP_MSG (1<<3) |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 884 | #define VERIFY_MSG (1<<4) |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 885 | #define CREATE_ROOT_COMMIT (1<<5) |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 886 | |
Alban Gruin | 34bec2c | 2018-08-10 18:51:32 +0200 | [diff] [blame] | 887 | static int run_command_silent_on_success(struct child_process *cmd) |
| 888 | { |
| 889 | struct strbuf buf = STRBUF_INIT; |
| 890 | int rc; |
| 891 | |
| 892 | cmd->stdout_to_stderr = 1; |
| 893 | rc = pipe_command(cmd, |
| 894 | NULL, 0, |
| 895 | NULL, 0, |
| 896 | &buf, 0); |
| 897 | |
| 898 | if (rc) |
| 899 | fputs(buf.buf, stderr); |
| 900 | strbuf_release(&buf); |
| 901 | return rc; |
| 902 | } |
| 903 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 904 | /* |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 905 | * If we are cherry-pick, and if the merge did not result in |
| 906 | * hand-editing, we will hit this commit and inherit the original |
| 907 | * author date and name. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 908 | * |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 909 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 910 | * we had better say that the current user is responsible for that. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 911 | * |
| 912 | * An exception is when run_git_commit() is called during an |
| 913 | * interactive rebase: in that case, we will want to retain the |
| 914 | * author metadata. |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 915 | */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 916 | static int run_git_commit(struct repository *r, |
| 917 | const char *defmsg, |
| 918 | struct replay_opts *opts, |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 919 | unsigned int flags) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 920 | { |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 921 | struct child_process cmd = CHILD_PROCESS_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 922 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 923 | cmd.git_cmd = 1; |
| 924 | |
Alban Gruin | 34bec2c | 2018-08-10 18:51:32 +0200 | [diff] [blame] | 925 | if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) { |
| 926 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
Johannes Schindelin | 9a757c4 | 2017-01-02 16:35:34 +0100 | [diff] [blame] | 927 | |
Alban Gruin | 34bec2c | 2018-08-10 18:51:32 +0200 | [diff] [blame] | 928 | return error(_(staged_changes_advice), |
| 929 | gpg_opt, gpg_opt); |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 932 | argv_array_push(&cmd.args, "commit"); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 933 | |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 934 | if (!(flags & VERIFY_MSG)) |
| 935 | argv_array_push(&cmd.args, "-n"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 936 | if ((flags & AMEND_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 937 | argv_array_push(&cmd.args, "--amend"); |
Jeff King | 3bdd552 | 2014-06-19 17:28:20 -0400 | [diff] [blame] | 938 | if (opts->gpg_sign) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 939 | argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
Đoàn Trần Công Danh | cf0ad4d | 2020-04-03 17:28:03 +0700 | [diff] [blame] | 940 | else |
| 941 | argv_array_push(&cmd.args, "--no-gpg-sign"); |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 942 | if (defmsg) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 943 | argv_array_pushl(&cmd.args, "-F", defmsg, NULL); |
Johannes Schindelin | dc4b5bc | 2018-04-27 22:48:28 +0200 | [diff] [blame] | 944 | else if (!(flags & EDIT_MSG)) |
| 945 | argv_array_pushl(&cmd.args, "-C", "HEAD", NULL); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 946 | if ((flags & CLEANUP_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 947 | argv_array_push(&cmd.args, "--cleanup=strip"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 948 | if ((flags & EDIT_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 949 | argv_array_push(&cmd.args, "-e"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 950 | else if (!(flags & CLEANUP_MSG) && |
Johannes Schindelin | 0009426 | 2016-10-21 14:25:28 +0200 | [diff] [blame] | 951 | !opts->signoff && !opts->record_origin && |
Denton Liu | 1a2b985 | 2019-04-17 11:23:30 +0100 | [diff] [blame] | 952 | !opts->explicit_cleanup) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 953 | argv_array_push(&cmd.args, "--cleanup=verbatim"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 954 | |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 955 | if ((flags & ALLOW_EMPTY)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 956 | argv_array_push(&cmd.args, "--allow-empty"); |
Neil Horman | df478b7 | 2012-04-11 16:21:53 -0400 | [diff] [blame] | 957 | |
Elijah Newren | a3ec9ea | 2018-09-12 14:18:48 -0700 | [diff] [blame] | 958 | if (!(flags & EDIT_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 959 | argv_array_push(&cmd.args, "--allow-empty-message"); |
Chris Webb | 4bee958 | 2012-08-02 11:38:51 +0100 | [diff] [blame] | 960 | |
Alban Gruin | 34bec2c | 2018-08-10 18:51:32 +0200 | [diff] [blame] | 961 | if (is_rebase_i(opts) && !(flags & EDIT_MSG)) |
| 962 | return run_command_silent_on_success(&cmd); |
| 963 | else |
| 964 | return run_command(&cmd); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 965 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 966 | |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 967 | static int rest_is_empty(const struct strbuf *sb, int start) |
| 968 | { |
| 969 | int i, eol; |
| 970 | const char *nl; |
| 971 | |
| 972 | /* Check if the rest is just whitespace and Signed-off-by's. */ |
| 973 | for (i = start; i < sb->len; i++) { |
| 974 | nl = memchr(sb->buf + i, '\n', sb->len - i); |
| 975 | if (nl) |
| 976 | eol = nl - sb->buf; |
| 977 | else |
| 978 | eol = sb->len; |
| 979 | |
| 980 | if (strlen(sign_off_header) <= eol - i && |
| 981 | starts_with(sb->buf + i, sign_off_header)) { |
| 982 | i = eol; |
| 983 | continue; |
| 984 | } |
| 985 | while (i < eol) |
| 986 | if (!isspace(sb->buf[i++])) |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | return 1; |
| 991 | } |
| 992 | |
Denton Liu | f29cd86 | 2019-04-17 11:23:25 +0100 | [diff] [blame] | 993 | void cleanup_message(struct strbuf *msgbuf, |
| 994 | enum commit_msg_cleanup_mode cleanup_mode, int verbose) |
| 995 | { |
| 996 | if (verbose || /* Truncate the message just before the diff, if any. */ |
| 997 | cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
| 998 | strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); |
| 999 | if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) |
| 1000 | strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); |
| 1001 | } |
| 1002 | |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 1003 | /* |
| 1004 | * Find out if the message in the strbuf contains only whitespace and |
| 1005 | * Signed-off-by lines. |
| 1006 | */ |
| 1007 | int message_is_empty(const struct strbuf *sb, |
| 1008 | enum commit_msg_cleanup_mode cleanup_mode) |
| 1009 | { |
| 1010 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
| 1011 | return 0; |
| 1012 | return rest_is_empty(sb, 0); |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * See if the user edited the message in the editor or left what |
| 1017 | * was in the template intact |
| 1018 | */ |
| 1019 | int template_untouched(const struct strbuf *sb, const char *template_file, |
| 1020 | enum commit_msg_cleanup_mode cleanup_mode) |
| 1021 | { |
| 1022 | struct strbuf tmpl = STRBUF_INIT; |
| 1023 | const char *start; |
| 1024 | |
| 1025 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
| 1026 | return 0; |
| 1027 | |
| 1028 | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) |
| 1029 | return 0; |
| 1030 | |
| 1031 | strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); |
| 1032 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) |
| 1033 | start = sb->buf; |
| 1034 | strbuf_release(&tmpl); |
| 1035 | return rest_is_empty(sb, start - sb->buf); |
| 1036 | } |
| 1037 | |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 1038 | int update_head_with_reflog(const struct commit *old_head, |
| 1039 | const struct object_id *new_head, |
| 1040 | const char *action, const struct strbuf *msg, |
| 1041 | struct strbuf *err) |
| 1042 | { |
| 1043 | struct ref_transaction *transaction; |
| 1044 | struct strbuf sb = STRBUF_INIT; |
| 1045 | const char *nl; |
| 1046 | int ret = 0; |
| 1047 | |
| 1048 | if (action) { |
| 1049 | strbuf_addstr(&sb, action); |
| 1050 | strbuf_addstr(&sb, ": "); |
| 1051 | } |
| 1052 | |
| 1053 | nl = strchr(msg->buf, '\n'); |
| 1054 | if (nl) { |
| 1055 | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); |
| 1056 | } else { |
| 1057 | strbuf_addbuf(&sb, msg); |
| 1058 | strbuf_addch(&sb, '\n'); |
| 1059 | } |
| 1060 | |
| 1061 | transaction = ref_transaction_begin(err); |
| 1062 | if (!transaction || |
| 1063 | ref_transaction_update(transaction, "HEAD", new_head, |
| 1064 | old_head ? &old_head->object.oid : &null_oid, |
| 1065 | 0, sb.buf, err) || |
| 1066 | ref_transaction_commit(transaction, err)) { |
| 1067 | ret = -1; |
| 1068 | } |
| 1069 | ref_transaction_free(transaction); |
| 1070 | strbuf_release(&sb); |
| 1071 | |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 1075 | static int run_rewrite_hook(const struct object_id *oldoid, |
| 1076 | const struct object_id *newoid) |
| 1077 | { |
| 1078 | struct child_process proc = CHILD_PROCESS_INIT; |
| 1079 | const char *argv[3]; |
| 1080 | int code; |
| 1081 | struct strbuf sb = STRBUF_INIT; |
| 1082 | |
| 1083 | argv[0] = find_hook("post-rewrite"); |
| 1084 | if (!argv[0]) |
| 1085 | return 0; |
| 1086 | |
| 1087 | argv[1] = "amend"; |
| 1088 | argv[2] = NULL; |
| 1089 | |
| 1090 | proc.argv = argv; |
| 1091 | proc.in = -1; |
| 1092 | proc.stdout_to_stderr = 1; |
Jeff Hostetler | 6206286 | 2019-02-22 14:25:06 -0800 | [diff] [blame] | 1093 | proc.trace2_hook_name = "post-rewrite"; |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 1094 | |
| 1095 | code = start_command(&proc); |
| 1096 | if (code) |
| 1097 | return code; |
| 1098 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); |
| 1099 | sigchain_push(SIGPIPE, SIG_IGN); |
| 1100 | write_in_full(proc.in, sb.buf, sb.len); |
| 1101 | close(proc.in); |
| 1102 | strbuf_release(&sb); |
| 1103 | sigchain_pop(SIGPIPE); |
| 1104 | return finish_command(&proc); |
| 1105 | } |
| 1106 | |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 1107 | void commit_post_rewrite(struct repository *r, |
| 1108 | const struct commit *old_head, |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 1109 | const struct object_id *new_head) |
| 1110 | { |
| 1111 | struct notes_rewrite_cfg *cfg; |
| 1112 | |
| 1113 | cfg = init_copy_notes_for_rewrite("amend"); |
| 1114 | if (cfg) { |
| 1115 | /* we are amending, so old_head is not NULL */ |
| 1116 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 1117 | finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'"); |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 1118 | } |
| 1119 | run_rewrite_hook(&old_head->object.oid, new_head); |
| 1120 | } |
| 1121 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1122 | static int run_prepare_commit_msg_hook(struct repository *r, |
| 1123 | struct strbuf *msg, |
| 1124 | const char *commit) |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1125 | { |
Phillip Wood | 49697cb | 2019-10-15 10:25:31 +0000 | [diff] [blame] | 1126 | int ret = 0; |
| 1127 | const char *name, *arg1 = NULL, *arg2 = NULL; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1128 | |
| 1129 | name = git_path_commit_editmsg(); |
| 1130 | if (write_message(msg->buf, msg->len, name, 0)) |
| 1131 | return -1; |
| 1132 | |
Phillip Wood | 49697cb | 2019-10-15 10:25:31 +0000 | [diff] [blame] | 1133 | if (commit) { |
| 1134 | arg1 = "commit"; |
| 1135 | arg2 = commit; |
| 1136 | } else { |
| 1137 | arg1 = "message"; |
| 1138 | } |
| 1139 | if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name, |
| 1140 | arg1, arg2, NULL)) |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1141 | ret = error(_("'prepare-commit-msg' hook failed")); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1142 | |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1146 | static const char implicit_ident_advice_noconfig[] = |
| 1147 | N_("Your name and email address were configured automatically based\n" |
| 1148 | "on your username and hostname. Please check that they are accurate.\n" |
| 1149 | "You can suppress this message by setting them explicitly. Run the\n" |
| 1150 | "following command and follow the instructions in your editor to edit\n" |
| 1151 | "your configuration file:\n" |
| 1152 | "\n" |
| 1153 | " git config --global --edit\n" |
| 1154 | "\n" |
| 1155 | "After doing this, you may fix the identity used for this commit with:\n" |
| 1156 | "\n" |
| 1157 | " git commit --amend --reset-author\n"); |
| 1158 | |
| 1159 | static const char implicit_ident_advice_config[] = |
| 1160 | N_("Your name and email address were configured automatically based\n" |
| 1161 | "on your username and hostname. Please check that they are accurate.\n" |
| 1162 | "You can suppress this message by setting them explicitly:\n" |
| 1163 | "\n" |
| 1164 | " git config --global user.name \"Your Name\"\n" |
| 1165 | " git config --global user.email you@example.com\n" |
| 1166 | "\n" |
| 1167 | "After doing this, you may fix the identity used for this commit with:\n" |
| 1168 | "\n" |
| 1169 | " git commit --amend --reset-author\n"); |
| 1170 | |
| 1171 | static const char *implicit_ident_advice(void) |
| 1172 | { |
| 1173 | char *user_config = expand_user_path("~/.gitconfig", 0); |
| 1174 | char *xdg_config = xdg_config_home("config"); |
| 1175 | int config_exists = file_exists(user_config) || file_exists(xdg_config); |
| 1176 | |
| 1177 | free(user_config); |
| 1178 | free(xdg_config); |
| 1179 | |
| 1180 | if (config_exists) |
| 1181 | return _(implicit_ident_advice_config); |
| 1182 | else |
| 1183 | return _(implicit_ident_advice_noconfig); |
| 1184 | |
| 1185 | } |
| 1186 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1187 | void print_commit_summary(struct repository *r, |
| 1188 | const char *prefix, |
| 1189 | const struct object_id *oid, |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1190 | unsigned int flags) |
| 1191 | { |
| 1192 | struct rev_info rev; |
| 1193 | struct commit *commit; |
| 1194 | struct strbuf format = STRBUF_INIT; |
| 1195 | const char *head; |
| 1196 | struct pretty_print_context pctx = {0}; |
| 1197 | struct strbuf author_ident = STRBUF_INIT; |
| 1198 | struct strbuf committer_ident = STRBUF_INIT; |
| 1199 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1200 | commit = lookup_commit(r, oid); |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1201 | if (!commit) |
| 1202 | die(_("couldn't look up newly created commit")); |
| 1203 | if (parse_commit(commit)) |
| 1204 | die(_("could not parse newly created commit")); |
| 1205 | |
| 1206 | strbuf_addstr(&format, "format:%h] %s"); |
| 1207 | |
| 1208 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); |
| 1209 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); |
| 1210 | if (strbuf_cmp(&author_ident, &committer_ident)) { |
| 1211 | strbuf_addstr(&format, "\n Author: "); |
| 1212 | strbuf_addbuf_percentquote(&format, &author_ident); |
| 1213 | } |
| 1214 | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { |
| 1215 | struct strbuf date = STRBUF_INIT; |
| 1216 | |
| 1217 | format_commit_message(commit, "%ad", &date, &pctx); |
| 1218 | strbuf_addstr(&format, "\n Date: "); |
| 1219 | strbuf_addbuf_percentquote(&format, &date); |
| 1220 | strbuf_release(&date); |
| 1221 | } |
| 1222 | if (!committer_ident_sufficiently_given()) { |
| 1223 | strbuf_addstr(&format, "\n Committer: "); |
| 1224 | strbuf_addbuf_percentquote(&format, &committer_ident); |
| 1225 | if (advice_implicit_identity) { |
| 1226 | strbuf_addch(&format, '\n'); |
| 1227 | strbuf_addstr(&format, implicit_ident_advice()); |
| 1228 | } |
| 1229 | } |
| 1230 | strbuf_release(&author_ident); |
| 1231 | strbuf_release(&committer_ident); |
| 1232 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1233 | repo_init_revisions(r, &rev, prefix); |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1234 | setup_revisions(0, NULL, &rev, NULL); |
| 1235 | |
| 1236 | rev.diff = 1; |
| 1237 | rev.diffopt.output_format = |
| 1238 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; |
| 1239 | |
| 1240 | rev.verbose_header = 1; |
| 1241 | rev.show_root_diff = 1; |
| 1242 | get_commit_format(format.buf, &rev); |
| 1243 | rev.always_show_header = 0; |
Junio C Hamano | 0f57f73 | 2018-02-13 13:39:15 -0800 | [diff] [blame] | 1244 | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1245 | rev.diffopt.break_opt = 0; |
| 1246 | diff_setup_done(&rev.diffopt); |
| 1247 | |
| 1248 | head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); |
| 1249 | if (!head) |
| 1250 | die_errno(_("unable to resolve HEAD after creating commit")); |
| 1251 | if (!strcmp(head, "HEAD")) |
| 1252 | head = _("detached HEAD"); |
| 1253 | else |
| 1254 | skip_prefix(head, "refs/heads/", &head); |
| 1255 | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? |
| 1256 | _(" (root-commit)") : ""); |
| 1257 | |
| 1258 | if (!log_tree_commit(&rev, commit)) { |
| 1259 | rev.always_show_header = 1; |
| 1260 | rev.use_terminator = 1; |
| 1261 | log_tree_commit(&rev, commit); |
| 1262 | } |
| 1263 | |
| 1264 | strbuf_release(&format); |
| 1265 | } |
| 1266 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1267 | static int parse_head(struct repository *r, struct commit **head) |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1268 | { |
| 1269 | struct commit *current_head; |
| 1270 | struct object_id oid; |
| 1271 | |
| 1272 | if (get_oid("HEAD", &oid)) { |
| 1273 | current_head = NULL; |
| 1274 | } else { |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1275 | current_head = lookup_commit_reference(r, &oid); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1276 | if (!current_head) |
| 1277 | return error(_("could not parse HEAD")); |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 1278 | if (!oideq(&oid, ¤t_head->object.oid)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1279 | warning(_("HEAD %s is not a commit!"), |
| 1280 | oid_to_hex(&oid)); |
| 1281 | } |
| 1282 | if (parse_commit(current_head)) |
| 1283 | return error(_("could not parse HEAD commit")); |
| 1284 | } |
| 1285 | *head = current_head; |
| 1286 | |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * Try to commit without forking 'git commit'. In some cases we need |
| 1292 | * to run 'git commit' to display an error message |
| 1293 | * |
| 1294 | * Returns: |
| 1295 | * -1 - error unable to commit |
| 1296 | * 0 - success |
| 1297 | * 1 - run 'git commit' |
| 1298 | */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1299 | static int try_to_commit(struct repository *r, |
| 1300 | struct strbuf *msg, const char *author, |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1301 | struct replay_opts *opts, unsigned int flags, |
| 1302 | struct object_id *oid) |
| 1303 | { |
| 1304 | struct object_id tree; |
Phillip Wood | b0a3186 | 2019-08-19 02:18:23 -0700 | [diff] [blame] | 1305 | struct commit *current_head = NULL; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1306 | struct commit_list *parents = NULL; |
| 1307 | struct commit_extra_header *extra = NULL; |
| 1308 | struct strbuf err = STRBUF_INIT; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1309 | struct strbuf commit_msg = STRBUF_INIT; |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 1310 | char *amend_author = NULL; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1311 | const char *hook_commit = NULL; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1312 | enum commit_msg_cleanup_mode cleanup; |
| 1313 | int res = 0; |
| 1314 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1315 | if (parse_head(r, ¤t_head)) |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1316 | return -1; |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 1317 | |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1318 | if (flags & AMEND_MSG) { |
brian m. carlson | 42d4e1d | 2020-02-22 20:17:42 +0000 | [diff] [blame] | 1319 | const char *exclude_gpgsig[] = { "gpgsig", "gpgsig-sha256", NULL }; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1320 | const char *out_enc = get_commit_output_encoding(); |
| 1321 | const char *message = logmsg_reencode(current_head, NULL, |
| 1322 | out_enc); |
| 1323 | |
| 1324 | if (!msg) { |
| 1325 | const char *orig_message = NULL; |
| 1326 | |
| 1327 | find_commit_subject(message, &orig_message); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1328 | msg = &commit_msg; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1329 | strbuf_addstr(msg, orig_message); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1330 | hook_commit = "HEAD"; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1331 | } |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 1332 | author = amend_author = get_author(message); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1333 | unuse_commit_buffer(current_head, message); |
| 1334 | if (!author) { |
| 1335 | res = error(_("unable to parse commit author")); |
| 1336 | goto out; |
| 1337 | } |
| 1338 | parents = copy_commit_list(current_head->parents); |
| 1339 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
Phillip Wood | b0a3186 | 2019-08-19 02:18:23 -0700 | [diff] [blame] | 1340 | } else if (current_head && |
| 1341 | (!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1342 | commit_list_insert(current_head, &parents); |
| 1343 | } |
| 1344 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1345 | if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1346 | res = error(_("git write-tree failed to write a tree")); |
| 1347 | goto out; |
| 1348 | } |
| 1349 | |
Phillip Wood | 2d05ef2 | 2019-11-22 19:43:03 +0000 | [diff] [blame] | 1350 | if (!(flags & ALLOW_EMPTY)) { |
| 1351 | struct commit *first_parent = current_head; |
| 1352 | |
| 1353 | if (flags & AMEND_MSG) { |
| 1354 | if (current_head->parents) { |
| 1355 | first_parent = current_head->parents->item; |
| 1356 | if (repo_parse_commit(r, first_parent)) { |
| 1357 | res = error(_("could not parse HEAD commit")); |
| 1358 | goto out; |
| 1359 | } |
| 1360 | } else { |
| 1361 | first_parent = NULL; |
| 1362 | } |
| 1363 | } |
| 1364 | if (oideq(first_parent |
| 1365 | ? get_commit_tree_oid(first_parent) |
| 1366 | : the_hash_algo->empty_tree, |
| 1367 | &tree)) { |
| 1368 | res = 1; /* run 'git commit' to display error message */ |
| 1369 | goto out; |
| 1370 | } |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1373 | if (find_hook("prepare-commit-msg")) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1374 | res = run_prepare_commit_msg_hook(r, msg, hook_commit); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1375 | if (res) |
| 1376 | goto out; |
| 1377 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), |
| 1378 | 2048) < 0) { |
| 1379 | res = error_errno(_("unable to read commit message " |
| 1380 | "from '%s'"), |
| 1381 | git_path_commit_editmsg()); |
| 1382 | goto out; |
| 1383 | } |
| 1384 | msg = &commit_msg; |
| 1385 | } |
| 1386 | |
Phillip Wood | d74f3e5 | 2019-03-29 11:08:42 +0000 | [diff] [blame] | 1387 | if (flags & CLEANUP_MSG) |
| 1388 | cleanup = COMMIT_MSG_CLEANUP_ALL; |
| 1389 | else if ((opts->signoff || opts->record_origin) && |
| 1390 | !opts->explicit_cleanup) |
| 1391 | cleanup = COMMIT_MSG_CLEANUP_SPACE; |
| 1392 | else |
| 1393 | cleanup = opts->default_msg_cleanup; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) |
| 1396 | strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); |
Elijah Newren | a3ec9ea | 2018-09-12 14:18:48 -0700 | [diff] [blame] | 1397 | if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1398 | res = 1; /* run 'git commit' to display error message */ |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
Johannes Sixt | 12f7bab | 2018-04-18 20:15:04 +0200 | [diff] [blame] | 1402 | reset_ident_date(); |
| 1403 | |
Junio C Hamano | 8be8342 | 2018-02-15 14:55:43 -0800 | [diff] [blame] | 1404 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, |
| 1405 | oid, author, opts->gpg_sign, extra)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1406 | res = error(_("failed to write commit object")); |
| 1407 | goto out; |
| 1408 | } |
| 1409 | |
| 1410 | if (update_head_with_reflog(current_head, oid, |
| 1411 | getenv("GIT_REFLOG_ACTION"), msg, &err)) { |
| 1412 | res = error("%s", err.buf); |
| 1413 | goto out; |
| 1414 | } |
| 1415 | |
Phillip Wood | 4627bc7 | 2019-10-15 10:25:32 +0000 | [diff] [blame] | 1416 | run_commit_hook(0, r->index_file, "post-commit", NULL); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1417 | if (flags & AMEND_MSG) |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 1418 | commit_post_rewrite(r, current_head, oid); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1419 | |
| 1420 | out: |
| 1421 | free_commit_extra_headers(extra); |
| 1422 | strbuf_release(&err); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1423 | strbuf_release(&commit_msg); |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 1424 | free(amend_author); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1425 | |
| 1426 | return res; |
| 1427 | } |
| 1428 | |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1429 | static int write_rebase_head(struct object_id *oid) |
| 1430 | { |
| 1431 | if (update_ref("rebase", "REBASE_HEAD", oid, |
| 1432 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
| 1433 | return error(_("could not update %s"), "REBASE_HEAD"); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1438 | static int do_commit(struct repository *r, |
| 1439 | const char *msg_file, const char *author, |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1440 | struct replay_opts *opts, unsigned int flags, |
| 1441 | struct object_id *oid) |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1442 | { |
| 1443 | int res = 1; |
| 1444 | |
Phillip Wood | b0a3186 | 2019-08-19 02:18:23 -0700 | [diff] [blame] | 1445 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1446 | struct object_id oid; |
| 1447 | struct strbuf sb = STRBUF_INIT; |
| 1448 | |
| 1449 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) |
| 1450 | return error_errno(_("unable to read commit message " |
| 1451 | "from '%s'"), |
| 1452 | msg_file); |
| 1453 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1454 | res = try_to_commit(r, msg_file ? &sb : NULL, |
| 1455 | author, opts, flags, &oid); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1456 | strbuf_release(&sb); |
| 1457 | if (!res) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1458 | unlink(git_path_cherry_pick_head(r)); |
| 1459 | unlink(git_path_merge_msg(r)); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1460 | if (!is_rebase_i(opts)) |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1461 | print_commit_summary(r, NULL, &oid, |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1462 | SUMMARY_SHOW_AUTHOR_DATE); |
| 1463 | return res; |
| 1464 | } |
| 1465 | } |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1466 | if (res == 1) { |
| 1467 | if (is_rebase_i(opts) && oid) |
| 1468 | if (write_rebase_head(oid)) |
| 1469 | return -1; |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1470 | return run_git_commit(r, msg_file, opts, flags); |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1471 | } |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1472 | |
| 1473 | return res; |
| 1474 | } |
| 1475 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1476 | static int is_original_commit_empty(struct commit *commit) |
| 1477 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1478 | const struct object_id *ptree_oid; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1479 | |
| 1480 | if (parse_commit(commit)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 1481 | return error(_("could not parse commit %s"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1482 | oid_to_hex(&commit->object.oid)); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1483 | if (commit->parents) { |
| 1484 | struct commit *parent = commit->parents->item; |
| 1485 | if (parse_commit(parent)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 1486 | return error(_("could not parse parent commit %s"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1487 | oid_to_hex(&parent->object.oid)); |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1488 | ptree_oid = get_commit_tree_oid(parent); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1489 | } else { |
brian m. carlson | eb0ccfd | 2017-11-12 21:28:54 +0000 | [diff] [blame] | 1490 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 1493 | return oideq(ptree_oid, get_commit_tree_oid(commit)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1494 | } |
| 1495 | |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1496 | /* |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1497 | * Should empty commits be allowed? Return status: |
| 1498 | * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit) |
| 1499 | * 0: Halt on empty commit |
| 1500 | * 1: Allow empty commit |
| 1501 | * 2: Drop empty commit |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1502 | */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1503 | static int allow_empty(struct repository *r, |
| 1504 | struct replay_opts *opts, |
| 1505 | struct commit *commit) |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1506 | { |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 1507 | int index_unchanged, originally_empty; |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1508 | |
| 1509 | /* |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1510 | * Four cases: |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1511 | * |
| 1512 | * (1) we do not allow empty at all and error out. |
| 1513 | * |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1514 | * (2) we allow ones that were initially empty, and |
| 1515 | * just drop the ones that become empty |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1516 | * |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1517 | * (3) we allow ones that were initially empty, but |
| 1518 | * halt for the ones that become empty; |
| 1519 | * |
| 1520 | * (4) we allow both. |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1521 | */ |
| 1522 | if (!opts->allow_empty) |
| 1523 | return 0; /* let "git commit" barf as necessary */ |
| 1524 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1525 | index_unchanged = is_index_unchanged(r); |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1526 | if (index_unchanged < 0) |
| 1527 | return index_unchanged; |
| 1528 | if (!index_unchanged) |
| 1529 | return 0; /* we do not have to say --allow-empty */ |
| 1530 | |
| 1531 | if (opts->keep_redundant_commits) |
| 1532 | return 1; |
| 1533 | |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 1534 | originally_empty = is_original_commit_empty(commit); |
| 1535 | if (originally_empty < 0) |
| 1536 | return originally_empty; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1537 | if (originally_empty) |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1538 | return 1; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1539 | else if (opts->drop_redundant_commits) |
| 1540 | return 2; |
| 1541 | else |
| 1542 | return 0; |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1545 | static struct { |
| 1546 | char c; |
| 1547 | const char *str; |
| 1548 | } todo_command_info[] = { |
| 1549 | { 'p', "pick" }, |
| 1550 | { 0, "revert" }, |
| 1551 | { 'e', "edit" }, |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1552 | { 'r', "reword" }, |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1553 | { 'f', "fixup" }, |
| 1554 | { 's', "squash" }, |
| 1555 | { 'x', "exec" }, |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 1556 | { 'b', "break" }, |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 1557 | { 'l', "label" }, |
| 1558 | { 't', "reset" }, |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1559 | { 'm', "merge" }, |
Johannes Schindelin | b3fdd58 | 2017-01-02 16:34:34 +0100 | [diff] [blame] | 1560 | { 0, "noop" }, |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1561 | { 'd', "drop" }, |
| 1562 | { 0, NULL } |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1563 | }; |
| 1564 | |
| 1565 | static const char *command_to_string(const enum todo_command command) |
| 1566 | { |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1567 | if (command < TODO_COMMENT) |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1568 | return todo_command_info[command].str; |
Nguyễn Thái Ngọc Duy | 02127c6 | 2018-07-21 09:49:38 +0200 | [diff] [blame] | 1569 | die(_("unknown command: %d"), command); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Junio C Hamano | ee5462d | 2017-12-27 11:12:45 -0800 | [diff] [blame] | 1572 | static char command_to_char(const enum todo_command command) |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 1573 | { |
Alban Gruin | 68e7090 | 2020-03-30 14:42:35 +0200 | [diff] [blame] | 1574 | if (command < TODO_COMMENT) |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 1575 | return todo_command_info[command].c; |
| 1576 | return comment_line_char; |
| 1577 | } |
| 1578 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1579 | static int is_noop(const enum todo_command command) |
| 1580 | { |
Johannes Schindelin | b3fdd58 | 2017-01-02 16:34:34 +0100 | [diff] [blame] | 1581 | return TODO_NOOP <= command; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1582 | } |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1583 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1584 | static int is_fixup(enum todo_command command) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1585 | { |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1586 | return command == TODO_FIXUP || command == TODO_SQUASH; |
| 1587 | } |
| 1588 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1589 | /* Does this command create a (non-merge) commit? */ |
| 1590 | static int is_pick_or_similar(enum todo_command command) |
| 1591 | { |
| 1592 | switch (command) { |
| 1593 | case TODO_PICK: |
| 1594 | case TODO_REVERT: |
| 1595 | case TODO_EDIT: |
| 1596 | case TODO_REWORD: |
| 1597 | case TODO_FIXUP: |
| 1598 | case TODO_SQUASH: |
| 1599 | return 1; |
| 1600 | default: |
| 1601 | return 0; |
| 1602 | } |
| 1603 | } |
| 1604 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1605 | static int update_squash_messages(struct repository *r, |
| 1606 | enum todo_command command, |
| 1607 | struct commit *commit, |
| 1608 | struct replay_opts *opts) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1609 | { |
| 1610 | struct strbuf buf = STRBUF_INIT; |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1611 | int res; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1612 | const char *message, *body; |
Doan Tran Cong Danh | b375744 | 2019-11-08 16:43:48 +0700 | [diff] [blame] | 1613 | const char *encoding = get_commit_output_encoding(); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1614 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1615 | if (opts->current_fixup_count > 0) { |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1616 | struct strbuf header = STRBUF_INIT; |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1617 | char *eol; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1618 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1619 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1620 | return error(_("could not read '%s'"), |
| 1621 | rebase_path_squash_msg()); |
| 1622 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1623 | eol = buf.buf[0] != comment_line_char ? |
| 1624 | buf.buf : strchrnul(buf.buf, '\n'); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1625 | |
| 1626 | strbuf_addf(&header, "%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1627 | strbuf_addf(&header, _("This is a combination of %d commits."), |
| 1628 | opts->current_fixup_count + 2); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1629 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); |
| 1630 | strbuf_release(&header); |
| 1631 | } else { |
brian m. carlson | 33d66df | 2017-05-06 22:10:07 +0000 | [diff] [blame] | 1632 | struct object_id head; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1633 | struct commit *head_commit; |
| 1634 | const char *head_message, *body; |
| 1635 | |
brian m. carlson | 33d66df | 2017-05-06 22:10:07 +0000 | [diff] [blame] | 1636 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1637 | return error(_("need a HEAD to fixup")); |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1638 | if (!(head_commit = lookup_commit_reference(r, &head))) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1639 | return error(_("could not read HEAD")); |
Doan Tran Cong Danh | b375744 | 2019-11-08 16:43:48 +0700 | [diff] [blame] | 1640 | if (!(head_message = logmsg_reencode(head_commit, NULL, encoding))) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1641 | return error(_("could not read HEAD's commit message")); |
| 1642 | |
| 1643 | find_commit_subject(head_message, &body); |
| 1644 | if (write_message(body, strlen(body), |
| 1645 | rebase_path_fixup_msg(), 0)) { |
| 1646 | unuse_commit_buffer(head_commit, head_message); |
| 1647 | return error(_("cannot write '%s'"), |
| 1648 | rebase_path_fixup_msg()); |
| 1649 | } |
| 1650 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1651 | strbuf_addf(&buf, "%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1652 | strbuf_addf(&buf, _("This is a combination of %d commits."), 2); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1653 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
| 1654 | strbuf_addstr(&buf, _("This is the 1st commit message:")); |
| 1655 | strbuf_addstr(&buf, "\n\n"); |
| 1656 | strbuf_addstr(&buf, body); |
| 1657 | |
| 1658 | unuse_commit_buffer(head_commit, head_message); |
| 1659 | } |
| 1660 | |
Doan Tran Cong Danh | b375744 | 2019-11-08 16:43:48 +0700 | [diff] [blame] | 1661 | if (!(message = logmsg_reencode(commit, NULL, encoding))) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1662 | return error(_("could not read commit message of %s"), |
| 1663 | oid_to_hex(&commit->object.oid)); |
| 1664 | find_commit_subject(message, &body); |
| 1665 | |
| 1666 | if (command == TODO_SQUASH) { |
| 1667 | unlink(rebase_path_fixup_msg()); |
| 1668 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1669 | strbuf_addf(&buf, _("This is the commit message #%d:"), |
Phillip Wood | dd2e36e | 2018-08-15 10:41:25 +0100 | [diff] [blame] | 1670 | ++opts->current_fixup_count + 1); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1671 | strbuf_addstr(&buf, "\n\n"); |
| 1672 | strbuf_addstr(&buf, body); |
| 1673 | } else if (command == TODO_FIXUP) { |
| 1674 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
| 1675 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), |
Phillip Wood | dd2e36e | 2018-08-15 10:41:25 +0100 | [diff] [blame] | 1676 | ++opts->current_fixup_count + 1); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1677 | strbuf_addstr(&buf, "\n\n"); |
| 1678 | strbuf_add_commented_lines(&buf, body, strlen(body)); |
| 1679 | } else |
| 1680 | return error(_("unknown command: %d"), command); |
| 1681 | unuse_commit_buffer(commit, message); |
| 1682 | |
| 1683 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); |
| 1684 | strbuf_release(&buf); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1685 | |
| 1686 | if (!res) { |
| 1687 | strbuf_addf(&opts->current_fixups, "%s%s %s", |
| 1688 | opts->current_fixups.len ? "\n" : "", |
| 1689 | command_to_string(command), |
| 1690 | oid_to_hex(&commit->object.oid)); |
| 1691 | res = write_message(opts->current_fixups.buf, |
| 1692 | opts->current_fixups.len, |
| 1693 | rebase_path_current_fixups(), 0); |
| 1694 | } |
| 1695 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1696 | return res; |
| 1697 | } |
| 1698 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1699 | static void flush_rewritten_pending(void) |
| 1700 | { |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1701 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1702 | struct object_id newoid; |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1703 | FILE *out; |
| 1704 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1705 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
| 1706 | !get_oid("HEAD", &newoid) && |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 1707 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1708 | char *bol = buf.buf, *eol; |
| 1709 | |
| 1710 | while (*bol) { |
| 1711 | eol = strchrnul(bol, '\n'); |
| 1712 | fprintf(out, "%.*s %s\n", (int)(eol - bol), |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1713 | bol, oid_to_hex(&newoid)); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1714 | if (!*eol) |
| 1715 | break; |
| 1716 | bol = eol + 1; |
| 1717 | } |
| 1718 | fclose(out); |
| 1719 | unlink(rebase_path_rewritten_pending()); |
| 1720 | } |
| 1721 | strbuf_release(&buf); |
| 1722 | } |
| 1723 | |
| 1724 | static void record_in_rewritten(struct object_id *oid, |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1725 | enum todo_command next_command) |
| 1726 | { |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 1727 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1728 | |
| 1729 | if (!out) |
| 1730 | return; |
| 1731 | |
| 1732 | fprintf(out, "%s\n", oid_to_hex(oid)); |
| 1733 | fclose(out); |
| 1734 | |
| 1735 | if (!is_fixup(next_command)) |
| 1736 | flush_rewritten_pending(); |
| 1737 | } |
| 1738 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1739 | static int do_pick_commit(struct repository *r, |
| 1740 | enum todo_command command, |
| 1741 | struct commit *commit, |
| 1742 | struct replay_opts *opts, |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 1743 | int final_fixup, int *check_todo) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1744 | { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1745 | unsigned int flags = opts->edit ? EDIT_MSG : 0; |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1746 | const char *msg_file = opts->edit ? NULL : git_path_merge_msg(r); |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1747 | struct object_id head; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1748 | struct commit *base, *next, *parent; |
| 1749 | const char *base_label, *next_label; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1750 | char *author = NULL; |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 1751 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1752 | struct strbuf msgbuf = STRBUF_INIT; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1753 | int res, unborn = 0, reword = 0, allow, drop_commit; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1754 | |
| 1755 | if (opts->no_commit) { |
| 1756 | /* |
| 1757 | * We do not intend to commit immediately. We just want to |
| 1758 | * merge the differences in, so let's compute the tree |
| 1759 | * that represents the "current" state for merge-recursive |
| 1760 | * to work on. |
| 1761 | */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1762 | if (write_index_as_tree(&head, r->index, r->index_file, 0, NULL)) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1763 | return error(_("your index file is unmerged.")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1764 | } else { |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1765 | unborn = get_oid("HEAD", &head); |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1766 | /* Do we want to generate a root commit? */ |
| 1767 | if (is_pick_or_similar(command) && opts->have_squash_onto && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 1768 | oideq(&head, &opts->squash_onto)) { |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1769 | if (is_fixup(command)) |
| 1770 | return error(_("cannot fixup root commit")); |
| 1771 | flags |= CREATE_ROOT_COMMIT; |
| 1772 | unborn = 1; |
| 1773 | } else if (unborn) |
brian m. carlson | eb0ccfd | 2017-11-12 21:28:54 +0000 | [diff] [blame] | 1774 | oidcpy(&head, the_hash_algo->empty_tree); |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 1775 | if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD", |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 1776 | NULL, 0)) |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 1777 | return error_dirty_index(r, opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1778 | } |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1779 | discard_index(r->index); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1780 | |
Johannes Schindelin | 637666c | 2017-01-02 16:26:08 +0100 | [diff] [blame] | 1781 | if (!commit->parents) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1782 | parent = NULL; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1783 | else if (commit->parents->next) { |
| 1784 | /* Reverting or cherry-picking a merge commit */ |
| 1785 | int cnt; |
| 1786 | struct commit_list *p; |
| 1787 | |
| 1788 | if (!opts->mainline) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1789 | return error(_("commit %s is a merge but no -m option was given."), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1790 | oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1791 | |
| 1792 | for (cnt = 1, p = commit->parents; |
| 1793 | cnt != opts->mainline && p; |
| 1794 | cnt++) |
| 1795 | p = p->next; |
| 1796 | if (cnt != opts->mainline || !p) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1797 | return error(_("commit %s does not have parent %d"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1798 | oid_to_hex(&commit->object.oid), opts->mainline); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1799 | parent = p->item; |
Sergey Organov | 37897bf | 2018-12-14 07:53:51 +0300 | [diff] [blame] | 1800 | } else if (1 < opts->mainline) |
| 1801 | /* |
| 1802 | * Non-first parent explicitly specified as mainline for |
| 1803 | * non-merge commit |
| 1804 | */ |
| 1805 | return error(_("commit %s does not have parent %d"), |
| 1806 | oid_to_hex(&commit->object.oid), opts->mainline); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1807 | else |
| 1808 | parent = commit->parents->item; |
| 1809 | |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1810 | if (get_message(commit, &msg) != 0) |
| 1811 | return error(_("cannot get commit message for %s"), |
| 1812 | oid_to_hex(&commit->object.oid)); |
| 1813 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1814 | if (opts->allow_ff && !is_fixup(command) && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 1815 | ((parent && oideq(&parent->object.oid, &head)) || |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1816 | (!parent && unborn))) { |
| 1817 | if (is_rebase_i(opts)) |
| 1818 | write_author_script(msg.message); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1819 | res = fast_forward_to(r, &commit->object.oid, &head, unborn, |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1820 | opts); |
| 1821 | if (res || command != TODO_REWORD) |
| 1822 | goto leave; |
Phillip Wood | 450efe2 | 2019-08-19 02:18:21 -0700 | [diff] [blame] | 1823 | reword = 1; |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1824 | msg_file = NULL; |
| 1825 | goto fast_forward_edit; |
| 1826 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1827 | if (parent && parse_commit(parent) < 0) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1828 | /* TRANSLATORS: The first %s will be a "todo" command like |
| 1829 | "revert" or "pick", the second %s a SHA1. */ |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1830 | return error(_("%s: cannot parse parent commit %s"), |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1831 | command_to_string(command), |
| 1832 | oid_to_hex(&parent->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1833 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1834 | /* |
| 1835 | * "commit" is an existing commit. We would want to apply |
| 1836 | * the difference it introduces since its first parent "prev" |
| 1837 | * on top of the current HEAD if we are cherry-pick. Or the |
| 1838 | * reverse of it if we are revert. |
| 1839 | */ |
| 1840 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1841 | if (command == TODO_REVERT) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1842 | base = commit; |
| 1843 | base_label = msg.label; |
| 1844 | next = parent; |
| 1845 | next_label = msg.parent_label; |
| 1846 | strbuf_addstr(&msgbuf, "Revert \""); |
| 1847 | strbuf_addstr(&msgbuf, msg.subject); |
| 1848 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1849 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1850 | |
| 1851 | if (commit->parents && commit->parents->next) { |
| 1852 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1853 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1854 | } |
| 1855 | strbuf_addstr(&msgbuf, ".\n"); |
| 1856 | } else { |
| 1857 | const char *p; |
| 1858 | |
| 1859 | base = parent; |
| 1860 | base_label = msg.parent_label; |
| 1861 | next = commit; |
| 1862 | next_label = msg.label; |
| 1863 | |
Johannes Schindelin | 23aa514 | 2017-01-02 16:26:20 +0100 | [diff] [blame] | 1864 | /* Append the commit log message to msgbuf. */ |
| 1865 | if (find_commit_subject(msg.message, &p)) |
| 1866 | strbuf_addstr(&msgbuf, p); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1867 | |
| 1868 | if (opts->record_origin) { |
Jonathan Tan | 44dc738 | 2017-04-26 13:50:03 -0700 | [diff] [blame] | 1869 | strbuf_complete_line(&msgbuf); |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1870 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 1871 | strbuf_addch(&msgbuf, '\n'); |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 1872 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1873 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1874 | strbuf_addstr(&msgbuf, ")\n"); |
| 1875 | } |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1876 | if (!is_fixup(command)) |
| 1877 | author = get_author(msg.message); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1878 | } |
| 1879 | |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1880 | if (command == TODO_REWORD) |
Phillip Wood | 450efe2 | 2019-08-19 02:18:21 -0700 | [diff] [blame] | 1881 | reword = 1; |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1882 | else if (is_fixup(command)) { |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1883 | if (update_squash_messages(r, command, commit, opts)) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1884 | return -1; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1885 | flags |= AMEND_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1886 | if (!final_fixup) |
| 1887 | msg_file = rebase_path_squash_msg(); |
| 1888 | else if (file_exists(rebase_path_fixup_msg())) { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1889 | flags |= CLEANUP_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1890 | msg_file = rebase_path_fixup_msg(); |
| 1891 | } else { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1892 | const char *dest = git_path_squash_msg(r); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1893 | unlink(dest); |
| 1894 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) |
| 1895 | return error(_("could not rename '%s' to '%s'"), |
| 1896 | rebase_path_squash_msg(), dest); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1897 | unlink(git_path_merge_msg(r)); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1898 | msg_file = dest; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1899 | flags |= EDIT_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 1903 | if (opts->signoff && !is_fixup(command)) |
Phillip Wood | b34eeea | 2017-11-24 11:07:55 +0000 | [diff] [blame] | 1904 | append_signoff(&msgbuf, 0, 0); |
| 1905 | |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 1906 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
| 1907 | res = -1; |
| 1908 | else if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1909 | res = do_recursive_merge(r, base, next, base_label, next_label, |
brian m. carlson | 48be4c6 | 2017-05-06 22:10:36 +0000 | [diff] [blame] | 1910 | &head, &msgbuf, opts); |
Johannes Schindelin | f241ff0 | 2016-07-26 18:06:02 +0200 | [diff] [blame] | 1911 | if (res < 0) |
Stefan Beller | 19517fb | 2018-06-01 13:01:45 -0700 | [diff] [blame] | 1912 | goto leave; |
| 1913 | |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 1914 | res |= write_message(msgbuf.buf, msgbuf.len, |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1915 | git_path_merge_msg(r), 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1916 | } else { |
| 1917 | struct commit_list *common = NULL; |
| 1918 | struct commit_list *remotes = NULL; |
| 1919 | |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 1920 | res = write_message(msgbuf.buf, msgbuf.len, |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1921 | git_path_merge_msg(r), 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1922 | |
| 1923 | commit_list_insert(base, &common); |
| 1924 | commit_list_insert(next, &remotes); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1925 | res |= try_merge_command(r, opts->strategy, |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 1926 | opts->xopts_nr, (const char **)opts->xopts, |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1927 | common, oid_to_hex(&head), remotes); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1928 | free_commit_list(common); |
| 1929 | free_commit_list(remotes); |
| 1930 | } |
Johannes Schindelin | 452202c | 2016-10-21 14:25:41 +0200 | [diff] [blame] | 1931 | strbuf_release(&msgbuf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1932 | |
| 1933 | /* |
| 1934 | * If the merge was clean or if it failed due to conflict, we write |
| 1935 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
| 1936 | * However, if the merge did not even start, then we don't want to |
| 1937 | * write it at all. |
| 1938 | */ |
Phillip Wood | 21b11c6 | 2019-12-06 16:06:09 +0000 | [diff] [blame] | 1939 | if ((command == TODO_PICK || command == TODO_REWORD || |
| 1940 | command == TODO_EDIT) && !opts->no_commit && |
| 1941 | (res == 0 || res == 1) && |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 1942 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 1943 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
Johannes Schindelin | dbfad03 | 2016-08-26 15:47:14 +0200 | [diff] [blame] | 1944 | res = -1; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1945 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 1946 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 1947 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
Johannes Schindelin | dbfad03 | 2016-08-26 15:47:14 +0200 | [diff] [blame] | 1948 | res = -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1949 | |
| 1950 | if (res) { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1951 | error(command == TODO_REVERT |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1952 | ? _("could not revert %s... %s") |
| 1953 | : _("could not apply %s... %s"), |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 1954 | short_commit_name(commit), msg.subject); |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 1955 | print_advice(r, res == 1, opts); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1956 | repo_rerere(r, opts->allow_rerere_auto); |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 1957 | goto leave; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1958 | } |
| 1959 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1960 | drop_commit = 0; |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 1961 | allow = allow_empty(r, opts, commit); |
Felipe Contreras | 706728a | 2013-06-06 03:58:57 -0500 | [diff] [blame] | 1962 | if (allow < 0) { |
| 1963 | res = allow; |
| 1964 | goto leave; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1965 | } else if (allow == 1) { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1966 | flags |= ALLOW_EMPTY; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1967 | } else if (allow == 2) { |
| 1968 | drop_commit = 1; |
Elijah Newren | 9a1b747 | 2020-03-11 15:30:22 +0000 | [diff] [blame] | 1969 | unlink(git_path_cherry_pick_head(r)); |
| 1970 | unlink(git_path_merge_msg(r)); |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1971 | fprintf(stderr, |
| 1972 | _("dropping %s %s -- patch contents already upstream\n"), |
| 1973 | oid_to_hex(&commit->object.oid), msg.subject); |
| 1974 | } /* else allow == 0 and there's nothing special to do */ |
| 1975 | if (!opts->no_commit && !drop_commit) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1976 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 1977 | res = do_commit(r, msg_file, author, opts, flags, |
| 1978 | commit? &commit->object.oid : NULL); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1979 | else |
| 1980 | res = error(_("unable to parse commit author")); |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 1981 | *check_todo = !!(flags & EDIT_MSG); |
| 1982 | if (!res && reword) { |
Phillip Wood | 450efe2 | 2019-08-19 02:18:21 -0700 | [diff] [blame] | 1983 | fast_forward_edit: |
| 1984 | res = run_git_commit(r, NULL, opts, EDIT_MSG | |
| 1985 | VERIFY_MSG | AMEND_MSG | |
| 1986 | (flags & ALLOW_EMPTY)); |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 1987 | *check_todo = 1; |
| 1988 | } |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1989 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1990 | |
Phillip Wood | 450efe2 | 2019-08-19 02:18:21 -0700 | [diff] [blame] | 1991 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1992 | if (!res && final_fixup) { |
| 1993 | unlink(rebase_path_fixup_msg()); |
| 1994 | unlink(rebase_path_squash_msg()); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1995 | unlink(rebase_path_current_fixups()); |
| 1996 | strbuf_reset(&opts->current_fixups); |
| 1997 | opts->current_fixup_count = 0; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1998 | } |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 1999 | |
| 2000 | leave: |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 2001 | free_message(commit, &msg); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 2002 | free(author); |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2003 | update_abort_safety_file(); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2004 | |
| 2005 | return res; |
| 2006 | } |
| 2007 | |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 2008 | static int prepare_revs(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2009 | { |
Martin von Zweigbergk | a73e22e | 2012-08-28 23:15:56 -0700 | [diff] [blame] | 2010 | /* |
| 2011 | * picking (but not reverting) ranges (but not individual revisions) |
| 2012 | * should be done in reverse |
| 2013 | */ |
| 2014 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2015 | opts->revs->reverse ^= 1; |
| 2016 | |
| 2017 | if (prepare_revision_walk(opts->revs)) |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 2018 | return error(_("revision walk setup failed")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2019 | |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 2020 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2021 | } |
| 2022 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 2023 | static int read_and_refresh_cache(struct repository *r, |
| 2024 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2025 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 2026 | struct lock_file index_lock = LOCK_INIT; |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 2027 | int index_fd = repo_hold_locked_index(r, &index_lock, 0); |
| 2028 | if (repo_read_index(r) < 0) { |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 2029 | rollback_lock_file(&index_lock); |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 2030 | return error(_("git %s: failed to read the index"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 2031 | _(action_name(opts))); |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 2032 | } |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 2033 | refresh_index(r->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 2034 | if (index_fd >= 0) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 2035 | if (write_locked_index(r->index, &index_lock, |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 2036 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 2037 | return error(_("git %s: failed to refresh the index"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 2038 | _(action_name(opts))); |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 2039 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2040 | } |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 2041 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2042 | } |
| 2043 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2044 | enum todo_item_flags { |
| 2045 | TODO_EDIT_MERGE_MSG = 1 |
| 2046 | }; |
| 2047 | |
Alban Gruin | 5d94d54 | 2018-12-29 17:03:59 +0100 | [diff] [blame] | 2048 | void todo_list_release(struct todo_list *todo_list) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2049 | { |
| 2050 | strbuf_release(&todo_list->buf); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2051 | FREE_AND_NULL(todo_list->items); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2052 | todo_list->nr = todo_list->alloc = 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2053 | } |
| 2054 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2055 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
| 2056 | { |
| 2057 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); |
Alban Gruin | 8638114 | 2019-11-24 18:43:28 +0100 | [diff] [blame] | 2058 | todo_list->total_nr++; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2059 | return todo_list->items + todo_list->nr++; |
| 2060 | } |
| 2061 | |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2062 | const char *todo_item_get_arg(struct todo_list *todo_list, |
| 2063 | struct todo_item *item) |
| 2064 | { |
| 2065 | return todo_list->buf.buf + item->arg_offset; |
| 2066 | } |
| 2067 | |
Phillip Wood | 3e81bcc | 2019-06-27 07:12:45 -0700 | [diff] [blame] | 2068 | static int is_command(enum todo_command command, const char **bol) |
| 2069 | { |
| 2070 | const char *str = todo_command_info[command].str; |
| 2071 | const char nick = todo_command_info[command].c; |
| 2072 | const char *p = *bol + 1; |
| 2073 | |
| 2074 | return skip_prefix(*bol, str, bol) || |
| 2075 | ((nick && **bol == nick) && |
| 2076 | (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && |
| 2077 | (*bol = p)); |
| 2078 | } |
| 2079 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2080 | static int parse_insn_line(struct repository *r, struct todo_item *item, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2081 | const char *buf, const char *bol, char *eol) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2082 | { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 2083 | struct object_id commit_oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2084 | char *end_of_object_name; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2085 | int i, saved, status, padding; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2086 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2087 | item->flags = 0; |
| 2088 | |
Johannes Schindelin | 8f8550b | 2016-10-21 14:25:36 +0200 | [diff] [blame] | 2089 | /* left-trim */ |
| 2090 | bol += strspn(bol, " \t"); |
| 2091 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 2092 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 2093 | item->command = TODO_COMMENT; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 2094 | item->commit = NULL; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2095 | item->arg_offset = bol - buf; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 2096 | item->arg_len = eol - bol; |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 2100 | for (i = 0; i < TODO_COMMENT; i++) |
Phillip Wood | 3e81bcc | 2019-06-27 07:12:45 -0700 | [diff] [blame] | 2101 | if (is_command(i, &bol)) { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2102 | item->command = i; |
| 2103 | break; |
| 2104 | } |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 2105 | if (i >= TODO_COMMENT) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2106 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2107 | |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 2108 | /* Eat up extra spaces/ tabs before object name */ |
| 2109 | padding = strspn(bol, " \t"); |
| 2110 | bol += padding; |
| 2111 | |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 2112 | if (item->command == TODO_NOOP || item->command == TODO_BREAK) { |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 2113 | if (bol != eol) |
| 2114 | return error(_("%s does not accept arguments: '%s'"), |
| 2115 | command_to_string(item->command), bol); |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 2116 | item->commit = NULL; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2117 | item->arg_offset = bol - buf; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 2118 | item->arg_len = eol - bol; |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2122 | if (!padding) |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 2123 | return error(_("missing arguments for %s"), |
| 2124 | command_to_string(item->command)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2125 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 2126 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
| 2127 | item->command == TODO_RESET) { |
Liam Beguin | 7dcbb3c | 2017-12-03 17:17:15 -0500 | [diff] [blame] | 2128 | item->commit = NULL; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2129 | item->arg_offset = bol - buf; |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 2130 | item->arg_len = (int)(eol - bol); |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2134 | if (item->command == TODO_MERGE) { |
| 2135 | if (skip_prefix(bol, "-C", &bol)) |
| 2136 | bol += strspn(bol, " \t"); |
| 2137 | else if (skip_prefix(bol, "-c", &bol)) { |
| 2138 | bol += strspn(bol, " \t"); |
| 2139 | item->flags |= TODO_EDIT_MERGE_MSG; |
| 2140 | } else { |
| 2141 | item->flags |= TODO_EDIT_MERGE_MSG; |
| 2142 | item->commit = NULL; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2143 | item->arg_offset = bol - buf; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2144 | item->arg_len = (int)(eol - bol); |
| 2145 | return 0; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2149 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2150 | saved = *end_of_object_name; |
| 2151 | *end_of_object_name = '\0'; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 2152 | status = get_oid(bol, &commit_oid); |
Johannes Schindelin | d859dca | 2020-01-23 12:28:17 +0000 | [diff] [blame] | 2153 | if (status < 0) |
| 2154 | error(_("could not parse '%s'"), bol); /* return later */ |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2155 | *end_of_object_name = saved; |
| 2156 | |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2157 | bol = end_of_object_name + strspn(end_of_object_name, " \t"); |
| 2158 | item->arg_offset = bol - buf; |
| 2159 | item->arg_len = (int)(eol - bol); |
Johannes Schindelin | c22f7df | 2016-10-21 14:25:00 +0200 | [diff] [blame] | 2160 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2161 | if (status < 0) |
Johannes Schindelin | d859dca | 2020-01-23 12:28:17 +0000 | [diff] [blame] | 2162 | return status; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2163 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2164 | item->commit = lookup_commit_reference(r, &commit_oid); |
Johannes Schindelin | d859dca | 2020-01-23 12:28:17 +0000 | [diff] [blame] | 2165 | return item->commit ? 0 : -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2166 | } |
| 2167 | |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2168 | int sequencer_get_last_command(struct repository *r, enum replay_action *action) |
| 2169 | { |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2170 | const char *todo_file, *bol; |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2171 | struct strbuf buf = STRBUF_INIT; |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2172 | int ret = 0; |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2173 | |
| 2174 | todo_file = git_path_todo_file(); |
| 2175 | if (strbuf_read_file(&buf, todo_file, 0) < 0) { |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2176 | if (errno == ENOENT || errno == ENOTDIR) |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2177 | return -1; |
| 2178 | else |
| 2179 | return error_errno("unable to open '%s'", todo_file); |
| 2180 | } |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2181 | bol = buf.buf + strspn(buf.buf, " \t\r\n"); |
| 2182 | if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t')) |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2183 | *action = REPLAY_PICK; |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2184 | else if (is_command(TODO_REVERT, &bol) && |
| 2185 | (*bol == ' ' || *bol == '\t')) |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2186 | *action = REPLAY_REVERT; |
| 2187 | else |
Phillip Wood | ed5b1ca | 2019-06-27 07:12:46 -0700 | [diff] [blame] | 2188 | ret = -1; |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2189 | |
Phillip Wood | 4a72486 | 2019-04-16 11:18:42 +0100 | [diff] [blame] | 2190 | strbuf_release(&buf); |
| 2191 | |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Alban Gruin | 5d94d54 | 2018-12-29 17:03:59 +0100 | [diff] [blame] | 2195 | int todo_list_parse_insn_buffer(struct repository *r, char *buf, |
| 2196 | struct todo_list *todo_list) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2197 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2198 | struct todo_item *item; |
| 2199 | char *p = buf, *next_p; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2200 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2201 | |
Alban Gruin | 2b71595 | 2018-12-29 17:03:58 +0100 | [diff] [blame] | 2202 | todo_list->current = todo_list->nr = 0; |
| 2203 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2204 | for (i = 1; *p; i++, p = next_p) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2205 | char *eol = strchrnul(p, '\n'); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2206 | |
| 2207 | next_p = *eol ? eol + 1 /* skip LF */ : eol; |
| 2208 | |
Johannes Schindelin | 6307041 | 2016-10-21 14:24:46 +0200 | [diff] [blame] | 2209 | if (p != eol && eol[-1] == '\r') |
| 2210 | eol--; /* strip Carriage Return */ |
| 2211 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2212 | item = append_new_todo(todo_list); |
| 2213 | item->offset_in_buf = p - todo_list->buf.buf; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2214 | if (parse_insn_line(r, item, buf, p, eol)) { |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2215 | res = error(_("invalid line %d: %.*s"), |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2216 | i, (int)(eol - p), p); |
Alban Gruin | 2b71595 | 2018-12-29 17:03:58 +0100 | [diff] [blame] | 2217 | item->command = TODO_COMMENT + 1; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2218 | item->arg_offset = p - buf; |
Alban Gruin | 2b71595 | 2018-12-29 17:03:58 +0100 | [diff] [blame] | 2219 | item->arg_len = (int)(eol - p); |
| 2220 | item->commit = NULL; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2221 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2222 | |
| 2223 | if (fixup_okay) |
| 2224 | ; /* do nothing */ |
| 2225 | else if (is_fixup(item->command)) |
| 2226 | return error(_("cannot '%s' without a previous commit"), |
| 2227 | command_to_string(item->command)); |
| 2228 | else if (!is_noop(item->command)) |
| 2229 | fixup_okay = 1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2230 | } |
Johannes Schindelin | 5286527 | 2017-01-02 16:27:34 +0100 | [diff] [blame] | 2231 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2232 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2233 | } |
| 2234 | |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2235 | static int count_commands(struct todo_list *todo_list) |
| 2236 | { |
| 2237 | int count = 0, i; |
| 2238 | |
| 2239 | for (i = 0; i < todo_list->nr; i++) |
| 2240 | if (todo_list->items[i].command != TODO_COMMENT) |
| 2241 | count++; |
| 2242 | |
| 2243 | return count; |
| 2244 | } |
| 2245 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2246 | static int get_item_line_offset(struct todo_list *todo_list, int index) |
| 2247 | { |
| 2248 | return index < todo_list->nr ? |
| 2249 | todo_list->items[index].offset_in_buf : todo_list->buf.len; |
| 2250 | } |
| 2251 | |
| 2252 | static const char *get_item_line(struct todo_list *todo_list, int index) |
| 2253 | { |
| 2254 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); |
| 2255 | } |
| 2256 | |
| 2257 | static int get_item_line_length(struct todo_list *todo_list, int index) |
| 2258 | { |
| 2259 | return get_item_line_offset(todo_list, index + 1) |
| 2260 | - get_item_line_offset(todo_list, index); |
| 2261 | } |
| 2262 | |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2263 | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
| 2264 | { |
| 2265 | int fd; |
| 2266 | ssize_t len; |
| 2267 | |
| 2268 | fd = open(path, O_RDONLY); |
| 2269 | if (fd < 0) |
| 2270 | return error_errno(_("could not open '%s'"), path); |
| 2271 | len = strbuf_read(sb, fd, 0); |
| 2272 | close(fd); |
| 2273 | if (len < 0) |
| 2274 | return error(_("could not read '%s'."), path); |
| 2275 | return len; |
| 2276 | } |
| 2277 | |
Phillip Wood | b07d9bf | 2019-04-16 11:18:41 +0100 | [diff] [blame] | 2278 | static int have_finished_the_last_pick(void) |
| 2279 | { |
| 2280 | struct strbuf buf = STRBUF_INIT; |
| 2281 | const char *eol; |
| 2282 | const char *todo_path = git_path_todo_file(); |
| 2283 | int ret = 0; |
| 2284 | |
| 2285 | if (strbuf_read_file(&buf, todo_path, 0) < 0) { |
| 2286 | if (errno == ENOENT) { |
| 2287 | return 0; |
| 2288 | } else { |
| 2289 | error_errno("unable to open '%s'", todo_path); |
| 2290 | return 0; |
| 2291 | } |
| 2292 | } |
| 2293 | /* If there is only one line then we are done */ |
| 2294 | eol = strchr(buf.buf, '\n'); |
| 2295 | if (!eol || !eol[1]) |
| 2296 | ret = 1; |
| 2297 | |
| 2298 | strbuf_release(&buf); |
| 2299 | |
| 2300 | return ret; |
| 2301 | } |
| 2302 | |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 2303 | void sequencer_post_commit_cleanup(struct repository *r, int verbose) |
Phillip Wood | b07d9bf | 2019-04-16 11:18:41 +0100 | [diff] [blame] | 2304 | { |
| 2305 | struct replay_opts opts = REPLAY_OPTS_INIT; |
| 2306 | int need_cleanup = 0; |
| 2307 | |
| 2308 | if (file_exists(git_path_cherry_pick_head(r))) { |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 2309 | if (!unlink(git_path_cherry_pick_head(r)) && verbose) |
| 2310 | warning(_("cancelling a cherry picking in progress")); |
Phillip Wood | b07d9bf | 2019-04-16 11:18:41 +0100 | [diff] [blame] | 2311 | opts.action = REPLAY_PICK; |
| 2312 | need_cleanup = 1; |
| 2313 | } |
| 2314 | |
| 2315 | if (file_exists(git_path_revert_head(r))) { |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 2316 | if (!unlink(git_path_revert_head(r)) && verbose) |
| 2317 | warning(_("cancelling a revert in progress")); |
Phillip Wood | b07d9bf | 2019-04-16 11:18:41 +0100 | [diff] [blame] | 2318 | opts.action = REPLAY_REVERT; |
| 2319 | need_cleanup = 1; |
| 2320 | } |
| 2321 | |
| 2322 | if (!need_cleanup) |
| 2323 | return; |
| 2324 | |
| 2325 | if (!have_finished_the_last_pick()) |
| 2326 | return; |
| 2327 | |
| 2328 | sequencer_remove_state(&opts); |
| 2329 | } |
| 2330 | |
Alban Gruin | 3f34f2d | 2019-11-24 18:43:30 +0100 | [diff] [blame] | 2331 | static void todo_list_write_total_nr(struct todo_list *todo_list) |
| 2332 | { |
| 2333 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
| 2334 | |
| 2335 | if (f) { |
| 2336 | fprintf(f, "%d\n", todo_list->total_nr); |
| 2337 | fclose(f); |
| 2338 | } |
| 2339 | } |
| 2340 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2341 | static int read_populate_todo(struct repository *r, |
| 2342 | struct todo_list *todo_list, |
| 2343 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2344 | { |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 2345 | struct stat st; |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 2346 | const char *todo_file = get_todo_path(opts); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2347 | int res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2348 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2349 | strbuf_reset(&todo_list->buf); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2350 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
| 2351 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2352 | |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 2353 | res = stat(todo_file, &st); |
| 2354 | if (res) |
| 2355 | return error(_("could not stat '%s'"), todo_file); |
| 2356 | fill_stat_data(&todo_list->stat, &st); |
| 2357 | |
Alban Gruin | 5d94d54 | 2018-12-29 17:03:59 +0100 | [diff] [blame] | 2358 | res = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list); |
Johannes Schindelin | 27fdbb9 | 2017-01-02 16:35:42 +0100 | [diff] [blame] | 2359 | if (res) { |
| 2360 | if (is_rebase_i(opts)) |
| 2361 | return error(_("please fix this using " |
| 2362 | "'git rebase --edit-todo'.")); |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2363 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
Johannes Schindelin | 27fdbb9 | 2017-01-02 16:35:42 +0100 | [diff] [blame] | 2364 | } |
Johannes Schindelin | 2eeaf1b | 2016-10-21 14:26:13 +0200 | [diff] [blame] | 2365 | |
Johannes Schindelin | 5286527 | 2017-01-02 16:27:34 +0100 | [diff] [blame] | 2366 | if (!todo_list->nr && |
| 2367 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) |
| 2368 | return error(_("no commits parsed.")); |
| 2369 | |
Johannes Schindelin | 2eeaf1b | 2016-10-21 14:26:13 +0200 | [diff] [blame] | 2370 | if (!is_rebase_i(opts)) { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2371 | enum todo_command valid = |
| 2372 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; |
| 2373 | int i; |
| 2374 | |
| 2375 | for (i = 0; i < todo_list->nr; i++) |
| 2376 | if (valid == todo_list->items[i].command) |
| 2377 | continue; |
| 2378 | else if (valid == TODO_PICK) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2379 | return error(_("cannot cherry-pick during a revert.")); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2380 | else |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2381 | return error(_("cannot revert during a cherry-pick.")); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2382 | } |
| 2383 | |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2384 | if (is_rebase_i(opts)) { |
| 2385 | struct todo_list done = TODO_LIST_INIT; |
| 2386 | |
| 2387 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && |
Alban Gruin | 5d94d54 | 2018-12-29 17:03:59 +0100 | [diff] [blame] | 2388 | !todo_list_parse_insn_buffer(r, done.buf.buf, &done)) |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2389 | todo_list->done_nr = count_commands(&done); |
| 2390 | else |
| 2391 | todo_list->done_nr = 0; |
| 2392 | |
| 2393 | todo_list->total_nr = todo_list->done_nr |
| 2394 | + count_commands(todo_list); |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2395 | todo_list_release(&done); |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 2396 | |
Alban Gruin | 3f34f2d | 2019-11-24 18:43:30 +0100 | [diff] [blame] | 2397 | todo_list_write_total_nr(todo_list); |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Johannes Schindelin | 0ae42a0 | 2016-09-09 16:37:24 +0200 | [diff] [blame] | 2400 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2401 | } |
| 2402 | |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2403 | static int git_config_string_dup(char **dest, |
| 2404 | const char *var, const char *value) |
| 2405 | { |
| 2406 | if (!value) |
| 2407 | return config_error_nonbool(var); |
| 2408 | free(*dest); |
| 2409 | *dest = xstrdup(value); |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2413 | static int populate_opts_cb(const char *key, const char *value, void *data) |
| 2414 | { |
| 2415 | struct replay_opts *opts = data; |
| 2416 | int error_flag = 1; |
| 2417 | |
| 2418 | if (!value) |
| 2419 | error_flag = 0; |
| 2420 | else if (!strcmp(key, "options.no-commit")) |
| 2421 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); |
| 2422 | else if (!strcmp(key, "options.edit")) |
| 2423 | opts->edit = git_config_bool_or_int(key, value, &error_flag); |
Phillip Wood | 6860ce5 | 2019-03-13 18:26:15 +0000 | [diff] [blame] | 2424 | else if (!strcmp(key, "options.allow-empty")) |
| 2425 | opts->allow_empty = |
| 2426 | git_config_bool_or_int(key, value, &error_flag); |
| 2427 | else if (!strcmp(key, "options.allow-empty-message")) |
| 2428 | opts->allow_empty_message = |
| 2429 | git_config_bool_or_int(key, value, &error_flag); |
| 2430 | else if (!strcmp(key, "options.keep-redundant-commits")) |
| 2431 | opts->keep_redundant_commits = |
| 2432 | git_config_bool_or_int(key, value, &error_flag); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2433 | else if (!strcmp(key, "options.signoff")) |
| 2434 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); |
| 2435 | else if (!strcmp(key, "options.record-origin")) |
| 2436 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); |
| 2437 | else if (!strcmp(key, "options.allow-ff")) |
| 2438 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); |
| 2439 | else if (!strcmp(key, "options.mainline")) |
| 2440 | opts->mainline = git_config_int(key, value); |
| 2441 | else if (!strcmp(key, "options.strategy")) |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2442 | git_config_string_dup(&opts->strategy, key, value); |
Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 +0000 | [diff] [blame] | 2443 | else if (!strcmp(key, "options.gpg-sign")) |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2444 | git_config_string_dup(&opts->gpg_sign, key, value); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2445 | else if (!strcmp(key, "options.strategy-option")) { |
| 2446 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
| 2447 | opts->xopts[opts->xopts_nr++] = xstrdup(value); |
Phillip Wood | 8d8cb4b | 2017-08-02 11:44:19 +0100 | [diff] [blame] | 2448 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
| 2449 | opts->allow_rerere_auto = |
| 2450 | git_config_bool_or_int(key, value, &error_flag) ? |
| 2451 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; |
Phillip Wood | dc42e9a | 2019-04-17 11:23:29 +0100 | [diff] [blame] | 2452 | else if (!strcmp(key, "options.default-msg-cleanup")) { |
| 2453 | opts->explicit_cleanup = 1; |
| 2454 | opts->default_msg_cleanup = get_cleanup_mode(value, 1); |
| 2455 | } else |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2456 | return error(_("invalid key: %s"), key); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2457 | |
| 2458 | if (!error_flag) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2459 | return error(_("invalid value for %s: %s"), key, value); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2464 | void parse_strategy_opts(struct replay_opts *opts, char *raw_opts) |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2465 | { |
| 2466 | int i; |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2467 | char *strategy_opts_string = raw_opts; |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2468 | |
Elijah Newren | 0060041 | 2018-06-27 08:48:04 -0700 | [diff] [blame] | 2469 | if (*strategy_opts_string == ' ') |
| 2470 | strategy_opts_string++; |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2471 | |
Elijah Newren | 0060041 | 2018-06-27 08:48:04 -0700 | [diff] [blame] | 2472 | opts->xopts_nr = split_cmdline(strategy_opts_string, |
| 2473 | (const char ***)&opts->xopts); |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2474 | for (i = 0; i < opts->xopts_nr; i++) { |
| 2475 | const char *arg = opts->xopts[i]; |
| 2476 | |
| 2477 | skip_prefix(arg, "--", &arg); |
| 2478 | opts->xopts[i] = xstrdup(arg); |
| 2479 | } |
| 2480 | } |
| 2481 | |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2482 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
| 2483 | { |
| 2484 | strbuf_reset(buf); |
| 2485 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) |
| 2486 | return; |
| 2487 | opts->strategy = strbuf_detach(buf, NULL); |
| 2488 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) |
| 2489 | return; |
| 2490 | |
| 2491 | parse_strategy_opts(opts, buf->buf); |
| 2492 | } |
| 2493 | |
Johannes Schindelin | 5adf9bd | 2016-10-14 15:17:16 +0200 | [diff] [blame] | 2494 | static int read_populate_opts(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2495 | { |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2496 | if (is_rebase_i(opts)) { |
| 2497 | struct strbuf buf = STRBUF_INIT; |
Denton Liu | 65c425a | 2020-04-03 21:11:16 -0400 | [diff] [blame] | 2498 | int ret = 0; |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2499 | |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 2500 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), |
| 2501 | READ_ONELINER_SKIP_IF_EMPTY)) { |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2502 | if (!starts_with(buf.buf, "-S")) |
| 2503 | strbuf_reset(&buf); |
| 2504 | else { |
| 2505 | free(opts->gpg_sign); |
| 2506 | opts->gpg_sign = xstrdup(buf.buf + 2); |
| 2507 | } |
Phillip Wood | 9b6d7a6 | 2017-08-02 11:44:17 +0100 | [diff] [blame] | 2508 | strbuf_reset(&buf); |
| 2509 | } |
| 2510 | |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 2511 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), |
| 2512 | READ_ONELINER_SKIP_IF_EMPTY)) { |
Phillip Wood | 9b6d7a6 | 2017-08-02 11:44:17 +0100 | [diff] [blame] | 2513 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
| 2514 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; |
| 2515 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) |
| 2516 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; |
| 2517 | strbuf_reset(&buf); |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2518 | } |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2519 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 2520 | if (file_exists(rebase_path_verbose())) |
| 2521 | opts->verbose = 1; |
| 2522 | |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 2523 | if (file_exists(rebase_path_quiet())) |
| 2524 | opts->quiet = 1; |
| 2525 | |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 2526 | if (file_exists(rebase_path_signoff())) { |
| 2527 | opts->allow_ff = 0; |
| 2528 | opts->signoff = 1; |
| 2529 | } |
| 2530 | |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 2531 | if (file_exists(rebase_path_reschedule_failed_exec())) |
| 2532 | opts->reschedule_failed_exec = 1; |
| 2533 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 2534 | if (file_exists(rebase_path_drop_redundant_commits())) |
| 2535 | opts->drop_redundant_commits = 1; |
| 2536 | |
| 2537 | if (file_exists(rebase_path_keep_redundant_commits())) |
| 2538 | opts->keep_redundant_commits = 1; |
| 2539 | |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2540 | read_strategy_opts(opts, &buf); |
Denton Liu | 65c425a | 2020-04-03 21:11:16 -0400 | [diff] [blame] | 2541 | strbuf_reset(&buf); |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2542 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 2543 | if (read_oneliner(&opts->current_fixups, |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 2544 | rebase_path_current_fixups(), |
| 2545 | READ_ONELINER_SKIP_IF_EMPTY)) { |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 2546 | const char *p = opts->current_fixups.buf; |
| 2547 | opts->current_fixup_count = 1; |
| 2548 | while ((p = strchr(p, '\n'))) { |
| 2549 | opts->current_fixup_count++; |
| 2550 | p++; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 2554 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
Denton Liu | 65c425a | 2020-04-03 21:11:16 -0400 | [diff] [blame] | 2555 | if (get_oid_hex(buf.buf, &opts->squash_onto) < 0) { |
| 2556 | ret = error(_("unusable squash-onto")); |
| 2557 | goto done_rebase_i; |
| 2558 | } |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 2559 | opts->have_squash_onto = 1; |
| 2560 | } |
| 2561 | |
Denton Liu | 65c425a | 2020-04-03 21:11:16 -0400 | [diff] [blame] | 2562 | done_rebase_i: |
| 2563 | strbuf_release(&buf); |
| 2564 | return ret; |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2565 | } |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 2566 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2567 | if (!file_exists(git_path_opts_file())) |
Johannes Schindelin | 0d00da7 | 2016-09-09 16:37:27 +0200 | [diff] [blame] | 2568 | return 0; |
| 2569 | /* |
| 2570 | * The function git_parse_source(), called from git_config_from_file(), |
| 2571 | * may die() in case of a syntactically incorrect file. We do not care |
| 2572 | * about this case, though, because we wrote that file ourselves, so we |
| 2573 | * are pretty certain that it is syntactically correct. |
| 2574 | */ |
Johannes Schindelin | 5adf9bd | 2016-10-14 15:17:16 +0200 | [diff] [blame] | 2575 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2576 | return error(_("malformed options sheet: '%s'"), |
Johannes Schindelin | 0d00da7 | 2016-09-09 16:37:27 +0200 | [diff] [blame] | 2577 | git_path_opts_file()); |
| 2578 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2579 | } |
| 2580 | |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2581 | static void write_strategy_opts(struct replay_opts *opts) |
| 2582 | { |
| 2583 | int i; |
| 2584 | struct strbuf buf = STRBUF_INIT; |
| 2585 | |
| 2586 | for (i = 0; i < opts->xopts_nr; ++i) |
| 2587 | strbuf_addf(&buf, " --%s", opts->xopts[i]); |
| 2588 | |
| 2589 | write_file(rebase_path_strategy_opts(), "%s\n", buf.buf); |
| 2590 | strbuf_release(&buf); |
| 2591 | } |
| 2592 | |
| 2593 | int write_basic_state(struct replay_opts *opts, const char *head_name, |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 2594 | struct commit *onto, const char *orig_head) |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2595 | { |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2596 | if (head_name) |
| 2597 | write_file(rebase_path_head_name(), "%s\n", head_name); |
| 2598 | if (onto) |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 2599 | write_file(rebase_path_onto(), "%s\n", |
| 2600 | oid_to_hex(&onto->object.oid)); |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2601 | if (orig_head) |
| 2602 | write_file(rebase_path_orig_head(), "%s\n", orig_head); |
| 2603 | |
Elijah Newren | 8a997ed | 2020-02-15 21:36:27 +0000 | [diff] [blame] | 2604 | if (opts->quiet) |
| 2605 | write_file(rebase_path_quiet(), "%s", ""); |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2606 | if (opts->verbose) |
Carlo Marcelo Arenas Belón | 4af5174 | 2018-10-25 02:38:54 -0700 | [diff] [blame] | 2607 | write_file(rebase_path_verbose(), "%s", ""); |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2608 | if (opts->strategy) |
| 2609 | write_file(rebase_path_strategy(), "%s\n", opts->strategy); |
| 2610 | if (opts->xopts_nr > 0) |
| 2611 | write_strategy_opts(opts); |
| 2612 | |
| 2613 | if (opts->allow_rerere_auto == RERERE_AUTOUPDATE) |
| 2614 | write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n"); |
| 2615 | else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE) |
| 2616 | write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n"); |
| 2617 | |
| 2618 | if (opts->gpg_sign) |
| 2619 | write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign); |
| 2620 | if (opts->signoff) |
| 2621 | write_file(rebase_path_signoff(), "--signoff\n"); |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 2622 | if (opts->drop_redundant_commits) |
| 2623 | write_file(rebase_path_drop_redundant_commits(), "%s", ""); |
| 2624 | if (opts->keep_redundant_commits) |
| 2625 | write_file(rebase_path_keep_redundant_commits(), "%s", ""); |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 2626 | if (opts->reschedule_failed_exec) |
| 2627 | write_file(rebase_path_reschedule_failed_exec(), "%s", ""); |
Alban Gruin | 6585068 | 2018-08-28 14:10:40 +0200 | [diff] [blame] | 2628 | |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2632 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2633 | struct replay_opts *opts) |
| 2634 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2635 | enum todo_command command = opts->action == REPLAY_PICK ? |
| 2636 | TODO_PICK : TODO_REVERT; |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 2637 | const char *command_string = todo_command_info[command].str; |
Doan Tran Cong Danh | 019a9d8 | 2019-11-08 16:43:47 +0700 | [diff] [blame] | 2638 | const char *encoding; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2639 | struct commit *commit; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2640 | |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 2641 | if (prepare_revs(opts)) |
| 2642 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2643 | |
Doan Tran Cong Danh | 019a9d8 | 2019-11-08 16:43:47 +0700 | [diff] [blame] | 2644 | encoding = get_log_output_encoding(); |
| 2645 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2646 | while ((commit = get_revision(opts->revs))) { |
| 2647 | struct todo_item *item = append_new_todo(todo_list); |
Doan Tran Cong Danh | 019a9d8 | 2019-11-08 16:43:47 +0700 | [diff] [blame] | 2648 | const char *commit_buffer = logmsg_reencode(commit, NULL, encoding); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2649 | const char *subject; |
| 2650 | int subject_len; |
| 2651 | |
| 2652 | item->command = command; |
| 2653 | item->commit = commit; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 2654 | item->arg_offset = 0; |
Johannes Schindelin | c22f7df | 2016-10-21 14:25:00 +0200 | [diff] [blame] | 2655 | item->arg_len = 0; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2656 | item->offset_in_buf = todo_list->buf.len; |
| 2657 | subject_len = find_commit_subject(commit_buffer, &subject); |
| 2658 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, |
| 2659 | short_commit_name(commit), subject_len, subject); |
| 2660 | unuse_commit_buffer(commit, commit_buffer); |
| 2661 | } |
Jeff King | 8530c73 | 2018-07-09 15:48:19 -0400 | [diff] [blame] | 2662 | |
| 2663 | if (!todo_list->nr) |
| 2664 | return error(_("empty commit set passed")); |
| 2665 | |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 2666 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2667 | } |
| 2668 | |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2669 | static int create_seq_dir(struct repository *r) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2670 | { |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2671 | enum replay_action action; |
| 2672 | const char *in_progress_error = NULL; |
| 2673 | const char *in_progress_advice = NULL; |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 2674 | unsigned int advise_skip = file_exists(git_path_revert_head(r)) || |
| 2675 | file_exists(git_path_cherry_pick_head(r)); |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2676 | |
| 2677 | if (!sequencer_get_last_command(r, &action)) { |
| 2678 | switch (action) { |
| 2679 | case REPLAY_REVERT: |
| 2680 | in_progress_error = _("revert is already in progress"); |
| 2681 | in_progress_advice = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 2682 | _("try \"git revert (--continue | %s--abort | --quit)\""); |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2683 | break; |
| 2684 | case REPLAY_PICK: |
| 2685 | in_progress_error = _("cherry-pick is already in progress"); |
| 2686 | in_progress_advice = |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 2687 | _("try \"git cherry-pick (--continue | %s--abort | --quit)\""); |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2688 | break; |
| 2689 | default: |
| 2690 | BUG("unexpected action in create_seq_dir"); |
| 2691 | } |
| 2692 | } |
| 2693 | if (in_progress_error) { |
| 2694 | error("%s", in_progress_error); |
| 2695 | if (advice_sequencer_in_use) |
Rohit Ashiwal | dcb500d | 2019-07-02 14:41:29 +0530 | [diff] [blame] | 2696 | advise(in_progress_advice, |
| 2697 | advise_skip ? "--skip | " : ""); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2698 | return -1; |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2699 | } |
| 2700 | if (mkdir(git_path_seq_dir(), 0777) < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2701 | return error_errno(_("could not create sequencer directory '%s'"), |
Johannes Schindelin | f6e82b0 | 2016-09-09 16:37:44 +0200 | [diff] [blame] | 2702 | git_path_seq_dir()); |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 2703 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2704 | return 0; |
| 2705 | } |
| 2706 | |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2707 | static int save_head(const char *head) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2708 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 2709 | struct lock_file head_lock = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2710 | struct strbuf buf = STRBUF_INIT; |
| 2711 | int fd; |
Rene Scharfe | ed3f9a1 | 2017-08-30 20:20:14 +0200 | [diff] [blame] | 2712 | ssize_t written; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2713 | |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2714 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 2715 | if (fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2716 | return error_errno(_("could not lock HEAD")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2717 | strbuf_addf(&buf, "%s\n", head); |
Rene Scharfe | ed3f9a1 | 2017-08-30 20:20:14 +0200 | [diff] [blame] | 2718 | written = write_in_full(fd, buf.buf, buf.len); |
| 2719 | strbuf_release(&buf); |
| 2720 | if (written < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 2721 | error_errno(_("could not write to '%s'"), git_path_head_file()); |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2722 | rollback_lock_file(&head_lock); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 2723 | return -1; |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2724 | } |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 2725 | if (commit_lock_file(&head_lock) < 0) |
| 2726 | return error(_("failed to finalize '%s'"), git_path_head_file()); |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2727 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2728 | } |
| 2729 | |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2730 | static int rollback_is_safe(void) |
| 2731 | { |
| 2732 | struct strbuf sb = STRBUF_INIT; |
| 2733 | struct object_id expected_head, actual_head; |
| 2734 | |
| 2735 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { |
| 2736 | strbuf_trim(&sb); |
| 2737 | if (get_oid_hex(sb.buf, &expected_head)) { |
| 2738 | strbuf_release(&sb); |
| 2739 | die(_("could not parse %s"), git_path_abort_safety_file()); |
| 2740 | } |
| 2741 | strbuf_release(&sb); |
| 2742 | } |
| 2743 | else if (errno == ENOENT) |
| 2744 | oidclr(&expected_head); |
| 2745 | else |
| 2746 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); |
| 2747 | |
| 2748 | if (get_oid("HEAD", &actual_head)) |
| 2749 | oidclr(&actual_head); |
| 2750 | |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 2751 | return oideq(&actual_head, &expected_head); |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2752 | } |
| 2753 | |
Rohit Ashiwal | 918d1e6 | 2019-07-02 14:41:26 +0530 | [diff] [blame] | 2754 | static int reset_merge(const struct object_id *oid) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2755 | { |
Rohit Ashiwal | 265ab48 | 2019-07-02 14:41:27 +0530 | [diff] [blame] | 2756 | int ret; |
| 2757 | struct argv_array argv = ARGV_ARRAY_INIT; |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2758 | |
Rohit Ashiwal | 265ab48 | 2019-07-02 14:41:27 +0530 | [diff] [blame] | 2759 | argv_array_pushl(&argv, "reset", "--merge", NULL); |
| 2760 | |
| 2761 | if (!is_null_oid(oid)) |
| 2762 | argv_array_push(&argv, oid_to_hex(oid)); |
| 2763 | |
| 2764 | ret = run_command_v_opt(argv.argv, RUN_GIT_CMD); |
| 2765 | argv_array_clear(&argv); |
| 2766 | |
| 2767 | return ret; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2768 | } |
| 2769 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2770 | static int rollback_single_pick(struct repository *r) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2771 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2772 | struct object_id head_oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2773 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2774 | if (!file_exists(git_path_cherry_pick_head(r)) && |
| 2775 | !file_exists(git_path_revert_head(r))) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2776 | return error(_("no cherry-pick or revert in progress")); |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 2777 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2778 | return error(_("cannot resolve HEAD")); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2779 | if (is_null_oid(&head_oid)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2780 | return error(_("cannot abort from a branch yet to be born")); |
Rohit Ashiwal | 918d1e6 | 2019-07-02 14:41:26 +0530 | [diff] [blame] | 2781 | return reset_merge(&head_oid); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2782 | } |
| 2783 | |
Rohit Ashiwal | de81ca3 | 2019-07-02 14:41:28 +0530 | [diff] [blame] | 2784 | static int skip_single_pick(void) |
| 2785 | { |
| 2786 | struct object_id head; |
| 2787 | |
| 2788 | if (read_ref_full("HEAD", 0, &head, NULL)) |
| 2789 | return error(_("cannot resolve HEAD")); |
| 2790 | return reset_merge(&head); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2791 | } |
| 2792 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2793 | int sequencer_rollback(struct repository *r, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2794 | { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2795 | FILE *f; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2796 | struct object_id oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2797 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2798 | const char *p; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2799 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2800 | f = fopen(git_path_head_file(), "r"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2801 | if (!f && errno == ENOENT) { |
| 2802 | /* |
| 2803 | * There is no multiple-cherry-pick in progress. |
| 2804 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
| 2805 | * a single-cherry-pick in progress, abort that. |
| 2806 | */ |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 2807 | return rollback_single_pick(r); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2808 | } |
| 2809 | if (!f) |
Johannes Schindelin | f7ed195 | 2016-10-21 14:26:21 +0200 | [diff] [blame] | 2810 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 2811 | if (strbuf_getline_lf(&buf, f)) { |
Johannes Schindelin | f7ed195 | 2016-10-21 14:26:21 +0200 | [diff] [blame] | 2812 | error(_("cannot read '%s': %s"), git_path_head_file(), |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2813 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2814 | fclose(f); |
| 2815 | goto fail; |
| 2816 | } |
| 2817 | fclose(f); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2818 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2819 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2820 | git_path_head_file()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2821 | goto fail; |
| 2822 | } |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2823 | if (is_null_oid(&oid)) { |
Michael J Gruber | 0f974e2 | 2016-06-06 15:23:54 +0200 | [diff] [blame] | 2824 | error(_("cannot abort from a branch yet to be born")); |
| 2825 | goto fail; |
| 2826 | } |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2827 | |
| 2828 | if (!rollback_is_safe()) { |
| 2829 | /* Do not error, just do not rollback */ |
| 2830 | warning(_("You seem to have moved HEAD. " |
| 2831 | "Not rewinding, check your HEAD!")); |
| 2832 | } else |
Rohit Ashiwal | 918d1e6 | 2019-07-02 14:41:26 +0530 | [diff] [blame] | 2833 | if (reset_merge(&oid)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2834 | goto fail; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2835 | strbuf_release(&buf); |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 2836 | return sequencer_remove_state(opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2837 | fail: |
| 2838 | strbuf_release(&buf); |
| 2839 | return -1; |
| 2840 | } |
| 2841 | |
Rohit Ashiwal | de81ca3 | 2019-07-02 14:41:28 +0530 | [diff] [blame] | 2842 | int sequencer_skip(struct repository *r, struct replay_opts *opts) |
| 2843 | { |
| 2844 | enum replay_action action = -1; |
| 2845 | sequencer_get_last_command(r, &action); |
| 2846 | |
| 2847 | /* |
| 2848 | * Check whether the subcommand requested to skip the commit is actually |
| 2849 | * in progress and that it's safe to skip the commit. |
| 2850 | * |
| 2851 | * opts->action tells us which subcommand requested to skip the commit. |
| 2852 | * If the corresponding .git/<ACTION>_HEAD exists, we know that the |
| 2853 | * action is in progress and we can skip the commit. |
| 2854 | * |
| 2855 | * Otherwise we check that the last instruction was related to the |
| 2856 | * particular subcommand we're trying to execute and barf if that's not |
| 2857 | * the case. |
| 2858 | * |
| 2859 | * Finally we check that the rollback is "safe", i.e., has the HEAD |
| 2860 | * moved? In this case, it doesn't make sense to "reset the merge" and |
| 2861 | * "skip the commit" as the user already handled this by committing. But |
| 2862 | * we'd not want to barf here, instead give advice on how to proceed. We |
| 2863 | * only need to check that when .git/<ACTION>_HEAD doesn't exist because |
| 2864 | * it gets removed when the user commits, so if it still exists we're |
| 2865 | * sure the user can't have committed before. |
| 2866 | */ |
| 2867 | switch (opts->action) { |
| 2868 | case REPLAY_REVERT: |
| 2869 | if (!file_exists(git_path_revert_head(r))) { |
| 2870 | if (action != REPLAY_REVERT) |
| 2871 | return error(_("no revert in progress")); |
| 2872 | if (!rollback_is_safe()) |
| 2873 | goto give_advice; |
| 2874 | } |
| 2875 | break; |
| 2876 | case REPLAY_PICK: |
| 2877 | if (!file_exists(git_path_cherry_pick_head(r))) { |
| 2878 | if (action != REPLAY_PICK) |
| 2879 | return error(_("no cherry-pick in progress")); |
| 2880 | if (!rollback_is_safe()) |
| 2881 | goto give_advice; |
| 2882 | } |
| 2883 | break; |
| 2884 | default: |
| 2885 | BUG("unexpected action in sequencer_skip"); |
| 2886 | } |
| 2887 | |
| 2888 | if (skip_single_pick()) |
| 2889 | return error(_("failed to skip the commit")); |
| 2890 | if (!is_directory(git_path_seq_dir())) |
| 2891 | return 0; |
| 2892 | |
| 2893 | return sequencer_continue(r, opts); |
| 2894 | |
| 2895 | give_advice: |
| 2896 | error(_("there is nothing to skip")); |
| 2897 | |
| 2898 | if (advice_resolve_conflict) { |
| 2899 | advise(_("have you committed already?\n" |
| 2900 | "try \"git %s --continue\""), |
| 2901 | action == REPLAY_REVERT ? "revert" : "cherry-pick"); |
| 2902 | } |
| 2903 | return -1; |
| 2904 | } |
| 2905 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2906 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2907 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 2908 | struct lock_file todo_lock = LOCK_INIT; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2909 | const char *todo_path = get_todo_path(opts); |
| 2910 | int next = todo_list->current, offset, fd; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2911 | |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 2912 | /* |
| 2913 | * rebase -i writes "git-rebase-todo" without the currently executing |
| 2914 | * command, appending it to "done" instead. |
| 2915 | */ |
| 2916 | if (is_rebase_i(opts)) |
| 2917 | next++; |
| 2918 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2919 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 2920 | if (fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2921 | return error_errno(_("could not lock '%s'"), todo_path); |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2922 | offset = get_item_line_offset(todo_list, next); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2923 | if (write_in_full(fd, todo_list->buf.buf + offset, |
| 2924 | todo_list->buf.len - offset) < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2925 | return error_errno(_("could not write to '%s'"), todo_path); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2926 | if (commit_lock_file(&todo_lock) < 0) |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 2927 | return error(_("failed to finalize '%s'"), todo_path); |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2928 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2929 | if (is_rebase_i(opts) && next > 0) { |
| 2930 | const char *done = rebase_path_done(); |
| 2931 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); |
| 2932 | int ret = 0; |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2933 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2934 | if (fd < 0) |
| 2935 | return 0; |
| 2936 | if (write_in_full(fd, get_item_line(todo_list, next - 1), |
| 2937 | get_item_line_length(todo_list, next - 1)) |
| 2938 | < 0) |
| 2939 | ret = error_errno(_("could not write to '%s'"), done); |
| 2940 | if (close(fd) < 0) |
| 2941 | ret = error_errno(_("failed to finalize '%s'"), done); |
| 2942 | return ret; |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2943 | } |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 2944 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2945 | } |
| 2946 | |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2947 | static int save_opts(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2948 | { |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2949 | const char *opts_file = git_path_opts_file(); |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2950 | int res = 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2951 | |
| 2952 | if (opts->no_commit) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2953 | res |= git_config_set_in_file_gently(opts_file, |
| 2954 | "options.no-commit", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2955 | if (opts->edit) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2956 | res |= git_config_set_in_file_gently(opts_file, |
| 2957 | "options.edit", "true"); |
Phillip Wood | 6860ce5 | 2019-03-13 18:26:15 +0000 | [diff] [blame] | 2958 | if (opts->allow_empty) |
| 2959 | res |= git_config_set_in_file_gently(opts_file, |
| 2960 | "options.allow-empty", "true"); |
| 2961 | if (opts->allow_empty_message) |
| 2962 | res |= git_config_set_in_file_gently(opts_file, |
| 2963 | "options.allow-empty-message", "true"); |
| 2964 | if (opts->keep_redundant_commits) |
| 2965 | res |= git_config_set_in_file_gently(opts_file, |
| 2966 | "options.keep-redundant-commits", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2967 | if (opts->signoff) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2968 | res |= git_config_set_in_file_gently(opts_file, |
| 2969 | "options.signoff", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2970 | if (opts->record_origin) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2971 | res |= git_config_set_in_file_gently(opts_file, |
| 2972 | "options.record-origin", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2973 | if (opts->allow_ff) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2974 | res |= git_config_set_in_file_gently(opts_file, |
| 2975 | "options.allow-ff", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2976 | if (opts->mainline) { |
| 2977 | struct strbuf buf = STRBUF_INIT; |
| 2978 | strbuf_addf(&buf, "%d", opts->mainline); |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2979 | res |= git_config_set_in_file_gently(opts_file, |
| 2980 | "options.mainline", buf.buf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2981 | strbuf_release(&buf); |
| 2982 | } |
| 2983 | if (opts->strategy) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2984 | res |= git_config_set_in_file_gently(opts_file, |
| 2985 | "options.strategy", opts->strategy); |
Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 +0000 | [diff] [blame] | 2986 | if (opts->gpg_sign) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2987 | res |= git_config_set_in_file_gently(opts_file, |
| 2988 | "options.gpg-sign", opts->gpg_sign); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2989 | if (opts->xopts) { |
| 2990 | int i; |
| 2991 | for (i = 0; i < opts->xopts_nr; i++) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2992 | res |= git_config_set_multivar_in_file_gently(opts_file, |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2993 | "options.strategy-option", |
| 2994 | opts->xopts[i], "^$", 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2995 | } |
Phillip Wood | 8d8cb4b | 2017-08-02 11:44:19 +0100 | [diff] [blame] | 2996 | if (opts->allow_rerere_auto) |
Phillip Wood | f59199d | 2019-03-13 18:26:13 +0000 | [diff] [blame] | 2997 | res |= git_config_set_in_file_gently(opts_file, |
| 2998 | "options.allow-rerere-auto", |
| 2999 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? |
| 3000 | "true" : "false"); |
Phillip Wood | dc42e9a | 2019-04-17 11:23:29 +0100 | [diff] [blame] | 3001 | |
| 3002 | if (opts->explicit_cleanup) |
| 3003 | res |= git_config_set_in_file_gently(opts_file, |
| 3004 | "options.default-msg-cleanup", |
| 3005 | describe_cleanup_mode(opts->default_msg_cleanup)); |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 3006 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3007 | } |
| 3008 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3009 | static int make_patch(struct repository *r, |
| 3010 | struct commit *commit, |
| 3011 | struct replay_opts *opts) |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3012 | { |
| 3013 | struct strbuf buf = STRBUF_INIT; |
| 3014 | struct rev_info log_tree_opt; |
| 3015 | const char *subject, *p; |
| 3016 | int res = 0; |
| 3017 | |
| 3018 | p = short_commit_name(commit); |
| 3019 | if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) |
| 3020 | return -1; |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 3021 | res |= write_rebase_head(&commit->object.oid); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3022 | |
| 3023 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); |
| 3024 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3025 | repo_init_revisions(r, &log_tree_opt, NULL); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3026 | log_tree_opt.abbrev = 0; |
| 3027 | log_tree_opt.diff = 1; |
| 3028 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; |
| 3029 | log_tree_opt.disable_stdin = 1; |
| 3030 | log_tree_opt.no_commit_id = 1; |
| 3031 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); |
| 3032 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; |
| 3033 | if (!log_tree_opt.diffopt.file) |
| 3034 | res |= error_errno(_("could not open '%s'"), buf.buf); |
| 3035 | else { |
| 3036 | res |= log_tree_commit(&log_tree_opt, commit); |
| 3037 | fclose(log_tree_opt.diffopt.file); |
| 3038 | } |
| 3039 | strbuf_reset(&buf); |
| 3040 | |
| 3041 | strbuf_addf(&buf, "%s/message", get_dir(opts)); |
| 3042 | if (!file_exists(buf.buf)) { |
Doan Tran Cong Danh | 52f52e5 | 2019-11-11 13:03:41 +0700 | [diff] [blame] | 3043 | const char *encoding = get_commit_output_encoding(); |
| 3044 | const char *commit_buffer = logmsg_reencode(commit, NULL, encoding); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3045 | find_commit_subject(commit_buffer, &subject); |
| 3046 | res |= write_message(subject, strlen(subject), buf.buf, 1); |
| 3047 | unuse_commit_buffer(commit, commit_buffer); |
| 3048 | } |
| 3049 | strbuf_release(&buf); |
| 3050 | |
| 3051 | return res; |
| 3052 | } |
| 3053 | |
| 3054 | static int intend_to_amend(void) |
| 3055 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3056 | struct object_id head; |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3057 | char *p; |
| 3058 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3059 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3060 | return error(_("cannot read HEAD")); |
| 3061 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3062 | p = oid_to_hex(&head); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3063 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
| 3064 | } |
| 3065 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3066 | static int error_with_patch(struct repository *r, |
| 3067 | struct commit *commit, |
| 3068 | const char *subject, int subject_len, |
| 3069 | struct replay_opts *opts, |
| 3070 | int exit_code, int to_amend) |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3071 | { |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3072 | if (commit) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3073 | if (make_patch(r, commit, opts)) |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3074 | return -1; |
Junio C Hamano | 5a5c5e9 | 2018-08-20 12:41:33 -0700 | [diff] [blame] | 3075 | } else if (copy_file(rebase_path_message(), |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3076 | git_path_merge_msg(r), 0666)) |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3077 | return error(_("unable to copy '%s' to '%s'"), |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3078 | git_path_merge_msg(r), rebase_path_message()); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3079 | |
| 3080 | if (to_amend) { |
| 3081 | if (intend_to_amend()) |
| 3082 | return -1; |
| 3083 | |
Nguyễn Thái Ngọc Duy | 02127c6 | 2018-07-21 09:49:38 +0200 | [diff] [blame] | 3084 | fprintf(stderr, |
| 3085 | _("You can amend the commit now, with\n" |
| 3086 | "\n" |
| 3087 | " git commit --amend %s\n" |
| 3088 | "\n" |
| 3089 | "Once you are satisfied with your changes, run\n" |
| 3090 | "\n" |
| 3091 | " git rebase --continue\n"), |
| 3092 | gpg_sign_opt_quoted(opts)); |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3093 | } else if (exit_code) { |
| 3094 | if (commit) |
Junio C Hamano | 5a5c5e9 | 2018-08-20 12:41:33 -0700 | [diff] [blame] | 3095 | fprintf_ln(stderr, _("Could not apply %s... %.*s"), |
| 3096 | short_commit_name(commit), subject_len, subject); |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3097 | else |
| 3098 | /* |
| 3099 | * We don't have the hash of the parent so |
| 3100 | * just print the line from the todo file. |
| 3101 | */ |
Junio C Hamano | 5a5c5e9 | 2018-08-20 12:41:33 -0700 | [diff] [blame] | 3102 | fprintf_ln(stderr, _("Could not merge %.*s"), |
| 3103 | subject_len, subject); |
Phillip Wood | bc9238b | 2018-08-15 10:39:35 +0100 | [diff] [blame] | 3104 | } |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3105 | |
| 3106 | return exit_code; |
| 3107 | } |
| 3108 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3109 | static int error_failed_squash(struct repository *r, |
| 3110 | struct commit *commit, |
| 3111 | struct replay_opts *opts, |
| 3112 | int subject_len, |
| 3113 | const char *subject) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3114 | { |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 3115 | if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666)) |
| 3116 | return error(_("could not copy '%s' to '%s'"), |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3117 | rebase_path_squash_msg(), rebase_path_message()); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3118 | unlink(git_path_merge_msg(r)); |
| 3119 | if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666)) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3120 | return error(_("could not copy '%s' to '%s'"), |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 3121 | rebase_path_message(), |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3122 | git_path_merge_msg(r)); |
| 3123 | return error_with_patch(r, commit, subject, subject_len, opts, 1, 0); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3126 | static int do_exec(struct repository *r, const char *command_line) |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3127 | { |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 3128 | struct argv_array child_env = ARGV_ARRAY_INIT; |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3129 | const char *child_argv[] = { NULL, NULL }; |
| 3130 | int dirty, status; |
| 3131 | |
Alban Gruin | 4d55d63 | 2020-03-28 14:05:15 +0100 | [diff] [blame] | 3132 | fprintf(stderr, _("Executing: %s\n"), command_line); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3133 | child_argv[0] = command_line; |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 3134 | argv_array_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); |
brian m. carlson | ab5e67d | 2018-07-14 18:38:59 +0000 | [diff] [blame] | 3135 | argv_array_pushf(&child_env, "GIT_WORK_TREE=%s", |
| 3136 | absolute_path(get_git_work_tree())); |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 3137 | status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, |
| 3138 | child_env.argv); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3139 | |
| 3140 | /* force re-reading of the cache */ |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3141 | if (discard_index(r->index) < 0 || repo_read_index(r) < 0) |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3142 | return error(_("could not read index")); |
| 3143 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3144 | dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3145 | |
| 3146 | if (status) { |
| 3147 | warning(_("execution failed: %s\n%s" |
| 3148 | "You can fix the problem, and then run\n" |
| 3149 | "\n" |
| 3150 | " git rebase --continue\n" |
| 3151 | "\n"), |
| 3152 | command_line, |
| 3153 | dirty ? N_("and made changes to the index and/or the " |
| 3154 | "working tree\n") : ""); |
| 3155 | if (status == 127) |
| 3156 | /* command not found */ |
| 3157 | status = 1; |
| 3158 | } else if (dirty) { |
| 3159 | warning(_("execution succeeded: %s\nbut " |
| 3160 | "left changes to the index and/or the working tree\n" |
| 3161 | "Commit or stash your changes, and then run\n" |
| 3162 | "\n" |
| 3163 | " git rebase --continue\n" |
| 3164 | "\n"), command_line); |
| 3165 | status = 1; |
| 3166 | } |
| 3167 | |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 3168 | argv_array_clear(&child_env); |
| 3169 | |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3170 | return status; |
| 3171 | } |
| 3172 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3173 | static int safe_append(const char *filename, const char *fmt, ...) |
| 3174 | { |
| 3175 | va_list ap; |
| 3176 | struct lock_file lock = LOCK_INIT; |
| 3177 | int fd = hold_lock_file_for_update(&lock, filename, |
| 3178 | LOCK_REPORT_ON_ERROR); |
| 3179 | struct strbuf buf = STRBUF_INIT; |
| 3180 | |
| 3181 | if (fd < 0) |
| 3182 | return -1; |
| 3183 | |
| 3184 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { |
| 3185 | error_errno(_("could not read '%s'"), filename); |
| 3186 | rollback_lock_file(&lock); |
| 3187 | return -1; |
| 3188 | } |
| 3189 | strbuf_complete(&buf, '\n'); |
| 3190 | va_start(ap, fmt); |
| 3191 | strbuf_vaddf(&buf, fmt, ap); |
| 3192 | va_end(ap); |
| 3193 | |
| 3194 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 3195 | error_errno(_("could not write to '%s'"), filename); |
| 3196 | strbuf_release(&buf); |
| 3197 | rollback_lock_file(&lock); |
| 3198 | return -1; |
| 3199 | } |
| 3200 | if (commit_lock_file(&lock) < 0) { |
| 3201 | strbuf_release(&buf); |
| 3202 | rollback_lock_file(&lock); |
| 3203 | return error(_("failed to finalize '%s'"), filename); |
| 3204 | } |
| 3205 | |
| 3206 | strbuf_release(&buf); |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3210 | static int do_label(struct repository *r, const char *name, int len) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3211 | { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3212 | struct ref_store *refs = get_main_ref_store(r); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3213 | struct ref_transaction *transaction; |
| 3214 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; |
| 3215 | struct strbuf msg = STRBUF_INIT; |
| 3216 | int ret = 0; |
| 3217 | struct object_id head_oid; |
| 3218 | |
| 3219 | if (len == 1 && *name == '#') |
Nguyễn Thái Ngọc Duy | 02127c6 | 2018-07-21 09:49:38 +0200 | [diff] [blame] | 3220 | return error(_("illegal label name: '%.*s'"), len, name); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3221 | |
| 3222 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
Elijah Newren | c2417d3 | 2020-02-15 21:36:36 +0000 | [diff] [blame] | 3223 | strbuf_addf(&msg, "rebase (label) '%.*s'", len, name); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3224 | |
| 3225 | transaction = ref_store_transaction_begin(refs, &err); |
| 3226 | if (!transaction) { |
| 3227 | error("%s", err.buf); |
| 3228 | ret = -1; |
| 3229 | } else if (get_oid("HEAD", &head_oid)) { |
| 3230 | error(_("could not read HEAD")); |
| 3231 | ret = -1; |
| 3232 | } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, |
| 3233 | NULL, 0, msg.buf, &err) < 0 || |
| 3234 | ref_transaction_commit(transaction, &err)) { |
| 3235 | error("%s", err.buf); |
| 3236 | ret = -1; |
| 3237 | } |
| 3238 | ref_transaction_free(transaction); |
| 3239 | strbuf_release(&err); |
| 3240 | strbuf_release(&msg); |
| 3241 | |
| 3242 | if (!ret) |
| 3243 | ret = safe_append(rebase_path_refs_to_delete(), |
| 3244 | "%s\n", ref_name.buf); |
| 3245 | strbuf_release(&ref_name); |
| 3246 | |
| 3247 | return ret; |
| 3248 | } |
| 3249 | |
| 3250 | static const char *reflog_message(struct replay_opts *opts, |
| 3251 | const char *sub_action, const char *fmt, ...); |
| 3252 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3253 | static int do_reset(struct repository *r, |
| 3254 | const char *name, int len, |
| 3255 | struct replay_opts *opts) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3256 | { |
| 3257 | struct strbuf ref_name = STRBUF_INIT; |
| 3258 | struct object_id oid; |
| 3259 | struct lock_file lock = LOCK_INIT; |
| 3260 | struct tree_desc desc; |
| 3261 | struct tree *tree; |
| 3262 | struct unpack_trees_options unpack_tree_opts; |
Martin Ågren | 71571cd | 2018-10-30 09:09:37 +0100 | [diff] [blame] | 3263 | int ret = 0; |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3264 | |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 3265 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3266 | return -1; |
| 3267 | |
Johannes Schindelin | ebddf39 | 2018-05-04 01:01:23 +0200 | [diff] [blame] | 3268 | if (len == 10 && !strncmp("[new root]", name, len)) { |
| 3269 | if (!opts->have_squash_onto) { |
| 3270 | const char *hex; |
| 3271 | if (commit_tree("", 0, the_hash_algo->empty_tree, |
| 3272 | NULL, &opts->squash_onto, |
| 3273 | NULL, NULL)) |
| 3274 | return error(_("writing fake root commit")); |
| 3275 | opts->have_squash_onto = 1; |
| 3276 | hex = oid_to_hex(&opts->squash_onto); |
| 3277 | if (write_message(hex, strlen(hex), |
| 3278 | rebase_path_squash_onto(), 0)) |
| 3279 | return error(_("writing squash-onto")); |
| 3280 | } |
| 3281 | oidcpy(&oid, &opts->squash_onto); |
| 3282 | } else { |
Martin Ågren | 71571cd | 2018-10-30 09:09:37 +0100 | [diff] [blame] | 3283 | int i; |
| 3284 | |
Johannes Schindelin | ebddf39 | 2018-05-04 01:01:23 +0200 | [diff] [blame] | 3285 | /* Determine the length of the label */ |
| 3286 | for (i = 0; i < len; i++) |
| 3287 | if (isspace(name[i])) |
Martin Ågren | 71571cd | 2018-10-30 09:09:37 +0100 | [diff] [blame] | 3288 | break; |
| 3289 | len = i; |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3290 | |
Johannes Schindelin | ebddf39 | 2018-05-04 01:01:23 +0200 | [diff] [blame] | 3291 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
| 3292 | if (get_oid(ref_name.buf, &oid) && |
| 3293 | get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) { |
| 3294 | error(_("could not read '%s'"), ref_name.buf); |
| 3295 | rollback_lock_file(&lock); |
| 3296 | strbuf_release(&ref_name); |
| 3297 | return -1; |
| 3298 | } |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); |
| 3302 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); |
| 3303 | unpack_tree_opts.head_idx = 1; |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3304 | unpack_tree_opts.src_index = r->index; |
| 3305 | unpack_tree_opts.dst_index = r->index; |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3306 | unpack_tree_opts.fn = oneway_merge; |
| 3307 | unpack_tree_opts.merge = 1; |
| 3308 | unpack_tree_opts.update = 1; |
brian m. carlson | 3f26785 | 2020-03-16 18:05:06 +0000 | [diff] [blame] | 3309 | init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3310 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3311 | if (repo_read_index_unmerged(r)) { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3312 | rollback_lock_file(&lock); |
| 3313 | strbuf_release(&ref_name); |
| 3314 | return error_resolve_conflict(_(action_name(opts))); |
| 3315 | } |
| 3316 | |
Nguyễn Thái Ngọc Duy | 5e57580 | 2019-06-27 16:28:48 +0700 | [diff] [blame] | 3317 | if (!fill_tree_descriptor(r, &desc, &oid)) { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3318 | error(_("failed to find tree of %s"), oid_to_hex(&oid)); |
| 3319 | rollback_lock_file(&lock); |
| 3320 | free((void *)desc.buffer); |
| 3321 | strbuf_release(&ref_name); |
| 3322 | return -1; |
| 3323 | } |
| 3324 | |
| 3325 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { |
| 3326 | rollback_lock_file(&lock); |
| 3327 | free((void *)desc.buffer); |
| 3328 | strbuf_release(&ref_name); |
| 3329 | return -1; |
| 3330 | } |
| 3331 | |
| 3332 | tree = parse_tree_indirect(&oid); |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 3333 | prime_cache_tree(r, r->index, tree); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3334 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3335 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3336 | ret = error(_("could not write index")); |
| 3337 | free((void *)desc.buffer); |
| 3338 | |
| 3339 | if (!ret) |
| 3340 | ret = update_ref(reflog_message(opts, "reset", "'%.*s'", |
| 3341 | len, name), "HEAD", &oid, |
| 3342 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
| 3343 | |
| 3344 | strbuf_release(&ref_name); |
| 3345 | return ret; |
| 3346 | } |
| 3347 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3348 | static struct commit *lookup_label(const char *label, int len, |
| 3349 | struct strbuf *buf) |
| 3350 | { |
| 3351 | struct commit *commit; |
| 3352 | |
| 3353 | strbuf_reset(buf); |
| 3354 | strbuf_addf(buf, "refs/rewritten/%.*s", len, label); |
| 3355 | commit = lookup_commit_reference_by_name(buf->buf); |
| 3356 | if (!commit) { |
| 3357 | /* fall back to non-rewritten ref or commit */ |
| 3358 | strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0); |
| 3359 | commit = lookup_commit_reference_by_name(buf->buf); |
| 3360 | } |
| 3361 | |
| 3362 | if (!commit) |
| 3363 | error(_("could not resolve '%s'"), buf->buf); |
| 3364 | |
| 3365 | return commit; |
| 3366 | } |
| 3367 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3368 | static int do_merge(struct repository *r, |
| 3369 | struct commit *commit, |
| 3370 | const char *arg, int arg_len, |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3371 | int flags, struct replay_opts *opts) |
| 3372 | { |
| 3373 | int run_commit_flags = (flags & TODO_EDIT_MERGE_MSG) ? |
| 3374 | EDIT_MSG | VERIFY_MSG : 0; |
| 3375 | struct strbuf ref_name = STRBUF_INIT; |
| 3376 | struct commit *head_commit, *merge_commit, *i; |
| 3377 | struct commit_list *bases, *j, *reversed = NULL; |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3378 | struct commit_list *to_merge = NULL, **tail = &to_merge; |
Johannes Schindelin | e145d99 | 2019-07-31 08:18:47 -0700 | [diff] [blame] | 3379 | const char *strategy = !opts->xopts_nr && |
| 3380 | (!opts->strategy || !strcmp(opts->strategy, "recursive")) ? |
| 3381 | NULL : opts->strategy; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3382 | struct merge_options o; |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3383 | int merge_arg_len, oneline_offset, can_fast_forward, ret, k; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3384 | static struct lock_file lock; |
| 3385 | const char *p; |
| 3386 | |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 3387 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3388 | ret = -1; |
| 3389 | goto leave_merge; |
| 3390 | } |
| 3391 | |
| 3392 | head_commit = lookup_commit_reference_by_name("HEAD"); |
| 3393 | if (!head_commit) { |
| 3394 | ret = error(_("cannot merge without a current revision")); |
| 3395 | goto leave_merge; |
| 3396 | } |
| 3397 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3398 | /* |
| 3399 | * For octopus merges, the arg starts with the list of revisions to be |
| 3400 | * merged. The list is optionally followed by '#' and the oneline. |
| 3401 | */ |
| 3402 | merge_arg_len = oneline_offset = arg_len; |
| 3403 | for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) { |
| 3404 | if (!*p) |
| 3405 | break; |
| 3406 | if (*p == '#' && (!p[1] || isspace(p[1]))) { |
| 3407 | p += 1 + strspn(p + 1, " \t\n"); |
| 3408 | oneline_offset = p - arg; |
| 3409 | break; |
| 3410 | } |
| 3411 | k = strcspn(p, " \t\n"); |
| 3412 | if (!k) |
| 3413 | continue; |
| 3414 | merge_commit = lookup_label(p, k, &ref_name); |
| 3415 | if (!merge_commit) { |
| 3416 | ret = error(_("unable to parse '%.*s'"), k, p); |
| 3417 | goto leave_merge; |
| 3418 | } |
| 3419 | tail = &commit_list_insert(merge_commit, tail)->next; |
| 3420 | p += k; |
| 3421 | merge_arg_len = p - arg; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3424 | if (!to_merge) { |
| 3425 | ret = error(_("nothing to merge: '%.*s'"), arg_len, arg); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3426 | goto leave_merge; |
| 3427 | } |
| 3428 | |
Johannes Schindelin | 9c85a1c | 2018-05-04 01:01:28 +0200 | [diff] [blame] | 3429 | if (opts->have_squash_onto && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3430 | oideq(&head_commit->object.oid, &opts->squash_onto)) { |
Johannes Schindelin | 9c85a1c | 2018-05-04 01:01:28 +0200 | [diff] [blame] | 3431 | /* |
| 3432 | * When the user tells us to "merge" something into a |
| 3433 | * "[new root]", let's simply fast-forward to the merge head. |
| 3434 | */ |
| 3435 | rollback_lock_file(&lock); |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3436 | if (to_merge->next) |
| 3437 | ret = error(_("octopus merge cannot be executed on " |
| 3438 | "top of a [new root]")); |
| 3439 | else |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3440 | ret = fast_forward_to(r, &to_merge->item->object.oid, |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3441 | &head_commit->object.oid, 0, |
| 3442 | opts); |
Johannes Schindelin | 9c85a1c | 2018-05-04 01:01:28 +0200 | [diff] [blame] | 3443 | goto leave_merge; |
| 3444 | } |
| 3445 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3446 | if (commit) { |
Doan Tran Cong Danh | 5772b0c | 2019-11-11 13:03:40 +0700 | [diff] [blame] | 3447 | const char *encoding = get_commit_output_encoding(); |
| 3448 | const char *message = logmsg_reencode(commit, NULL, encoding); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3449 | const char *body; |
| 3450 | int len; |
| 3451 | |
| 3452 | if (!message) { |
| 3453 | ret = error(_("could not get commit message of '%s'"), |
| 3454 | oid_to_hex(&commit->object.oid)); |
| 3455 | goto leave_merge; |
| 3456 | } |
| 3457 | write_author_script(message); |
| 3458 | find_commit_subject(message, &body); |
| 3459 | len = strlen(body); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3460 | ret = write_message(body, len, git_path_merge_msg(r), 0); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3461 | unuse_commit_buffer(commit, message); |
| 3462 | if (ret) { |
| 3463 | error_errno(_("could not write '%s'"), |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3464 | git_path_merge_msg(r)); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3465 | goto leave_merge; |
| 3466 | } |
| 3467 | } else { |
| 3468 | struct strbuf buf = STRBUF_INIT; |
| 3469 | int len; |
| 3470 | |
| 3471 | strbuf_addf(&buf, "author %s", git_author_info(0)); |
| 3472 | write_author_script(buf.buf); |
| 3473 | strbuf_reset(&buf); |
| 3474 | |
| 3475 | if (oneline_offset < arg_len) { |
| 3476 | p = arg + oneline_offset; |
| 3477 | len = arg_len - oneline_offset; |
| 3478 | } else { |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3479 | strbuf_addf(&buf, "Merge %s '%.*s'", |
| 3480 | to_merge->next ? "branches" : "branch", |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3481 | merge_arg_len, arg); |
| 3482 | p = buf.buf; |
| 3483 | len = buf.len; |
| 3484 | } |
| 3485 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3486 | ret = write_message(p, len, git_path_merge_msg(r), 0); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3487 | strbuf_release(&buf); |
| 3488 | if (ret) { |
| 3489 | error_errno(_("could not write '%s'"), |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3490 | git_path_merge_msg(r)); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3491 | goto leave_merge; |
| 3492 | } |
| 3493 | } |
| 3494 | |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3495 | /* |
| 3496 | * If HEAD is not identical to the first parent of the original merge |
| 3497 | * commit, we cannot fast-forward. |
| 3498 | */ |
| 3499 | can_fast_forward = opts->allow_ff && commit && commit->parents && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3500 | oideq(&commit->parents->item->object.oid, |
| 3501 | &head_commit->object.oid); |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3502 | |
| 3503 | /* |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3504 | * If any merge head is different from the original one, we cannot |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3505 | * fast-forward. |
| 3506 | */ |
| 3507 | if (can_fast_forward) { |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3508 | struct commit_list *p = commit->parents->next; |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3509 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3510 | for (j = to_merge; j && p; j = j->next, p = p->next) |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 3511 | if (!oideq(&j->item->object.oid, |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3512 | &p->item->object.oid)) { |
| 3513 | can_fast_forward = 0; |
| 3514 | break; |
| 3515 | } |
| 3516 | /* |
| 3517 | * If the number of merge heads differs from the original merge |
| 3518 | * commit, we cannot fast-forward. |
| 3519 | */ |
| 3520 | if (j || p) |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3521 | can_fast_forward = 0; |
| 3522 | } |
| 3523 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3524 | if (can_fast_forward) { |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3525 | rollback_lock_file(&lock); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3526 | ret = fast_forward_to(r, &commit->object.oid, |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3527 | &head_commit->object.oid, 0, opts); |
Phillip Wood | 6df8df0 | 2019-05-02 11:22:49 +0100 | [diff] [blame] | 3528 | if (flags & TODO_EDIT_MERGE_MSG) { |
| 3529 | run_commit_flags |= AMEND_MSG; |
| 3530 | goto fast_forward_edit; |
| 3531 | } |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 3532 | goto leave_merge; |
| 3533 | } |
| 3534 | |
Johannes Schindelin | e145d99 | 2019-07-31 08:18:47 -0700 | [diff] [blame] | 3535 | if (strategy || to_merge->next) { |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3536 | /* Octopus merge */ |
| 3537 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 3538 | |
| 3539 | if (read_env_script(&cmd.env_array)) { |
| 3540 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
| 3541 | |
| 3542 | ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); |
| 3543 | goto leave_merge; |
| 3544 | } |
| 3545 | |
| 3546 | cmd.git_cmd = 1; |
| 3547 | argv_array_push(&cmd.args, "merge"); |
| 3548 | argv_array_push(&cmd.args, "-s"); |
Johannes Schindelin | e145d99 | 2019-07-31 08:18:47 -0700 | [diff] [blame] | 3549 | if (!strategy) |
| 3550 | argv_array_push(&cmd.args, "octopus"); |
| 3551 | else { |
| 3552 | argv_array_push(&cmd.args, strategy); |
| 3553 | for (k = 0; k < opts->xopts_nr; k++) |
| 3554 | argv_array_pushf(&cmd.args, |
| 3555 | "-X%s", opts->xopts[k]); |
| 3556 | } |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3557 | argv_array_push(&cmd.args, "--no-edit"); |
| 3558 | argv_array_push(&cmd.args, "--no-ff"); |
| 3559 | argv_array_push(&cmd.args, "--no-log"); |
| 3560 | argv_array_push(&cmd.args, "--no-stat"); |
| 3561 | argv_array_push(&cmd.args, "-F"); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3562 | argv_array_push(&cmd.args, git_path_merge_msg(r)); |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3563 | if (opts->gpg_sign) |
| 3564 | argv_array_push(&cmd.args, opts->gpg_sign); |
| 3565 | |
| 3566 | /* Add the tips to be merged */ |
| 3567 | for (j = to_merge; j; j = j->next) |
| 3568 | argv_array_push(&cmd.args, |
| 3569 | oid_to_hex(&j->item->object.oid)); |
| 3570 | |
| 3571 | strbuf_release(&ref_name); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3572 | unlink(git_path_cherry_pick_head(r)); |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3573 | rollback_lock_file(&lock); |
| 3574 | |
| 3575 | rollback_lock_file(&lock); |
| 3576 | ret = run_command(&cmd); |
| 3577 | |
| 3578 | /* force re-reading of the cache */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3579 | if (!ret && (discard_index(r->index) < 0 || |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3580 | repo_read_index(r) < 0)) |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3581 | ret = error(_("could not read index")); |
| 3582 | goto leave_merge; |
| 3583 | } |
| 3584 | |
| 3585 | merge_commit = to_merge->item; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3586 | bases = get_merge_bases(head_commit, merge_commit); |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3587 | if (bases && oideq(&merge_commit->object.oid, |
| 3588 | &bases->item->object.oid)) { |
Johannes Schindelin | 7ccdf65 | 2018-04-25 14:29:31 +0200 | [diff] [blame] | 3589 | ret = 0; |
| 3590 | /* skip merging an ancestor of HEAD */ |
| 3591 | goto leave_merge; |
| 3592 | } |
| 3593 | |
brian m. carlson | 4439c7a | 2019-08-18 20:04:16 +0000 | [diff] [blame] | 3594 | write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz, |
Junio C Hamano | cde5554 | 2019-01-04 13:33:33 -0800 | [diff] [blame] | 3595 | git_path_merge_head(r), 0); |
| 3596 | write_message("no-ff", 5, git_path_merge_mode(r), 0); |
Johannes Schindelin | 85f8d9d | 2018-11-12 15:25:58 -0800 | [diff] [blame] | 3597 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3598 | for (j = bases; j; j = j->next) |
| 3599 | commit_list_insert(j->item, &reversed); |
| 3600 | free_commit_list(bases); |
| 3601 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3602 | repo_read_index(r); |
Nguyễn Thái Ngọc Duy | 0d6caa2 | 2019-01-12 09:13:29 +0700 | [diff] [blame] | 3603 | init_merge_options(&o, r); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3604 | o.branch1 = "HEAD"; |
| 3605 | o.branch2 = ref_name.buf; |
| 3606 | o.buffer_output = 2; |
| 3607 | |
| 3608 | ret = merge_recursive(&o, head_commit, merge_commit, reversed, &i); |
| 3609 | if (ret <= 0) |
| 3610 | fputs(o.obuf.buf, stdout); |
| 3611 | strbuf_release(&o.obuf); |
| 3612 | if (ret < 0) { |
| 3613 | error(_("could not even attempt to merge '%.*s'"), |
| 3614 | merge_arg_len, arg); |
| 3615 | goto leave_merge; |
| 3616 | } |
| 3617 | /* |
| 3618 | * The return value of merge_recursive() is 1 on clean, and 0 on |
| 3619 | * unclean merge. |
| 3620 | * |
| 3621 | * Let's reverse that, so that do_merge() returns 0 upon success and |
| 3622 | * 1 upon failed merge (keeping the return value -1 for the cases where |
| 3623 | * we will want to reschedule the `merge` command). |
| 3624 | */ |
| 3625 | ret = !ret; |
| 3626 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3627 | if (r->index->cache_changed && |
| 3628 | write_locked_index(r->index, &lock, COMMIT_LOCK)) { |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3629 | ret = error(_("merge: Unable to write new index file")); |
| 3630 | goto leave_merge; |
| 3631 | } |
| 3632 | |
| 3633 | rollback_lock_file(&lock); |
| 3634 | if (ret) |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3635 | repo_rerere(r, opts->allow_rerere_auto); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3636 | else |
| 3637 | /* |
| 3638 | * In case of problems, we now want to return a positive |
| 3639 | * value (a negative one would indicate that the `merge` |
| 3640 | * command needs to be rescheduled). |
| 3641 | */ |
Phillip Wood | 6df8df0 | 2019-05-02 11:22:49 +0100 | [diff] [blame] | 3642 | fast_forward_edit: |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3643 | ret = !!run_git_commit(r, git_path_merge_msg(r), opts, |
| 3644 | run_commit_flags); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3645 | |
| 3646 | leave_merge: |
| 3647 | strbuf_release(&ref_name); |
| 3648 | rollback_lock_file(&lock); |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 3649 | free_commit_list(to_merge); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3650 | return ret; |
| 3651 | } |
| 3652 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3653 | static int is_final_fixup(struct todo_list *todo_list) |
| 3654 | { |
| 3655 | int i = todo_list->current; |
| 3656 | |
| 3657 | if (!is_fixup(todo_list->items[i].command)) |
| 3658 | return 0; |
| 3659 | |
| 3660 | while (++i < todo_list->nr) |
| 3661 | if (is_fixup(todo_list->items[i].command)) |
| 3662 | return 0; |
| 3663 | else if (!is_noop(todo_list->items[i].command)) |
| 3664 | break; |
| 3665 | return 1; |
| 3666 | } |
| 3667 | |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3668 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
| 3669 | { |
| 3670 | int i; |
| 3671 | |
| 3672 | for (i = todo_list->current + offset; i < todo_list->nr; i++) |
| 3673 | if (!is_noop(todo_list->items[i].command)) |
| 3674 | return todo_list->items[i].command; |
| 3675 | |
| 3676 | return -1; |
| 3677 | } |
| 3678 | |
Denton Liu | 0816f1d | 2020-04-07 10:28:03 -0400 | [diff] [blame] | 3679 | void create_autostash(struct repository *r, const char *path, |
| 3680 | const char *default_reflog_action) |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3681 | { |
Denton Liu | 0816f1d | 2020-04-07 10:28:03 -0400 | [diff] [blame] | 3682 | struct strbuf buf = STRBUF_INIT; |
| 3683 | struct lock_file lock_file = LOCK_INIT; |
| 3684 | int fd; |
| 3685 | |
| 3686 | fd = repo_hold_locked_index(r, &lock_file, 0); |
| 3687 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 3688 | if (0 <= fd) |
| 3689 | repo_update_index_if_able(r, &lock_file); |
| 3690 | rollback_lock_file(&lock_file); |
| 3691 | |
| 3692 | if (has_unstaged_changes(r, 1) || |
| 3693 | has_uncommitted_changes(r, 1)) { |
| 3694 | struct child_process stash = CHILD_PROCESS_INIT; |
| 3695 | struct object_id oid; |
| 3696 | |
| 3697 | argv_array_pushl(&stash.args, |
| 3698 | "stash", "create", "autostash", NULL); |
| 3699 | stash.git_cmd = 1; |
| 3700 | stash.no_stdin = 1; |
| 3701 | strbuf_reset(&buf); |
| 3702 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) |
| 3703 | die(_("Cannot autostash")); |
| 3704 | strbuf_trim_trailing_newline(&buf); |
| 3705 | if (get_oid(buf.buf, &oid)) |
| 3706 | die(_("Unexpected stash response: '%s'"), |
| 3707 | buf.buf); |
| 3708 | strbuf_reset(&buf); |
| 3709 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); |
| 3710 | |
| 3711 | if (safe_create_leading_directories_const(path)) |
| 3712 | die(_("Could not create directory for '%s'"), |
| 3713 | path); |
| 3714 | write_file(path, "%s", oid_to_hex(&oid)); |
| 3715 | printf(_("Created autostash: %s\n"), buf.buf); |
| 3716 | if (reset_head(r, NULL, "reset --hard", |
| 3717 | NULL, RESET_HEAD_HARD, NULL, NULL, |
| 3718 | default_reflog_action) < 0) |
| 3719 | die(_("could not reset --hard")); |
| 3720 | |
| 3721 | if (discard_index(r->index) < 0 || |
| 3722 | repo_read_index(r) < 0) |
| 3723 | die(_("could not read index")); |
| 3724 | } |
| 3725 | strbuf_release(&buf); |
| 3726 | } |
| 3727 | |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3728 | static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3729 | { |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3730 | struct child_process child = CHILD_PROCESS_INIT; |
| 3731 | int ret = 0; |
| 3732 | |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3733 | if (attempt_apply) { |
| 3734 | child.git_cmd = 1; |
| 3735 | child.no_stdout = 1; |
| 3736 | child.no_stderr = 1; |
| 3737 | argv_array_push(&child.args, "stash"); |
| 3738 | argv_array_push(&child.args, "apply"); |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3739 | argv_array_push(&child.args, stash_oid); |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3740 | ret = run_command(&child); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3741 | } |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3742 | |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3743 | if (attempt_apply && !ret) |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 3744 | fprintf(stderr, _("Applied autostash.\n")); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3745 | else { |
| 3746 | struct child_process store = CHILD_PROCESS_INIT; |
| 3747 | |
| 3748 | store.git_cmd = 1; |
| 3749 | argv_array_push(&store.args, "stash"); |
| 3750 | argv_array_push(&store.args, "store"); |
| 3751 | argv_array_push(&store.args, "-m"); |
| 3752 | argv_array_push(&store.args, "autostash"); |
| 3753 | argv_array_push(&store.args, "-q"); |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3754 | argv_array_push(&store.args, stash_oid); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3755 | if (run_command(&store)) |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3756 | ret = error(_("cannot store %s"), stash_oid); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3757 | else |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 3758 | fprintf(stderr, |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3759 | _("%s\n" |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 3760 | "Your changes are safe in the stash.\n" |
| 3761 | "You can run \"git stash pop\" or" |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3762 | " \"git stash drop\" at any time.\n"), |
| 3763 | attempt_apply ? |
| 3764 | _("Applying autostash resulted in conflicts.") : |
| 3765 | _("Autostash exists; creating a new stash entry.")); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3768 | return ret; |
| 3769 | } |
| 3770 | |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3771 | static int apply_save_autostash(const char *path, int attempt_apply) |
| 3772 | { |
| 3773 | struct strbuf stash_oid = STRBUF_INIT; |
| 3774 | int ret = 0; |
| 3775 | |
| 3776 | if (!read_oneliner(&stash_oid, path, |
| 3777 | READ_ONELINER_SKIP_IF_EMPTY)) { |
| 3778 | strbuf_release(&stash_oid); |
| 3779 | return 0; |
| 3780 | } |
| 3781 | strbuf_trim(&stash_oid); |
| 3782 | |
| 3783 | ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply); |
| 3784 | |
Denton Liu | 0dd562e | 2020-04-07 10:28:04 -0400 | [diff] [blame] | 3785 | unlink(path); |
Denton Liu | facca7f | 2020-04-07 10:27:57 -0400 | [diff] [blame] | 3786 | strbuf_release(&stash_oid); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3787 | return ret; |
| 3788 | } |
| 3789 | |
Denton Liu | 12b6e13 | 2020-04-07 10:28:05 -0400 | [diff] [blame] | 3790 | int save_autostash(const char *path) |
| 3791 | { |
| 3792 | return apply_save_autostash(path, 0); |
| 3793 | } |
| 3794 | |
| 3795 | int apply_autostash(const char *path) |
| 3796 | { |
| 3797 | return apply_save_autostash(path, 1); |
| 3798 | } |
| 3799 | |
Denton Liu | 804fe31 | 2020-04-07 10:28:06 -0400 | [diff] [blame] | 3800 | int apply_autostash_oid(const char *stash_oid) |
| 3801 | { |
| 3802 | return apply_save_autostash_oid(stash_oid, 1); |
| 3803 | } |
| 3804 | |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3805 | static const char *reflog_message(struct replay_opts *opts, |
| 3806 | const char *sub_action, const char *fmt, ...) |
| 3807 | { |
| 3808 | va_list ap; |
| 3809 | static struct strbuf buf = STRBUF_INIT; |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3810 | char *reflog_action = getenv(GIT_REFLOG_ACTION); |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3811 | |
| 3812 | va_start(ap, fmt); |
| 3813 | strbuf_reset(&buf); |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3814 | strbuf_addstr(&buf, reflog_action ? reflog_action : action_name(opts)); |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3815 | if (sub_action) |
| 3816 | strbuf_addf(&buf, " (%s)", sub_action); |
| 3817 | if (fmt) { |
| 3818 | strbuf_addstr(&buf, ": "); |
| 3819 | strbuf_vaddf(&buf, fmt, ap); |
| 3820 | } |
| 3821 | va_end(ap); |
| 3822 | |
| 3823 | return buf.buf; |
| 3824 | } |
| 3825 | |
Phillip Wood | fc4a673 | 2019-03-19 19:03:07 +0000 | [diff] [blame] | 3826 | static int run_git_checkout(struct repository *r, struct replay_opts *opts, |
| 3827 | const char *commit, const char *action) |
Alban Gruin | 2c58483 | 2018-08-10 18:51:33 +0200 | [diff] [blame] | 3828 | { |
| 3829 | struct child_process cmd = CHILD_PROCESS_INIT; |
Phillip Wood | fc4a673 | 2019-03-19 19:03:07 +0000 | [diff] [blame] | 3830 | int ret; |
Alban Gruin | 2c58483 | 2018-08-10 18:51:33 +0200 | [diff] [blame] | 3831 | |
| 3832 | cmd.git_cmd = 1; |
| 3833 | |
| 3834 | argv_array_push(&cmd.args, "checkout"); |
| 3835 | argv_array_push(&cmd.args, commit); |
| 3836 | argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action); |
| 3837 | |
| 3838 | if (opts->verbose) |
Phillip Wood | fc4a673 | 2019-03-19 19:03:07 +0000 | [diff] [blame] | 3839 | ret = run_command(&cmd); |
Alban Gruin | 2c58483 | 2018-08-10 18:51:33 +0200 | [diff] [blame] | 3840 | else |
Phillip Wood | fc4a673 | 2019-03-19 19:03:07 +0000 | [diff] [blame] | 3841 | ret = run_command_silent_on_success(&cmd); |
| 3842 | |
| 3843 | if (!ret) |
| 3844 | discard_index(r->index); |
| 3845 | |
| 3846 | return ret; |
Alban Gruin | 2c58483 | 2018-08-10 18:51:33 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
Phillip Wood | fc4a673 | 2019-03-19 19:03:07 +0000 | [diff] [blame] | 3849 | static int checkout_onto(struct repository *r, struct replay_opts *opts, |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 3850 | const char *onto_name, const struct object_id *onto, |
Alban Gruin | 91f0d95 | 2018-08-28 14:10:37 +0200 | [diff] [blame] | 3851 | const char *orig_head) |
Alban Gruin | 4df66c4 | 2018-08-10 18:51:34 +0200 | [diff] [blame] | 3852 | { |
| 3853 | struct object_id oid; |
| 3854 | const char *action = reflog_message(opts, "start", "checkout %s", onto_name); |
| 3855 | |
| 3856 | if (get_oid(orig_head, &oid)) |
| 3857 | return error(_("%s: not a valid OID"), orig_head); |
| 3858 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 3859 | if (run_git_checkout(r, opts, oid_to_hex(onto), action)) { |
Denton Liu | be1bb60 | 2020-04-07 10:27:56 -0400 | [diff] [blame] | 3860 | apply_autostash(rebase_path_autostash()); |
Alban Gruin | 4df66c4 | 2018-08-10 18:51:34 +0200 | [diff] [blame] | 3861 | sequencer_remove_state(opts); |
| 3862 | return error(_("could not detach HEAD")); |
| 3863 | } |
| 3864 | |
| 3865 | return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
| 3866 | } |
| 3867 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 3868 | static int stopped_at_head(struct repository *r) |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 3869 | { |
| 3870 | struct object_id head; |
| 3871 | struct commit *commit; |
| 3872 | struct commit_message message; |
| 3873 | |
Junio C Hamano | 789b1f7 | 2018-11-02 11:04:58 +0900 | [diff] [blame] | 3874 | if (get_oid("HEAD", &head) || |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 3875 | !(commit = lookup_commit(r, &head)) || |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 3876 | parse_commit(commit) || get_message(commit, &message)) |
| 3877 | fprintf(stderr, _("Stopped at HEAD\n")); |
| 3878 | else { |
| 3879 | fprintf(stderr, _("Stopped at %s\n"), message.label); |
| 3880 | free_message(commit, &message); |
| 3881 | } |
| 3882 | return 0; |
| 3883 | |
| 3884 | } |
| 3885 | |
Johannes Schindelin | cb5206e | 2018-04-25 14:28:33 +0200 | [diff] [blame] | 3886 | static const char rescheduled_advice[] = |
| 3887 | N_("Could not execute the todo command\n" |
| 3888 | "\n" |
| 3889 | " %.*s" |
| 3890 | "\n" |
| 3891 | "It has been rescheduled; To edit the command before continuing, please\n" |
| 3892 | "edit the todo list first:\n" |
| 3893 | "\n" |
| 3894 | " git rebase --edit-todo\n" |
| 3895 | " git rebase --continue\n"); |
| 3896 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3897 | static int pick_commits(struct repository *r, |
| 3898 | struct todo_list *todo_list, |
| 3899 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3900 | { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3901 | int res = 0, reschedule = 0; |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3902 | char *prev_reflog_action; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3903 | |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3904 | /* Note that 0 for 3rd parameter of setenv means set only if not set */ |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3905 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3906 | prev_reflog_action = xstrdup(getenv(GIT_REFLOG_ACTION)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3907 | if (opts->allow_ff) |
| 3908 | assert(!(opts->signoff || opts->no_commit || |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 3909 | opts->record_origin || opts->edit)); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3910 | if (read_and_refresh_cache(r, opts)) |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 3911 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3912 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3913 | while (todo_list->current < todo_list->nr) { |
| 3914 | struct todo_item *item = todo_list->items + todo_list->current; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 3915 | const char *arg = todo_item_get_arg(todo_list, item); |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 3916 | int check_todo = 0; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 3917 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3918 | if (save_todo(todo_list, opts)) |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 3919 | return -1; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3920 | if (is_rebase_i(opts)) { |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 3921 | if (item->command != TODO_COMMENT) { |
| 3922 | FILE *f = fopen(rebase_path_msgnum(), "w"); |
| 3923 | |
| 3924 | todo_list->done_nr++; |
| 3925 | |
| 3926 | if (f) { |
| 3927 | fprintf(f, "%d\n", todo_list->done_nr); |
| 3928 | fclose(f); |
| 3929 | } |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 3930 | if (!opts->quiet) |
Alban Gruin | 4d55d63 | 2020-03-28 14:05:15 +0100 | [diff] [blame] | 3931 | fprintf(stderr, _("Rebasing (%d/%d)%s"), |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 3932 | todo_list->done_nr, |
| 3933 | todo_list->total_nr, |
| 3934 | opts->verbose ? "\n" : "\r"); |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 3935 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3936 | unlink(rebase_path_message()); |
| 3937 | unlink(rebase_path_author_script()); |
| 3938 | unlink(rebase_path_stopped_sha()); |
| 3939 | unlink(rebase_path_amend()); |
Nguyễn Thái Ngọc Duy | 34e7771 | 2019-06-27 16:28:52 +0700 | [diff] [blame] | 3940 | unlink(git_path_merge_head(r)); |
Nguyễn Thái Ngọc Duy | fbd7a23 | 2018-02-11 16:43:28 +0700 | [diff] [blame] | 3941 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 3942 | |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 3943 | if (item->command == TODO_BREAK) { |
| 3944 | if (!opts->verbose) |
| 3945 | term_clear_line(); |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 3946 | return stopped_at_head(r); |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 3947 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3948 | } |
| 3949 | if (item->command <= TODO_SQUASH) { |
Johannes Schindelin | 8ab37ef | 2017-01-02 16:28:13 +0100 | [diff] [blame] | 3950 | if (is_rebase_i(opts)) |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3951 | setenv(GIT_REFLOG_ACTION, reflog_message(opts, |
Johannes Schindelin | 8ab37ef | 2017-01-02 16:28:13 +0100 | [diff] [blame] | 3952 | command_to_string(item->command), NULL), |
| 3953 | 1); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3954 | res = do_pick_commit(r, item->command, item->commit, |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 3955 | opts, is_final_fixup(todo_list), |
| 3956 | &check_todo); |
Elijah Newren | 1f6965f | 2020-04-07 16:59:23 +0000 | [diff] [blame] | 3957 | if (is_rebase_i(opts)) |
| 3958 | setenv(GIT_REFLOG_ACTION, prev_reflog_action, 1); |
Johannes Schindelin | 9d7bf3c | 2017-01-02 16:28:34 +0100 | [diff] [blame] | 3959 | if (is_rebase_i(opts) && res < 0) { |
| 3960 | /* Reschedule */ |
Johannes Schindelin | cb5206e | 2018-04-25 14:28:33 +0200 | [diff] [blame] | 3961 | advise(_(rescheduled_advice), |
| 3962 | get_item_line_length(todo_list, |
| 3963 | todo_list->current), |
| 3964 | get_item_line(todo_list, |
| 3965 | todo_list->current)); |
Johannes Schindelin | 9d7bf3c | 2017-01-02 16:28:34 +0100 | [diff] [blame] | 3966 | todo_list->current--; |
| 3967 | if (save_todo(todo_list, opts)) |
| 3968 | return -1; |
| 3969 | } |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3970 | if (item->command == TODO_EDIT) { |
| 3971 | struct commit *commit = item->commit; |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 3972 | if (!res) { |
| 3973 | if (!opts->verbose) |
| 3974 | term_clear_line(); |
Jeff King | 9942921 | 2017-03-16 20:19:42 -0400 | [diff] [blame] | 3975 | fprintf(stderr, |
Brandon Williams | a42e1b4 | 2017-03-23 10:02:33 -0700 | [diff] [blame] | 3976 | _("Stopped at %s... %.*s\n"), |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3977 | short_commit_name(commit), |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 3978 | item->arg_len, arg); |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 3979 | } |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3980 | return error_with_patch(r, commit, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 3981 | arg, item->arg_len, opts, res, !res); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3982 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3983 | if (is_rebase_i(opts) && !res) |
| 3984 | record_in_rewritten(&item->commit->object.oid, |
| 3985 | peek_command(todo_list, 1)); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3986 | if (res && is_fixup(item->command)) { |
| 3987 | if (res == 1) |
| 3988 | intend_to_amend(); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 3989 | return error_failed_squash(r, item->commit, opts, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 3990 | item->arg_len, arg); |
Phillip Wood | a9279c6 | 2018-06-19 13:46:51 +0100 | [diff] [blame] | 3991 | } else if (res && is_rebase_i(opts) && item->commit) { |
| 3992 | int to_amend = 0; |
| 3993 | struct object_id oid; |
| 3994 | |
| 3995 | /* |
| 3996 | * If we are rewording and have either |
| 3997 | * fast-forwarded already, or are about to |
| 3998 | * create a new root commit, we want to amend, |
| 3999 | * otherwise we do not. |
| 4000 | */ |
| 4001 | if (item->command == TODO_REWORD && |
| 4002 | !get_oid("HEAD", &oid) && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 4003 | (oideq(&item->commit->object.oid, &oid) || |
Phillip Wood | a9279c6 | 2018-06-19 13:46:51 +0100 | [diff] [blame] | 4004 | (opts->have_squash_onto && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 4005 | oideq(&opts->squash_onto, &oid)))) |
Phillip Wood | a9279c6 | 2018-06-19 13:46:51 +0100 | [diff] [blame] | 4006 | to_amend = 1; |
| 4007 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4008 | return res | error_with_patch(r, item->commit, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4009 | arg, item->arg_len, opts, |
Phillip Wood | a9279c6 | 2018-06-19 13:46:51 +0100 | [diff] [blame] | 4010 | res, to_amend); |
| 4011 | } |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 4012 | } else if (item->command == TODO_EXEC) { |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4013 | char *end_of_arg = (char *)(arg + item->arg_len); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 4014 | int saved = *end_of_arg; |
| 4015 | |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 4016 | if (!opts->verbose) |
| 4017 | term_clear_line(); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 4018 | *end_of_arg = '\0'; |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4019 | res = do_exec(r, arg); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 4020 | *end_of_arg = saved; |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 4021 | |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 4022 | if (res) { |
| 4023 | if (opts->reschedule_failed_exec) |
| 4024 | reschedule = 1; |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 4025 | } |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 4026 | check_todo = 1; |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 4027 | } else if (item->command == TODO_LABEL) { |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4028 | if ((res = do_label(r, arg, item->arg_len))) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 4029 | reschedule = 1; |
| 4030 | } else if (item->command == TODO_RESET) { |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4031 | if ((res = do_reset(r, arg, item->arg_len, opts))) |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 4032 | reschedule = 1; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4033 | } else if (item->command == TODO_MERGE) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4034 | if ((res = do_merge(r, item->commit, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4035 | arg, item->arg_len, |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4036 | item->flags, opts)) < 0) |
| 4037 | reschedule = 1; |
Johannes Schindelin | 537e7d6 | 2018-04-25 14:29:29 +0200 | [diff] [blame] | 4038 | else if (item->commit) |
| 4039 | record_in_rewritten(&item->commit->object.oid, |
| 4040 | peek_command(todo_list, 1)); |
| 4041 | if (res > 0) |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4042 | /* failed with merge conflicts */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4043 | return error_with_patch(r, item->commit, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4044 | arg, item->arg_len, |
| 4045 | opts, res, 0); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 4046 | } else if (!is_noop(item->command)) |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 4047 | return error(_("unknown command %d"), item->command); |
| 4048 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 4049 | if (reschedule) { |
| 4050 | advise(_(rescheduled_advice), |
| 4051 | get_item_line_length(todo_list, |
| 4052 | todo_list->current), |
| 4053 | get_item_line(todo_list, todo_list->current)); |
| 4054 | todo_list->current--; |
| 4055 | if (save_todo(todo_list, opts)) |
| 4056 | return -1; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4057 | if (item->commit) |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4058 | return error_with_patch(r, |
| 4059 | item->commit, |
Alban Gruin | 6ad656d | 2019-01-29 16:01:46 +0100 | [diff] [blame] | 4060 | arg, item->arg_len, |
| 4061 | opts, res, 0); |
SZEDER Gábor | befd4f6 | 2019-11-23 18:20:46 +0100 | [diff] [blame] | 4062 | } else if (is_rebase_i(opts) && check_todo && !res) { |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 4063 | struct stat st; |
| 4064 | |
| 4065 | if (stat(get_todo_path(opts), &st)) { |
| 4066 | res = error_errno(_("could not stat '%s'"), |
| 4067 | get_todo_path(opts)); |
| 4068 | } else if (match_stat_data(&todo_list->stat, &st)) { |
| 4069 | /* Reread the todo file if it has changed. */ |
| 4070 | todo_list_release(todo_list); |
| 4071 | if (read_populate_todo(r, todo_list, opts)) |
| 4072 | res = -1; /* message was printed */ |
| 4073 | /* `current` will be incremented below */ |
| 4074 | todo_list->current = -1; |
| 4075 | } |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 4076 | } |
| 4077 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4078 | todo_list->current++; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4079 | if (res) |
| 4080 | return res; |
| 4081 | } |
| 4082 | |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 4083 | if (is_rebase_i(opts)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4084 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 4085 | struct stat st; |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4086 | |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 4087 | /* Stopped in the middle, as planned? */ |
| 4088 | if (todo_list->current < todo_list->nr) |
| 4089 | return 0; |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4090 | |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4091 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
| 4092 | starts_with(head_ref.buf, "refs/")) { |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 4093 | const char *msg; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4094 | struct object_id head, orig; |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4095 | int res; |
| 4096 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4097 | if (get_oid("HEAD", &head)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4098 | res = error(_("cannot read HEAD")); |
| 4099 | cleanup_head_ref: |
| 4100 | strbuf_release(&head_ref); |
| 4101 | strbuf_release(&buf); |
| 4102 | return res; |
| 4103 | } |
| 4104 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4105 | get_oid_hex(buf.buf, &orig)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4106 | res = error(_("could not read orig-head")); |
| 4107 | goto cleanup_head_ref; |
| 4108 | } |
Phillip Wood | 4ab867b | 2017-05-18 11:02:32 +0100 | [diff] [blame] | 4109 | strbuf_reset(&buf); |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4110 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
| 4111 | res = error(_("could not read 'onto'")); |
| 4112 | goto cleanup_head_ref; |
| 4113 | } |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 4114 | msg = reflog_message(opts, "finish", "%s onto %s", |
| 4115 | head_ref.buf, buf.buf); |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 4116 | if (update_ref(msg, head_ref.buf, &head, &orig, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 4117 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4118 | res = error(_("could not update %s"), |
| 4119 | head_ref.buf); |
| 4120 | goto cleanup_head_ref; |
| 4121 | } |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 4122 | msg = reflog_message(opts, "finish", "returning to %s", |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4123 | head_ref.buf); |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 4124 | if (create_symref("HEAD", head_ref.buf, msg)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4125 | res = error(_("could not update HEAD to %s"), |
| 4126 | head_ref.buf); |
| 4127 | goto cleanup_head_ref; |
| 4128 | } |
| 4129 | strbuf_reset(&buf); |
| 4130 | } |
| 4131 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4132 | if (opts->verbose) { |
| 4133 | struct rev_info log_tree_opt; |
| 4134 | struct object_id orig, head; |
| 4135 | |
| 4136 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4137 | repo_init_revisions(r, &log_tree_opt, NULL); |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4138 | log_tree_opt.diff = 1; |
| 4139 | log_tree_opt.diffopt.output_format = |
| 4140 | DIFF_FORMAT_DIFFSTAT; |
| 4141 | log_tree_opt.disable_stdin = 1; |
| 4142 | |
| 4143 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 4144 | !get_oid(buf.buf, &orig) && |
| 4145 | !get_oid("HEAD", &head)) { |
Brandon Williams | 66f414f | 2017-05-30 10:31:03 -0700 | [diff] [blame] | 4146 | diff_tree_oid(&orig, &head, "", |
| 4147 | &log_tree_opt.diffopt); |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4148 | log_tree_diff_flush(&log_tree_opt); |
| 4149 | } |
| 4150 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 4151 | flush_rewritten_pending(); |
| 4152 | if (!stat(rebase_path_rewritten_list(), &st) && |
| 4153 | st.st_size > 0) { |
| 4154 | struct child_process child = CHILD_PROCESS_INIT; |
Johannes Schindelin | 7951604 | 2017-01-02 16:28:23 +0100 | [diff] [blame] | 4155 | const char *post_rewrite_hook = |
| 4156 | find_hook("post-rewrite"); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 4157 | |
| 4158 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); |
| 4159 | child.git_cmd = 1; |
| 4160 | argv_array_push(&child.args, "notes"); |
| 4161 | argv_array_push(&child.args, "copy"); |
| 4162 | argv_array_push(&child.args, "--for-rewrite=rebase"); |
| 4163 | /* we don't care if this copying failed */ |
| 4164 | run_command(&child); |
Johannes Schindelin | 7951604 | 2017-01-02 16:28:23 +0100 | [diff] [blame] | 4165 | |
| 4166 | if (post_rewrite_hook) { |
| 4167 | struct child_process hook = CHILD_PROCESS_INIT; |
| 4168 | |
| 4169 | hook.in = open(rebase_path_rewritten_list(), |
| 4170 | O_RDONLY); |
| 4171 | hook.stdout_to_stderr = 1; |
Jeff Hostetler | 6206286 | 2019-02-22 14:25:06 -0800 | [diff] [blame] | 4172 | hook.trace2_hook_name = "post-rewrite"; |
Johannes Schindelin | 7951604 | 2017-01-02 16:28:23 +0100 | [diff] [blame] | 4173 | argv_array_push(&hook.args, post_rewrite_hook); |
| 4174 | argv_array_push(&hook.args, "rebase"); |
| 4175 | /* we don't care if this hook failed */ |
| 4176 | run_command(&hook); |
| 4177 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 4178 | } |
Denton Liu | be1bb60 | 2020-04-07 10:27:56 -0400 | [diff] [blame] | 4179 | apply_autostash(rebase_path_autostash()); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 4180 | |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 4181 | if (!opts->quiet) { |
| 4182 | if (!opts->verbose) |
| 4183 | term_clear_line(); |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 4184 | fprintf(stderr, |
Alban Gruin | 4d55d63 | 2020-03-28 14:05:15 +0100 | [diff] [blame] | 4185 | _("Successfully rebased and updated %s.\n"), |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 4186 | head_ref.buf); |
SZEDER Gábor | d7d9088 | 2019-06-27 15:42:48 +0200 | [diff] [blame] | 4187 | } |
Johannes Schindelin | 5da4966 | 2017-01-02 16:36:25 +0100 | [diff] [blame] | 4188 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 4189 | strbuf_release(&buf); |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 4190 | strbuf_release(&head_ref); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 4191 | } |
| 4192 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4193 | /* |
| 4194 | * Sequence of picks finished successfully; cleanup by |
| 4195 | * removing the .git/sequencer directory |
| 4196 | */ |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 4197 | return sequencer_remove_state(opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4198 | } |
| 4199 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4200 | static int continue_single_pick(struct repository *r) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4201 | { |
| 4202 | const char *argv[] = { "commit", NULL }; |
| 4203 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4204 | if (!file_exists(git_path_cherry_pick_head(r)) && |
| 4205 | !file_exists(git_path_revert_head(r))) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4206 | return error(_("no cherry-pick or revert in progress")); |
| 4207 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 4208 | } |
| 4209 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4210 | static int commit_staged_changes(struct repository *r, |
| 4211 | struct replay_opts *opts, |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4212 | struct todo_list *todo_list) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4213 | { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 4214 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4215 | unsigned int final_fixup = 0, is_clean; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4216 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4217 | if (has_unstaged_changes(r, 1)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4218 | return error(_("cannot rebase: You have unstaged changes.")); |
Johannes Schindelin | 5263220 | 2017-01-02 16:27:25 +0100 | [diff] [blame] | 4219 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4220 | is_clean = !has_uncommitted_changes(r, 0); |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4221 | |
| 4222 | if (file_exists(rebase_path_amend())) { |
| 4223 | struct strbuf rev = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4224 | struct object_id head, to_amend; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4225 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4226 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4227 | return error(_("cannot amend non-existing commit")); |
| 4228 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) |
| 4229 | return error(_("invalid file: '%s'"), rebase_path_amend()); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 4230 | if (get_oid_hex(rev.buf, &to_amend)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4231 | return error(_("invalid contents: '%s'"), |
| 4232 | rebase_path_amend()); |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4233 | if (!is_clean && !oideq(&head, &to_amend)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4234 | return error(_("\nYou have uncommitted changes in your " |
| 4235 | "working tree. Please, commit them\n" |
| 4236 | "first and then run 'git rebase " |
| 4237 | "--continue' again.")); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4238 | /* |
| 4239 | * When skipping a failed fixup/squash, we need to edit the |
| 4240 | * commit message, the current fixup list and count, and if it |
| 4241 | * was the last fixup/squash in the chain, we need to clean up |
| 4242 | * the commit message and if there was a squash, let the user |
| 4243 | * edit it. |
| 4244 | */ |
Johannes Schindelin | 10d2f35 | 2018-08-31 16:45:04 -0700 | [diff] [blame] | 4245 | if (!is_clean || !opts->current_fixup_count) |
| 4246 | ; /* this is not the final fixup */ |
Junio C Hamano | 87ae8a1 | 2018-09-24 10:30:45 -0700 | [diff] [blame] | 4247 | else if (!oideq(&head, &to_amend) || |
Johannes Schindelin | 10d2f35 | 2018-08-31 16:45:04 -0700 | [diff] [blame] | 4248 | !file_exists(rebase_path_stopped_sha())) { |
| 4249 | /* was a final fixup or squash done manually? */ |
| 4250 | if (!is_fixup(peek_command(todo_list, 0))) { |
| 4251 | unlink(rebase_path_fixup_msg()); |
| 4252 | unlink(rebase_path_squash_msg()); |
| 4253 | unlink(rebase_path_current_fixups()); |
| 4254 | strbuf_reset(&opts->current_fixups); |
| 4255 | opts->current_fixup_count = 0; |
| 4256 | } |
| 4257 | } else { |
| 4258 | /* we are in a fixup/squash chain */ |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4259 | const char *p = opts->current_fixups.buf; |
| 4260 | int len = opts->current_fixups.len; |
| 4261 | |
| 4262 | opts->current_fixup_count--; |
| 4263 | if (!len) |
| 4264 | BUG("Incorrect current_fixups:\n%s", p); |
| 4265 | while (len && p[len - 1] != '\n') |
| 4266 | len--; |
| 4267 | strbuf_setlen(&opts->current_fixups, len); |
| 4268 | if (write_message(p, len, rebase_path_current_fixups(), |
| 4269 | 0) < 0) |
| 4270 | return error(_("could not write file: '%s'"), |
| 4271 | rebase_path_current_fixups()); |
| 4272 | |
| 4273 | /* |
| 4274 | * If a fixup/squash in a fixup/squash chain failed, the |
| 4275 | * commit message is already correct, no need to commit |
| 4276 | * it again. |
| 4277 | * |
| 4278 | * Only if it is the final command in the fixup/squash |
| 4279 | * chain, and only if the chain is longer than a single |
| 4280 | * fixup/squash command (which was just skipped), do we |
| 4281 | * actually need to re-commit with a cleaned up commit |
| 4282 | * message. |
| 4283 | */ |
| 4284 | if (opts->current_fixup_count > 0 && |
| 4285 | !is_fixup(peek_command(todo_list, 0))) { |
| 4286 | final_fixup = 1; |
| 4287 | /* |
| 4288 | * If there was not a single "squash" in the |
| 4289 | * chain, we only need to clean up the commit |
| 4290 | * message, no need to bother the user with |
| 4291 | * opening the commit message in the editor. |
| 4292 | */ |
| 4293 | if (!starts_with(p, "squash ") && |
| 4294 | !strstr(p, "\nsquash ")) |
| 4295 | flags = (flags & ~EDIT_MSG) | CLEANUP_MSG; |
| 4296 | } else if (is_fixup(peek_command(todo_list, 0))) { |
| 4297 | /* |
| 4298 | * We need to update the squash message to skip |
| 4299 | * the latest commit message. |
| 4300 | */ |
| 4301 | struct commit *commit; |
| 4302 | const char *path = rebase_path_squash_msg(); |
Doan Tran Cong Danh | b375744 | 2019-11-08 16:43:48 +0700 | [diff] [blame] | 4303 | const char *encoding = get_commit_output_encoding(); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4304 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4305 | if (parse_head(r, &commit) || |
Doan Tran Cong Danh | b375744 | 2019-11-08 16:43:48 +0700 | [diff] [blame] | 4306 | !(p = logmsg_reencode(commit, NULL, encoding)) || |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4307 | write_message(p, strlen(p), path, 0)) { |
| 4308 | unuse_commit_buffer(commit, p); |
| 4309 | return error(_("could not write file: " |
| 4310 | "'%s'"), path); |
| 4311 | } |
| 4312 | unuse_commit_buffer(commit, p); |
| 4313 | } |
| 4314 | } |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4315 | |
| 4316 | strbuf_release(&rev); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 4317 | flags |= AMEND_MSG; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4320 | if (is_clean) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4321 | const char *cherry_pick_head = git_path_cherry_pick_head(r); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4322 | |
| 4323 | if (file_exists(cherry_pick_head) && unlink(cherry_pick_head)) |
| 4324 | return error(_("could not remove CHERRY_PICK_HEAD")); |
| 4325 | if (!final_fixup) |
| 4326 | return 0; |
| 4327 | } |
| 4328 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4329 | if (run_git_commit(r, final_fixup ? NULL : rebase_path_message(), |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4330 | opts, flags)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4331 | return error(_("could not commit staged changes.")); |
| 4332 | unlink(rebase_path_amend()); |
Nguyễn Thái Ngọc Duy | 34e7771 | 2019-06-27 16:28:52 +0700 | [diff] [blame] | 4333 | unlink(git_path_merge_head(r)); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4334 | if (final_fixup) { |
| 4335 | unlink(rebase_path_fixup_msg()); |
| 4336 | unlink(rebase_path_squash_msg()); |
| 4337 | } |
| 4338 | if (opts->current_fixup_count > 0) { |
| 4339 | /* |
| 4340 | * Whether final fixup or not, we just cleaned up the commit |
| 4341 | * message... |
| 4342 | */ |
| 4343 | unlink(rebase_path_current_fixups()); |
| 4344 | strbuf_reset(&opts->current_fixups); |
| 4345 | opts->current_fixup_count = 0; |
| 4346 | } |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4347 | return 0; |
| 4348 | } |
| 4349 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4350 | int sequencer_continue(struct repository *r, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4351 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4352 | struct todo_list todo_list = TODO_LIST_INIT; |
| 4353 | int res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4354 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4355 | if (read_and_refresh_cache(r, opts)) |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 4356 | return -1; |
| 4357 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4358 | if (read_populate_opts(opts)) |
| 4359 | return -1; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 4360 | if (is_rebase_i(opts)) { |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4361 | if ((res = read_populate_todo(r, &todo_list, opts))) |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 4362 | goto release_todo_list; |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 4363 | |
| 4364 | if (file_exists(rebase_path_dropped())) { |
| 4365 | if ((res = todo_list_check_against_backup(r, &todo_list))) |
| 4366 | goto release_todo_list; |
| 4367 | |
| 4368 | unlink(rebase_path_dropped()); |
| 4369 | } |
| 4370 | |
Alban Gruin | f6b9413 | 2019-11-29 00:02:03 +0100 | [diff] [blame] | 4371 | if (commit_staged_changes(r, opts, &todo_list)) { |
| 4372 | res = -1; |
| 4373 | goto release_todo_list; |
| 4374 | } |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 4375 | } else if (!file_exists(get_todo_path(opts))) |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4376 | return continue_single_pick(r); |
| 4377 | else if ((res = read_populate_todo(r, &todo_list, opts))) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4378 | goto release_todo_list; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4379 | |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 4380 | if (!is_rebase_i(opts)) { |
| 4381 | /* Verify that the conflict has been resolved */ |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4382 | if (file_exists(git_path_cherry_pick_head(r)) || |
| 4383 | file_exists(git_path_revert_head(r))) { |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 4384 | res = continue_single_pick(r); |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 4385 | if (res) |
| 4386 | goto release_todo_list; |
| 4387 | } |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 4388 | if (index_differs_from(r, "HEAD", NULL, 0)) { |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 4389 | res = error_dirty_index(r, opts); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4390 | goto release_todo_list; |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 4391 | } |
| 4392 | todo_list.current++; |
Johannes Schindelin | ca98c6d | 2017-01-02 16:28:20 +0100 | [diff] [blame] | 4393 | } else if (file_exists(rebase_path_stopped_sha())) { |
| 4394 | struct strbuf buf = STRBUF_INIT; |
| 4395 | struct object_id oid; |
| 4396 | |
Denton Liu | 3442c3d | 2020-04-07 10:27:52 -0400 | [diff] [blame] | 4397 | if (read_oneliner(&buf, rebase_path_stopped_sha(), |
| 4398 | READ_ONELINER_SKIP_IF_EMPTY) && |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 4399 | !get_oid_committish(buf.buf, &oid)) |
Johannes Schindelin | ca98c6d | 2017-01-02 16:28:20 +0100 | [diff] [blame] | 4400 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
| 4401 | strbuf_release(&buf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4402 | } |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 4403 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4404 | res = pick_commits(r, &todo_list, opts); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4405 | release_todo_list: |
| 4406 | todo_list_release(&todo_list); |
| 4407 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4408 | } |
| 4409 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4410 | static int single_pick(struct repository *r, |
| 4411 | struct commit *cmit, |
| 4412 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4413 | { |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 4414 | int check_todo; |
| 4415 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4416 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4417 | return do_pick_commit(r, opts->action == REPLAY_PICK ? |
Phillip Wood | a47ba3c | 2019-08-19 02:18:22 -0700 | [diff] [blame] | 4418 | TODO_PICK : TODO_REVERT, cmit, opts, 0, |
| 4419 | &check_todo); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4420 | } |
| 4421 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4422 | int sequencer_pick_revisions(struct repository *r, |
| 4423 | struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4424 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4425 | struct todo_list todo_list = TODO_LIST_INIT; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 4426 | struct object_id oid; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4427 | int i, res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4428 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 4429 | assert(opts->revs); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4430 | if (read_and_refresh_cache(r, opts)) |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 4431 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4432 | |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 4433 | for (i = 0; i < opts->revs->pending.nr; i++) { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 4434 | struct object_id oid; |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 4435 | const char *name = opts->revs->pending.objects[i].name; |
| 4436 | |
| 4437 | /* This happens when using --stdin. */ |
| 4438 | if (!strlen(name)) |
| 4439 | continue; |
| 4440 | |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 4441 | if (!get_oid(name, &oid)) { |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4442 | if (!lookup_commit_reference_gently(r, &oid, 1)) { |
| 4443 | enum object_type type = oid_object_info(r, |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 4444 | &oid, |
brian m. carlson | abef902 | 2018-03-12 02:27:46 +0000 | [diff] [blame] | 4445 | NULL); |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 4446 | return error(_("%s: can't cherry-pick a %s"), |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 4447 | name, type_name(type)); |
Junio C Hamano | 7c0b0d8 | 2013-05-09 13:27:49 -0700 | [diff] [blame] | 4448 | } |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 4449 | } else |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 4450 | return error(_("%s: bad revision"), name); |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 4451 | } |
| 4452 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4453 | /* |
| 4454 | * If we were called as "git cherry-pick <commit>", just |
| 4455 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / |
| 4456 | * REVERT_HEAD, and don't touch the sequencer state. |
| 4457 | * This means it is possible to cherry-pick in the middle |
| 4458 | * of a cherry-pick sequence. |
| 4459 | */ |
| 4460 | if (opts->revs->cmdline.nr == 1 && |
| 4461 | opts->revs->cmdline.rev->whence == REV_CMD_REV && |
| 4462 | opts->revs->no_walk && |
| 4463 | !opts->revs->cmdline.rev->flags) { |
| 4464 | struct commit *cmit; |
| 4465 | if (prepare_revision_walk(opts->revs)) |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 4466 | return error(_("revision walk setup failed")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4467 | cmit = get_revision(opts->revs); |
Jeff King | c5e358d | 2018-07-10 00:32:08 -0400 | [diff] [blame] | 4468 | if (!cmit) |
| 4469 | return error(_("empty commit set passed")); |
| 4470 | if (get_revision(opts->revs)) |
| 4471 | BUG("unexpected extra commit from walk"); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4472 | return single_pick(r, cmit, opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | /* |
| 4476 | * Start a new cherry-pick/ revert sequence; but |
| 4477 | * first, make sure that an existing one isn't in |
| 4478 | * progress |
| 4479 | */ |
| 4480 | |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 4481 | if (walk_revs_populate_todo(&todo_list, opts) || |
Rohit Ashiwal | 6a1f904 | 2019-07-02 14:41:25 +0530 | [diff] [blame] | 4482 | create_seq_dir(r) < 0) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4483 | return -1; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 4484 | if (get_oid("HEAD", &oid) && (opts->action == REPLAY_REVERT)) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 4485 | return error(_("can't revert as initial commit")); |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 4486 | if (save_head(oid_to_hex(&oid))) |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 4487 | return -1; |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 4488 | if (save_opts(opts)) |
| 4489 | return -1; |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 4490 | update_abort_safety_file(); |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4491 | res = pick_commits(r, &todo_list, opts); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 4492 | todo_list_release(&todo_list); |
| 4493 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4494 | } |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4495 | |
Jeff King | 66e83d9 | 2018-08-22 20:50:51 -0400 | [diff] [blame] | 4496 | void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag) |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4497 | { |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 4498 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4499 | struct strbuf sob = STRBUF_INIT; |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 4500 | int has_footer; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4501 | |
| 4502 | strbuf_addstr(&sob, sign_off_header); |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 4503 | strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT)); |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4504 | strbuf_addch(&sob, '\n'); |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 4505 | |
Jonathan Tan | 44dc738 | 2017-04-26 13:50:03 -0700 | [diff] [blame] | 4506 | if (!ignore_footer) |
| 4507 | strbuf_complete_line(msgbuf); |
| 4508 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 4509 | /* |
| 4510 | * If the whole message buffer is equal to the sob, pretend that we |
| 4511 | * found a conforming footer with a matching sob |
| 4512 | */ |
| 4513 | if (msgbuf->len - ignore_footer == sob.len && |
| 4514 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
| 4515 | has_footer = 3; |
| 4516 | else |
| 4517 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
| 4518 | |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 4519 | if (!has_footer) { |
| 4520 | const char *append_newlines = NULL; |
| 4521 | size_t len = msgbuf->len - ignore_footer; |
| 4522 | |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 4523 | if (!len) { |
| 4524 | /* |
| 4525 | * The buffer is completely empty. Leave foom for |
| 4526 | * the title and body to be filled in by the user. |
| 4527 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 4528 | append_newlines = "\n\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 4529 | } else if (len == 1) { |
| 4530 | /* |
| 4531 | * Buffer contains a single newline. Add another |
| 4532 | * so that we leave room for the title and body. |
| 4533 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 4534 | append_newlines = "\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 4535 | } else if (msgbuf->buf[len - 2] != '\n') { |
| 4536 | /* |
| 4537 | * Buffer ends with a single newline. Add another |
| 4538 | * so that there is an empty line between the message |
| 4539 | * body and the sob. |
| 4540 | */ |
| 4541 | append_newlines = "\n"; |
| 4542 | } /* else, the buffer already ends with two newlines. */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 4543 | |
| 4544 | if (append_newlines) |
| 4545 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 4546 | append_newlines, strlen(append_newlines)); |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4547 | } |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 4548 | |
| 4549 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
| 4550 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 4551 | sob.buf, sob.len); |
| 4552 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 4553 | strbuf_release(&sob); |
| 4554 | } |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4555 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4556 | struct labels_entry { |
| 4557 | struct hashmap_entry entry; |
| 4558 | char label[FLEX_ARRAY]; |
| 4559 | }; |
| 4560 | |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 4561 | static int labels_cmp(const void *fndata, const struct hashmap_entry *eptr, |
| 4562 | const struct hashmap_entry *entry_or_key, const void *key) |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4563 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 4564 | const struct labels_entry *a, *b; |
| 4565 | |
| 4566 | a = container_of(eptr, const struct labels_entry, entry); |
| 4567 | b = container_of(entry_or_key, const struct labels_entry, entry); |
| 4568 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4569 | return key ? strcmp(a->label, key) : strcmp(a->label, b->label); |
| 4570 | } |
| 4571 | |
| 4572 | struct string_entry { |
| 4573 | struct oidmap_entry entry; |
| 4574 | char string[FLEX_ARRAY]; |
| 4575 | }; |
| 4576 | |
| 4577 | struct label_state { |
| 4578 | struct oidmap commit2label; |
| 4579 | struct hashmap labels; |
| 4580 | struct strbuf buf; |
| 4581 | }; |
| 4582 | |
| 4583 | static const char *label_oid(struct object_id *oid, const char *label, |
| 4584 | struct label_state *state) |
| 4585 | { |
| 4586 | struct labels_entry *labels_entry; |
| 4587 | struct string_entry *string_entry; |
| 4588 | struct object_id dummy; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4589 | int i; |
| 4590 | |
| 4591 | string_entry = oidmap_get(&state->commit2label, oid); |
| 4592 | if (string_entry) |
| 4593 | return string_entry->string; |
| 4594 | |
| 4595 | /* |
| 4596 | * For "uninteresting" commits, i.e. commits that are not to be |
| 4597 | * rebased, and which can therefore not be labeled, we use a unique |
| 4598 | * abbreviation of the commit name. This is slightly more complicated |
| 4599 | * than calling find_unique_abbrev() because we also need to make |
| 4600 | * sure that the abbreviation does not conflict with any other |
| 4601 | * label. |
| 4602 | * |
| 4603 | * We disallow "interesting" commits to be labeled by a string that |
| 4604 | * is a valid full-length hash, to ensure that we always can find an |
| 4605 | * abbreviation for any uninteresting commit's names that does not |
| 4606 | * clash with any other label. |
| 4607 | */ |
Johannes Schindelin | 867bc1d | 2019-11-17 23:16:09 +0000 | [diff] [blame] | 4608 | strbuf_reset(&state->buf); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4609 | if (!label) { |
| 4610 | char *p; |
| 4611 | |
brian m. carlson | 4439c7a | 2019-08-18 20:04:16 +0000 | [diff] [blame] | 4612 | strbuf_grow(&state->buf, GIT_MAX_HEXSZ); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4613 | label = p = state->buf.buf; |
| 4614 | |
| 4615 | find_unique_abbrev_r(p, oid, default_abbrev); |
| 4616 | |
| 4617 | /* |
| 4618 | * We may need to extend the abbreviated hash so that there is |
| 4619 | * no conflicting label. |
| 4620 | */ |
| 4621 | if (hashmap_get_from_hash(&state->labels, strihash(p), p)) { |
| 4622 | size_t i = strlen(p) + 1; |
| 4623 | |
| 4624 | oid_to_hex_r(p, oid); |
brian m. carlson | 4439c7a | 2019-08-18 20:04:16 +0000 | [diff] [blame] | 4625 | for (; i < the_hash_algo->hexsz; i++) { |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4626 | char save = p[i]; |
| 4627 | p[i] = '\0'; |
| 4628 | if (!hashmap_get_from_hash(&state->labels, |
| 4629 | strihash(p), p)) |
| 4630 | break; |
| 4631 | p[i] = save; |
| 4632 | } |
| 4633 | } |
Johannes Schindelin | 867bc1d | 2019-11-17 23:16:09 +0000 | [diff] [blame] | 4634 | } else { |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4635 | struct strbuf *buf = &state->buf; |
| 4636 | |
Matthew Rogers | cd55222 | 2019-11-17 23:16:10 +0000 | [diff] [blame] | 4637 | /* |
| 4638 | * Sanitize labels by replacing non-alpha-numeric characters |
| 4639 | * (including white-space ones) by dashes, as they might be |
| 4640 | * illegal in file names (and hence in ref names). |
| 4641 | * |
| 4642 | * Note that we retain non-ASCII UTF-8 characters (identified |
| 4643 | * via the most significant bit). They should be all acceptable |
| 4644 | * in file names. We do not validate the UTF-8 here, that's not |
| 4645 | * the job of this function. |
| 4646 | */ |
Johannes Schindelin | 867bc1d | 2019-11-17 23:16:09 +0000 | [diff] [blame] | 4647 | for (; *label; label++) |
Matthew Rogers | cd55222 | 2019-11-17 23:16:10 +0000 | [diff] [blame] | 4648 | if ((*label & 0x80) || isalnum(*label)) |
| 4649 | strbuf_addch(buf, *label); |
| 4650 | /* avoid leading dash and double-dashes */ |
| 4651 | else if (buf->len && buf->buf[buf->len - 1] != '-') |
| 4652 | strbuf_addch(buf, '-'); |
| 4653 | if (!buf->len) { |
| 4654 | strbuf_addstr(buf, "rev-"); |
| 4655 | strbuf_add_unique_abbrev(buf, oid, default_abbrev); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4656 | } |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4657 | label = buf->buf; |
Johannes Schindelin | 867bc1d | 2019-11-17 23:16:09 +0000 | [diff] [blame] | 4658 | |
| 4659 | if ((buf->len == the_hash_algo->hexsz && |
| 4660 | !get_oid_hex(label, &dummy)) || |
| 4661 | (buf->len == 1 && *label == '#') || |
| 4662 | hashmap_get_from_hash(&state->labels, |
| 4663 | strihash(label), label)) { |
| 4664 | /* |
| 4665 | * If the label already exists, or if the label is a |
| 4666 | * valid full OID, or the label is a '#' (which we use |
| 4667 | * as a separator between merge heads and oneline), we |
| 4668 | * append a dash and a number to make it unique. |
| 4669 | */ |
| 4670 | size_t len = buf->len; |
| 4671 | |
| 4672 | for (i = 2; ; i++) { |
| 4673 | strbuf_setlen(buf, len); |
| 4674 | strbuf_addf(buf, "-%d", i); |
| 4675 | if (!hashmap_get_from_hash(&state->labels, |
| 4676 | strihash(buf->buf), |
| 4677 | buf->buf)) |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | label = buf->buf; |
| 4682 | } |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | FLEX_ALLOC_STR(labels_entry, label, label); |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 4686 | hashmap_entry_init(&labels_entry->entry, strihash(label)); |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 4687 | hashmap_add(&state->labels, &labels_entry->entry); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4688 | |
| 4689 | FLEX_ALLOC_STR(string_entry, string, label); |
| 4690 | oidcpy(&string_entry->entry.oid, oid); |
| 4691 | oidmap_put(&state->commit2label, string_entry); |
| 4692 | |
| 4693 | return string_entry->string; |
| 4694 | } |
| 4695 | |
| 4696 | static int make_script_with_merges(struct pretty_print_context *pp, |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4697 | struct rev_info *revs, struct strbuf *out, |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4698 | unsigned flags) |
| 4699 | { |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 4700 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 4701 | int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS; |
Johannes Schindelin | e1fac53 | 2019-07-31 08:18:49 -0700 | [diff] [blame] | 4702 | int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4703 | struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT; |
| 4704 | struct strbuf label = STRBUF_INIT; |
| 4705 | struct commit_list *commits = NULL, **tail = &commits, *iter; |
| 4706 | struct commit_list *tips = NULL, **tips_tail = &tips; |
| 4707 | struct commit *commit; |
| 4708 | struct oidmap commit2todo = OIDMAP_INIT; |
| 4709 | struct string_entry *entry; |
| 4710 | struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, |
| 4711 | shown = OIDSET_INIT; |
| 4712 | struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT }; |
| 4713 | |
| 4714 | int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; |
| 4715 | const char *cmd_pick = abbr ? "p" : "pick", |
| 4716 | *cmd_label = abbr ? "l" : "label", |
| 4717 | *cmd_reset = abbr ? "t" : "reset", |
| 4718 | *cmd_merge = abbr ? "m" : "merge"; |
| 4719 | |
| 4720 | oidmap_init(&commit2todo, 0); |
| 4721 | oidmap_init(&state.commit2label, 0); |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 4722 | hashmap_init(&state.labels, labels_cmp, NULL, 0); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4723 | strbuf_init(&state.buf, 32); |
| 4724 | |
| 4725 | if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) { |
Doan Tran Cong Danh | e02058a | 2019-11-18 18:57:47 +0700 | [diff] [blame] | 4726 | struct labels_entry *onto_label_entry; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4727 | struct object_id *oid = &revs->cmdline.rev[0].item->oid; |
| 4728 | FLEX_ALLOC_STR(entry, string, "onto"); |
| 4729 | oidcpy(&entry->entry.oid, oid); |
| 4730 | oidmap_put(&state.commit2label, entry); |
Doan Tran Cong Danh | e02058a | 2019-11-18 18:57:47 +0700 | [diff] [blame] | 4731 | |
| 4732 | FLEX_ALLOC_STR(onto_label_entry, label, "onto"); |
| 4733 | hashmap_entry_init(&onto_label_entry->entry, strihash("onto")); |
| 4734 | hashmap_add(&state.labels, &onto_label_entry->entry); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | /* |
| 4738 | * First phase: |
| 4739 | * - get onelines for all commits |
| 4740 | * - gather all branch tips (i.e. 2nd or later parents of merges) |
| 4741 | * - label all branch tips |
| 4742 | */ |
| 4743 | while ((commit = get_revision(revs))) { |
| 4744 | struct commit_list *to_merge; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4745 | const char *p1, *p2; |
| 4746 | struct object_id *oid; |
| 4747 | int is_empty; |
| 4748 | |
| 4749 | tail = &commit_list_insert(commit, tail)->next; |
| 4750 | oidset_insert(&interesting, &commit->object.oid); |
| 4751 | |
| 4752 | is_empty = is_original_commit_empty(commit); |
| 4753 | if (!is_empty && (commit->object.flags & PATCHSAME)) |
| 4754 | continue; |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 4755 | if (is_empty && !keep_empty) |
| 4756 | continue; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4757 | |
| 4758 | strbuf_reset(&oneline); |
| 4759 | pretty_print_commit(pp, commit, &oneline); |
| 4760 | |
| 4761 | to_merge = commit->parents ? commit->parents->next : NULL; |
| 4762 | if (!to_merge) { |
| 4763 | /* non-merge commit: easy case */ |
| 4764 | strbuf_reset(&buf); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4765 | strbuf_addf(&buf, "%s %s %s", cmd_pick, |
| 4766 | oid_to_hex(&commit->object.oid), |
| 4767 | oneline.buf); |
Elijah Newren | 1b5735f | 2020-04-11 02:44:24 +0000 | [diff] [blame] | 4768 | if (is_empty) |
| 4769 | strbuf_addf(&buf, " %c empty", |
| 4770 | comment_line_char); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4771 | |
| 4772 | FLEX_ALLOC_STR(entry, string, buf.buf); |
| 4773 | oidcpy(&entry->entry.oid, &commit->object.oid); |
| 4774 | oidmap_put(&commit2todo, entry); |
| 4775 | |
| 4776 | continue; |
| 4777 | } |
| 4778 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4779 | /* Create a label */ |
| 4780 | strbuf_reset(&label); |
| 4781 | if (skip_prefix(oneline.buf, "Merge ", &p1) && |
| 4782 | (p1 = strchr(p1, '\'')) && |
| 4783 | (p2 = strchr(++p1, '\''))) |
| 4784 | strbuf_add(&label, p1, p2 - p1); |
| 4785 | else if (skip_prefix(oneline.buf, "Merge pull request ", |
| 4786 | &p1) && |
| 4787 | (p1 = strstr(p1, " from "))) |
| 4788 | strbuf_addstr(&label, p1 + strlen(" from ")); |
| 4789 | else |
| 4790 | strbuf_addbuf(&label, &oneline); |
| 4791 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4792 | strbuf_reset(&buf); |
| 4793 | strbuf_addf(&buf, "%s -C %s", |
| 4794 | cmd_merge, oid_to_hex(&commit->object.oid)); |
| 4795 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 4796 | /* label the tips of merged branches */ |
| 4797 | for (; to_merge; to_merge = to_merge->next) { |
| 4798 | oid = &to_merge->item->object.oid; |
| 4799 | strbuf_addch(&buf, ' '); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4800 | |
Johannes Schindelin | 2b6ad0f | 2017-12-21 15:52:45 +0100 | [diff] [blame] | 4801 | if (!oidset_contains(&interesting, oid)) { |
| 4802 | strbuf_addstr(&buf, label_oid(oid, NULL, |
| 4803 | &state)); |
| 4804 | continue; |
| 4805 | } |
| 4806 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4807 | tips_tail = &commit_list_insert(to_merge->item, |
| 4808 | tips_tail)->next; |
| 4809 | |
| 4810 | strbuf_addstr(&buf, label_oid(oid, label.buf, &state)); |
| 4811 | } |
| 4812 | strbuf_addf(&buf, " # %s", oneline.buf); |
| 4813 | |
| 4814 | FLEX_ALLOC_STR(entry, string, buf.buf); |
| 4815 | oidcpy(&entry->entry.oid, &commit->object.oid); |
| 4816 | oidmap_put(&commit2todo, entry); |
| 4817 | } |
| 4818 | |
| 4819 | /* |
| 4820 | * Second phase: |
| 4821 | * - label branch points |
| 4822 | * - add HEAD to the branch tips |
| 4823 | */ |
| 4824 | for (iter = commits; iter; iter = iter->next) { |
| 4825 | struct commit_list *parent = iter->item->parents; |
| 4826 | for (; parent; parent = parent->next) { |
| 4827 | struct object_id *oid = &parent->item->object.oid; |
| 4828 | if (!oidset_contains(&interesting, oid)) |
| 4829 | continue; |
René Scharfe | 6e8fc70 | 2018-10-03 15:06:49 +0200 | [diff] [blame] | 4830 | if (oidset_insert(&child_seen, oid)) |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4831 | label_oid(oid, "branch-point", &state); |
| 4832 | } |
| 4833 | |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 4834 | /* Add HEAD as implicit "tip of branch" */ |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4835 | if (!iter->next) |
| 4836 | tips_tail = &commit_list_insert(iter->item, |
| 4837 | tips_tail)->next; |
| 4838 | } |
| 4839 | |
| 4840 | /* |
| 4841 | * Third phase: output the todo list. This is a bit tricky, as we |
| 4842 | * want to avoid jumping back and forth between revisions. To |
| 4843 | * accomplish that goal, we walk backwards from the branch tips, |
| 4844 | * gathering commits not yet shown, reversing the list on the fly, |
| 4845 | * then outputting that list (labeling revisions as needed). |
| 4846 | */ |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4847 | strbuf_addf(out, "%s onto\n", cmd_label); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4848 | for (iter = tips; iter; iter = iter->next) { |
| 4849 | struct commit_list *list = NULL, *iter2; |
| 4850 | |
| 4851 | commit = iter->item; |
| 4852 | if (oidset_contains(&shown, &commit->object.oid)) |
| 4853 | continue; |
| 4854 | entry = oidmap_get(&state.commit2label, &commit->object.oid); |
| 4855 | |
| 4856 | if (entry) |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4857 | strbuf_addf(out, "\n%c Branch %s\n", comment_line_char, entry->string); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4858 | else |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4859 | strbuf_addch(out, '\n'); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4860 | |
| 4861 | while (oidset_contains(&interesting, &commit->object.oid) && |
| 4862 | !oidset_contains(&shown, &commit->object.oid)) { |
| 4863 | commit_list_insert(commit, &list); |
| 4864 | if (!commit->parents) { |
| 4865 | commit = NULL; |
| 4866 | break; |
| 4867 | } |
| 4868 | commit = commit->parents->item; |
| 4869 | } |
| 4870 | |
| 4871 | if (!commit) |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4872 | strbuf_addf(out, "%s %s\n", cmd_reset, |
Johannes Schindelin | e1fac53 | 2019-07-31 08:18:49 -0700 | [diff] [blame] | 4873 | rebase_cousins || root_with_onto ? |
| 4874 | "onto" : "[new root]"); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4875 | else { |
| 4876 | const char *to = NULL; |
| 4877 | |
| 4878 | entry = oidmap_get(&state.commit2label, |
| 4879 | &commit->object.oid); |
| 4880 | if (entry) |
| 4881 | to = entry->string; |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 4882 | else if (!rebase_cousins) |
| 4883 | to = label_oid(&commit->object.oid, NULL, |
| 4884 | &state); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4885 | |
| 4886 | if (!to || !strcmp(to, "onto")) |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4887 | strbuf_addf(out, "%s onto\n", cmd_reset); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4888 | else { |
| 4889 | strbuf_reset(&oneline); |
| 4890 | pretty_print_commit(pp, commit, &oneline); |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4891 | strbuf_addf(out, "%s %s # %s\n", |
| 4892 | cmd_reset, to, oneline.buf); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | for (iter2 = list; iter2; iter2 = iter2->next) { |
| 4897 | struct object_id *oid = &iter2->item->object.oid; |
| 4898 | entry = oidmap_get(&commit2todo, oid); |
| 4899 | /* only show if not already upstream */ |
| 4900 | if (entry) |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4901 | strbuf_addf(out, "%s\n", entry->string); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4902 | entry = oidmap_get(&state.commit2label, oid); |
| 4903 | if (entry) |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4904 | strbuf_addf(out, "%s %s\n", |
| 4905 | cmd_label, entry->string); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4906 | oidset_insert(&shown, oid); |
| 4907 | } |
| 4908 | |
| 4909 | free_commit_list(list); |
| 4910 | } |
| 4911 | |
| 4912 | free_commit_list(commits); |
| 4913 | free_commit_list(tips); |
| 4914 | |
| 4915 | strbuf_release(&label); |
| 4916 | strbuf_release(&oneline); |
| 4917 | strbuf_release(&buf); |
| 4918 | |
| 4919 | oidmap_free(&commit2todo, 1); |
| 4920 | oidmap_free(&state.commit2label, 1); |
Eric Wong | c8e424c | 2019-10-06 23:30:40 +0000 | [diff] [blame] | 4921 | hashmap_free_entries(&state.labels, struct labels_entry, entry); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4922 | strbuf_release(&state.buf); |
| 4923 | |
| 4924 | return 0; |
| 4925 | } |
| 4926 | |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4927 | int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, |
| 4928 | const char **argv, unsigned flags) |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4929 | { |
| 4930 | char *format = NULL; |
| 4931 | struct pretty_print_context pp = {0}; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4932 | struct rev_info revs; |
| 4933 | struct commit *commit; |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 4934 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 4935 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4936 | int rebase_merges = flags & TODO_LIST_REBASE_MERGES; |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 4937 | int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4938 | |
Nguyễn Thái Ngọc Duy | f11c958 | 2018-11-10 06:48:56 +0100 | [diff] [blame] | 4939 | repo_init_revisions(r, &revs, NULL); |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4940 | revs.verbose_header = 1; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4941 | if (!rebase_merges) |
| 4942 | revs.max_parents = 1; |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 4943 | revs.cherry_mark = !reapply_cherry_picks; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4944 | revs.limited = 1; |
| 4945 | revs.reverse = 1; |
| 4946 | revs.right_only = 1; |
| 4947 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; |
| 4948 | revs.topo_order = 1; |
| 4949 | |
| 4950 | revs.pretty_given = 1; |
| 4951 | git_config_get_string("rebase.instructionFormat", &format); |
| 4952 | if (!format || !*format) { |
| 4953 | free(format); |
| 4954 | format = xstrdup("%s"); |
| 4955 | } |
| 4956 | get_commit_format(format, &revs); |
| 4957 | free(format); |
| 4958 | pp.fmt = revs.commit_format; |
| 4959 | pp.output_encoding = get_log_output_encoding(); |
| 4960 | |
| 4961 | if (setup_revisions(argc, argv, &revs, NULL) > 1) |
| 4962 | return error(_("make_script: unhandled options")); |
| 4963 | |
| 4964 | if (prepare_revision_walk(&revs) < 0) |
| 4965 | return error(_("make_script: error preparing revisions")); |
| 4966 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4967 | if (rebase_merges) |
| 4968 | return make_script_with_merges(&pp, &revs, out, flags); |
| 4969 | |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4970 | while ((commit = get_revision(&revs))) { |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 4971 | int is_empty = is_original_commit_empty(commit); |
Phillip Wood | 76ea235 | 2018-03-20 10:03:14 +0000 | [diff] [blame] | 4972 | |
| 4973 | if (!is_empty && (commit->object.flags & PATCHSAME)) |
| 4974 | continue; |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 4975 | if (is_empty && !keep_empty) |
| 4976 | continue; |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4977 | strbuf_addf(out, "%s %s ", insn, |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 4978 | oid_to_hex(&commit->object.oid)); |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4979 | pretty_print_commit(&pp, commit, out); |
Elijah Newren | 1b5735f | 2020-04-11 02:44:24 +0000 | [diff] [blame] | 4980 | if (is_empty) |
| 4981 | strbuf_addf(out, " %c empty", comment_line_char); |
Alban Gruin | d358fc2 | 2019-03-05 20:17:56 +0100 | [diff] [blame] | 4982 | strbuf_addch(out, '\n'); |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4983 | } |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4984 | return 0; |
| 4985 | } |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4986 | |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4987 | /* |
| 4988 | * Add commands after pick and (series of) squash/fixup commands |
| 4989 | * in the todo list. |
| 4990 | */ |
Alban Gruin | 1ba204d | 2019-03-05 20:17:58 +0100 | [diff] [blame] | 4991 | void todo_list_add_exec_commands(struct todo_list *todo_list, |
| 4992 | struct string_list *commands) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4993 | { |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 4994 | struct strbuf *buf = &todo_list->buf; |
| 4995 | size_t base_offset = buf->len; |
| 4996 | int i, insert, nr = 0, alloc = 0; |
| 4997 | struct todo_item *items = NULL, *base_items = NULL; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4998 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 4999 | base_items = xcalloc(commands->nr, sizeof(struct todo_item)); |
| 5000 | for (i = 0; i < commands->nr; i++) { |
| 5001 | size_t command_len = strlen(commands->items[i].string); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5002 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5003 | strbuf_addstr(buf, commands->items[i].string); |
| 5004 | strbuf_addch(buf, '\n'); |
| 5005 | |
| 5006 | base_items[i].command = TODO_EXEC; |
| 5007 | base_items[i].offset_in_buf = base_offset; |
| 5008 | base_items[i].arg_offset = base_offset + strlen("exec "); |
| 5009 | base_items[i].arg_len = command_len - strlen("exec "); |
| 5010 | |
| 5011 | base_offset += command_len + 1; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5012 | } |
| 5013 | |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5014 | /* |
| 5015 | * Insert <commands> after every pick. Here, fixup/squash chains |
| 5016 | * are considered part of the pick, so we insert the commands *after* |
| 5017 | * those chains if there are any. |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5018 | * |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 5019 | * As we insert the exec commands immediately after rearranging |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5020 | * any fixups and before the user edits the list, a fixup chain |
| 5021 | * can never contain comments (any comments are empty picks that |
| 5022 | * have been commented out because the user did not specify |
| 5023 | * --keep-empty). So, it is safe to insert an exec command |
| 5024 | * without looking at the command following a comment. |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5025 | */ |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5026 | insert = 0; |
| 5027 | for (i = 0; i < todo_list->nr; i++) { |
| 5028 | enum todo_command command = todo_list->items[i].command; |
| 5029 | if (insert && !is_fixup(command)) { |
| 5030 | ALLOC_GROW(items, nr + commands->nr, alloc); |
| 5031 | COPY_ARRAY(items + nr, base_items, commands->nr); |
| 5032 | nr += commands->nr; |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5033 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5034 | insert = 0; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5035 | } |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5036 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5037 | ALLOC_GROW(items, nr + 1, alloc); |
| 5038 | items[nr++] = todo_list->items[i]; |
| 5039 | |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5040 | if (command == TODO_PICK || command == TODO_MERGE) |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5041 | insert = 1; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5042 | } |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 5043 | |
Johannes Schindelin | 1ace63b | 2018-08-09 02:41:11 -0700 | [diff] [blame] | 5044 | /* insert or append final <commands> */ |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5045 | if (insert || nr == todo_list->nr) { |
| 5046 | ALLOC_GROW(items, nr + commands->nr, alloc); |
| 5047 | COPY_ARRAY(items + nr, base_items, commands->nr); |
| 5048 | nr += commands->nr; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5049 | } |
| 5050 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5051 | free(base_items); |
| 5052 | FREE_AND_NULL(todo_list->items); |
| 5053 | todo_list->items = items; |
| 5054 | todo_list->nr = nr; |
| 5055 | todo_list->alloc = alloc; |
| 5056 | } |
| 5057 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5058 | static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_list, |
| 5059 | struct strbuf *buf, int num, unsigned flags) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5060 | { |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5061 | struct todo_item *item; |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5062 | int i, max = todo_list->nr; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5063 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5064 | if (num > 0 && num < max) |
| 5065 | max = num; |
| 5066 | |
| 5067 | for (item = todo_list->items, i = 0; i < max; i++, item++) { |
Alban Gruin | 68e7090 | 2020-03-30 14:42:35 +0200 | [diff] [blame] | 5068 | char cmd; |
| 5069 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5070 | /* if the item is not a command write it and continue */ |
| 5071 | if (item->command >= TODO_COMMENT) { |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5072 | strbuf_addf(buf, "%.*s\n", item->arg_len, |
Alban Gruin | cbef27d | 2019-01-29 16:01:47 +0100 | [diff] [blame] | 5073 | todo_item_get_arg(todo_list, item)); |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5074 | continue; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5075 | } |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5076 | |
| 5077 | /* add command to the buffer */ |
Alban Gruin | 68e7090 | 2020-03-30 14:42:35 +0200 | [diff] [blame] | 5078 | cmd = command_to_char(item->command); |
| 5079 | if ((flags & TODO_LIST_ABBREVIATE_CMDS) && cmd) |
| 5080 | strbuf_addch(buf, cmd); |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 5081 | else |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5082 | strbuf_addstr(buf, command_to_string(item->command)); |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5083 | |
| 5084 | /* add commit id */ |
| 5085 | if (item->commit) { |
Liam Beguin | 313a48e | 2017-12-05 12:52:32 -0500 | [diff] [blame] | 5086 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5087 | short_commit_name(item->commit) : |
| 5088 | oid_to_hex(&item->commit->object.oid); |
| 5089 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 5090 | if (item->command == TODO_MERGE) { |
| 5091 | if (item->flags & TODO_EDIT_MERGE_MSG) |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5092 | strbuf_addstr(buf, " -c"); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 5093 | else |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5094 | strbuf_addstr(buf, " -C"); |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 5095 | } |
| 5096 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5097 | strbuf_addf(buf, " %s", oid); |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5098 | } |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 5099 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 5100 | /* add all the rest */ |
Johannes Schindelin | c7b4d79 | 2017-12-23 00:56:00 +0100 | [diff] [blame] | 5101 | if (!item->arg_len) |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5102 | strbuf_addch(buf, '\n'); |
Johannes Schindelin | c7b4d79 | 2017-12-23 00:56:00 +0100 | [diff] [blame] | 5103 | else |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5104 | strbuf_addf(buf, " %.*s\n", item->arg_len, |
Alban Gruin | cbef27d | 2019-01-29 16:01:47 +0100 | [diff] [blame] | 5105 | todo_item_get_arg(todo_list, item)); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5106 | } |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 5107 | } |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 5108 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5109 | int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list, |
| 5110 | const char *file, const char *shortrevisions, |
| 5111 | const char *shortonto, int num, unsigned flags) |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 5112 | { |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 5113 | int res; |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5114 | struct strbuf buf = STRBUF_INIT; |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 5115 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5116 | todo_list_to_strbuf(r, todo_list, &buf, num, flags); |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 5117 | if (flags & TODO_LIST_APPEND_TODO_HELP) |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 5118 | append_todo_help(count_commands(todo_list), |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 5119 | shortrevisions, shortonto, &buf); |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 5120 | |
Alban Gruin | 616d774 | 2019-01-29 16:01:48 +0100 | [diff] [blame] | 5121 | res = write_message(buf.buf, buf.len, file, 0); |
Alban Gruin | cbef27d | 2019-01-29 16:01:47 +0100 | [diff] [blame] | 5122 | strbuf_release(&buf); |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 5123 | |
| 5124 | return res; |
| 5125 | } |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5126 | |
| 5127 | /* skip picking commits whose parents are unchanged */ |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5128 | static int skip_unnecessary_picks(struct repository *r, |
| 5129 | struct todo_list *todo_list, |
| 5130 | struct object_id *base_oid) |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5131 | { |
Alban Gruin | d4ed5d7 | 2018-08-10 18:51:36 +0200 | [diff] [blame] | 5132 | struct object_id *parent_oid; |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5133 | int i; |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5134 | |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5135 | for (i = 0; i < todo_list->nr; i++) { |
| 5136 | struct todo_item *item = todo_list->items + i; |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5137 | |
| 5138 | if (item->command >= TODO_NOOP) |
| 5139 | continue; |
| 5140 | if (item->command != TODO_PICK) |
| 5141 | break; |
| 5142 | if (parse_commit(item->commit)) { |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5143 | return error(_("could not parse commit '%s'"), |
| 5144 | oid_to_hex(&item->commit->object.oid)); |
| 5145 | } |
| 5146 | if (!item->commit->parents) |
| 5147 | break; /* root commit */ |
| 5148 | if (item->commit->parents->next) |
| 5149 | break; /* merge commit */ |
| 5150 | parent_oid = &item->commit->parents->item->object.oid; |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5151 | if (!oideq(parent_oid, base_oid)) |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5152 | break; |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5153 | oidcpy(base_oid, &item->commit->object.oid); |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5154 | } |
| 5155 | if (i > 0) { |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5156 | const char *done_path = rebase_path_done(); |
| 5157 | |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5158 | if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) { |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5159 | error_errno(_("could not write to '%s'"), done_path); |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5160 | return -1; |
| 5161 | } |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5162 | |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5163 | MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i); |
| 5164 | todo_list->nr -= i; |
| 5165 | todo_list->current = 0; |
Alban Gruin | 3406554 | 2019-11-24 18:43:29 +0100 | [diff] [blame] | 5166 | todo_list->done_nr += i; |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5167 | |
| 5168 | if (is_fixup(peek_command(todo_list, 0))) |
| 5169 | record_in_rewritten(base_oid, peek_command(todo_list, 0)); |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 5172 | return 0; |
| 5173 | } |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5174 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 5175 | int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5176 | const char *shortrevisions, const char *onto_name, |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 5177 | struct commit *onto, const char *orig_head, |
| 5178 | struct string_list *commands, unsigned autosquash, |
| 5179 | struct todo_list *todo_list) |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5180 | { |
| 5181 | const char *shortonto, *todo_file = rebase_path_todo(); |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5182 | struct todo_list new_todo = TODO_LIST_INIT; |
Johannes Schindelin | b699226 | 2020-01-23 12:28:18 +0000 | [diff] [blame] | 5183 | struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT; |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 5184 | struct object_id oid = onto->object.oid; |
Alban Gruin | 1451d0f | 2019-03-05 20:18:04 +0100 | [diff] [blame] | 5185 | int res; |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5186 | |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5187 | shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV); |
| 5188 | |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5189 | if (buf->len == 0) { |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5190 | struct todo_item *item = append_new_todo(todo_list); |
| 5191 | item->command = TODO_NOOP; |
| 5192 | item->commit = NULL; |
| 5193 | item->arg_len = item->arg_offset = item->flags = item->offset_in_buf = 0; |
| 5194 | } |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5195 | |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5196 | if (autosquash && todo_list_rearrange_squash(todo_list)) |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5197 | return -1; |
| 5198 | |
Alban Gruin | 683153a | 2019-03-05 20:17:54 +0100 | [diff] [blame] | 5199 | if (commands->nr) |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5200 | todo_list_add_exec_commands(todo_list, commands); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5201 | |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5202 | if (count_commands(todo_list) == 0) { |
Denton Liu | be1bb60 | 2020-04-07 10:27:56 -0400 | [diff] [blame] | 5203 | apply_autostash(rebase_path_autostash()); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5204 | sequencer_remove_state(opts); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5205 | |
| 5206 | return error(_("nothing to do")); |
| 5207 | } |
| 5208 | |
Alban Gruin | 1451d0f | 2019-03-05 20:18:04 +0100 | [diff] [blame] | 5209 | res = edit_todo_list(r, todo_list, &new_todo, shortrevisions, |
| 5210 | shortonto, flags); |
| 5211 | if (res == -1) |
| 5212 | return -1; |
| 5213 | else if (res == -2) { |
Denton Liu | be1bb60 | 2020-04-07 10:27:56 -0400 | [diff] [blame] | 5214 | apply_autostash(rebase_path_autostash()); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5215 | sequencer_remove_state(opts); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5216 | |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5217 | return -1; |
Alban Gruin | 1451d0f | 2019-03-05 20:18:04 +0100 | [diff] [blame] | 5218 | } else if (res == -3) { |
Denton Liu | be1bb60 | 2020-04-07 10:27:56 -0400 | [diff] [blame] | 5219 | apply_autostash(rebase_path_autostash()); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5220 | sequencer_remove_state(opts); |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5221 | todo_list_release(&new_todo); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5222 | |
| 5223 | return error(_("nothing to do")); |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 5224 | } else if (res == -4) { |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 5225 | checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head); |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5226 | todo_list_release(&new_todo); |
| 5227 | |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5228 | return -1; |
| 5229 | } |
| 5230 | |
Johannes Schindelin | b699226 | 2020-01-23 12:28:18 +0000 | [diff] [blame] | 5231 | /* Expand the commit IDs */ |
| 5232 | todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0); |
| 5233 | strbuf_swap(&new_todo.buf, &buf2); |
| 5234 | strbuf_release(&buf2); |
| 5235 | new_todo.total_nr -= new_todo.nr; |
| 5236 | if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) < 0) |
| 5237 | BUG("invalid todo list after expanding IDs:\n%s", |
| 5238 | new_todo.buf.buf); |
| 5239 | |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5240 | if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) { |
| 5241 | todo_list_release(&new_todo); |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5242 | return error(_("could not skip unnecessary pick commands")); |
Alban Gruin | 6bfeb7f | 2019-03-05 20:18:00 +0100 | [diff] [blame] | 5243 | } |
| 5244 | |
Alban Gruin | 94bcad7 | 2019-03-05 20:17:57 +0100 | [diff] [blame] | 5245 | if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1, |
| 5246 | flags & ~(TODO_LIST_SHORTEN_IDS))) { |
| 5247 | todo_list_release(&new_todo); |
| 5248 | return error_errno(_("could not write '%s'"), todo_file); |
| 5249 | } |
| 5250 | |
Alban Gruin | 393adf7 | 2019-11-24 18:43:32 +0100 | [diff] [blame] | 5251 | res = -1; |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5252 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 5253 | if (checkout_onto(r, opts, onto_name, &oid, orig_head)) |
Alban Gruin | 393adf7 | 2019-11-24 18:43:32 +0100 | [diff] [blame] | 5254 | goto cleanup; |
Nguyễn Thái Ngọc Duy | 29d51e2 | 2018-11-03 15:32:29 +0100 | [diff] [blame] | 5255 | |
Nguyễn Thái Ngọc Duy | 005af33 | 2018-11-10 06:48:57 +0100 | [diff] [blame] | 5256 | if (require_clean_work_tree(r, "rebase", "", 1, 1)) |
Alban Gruin | 393adf7 | 2019-11-24 18:43:32 +0100 | [diff] [blame] | 5257 | goto cleanup; |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5258 | |
Alban Gruin | 393adf7 | 2019-11-24 18:43:32 +0100 | [diff] [blame] | 5259 | todo_list_write_total_nr(&new_todo); |
| 5260 | res = pick_commits(r, &new_todo, opts); |
| 5261 | |
| 5262 | cleanup: |
| 5263 | todo_list_release(&new_todo); |
| 5264 | |
| 5265 | return res; |
Alban Gruin | b97e187 | 2018-08-28 14:10:36 +0200 | [diff] [blame] | 5266 | } |
| 5267 | |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5268 | struct subject2item_entry { |
| 5269 | struct hashmap_entry entry; |
| 5270 | int i; |
| 5271 | char subject[FLEX_ARRAY]; |
| 5272 | }; |
| 5273 | |
| 5274 | static int subject2item_cmp(const void *fndata, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 5275 | const struct hashmap_entry *eptr, |
| 5276 | const struct hashmap_entry *entry_or_key, |
| 5277 | const void *key) |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5278 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 5279 | const struct subject2item_entry *a, *b; |
| 5280 | |
| 5281 | a = container_of(eptr, const struct subject2item_entry, entry); |
| 5282 | b = container_of(entry_or_key, const struct subject2item_entry, entry); |
| 5283 | |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5284 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); |
| 5285 | } |
| 5286 | |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5287 | define_commit_slab(commit_todo_item, struct todo_item *); |
| 5288 | |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5289 | /* |
| 5290 | * Rearrange the todo list that has both "pick commit-id msg" and "pick |
| 5291 | * commit-id fixup!/squash! msg" in it so that the latter is put immediately |
| 5292 | * after the former, and change "pick" to "fixup"/"squash". |
| 5293 | * |
| 5294 | * Note that if the config has specified a custom instruction format, each log |
| 5295 | * message will have to be retrieved from the commit (as the oneline in the |
| 5296 | * script cannot be trusted) in order to normalize the autosquash arrangement. |
| 5297 | */ |
Alban Gruin | 79d7e88 | 2019-03-05 20:17:59 +0100 | [diff] [blame] | 5298 | int todo_list_rearrange_squash(struct todo_list *todo_list) |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5299 | { |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5300 | struct hashmap subject2item; |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5301 | int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5302 | char **subjects; |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5303 | struct commit_todo_item commit_todo; |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5304 | struct todo_item *items = NULL; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5305 | |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5306 | init_commit_todo_item(&commit_todo); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5307 | /* |
| 5308 | * The hashmap maps onelines to the respective todo list index. |
| 5309 | * |
| 5310 | * If any items need to be rearranged, the next[i] value will indicate |
| 5311 | * which item was moved directly after the i'th. |
| 5312 | * |
| 5313 | * In that case, last[i] will indicate the index of the latest item to |
| 5314 | * be moved to appear after the i'th. |
| 5315 | */ |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 5316 | hashmap_init(&subject2item, subject2item_cmp, NULL, todo_list->nr); |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5317 | ALLOC_ARRAY(next, todo_list->nr); |
| 5318 | ALLOC_ARRAY(tail, todo_list->nr); |
| 5319 | ALLOC_ARRAY(subjects, todo_list->nr); |
| 5320 | for (i = 0; i < todo_list->nr; i++) { |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5321 | struct strbuf buf = STRBUF_INIT; |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5322 | struct todo_item *item = todo_list->items + i; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5323 | const char *commit_buffer, *subject, *p; |
| 5324 | size_t subject_len; |
| 5325 | int i2 = -1; |
| 5326 | struct subject2item_entry *entry; |
| 5327 | |
| 5328 | next[i] = tail[i] = -1; |
Johannes Schindelin | 2f6b1d1 | 2018-04-25 14:28:25 +0200 | [diff] [blame] | 5329 | if (!item->commit || item->command == TODO_DROP) { |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5330 | subjects[i] = NULL; |
| 5331 | continue; |
| 5332 | } |
| 5333 | |
| 5334 | if (is_fixup(item->command)) { |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5335 | clear_commit_todo_item(&commit_todo); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5336 | return error(_("the script was already rearranged.")); |
| 5337 | } |
| 5338 | |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5339 | *commit_todo_item_at(&commit_todo, item->commit) = item; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5340 | |
| 5341 | parse_commit(item->commit); |
Doan Tran Cong Danh | 0798d16 | 2019-11-08 16:43:46 +0700 | [diff] [blame] | 5342 | commit_buffer = logmsg_reencode(item->commit, NULL, "UTF-8"); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5343 | find_commit_subject(commit_buffer, &subject); |
| 5344 | format_subject(&buf, subject, " "); |
| 5345 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); |
| 5346 | unuse_commit_buffer(item->commit, commit_buffer); |
| 5347 | if ((skip_prefix(subject, "fixup! ", &p) || |
| 5348 | skip_prefix(subject, "squash! ", &p))) { |
| 5349 | struct commit *commit2; |
| 5350 | |
| 5351 | for (;;) { |
| 5352 | while (isspace(*p)) |
| 5353 | p++; |
| 5354 | if (!skip_prefix(p, "fixup! ", &p) && |
| 5355 | !skip_prefix(p, "squash! ", &p)) |
| 5356 | break; |
| 5357 | } |
| 5358 | |
Eric Wong | f23a465 | 2019-10-06 23:30:36 +0000 | [diff] [blame] | 5359 | entry = hashmap_get_entry_from_hash(&subject2item, |
| 5360 | strhash(p), p, |
| 5361 | struct subject2item_entry, |
| 5362 | entry); |
| 5363 | if (entry) |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5364 | /* found by title */ |
| 5365 | i2 = entry->i; |
| 5366 | else if (!strchr(p, ' ') && |
| 5367 | (commit2 = |
| 5368 | lookup_commit_reference_by_name(p)) && |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5369 | *commit_todo_item_at(&commit_todo, commit2)) |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5370 | /* found by commit name */ |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5371 | i2 = *commit_todo_item_at(&commit_todo, commit2) |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5372 | - todo_list->items; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5373 | else { |
| 5374 | /* copy can be a prefix of the commit subject */ |
| 5375 | for (i2 = 0; i2 < i; i2++) |
| 5376 | if (subjects[i2] && |
| 5377 | starts_with(subjects[i2], p)) |
| 5378 | break; |
| 5379 | if (i2 == i) |
| 5380 | i2 = -1; |
| 5381 | } |
| 5382 | } |
| 5383 | if (i2 >= 0) { |
| 5384 | rearranged = 1; |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5385 | todo_list->items[i].command = |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5386 | starts_with(subject, "fixup!") ? |
| 5387 | TODO_FIXUP : TODO_SQUASH; |
| 5388 | if (next[i2] < 0) |
| 5389 | next[i2] = i; |
| 5390 | else |
| 5391 | next[tail[i2]] = i; |
| 5392 | tail[i2] = i; |
| 5393 | } else if (!hashmap_get_from_hash(&subject2item, |
| 5394 | strhash(subject), subject)) { |
| 5395 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); |
| 5396 | entry->i = i; |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 5397 | hashmap_entry_init(&entry->entry, |
| 5398 | strhash(entry->subject)); |
Eric Wong | 26b455f | 2019-10-06 23:30:32 +0000 | [diff] [blame] | 5399 | hashmap_put(&subject2item, &entry->entry); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | if (rearranged) { |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5404 | for (i = 0; i < todo_list->nr; i++) { |
| 5405 | enum todo_command command = todo_list->items[i].command; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5406 | int cur = i; |
| 5407 | |
| 5408 | /* |
| 5409 | * Initially, all commands are 'pick's. If it is a |
| 5410 | * fixup or a squash now, we have rearranged it. |
| 5411 | */ |
| 5412 | if (is_fixup(command)) |
| 5413 | continue; |
| 5414 | |
| 5415 | while (cur >= 0) { |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5416 | ALLOC_GROW(items, nr + 1, alloc); |
| 5417 | items[nr++] = todo_list->items[cur]; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5418 | cur = next[cur]; |
| 5419 | } |
| 5420 | } |
| 5421 | |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5422 | FREE_AND_NULL(todo_list->items); |
| 5423 | todo_list->items = items; |
| 5424 | todo_list->nr = nr; |
| 5425 | todo_list->alloc = alloc; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | free(next); |
| 5429 | free(tail); |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5430 | for (i = 0; i < todo_list->nr; i++) |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5431 | free(subjects[i]); |
| 5432 | free(subjects); |
Eric Wong | c8e424c | 2019-10-06 23:30:40 +0000 | [diff] [blame] | 5433 | hashmap_free_entries(&subject2item, struct subject2item_entry, entry); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5434 | |
Nguyễn Thái Ngọc Duy | 3cc0287 | 2018-05-19 07:28:23 +0200 | [diff] [blame] | 5435 | clear_commit_todo_item(&commit_todo); |
Alban Gruin | f2a0490 | 2019-03-05 20:17:55 +0100 | [diff] [blame] | 5436 | |
| 5437 | return 0; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 5438 | } |
Phillip Wood | 901ba7b | 2019-12-06 16:06:11 +0000 | [diff] [blame] | 5439 | |
| 5440 | int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) |
| 5441 | { |
| 5442 | if (file_exists(git_path_cherry_pick_head(r))) { |
Phillip Wood | 430b75f | 2019-12-06 16:06:12 +0000 | [diff] [blame] | 5443 | struct object_id cherry_pick_head, rebase_head; |
| 5444 | |
| 5445 | if (file_exists(git_path_seq_dir())) |
| 5446 | *whence = FROM_CHERRY_PICK_MULTI; |
| 5447 | if (file_exists(rebase_path()) && |
| 5448 | !get_oid("REBASE_HEAD", &rebase_head) && |
| 5449 | !get_oid("CHERRY_PICK_HEAD", &cherry_pick_head) && |
| 5450 | oideq(&rebase_head, &cherry_pick_head)) |
| 5451 | *whence = FROM_REBASE_PICK; |
| 5452 | else |
| 5453 | *whence = FROM_CHERRY_PICK_SINGLE; |
| 5454 | |
Phillip Wood | 901ba7b | 2019-12-06 16:06:11 +0000 | [diff] [blame] | 5455 | return 1; |
| 5456 | } |
| 5457 | |
| 5458 | return 0; |
| 5459 | } |