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" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 5 | #include "object.h" |
| 6 | #include "commit.h" |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 7 | #include "sequencer.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 8 | #include "tag.h" |
| 9 | #include "run-command.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 10 | #include "exec-cmd.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 11 | #include "utf8.h" |
| 12 | #include "cache-tree.h" |
| 13 | #include "diff.h" |
| 14 | #include "revision.h" |
| 15 | #include "rerere.h" |
| 16 | #include "merge-recursive.h" |
| 17 | #include "refs.h" |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 18 | #include "argv-array.h" |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 19 | #include "quote.h" |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 20 | #include "trailer.h" |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 21 | #include "log-tree.h" |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 22 | #include "wt-status.h" |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 23 | #include "hashmap.h" |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 24 | #include "notes-utils.h" |
| 25 | #include "sigchain.h" |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 26 | #include "unpack-trees.h" |
| 27 | #include "worktree.h" |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 28 | #include "oidmap.h" |
| 29 | #include "oidset.h" |
Nguyễn Thái Ngọc Duy | 65b5f94 | 2018-05-20 20:40:06 +0200 | [diff] [blame] | 30 | #include "alias.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 31 | |
| 32 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 33 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 34 | const char sign_off_header[] = "Signed-off-by: "; |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 35 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 36 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 37 | GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
| 38 | |
Johannes Schindelin | 8a2a0f5 | 2016-10-14 15:17:12 +0200 | [diff] [blame] | 39 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
| 40 | |
| 41 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") |
| 42 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") |
| 43 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 44 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 45 | |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 46 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
| 47 | /* |
| 48 | * The file containing rebase commands, comments, and empty lines. |
| 49 | * This file is created by "git rebase -i" then edited by the user. As |
| 50 | * the lines are processed, they are removed from the front of this |
| 51 | * file and written to the tail of 'done'. |
| 52 | */ |
| 53 | static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 54 | /* |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 55 | * The rebase command lines that have already been processed. A line |
| 56 | * is moved here when it is first handled, before any associated user |
| 57 | * actions. |
| 58 | */ |
| 59 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") |
| 60 | /* |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 61 | * The file to keep track of how many commands were already processed (e.g. |
| 62 | * for the prompt). |
| 63 | */ |
| 64 | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum"); |
| 65 | /* |
| 66 | * The file to keep track of how many commands are to be processed in total |
| 67 | * (e.g. for the prompt). |
| 68 | */ |
| 69 | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end"); |
| 70 | /* |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 71 | * The commit message that is planned to be used for any changes that |
| 72 | * need to be committed following a user interaction. |
| 73 | */ |
| 74 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") |
| 75 | /* |
| 76 | * The file into which is accumulated the suggested commit message for |
| 77 | * squash/fixup commands. When the first of a series of squash/fixups |
| 78 | * is seen, the file is created and the commit message from the |
| 79 | * previous commit and from the first squash/fixup commit are written |
| 80 | * to it. The commit message for each subsequent squash/fixup commit |
| 81 | * is appended to the file as it is processed. |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 82 | */ |
| 83 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") |
| 84 | /* |
| 85 | * If the current series of squash/fixups has not yet included a squash |
| 86 | * command, then this file exists and holds the commit message of the |
| 87 | * original "pick" commit. (If the series ends without a "squash" |
| 88 | * command, then this can be used as the commit message of the combined |
| 89 | * commit without opening the editor.) |
| 90 | */ |
| 91 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") |
| 92 | /* |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 93 | * This file contains the list fixup/squash commands that have been |
| 94 | * accumulated into message-fixup or message-squash so far. |
| 95 | */ |
| 96 | static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups") |
| 97 | /* |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 98 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
| 99 | * GIT_AUTHOR_DATE that will be used for the commit that is currently |
| 100 | * being rebased. |
| 101 | */ |
| 102 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 103 | /* |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 104 | * When an "edit" rebase command is being processed, the SHA1 of the |
| 105 | * commit to be edited is recorded in this file. When "git rebase |
| 106 | * --continue" is executed, if there are any staged changes then they |
| 107 | * will be amended to the HEAD commit, but only provided the HEAD |
| 108 | * commit is still the commit to be edited. When any other rebase |
| 109 | * command is processed, this file is deleted. |
| 110 | */ |
| 111 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") |
| 112 | /* |
| 113 | * When we stop at a given patch via the "edit" command, this file contains |
| 114 | * the abbreviated commit name of the corresponding patch. |
| 115 | */ |
| 116 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") |
| 117 | /* |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 118 | * For the post-rewrite hook, we make a list of rewritten commits and |
| 119 | * their new sha1s. The rewritten-pending list keeps the sha1s of |
| 120 | * commits that have been processed, but not committed yet, |
| 121 | * e.g. because they are waiting for a 'squash' command. |
| 122 | */ |
| 123 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") |
| 124 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, |
| 125 | "rebase-merge/rewritten-pending") |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 126 | |
| 127 | /* |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 128 | * The path of the file containig the OID of the "squash onto" commit, i.e. |
| 129 | * the dummy commit used for `reset [new root]`. |
| 130 | */ |
| 131 | static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto") |
| 132 | |
| 133 | /* |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 134 | * The path of the file listing refs that need to be deleted after the rebase |
| 135 | * finishes. This is used by the `label` command to record the need for cleanup. |
| 136 | */ |
| 137 | static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") |
| 138 | |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 139 | /* |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 140 | * The following files are written by git-rebase just after parsing the |
| 141 | * command-line (and are only consumed, not modified, by the sequencer). |
| 142 | */ |
| 143 | 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] | 144 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
| 145 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 146 | static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff") |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 147 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
| 148 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 149 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 150 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
| 151 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") |
Phillip Wood | 9b6d7a6 | 2017-08-02 11:44:17 +0100 | [diff] [blame] | 152 | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 153 | |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 154 | static int git_sequencer_config(const char *k, const char *v, void *cb) |
| 155 | { |
| 156 | struct replay_opts *opts = cb; |
| 157 | int status; |
| 158 | |
| 159 | if (!strcmp(k, "commit.cleanup")) { |
| 160 | const char *s; |
| 161 | |
| 162 | status = git_config_string(&s, k, v); |
| 163 | if (status) |
| 164 | return status; |
| 165 | |
| 166 | if (!strcmp(s, "verbatim")) |
| 167 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
| 168 | else if (!strcmp(s, "whitespace")) |
| 169 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
| 170 | else if (!strcmp(s, "strip")) |
| 171 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; |
| 172 | else if (!strcmp(s, "scissors")) |
| 173 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
| 174 | else |
| 175 | warning(_("invalid commit message cleanup mode '%s'"), |
| 176 | s); |
| 177 | |
| 178 | return status; |
| 179 | } |
| 180 | |
| 181 | if (!strcmp(k, "commit.gpgsign")) { |
Johannes Schindelin | ed1e528 | 2017-12-22 12:50:50 +0100 | [diff] [blame] | 182 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
Phillip Wood | 28d6dae | 2017-12-13 11:46:21 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | status = git_gpg_config(k, v, NULL); |
| 187 | if (status) |
| 188 | return status; |
| 189 | |
| 190 | return git_diff_basic_config(k, v, NULL); |
| 191 | } |
| 192 | |
| 193 | void sequencer_init_config(struct replay_opts *opts) |
| 194 | { |
| 195 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
| 196 | git_config(git_sequencer_config, opts); |
| 197 | } |
| 198 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 199 | static inline int is_rebase_i(const struct replay_opts *opts) |
| 200 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 201 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Johannes Schindelin | 285abf5 | 2016-10-14 15:17:20 +0200 | [diff] [blame] | 204 | static const char *get_dir(const struct replay_opts *opts) |
| 205 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 206 | if (is_rebase_i(opts)) |
| 207 | return rebase_path(); |
Johannes Schindelin | 285abf5 | 2016-10-14 15:17:20 +0200 | [diff] [blame] | 208 | return git_path_seq_dir(); |
| 209 | } |
| 210 | |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 211 | static const char *get_todo_path(const struct replay_opts *opts) |
| 212 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 213 | if (is_rebase_i(opts)) |
| 214 | return rebase_path_todo(); |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 215 | return git_path_todo_file(); |
| 216 | } |
| 217 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 218 | /* |
| 219 | * Returns 0 for non-conforming footer |
| 220 | * Returns 1 for conforming footer |
| 221 | * Returns 2 when sob exists within conforming footer |
| 222 | * Returns 3 when sob exists within conforming footer as last entry |
| 223 | */ |
| 224 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
| 225 | int ignore_footer) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 226 | { |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 227 | struct trailer_info info; |
| 228 | int i; |
| 229 | int found_sob = 0, found_sob_last = 0; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 230 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 231 | trailer_info_get(&info, sb->buf); |
| 232 | |
| 233 | if (info.trailer_start == info.trailer_end) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 234 | return 0; |
| 235 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 236 | for (i = 0; i < info.trailer_nr; i++) |
| 237 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { |
| 238 | found_sob = 1; |
| 239 | if (i == info.trailer_nr - 1) |
| 240 | found_sob_last = 1; |
| 241 | } |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 242 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 243 | trailer_info_release(&info); |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 244 | |
Jonathan Tan | 967dfd4 | 2016-11-02 10:29:20 -0700 | [diff] [blame] | 245 | if (found_sob_last) |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 246 | return 3; |
| 247 | if (found_sob) |
| 248 | return 2; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 251 | |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 252 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
| 253 | { |
| 254 | static struct strbuf buf = STRBUF_INIT; |
| 255 | |
| 256 | strbuf_reset(&buf); |
| 257 | if (opts->gpg_sign) |
| 258 | sq_quotef(&buf, "-S%s", opts->gpg_sign); |
| 259 | return buf.buf; |
| 260 | } |
| 261 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 262 | int sequencer_remove_state(struct replay_opts *opts) |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 263 | { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 264 | struct strbuf buf = STRBUF_INIT; |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 265 | int i; |
| 266 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 267 | if (is_rebase_i(opts) && |
| 268 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { |
| 269 | char *p = buf.buf; |
| 270 | while (*p) { |
| 271 | char *eol = strchr(p, '\n'); |
| 272 | if (eol) |
| 273 | *eol = '\0'; |
| 274 | if (delete_ref("(rebase -i) cleanup", p, NULL, 0) < 0) |
| 275 | warning(_("could not delete '%s'"), p); |
| 276 | if (!eol) |
| 277 | break; |
| 278 | p = eol + 1; |
| 279 | } |
| 280 | } |
| 281 | |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 282 | free(opts->gpg_sign); |
| 283 | free(opts->strategy); |
| 284 | for (i = 0; i < opts->xopts_nr; i++) |
| 285 | free(opts->xopts[i]); |
| 286 | free(opts->xopts); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 287 | strbuf_release(&opts->current_fixups); |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 288 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 289 | strbuf_reset(&buf); |
| 290 | strbuf_addstr(&buf, get_dir(opts)); |
| 291 | remove_dir_recursively(&buf, 0); |
| 292 | strbuf_release(&buf); |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 293 | |
| 294 | return 0; |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 295 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 296 | |
| 297 | static const char *action_name(const struct replay_opts *opts) |
| 298 | { |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 299 | switch (opts->action) { |
| 300 | case REPLAY_REVERT: |
| 301 | return N_("revert"); |
| 302 | case REPLAY_PICK: |
| 303 | return N_("cherry-pick"); |
| 304 | case REPLAY_INTERACTIVE_REBASE: |
| 305 | return N_("rebase -i"); |
| 306 | } |
| 307 | die(_("Unknown action: %d"), opts->action); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 308 | } |
| 309 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 310 | struct commit_message { |
| 311 | char *parent_label; |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 312 | char *label; |
| 313 | char *subject; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 314 | const char *message; |
| 315 | }; |
| 316 | |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 317 | static const char *short_commit_name(struct commit *commit) |
| 318 | { |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 319 | return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV); |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 322 | static int get_message(struct commit *commit, struct commit_message *out) |
| 323 | { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 324 | const char *abbrev, *subject; |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 325 | int subject_len; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 326 | |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 327 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 328 | abbrev = short_commit_name(commit); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 329 | |
| 330 | subject_len = find_commit_subject(out->message, &subject); |
| 331 | |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 332 | out->subject = xmemdupz(subject, subject_len); |
| 333 | out->label = xstrfmt("%s... %s", abbrev, out->subject); |
| 334 | out->parent_label = xstrfmt("parent of %s", out->label); |
| 335 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 339 | static void free_message(struct commit *commit, struct commit_message *msg) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 340 | { |
| 341 | free(msg->parent_label); |
Jeff King | 7b35eaf | 2016-02-22 17:44:57 -0500 | [diff] [blame] | 342 | free(msg->label); |
| 343 | free(msg->subject); |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 344 | unuse_commit_buffer(commit, msg->message); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 345 | } |
| 346 | |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 347 | static void print_advice(int show_hint, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 348 | { |
| 349 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 350 | |
| 351 | if (msg) { |
| 352 | fprintf(stderr, "%s\n", msg); |
| 353 | /* |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 354 | * A conflict has occurred but the porcelain |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 355 | * (typically rebase --interactive) wants to take care |
| 356 | * of the commit itself so remove CHERRY_PICK_HEAD |
| 357 | */ |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 358 | unlink(git_path_cherry_pick_head()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 362 | if (show_hint) { |
| 363 | if (opts->no_commit) |
| 364 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 365 | "with 'git add <paths>' or 'git rm <paths>'")); |
| 366 | else |
| 367 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 368 | "with 'git add <paths>' or 'git rm <paths>'\n" |
| 369 | "and commit the result with 'git commit'")); |
| 370 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 371 | } |
| 372 | |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 373 | static int write_message(const void *buf, size_t len, const char *filename, |
| 374 | int append_eol) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 375 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 376 | struct lock_file msg_file = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 377 | |
Johannes Schindelin | 4ef3d8f | 2016-09-09 16:37:05 +0200 | [diff] [blame] | 378 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
| 379 | if (msg_fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 380 | return error_errno(_("could not lock '%s'"), filename); |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 381 | if (write_in_full(msg_fd, buf, len) < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 382 | error_errno(_("could not write to '%s'"), filename); |
Johannes Schindelin | 4f66c83 | 2016-10-21 14:26:00 +0200 | [diff] [blame] | 383 | rollback_lock_file(&msg_file); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 384 | return -1; |
Johannes Schindelin | 4f66c83 | 2016-10-21 14:26:00 +0200 | [diff] [blame] | 385 | } |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 386 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 387 | error_errno(_("could not write eol to '%s'"), filename); |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 388 | rollback_lock_file(&msg_file); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 389 | return -1; |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 390 | } |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 391 | if (commit_lock_file(&msg_file) < 0) |
| 392 | return error(_("failed to finalize '%s'"), filename); |
Johannes Schindelin | 4ef3d8f | 2016-09-09 16:37:05 +0200 | [diff] [blame] | 393 | |
| 394 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 395 | } |
| 396 | |
Johannes Schindelin | 1dfc84e | 2016-10-21 14:25:08 +0200 | [diff] [blame] | 397 | /* |
| 398 | * Reads a file that was presumably written by a shell script, i.e. with an |
| 399 | * end-of-line marker that needs to be stripped. |
| 400 | * |
| 401 | * Note that only the last end-of-line marker is stripped, consistent with the |
| 402 | * behavior of "$(cat path)" in a shell script. |
| 403 | * |
| 404 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. |
| 405 | */ |
| 406 | static int read_oneliner(struct strbuf *buf, |
| 407 | const char *path, int skip_if_empty) |
| 408 | { |
| 409 | int orig_len = buf->len; |
| 410 | |
| 411 | if (!file_exists(path)) |
| 412 | return 0; |
| 413 | |
| 414 | if (strbuf_read_file(buf, path, 0) < 0) { |
| 415 | warning_errno(_("could not read '%s'"), path); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { |
| 420 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') |
| 421 | --buf->len; |
| 422 | buf->buf[buf->len] = '\0'; |
| 423 | } |
| 424 | |
| 425 | if (skip_if_empty && buf->len == orig_len) |
| 426 | return 0; |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 431 | static struct tree *empty_tree(void) |
| 432 | { |
brian m. carlson | eb0ccfd | 2017-11-12 21:28:54 +0000 | [diff] [blame] | 433 | return lookup_tree(the_hash_algo->empty_tree); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static int error_dirty_index(struct replay_opts *opts) |
| 437 | { |
| 438 | if (read_cache_unmerged()) |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 439 | return error_resolve_conflict(_(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 440 | |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 441 | error(_("your local changes would be overwritten by %s."), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 442 | _(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 443 | |
| 444 | if (advice_commit_before_merge) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 445 | advise(_("commit your changes or stash them to proceed.")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 449 | static void update_abort_safety_file(void) |
| 450 | { |
| 451 | struct object_id head; |
| 452 | |
| 453 | /* Do nothing on a single-pick */ |
| 454 | if (!file_exists(git_path_seq_dir())) |
| 455 | return; |
| 456 | |
| 457 | if (!get_oid("HEAD", &head)) |
| 458 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); |
| 459 | else |
| 460 | write_file(git_path_abort_safety_file(), "%s", ""); |
| 461 | } |
| 462 | |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 463 | static int fast_forward_to(const struct object_id *to, const struct object_id *from, |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 464 | int unborn, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 465 | { |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 466 | struct ref_transaction *transaction; |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 467 | struct strbuf sb = STRBUF_INIT; |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 468 | struct strbuf err = STRBUF_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 469 | |
| 470 | read_cache(); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 471 | if (checkout_fast_forward(from, to, 1)) |
Johannes Schindelin | 0e408fc | 2016-09-09 16:37:55 +0200 | [diff] [blame] | 472 | return -1; /* the callee should have complained already */ |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 473 | |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 474 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 475 | |
| 476 | transaction = ref_transaction_begin(&err); |
| 477 | if (!transaction || |
| 478 | ref_transaction_update(transaction, "HEAD", |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 479 | to, unborn && !is_rebase_i(opts) ? |
| 480 | &null_oid : from, |
Michael Haggerty | 1d147bd | 2015-02-17 18:00:15 +0100 | [diff] [blame] | 481 | 0, sb.buf, &err) || |
Ronnie Sahlberg | db7516a | 2014-04-30 12:22:42 -0700 | [diff] [blame] | 482 | ref_transaction_commit(transaction, &err)) { |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 483 | ref_transaction_free(transaction); |
| 484 | error("%s", err.buf); |
| 485 | strbuf_release(&sb); |
| 486 | strbuf_release(&err); |
| 487 | return -1; |
| 488 | } |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 489 | |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 490 | strbuf_release(&sb); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 491 | strbuf_release(&err); |
| 492 | ref_transaction_free(transaction); |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 493 | update_abort_safety_file(); |
Ronnie Sahlberg | d668d16 | 2014-04-16 15:37:45 -0700 | [diff] [blame] | 494 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 495 | } |
| 496 | |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 497 | void append_conflicts_hint(struct strbuf *msgbuf) |
| 498 | { |
| 499 | int i; |
| 500 | |
Junio C Hamano | 261f315 | 2014-10-28 13:04:38 -0700 | [diff] [blame] | 501 | strbuf_addch(msgbuf, '\n'); |
| 502 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 503 | for (i = 0; i < active_nr;) { |
| 504 | const struct cache_entry *ce = active_cache[i++]; |
| 505 | if (ce_stage(ce)) { |
Junio C Hamano | 261f315 | 2014-10-28 13:04:38 -0700 | [diff] [blame] | 506 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 507 | while (i < active_nr && !strcmp(ce->name, |
| 508 | active_cache[i]->name)) |
| 509 | i++; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 514 | static int do_recursive_merge(struct commit *base, struct commit *next, |
| 515 | const char *base_label, const char *next_label, |
brian m. carlson | 48be4c6 | 2017-05-06 22:10:36 +0000 | [diff] [blame] | 516 | struct object_id *head, struct strbuf *msgbuf, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 517 | struct replay_opts *opts) |
| 518 | { |
| 519 | struct merge_options o; |
| 520 | struct tree *result, *next_tree, *base_tree, *head_tree; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 521 | int clean; |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 522 | char **xopt; |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 523 | struct lock_file index_lock = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 524 | |
Phillip Wood | bd58886 | 2017-11-15 10:41:25 +0000 | [diff] [blame] | 525 | if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0) |
| 526 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 527 | |
| 528 | read_cache(); |
| 529 | |
| 530 | init_merge_options(&o); |
| 531 | o.ancestor = base ? base_label : "(empty tree)"; |
| 532 | o.branch1 = "HEAD"; |
| 533 | o.branch2 = next ? next_label : "(empty tree)"; |
Johannes Schindelin | 62fdb65 | 2017-01-02 16:35:39 +0100 | [diff] [blame] | 534 | if (is_rebase_i(opts)) |
| 535 | o.buffer_output = 2; |
Elijah Newren | 9268cf4 | 2017-11-13 12:16:00 -0800 | [diff] [blame] | 536 | o.show_rename_progress = 1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 537 | |
| 538 | head_tree = parse_tree_indirect(head); |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 539 | next_tree = next ? get_commit_tree(next) : empty_tree(); |
| 540 | base_tree = base ? get_commit_tree(base) : empty_tree(); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 541 | |
| 542 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
| 543 | parse_merge_opt(&o, *xopt); |
| 544 | |
| 545 | clean = merge_trees(&o, |
| 546 | head_tree, |
| 547 | next_tree, base_tree, &result); |
Johannes Schindelin | 62fdb65 | 2017-01-02 16:35:39 +0100 | [diff] [blame] | 548 | if (is_rebase_i(opts) && clean <= 0) |
| 549 | fputs(o.obuf.buf, stdout); |
Johannes Schindelin | 548009c | 2016-08-01 13:44:53 +0200 | [diff] [blame] | 550 | strbuf_release(&o.obuf); |
Elijah Newren | b520abf | 2017-11-13 12:15:57 -0800 | [diff] [blame] | 551 | diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0); |
Martin Ågren | 6481652 | 2018-02-27 22:30:10 +0100 | [diff] [blame] | 552 | if (clean < 0) { |
| 553 | rollback_lock_file(&index_lock); |
Johannes Schindelin | f241ff0 | 2016-07-26 18:06:02 +0200 | [diff] [blame] | 554 | return clean; |
Martin Ågren | 6481652 | 2018-02-27 22:30:10 +0100 | [diff] [blame] | 555 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 556 | |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 557 | if (write_locked_index(&the_index, &index_lock, |
| 558 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 559 | /* |
| 560 | * TRANSLATORS: %s will be "revert", "cherry-pick" or |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 561 | * "rebase -i". |
| 562 | */ |
Johannes Schindelin | c527b55 | 2016-09-09 16:37:10 +0200 | [diff] [blame] | 563 | return error(_("%s: Unable to write new index file"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 564 | _(action_name(opts))); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 565 | |
Junio C Hamano | 75c961b | 2014-10-24 11:34:59 -0700 | [diff] [blame] | 566 | if (!clean) |
| 567 | append_conflicts_hint(msgbuf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 568 | |
| 569 | return !clean; |
| 570 | } |
| 571 | |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 572 | static struct object_id *get_cache_tree_oid(void) |
| 573 | { |
| 574 | if (!active_cache_tree) |
| 575 | active_cache_tree = cache_tree(); |
| 576 | |
| 577 | if (!cache_tree_fully_valid(active_cache_tree)) |
| 578 | if (cache_tree_update(&the_index, 0)) { |
| 579 | error(_("unable to update cache tree")); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | return &active_cache_tree->oid; |
| 584 | } |
| 585 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 586 | static int is_index_unchanged(void) |
| 587 | { |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 588 | struct object_id head_oid, *cache_tree_oid; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 589 | struct commit *head_commit; |
| 590 | |
brian m. carlson | 49e6147 | 2017-10-15 22:07:09 +0000 | [diff] [blame] | 591 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 592 | return error(_("could not resolve HEAD commit")); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 593 | |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 594 | head_commit = lookup_commit(&head_oid); |
Neil Horman | 4b58006 | 2012-05-03 08:10:22 -0400 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * If head_commit is NULL, check_commit, called from |
| 598 | * lookup_commit, would have indicated that head_commit is not |
| 599 | * a commit object already. parse_commit() will return failure |
| 600 | * without further complaints in such a case. Otherwise, if |
| 601 | * the commit is invalid, parse_commit() will complain. So |
| 602 | * there is nothing for us to say here. Just return failure. |
| 603 | */ |
| 604 | if (parse_commit(head_commit)) |
| 605 | return -1; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 606 | |
Johannes Schindelin | ba97aea | 2018-05-04 01:01:15 +0200 | [diff] [blame] | 607 | if (!(cache_tree_oid = get_cache_tree_oid())) |
| 608 | return -1; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 609 | |
Junio C Hamano | c5aa4bc | 2018-05-30 14:04:04 +0900 | [diff] [blame] | 610 | return !oidcmp(cache_tree_oid, get_commit_tree_oid(head_commit)); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 611 | } |
| 612 | |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 613 | static int write_author_script(const char *message) |
| 614 | { |
| 615 | struct strbuf buf = STRBUF_INIT; |
| 616 | const char *eol; |
| 617 | int res; |
| 618 | |
| 619 | for (;;) |
| 620 | if (!*message || starts_with(message, "\n")) { |
| 621 | missing_author: |
| 622 | /* Missing 'author' line? */ |
| 623 | unlink(rebase_path_author_script()); |
| 624 | return 0; |
| 625 | } else if (skip_prefix(message, "author ", &message)) |
| 626 | break; |
| 627 | else if ((eol = strchr(message, '\n'))) |
| 628 | message = eol + 1; |
| 629 | else |
| 630 | goto missing_author; |
| 631 | |
| 632 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); |
| 633 | while (*message && *message != '\n' && *message != '\r') |
| 634 | if (skip_prefix(message, " <", &message)) |
| 635 | break; |
| 636 | else if (*message != '\'') |
| 637 | strbuf_addch(&buf, *(message++)); |
| 638 | else |
| 639 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); |
| 640 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); |
| 641 | while (*message && *message != '\n' && *message != '\r') |
| 642 | if (skip_prefix(message, "> ", &message)) |
| 643 | break; |
| 644 | else if (*message != '\'') |
| 645 | strbuf_addch(&buf, *(message++)); |
| 646 | else |
| 647 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); |
| 648 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); |
| 649 | while (*message && *message != '\n' && *message != '\r') |
| 650 | if (*message != '\'') |
| 651 | strbuf_addch(&buf, *(message++)); |
| 652 | else |
| 653 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); |
| 654 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); |
| 655 | strbuf_release(&buf); |
| 656 | return res; |
| 657 | } |
| 658 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 659 | /* |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 660 | * Read a list of environment variable assignments (such as the author-script |
| 661 | * file) into an environment block. Returns -1 on error, 0 otherwise. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 662 | */ |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 663 | static int read_env_script(struct argv_array *env) |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 664 | { |
| 665 | struct strbuf script = STRBUF_INIT; |
| 666 | int i, count = 0; |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 667 | char *p, *p2; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 668 | |
| 669 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 670 | return -1; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 671 | |
| 672 | for (p = script.buf; *p; p++) |
| 673 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) |
| 674 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); |
| 675 | else if (*p == '\'') |
| 676 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); |
| 677 | else if (*p == '\n') { |
| 678 | *p = '\0'; |
| 679 | count++; |
| 680 | } |
| 681 | |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 682 | for (i = 0, p = script.buf; i < count; i++) { |
| 683 | argv_array_push(env, p); |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 684 | p += strlen(p) + 1; |
| 685 | } |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 686 | |
Johannes Schindelin | a2a20b0 | 2017-01-02 16:35:25 +0100 | [diff] [blame] | 687 | return 0; |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 690 | static char *get_author(const char *message) |
| 691 | { |
| 692 | size_t len; |
| 693 | const char *a; |
| 694 | |
| 695 | a = find_commit_header(message, "author", &len); |
| 696 | if (a) |
| 697 | return xmemdupz(a, len); |
| 698 | |
| 699 | return NULL; |
| 700 | } |
| 701 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 702 | /* Read author-script and return an ident line (author <email> timestamp) */ |
| 703 | static const char *read_author_ident(struct strbuf *buf) |
| 704 | { |
| 705 | const char *keys[] = { |
| 706 | "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE=" |
| 707 | }; |
| 708 | char *in, *out, *eol; |
| 709 | int i = 0, len; |
| 710 | |
| 711 | if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0) |
| 712 | return NULL; |
| 713 | |
| 714 | /* dequote values and construct ident line in-place */ |
| 715 | for (in = out = buf->buf; i < 3 && in - buf->buf < buf->len; i++) { |
| 716 | if (!skip_prefix(in, keys[i], (const char **)&in)) { |
| 717 | warning("could not parse '%s' (looking for '%s'", |
| 718 | rebase_path_author_script(), keys[i]); |
| 719 | return NULL; |
| 720 | } |
| 721 | |
| 722 | eol = strchrnul(in, '\n'); |
| 723 | *eol = '\0'; |
| 724 | sq_dequote(in); |
| 725 | len = strlen(in); |
| 726 | |
| 727 | if (i > 0) /* separate values by spaces */ |
| 728 | *(out++) = ' '; |
| 729 | if (i == 1) /* email needs to be surrounded by <...> */ |
| 730 | *(out++) = '<'; |
| 731 | memmove(out, in, len); |
| 732 | out += len; |
| 733 | if (i == 1) /* email needs to be surrounded by <...> */ |
| 734 | *(out++) = '>'; |
| 735 | in = eol + 1; |
| 736 | } |
| 737 | |
| 738 | if (i < 3) { |
| 739 | warning("could not parse '%s' (looking for '%s')", |
| 740 | rebase_path_author_script(), keys[i]); |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | buf->len = out - buf->buf; |
| 745 | return buf->buf; |
| 746 | } |
| 747 | |
Johannes Schindelin | 791eb87 | 2016-10-21 14:26:32 +0200 | [diff] [blame] | 748 | static const char staged_changes_advice[] = |
| 749 | N_("you have staged changes in your working tree\n" |
| 750 | "If these changes are meant to be squashed into the previous commit, run:\n" |
| 751 | "\n" |
| 752 | " git commit --amend %s\n" |
| 753 | "\n" |
| 754 | "If they are meant to go into a new commit, run:\n" |
| 755 | "\n" |
| 756 | " git commit %s\n" |
| 757 | "\n" |
| 758 | "In both cases, once you're done, continue with:\n" |
| 759 | "\n" |
| 760 | " git rebase --continue\n"); |
| 761 | |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 762 | #define ALLOW_EMPTY (1<<0) |
| 763 | #define EDIT_MSG (1<<1) |
| 764 | #define AMEND_MSG (1<<2) |
| 765 | #define CLEANUP_MSG (1<<3) |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 766 | #define VERIFY_MSG (1<<4) |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 767 | #define CREATE_ROOT_COMMIT (1<<5) |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 768 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 769 | /* |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 770 | * If we are cherry-pick, and if the merge did not result in |
| 771 | * hand-editing, we will hit this commit and inherit the original |
| 772 | * author date and name. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 773 | * |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 774 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 775 | * we had better say that the current user is responsible for that. |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 776 | * |
| 777 | * An exception is when run_git_commit() is called during an |
| 778 | * interactive rebase: in that case, we will want to retain the |
| 779 | * author metadata. |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 780 | */ |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 781 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 782 | unsigned int flags) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 783 | { |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 784 | struct child_process cmd = CHILD_PROCESS_INIT; |
Michael J Gruber | 17d65f0 | 2015-03-06 14:55:32 +0100 | [diff] [blame] | 785 | const char *value; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 786 | |
Johannes Schindelin | 76fda6e | 2018-06-16 21:00:38 +0200 | [diff] [blame] | 787 | if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) { |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 788 | struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT; |
| 789 | const char *author = is_rebase_i(opts) ? |
| 790 | read_author_ident(&script) : NULL; |
| 791 | struct object_id root_commit, *cache_tree_oid; |
| 792 | int res = 0; |
| 793 | |
| 794 | if (!defmsg) |
| 795 | BUG("root commit without message"); |
| 796 | |
| 797 | if (!(cache_tree_oid = get_cache_tree_oid())) |
| 798 | res = -1; |
| 799 | |
| 800 | if (!res) |
| 801 | res = strbuf_read_file(&msg, defmsg, 0); |
| 802 | |
| 803 | if (res <= 0) |
| 804 | res = error_errno(_("could not read '%s'"), defmsg); |
| 805 | else |
| 806 | res = commit_tree(msg.buf, msg.len, cache_tree_oid, |
| 807 | NULL, &root_commit, author, |
| 808 | opts->gpg_sign); |
| 809 | |
| 810 | strbuf_release(&msg); |
| 811 | strbuf_release(&script); |
| 812 | if (!res) { |
| 813 | update_ref(NULL, "CHERRY_PICK_HEAD", &root_commit, NULL, |
| 814 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR); |
| 815 | res = update_ref(NULL, "HEAD", &root_commit, NULL, 0, |
| 816 | UPDATE_REFS_MSG_ON_ERR); |
| 817 | } |
| 818 | return res < 0 ? error(_("writing root commit")) : 0; |
| 819 | } |
| 820 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 821 | cmd.git_cmd = 1; |
| 822 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 823 | if (is_rebase_i(opts)) { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 824 | if (!(flags & EDIT_MSG)) { |
Johannes Schindelin | 9a757c4 | 2017-01-02 16:35:34 +0100 | [diff] [blame] | 825 | cmd.stdout_to_stderr = 1; |
| 826 | cmd.err = -1; |
| 827 | } |
| 828 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 829 | if (read_env_script(&cmd.env_array)) { |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 830 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
| 831 | |
Johannes Schindelin | 791eb87 | 2016-10-21 14:26:32 +0200 | [diff] [blame] | 832 | return error(_(staged_changes_advice), |
| 833 | gpg_opt, gpg_opt); |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 834 | } |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 837 | argv_array_push(&cmd.args, "commit"); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 838 | |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 839 | if (!(flags & VERIFY_MSG)) |
| 840 | argv_array_push(&cmd.args, "-n"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 841 | if ((flags & AMEND_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 842 | argv_array_push(&cmd.args, "--amend"); |
Jeff King | 3bdd552 | 2014-06-19 17:28:20 -0400 | [diff] [blame] | 843 | if (opts->gpg_sign) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 844 | argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 845 | if (defmsg) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 846 | argv_array_pushl(&cmd.args, "-F", defmsg, NULL); |
Johannes Schindelin | dc4b5bc | 2018-04-27 22:48:28 +0200 | [diff] [blame] | 847 | else if (!(flags & EDIT_MSG)) |
| 848 | argv_array_pushl(&cmd.args, "-C", "HEAD", NULL); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 849 | if ((flags & CLEANUP_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 850 | argv_array_push(&cmd.args, "--cleanup=strip"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 851 | if ((flags & EDIT_MSG)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 852 | argv_array_push(&cmd.args, "-e"); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 853 | else if (!(flags & CLEANUP_MSG) && |
Johannes Schindelin | 0009426 | 2016-10-21 14:25:28 +0200 | [diff] [blame] | 854 | !opts->signoff && !opts->record_origin && |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 855 | git_config_get_value("commit.cleanup", &value)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 856 | argv_array_push(&cmd.args, "--cleanup=verbatim"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 857 | |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 858 | if ((flags & ALLOW_EMPTY)) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 859 | argv_array_push(&cmd.args, "--allow-empty"); |
Neil Horman | df478b7 | 2012-04-11 16:21:53 -0400 | [diff] [blame] | 860 | |
Chris Webb | 4bee958 | 2012-08-02 11:38:51 +0100 | [diff] [blame] | 861 | if (opts->allow_empty_message) |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 862 | argv_array_push(&cmd.args, "--allow-empty-message"); |
Chris Webb | 4bee958 | 2012-08-02 11:38:51 +0100 | [diff] [blame] | 863 | |
Johannes Schindelin | 9a757c4 | 2017-01-02 16:35:34 +0100 | [diff] [blame] | 864 | if (cmd.err == -1) { |
| 865 | /* hide stderr on success */ |
| 866 | struct strbuf buf = STRBUF_INIT; |
| 867 | int rc = pipe_command(&cmd, |
| 868 | NULL, 0, |
| 869 | /* stdout is already redirected */ |
| 870 | NULL, 0, |
| 871 | &buf, 0); |
| 872 | if (rc) |
| 873 | fputs(buf.buf, stderr); |
| 874 | strbuf_release(&buf); |
| 875 | return rc; |
| 876 | } |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 877 | |
Johannes Schindelin | 07d968e | 2017-01-02 16:35:29 +0100 | [diff] [blame] | 878 | return run_command(&cmd); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 879 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 880 | |
Phillip Wood | d0aaa46 | 2017-11-10 11:09:42 +0000 | [diff] [blame] | 881 | static int rest_is_empty(const struct strbuf *sb, int start) |
| 882 | { |
| 883 | int i, eol; |
| 884 | const char *nl; |
| 885 | |
| 886 | /* Check if the rest is just whitespace and Signed-off-by's. */ |
| 887 | for (i = start; i < sb->len; i++) { |
| 888 | nl = memchr(sb->buf + i, '\n', sb->len - i); |
| 889 | if (nl) |
| 890 | eol = nl - sb->buf; |
| 891 | else |
| 892 | eol = sb->len; |
| 893 | |
| 894 | if (strlen(sign_off_header) <= eol - i && |
| 895 | starts_with(sb->buf + i, sign_off_header)) { |
| 896 | i = eol; |
| 897 | continue; |
| 898 | } |
| 899 | while (i < eol) |
| 900 | if (!isspace(sb->buf[i++])) |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | return 1; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Find out if the message in the strbuf contains only whitespace and |
| 909 | * Signed-off-by lines. |
| 910 | */ |
| 911 | int message_is_empty(const struct strbuf *sb, |
| 912 | enum commit_msg_cleanup_mode cleanup_mode) |
| 913 | { |
| 914 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
| 915 | return 0; |
| 916 | return rest_is_empty(sb, 0); |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * See if the user edited the message in the editor or left what |
| 921 | * was in the template intact |
| 922 | */ |
| 923 | int template_untouched(const struct strbuf *sb, const char *template_file, |
| 924 | enum commit_msg_cleanup_mode cleanup_mode) |
| 925 | { |
| 926 | struct strbuf tmpl = STRBUF_INIT; |
| 927 | const char *start; |
| 928 | |
| 929 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) |
| 930 | return 0; |
| 931 | |
| 932 | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) |
| 933 | return 0; |
| 934 | |
| 935 | strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); |
| 936 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) |
| 937 | start = sb->buf; |
| 938 | strbuf_release(&tmpl); |
| 939 | return rest_is_empty(sb, start - sb->buf); |
| 940 | } |
| 941 | |
Phillip Wood | 0505d60 | 2017-11-17 11:34:47 +0000 | [diff] [blame] | 942 | int update_head_with_reflog(const struct commit *old_head, |
| 943 | const struct object_id *new_head, |
| 944 | const char *action, const struct strbuf *msg, |
| 945 | struct strbuf *err) |
| 946 | { |
| 947 | struct ref_transaction *transaction; |
| 948 | struct strbuf sb = STRBUF_INIT; |
| 949 | const char *nl; |
| 950 | int ret = 0; |
| 951 | |
| 952 | if (action) { |
| 953 | strbuf_addstr(&sb, action); |
| 954 | strbuf_addstr(&sb, ": "); |
| 955 | } |
| 956 | |
| 957 | nl = strchr(msg->buf, '\n'); |
| 958 | if (nl) { |
| 959 | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); |
| 960 | } else { |
| 961 | strbuf_addbuf(&sb, msg); |
| 962 | strbuf_addch(&sb, '\n'); |
| 963 | } |
| 964 | |
| 965 | transaction = ref_transaction_begin(err); |
| 966 | if (!transaction || |
| 967 | ref_transaction_update(transaction, "HEAD", new_head, |
| 968 | old_head ? &old_head->object.oid : &null_oid, |
| 969 | 0, sb.buf, err) || |
| 970 | ref_transaction_commit(transaction, err)) { |
| 971 | ret = -1; |
| 972 | } |
| 973 | ref_transaction_free(transaction); |
| 974 | strbuf_release(&sb); |
| 975 | |
| 976 | return ret; |
| 977 | } |
| 978 | |
Phillip Wood | a87a6f3 | 2017-11-17 11:34:48 +0000 | [diff] [blame] | 979 | static int run_rewrite_hook(const struct object_id *oldoid, |
| 980 | const struct object_id *newoid) |
| 981 | { |
| 982 | struct child_process proc = CHILD_PROCESS_INIT; |
| 983 | const char *argv[3]; |
| 984 | int code; |
| 985 | struct strbuf sb = STRBUF_INIT; |
| 986 | |
| 987 | argv[0] = find_hook("post-rewrite"); |
| 988 | if (!argv[0]) |
| 989 | return 0; |
| 990 | |
| 991 | argv[1] = "amend"; |
| 992 | argv[2] = NULL; |
| 993 | |
| 994 | proc.argv = argv; |
| 995 | proc.in = -1; |
| 996 | proc.stdout_to_stderr = 1; |
| 997 | |
| 998 | code = start_command(&proc); |
| 999 | if (code) |
| 1000 | return code; |
| 1001 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); |
| 1002 | sigchain_push(SIGPIPE, SIG_IGN); |
| 1003 | write_in_full(proc.in, sb.buf, sb.len); |
| 1004 | close(proc.in); |
| 1005 | strbuf_release(&sb); |
| 1006 | sigchain_pop(SIGPIPE); |
| 1007 | return finish_command(&proc); |
| 1008 | } |
| 1009 | |
| 1010 | void commit_post_rewrite(const struct commit *old_head, |
| 1011 | const struct object_id *new_head) |
| 1012 | { |
| 1013 | struct notes_rewrite_cfg *cfg; |
| 1014 | |
| 1015 | cfg = init_copy_notes_for_rewrite("amend"); |
| 1016 | if (cfg) { |
| 1017 | /* we are amending, so old_head is not NULL */ |
| 1018 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); |
| 1019 | finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'"); |
| 1020 | } |
| 1021 | run_rewrite_hook(&old_head->object.oid, new_head); |
| 1022 | } |
| 1023 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1024 | static int run_prepare_commit_msg_hook(struct strbuf *msg, const char *commit) |
| 1025 | { |
| 1026 | struct argv_array hook_env = ARGV_ARRAY_INIT; |
| 1027 | int ret; |
| 1028 | const char *name; |
| 1029 | |
| 1030 | name = git_path_commit_editmsg(); |
| 1031 | if (write_message(msg->buf, msg->len, name, 0)) |
| 1032 | return -1; |
| 1033 | |
| 1034 | argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", get_index_file()); |
| 1035 | argv_array_push(&hook_env, "GIT_EDITOR=:"); |
| 1036 | if (commit) |
| 1037 | ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name, |
| 1038 | "commit", commit, NULL); |
| 1039 | else |
| 1040 | ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name, |
| 1041 | "message", NULL); |
| 1042 | if (ret) |
| 1043 | ret = error(_("'prepare-commit-msg' hook failed")); |
| 1044 | argv_array_clear(&hook_env); |
| 1045 | |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1049 | static const char implicit_ident_advice_noconfig[] = |
| 1050 | N_("Your name and email address were configured automatically based\n" |
| 1051 | "on your username and hostname. Please check that they are accurate.\n" |
| 1052 | "You can suppress this message by setting them explicitly. Run the\n" |
| 1053 | "following command and follow the instructions in your editor to edit\n" |
| 1054 | "your configuration file:\n" |
| 1055 | "\n" |
| 1056 | " git config --global --edit\n" |
| 1057 | "\n" |
| 1058 | "After doing this, you may fix the identity used for this commit with:\n" |
| 1059 | "\n" |
| 1060 | " git commit --amend --reset-author\n"); |
| 1061 | |
| 1062 | static const char implicit_ident_advice_config[] = |
| 1063 | N_("Your name and email address were configured automatically based\n" |
| 1064 | "on your username and hostname. Please check that they are accurate.\n" |
| 1065 | "You can suppress this message by setting them explicitly:\n" |
| 1066 | "\n" |
| 1067 | " git config --global user.name \"Your Name\"\n" |
| 1068 | " git config --global user.email you@example.com\n" |
| 1069 | "\n" |
| 1070 | "After doing this, you may fix the identity used for this commit with:\n" |
| 1071 | "\n" |
| 1072 | " git commit --amend --reset-author\n"); |
| 1073 | |
| 1074 | static const char *implicit_ident_advice(void) |
| 1075 | { |
| 1076 | char *user_config = expand_user_path("~/.gitconfig", 0); |
| 1077 | char *xdg_config = xdg_config_home("config"); |
| 1078 | int config_exists = file_exists(user_config) || file_exists(xdg_config); |
| 1079 | |
| 1080 | free(user_config); |
| 1081 | free(xdg_config); |
| 1082 | |
| 1083 | if (config_exists) |
| 1084 | return _(implicit_ident_advice_config); |
| 1085 | else |
| 1086 | return _(implicit_ident_advice_noconfig); |
| 1087 | |
| 1088 | } |
| 1089 | |
| 1090 | void print_commit_summary(const char *prefix, const struct object_id *oid, |
| 1091 | unsigned int flags) |
| 1092 | { |
| 1093 | struct rev_info rev; |
| 1094 | struct commit *commit; |
| 1095 | struct strbuf format = STRBUF_INIT; |
| 1096 | const char *head; |
| 1097 | struct pretty_print_context pctx = {0}; |
| 1098 | struct strbuf author_ident = STRBUF_INIT; |
| 1099 | struct strbuf committer_ident = STRBUF_INIT; |
| 1100 | |
| 1101 | commit = lookup_commit(oid); |
| 1102 | if (!commit) |
| 1103 | die(_("couldn't look up newly created commit")); |
| 1104 | if (parse_commit(commit)) |
| 1105 | die(_("could not parse newly created commit")); |
| 1106 | |
| 1107 | strbuf_addstr(&format, "format:%h] %s"); |
| 1108 | |
| 1109 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); |
| 1110 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); |
| 1111 | if (strbuf_cmp(&author_ident, &committer_ident)) { |
| 1112 | strbuf_addstr(&format, "\n Author: "); |
| 1113 | strbuf_addbuf_percentquote(&format, &author_ident); |
| 1114 | } |
| 1115 | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { |
| 1116 | struct strbuf date = STRBUF_INIT; |
| 1117 | |
| 1118 | format_commit_message(commit, "%ad", &date, &pctx); |
| 1119 | strbuf_addstr(&format, "\n Date: "); |
| 1120 | strbuf_addbuf_percentquote(&format, &date); |
| 1121 | strbuf_release(&date); |
| 1122 | } |
| 1123 | if (!committer_ident_sufficiently_given()) { |
| 1124 | strbuf_addstr(&format, "\n Committer: "); |
| 1125 | strbuf_addbuf_percentquote(&format, &committer_ident); |
| 1126 | if (advice_implicit_identity) { |
| 1127 | strbuf_addch(&format, '\n'); |
| 1128 | strbuf_addstr(&format, implicit_ident_advice()); |
| 1129 | } |
| 1130 | } |
| 1131 | strbuf_release(&author_ident); |
| 1132 | strbuf_release(&committer_ident); |
| 1133 | |
| 1134 | init_revisions(&rev, prefix); |
| 1135 | setup_revisions(0, NULL, &rev, NULL); |
| 1136 | |
| 1137 | rev.diff = 1; |
| 1138 | rev.diffopt.output_format = |
| 1139 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; |
| 1140 | |
| 1141 | rev.verbose_header = 1; |
| 1142 | rev.show_root_diff = 1; |
| 1143 | get_commit_format(format.buf, &rev); |
| 1144 | rev.always_show_header = 0; |
Junio C Hamano | 0f57f73 | 2018-02-13 13:39:15 -0800 | [diff] [blame] | 1145 | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
Phillip Wood | e47c6ca | 2017-11-24 11:07:54 +0000 | [diff] [blame] | 1146 | rev.diffopt.break_opt = 0; |
| 1147 | diff_setup_done(&rev.diffopt); |
| 1148 | |
| 1149 | head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); |
| 1150 | if (!head) |
| 1151 | die_errno(_("unable to resolve HEAD after creating commit")); |
| 1152 | if (!strcmp(head, "HEAD")) |
| 1153 | head = _("detached HEAD"); |
| 1154 | else |
| 1155 | skip_prefix(head, "refs/heads/", &head); |
| 1156 | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? |
| 1157 | _(" (root-commit)") : ""); |
| 1158 | |
| 1159 | if (!log_tree_commit(&rev, commit)) { |
| 1160 | rev.always_show_header = 1; |
| 1161 | rev.use_terminator = 1; |
| 1162 | log_tree_commit(&rev, commit); |
| 1163 | } |
| 1164 | |
| 1165 | strbuf_release(&format); |
| 1166 | } |
| 1167 | |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1168 | static int parse_head(struct commit **head) |
| 1169 | { |
| 1170 | struct commit *current_head; |
| 1171 | struct object_id oid; |
| 1172 | |
| 1173 | if (get_oid("HEAD", &oid)) { |
| 1174 | current_head = NULL; |
| 1175 | } else { |
| 1176 | current_head = lookup_commit_reference(&oid); |
| 1177 | if (!current_head) |
| 1178 | return error(_("could not parse HEAD")); |
| 1179 | if (oidcmp(&oid, ¤t_head->object.oid)) { |
| 1180 | warning(_("HEAD %s is not a commit!"), |
| 1181 | oid_to_hex(&oid)); |
| 1182 | } |
| 1183 | if (parse_commit(current_head)) |
| 1184 | return error(_("could not parse HEAD commit")); |
| 1185 | } |
| 1186 | *head = current_head; |
| 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * Try to commit without forking 'git commit'. In some cases we need |
| 1193 | * to run 'git commit' to display an error message |
| 1194 | * |
| 1195 | * Returns: |
| 1196 | * -1 - error unable to commit |
| 1197 | * 0 - success |
| 1198 | * 1 - run 'git commit' |
| 1199 | */ |
| 1200 | static int try_to_commit(struct strbuf *msg, const char *author, |
| 1201 | struct replay_opts *opts, unsigned int flags, |
| 1202 | struct object_id *oid) |
| 1203 | { |
| 1204 | struct object_id tree; |
| 1205 | struct commit *current_head; |
| 1206 | struct commit_list *parents = NULL; |
| 1207 | struct commit_extra_header *extra = NULL; |
| 1208 | struct strbuf err = STRBUF_INIT; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1209 | struct strbuf commit_msg = STRBUF_INIT; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1210 | char *amend_author = NULL; |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1211 | const char *hook_commit = NULL; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1212 | enum commit_msg_cleanup_mode cleanup; |
| 1213 | int res = 0; |
| 1214 | |
| 1215 | if (parse_head(¤t_head)) |
| 1216 | return -1; |
| 1217 | |
| 1218 | if (flags & AMEND_MSG) { |
| 1219 | const char *exclude_gpgsig[] = { "gpgsig", NULL }; |
| 1220 | const char *out_enc = get_commit_output_encoding(); |
| 1221 | const char *message = logmsg_reencode(current_head, NULL, |
| 1222 | out_enc); |
| 1223 | |
| 1224 | if (!msg) { |
| 1225 | const char *orig_message = NULL; |
| 1226 | |
| 1227 | find_commit_subject(message, &orig_message); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1228 | msg = &commit_msg; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1229 | strbuf_addstr(msg, orig_message); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1230 | hook_commit = "HEAD"; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1231 | } |
| 1232 | author = amend_author = get_author(message); |
| 1233 | unuse_commit_buffer(current_head, message); |
| 1234 | if (!author) { |
| 1235 | res = error(_("unable to parse commit author")); |
| 1236 | goto out; |
| 1237 | } |
| 1238 | parents = copy_commit_list(current_head->parents); |
| 1239 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); |
| 1240 | } else if (current_head) { |
| 1241 | commit_list_insert(current_head, &parents); |
| 1242 | } |
| 1243 | |
brian m. carlson | fc5cb99 | 2018-03-12 02:27:23 +0000 | [diff] [blame] | 1244 | if (write_cache_as_tree(&tree, 0, NULL)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1245 | res = error(_("git write-tree failed to write a tree")); |
| 1246 | goto out; |
| 1247 | } |
| 1248 | |
| 1249 | if (!(flags & ALLOW_EMPTY) && !oidcmp(current_head ? |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1250 | get_commit_tree_oid(current_head) : |
brian m. carlson | 57911a3 | 2018-05-02 00:26:05 +0000 | [diff] [blame] | 1251 | the_hash_algo->empty_tree, &tree)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1252 | res = 1; /* run 'git commit' to display error message */ |
| 1253 | goto out; |
| 1254 | } |
| 1255 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1256 | if (find_hook("prepare-commit-msg")) { |
| 1257 | res = run_prepare_commit_msg_hook(msg, hook_commit); |
| 1258 | if (res) |
| 1259 | goto out; |
| 1260 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), |
| 1261 | 2048) < 0) { |
| 1262 | res = error_errno(_("unable to read commit message " |
| 1263 | "from '%s'"), |
| 1264 | git_path_commit_editmsg()); |
| 1265 | goto out; |
| 1266 | } |
| 1267 | msg = &commit_msg; |
| 1268 | } |
| 1269 | |
| 1270 | cleanup = (flags & CLEANUP_MSG) ? COMMIT_MSG_CLEANUP_ALL : |
| 1271 | opts->default_msg_cleanup; |
| 1272 | |
| 1273 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) |
| 1274 | strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); |
| 1275 | if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) { |
| 1276 | res = 1; /* run 'git commit' to display error message */ |
| 1277 | goto out; |
| 1278 | } |
| 1279 | |
Johannes Sixt | 12f7bab | 2018-04-18 20:15:04 +0200 | [diff] [blame] | 1280 | reset_ident_date(); |
| 1281 | |
Junio C Hamano | 8be8342 | 2018-02-15 14:55:43 -0800 | [diff] [blame] | 1282 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, |
| 1283 | oid, author, opts->gpg_sign, extra)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1284 | res = error(_("failed to write commit object")); |
| 1285 | goto out; |
| 1286 | } |
| 1287 | |
| 1288 | if (update_head_with_reflog(current_head, oid, |
| 1289 | getenv("GIT_REFLOG_ACTION"), msg, &err)) { |
| 1290 | res = error("%s", err.buf); |
| 1291 | goto out; |
| 1292 | } |
| 1293 | |
| 1294 | if (flags & AMEND_MSG) |
| 1295 | commit_post_rewrite(current_head, oid); |
| 1296 | |
| 1297 | out: |
| 1298 | free_commit_extra_headers(extra); |
| 1299 | strbuf_release(&err); |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 1300 | strbuf_release(&commit_msg); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1301 | free(amend_author); |
| 1302 | |
| 1303 | return res; |
| 1304 | } |
| 1305 | |
| 1306 | static int do_commit(const char *msg_file, const char *author, |
| 1307 | struct replay_opts *opts, unsigned int flags) |
| 1308 | { |
| 1309 | int res = 1; |
| 1310 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1311 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG) && |
| 1312 | !(flags & CREATE_ROOT_COMMIT)) { |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1313 | struct object_id oid; |
| 1314 | struct strbuf sb = STRBUF_INIT; |
| 1315 | |
| 1316 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) |
| 1317 | return error_errno(_("unable to read commit message " |
| 1318 | "from '%s'"), |
| 1319 | msg_file); |
| 1320 | |
| 1321 | res = try_to_commit(msg_file ? &sb : NULL, author, opts, flags, |
| 1322 | &oid); |
| 1323 | strbuf_release(&sb); |
| 1324 | if (!res) { |
| 1325 | unlink(git_path_cherry_pick_head()); |
| 1326 | unlink(git_path_merge_msg()); |
| 1327 | if (!is_rebase_i(opts)) |
| 1328 | print_commit_summary(NULL, &oid, |
| 1329 | SUMMARY_SHOW_AUTHOR_DATE); |
| 1330 | return res; |
| 1331 | } |
| 1332 | } |
| 1333 | if (res == 1) |
| 1334 | return run_git_commit(msg_file, opts, flags); |
| 1335 | |
| 1336 | return res; |
| 1337 | } |
| 1338 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1339 | static int is_original_commit_empty(struct commit *commit) |
| 1340 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1341 | const struct object_id *ptree_oid; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1342 | |
| 1343 | if (parse_commit(commit)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 1344 | return error(_("could not parse commit %s"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1345 | oid_to_hex(&commit->object.oid)); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1346 | if (commit->parents) { |
| 1347 | struct commit *parent = commit->parents->item; |
| 1348 | if (parse_commit(parent)) |
Johannes Schindelin | aee42e1 | 2017-12-23 00:55:43 +0100 | [diff] [blame] | 1349 | return error(_("could not parse parent commit %s"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1350 | oid_to_hex(&parent->object.oid)); |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1351 | ptree_oid = get_commit_tree_oid(parent); |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1352 | } else { |
brian m. carlson | eb0ccfd | 2017-11-12 21:28:54 +0000 | [diff] [blame] | 1353 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 1354 | } |
| 1355 | |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 1356 | return !oidcmp(ptree_oid, get_commit_tree_oid(commit)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1357 | } |
| 1358 | |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 1359 | /* |
| 1360 | * Do we run "git commit" with "--allow-empty"? |
| 1361 | */ |
| 1362 | static int allow_empty(struct replay_opts *opts, struct commit *commit) |
| 1363 | { |
| 1364 | int index_unchanged, empty_commit; |
| 1365 | |
| 1366 | /* |
| 1367 | * Three cases: |
| 1368 | * |
| 1369 | * (1) we do not allow empty at all and error out. |
| 1370 | * |
| 1371 | * (2) we allow ones that were initially empty, but |
| 1372 | * forbid the ones that become empty; |
| 1373 | * |
| 1374 | * (3) we allow both. |
| 1375 | */ |
| 1376 | if (!opts->allow_empty) |
| 1377 | return 0; /* let "git commit" barf as necessary */ |
| 1378 | |
| 1379 | index_unchanged = is_index_unchanged(); |
| 1380 | if (index_unchanged < 0) |
| 1381 | return index_unchanged; |
| 1382 | if (!index_unchanged) |
| 1383 | return 0; /* we do not have to say --allow-empty */ |
| 1384 | |
| 1385 | if (opts->keep_redundant_commits) |
| 1386 | return 1; |
| 1387 | |
| 1388 | empty_commit = is_original_commit_empty(commit); |
| 1389 | if (empty_commit < 0) |
| 1390 | return empty_commit; |
| 1391 | if (!empty_commit) |
| 1392 | return 0; |
| 1393 | else |
| 1394 | return 1; |
| 1395 | } |
| 1396 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1397 | /* |
| 1398 | * Note that ordering matters in this enum. Not only must it match the mapping |
| 1399 | * below, it is also divided into several sections that matter. When adding |
| 1400 | * new commands, make sure you add it in the right section. |
| 1401 | */ |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1402 | enum todo_command { |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1403 | /* commands that handle commits */ |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1404 | TODO_PICK = 0, |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1405 | TODO_REVERT, |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 1406 | TODO_EDIT, |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1407 | TODO_REWORD, |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1408 | TODO_FIXUP, |
| 1409 | TODO_SQUASH, |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 1410 | /* commands that do something else than handling a single commit */ |
| 1411 | TODO_EXEC, |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 1412 | TODO_LABEL, |
| 1413 | TODO_RESET, |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1414 | TODO_MERGE, |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1415 | /* commands that do nothing but are counted for reporting progress */ |
Johannes Schindelin | b3fdd58 | 2017-01-02 16:34:34 +0100 | [diff] [blame] | 1416 | TODO_NOOP, |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1417 | TODO_DROP, |
| 1418 | /* comments (not counted for reporting progress) */ |
| 1419 | TODO_COMMENT |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1420 | }; |
| 1421 | |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1422 | static struct { |
| 1423 | char c; |
| 1424 | const char *str; |
| 1425 | } todo_command_info[] = { |
| 1426 | { 'p', "pick" }, |
| 1427 | { 0, "revert" }, |
| 1428 | { 'e', "edit" }, |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1429 | { 'r', "reword" }, |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1430 | { 'f', "fixup" }, |
| 1431 | { 's', "squash" }, |
| 1432 | { 'x', "exec" }, |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 1433 | { 'l', "label" }, |
| 1434 | { 't', "reset" }, |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1435 | { 'm', "merge" }, |
Johannes Schindelin | b3fdd58 | 2017-01-02 16:34:34 +0100 | [diff] [blame] | 1436 | { 0, "noop" }, |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1437 | { 'd', "drop" }, |
| 1438 | { 0, NULL } |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1439 | }; |
| 1440 | |
| 1441 | static const char *command_to_string(const enum todo_command command) |
| 1442 | { |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1443 | if (command < TODO_COMMENT) |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1444 | return todo_command_info[command].str; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1445 | die("Unknown command: %d", command); |
| 1446 | } |
| 1447 | |
Junio C Hamano | ee5462d | 2017-12-27 11:12:45 -0800 | [diff] [blame] | 1448 | static char command_to_char(const enum todo_command command) |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 1449 | { |
| 1450 | if (command < TODO_COMMENT && todo_command_info[command].c) |
| 1451 | return todo_command_info[command].c; |
| 1452 | return comment_line_char; |
| 1453 | } |
| 1454 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1455 | static int is_noop(const enum todo_command command) |
| 1456 | { |
Johannes Schindelin | b3fdd58 | 2017-01-02 16:34:34 +0100 | [diff] [blame] | 1457 | return TODO_NOOP <= command; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1458 | } |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1459 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1460 | static int is_fixup(enum todo_command command) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1461 | { |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1462 | return command == TODO_FIXUP || command == TODO_SQUASH; |
| 1463 | } |
| 1464 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1465 | /* Does this command create a (non-merge) commit? */ |
| 1466 | static int is_pick_or_similar(enum todo_command command) |
| 1467 | { |
| 1468 | switch (command) { |
| 1469 | case TODO_PICK: |
| 1470 | case TODO_REVERT: |
| 1471 | case TODO_EDIT: |
| 1472 | case TODO_REWORD: |
| 1473 | case TODO_FIXUP: |
| 1474 | case TODO_SQUASH: |
| 1475 | return 1; |
| 1476 | default: |
| 1477 | return 0; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1481 | static int update_squash_messages(enum todo_command command, |
| 1482 | struct commit *commit, struct replay_opts *opts) |
| 1483 | { |
| 1484 | struct strbuf buf = STRBUF_INIT; |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1485 | int res; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1486 | const char *message, *body; |
| 1487 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1488 | if (opts->current_fixup_count > 0) { |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1489 | struct strbuf header = STRBUF_INIT; |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1490 | char *eol; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1491 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1492 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1493 | return error(_("could not read '%s'"), |
| 1494 | rebase_path_squash_msg()); |
| 1495 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1496 | eol = buf.buf[0] != comment_line_char ? |
| 1497 | buf.buf : strchrnul(buf.buf, '\n'); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1498 | |
| 1499 | strbuf_addf(&header, "%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1500 | strbuf_addf(&header, _("This is a combination of %d commits."), |
| 1501 | opts->current_fixup_count + 2); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1502 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); |
| 1503 | strbuf_release(&header); |
| 1504 | } else { |
brian m. carlson | 33d66df | 2017-05-06 22:10:07 +0000 | [diff] [blame] | 1505 | struct object_id head; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1506 | struct commit *head_commit; |
| 1507 | const char *head_message, *body; |
| 1508 | |
brian m. carlson | 33d66df | 2017-05-06 22:10:07 +0000 | [diff] [blame] | 1509 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1510 | return error(_("need a HEAD to fixup")); |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 1511 | if (!(head_commit = lookup_commit_reference(&head))) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1512 | return error(_("could not read HEAD")); |
| 1513 | if (!(head_message = get_commit_buffer(head_commit, NULL))) |
| 1514 | return error(_("could not read HEAD's commit message")); |
| 1515 | |
| 1516 | find_commit_subject(head_message, &body); |
| 1517 | if (write_message(body, strlen(body), |
| 1518 | rebase_path_fixup_msg(), 0)) { |
| 1519 | unuse_commit_buffer(head_commit, head_message); |
| 1520 | return error(_("cannot write '%s'"), |
| 1521 | rebase_path_fixup_msg()); |
| 1522 | } |
| 1523 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1524 | strbuf_addf(&buf, "%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1525 | strbuf_addf(&buf, _("This is a combination of %d commits."), 2); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1526 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
| 1527 | strbuf_addstr(&buf, _("This is the 1st commit message:")); |
| 1528 | strbuf_addstr(&buf, "\n\n"); |
| 1529 | strbuf_addstr(&buf, body); |
| 1530 | |
| 1531 | unuse_commit_buffer(head_commit, head_message); |
| 1532 | } |
| 1533 | |
| 1534 | if (!(message = get_commit_buffer(commit, NULL))) |
| 1535 | return error(_("could not read commit message of %s"), |
| 1536 | oid_to_hex(&commit->object.oid)); |
| 1537 | find_commit_subject(message, &body); |
| 1538 | |
| 1539 | if (command == TODO_SQUASH) { |
| 1540 | unlink(rebase_path_fixup_msg()); |
| 1541 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1542 | strbuf_addf(&buf, _("This is the commit message #%d:"), |
| 1543 | ++opts->current_fixup_count); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1544 | strbuf_addstr(&buf, "\n\n"); |
| 1545 | strbuf_addstr(&buf, body); |
| 1546 | } else if (command == TODO_FIXUP) { |
| 1547 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
| 1548 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1549 | ++opts->current_fixup_count); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1550 | strbuf_addstr(&buf, "\n\n"); |
| 1551 | strbuf_add_commented_lines(&buf, body, strlen(body)); |
| 1552 | } else |
| 1553 | return error(_("unknown command: %d"), command); |
| 1554 | unuse_commit_buffer(commit, message); |
| 1555 | |
| 1556 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); |
| 1557 | strbuf_release(&buf); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1558 | |
| 1559 | if (!res) { |
| 1560 | strbuf_addf(&opts->current_fixups, "%s%s %s", |
| 1561 | opts->current_fixups.len ? "\n" : "", |
| 1562 | command_to_string(command), |
| 1563 | oid_to_hex(&commit->object.oid)); |
| 1564 | res = write_message(opts->current_fixups.buf, |
| 1565 | opts->current_fixups.len, |
| 1566 | rebase_path_current_fixups(), 0); |
| 1567 | } |
| 1568 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1569 | return res; |
| 1570 | } |
| 1571 | |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1572 | static void flush_rewritten_pending(void) { |
| 1573 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1574 | struct object_id newoid; |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1575 | FILE *out; |
| 1576 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1577 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
| 1578 | !get_oid("HEAD", &newoid) && |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 1579 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1580 | char *bol = buf.buf, *eol; |
| 1581 | |
| 1582 | while (*bol) { |
| 1583 | eol = strchrnul(bol, '\n'); |
| 1584 | fprintf(out, "%.*s %s\n", (int)(eol - bol), |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 1585 | bol, oid_to_hex(&newoid)); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1586 | if (!*eol) |
| 1587 | break; |
| 1588 | bol = eol + 1; |
| 1589 | } |
| 1590 | fclose(out); |
| 1591 | unlink(rebase_path_rewritten_pending()); |
| 1592 | } |
| 1593 | strbuf_release(&buf); |
| 1594 | } |
| 1595 | |
| 1596 | static void record_in_rewritten(struct object_id *oid, |
| 1597 | enum todo_command next_command) { |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 1598 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 1599 | |
| 1600 | if (!out) |
| 1601 | return; |
| 1602 | |
| 1603 | fprintf(out, "%s\n", oid_to_hex(oid)); |
| 1604 | fclose(out); |
| 1605 | |
| 1606 | if (!is_fixup(next_command)) |
| 1607 | flush_rewritten_pending(); |
| 1608 | } |
| 1609 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1610 | static int do_pick_commit(enum todo_command command, struct commit *commit, |
| 1611 | struct replay_opts *opts, int final_fixup) |
| 1612 | { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1613 | unsigned int flags = opts->edit ? EDIT_MSG : 0; |
| 1614 | const char *msg_file = opts->edit ? NULL : git_path_merge_msg(); |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1615 | struct object_id head; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1616 | struct commit *base, *next, *parent; |
| 1617 | const char *base_label, *next_label; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1618 | char *author = NULL; |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 1619 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1620 | struct strbuf msgbuf = STRBUF_INIT; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1621 | int res, unborn = 0, allow; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1622 | |
| 1623 | if (opts->no_commit) { |
| 1624 | /* |
| 1625 | * We do not intend to commit immediately. We just want to |
| 1626 | * merge the differences in, so let's compute the tree |
| 1627 | * that represents the "current" state for merge-recursive |
| 1628 | * to work on. |
| 1629 | */ |
brian m. carlson | fc5cb99 | 2018-03-12 02:27:23 +0000 | [diff] [blame] | 1630 | if (write_cache_as_tree(&head, 0, NULL)) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1631 | return error(_("your index file is unmerged.")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1632 | } else { |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1633 | unborn = get_oid("HEAD", &head); |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 1634 | /* Do we want to generate a root commit? */ |
| 1635 | if (is_pick_or_similar(command) && opts->have_squash_onto && |
| 1636 | !oidcmp(&head, &opts->squash_onto)) { |
| 1637 | if (is_fixup(command)) |
| 1638 | return error(_("cannot fixup root commit")); |
| 1639 | flags |= CREATE_ROOT_COMMIT; |
| 1640 | unborn = 1; |
| 1641 | } else if (unborn) |
brian m. carlson | eb0ccfd | 2017-11-12 21:28:54 +0000 | [diff] [blame] | 1642 | oidcpy(&head, the_hash_algo->empty_tree); |
brian m. carlson | 7a915b4 | 2018-05-02 00:25:58 +0000 | [diff] [blame] | 1643 | if (index_differs_from(unborn ? empty_tree_oid_hex() : "HEAD", |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 1644 | NULL, 0)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1645 | return error_dirty_index(opts); |
| 1646 | } |
| 1647 | discard_cache(); |
| 1648 | |
Johannes Schindelin | 637666c | 2017-01-02 16:26:08 +0100 | [diff] [blame] | 1649 | if (!commit->parents) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1650 | parent = NULL; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1651 | else if (commit->parents->next) { |
| 1652 | /* Reverting or cherry-picking a merge commit */ |
| 1653 | int cnt; |
| 1654 | struct commit_list *p; |
| 1655 | |
| 1656 | if (!opts->mainline) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1657 | 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] | 1658 | oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1659 | |
| 1660 | for (cnt = 1, p = commit->parents; |
| 1661 | cnt != opts->mainline && p; |
| 1662 | cnt++) |
| 1663 | p = p->next; |
| 1664 | if (cnt != opts->mainline || !p) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1665 | return error(_("commit %s does not have parent %d"), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1666 | oid_to_hex(&commit->object.oid), opts->mainline); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1667 | parent = p->item; |
| 1668 | } else if (0 < opts->mainline) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 1669 | return error(_("mainline was specified but commit %s is not a merge."), |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1670 | oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1671 | else |
| 1672 | parent = commit->parents->item; |
| 1673 | |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1674 | if (get_message(commit, &msg) != 0) |
| 1675 | return error(_("cannot get commit message for %s"), |
| 1676 | oid_to_hex(&commit->object.oid)); |
| 1677 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1678 | if (opts->allow_ff && !is_fixup(command) && |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1679 | ((parent && !oidcmp(&parent->object.oid, &head)) || |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1680 | (!parent && unborn))) { |
| 1681 | if (is_rebase_i(opts)) |
| 1682 | write_author_script(msg.message); |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1683 | res = fast_forward_to(&commit->object.oid, &head, unborn, |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1684 | opts); |
| 1685 | if (res || command != TODO_REWORD) |
| 1686 | goto leave; |
Johannes Schindelin | 5f8f927 | 2017-12-23 00:55:53 +0100 | [diff] [blame] | 1687 | flags |= EDIT_MSG | AMEND_MSG | VERIFY_MSG; |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1688 | msg_file = NULL; |
| 1689 | goto fast_forward_edit; |
| 1690 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1691 | if (parent && parse_commit(parent) < 0) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1692 | /* TRANSLATORS: The first %s will be a "todo" command like |
| 1693 | "revert" or "pick", the second %s a SHA1. */ |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1694 | return error(_("%s: cannot parse parent commit %s"), |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1695 | command_to_string(command), |
| 1696 | oid_to_hex(&parent->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1697 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1698 | /* |
| 1699 | * "commit" is an existing commit. We would want to apply |
| 1700 | * the difference it introduces since its first parent "prev" |
| 1701 | * on top of the current HEAD if we are cherry-pick. Or the |
| 1702 | * reverse of it if we are revert. |
| 1703 | */ |
| 1704 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1705 | if (command == TODO_REVERT) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1706 | base = commit; |
| 1707 | base_label = msg.label; |
| 1708 | next = parent; |
| 1709 | next_label = msg.parent_label; |
| 1710 | strbuf_addstr(&msgbuf, "Revert \""); |
| 1711 | strbuf_addstr(&msgbuf, msg.subject); |
| 1712 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1713 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1714 | |
| 1715 | if (commit->parents && commit->parents->next) { |
| 1716 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1717 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1718 | } |
| 1719 | strbuf_addstr(&msgbuf, ".\n"); |
| 1720 | } else { |
| 1721 | const char *p; |
| 1722 | |
| 1723 | base = parent; |
| 1724 | base_label = msg.parent_label; |
| 1725 | next = commit; |
| 1726 | next_label = msg.label; |
| 1727 | |
Johannes Schindelin | 23aa514 | 2017-01-02 16:26:20 +0100 | [diff] [blame] | 1728 | /* Append the commit log message to msgbuf. */ |
| 1729 | if (find_commit_subject(msg.message, &p)) |
| 1730 | strbuf_addstr(&msgbuf, p); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1731 | |
| 1732 | if (opts->record_origin) { |
Jonathan Tan | 44dc738 | 2017-04-26 13:50:03 -0700 | [diff] [blame] | 1733 | strbuf_complete_line(&msgbuf); |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1734 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 1735 | strbuf_addch(&msgbuf, '\n'); |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 1736 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1737 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1738 | strbuf_addstr(&msgbuf, ")\n"); |
| 1739 | } |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1740 | if (!is_fixup(command)) |
| 1741 | author = get_author(msg.message); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1742 | } |
| 1743 | |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1744 | if (command == TODO_REWORD) |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 1745 | flags |= EDIT_MSG | VERIFY_MSG; |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 1746 | else if (is_fixup(command)) { |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1747 | if (update_squash_messages(command, commit, opts)) |
| 1748 | return -1; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1749 | flags |= AMEND_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1750 | if (!final_fixup) |
| 1751 | msg_file = rebase_path_squash_msg(); |
| 1752 | else if (file_exists(rebase_path_fixup_msg())) { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1753 | flags |= CLEANUP_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1754 | msg_file = rebase_path_fixup_msg(); |
| 1755 | } else { |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 1756 | const char *dest = git_path_squash_msg(); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1757 | unlink(dest); |
| 1758 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) |
| 1759 | return error(_("could not rename '%s' to '%s'"), |
| 1760 | rebase_path_squash_msg(), dest); |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 1761 | unlink(git_path_merge_msg()); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1762 | msg_file = dest; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1763 | flags |= EDIT_MSG; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 1767 | if (opts->signoff && !is_fixup(command)) |
Phillip Wood | b34eeea | 2017-11-24 11:07:55 +0000 | [diff] [blame] | 1768 | append_signoff(&msgbuf, 0, 0); |
| 1769 | |
Johannes Schindelin | 0473f28 | 2017-01-02 16:27:18 +0100 | [diff] [blame] | 1770 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
| 1771 | res = -1; |
| 1772 | else if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1773 | res = do_recursive_merge(base, next, base_label, next_label, |
brian m. carlson | 48be4c6 | 2017-05-06 22:10:36 +0000 | [diff] [blame] | 1774 | &head, &msgbuf, opts); |
Johannes Schindelin | f241ff0 | 2016-07-26 18:06:02 +0200 | [diff] [blame] | 1775 | if (res < 0) |
| 1776 | return res; |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 1777 | res |= write_message(msgbuf.buf, msgbuf.len, |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 1778 | git_path_merge_msg(), 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1779 | } else { |
| 1780 | struct commit_list *common = NULL; |
| 1781 | struct commit_list *remotes = NULL; |
| 1782 | |
Johannes Schindelin | 7587149 | 2016-10-21 14:26:05 +0200 | [diff] [blame] | 1783 | res = write_message(msgbuf.buf, msgbuf.len, |
Johannes Schindelin | f56fffe | 2016-10-21 14:26:09 +0200 | [diff] [blame] | 1784 | git_path_merge_msg(), 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1785 | |
| 1786 | commit_list_insert(base, &common); |
| 1787 | commit_list_insert(next, &remotes); |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 1788 | res |= try_merge_command(opts->strategy, |
| 1789 | opts->xopts_nr, (const char **)opts->xopts, |
brian m. carlson | ace976b | 2017-05-06 22:10:32 +0000 | [diff] [blame] | 1790 | common, oid_to_hex(&head), remotes); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1791 | free_commit_list(common); |
| 1792 | free_commit_list(remotes); |
| 1793 | } |
Johannes Schindelin | 452202c | 2016-10-21 14:25:41 +0200 | [diff] [blame] | 1794 | strbuf_release(&msgbuf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1795 | |
| 1796 | /* |
| 1797 | * If the merge was clean or if it failed due to conflict, we write |
| 1798 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
| 1799 | * However, if the merge did not even start, then we don't want to |
| 1800 | * write it at all. |
| 1801 | */ |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1802 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 1803 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 1804 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
Johannes Schindelin | dbfad03 | 2016-08-26 15:47:14 +0200 | [diff] [blame] | 1805 | res = -1; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1806 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 1807 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 1808 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
Johannes Schindelin | dbfad03 | 2016-08-26 15:47:14 +0200 | [diff] [blame] | 1809 | res = -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1810 | |
| 1811 | if (res) { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1812 | error(command == TODO_REVERT |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1813 | ? _("could not revert %s... %s") |
| 1814 | : _("could not apply %s... %s"), |
Johannes Schindelin | 3975596 | 2016-10-21 14:24:37 +0200 | [diff] [blame] | 1815 | short_commit_name(commit), msg.subject); |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 1816 | print_advice(res == 1, opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1817 | rerere(opts->allow_rerere_auto); |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 1818 | goto leave; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1819 | } |
| 1820 | |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 1821 | allow = allow_empty(opts, commit); |
Felipe Contreras | 706728a | 2013-06-06 03:58:57 -0500 | [diff] [blame] | 1822 | if (allow < 0) { |
| 1823 | res = allow; |
| 1824 | goto leave; |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 1825 | } else if (allow) |
| 1826 | flags |= ALLOW_EMPTY; |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1827 | if (!opts->no_commit) { |
Johannes Schindelin | bcbb68b | 2017-01-02 16:28:05 +0100 | [diff] [blame] | 1828 | fast_forward_edit: |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1829 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
| 1830 | res = do_commit(msg_file, author, opts, flags); |
| 1831 | else |
| 1832 | res = error(_("unable to parse commit author")); |
| 1833 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1834 | |
| 1835 | if (!res && final_fixup) { |
| 1836 | unlink(rebase_path_fixup_msg()); |
| 1837 | unlink(rebase_path_squash_msg()); |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 1838 | unlink(rebase_path_current_fixups()); |
| 1839 | strbuf_reset(&opts->current_fixups); |
| 1840 | opts->current_fixup_count = 0; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 1841 | } |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 1842 | |
| 1843 | leave: |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 1844 | free_message(commit, &msg); |
Phillip Wood | 356ee46 | 2017-11-24 11:07:57 +0000 | [diff] [blame] | 1845 | free(author); |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 1846 | update_abort_safety_file(); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1847 | |
| 1848 | return res; |
| 1849 | } |
| 1850 | |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 1851 | static int prepare_revs(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1852 | { |
Martin von Zweigbergk | a73e22e | 2012-08-28 23:15:56 -0700 | [diff] [blame] | 1853 | /* |
| 1854 | * picking (but not reverting) ranges (but not individual revisions) |
| 1855 | * should be done in reverse |
| 1856 | */ |
| 1857 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1858 | opts->revs->reverse ^= 1; |
| 1859 | |
| 1860 | if (prepare_revision_walk(opts->revs)) |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 1861 | return error(_("revision walk setup failed")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1862 | |
| 1863 | if (!opts->revs->commits) |
Johannes Schindelin | c3e8618 | 2016-09-09 16:37:18 +0200 | [diff] [blame] | 1864 | return error(_("empty commit set passed")); |
| 1865 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1866 | } |
| 1867 | |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 1868 | static int read_and_refresh_cache(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1869 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 1870 | struct lock_file index_lock = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1871 | int index_fd = hold_locked_index(&index_lock, 0); |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 1872 | if (read_index_preload(&the_index, NULL) < 0) { |
| 1873 | rollback_lock_file(&index_lock); |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 1874 | return error(_("git %s: failed to read the index"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 1875 | _(action_name(opts))); |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 1876 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1877 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 1878 | if (index_fd >= 0) { |
| 1879 | if (write_locked_index(&the_index, &index_lock, |
| 1880 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 1881 | return error(_("git %s: failed to refresh the index"), |
Johannes Schindelin | c28cbc5 | 2016-10-21 14:26:17 +0200 | [diff] [blame] | 1882 | _(action_name(opts))); |
Johannes Schindelin | 49fb937 | 2016-09-09 16:38:20 +0200 | [diff] [blame] | 1883 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1884 | } |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 1885 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1886 | } |
| 1887 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1888 | enum todo_item_flags { |
| 1889 | TODO_EDIT_MERGE_MSG = 1 |
| 1890 | }; |
| 1891 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1892 | struct todo_item { |
| 1893 | enum todo_command command; |
| 1894 | struct commit *commit; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1895 | unsigned int flags; |
Johannes Schindelin | c22f7df | 2016-10-21 14:25:00 +0200 | [diff] [blame] | 1896 | const char *arg; |
| 1897 | int arg_len; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1898 | size_t offset_in_buf; |
| 1899 | }; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1900 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1901 | struct todo_list { |
| 1902 | struct strbuf buf; |
| 1903 | struct todo_item *items; |
| 1904 | int nr, alloc, current; |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 1905 | int done_nr, total_nr; |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 1906 | struct stat_data stat; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1907 | }; |
| 1908 | |
| 1909 | #define TODO_LIST_INIT { STRBUF_INIT } |
| 1910 | |
| 1911 | static void todo_list_release(struct todo_list *todo_list) |
| 1912 | { |
| 1913 | strbuf_release(&todo_list->buf); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 1914 | FREE_AND_NULL(todo_list->items); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1915 | todo_list->nr = todo_list->alloc = 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1916 | } |
| 1917 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1918 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
| 1919 | { |
| 1920 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); |
| 1921 | return todo_list->items + todo_list->nr++; |
| 1922 | } |
| 1923 | |
| 1924 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1925 | { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 1926 | struct object_id commit_oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1927 | char *end_of_object_name; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1928 | int i, saved, status, padding; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1929 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1930 | item->flags = 0; |
| 1931 | |
Johannes Schindelin | 8f8550b | 2016-10-21 14:25:36 +0200 | [diff] [blame] | 1932 | /* left-trim */ |
| 1933 | bol += strspn(bol, " \t"); |
| 1934 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1935 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1936 | item->command = TODO_COMMENT; |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1937 | item->commit = NULL; |
| 1938 | item->arg = bol; |
| 1939 | item->arg_len = eol - bol; |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1943 | for (i = 0; i < TODO_COMMENT; i++) |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 1944 | if (skip_prefix(bol, todo_command_info[i].str, &bol)) { |
| 1945 | item->command = i; |
| 1946 | break; |
| 1947 | } else if (bol[1] == ' ' && *bol == todo_command_info[i].c) { |
| 1948 | bol++; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1949 | item->command = i; |
| 1950 | break; |
| 1951 | } |
Johannes Schindelin | ac19147 | 2017-01-02 16:34:39 +0100 | [diff] [blame] | 1952 | if (i >= TODO_COMMENT) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1953 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1954 | |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 1955 | /* Eat up extra spaces/ tabs before object name */ |
| 1956 | padding = strspn(bol, " \t"); |
| 1957 | bol += padding; |
| 1958 | |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1959 | if (item->command == TODO_NOOP) { |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 1960 | if (bol != eol) |
| 1961 | return error(_("%s does not accept arguments: '%s'"), |
| 1962 | command_to_string(item->command), bol); |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 1963 | item->commit = NULL; |
| 1964 | item->arg = bol; |
| 1965 | item->arg_len = eol - bol; |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1969 | if (!padding) |
Johannes Schindelin | 66afa24 | 2017-12-23 00:55:57 +0100 | [diff] [blame] | 1970 | return error(_("missing arguments for %s"), |
| 1971 | command_to_string(item->command)); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1972 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 1973 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
| 1974 | item->command == TODO_RESET) { |
Liam Beguin | 7dcbb3c | 2017-12-03 17:17:15 -0500 | [diff] [blame] | 1975 | item->commit = NULL; |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 1976 | item->arg = bol; |
| 1977 | item->arg_len = (int)(eol - bol); |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 1981 | if (item->command == TODO_MERGE) { |
| 1982 | if (skip_prefix(bol, "-C", &bol)) |
| 1983 | bol += strspn(bol, " \t"); |
| 1984 | else if (skip_prefix(bol, "-c", &bol)) { |
| 1985 | bol += strspn(bol, " \t"); |
| 1986 | item->flags |= TODO_EDIT_MERGE_MSG; |
| 1987 | } else { |
| 1988 | item->flags |= TODO_EDIT_MERGE_MSG; |
| 1989 | item->commit = NULL; |
| 1990 | item->arg = bol; |
| 1991 | item->arg_len = (int)(eol - bol); |
| 1992 | return 0; |
| 1993 | } |
| 1994 | } |
| 1995 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 1996 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 1997 | saved = *end_of_object_name; |
| 1998 | *end_of_object_name = '\0'; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 1999 | status = get_oid(bol, &commit_oid); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2000 | *end_of_object_name = saved; |
| 2001 | |
Johannes Schindelin | c22f7df | 2016-10-21 14:25:00 +0200 | [diff] [blame] | 2002 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
| 2003 | item->arg_len = (int)(eol - item->arg); |
| 2004 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2005 | if (status < 0) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2006 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2007 | |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 2008 | item->commit = lookup_commit_reference(&commit_oid); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2009 | return !item->commit; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2010 | } |
| 2011 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2012 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2013 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2014 | struct todo_item *item; |
| 2015 | char *p = buf, *next_p; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2016 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2017 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2018 | for (i = 1; *p; i++, p = next_p) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2019 | char *eol = strchrnul(p, '\n'); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2020 | |
| 2021 | next_p = *eol ? eol + 1 /* skip LF */ : eol; |
| 2022 | |
Johannes Schindelin | 6307041 | 2016-10-21 14:24:46 +0200 | [diff] [blame] | 2023 | if (p != eol && eol[-1] == '\r') |
| 2024 | eol--; /* strip Carriage Return */ |
| 2025 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2026 | item = append_new_todo(todo_list); |
| 2027 | item->offset_in_buf = p - todo_list->buf.buf; |
| 2028 | if (parse_insn_line(item, p, eol)) { |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2029 | res = error(_("invalid line %d: %.*s"), |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2030 | i, (int)(eol - p), p); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2031 | item->command = TODO_NOOP; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2032 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2033 | |
| 2034 | if (fixup_okay) |
| 2035 | ; /* do nothing */ |
| 2036 | else if (is_fixup(item->command)) |
| 2037 | return error(_("cannot '%s' without a previous commit"), |
| 2038 | command_to_string(item->command)); |
| 2039 | else if (!is_noop(item->command)) |
| 2040 | fixup_okay = 1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2041 | } |
Johannes Schindelin | 5286527 | 2017-01-02 16:27:34 +0100 | [diff] [blame] | 2042 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2043 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2044 | } |
| 2045 | |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2046 | static int count_commands(struct todo_list *todo_list) |
| 2047 | { |
| 2048 | int count = 0, i; |
| 2049 | |
| 2050 | for (i = 0; i < todo_list->nr; i++) |
| 2051 | if (todo_list->items[i].command != TODO_COMMENT) |
| 2052 | count++; |
| 2053 | |
| 2054 | return count; |
| 2055 | } |
| 2056 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2057 | static int get_item_line_offset(struct todo_list *todo_list, int index) |
| 2058 | { |
| 2059 | return index < todo_list->nr ? |
| 2060 | todo_list->items[index].offset_in_buf : todo_list->buf.len; |
| 2061 | } |
| 2062 | |
| 2063 | static const char *get_item_line(struct todo_list *todo_list, int index) |
| 2064 | { |
| 2065 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); |
| 2066 | } |
| 2067 | |
| 2068 | static int get_item_line_length(struct todo_list *todo_list, int index) |
| 2069 | { |
| 2070 | return get_item_line_offset(todo_list, index + 1) |
| 2071 | - get_item_line_offset(todo_list, index); |
| 2072 | } |
| 2073 | |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2074 | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
| 2075 | { |
| 2076 | int fd; |
| 2077 | ssize_t len; |
| 2078 | |
| 2079 | fd = open(path, O_RDONLY); |
| 2080 | if (fd < 0) |
| 2081 | return error_errno(_("could not open '%s'"), path); |
| 2082 | len = strbuf_read(sb, fd, 0); |
| 2083 | close(fd); |
| 2084 | if (len < 0) |
| 2085 | return error(_("could not read '%s'."), path); |
| 2086 | return len; |
| 2087 | } |
| 2088 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2089 | static int read_populate_todo(struct todo_list *todo_list, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2090 | struct replay_opts *opts) |
| 2091 | { |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 2092 | struct stat st; |
Johannes Schindelin | c024650 | 2016-10-21 14:24:32 +0200 | [diff] [blame] | 2093 | const char *todo_file = get_todo_path(opts); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2094 | int res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2095 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2096 | strbuf_reset(&todo_list->buf); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 2097 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
| 2098 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2099 | |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 2100 | res = stat(todo_file, &st); |
| 2101 | if (res) |
| 2102 | return error(_("could not stat '%s'"), todo_file); |
| 2103 | fill_stat_data(&todo_list->stat, &st); |
| 2104 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2105 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
Johannes Schindelin | 27fdbb9 | 2017-01-02 16:35:42 +0100 | [diff] [blame] | 2106 | if (res) { |
| 2107 | if (is_rebase_i(opts)) |
| 2108 | return error(_("please fix this using " |
| 2109 | "'git rebase --edit-todo'.")); |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2110 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
Johannes Schindelin | 27fdbb9 | 2017-01-02 16:35:42 +0100 | [diff] [blame] | 2111 | } |
Johannes Schindelin | 2eeaf1b | 2016-10-21 14:26:13 +0200 | [diff] [blame] | 2112 | |
Johannes Schindelin | 5286527 | 2017-01-02 16:27:34 +0100 | [diff] [blame] | 2113 | if (!todo_list->nr && |
| 2114 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) |
| 2115 | return error(_("no commits parsed.")); |
| 2116 | |
Johannes Schindelin | 2eeaf1b | 2016-10-21 14:26:13 +0200 | [diff] [blame] | 2117 | if (!is_rebase_i(opts)) { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2118 | enum todo_command valid = |
| 2119 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; |
| 2120 | int i; |
| 2121 | |
| 2122 | for (i = 0; i < todo_list->nr; i++) |
| 2123 | if (valid == todo_list->items[i].command) |
| 2124 | continue; |
| 2125 | else if (valid == TODO_PICK) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2126 | return error(_("cannot cherry-pick during a revert.")); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2127 | else |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2128 | return error(_("cannot revert during a cherry-pick.")); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2131 | if (is_rebase_i(opts)) { |
| 2132 | struct todo_list done = TODO_LIST_INIT; |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 2133 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2134 | |
| 2135 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && |
| 2136 | !parse_insn_buffer(done.buf.buf, &done)) |
| 2137 | todo_list->done_nr = count_commands(&done); |
| 2138 | else |
| 2139 | todo_list->done_nr = 0; |
| 2140 | |
| 2141 | todo_list->total_nr = todo_list->done_nr |
| 2142 | + count_commands(todo_list); |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2143 | todo_list_release(&done); |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 2144 | |
| 2145 | if (f) { |
| 2146 | fprintf(f, "%d\n", todo_list->total_nr); |
| 2147 | fclose(f); |
| 2148 | } |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
Johannes Schindelin | 0ae42a0 | 2016-09-09 16:37:24 +0200 | [diff] [blame] | 2151 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2152 | } |
| 2153 | |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2154 | static int git_config_string_dup(char **dest, |
| 2155 | const char *var, const char *value) |
| 2156 | { |
| 2157 | if (!value) |
| 2158 | return config_error_nonbool(var); |
| 2159 | free(*dest); |
| 2160 | *dest = xstrdup(value); |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2164 | static int populate_opts_cb(const char *key, const char *value, void *data) |
| 2165 | { |
| 2166 | struct replay_opts *opts = data; |
| 2167 | int error_flag = 1; |
| 2168 | |
| 2169 | if (!value) |
| 2170 | error_flag = 0; |
| 2171 | else if (!strcmp(key, "options.no-commit")) |
| 2172 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); |
| 2173 | else if (!strcmp(key, "options.edit")) |
| 2174 | opts->edit = git_config_bool_or_int(key, value, &error_flag); |
| 2175 | else if (!strcmp(key, "options.signoff")) |
| 2176 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); |
| 2177 | else if (!strcmp(key, "options.record-origin")) |
| 2178 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); |
| 2179 | else if (!strcmp(key, "options.allow-ff")) |
| 2180 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); |
| 2181 | else if (!strcmp(key, "options.mainline")) |
| 2182 | opts->mainline = git_config_int(key, value); |
| 2183 | else if (!strcmp(key, "options.strategy")) |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2184 | git_config_string_dup(&opts->strategy, key, value); |
Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 +0000 | [diff] [blame] | 2185 | else if (!strcmp(key, "options.gpg-sign")) |
Johannes Schindelin | 03a4e26 | 2016-10-21 14:24:13 +0200 | [diff] [blame] | 2186 | git_config_string_dup(&opts->gpg_sign, key, value); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2187 | else if (!strcmp(key, "options.strategy-option")) { |
| 2188 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
| 2189 | opts->xopts[opts->xopts_nr++] = xstrdup(value); |
Phillip Wood | 8d8cb4b | 2017-08-02 11:44:19 +0100 | [diff] [blame] | 2190 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
| 2191 | opts->allow_rerere_auto = |
| 2192 | git_config_bool_or_int(key, value, &error_flag) ? |
| 2193 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; |
| 2194 | else |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2195 | return error(_("invalid key: %s"), key); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2196 | |
| 2197 | if (!error_flag) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2198 | return error(_("invalid value for %s: %s"), key, value); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2199 | |
| 2200 | return 0; |
| 2201 | } |
| 2202 | |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2203 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
| 2204 | { |
| 2205 | int i; |
| 2206 | |
| 2207 | strbuf_reset(buf); |
| 2208 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) |
| 2209 | return; |
| 2210 | opts->strategy = strbuf_detach(buf, NULL); |
| 2211 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) |
| 2212 | return; |
| 2213 | |
| 2214 | opts->xopts_nr = split_cmdline(buf->buf, (const char ***)&opts->xopts); |
| 2215 | for (i = 0; i < opts->xopts_nr; i++) { |
| 2216 | const char *arg = opts->xopts[i]; |
| 2217 | |
| 2218 | skip_prefix(arg, "--", &arg); |
| 2219 | opts->xopts[i] = xstrdup(arg); |
| 2220 | } |
| 2221 | } |
| 2222 | |
Johannes Schindelin | 5adf9bd | 2016-10-14 15:17:16 +0200 | [diff] [blame] | 2223 | static int read_populate_opts(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2224 | { |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2225 | if (is_rebase_i(opts)) { |
| 2226 | struct strbuf buf = STRBUF_INIT; |
| 2227 | |
| 2228 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { |
| 2229 | if (!starts_with(buf.buf, "-S")) |
| 2230 | strbuf_reset(&buf); |
| 2231 | else { |
| 2232 | free(opts->gpg_sign); |
| 2233 | opts->gpg_sign = xstrdup(buf.buf + 2); |
| 2234 | } |
Phillip Wood | 9b6d7a6 | 2017-08-02 11:44:17 +0100 | [diff] [blame] | 2235 | strbuf_reset(&buf); |
| 2236 | } |
| 2237 | |
| 2238 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), 1)) { |
| 2239 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
| 2240 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; |
| 2241 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) |
| 2242 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; |
| 2243 | strbuf_reset(&buf); |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2244 | } |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2245 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 2246 | if (file_exists(rebase_path_verbose())) |
| 2247 | opts->verbose = 1; |
| 2248 | |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 2249 | if (file_exists(rebase_path_signoff())) { |
| 2250 | opts->allow_ff = 0; |
| 2251 | opts->signoff = 1; |
| 2252 | } |
| 2253 | |
Johannes Schindelin | ca6c6b4 | 2017-01-02 16:28:30 +0100 | [diff] [blame] | 2254 | read_strategy_opts(opts, &buf); |
| 2255 | strbuf_release(&buf); |
| 2256 | |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 2257 | if (read_oneliner(&opts->current_fixups, |
| 2258 | rebase_path_current_fixups(), 1)) { |
| 2259 | const char *p = opts->current_fixups.buf; |
| 2260 | opts->current_fixup_count = 1; |
| 2261 | while ((p = strchr(p, '\n'))) { |
| 2262 | opts->current_fixup_count++; |
| 2263 | p++; |
| 2264 | } |
| 2265 | } |
| 2266 | |
Johannes Schindelin | d87d48b | 2018-05-04 01:01:17 +0200 | [diff] [blame] | 2267 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
| 2268 | if (get_oid_hex(buf.buf, &opts->squash_onto) < 0) |
| 2269 | return error(_("unusable squash-onto")); |
| 2270 | opts->have_squash_onto = 1; |
| 2271 | } |
| 2272 | |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 2273 | return 0; |
Johannes Schindelin | a1c7576 | 2016-10-21 14:25:12 +0200 | [diff] [blame] | 2274 | } |
Johannes Schindelin | b5a6704 | 2016-10-21 14:25:04 +0200 | [diff] [blame] | 2275 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2276 | if (!file_exists(git_path_opts_file())) |
Johannes Schindelin | 0d00da7 | 2016-09-09 16:37:27 +0200 | [diff] [blame] | 2277 | return 0; |
| 2278 | /* |
| 2279 | * The function git_parse_source(), called from git_config_from_file(), |
| 2280 | * may die() in case of a syntactically incorrect file. We do not care |
| 2281 | * about this case, though, because we wrote that file ourselves, so we |
| 2282 | * are pretty certain that it is syntactically correct. |
| 2283 | */ |
Johannes Schindelin | 5adf9bd | 2016-10-14 15:17:16 +0200 | [diff] [blame] | 2284 | 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] | 2285 | return error(_("malformed options sheet: '%s'"), |
Johannes Schindelin | 0d00da7 | 2016-09-09 16:37:27 +0200 | [diff] [blame] | 2286 | git_path_opts_file()); |
| 2287 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2288 | } |
| 2289 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2290 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2291 | struct replay_opts *opts) |
| 2292 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2293 | enum todo_command command = opts->action == REPLAY_PICK ? |
| 2294 | TODO_PICK : TODO_REVERT; |
Johannes Schindelin | 414697a | 2017-01-02 16:27:15 +0100 | [diff] [blame] | 2295 | const char *command_string = todo_command_info[command].str; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2296 | struct commit *commit; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2297 | |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 2298 | if (prepare_revs(opts)) |
| 2299 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2300 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2301 | while ((commit = get_revision(opts->revs))) { |
| 2302 | struct todo_item *item = append_new_todo(todo_list); |
| 2303 | const char *commit_buffer = get_commit_buffer(commit, NULL); |
| 2304 | const char *subject; |
| 2305 | int subject_len; |
| 2306 | |
| 2307 | item->command = command; |
| 2308 | item->commit = commit; |
Johannes Schindelin | c22f7df | 2016-10-21 14:25:00 +0200 | [diff] [blame] | 2309 | item->arg = NULL; |
| 2310 | item->arg_len = 0; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2311 | item->offset_in_buf = todo_list->buf.len; |
| 2312 | subject_len = find_commit_subject(commit_buffer, &subject); |
| 2313 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, |
| 2314 | short_commit_name(commit), subject_len, subject); |
| 2315 | unuse_commit_buffer(commit, commit_buffer); |
| 2316 | } |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 2317 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | static int create_seq_dir(void) |
| 2321 | { |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2322 | if (file_exists(git_path_seq_dir())) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2323 | error(_("a cherry-pick or revert is already in progress")); |
| 2324 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); |
| 2325 | return -1; |
Johannes Schindelin | a70d8f8 | 2017-01-02 16:26:14 +0100 | [diff] [blame] | 2326 | } else if (mkdir(git_path_seq_dir(), 0777) < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2327 | return error_errno(_("could not create sequencer directory '%s'"), |
Johannes Schindelin | f6e82b0 | 2016-09-09 16:37:44 +0200 | [diff] [blame] | 2328 | git_path_seq_dir()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2332 | static int save_head(const char *head) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2333 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 2334 | struct lock_file head_lock = LOCK_INIT; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2335 | struct strbuf buf = STRBUF_INIT; |
| 2336 | int fd; |
Rene Scharfe | ed3f9a1 | 2017-08-30 20:20:14 +0200 | [diff] [blame] | 2337 | ssize_t written; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2338 | |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2339 | 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] | 2340 | if (fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2341 | return error_errno(_("could not lock HEAD")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2342 | strbuf_addf(&buf, "%s\n", head); |
Rene Scharfe | ed3f9a1 | 2017-08-30 20:20:14 +0200 | [diff] [blame] | 2343 | written = write_in_full(fd, buf.buf, buf.len); |
| 2344 | strbuf_release(&buf); |
| 2345 | if (written < 0) { |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 2346 | error_errno(_("could not write to '%s'"), git_path_head_file()); |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2347 | rollback_lock_file(&head_lock); |
Johannes Schindelin | bf5c057 | 2018-04-25 14:28:17 +0200 | [diff] [blame] | 2348 | return -1; |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2349 | } |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 2350 | if (commit_lock_file(&head_lock) < 0) |
| 2351 | return error(_("failed to finalize '%s'"), git_path_head_file()); |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 2352 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2353 | } |
| 2354 | |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2355 | static int rollback_is_safe(void) |
| 2356 | { |
| 2357 | struct strbuf sb = STRBUF_INIT; |
| 2358 | struct object_id expected_head, actual_head; |
| 2359 | |
| 2360 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { |
| 2361 | strbuf_trim(&sb); |
| 2362 | if (get_oid_hex(sb.buf, &expected_head)) { |
| 2363 | strbuf_release(&sb); |
| 2364 | die(_("could not parse %s"), git_path_abort_safety_file()); |
| 2365 | } |
| 2366 | strbuf_release(&sb); |
| 2367 | } |
| 2368 | else if (errno == ENOENT) |
| 2369 | oidclr(&expected_head); |
| 2370 | else |
| 2371 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); |
| 2372 | |
| 2373 | if (get_oid("HEAD", &actual_head)) |
| 2374 | oidclr(&actual_head); |
| 2375 | |
| 2376 | return !oidcmp(&actual_head, &expected_head); |
| 2377 | } |
| 2378 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2379 | static int reset_for_rollback(const struct object_id *oid) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2380 | { |
| 2381 | const char *argv[4]; /* reset --merge <arg> + NULL */ |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2382 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2383 | argv[0] = "reset"; |
| 2384 | argv[1] = "--merge"; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2385 | argv[2] = oid_to_hex(oid); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2386 | argv[3] = NULL; |
| 2387 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 2388 | } |
| 2389 | |
| 2390 | static int rollback_single_pick(void) |
| 2391 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2392 | struct object_id head_oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2393 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2394 | if (!file_exists(git_path_cherry_pick_head()) && |
| 2395 | !file_exists(git_path_revert_head())) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2396 | return error(_("no cherry-pick or revert in progress")); |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 2397 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2398 | return error(_("cannot resolve HEAD")); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2399 | if (is_null_oid(&head_oid)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2400 | return error(_("cannot abort from a branch yet to be born")); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2401 | return reset_for_rollback(&head_oid); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2402 | } |
| 2403 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 2404 | int sequencer_rollback(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2405 | { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2406 | FILE *f; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2407 | struct object_id oid; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2408 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2409 | const char *p; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2410 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2411 | f = fopen(git_path_head_file(), "r"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2412 | if (!f && errno == ENOENT) { |
| 2413 | /* |
| 2414 | * There is no multiple-cherry-pick in progress. |
| 2415 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
| 2416 | * a single-cherry-pick in progress, abort that. |
| 2417 | */ |
| 2418 | return rollback_single_pick(); |
| 2419 | } |
| 2420 | if (!f) |
Johannes Schindelin | f7ed195 | 2016-10-21 14:26:21 +0200 | [diff] [blame] | 2421 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 2422 | if (strbuf_getline_lf(&buf, f)) { |
Johannes Schindelin | f7ed195 | 2016-10-21 14:26:21 +0200 | [diff] [blame] | 2423 | error(_("cannot read '%s': %s"), git_path_head_file(), |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2424 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2425 | fclose(f); |
| 2426 | goto fail; |
| 2427 | } |
| 2428 | fclose(f); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2429 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2430 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2431 | git_path_head_file()); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2432 | goto fail; |
| 2433 | } |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2434 | if (is_null_oid(&oid)) { |
Michael J Gruber | 0f974e2 | 2016-06-06 15:23:54 +0200 | [diff] [blame] | 2435 | error(_("cannot abort from a branch yet to be born")); |
| 2436 | goto fail; |
| 2437 | } |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 2438 | |
| 2439 | if (!rollback_is_safe()) { |
| 2440 | /* Do not error, just do not rollback */ |
| 2441 | warning(_("You seem to have moved HEAD. " |
| 2442 | "Not rewinding, check your HEAD!")); |
| 2443 | } else |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2444 | if (reset_for_rollback(&oid)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2445 | goto fail; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2446 | strbuf_release(&buf); |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 2447 | return sequencer_remove_state(opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2448 | fail: |
| 2449 | strbuf_release(&buf); |
| 2450 | return -1; |
| 2451 | } |
| 2452 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2453 | 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] | 2454 | { |
Martin Ågren | 14bca6c | 2018-02-27 22:30:09 +0100 | [diff] [blame] | 2455 | struct lock_file todo_lock = LOCK_INIT; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2456 | const char *todo_path = get_todo_path(opts); |
| 2457 | int next = todo_list->current, offset, fd; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2458 | |
Johannes Schindelin | 8458395 | 2017-01-02 16:26:28 +0100 | [diff] [blame] | 2459 | /* |
| 2460 | * rebase -i writes "git-rebase-todo" without the currently executing |
| 2461 | * command, appending it to "done" instead. |
| 2462 | */ |
| 2463 | if (is_rebase_i(opts)) |
| 2464 | next++; |
| 2465 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2466 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 2467 | if (fd < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2468 | return error_errno(_("could not lock '%s'"), todo_path); |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2469 | offset = get_item_line_offset(todo_list, next); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2470 | if (write_in_full(fd, todo_list->buf.buf + offset, |
| 2471 | todo_list->buf.len - offset) < 0) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 2472 | return error_errno(_("could not write to '%s'"), todo_path); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 2473 | if (commit_lock_file(&todo_lock) < 0) |
Martin Ågren | 350292a | 2018-02-28 20:07:58 +0100 | [diff] [blame] | 2474 | return error(_("failed to finalize '%s'"), todo_path); |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2475 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2476 | if (is_rebase_i(opts) && next > 0) { |
| 2477 | const char *done = rebase_path_done(); |
| 2478 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); |
| 2479 | int ret = 0; |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2480 | |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 2481 | if (fd < 0) |
| 2482 | return 0; |
| 2483 | if (write_in_full(fd, get_item_line(todo_list, next - 1), |
| 2484 | get_item_line_length(todo_list, next - 1)) |
| 2485 | < 0) |
| 2486 | ret = error_errno(_("could not write to '%s'"), done); |
| 2487 | if (close(fd) < 0) |
| 2488 | ret = error_errno(_("failed to finalize '%s'"), done); |
| 2489 | return ret; |
Johannes Schindelin | 1df6df0 | 2017-01-02 16:27:00 +0100 | [diff] [blame] | 2490 | } |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 2491 | return 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2492 | } |
| 2493 | |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2494 | static int save_opts(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2495 | { |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 2496 | const char *opts_file = git_path_opts_file(); |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2497 | int res = 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2498 | |
| 2499 | if (opts->no_commit) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2500 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2501 | if (opts->edit) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2502 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2503 | if (opts->signoff) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2504 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2505 | if (opts->record_origin) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2506 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2507 | if (opts->allow_ff) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2508 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2509 | if (opts->mainline) { |
| 2510 | struct strbuf buf = STRBUF_INIT; |
| 2511 | strbuf_addf(&buf, "%d", opts->mainline); |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2512 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2513 | strbuf_release(&buf); |
| 2514 | } |
| 2515 | if (opts->strategy) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2516 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 +0000 | [diff] [blame] | 2517 | if (opts->gpg_sign) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2518 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2519 | if (opts->xopts) { |
| 2520 | int i; |
| 2521 | for (i = 0; i < opts->xopts_nr; i++) |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2522 | res |= git_config_set_multivar_in_file_gently(opts_file, |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2523 | "options.strategy-option", |
| 2524 | opts->xopts[i], "^$", 0); |
| 2525 | } |
Phillip Wood | 8d8cb4b | 2017-08-02 11:44:19 +0100 | [diff] [blame] | 2526 | if (opts->allow_rerere_auto) |
| 2527 | res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto", |
| 2528 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? |
| 2529 | "true" : "false"); |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 2530 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 2531 | } |
| 2532 | |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 2533 | static int make_patch(struct commit *commit, struct replay_opts *opts) |
| 2534 | { |
| 2535 | struct strbuf buf = STRBUF_INIT; |
| 2536 | struct rev_info log_tree_opt; |
| 2537 | const char *subject, *p; |
| 2538 | int res = 0; |
| 2539 | |
| 2540 | p = short_commit_name(commit); |
| 2541 | if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) |
| 2542 | return -1; |
Nguyễn Thái Ngọc Duy | fbd7a23 | 2018-02-11 16:43:28 +0700 | [diff] [blame] | 2543 | if (update_ref("rebase", "REBASE_HEAD", &commit->object.oid, |
| 2544 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
| 2545 | res |= error(_("could not update %s"), "REBASE_HEAD"); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 2546 | |
| 2547 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); |
| 2548 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
| 2549 | init_revisions(&log_tree_opt, NULL); |
| 2550 | log_tree_opt.abbrev = 0; |
| 2551 | log_tree_opt.diff = 1; |
| 2552 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; |
| 2553 | log_tree_opt.disable_stdin = 1; |
| 2554 | log_tree_opt.no_commit_id = 1; |
| 2555 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); |
| 2556 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; |
| 2557 | if (!log_tree_opt.diffopt.file) |
| 2558 | res |= error_errno(_("could not open '%s'"), buf.buf); |
| 2559 | else { |
| 2560 | res |= log_tree_commit(&log_tree_opt, commit); |
| 2561 | fclose(log_tree_opt.diffopt.file); |
| 2562 | } |
| 2563 | strbuf_reset(&buf); |
| 2564 | |
| 2565 | strbuf_addf(&buf, "%s/message", get_dir(opts)); |
| 2566 | if (!file_exists(buf.buf)) { |
| 2567 | const char *commit_buffer = get_commit_buffer(commit, NULL); |
| 2568 | find_commit_subject(commit_buffer, &subject); |
| 2569 | res |= write_message(subject, strlen(subject), buf.buf, 1); |
| 2570 | unuse_commit_buffer(commit, commit_buffer); |
| 2571 | } |
| 2572 | strbuf_release(&buf); |
| 2573 | |
| 2574 | return res; |
| 2575 | } |
| 2576 | |
| 2577 | static int intend_to_amend(void) |
| 2578 | { |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2579 | struct object_id head; |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 2580 | char *p; |
| 2581 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2582 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 2583 | return error(_("cannot read HEAD")); |
| 2584 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 2585 | p = oid_to_hex(&head); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 2586 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
| 2587 | } |
| 2588 | |
| 2589 | static int error_with_patch(struct commit *commit, |
| 2590 | const char *subject, int subject_len, |
| 2591 | struct replay_opts *opts, int exit_code, int to_amend) |
| 2592 | { |
| 2593 | if (make_patch(commit, opts)) |
| 2594 | return -1; |
| 2595 | |
| 2596 | if (to_amend) { |
| 2597 | if (intend_to_amend()) |
| 2598 | return -1; |
| 2599 | |
| 2600 | fprintf(stderr, "You can amend the commit now, with\n" |
| 2601 | "\n" |
| 2602 | " git commit --amend %s\n" |
| 2603 | "\n" |
| 2604 | "Once you are satisfied with your changes, run\n" |
| 2605 | "\n" |
| 2606 | " git rebase --continue\n", gpg_sign_opt_quoted(opts)); |
| 2607 | } else if (exit_code) |
| 2608 | fprintf(stderr, "Could not apply %s... %.*s\n", |
| 2609 | short_commit_name(commit), subject_len, subject); |
| 2610 | |
| 2611 | return exit_code; |
| 2612 | } |
| 2613 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2614 | static int error_failed_squash(struct commit *commit, |
| 2615 | struct replay_opts *opts, int subject_len, const char *subject) |
| 2616 | { |
Johannes Schindelin | e12a7ef | 2018-04-27 22:48:21 +0200 | [diff] [blame] | 2617 | if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666)) |
| 2618 | return error(_("could not copy '%s' to '%s'"), |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2619 | rebase_path_squash_msg(), rebase_path_message()); |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 2620 | unlink(git_path_merge_msg()); |
| 2621 | if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666)) |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2622 | return error(_("could not copy '%s' to '%s'"), |
Jeff King | ca03e06 | 2017-04-20 17:08:54 -0400 | [diff] [blame] | 2623 | rebase_path_message(), git_path_merge_msg()); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 2624 | return error_with_patch(commit, subject, subject_len, opts, 1, 0); |
| 2625 | } |
| 2626 | |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 2627 | static int do_exec(const char *command_line) |
| 2628 | { |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 2629 | struct argv_array child_env = ARGV_ARRAY_INIT; |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 2630 | const char *child_argv[] = { NULL, NULL }; |
| 2631 | int dirty, status; |
| 2632 | |
| 2633 | fprintf(stderr, "Executing: %s\n", command_line); |
| 2634 | child_argv[0] = command_line; |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 2635 | argv_array_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); |
| 2636 | status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, |
| 2637 | child_env.argv); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 2638 | |
| 2639 | /* force re-reading of the cache */ |
| 2640 | if (discard_cache() < 0 || read_cache() < 0) |
| 2641 | return error(_("could not read index")); |
| 2642 | |
| 2643 | dirty = require_clean_work_tree("rebase", NULL, 1, 1); |
| 2644 | |
| 2645 | if (status) { |
| 2646 | warning(_("execution failed: %s\n%s" |
| 2647 | "You can fix the problem, and then run\n" |
| 2648 | "\n" |
| 2649 | " git rebase --continue\n" |
| 2650 | "\n"), |
| 2651 | command_line, |
| 2652 | dirty ? N_("and made changes to the index and/or the " |
| 2653 | "working tree\n") : ""); |
| 2654 | if (status == 127) |
| 2655 | /* command not found */ |
| 2656 | status = 1; |
| 2657 | } else if (dirty) { |
| 2658 | warning(_("execution succeeded: %s\nbut " |
| 2659 | "left changes to the index and/or the working tree\n" |
| 2660 | "Commit or stash your changes, and then run\n" |
| 2661 | "\n" |
| 2662 | " git rebase --continue\n" |
| 2663 | "\n"), command_line); |
| 2664 | status = 1; |
| 2665 | } |
| 2666 | |
Jacob Keller | 09d7b6c | 2017-10-31 16:07:33 -0700 | [diff] [blame] | 2667 | argv_array_clear(&child_env); |
| 2668 | |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 2669 | return status; |
| 2670 | } |
| 2671 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 2672 | static int safe_append(const char *filename, const char *fmt, ...) |
| 2673 | { |
| 2674 | va_list ap; |
| 2675 | struct lock_file lock = LOCK_INIT; |
| 2676 | int fd = hold_lock_file_for_update(&lock, filename, |
| 2677 | LOCK_REPORT_ON_ERROR); |
| 2678 | struct strbuf buf = STRBUF_INIT; |
| 2679 | |
| 2680 | if (fd < 0) |
| 2681 | return -1; |
| 2682 | |
| 2683 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { |
| 2684 | error_errno(_("could not read '%s'"), filename); |
| 2685 | rollback_lock_file(&lock); |
| 2686 | return -1; |
| 2687 | } |
| 2688 | strbuf_complete(&buf, '\n'); |
| 2689 | va_start(ap, fmt); |
| 2690 | strbuf_vaddf(&buf, fmt, ap); |
| 2691 | va_end(ap); |
| 2692 | |
| 2693 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 2694 | error_errno(_("could not write to '%s'"), filename); |
| 2695 | strbuf_release(&buf); |
| 2696 | rollback_lock_file(&lock); |
| 2697 | return -1; |
| 2698 | } |
| 2699 | if (commit_lock_file(&lock) < 0) { |
| 2700 | strbuf_release(&buf); |
| 2701 | rollback_lock_file(&lock); |
| 2702 | return error(_("failed to finalize '%s'"), filename); |
| 2703 | } |
| 2704 | |
| 2705 | strbuf_release(&buf); |
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
| 2709 | static int do_label(const char *name, int len) |
| 2710 | { |
Junio C Hamano | 2c18e6a | 2018-05-23 14:38:20 +0900 | [diff] [blame] | 2711 | struct ref_store *refs = get_main_ref_store(the_repository); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 2712 | struct ref_transaction *transaction; |
| 2713 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; |
| 2714 | struct strbuf msg = STRBUF_INIT; |
| 2715 | int ret = 0; |
| 2716 | struct object_id head_oid; |
| 2717 | |
| 2718 | if (len == 1 && *name == '#') |
| 2719 | return error("Illegal label name: '%.*s'", len, name); |
| 2720 | |
| 2721 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
| 2722 | strbuf_addf(&msg, "rebase -i (label) '%.*s'", len, name); |
| 2723 | |
| 2724 | transaction = ref_store_transaction_begin(refs, &err); |
| 2725 | if (!transaction) { |
| 2726 | error("%s", err.buf); |
| 2727 | ret = -1; |
| 2728 | } else if (get_oid("HEAD", &head_oid)) { |
| 2729 | error(_("could not read HEAD")); |
| 2730 | ret = -1; |
| 2731 | } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, |
| 2732 | NULL, 0, msg.buf, &err) < 0 || |
| 2733 | ref_transaction_commit(transaction, &err)) { |
| 2734 | error("%s", err.buf); |
| 2735 | ret = -1; |
| 2736 | } |
| 2737 | ref_transaction_free(transaction); |
| 2738 | strbuf_release(&err); |
| 2739 | strbuf_release(&msg); |
| 2740 | |
| 2741 | if (!ret) |
| 2742 | ret = safe_append(rebase_path_refs_to_delete(), |
| 2743 | "%s\n", ref_name.buf); |
| 2744 | strbuf_release(&ref_name); |
| 2745 | |
| 2746 | return ret; |
| 2747 | } |
| 2748 | |
| 2749 | static const char *reflog_message(struct replay_opts *opts, |
| 2750 | const char *sub_action, const char *fmt, ...); |
| 2751 | |
| 2752 | static int do_reset(const char *name, int len, struct replay_opts *opts) |
| 2753 | { |
| 2754 | struct strbuf ref_name = STRBUF_INIT; |
| 2755 | struct object_id oid; |
| 2756 | struct lock_file lock = LOCK_INIT; |
| 2757 | struct tree_desc desc; |
| 2758 | struct tree *tree; |
| 2759 | struct unpack_trees_options unpack_tree_opts; |
| 2760 | int ret = 0, i; |
| 2761 | |
| 2762 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) |
| 2763 | return -1; |
| 2764 | |
Johannes Schindelin | ebddf39 | 2018-05-04 01:01:23 +0200 | [diff] [blame] | 2765 | if (len == 10 && !strncmp("[new root]", name, len)) { |
| 2766 | if (!opts->have_squash_onto) { |
| 2767 | const char *hex; |
| 2768 | if (commit_tree("", 0, the_hash_algo->empty_tree, |
| 2769 | NULL, &opts->squash_onto, |
| 2770 | NULL, NULL)) |
| 2771 | return error(_("writing fake root commit")); |
| 2772 | opts->have_squash_onto = 1; |
| 2773 | hex = oid_to_hex(&opts->squash_onto); |
| 2774 | if (write_message(hex, strlen(hex), |
| 2775 | rebase_path_squash_onto(), 0)) |
| 2776 | return error(_("writing squash-onto")); |
| 2777 | } |
| 2778 | oidcpy(&oid, &opts->squash_onto); |
| 2779 | } else { |
| 2780 | /* Determine the length of the label */ |
| 2781 | for (i = 0; i < len; i++) |
| 2782 | if (isspace(name[i])) |
| 2783 | len = i; |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 2784 | |
Johannes Schindelin | ebddf39 | 2018-05-04 01:01:23 +0200 | [diff] [blame] | 2785 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); |
| 2786 | if (get_oid(ref_name.buf, &oid) && |
| 2787 | get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) { |
| 2788 | error(_("could not read '%s'"), ref_name.buf); |
| 2789 | rollback_lock_file(&lock); |
| 2790 | strbuf_release(&ref_name); |
| 2791 | return -1; |
| 2792 | } |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); |
| 2796 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); |
| 2797 | unpack_tree_opts.head_idx = 1; |
| 2798 | unpack_tree_opts.src_index = &the_index; |
| 2799 | unpack_tree_opts.dst_index = &the_index; |
| 2800 | unpack_tree_opts.fn = oneway_merge; |
| 2801 | unpack_tree_opts.merge = 1; |
| 2802 | unpack_tree_opts.update = 1; |
| 2803 | |
| 2804 | if (read_cache_unmerged()) { |
| 2805 | rollback_lock_file(&lock); |
| 2806 | strbuf_release(&ref_name); |
| 2807 | return error_resolve_conflict(_(action_name(opts))); |
| 2808 | } |
| 2809 | |
| 2810 | if (!fill_tree_descriptor(&desc, &oid)) { |
| 2811 | error(_("failed to find tree of %s"), oid_to_hex(&oid)); |
| 2812 | rollback_lock_file(&lock); |
| 2813 | free((void *)desc.buffer); |
| 2814 | strbuf_release(&ref_name); |
| 2815 | return -1; |
| 2816 | } |
| 2817 | |
| 2818 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { |
| 2819 | rollback_lock_file(&lock); |
| 2820 | free((void *)desc.buffer); |
| 2821 | strbuf_release(&ref_name); |
| 2822 | return -1; |
| 2823 | } |
| 2824 | |
| 2825 | tree = parse_tree_indirect(&oid); |
| 2826 | prime_cache_tree(&the_index, tree); |
| 2827 | |
| 2828 | if (write_locked_index(&the_index, &lock, COMMIT_LOCK) < 0) |
| 2829 | ret = error(_("could not write index")); |
| 2830 | free((void *)desc.buffer); |
| 2831 | |
| 2832 | if (!ret) |
| 2833 | ret = update_ref(reflog_message(opts, "reset", "'%.*s'", |
| 2834 | len, name), "HEAD", &oid, |
| 2835 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
| 2836 | |
| 2837 | strbuf_release(&ref_name); |
| 2838 | return ret; |
| 2839 | } |
| 2840 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2841 | static int do_merge(struct commit *commit, const char *arg, int arg_len, |
| 2842 | int flags, struct replay_opts *opts) |
| 2843 | { |
| 2844 | int run_commit_flags = (flags & TODO_EDIT_MERGE_MSG) ? |
| 2845 | EDIT_MSG | VERIFY_MSG : 0; |
| 2846 | struct strbuf ref_name = STRBUF_INIT; |
| 2847 | struct commit *head_commit, *merge_commit, *i; |
| 2848 | struct commit_list *bases, *j, *reversed = NULL; |
| 2849 | struct merge_options o; |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 2850 | int merge_arg_len, oneline_offset, can_fast_forward, ret; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2851 | static struct lock_file lock; |
| 2852 | const char *p; |
| 2853 | |
| 2854 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { |
| 2855 | ret = -1; |
| 2856 | goto leave_merge; |
| 2857 | } |
| 2858 | |
| 2859 | head_commit = lookup_commit_reference_by_name("HEAD"); |
| 2860 | if (!head_commit) { |
| 2861 | ret = error(_("cannot merge without a current revision")); |
| 2862 | goto leave_merge; |
| 2863 | } |
| 2864 | |
| 2865 | oneline_offset = arg_len; |
| 2866 | merge_arg_len = strcspn(arg, " \t\n"); |
| 2867 | p = arg + merge_arg_len; |
| 2868 | p += strspn(p, " \t\n"); |
| 2869 | if (*p == '#' && (!p[1] || isspace(p[1]))) { |
| 2870 | p += 1 + strspn(p + 1, " \t\n"); |
| 2871 | oneline_offset = p - arg; |
| 2872 | } else if (p - arg < arg_len) |
| 2873 | BUG("octopus merges are not supported yet: '%s'", p); |
| 2874 | |
| 2875 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", merge_arg_len, arg); |
| 2876 | merge_commit = lookup_commit_reference_by_name(ref_name.buf); |
| 2877 | if (!merge_commit) { |
| 2878 | /* fall back to non-rewritten ref or commit */ |
| 2879 | strbuf_splice(&ref_name, 0, strlen("refs/rewritten/"), "", 0); |
| 2880 | merge_commit = lookup_commit_reference_by_name(ref_name.buf); |
| 2881 | } |
| 2882 | |
| 2883 | if (!merge_commit) { |
| 2884 | ret = error(_("could not resolve '%s'"), ref_name.buf); |
| 2885 | goto leave_merge; |
| 2886 | } |
| 2887 | |
Johannes Schindelin | 9c85a1c | 2018-05-04 01:01:28 +0200 | [diff] [blame] | 2888 | if (opts->have_squash_onto && |
| 2889 | !oidcmp(&head_commit->object.oid, &opts->squash_onto)) { |
| 2890 | /* |
| 2891 | * When the user tells us to "merge" something into a |
| 2892 | * "[new root]", let's simply fast-forward to the merge head. |
| 2893 | */ |
| 2894 | rollback_lock_file(&lock); |
| 2895 | ret = fast_forward_to(&merge_commit->object.oid, |
| 2896 | &head_commit->object.oid, 0, opts); |
| 2897 | goto leave_merge; |
| 2898 | } |
| 2899 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2900 | if (commit) { |
| 2901 | const char *message = get_commit_buffer(commit, NULL); |
| 2902 | const char *body; |
| 2903 | int len; |
| 2904 | |
| 2905 | if (!message) { |
| 2906 | ret = error(_("could not get commit message of '%s'"), |
| 2907 | oid_to_hex(&commit->object.oid)); |
| 2908 | goto leave_merge; |
| 2909 | } |
| 2910 | write_author_script(message); |
| 2911 | find_commit_subject(message, &body); |
| 2912 | len = strlen(body); |
| 2913 | ret = write_message(body, len, git_path_merge_msg(), 0); |
| 2914 | unuse_commit_buffer(commit, message); |
| 2915 | if (ret) { |
| 2916 | error_errno(_("could not write '%s'"), |
| 2917 | git_path_merge_msg()); |
| 2918 | goto leave_merge; |
| 2919 | } |
| 2920 | } else { |
| 2921 | struct strbuf buf = STRBUF_INIT; |
| 2922 | int len; |
| 2923 | |
| 2924 | strbuf_addf(&buf, "author %s", git_author_info(0)); |
| 2925 | write_author_script(buf.buf); |
| 2926 | strbuf_reset(&buf); |
| 2927 | |
| 2928 | if (oneline_offset < arg_len) { |
| 2929 | p = arg + oneline_offset; |
| 2930 | len = arg_len - oneline_offset; |
| 2931 | } else { |
| 2932 | strbuf_addf(&buf, "Merge branch '%.*s'", |
| 2933 | merge_arg_len, arg); |
| 2934 | p = buf.buf; |
| 2935 | len = buf.len; |
| 2936 | } |
| 2937 | |
| 2938 | ret = write_message(p, len, git_path_merge_msg(), 0); |
| 2939 | strbuf_release(&buf); |
| 2940 | if (ret) { |
| 2941 | error_errno(_("could not write '%s'"), |
| 2942 | git_path_merge_msg()); |
| 2943 | goto leave_merge; |
| 2944 | } |
| 2945 | } |
| 2946 | |
Johannes Schindelin | d1e8b01 | 2018-04-25 14:28:56 +0200 | [diff] [blame] | 2947 | /* |
| 2948 | * If HEAD is not identical to the first parent of the original merge |
| 2949 | * commit, we cannot fast-forward. |
| 2950 | */ |
| 2951 | can_fast_forward = opts->allow_ff && commit && commit->parents && |
| 2952 | !oidcmp(&commit->parents->item->object.oid, |
| 2953 | &head_commit->object.oid); |
| 2954 | |
| 2955 | /* |
| 2956 | * If the merge head is different from the original one, we cannot |
| 2957 | * fast-forward. |
| 2958 | */ |
| 2959 | if (can_fast_forward) { |
| 2960 | struct commit_list *second_parent = commit->parents->next; |
| 2961 | |
| 2962 | if (second_parent && !second_parent->next && |
| 2963 | oidcmp(&merge_commit->object.oid, |
| 2964 | &second_parent->item->object.oid)) |
| 2965 | can_fast_forward = 0; |
| 2966 | } |
| 2967 | |
| 2968 | if (can_fast_forward && commit->parents->next && |
| 2969 | !commit->parents->next->next && |
| 2970 | !oidcmp(&commit->parents->next->item->object.oid, |
| 2971 | &merge_commit->object.oid)) { |
| 2972 | rollback_lock_file(&lock); |
| 2973 | ret = fast_forward_to(&commit->object.oid, |
| 2974 | &head_commit->object.oid, 0, opts); |
| 2975 | goto leave_merge; |
| 2976 | } |
| 2977 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2978 | write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ, |
| 2979 | git_path_merge_head(), 0); |
| 2980 | write_message("no-ff", 5, git_path_merge_mode(), 0); |
| 2981 | |
| 2982 | bases = get_merge_bases(head_commit, merge_commit); |
Johannes Schindelin | 7ccdf65 | 2018-04-25 14:29:31 +0200 | [diff] [blame] | 2983 | if (bases && !oidcmp(&merge_commit->object.oid, |
| 2984 | &bases->item->object.oid)) { |
| 2985 | ret = 0; |
| 2986 | /* skip merging an ancestor of HEAD */ |
| 2987 | goto leave_merge; |
| 2988 | } |
| 2989 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 2990 | for (j = bases; j; j = j->next) |
| 2991 | commit_list_insert(j->item, &reversed); |
| 2992 | free_commit_list(bases); |
| 2993 | |
| 2994 | read_cache(); |
| 2995 | init_merge_options(&o); |
| 2996 | o.branch1 = "HEAD"; |
| 2997 | o.branch2 = ref_name.buf; |
| 2998 | o.buffer_output = 2; |
| 2999 | |
| 3000 | ret = merge_recursive(&o, head_commit, merge_commit, reversed, &i); |
| 3001 | if (ret <= 0) |
| 3002 | fputs(o.obuf.buf, stdout); |
| 3003 | strbuf_release(&o.obuf); |
| 3004 | if (ret < 0) { |
| 3005 | error(_("could not even attempt to merge '%.*s'"), |
| 3006 | merge_arg_len, arg); |
| 3007 | goto leave_merge; |
| 3008 | } |
| 3009 | /* |
| 3010 | * The return value of merge_recursive() is 1 on clean, and 0 on |
| 3011 | * unclean merge. |
| 3012 | * |
| 3013 | * Let's reverse that, so that do_merge() returns 0 upon success and |
| 3014 | * 1 upon failed merge (keeping the return value -1 for the cases where |
| 3015 | * we will want to reschedule the `merge` command). |
| 3016 | */ |
| 3017 | ret = !ret; |
| 3018 | |
| 3019 | if (active_cache_changed && |
| 3020 | write_locked_index(&the_index, &lock, COMMIT_LOCK)) { |
| 3021 | ret = error(_("merge: Unable to write new index file")); |
| 3022 | goto leave_merge; |
| 3023 | } |
| 3024 | |
| 3025 | rollback_lock_file(&lock); |
| 3026 | if (ret) |
| 3027 | rerere(opts->allow_rerere_auto); |
| 3028 | else |
| 3029 | /* |
| 3030 | * In case of problems, we now want to return a positive |
| 3031 | * value (a negative one would indicate that the `merge` |
| 3032 | * command needs to be rescheduled). |
| 3033 | */ |
| 3034 | ret = !!run_git_commit(git_path_merge_msg(), opts, |
| 3035 | run_commit_flags); |
| 3036 | |
| 3037 | leave_merge: |
| 3038 | strbuf_release(&ref_name); |
| 3039 | rollback_lock_file(&lock); |
| 3040 | return ret; |
| 3041 | } |
| 3042 | |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3043 | static int is_final_fixup(struct todo_list *todo_list) |
| 3044 | { |
| 3045 | int i = todo_list->current; |
| 3046 | |
| 3047 | if (!is_fixup(todo_list->items[i].command)) |
| 3048 | return 0; |
| 3049 | |
| 3050 | while (++i < todo_list->nr) |
| 3051 | if (is_fixup(todo_list->items[i].command)) |
| 3052 | return 0; |
| 3053 | else if (!is_noop(todo_list->items[i].command)) |
| 3054 | break; |
| 3055 | return 1; |
| 3056 | } |
| 3057 | |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3058 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
| 3059 | { |
| 3060 | int i; |
| 3061 | |
| 3062 | for (i = todo_list->current + offset; i < todo_list->nr; i++) |
| 3063 | if (!is_noop(todo_list->items[i].command)) |
| 3064 | return todo_list->items[i].command; |
| 3065 | |
| 3066 | return -1; |
| 3067 | } |
| 3068 | |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3069 | static int apply_autostash(struct replay_opts *opts) |
| 3070 | { |
| 3071 | struct strbuf stash_sha1 = STRBUF_INIT; |
| 3072 | struct child_process child = CHILD_PROCESS_INIT; |
| 3073 | int ret = 0; |
| 3074 | |
| 3075 | if (!read_oneliner(&stash_sha1, rebase_path_autostash(), 1)) { |
| 3076 | strbuf_release(&stash_sha1); |
| 3077 | return 0; |
| 3078 | } |
| 3079 | strbuf_trim(&stash_sha1); |
| 3080 | |
| 3081 | child.git_cmd = 1; |
Phillip Wood | 79a6226 | 2017-05-18 11:02:33 +0100 | [diff] [blame] | 3082 | child.no_stdout = 1; |
| 3083 | child.no_stderr = 1; |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3084 | argv_array_push(&child.args, "stash"); |
| 3085 | argv_array_push(&child.args, "apply"); |
| 3086 | argv_array_push(&child.args, stash_sha1.buf); |
| 3087 | if (!run_command(&child)) |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 3088 | fprintf(stderr, _("Applied autostash.\n")); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3089 | else { |
| 3090 | struct child_process store = CHILD_PROCESS_INIT; |
| 3091 | |
| 3092 | store.git_cmd = 1; |
| 3093 | argv_array_push(&store.args, "stash"); |
| 3094 | argv_array_push(&store.args, "store"); |
| 3095 | argv_array_push(&store.args, "-m"); |
| 3096 | argv_array_push(&store.args, "autostash"); |
| 3097 | argv_array_push(&store.args, "-q"); |
| 3098 | argv_array_push(&store.args, stash_sha1.buf); |
| 3099 | if (run_command(&store)) |
| 3100 | ret = error(_("cannot store %s"), stash_sha1.buf); |
| 3101 | else |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 3102 | fprintf(stderr, |
| 3103 | _("Applying autostash resulted in conflicts.\n" |
| 3104 | "Your changes are safe in the stash.\n" |
| 3105 | "You can run \"git stash pop\" or" |
| 3106 | " \"git stash drop\" at any time.\n")); |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | strbuf_release(&stash_sha1); |
| 3110 | return ret; |
| 3111 | } |
| 3112 | |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3113 | static const char *reflog_message(struct replay_opts *opts, |
| 3114 | const char *sub_action, const char *fmt, ...) |
| 3115 | { |
| 3116 | va_list ap; |
| 3117 | static struct strbuf buf = STRBUF_INIT; |
| 3118 | |
| 3119 | va_start(ap, fmt); |
| 3120 | strbuf_reset(&buf); |
| 3121 | strbuf_addstr(&buf, action_name(opts)); |
| 3122 | if (sub_action) |
| 3123 | strbuf_addf(&buf, " (%s)", sub_action); |
| 3124 | if (fmt) { |
| 3125 | strbuf_addstr(&buf, ": "); |
| 3126 | strbuf_vaddf(&buf, fmt, ap); |
| 3127 | } |
| 3128 | va_end(ap); |
| 3129 | |
| 3130 | return buf.buf; |
| 3131 | } |
| 3132 | |
Johannes Schindelin | cb5206e | 2018-04-25 14:28:33 +0200 | [diff] [blame] | 3133 | static const char rescheduled_advice[] = |
| 3134 | N_("Could not execute the todo command\n" |
| 3135 | "\n" |
| 3136 | " %.*s" |
| 3137 | "\n" |
| 3138 | "It has been rescheduled; To edit the command before continuing, please\n" |
| 3139 | "edit the todo list first:\n" |
| 3140 | "\n" |
| 3141 | " git rebase --edit-todo\n" |
| 3142 | " git rebase --continue\n"); |
| 3143 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3144 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3145 | { |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3146 | int res = 0, reschedule = 0; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3147 | |
| 3148 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
| 3149 | if (opts->allow_ff) |
| 3150 | assert(!(opts->signoff || opts->no_commit || |
| 3151 | opts->record_origin || opts->edit)); |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 3152 | if (read_and_refresh_cache(opts)) |
| 3153 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3154 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3155 | while (todo_list->current < todo_list->nr) { |
| 3156 | struct todo_item *item = todo_list->items + todo_list->current; |
| 3157 | if (save_todo(todo_list, opts)) |
Johannes Schindelin | 221675d | 2016-09-09 16:37:50 +0200 | [diff] [blame] | 3158 | return -1; |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3159 | if (is_rebase_i(opts)) { |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 3160 | if (item->command != TODO_COMMENT) { |
| 3161 | FILE *f = fopen(rebase_path_msgnum(), "w"); |
| 3162 | |
| 3163 | todo_list->done_nr++; |
| 3164 | |
| 3165 | if (f) { |
| 3166 | fprintf(f, "%d\n", todo_list->done_nr); |
| 3167 | fclose(f); |
| 3168 | } |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 3169 | fprintf(stderr, "Rebasing (%d/%d)%s", |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 3170 | todo_list->done_nr, |
Johannes Schindelin | 968492e | 2017-01-02 16:35:46 +0100 | [diff] [blame] | 3171 | todo_list->total_nr, |
| 3172 | opts->verbose ? "\n" : "\r"); |
Johannes Schindelin | ef80069 | 2017-01-02 16:36:20 +0100 | [diff] [blame] | 3173 | } |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3174 | unlink(rebase_path_message()); |
| 3175 | unlink(rebase_path_author_script()); |
| 3176 | unlink(rebase_path_stopped_sha()); |
| 3177 | unlink(rebase_path_amend()); |
Nguyễn Thái Ngọc Duy | fbd7a23 | 2018-02-11 16:43:28 +0700 | [diff] [blame] | 3178 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3179 | } |
| 3180 | if (item->command <= TODO_SQUASH) { |
Johannes Schindelin | 8ab37ef | 2017-01-02 16:28:13 +0100 | [diff] [blame] | 3181 | if (is_rebase_i(opts)) |
| 3182 | setenv("GIT_REFLOG_ACTION", reflog_message(opts, |
| 3183 | command_to_string(item->command), NULL), |
| 3184 | 1); |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 3185 | res = do_pick_commit(item->command, item->commit, |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3186 | opts, is_final_fixup(todo_list)); |
Johannes Schindelin | 9d7bf3c | 2017-01-02 16:28:34 +0100 | [diff] [blame] | 3187 | if (is_rebase_i(opts) && res < 0) { |
| 3188 | /* Reschedule */ |
Johannes Schindelin | cb5206e | 2018-04-25 14:28:33 +0200 | [diff] [blame] | 3189 | advise(_(rescheduled_advice), |
| 3190 | get_item_line_length(todo_list, |
| 3191 | todo_list->current), |
| 3192 | get_item_line(todo_list, |
| 3193 | todo_list->current)); |
Johannes Schindelin | 9d7bf3c | 2017-01-02 16:28:34 +0100 | [diff] [blame] | 3194 | todo_list->current--; |
| 3195 | if (save_todo(todo_list, opts)) |
| 3196 | return -1; |
| 3197 | } |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3198 | if (item->command == TODO_EDIT) { |
| 3199 | struct commit *commit = item->commit; |
| 3200 | if (!res) |
Jeff King | 9942921 | 2017-03-16 20:19:42 -0400 | [diff] [blame] | 3201 | fprintf(stderr, |
Brandon Williams | a42e1b4 | 2017-03-23 10:02:33 -0700 | [diff] [blame] | 3202 | _("Stopped at %s... %.*s\n"), |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3203 | short_commit_name(commit), |
| 3204 | item->arg_len, item->arg); |
| 3205 | return error_with_patch(commit, |
| 3206 | item->arg, item->arg_len, opts, res, |
| 3207 | !res); |
| 3208 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3209 | if (is_rebase_i(opts) && !res) |
| 3210 | record_in_rewritten(&item->commit->object.oid, |
| 3211 | peek_command(todo_list, 1)); |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3212 | if (res && is_fixup(item->command)) { |
| 3213 | if (res == 1) |
| 3214 | intend_to_amend(); |
| 3215 | return error_failed_squash(item->commit, opts, |
| 3216 | item->arg_len, item->arg); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3217 | } else if (res && is_rebase_i(opts) && item->commit) |
Johannes Schindelin | 4a5146f | 2017-01-02 16:27:57 +0100 | [diff] [blame] | 3218 | return res | error_with_patch(item->commit, |
Johannes Schindelin | 04efc8b | 2017-01-02 16:28:00 +0100 | [diff] [blame] | 3219 | item->arg, item->arg_len, opts, res, |
| 3220 | item->command == TODO_REWORD); |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3221 | } else if (item->command == TODO_EXEC) { |
| 3222 | char *end_of_arg = (char *)(item->arg + item->arg_len); |
| 3223 | int saved = *end_of_arg; |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 3224 | struct stat st; |
Johannes Schindelin | 311af52 | 2017-01-02 16:26:47 +0100 | [diff] [blame] | 3225 | |
| 3226 | *end_of_arg = '\0'; |
| 3227 | res = do_exec(item->arg); |
| 3228 | *end_of_arg = saved; |
Stephen Hicks | 54fd324 | 2017-04-26 21:17:40 +0200 | [diff] [blame] | 3229 | |
| 3230 | /* Reread the todo file if it has changed. */ |
| 3231 | if (res) |
| 3232 | ; /* fall through */ |
| 3233 | else if (stat(get_todo_path(opts), &st)) |
| 3234 | res = error_errno(_("could not stat '%s'"), |
| 3235 | get_todo_path(opts)); |
| 3236 | else if (match_stat_data(&todo_list->stat, &st)) { |
| 3237 | todo_list_release(todo_list); |
| 3238 | if (read_populate_todo(todo_list, opts)) |
| 3239 | res = -1; /* message was printed */ |
| 3240 | /* `current` will be incremented below */ |
| 3241 | todo_list->current = -1; |
| 3242 | } |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3243 | } else if (item->command == TODO_LABEL) { |
| 3244 | if ((res = do_label(item->arg, item->arg_len))) |
| 3245 | reschedule = 1; |
| 3246 | } else if (item->command == TODO_RESET) { |
| 3247 | if ((res = do_reset(item->arg, item->arg_len, opts))) |
| 3248 | reschedule = 1; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3249 | } else if (item->command == TODO_MERGE) { |
| 3250 | if ((res = do_merge(item->commit, |
| 3251 | item->arg, item->arg_len, |
| 3252 | item->flags, opts)) < 0) |
| 3253 | reschedule = 1; |
Johannes Schindelin | 537e7d6 | 2018-04-25 14:29:29 +0200 | [diff] [blame] | 3254 | else if (item->commit) |
| 3255 | record_in_rewritten(&item->commit->object.oid, |
| 3256 | peek_command(todo_list, 1)); |
| 3257 | if (res > 0) |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3258 | /* failed with merge conflicts */ |
| 3259 | return error_with_patch(item->commit, |
| 3260 | item->arg, |
| 3261 | item->arg_len, opts, |
| 3262 | res, 0); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3263 | } else if (!is_noop(item->command)) |
Johannes Schindelin | 25c4366 | 2017-01-02 16:26:38 +0100 | [diff] [blame] | 3264 | return error(_("unknown command %d"), item->command); |
| 3265 | |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3266 | if (reschedule) { |
| 3267 | advise(_(rescheduled_advice), |
| 3268 | get_item_line_length(todo_list, |
| 3269 | todo_list->current), |
| 3270 | get_item_line(todo_list, todo_list->current)); |
| 3271 | todo_list->current--; |
| 3272 | if (save_todo(todo_list, opts)) |
| 3273 | return -1; |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 3274 | if (item->commit) |
| 3275 | return error_with_patch(item->commit, |
| 3276 | item->arg, |
| 3277 | item->arg_len, opts, |
| 3278 | res, 0); |
Johannes Schindelin | 9055e40 | 2018-04-25 14:28:47 +0200 | [diff] [blame] | 3279 | } |
| 3280 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3281 | todo_list->current++; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3282 | if (res) |
| 3283 | return res; |
| 3284 | } |
| 3285 | |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3286 | if (is_rebase_i(opts)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3287 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3288 | struct stat st; |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 3289 | |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3290 | /* Stopped in the middle, as planned? */ |
| 3291 | if (todo_list->current < todo_list->nr) |
| 3292 | return 0; |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 3293 | |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3294 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
| 3295 | starts_with(head_ref.buf, "refs/")) { |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3296 | const char *msg; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3297 | struct object_id head, orig; |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3298 | int res; |
| 3299 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3300 | if (get_oid("HEAD", &head)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3301 | res = error(_("cannot read HEAD")); |
| 3302 | cleanup_head_ref: |
| 3303 | strbuf_release(&head_ref); |
| 3304 | strbuf_release(&buf); |
| 3305 | return res; |
| 3306 | } |
| 3307 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3308 | get_oid_hex(buf.buf, &orig)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3309 | res = error(_("could not read orig-head")); |
| 3310 | goto cleanup_head_ref; |
| 3311 | } |
Phillip Wood | 4ab867b | 2017-05-18 11:02:32 +0100 | [diff] [blame] | 3312 | strbuf_reset(&buf); |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3313 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
| 3314 | res = error(_("could not read 'onto'")); |
| 3315 | goto cleanup_head_ref; |
| 3316 | } |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3317 | msg = reflog_message(opts, "finish", "%s onto %s", |
| 3318 | head_ref.buf, buf.buf); |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 3319 | if (update_ref(msg, head_ref.buf, &head, &orig, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 3320 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3321 | res = error(_("could not update %s"), |
| 3322 | head_ref.buf); |
| 3323 | goto cleanup_head_ref; |
| 3324 | } |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3325 | msg = reflog_message(opts, "finish", "returning to %s", |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3326 | head_ref.buf); |
Johannes Schindelin | 96e832a | 2017-01-02 16:28:09 +0100 | [diff] [blame] | 3327 | if (create_symref("HEAD", head_ref.buf, msg)) { |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3328 | res = error(_("could not update HEAD to %s"), |
| 3329 | head_ref.buf); |
| 3330 | goto cleanup_head_ref; |
| 3331 | } |
| 3332 | strbuf_reset(&buf); |
| 3333 | } |
| 3334 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 3335 | if (opts->verbose) { |
| 3336 | struct rev_info log_tree_opt; |
| 3337 | struct object_id orig, head; |
| 3338 | |
| 3339 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); |
| 3340 | init_revisions(&log_tree_opt, NULL); |
| 3341 | log_tree_opt.diff = 1; |
| 3342 | log_tree_opt.diffopt.output_format = |
| 3343 | DIFF_FORMAT_DIFFSTAT; |
| 3344 | log_tree_opt.disable_stdin = 1; |
| 3345 | |
| 3346 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 3347 | !get_oid(buf.buf, &orig) && |
| 3348 | !get_oid("HEAD", &head)) { |
Brandon Williams | 66f414f | 2017-05-30 10:31:03 -0700 | [diff] [blame] | 3349 | diff_tree_oid(&orig, &head, "", |
| 3350 | &log_tree_opt.diffopt); |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 3351 | log_tree_diff_flush(&log_tree_opt); |
| 3352 | } |
| 3353 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3354 | flush_rewritten_pending(); |
| 3355 | if (!stat(rebase_path_rewritten_list(), &st) && |
| 3356 | st.st_size > 0) { |
| 3357 | struct child_process child = CHILD_PROCESS_INIT; |
Johannes Schindelin | 7951604 | 2017-01-02 16:28:23 +0100 | [diff] [blame] | 3358 | const char *post_rewrite_hook = |
| 3359 | find_hook("post-rewrite"); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3360 | |
| 3361 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); |
| 3362 | child.git_cmd = 1; |
| 3363 | argv_array_push(&child.args, "notes"); |
| 3364 | argv_array_push(&child.args, "copy"); |
| 3365 | argv_array_push(&child.args, "--for-rewrite=rebase"); |
| 3366 | /* we don't care if this copying failed */ |
| 3367 | run_command(&child); |
Johannes Schindelin | 7951604 | 2017-01-02 16:28:23 +0100 | [diff] [blame] | 3368 | |
| 3369 | if (post_rewrite_hook) { |
| 3370 | struct child_process hook = CHILD_PROCESS_INIT; |
| 3371 | |
| 3372 | hook.in = open(rebase_path_rewritten_list(), |
| 3373 | O_RDONLY); |
| 3374 | hook.stdout_to_stderr = 1; |
| 3375 | argv_array_push(&hook.args, post_rewrite_hook); |
| 3376 | argv_array_push(&hook.args, "rebase"); |
| 3377 | /* we don't care if this hook failed */ |
| 3378 | run_command(&hook); |
| 3379 | } |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3380 | } |
Johannes Schindelin | 796c797 | 2017-01-02 16:28:27 +0100 | [diff] [blame] | 3381 | apply_autostash(opts); |
Johannes Schindelin | 25cb8df | 2017-01-02 16:28:16 +0100 | [diff] [blame] | 3382 | |
Johannes Schindelin | 5da4966 | 2017-01-02 16:36:25 +0100 | [diff] [blame] | 3383 | fprintf(stderr, "Successfully rebased and updated %s.\n", |
| 3384 | head_ref.buf); |
| 3385 | |
Johannes Schindelin | 556907f | 2017-01-02 16:26:53 +0100 | [diff] [blame] | 3386 | strbuf_release(&buf); |
Johannes Schindelin | 4b83ce9 | 2017-01-02 16:27:53 +0100 | [diff] [blame] | 3387 | strbuf_release(&head_ref); |
Johannes Schindelin | 56dc3ab | 2017-01-02 16:26:43 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3390 | /* |
| 3391 | * Sequence of picks finished successfully; cleanup by |
| 3392 | * removing the .git/sequencer directory |
| 3393 | */ |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 3394 | return sequencer_remove_state(opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static int continue_single_pick(void) |
| 3398 | { |
| 3399 | const char *argv[] = { "commit", NULL }; |
| 3400 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 3401 | if (!file_exists(git_path_cherry_pick_head()) && |
| 3402 | !file_exists(git_path_revert_head())) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3403 | return error(_("no cherry-pick or revert in progress")); |
| 3404 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 3405 | } |
| 3406 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3407 | static int commit_staged_changes(struct replay_opts *opts, |
| 3408 | struct todo_list *todo_list) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3409 | { |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 3410 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3411 | unsigned int final_fixup = 0, is_clean; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3412 | |
| 3413 | if (has_unstaged_changes(1)) |
| 3414 | return error(_("cannot rebase: You have unstaged changes.")); |
Johannes Schindelin | 5263220 | 2017-01-02 16:27:25 +0100 | [diff] [blame] | 3415 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3416 | is_clean = !has_uncommitted_changes(0); |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3417 | |
| 3418 | if (file_exists(rebase_path_amend())) { |
| 3419 | struct strbuf rev = STRBUF_INIT; |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3420 | struct object_id head, to_amend; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3421 | |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3422 | if (get_oid("HEAD", &head)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3423 | return error(_("cannot amend non-existing commit")); |
| 3424 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) |
| 3425 | return error(_("invalid file: '%s'"), rebase_path_amend()); |
brian m. carlson | 092bbcd | 2017-07-13 23:49:22 +0000 | [diff] [blame] | 3426 | if (get_oid_hex(rev.buf, &to_amend)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3427 | return error(_("invalid contents: '%s'"), |
| 3428 | rebase_path_amend()); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3429 | if (!is_clean && oidcmp(&head, &to_amend)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3430 | return error(_("\nYou have uncommitted changes in your " |
| 3431 | "working tree. Please, commit them\n" |
| 3432 | "first and then run 'git rebase " |
| 3433 | "--continue' again.")); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3434 | /* |
| 3435 | * When skipping a failed fixup/squash, we need to edit the |
| 3436 | * commit message, the current fixup list and count, and if it |
| 3437 | * was the last fixup/squash in the chain, we need to clean up |
| 3438 | * the commit message and if there was a squash, let the user |
| 3439 | * edit it. |
| 3440 | */ |
| 3441 | if (is_clean && !oidcmp(&head, &to_amend) && |
| 3442 | opts->current_fixup_count > 0 && |
| 3443 | file_exists(rebase_path_stopped_sha())) { |
| 3444 | const char *p = opts->current_fixups.buf; |
| 3445 | int len = opts->current_fixups.len; |
| 3446 | |
| 3447 | opts->current_fixup_count--; |
| 3448 | if (!len) |
| 3449 | BUG("Incorrect current_fixups:\n%s", p); |
| 3450 | while (len && p[len - 1] != '\n') |
| 3451 | len--; |
| 3452 | strbuf_setlen(&opts->current_fixups, len); |
| 3453 | if (write_message(p, len, rebase_path_current_fixups(), |
| 3454 | 0) < 0) |
| 3455 | return error(_("could not write file: '%s'"), |
| 3456 | rebase_path_current_fixups()); |
| 3457 | |
| 3458 | /* |
| 3459 | * If a fixup/squash in a fixup/squash chain failed, the |
| 3460 | * commit message is already correct, no need to commit |
| 3461 | * it again. |
| 3462 | * |
| 3463 | * Only if it is the final command in the fixup/squash |
| 3464 | * chain, and only if the chain is longer than a single |
| 3465 | * fixup/squash command (which was just skipped), do we |
| 3466 | * actually need to re-commit with a cleaned up commit |
| 3467 | * message. |
| 3468 | */ |
| 3469 | if (opts->current_fixup_count > 0 && |
| 3470 | !is_fixup(peek_command(todo_list, 0))) { |
| 3471 | final_fixup = 1; |
| 3472 | /* |
| 3473 | * If there was not a single "squash" in the |
| 3474 | * chain, we only need to clean up the commit |
| 3475 | * message, no need to bother the user with |
| 3476 | * opening the commit message in the editor. |
| 3477 | */ |
| 3478 | if (!starts_with(p, "squash ") && |
| 3479 | !strstr(p, "\nsquash ")) |
| 3480 | flags = (flags & ~EDIT_MSG) | CLEANUP_MSG; |
| 3481 | } else if (is_fixup(peek_command(todo_list, 0))) { |
| 3482 | /* |
| 3483 | * We need to update the squash message to skip |
| 3484 | * the latest commit message. |
| 3485 | */ |
| 3486 | struct commit *commit; |
| 3487 | const char *path = rebase_path_squash_msg(); |
| 3488 | |
| 3489 | if (parse_head(&commit) || |
| 3490 | !(p = get_commit_buffer(commit, NULL)) || |
| 3491 | write_message(p, strlen(p), path, 0)) { |
| 3492 | unuse_commit_buffer(commit, p); |
| 3493 | return error(_("could not write file: " |
| 3494 | "'%s'"), path); |
| 3495 | } |
| 3496 | unuse_commit_buffer(commit, p); |
| 3497 | } |
| 3498 | } |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3499 | |
| 3500 | strbuf_release(&rev); |
Johannes Schindelin | 789b3ef | 2017-03-23 17:07:11 +0100 | [diff] [blame] | 3501 | flags |= AMEND_MSG; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3502 | } |
| 3503 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3504 | if (is_clean) { |
| 3505 | const char *cherry_pick_head = git_path_cherry_pick_head(); |
| 3506 | |
| 3507 | if (file_exists(cherry_pick_head) && unlink(cherry_pick_head)) |
| 3508 | return error(_("could not remove CHERRY_PICK_HEAD")); |
| 3509 | if (!final_fixup) |
| 3510 | return 0; |
| 3511 | } |
| 3512 | |
| 3513 | if (run_git_commit(final_fixup ? NULL : rebase_path_message(), |
| 3514 | opts, flags)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3515 | return error(_("could not commit staged changes.")); |
| 3516 | unlink(rebase_path_amend()); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3517 | if (final_fixup) { |
| 3518 | unlink(rebase_path_fixup_msg()); |
| 3519 | unlink(rebase_path_squash_msg()); |
| 3520 | } |
| 3521 | if (opts->current_fixup_count > 0) { |
| 3522 | /* |
| 3523 | * Whether final fixup or not, we just cleaned up the commit |
| 3524 | * message... |
| 3525 | */ |
| 3526 | unlink(rebase_path_current_fixups()); |
| 3527 | strbuf_reset(&opts->current_fixups); |
| 3528 | opts->current_fixup_count = 0; |
| 3529 | } |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3530 | return 0; |
| 3531 | } |
| 3532 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 3533 | int sequencer_continue(struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3534 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3535 | struct todo_list todo_list = TODO_LIST_INIT; |
| 3536 | int res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3537 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 3538 | if (read_and_refresh_cache(opts)) |
| 3539 | return -1; |
| 3540 | |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3541 | if (read_populate_opts(opts)) |
| 3542 | return -1; |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3543 | if (is_rebase_i(opts)) { |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3544 | if ((res = read_populate_todo(&todo_list, opts))) |
| 3545 | goto release_todo_list; |
| 3546 | if (commit_staged_changes(opts, &todo_list)) |
Johannes Schindelin | 9d93ccd | 2017-01-02 16:27:21 +0100 | [diff] [blame] | 3547 | return -1; |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 3548 | } else if (!file_exists(get_todo_path(opts))) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3549 | return continue_single_pick(); |
Johannes Schindelin | 15ef693 | 2018-04-27 22:48:30 +0200 | [diff] [blame] | 3550 | else if ((res = read_populate_todo(&todo_list, opts))) |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3551 | goto release_todo_list; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3552 | |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 3553 | if (!is_rebase_i(opts)) { |
| 3554 | /* Verify that the conflict has been resolved */ |
| 3555 | if (file_exists(git_path_cherry_pick_head()) || |
| 3556 | file_exists(git_path_revert_head())) { |
| 3557 | res = continue_single_pick(); |
| 3558 | if (res) |
| 3559 | goto release_todo_list; |
| 3560 | } |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 3561 | if (index_differs_from("HEAD", NULL, 0)) { |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 3562 | res = error_dirty_index(opts); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3563 | goto release_todo_list; |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 3564 | } |
| 3565 | todo_list.current++; |
Johannes Schindelin | ca98c6d | 2017-01-02 16:28:20 +0100 | [diff] [blame] | 3566 | } else if (file_exists(rebase_path_stopped_sha())) { |
| 3567 | struct strbuf buf = STRBUF_INIT; |
| 3568 | struct object_id oid; |
| 3569 | |
| 3570 | if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) && |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 3571 | !get_oid_committish(buf.buf, &oid)) |
Johannes Schindelin | ca98c6d | 2017-01-02 16:28:20 +0100 | [diff] [blame] | 3572 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
| 3573 | strbuf_release(&buf); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3574 | } |
Johannes Schindelin | 4258a6d | 2017-01-02 16:27:30 +0100 | [diff] [blame] | 3575 | |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3576 | res = pick_commits(&todo_list, opts); |
| 3577 | release_todo_list: |
| 3578 | todo_list_release(&todo_list); |
| 3579 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | static int single_pick(struct commit *cmit, struct replay_opts *opts) |
| 3583 | { |
| 3584 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3585 | return do_pick_commit(opts->action == REPLAY_PICK ? |
Johannes Schindelin | 6e98de7 | 2017-01-02 16:27:07 +0100 | [diff] [blame] | 3586 | TODO_PICK : TODO_REVERT, cmit, opts, 0); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | int sequencer_pick_revisions(struct replay_opts *opts) |
| 3590 | { |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3591 | struct todo_list todo_list = TODO_LIST_INIT; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3592 | struct object_id oid; |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3593 | int i, res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3594 | |
Johannes Schindelin | 2863584 | 2016-10-21 14:24:55 +0200 | [diff] [blame] | 3595 | assert(opts->revs); |
Johannes Schindelin | 0d9c6dc | 2016-09-09 16:37:21 +0200 | [diff] [blame] | 3596 | if (read_and_refresh_cache(opts)) |
| 3597 | return -1; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3598 | |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 3599 | for (i = 0; i < opts->revs->pending.nr; i++) { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3600 | struct object_id oid; |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 3601 | const char *name = opts->revs->pending.objects[i].name; |
| 3602 | |
| 3603 | /* This happens when using --stdin. */ |
| 3604 | if (!strlen(name)) |
| 3605 | continue; |
| 3606 | |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3607 | if (!get_oid(name, &oid)) { |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 3608 | if (!lookup_commit_reference_gently(&oid, 1)) { |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 3609 | enum object_type type = oid_object_info(the_repository, |
| 3610 | &oid, |
brian m. carlson | abef902 | 2018-03-12 02:27:46 +0000 | [diff] [blame] | 3611 | NULL); |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 3612 | return error(_("%s: can't cherry-pick a %s"), |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 3613 | name, type_name(type)); |
Junio C Hamano | 7c0b0d8 | 2013-05-09 13:27:49 -0700 | [diff] [blame] | 3614 | } |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 3615 | } else |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 3616 | return error(_("%s: bad revision"), name); |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 3617 | } |
| 3618 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3619 | /* |
| 3620 | * If we were called as "git cherry-pick <commit>", just |
| 3621 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / |
| 3622 | * REVERT_HEAD, and don't touch the sequencer state. |
| 3623 | * This means it is possible to cherry-pick in the middle |
| 3624 | * of a cherry-pick sequence. |
| 3625 | */ |
| 3626 | if (opts->revs->cmdline.nr == 1 && |
| 3627 | opts->revs->cmdline.rev->whence == REV_CMD_REV && |
| 3628 | opts->revs->no_walk && |
| 3629 | !opts->revs->cmdline.rev->flags) { |
| 3630 | struct commit *cmit; |
| 3631 | if (prepare_revision_walk(opts->revs)) |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 3632 | return error(_("revision walk setup failed")); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3633 | cmit = get_revision(opts->revs); |
| 3634 | if (!cmit || get_revision(opts->revs)) |
Johannes Schindelin | b9b946d | 2016-08-26 15:47:10 +0200 | [diff] [blame] | 3635 | return error("BUG: expected exactly one commit from walk"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3636 | return single_pick(cmit, opts); |
| 3637 | } |
| 3638 | |
| 3639 | /* |
| 3640 | * Start a new cherry-pick/ revert sequence; but |
| 3641 | * first, make sure that an existing one isn't in |
| 3642 | * progress |
| 3643 | */ |
| 3644 | |
Johannes Schindelin | 34b0528 | 2016-09-09 16:37:15 +0200 | [diff] [blame] | 3645 | if (walk_revs_populate_todo(&todo_list, opts) || |
| 3646 | create_seq_dir() < 0) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3647 | return -1; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3648 | if (get_oid("HEAD", &oid) && (opts->action == REPLAY_REVERT)) |
Johannes Schindelin | 93b3df6 | 2016-10-21 14:26:25 +0200 | [diff] [blame] | 3649 | return error(_("can't revert as initial commit")); |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 3650 | if (save_head(oid_to_hex(&oid))) |
Johannes Schindelin | 311fd39 | 2016-09-09 16:37:47 +0200 | [diff] [blame] | 3651 | return -1; |
Johannes Schindelin | 88d5a27 | 2016-09-09 16:37:53 +0200 | [diff] [blame] | 3652 | if (save_opts(opts)) |
| 3653 | return -1; |
Stephan Beyer | 1e41229 | 2016-12-07 22:51:32 +0100 | [diff] [blame] | 3654 | update_abort_safety_file(); |
Johannes Schindelin | 004fefa | 2016-10-21 14:24:41 +0200 | [diff] [blame] | 3655 | res = pick_commits(&todo_list, opts); |
| 3656 | todo_list_release(&todo_list); |
| 3657 | return res; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 3658 | } |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3659 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3660 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3661 | { |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3662 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3663 | struct strbuf sob = STRBUF_INIT; |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3664 | int has_footer; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3665 | |
| 3666 | strbuf_addstr(&sob, sign_off_header); |
| 3667 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
| 3668 | getenv("GIT_COMMITTER_EMAIL"))); |
| 3669 | strbuf_addch(&sob, '\n'); |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3670 | |
Jonathan Tan | 44dc738 | 2017-04-26 13:50:03 -0700 | [diff] [blame] | 3671 | if (!ignore_footer) |
| 3672 | strbuf_complete_line(msgbuf); |
| 3673 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3674 | /* |
| 3675 | * If the whole message buffer is equal to the sob, pretend that we |
| 3676 | * found a conforming footer with a matching sob |
| 3677 | */ |
| 3678 | if (msgbuf->len - ignore_footer == sob.len && |
| 3679 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
| 3680 | has_footer = 3; |
| 3681 | else |
| 3682 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
| 3683 | |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 3684 | if (!has_footer) { |
| 3685 | const char *append_newlines = NULL; |
| 3686 | size_t len = msgbuf->len - ignore_footer; |
| 3687 | |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 3688 | if (!len) { |
| 3689 | /* |
| 3690 | * The buffer is completely empty. Leave foom for |
| 3691 | * the title and body to be filled in by the user. |
| 3692 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 3693 | append_newlines = "\n\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 3694 | } else if (len == 1) { |
| 3695 | /* |
| 3696 | * Buffer contains a single newline. Add another |
| 3697 | * so that we leave room for the title and body. |
| 3698 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 3699 | append_newlines = "\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 3700 | } else if (msgbuf->buf[len - 2] != '\n') { |
| 3701 | /* |
| 3702 | * Buffer ends with a single newline. Add another |
| 3703 | * so that there is an empty line between the message |
| 3704 | * body and the sob. |
| 3705 | */ |
| 3706 | append_newlines = "\n"; |
| 3707 | } /* else, the buffer already ends with two newlines. */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 3708 | |
| 3709 | if (append_newlines) |
| 3710 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 3711 | append_newlines, strlen(append_newlines)); |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3712 | } |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 3713 | |
| 3714 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
| 3715 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 3716 | sob.buf, sob.len); |
| 3717 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 3718 | strbuf_release(&sob); |
| 3719 | } |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 3720 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 3721 | struct labels_entry { |
| 3722 | struct hashmap_entry entry; |
| 3723 | char label[FLEX_ARRAY]; |
| 3724 | }; |
| 3725 | |
| 3726 | static int labels_cmp(const void *fndata, const struct labels_entry *a, |
| 3727 | const struct labels_entry *b, const void *key) |
| 3728 | { |
| 3729 | return key ? strcmp(a->label, key) : strcmp(a->label, b->label); |
| 3730 | } |
| 3731 | |
| 3732 | struct string_entry { |
| 3733 | struct oidmap_entry entry; |
| 3734 | char string[FLEX_ARRAY]; |
| 3735 | }; |
| 3736 | |
| 3737 | struct label_state { |
| 3738 | struct oidmap commit2label; |
| 3739 | struct hashmap labels; |
| 3740 | struct strbuf buf; |
| 3741 | }; |
| 3742 | |
| 3743 | static const char *label_oid(struct object_id *oid, const char *label, |
| 3744 | struct label_state *state) |
| 3745 | { |
| 3746 | struct labels_entry *labels_entry; |
| 3747 | struct string_entry *string_entry; |
| 3748 | struct object_id dummy; |
| 3749 | size_t len; |
| 3750 | int i; |
| 3751 | |
| 3752 | string_entry = oidmap_get(&state->commit2label, oid); |
| 3753 | if (string_entry) |
| 3754 | return string_entry->string; |
| 3755 | |
| 3756 | /* |
| 3757 | * For "uninteresting" commits, i.e. commits that are not to be |
| 3758 | * rebased, and which can therefore not be labeled, we use a unique |
| 3759 | * abbreviation of the commit name. This is slightly more complicated |
| 3760 | * than calling find_unique_abbrev() because we also need to make |
| 3761 | * sure that the abbreviation does not conflict with any other |
| 3762 | * label. |
| 3763 | * |
| 3764 | * We disallow "interesting" commits to be labeled by a string that |
| 3765 | * is a valid full-length hash, to ensure that we always can find an |
| 3766 | * abbreviation for any uninteresting commit's names that does not |
| 3767 | * clash with any other label. |
| 3768 | */ |
| 3769 | if (!label) { |
| 3770 | char *p; |
| 3771 | |
| 3772 | strbuf_reset(&state->buf); |
| 3773 | strbuf_grow(&state->buf, GIT_SHA1_HEXSZ); |
| 3774 | label = p = state->buf.buf; |
| 3775 | |
| 3776 | find_unique_abbrev_r(p, oid, default_abbrev); |
| 3777 | |
| 3778 | /* |
| 3779 | * We may need to extend the abbreviated hash so that there is |
| 3780 | * no conflicting label. |
| 3781 | */ |
| 3782 | if (hashmap_get_from_hash(&state->labels, strihash(p), p)) { |
| 3783 | size_t i = strlen(p) + 1; |
| 3784 | |
| 3785 | oid_to_hex_r(p, oid); |
| 3786 | for (; i < GIT_SHA1_HEXSZ; i++) { |
| 3787 | char save = p[i]; |
| 3788 | p[i] = '\0'; |
| 3789 | if (!hashmap_get_from_hash(&state->labels, |
| 3790 | strihash(p), p)) |
| 3791 | break; |
| 3792 | p[i] = save; |
| 3793 | } |
| 3794 | } |
brian m. carlson | 5971b08 | 2018-05-29 16:32:36 +0000 | [diff] [blame] | 3795 | } else if (((len = strlen(label)) == the_hash_algo->hexsz && |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 3796 | !get_oid_hex(label, &dummy)) || |
| 3797 | (len == 1 && *label == '#') || |
| 3798 | hashmap_get_from_hash(&state->labels, |
| 3799 | strihash(label), label)) { |
| 3800 | /* |
| 3801 | * If the label already exists, or if the label is a valid full |
| 3802 | * OID, or the label is a '#' (which we use as a separator |
| 3803 | * between merge heads and oneline), we append a dash and a |
| 3804 | * number to make it unique. |
| 3805 | */ |
| 3806 | struct strbuf *buf = &state->buf; |
| 3807 | |
| 3808 | strbuf_reset(buf); |
| 3809 | strbuf_add(buf, label, len); |
| 3810 | |
| 3811 | for (i = 2; ; i++) { |
| 3812 | strbuf_setlen(buf, len); |
| 3813 | strbuf_addf(buf, "-%d", i); |
| 3814 | if (!hashmap_get_from_hash(&state->labels, |
| 3815 | strihash(buf->buf), |
| 3816 | buf->buf)) |
| 3817 | break; |
| 3818 | } |
| 3819 | |
| 3820 | label = buf->buf; |
| 3821 | } |
| 3822 | |
| 3823 | FLEX_ALLOC_STR(labels_entry, label, label); |
| 3824 | hashmap_entry_init(labels_entry, strihash(label)); |
| 3825 | hashmap_add(&state->labels, labels_entry); |
| 3826 | |
| 3827 | FLEX_ALLOC_STR(string_entry, string, label); |
| 3828 | oidcpy(&string_entry->entry.oid, oid); |
| 3829 | oidmap_put(&state->commit2label, string_entry); |
| 3830 | |
| 3831 | return string_entry->string; |
| 3832 | } |
| 3833 | |
| 3834 | static int make_script_with_merges(struct pretty_print_context *pp, |
| 3835 | struct rev_info *revs, FILE *out, |
| 3836 | unsigned flags) |
| 3837 | { |
| 3838 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 3839 | int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 3840 | struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT; |
| 3841 | struct strbuf label = STRBUF_INIT; |
| 3842 | struct commit_list *commits = NULL, **tail = &commits, *iter; |
| 3843 | struct commit_list *tips = NULL, **tips_tail = &tips; |
| 3844 | struct commit *commit; |
| 3845 | struct oidmap commit2todo = OIDMAP_INIT; |
| 3846 | struct string_entry *entry; |
| 3847 | struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, |
| 3848 | shown = OIDSET_INIT; |
| 3849 | struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT }; |
| 3850 | |
| 3851 | int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; |
| 3852 | const char *cmd_pick = abbr ? "p" : "pick", |
| 3853 | *cmd_label = abbr ? "l" : "label", |
| 3854 | *cmd_reset = abbr ? "t" : "reset", |
| 3855 | *cmd_merge = abbr ? "m" : "merge"; |
| 3856 | |
| 3857 | oidmap_init(&commit2todo, 0); |
| 3858 | oidmap_init(&state.commit2label, 0); |
| 3859 | hashmap_init(&state.labels, (hashmap_cmp_fn) labels_cmp, NULL, 0); |
| 3860 | strbuf_init(&state.buf, 32); |
| 3861 | |
| 3862 | if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) { |
| 3863 | struct object_id *oid = &revs->cmdline.rev[0].item->oid; |
| 3864 | FLEX_ALLOC_STR(entry, string, "onto"); |
| 3865 | oidcpy(&entry->entry.oid, oid); |
| 3866 | oidmap_put(&state.commit2label, entry); |
| 3867 | } |
| 3868 | |
| 3869 | /* |
| 3870 | * First phase: |
| 3871 | * - get onelines for all commits |
| 3872 | * - gather all branch tips (i.e. 2nd or later parents of merges) |
| 3873 | * - label all branch tips |
| 3874 | */ |
| 3875 | while ((commit = get_revision(revs))) { |
| 3876 | struct commit_list *to_merge; |
| 3877 | int is_octopus; |
| 3878 | const char *p1, *p2; |
| 3879 | struct object_id *oid; |
| 3880 | int is_empty; |
| 3881 | |
| 3882 | tail = &commit_list_insert(commit, tail)->next; |
| 3883 | oidset_insert(&interesting, &commit->object.oid); |
| 3884 | |
| 3885 | is_empty = is_original_commit_empty(commit); |
| 3886 | if (!is_empty && (commit->object.flags & PATCHSAME)) |
| 3887 | continue; |
| 3888 | |
| 3889 | strbuf_reset(&oneline); |
| 3890 | pretty_print_commit(pp, commit, &oneline); |
| 3891 | |
| 3892 | to_merge = commit->parents ? commit->parents->next : NULL; |
| 3893 | if (!to_merge) { |
| 3894 | /* non-merge commit: easy case */ |
| 3895 | strbuf_reset(&buf); |
| 3896 | if (!keep_empty && is_empty) |
| 3897 | strbuf_addf(&buf, "%c ", comment_line_char); |
| 3898 | strbuf_addf(&buf, "%s %s %s", cmd_pick, |
| 3899 | oid_to_hex(&commit->object.oid), |
| 3900 | oneline.buf); |
| 3901 | |
| 3902 | FLEX_ALLOC_STR(entry, string, buf.buf); |
| 3903 | oidcpy(&entry->entry.oid, &commit->object.oid); |
| 3904 | oidmap_put(&commit2todo, entry); |
| 3905 | |
| 3906 | continue; |
| 3907 | } |
| 3908 | |
| 3909 | is_octopus = to_merge && to_merge->next; |
| 3910 | |
| 3911 | if (is_octopus) |
| 3912 | BUG("Octopus merges not yet supported"); |
| 3913 | |
| 3914 | /* Create a label */ |
| 3915 | strbuf_reset(&label); |
| 3916 | if (skip_prefix(oneline.buf, "Merge ", &p1) && |
| 3917 | (p1 = strchr(p1, '\'')) && |
| 3918 | (p2 = strchr(++p1, '\''))) |
| 3919 | strbuf_add(&label, p1, p2 - p1); |
| 3920 | else if (skip_prefix(oneline.buf, "Merge pull request ", |
| 3921 | &p1) && |
| 3922 | (p1 = strstr(p1, " from "))) |
| 3923 | strbuf_addstr(&label, p1 + strlen(" from ")); |
| 3924 | else |
| 3925 | strbuf_addbuf(&label, &oneline); |
| 3926 | |
| 3927 | for (p1 = label.buf; *p1; p1++) |
| 3928 | if (isspace(*p1)) |
| 3929 | *(char *)p1 = '-'; |
| 3930 | |
| 3931 | strbuf_reset(&buf); |
| 3932 | strbuf_addf(&buf, "%s -C %s", |
| 3933 | cmd_merge, oid_to_hex(&commit->object.oid)); |
| 3934 | |
| 3935 | /* label the tip of merged branch */ |
| 3936 | oid = &to_merge->item->object.oid; |
| 3937 | strbuf_addch(&buf, ' '); |
| 3938 | |
| 3939 | if (!oidset_contains(&interesting, oid)) |
| 3940 | strbuf_addstr(&buf, label_oid(oid, NULL, &state)); |
| 3941 | else { |
| 3942 | tips_tail = &commit_list_insert(to_merge->item, |
| 3943 | tips_tail)->next; |
| 3944 | |
| 3945 | strbuf_addstr(&buf, label_oid(oid, label.buf, &state)); |
| 3946 | } |
| 3947 | strbuf_addf(&buf, " # %s", oneline.buf); |
| 3948 | |
| 3949 | FLEX_ALLOC_STR(entry, string, buf.buf); |
| 3950 | oidcpy(&entry->entry.oid, &commit->object.oid); |
| 3951 | oidmap_put(&commit2todo, entry); |
| 3952 | } |
| 3953 | |
| 3954 | /* |
| 3955 | * Second phase: |
| 3956 | * - label branch points |
| 3957 | * - add HEAD to the branch tips |
| 3958 | */ |
| 3959 | for (iter = commits; iter; iter = iter->next) { |
| 3960 | struct commit_list *parent = iter->item->parents; |
| 3961 | for (; parent; parent = parent->next) { |
| 3962 | struct object_id *oid = &parent->item->object.oid; |
| 3963 | if (!oidset_contains(&interesting, oid)) |
| 3964 | continue; |
| 3965 | if (!oidset_contains(&child_seen, oid)) |
| 3966 | oidset_insert(&child_seen, oid); |
| 3967 | else |
| 3968 | label_oid(oid, "branch-point", &state); |
| 3969 | } |
| 3970 | |
| 3971 | /* Add HEAD as implict "tip of branch" */ |
| 3972 | if (!iter->next) |
| 3973 | tips_tail = &commit_list_insert(iter->item, |
| 3974 | tips_tail)->next; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
| 3978 | * Third phase: output the todo list. This is a bit tricky, as we |
| 3979 | * want to avoid jumping back and forth between revisions. To |
| 3980 | * accomplish that goal, we walk backwards from the branch tips, |
| 3981 | * gathering commits not yet shown, reversing the list on the fly, |
| 3982 | * then outputting that list (labeling revisions as needed). |
| 3983 | */ |
| 3984 | fprintf(out, "%s onto\n", cmd_label); |
| 3985 | for (iter = tips; iter; iter = iter->next) { |
| 3986 | struct commit_list *list = NULL, *iter2; |
| 3987 | |
| 3988 | commit = iter->item; |
| 3989 | if (oidset_contains(&shown, &commit->object.oid)) |
| 3990 | continue; |
| 3991 | entry = oidmap_get(&state.commit2label, &commit->object.oid); |
| 3992 | |
| 3993 | if (entry) |
| 3994 | fprintf(out, "\n# Branch %s\n", entry->string); |
| 3995 | else |
| 3996 | fprintf(out, "\n"); |
| 3997 | |
| 3998 | while (oidset_contains(&interesting, &commit->object.oid) && |
| 3999 | !oidset_contains(&shown, &commit->object.oid)) { |
| 4000 | commit_list_insert(commit, &list); |
| 4001 | if (!commit->parents) { |
| 4002 | commit = NULL; |
| 4003 | break; |
| 4004 | } |
| 4005 | commit = commit->parents->item; |
| 4006 | } |
| 4007 | |
| 4008 | if (!commit) |
Johannes Schindelin | 8fa6eea | 2018-05-04 01:01:29 +0200 | [diff] [blame] | 4009 | fprintf(out, "%s %s\n", cmd_reset, |
| 4010 | rebase_cousins ? "onto" : "[new root]"); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4011 | else { |
| 4012 | const char *to = NULL; |
| 4013 | |
| 4014 | entry = oidmap_get(&state.commit2label, |
| 4015 | &commit->object.oid); |
| 4016 | if (entry) |
| 4017 | to = entry->string; |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 4018 | else if (!rebase_cousins) |
| 4019 | to = label_oid(&commit->object.oid, NULL, |
| 4020 | &state); |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4021 | |
| 4022 | if (!to || !strcmp(to, "onto")) |
| 4023 | fprintf(out, "%s onto\n", cmd_reset); |
| 4024 | else { |
| 4025 | strbuf_reset(&oneline); |
| 4026 | pretty_print_commit(pp, commit, &oneline); |
| 4027 | fprintf(out, "%s %s # %s\n", |
| 4028 | cmd_reset, to, oneline.buf); |
| 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | for (iter2 = list; iter2; iter2 = iter2->next) { |
| 4033 | struct object_id *oid = &iter2->item->object.oid; |
| 4034 | entry = oidmap_get(&commit2todo, oid); |
| 4035 | /* only show if not already upstream */ |
| 4036 | if (entry) |
| 4037 | fprintf(out, "%s\n", entry->string); |
| 4038 | entry = oidmap_get(&state.commit2label, oid); |
| 4039 | if (entry) |
| 4040 | fprintf(out, "%s %s\n", |
| 4041 | cmd_label, entry->string); |
| 4042 | oidset_insert(&shown, oid); |
| 4043 | } |
| 4044 | |
| 4045 | free_commit_list(list); |
| 4046 | } |
| 4047 | |
| 4048 | free_commit_list(commits); |
| 4049 | free_commit_list(tips); |
| 4050 | |
| 4051 | strbuf_release(&label); |
| 4052 | strbuf_release(&oneline); |
| 4053 | strbuf_release(&buf); |
| 4054 | |
| 4055 | oidmap_free(&commit2todo, 1); |
| 4056 | oidmap_free(&state.commit2label, 1); |
| 4057 | hashmap_free(&state.labels, 1); |
| 4058 | strbuf_release(&state.buf); |
| 4059 | |
| 4060 | return 0; |
| 4061 | } |
| 4062 | |
Liam Beguin | 313a48e | 2017-12-05 12:52:32 -0500 | [diff] [blame] | 4063 | int sequencer_make_script(FILE *out, int argc, const char **argv, |
| 4064 | unsigned flags) |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4065 | { |
| 4066 | char *format = NULL; |
| 4067 | struct pretty_print_context pp = {0}; |
| 4068 | struct strbuf buf = STRBUF_INIT; |
| 4069 | struct rev_info revs; |
| 4070 | struct commit *commit; |
Liam Beguin | 313a48e | 2017-12-05 12:52:32 -0500 | [diff] [blame] | 4071 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 4072 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4073 | int rebase_merges = flags & TODO_LIST_REBASE_MERGES; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4074 | |
| 4075 | init_revisions(&revs, NULL); |
| 4076 | revs.verbose_header = 1; |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4077 | if (!rebase_merges) |
| 4078 | revs.max_parents = 1; |
Phillip Wood | 76ea235 | 2018-03-20 10:03:14 +0000 | [diff] [blame] | 4079 | revs.cherry_mark = 1; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4080 | revs.limited = 1; |
| 4081 | revs.reverse = 1; |
| 4082 | revs.right_only = 1; |
| 4083 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; |
| 4084 | revs.topo_order = 1; |
| 4085 | |
| 4086 | revs.pretty_given = 1; |
| 4087 | git_config_get_string("rebase.instructionFormat", &format); |
| 4088 | if (!format || !*format) { |
| 4089 | free(format); |
| 4090 | format = xstrdup("%s"); |
| 4091 | } |
| 4092 | get_commit_format(format, &revs); |
| 4093 | free(format); |
| 4094 | pp.fmt = revs.commit_format; |
| 4095 | pp.output_encoding = get_log_output_encoding(); |
| 4096 | |
| 4097 | if (setup_revisions(argc, argv, &revs, NULL) > 1) |
| 4098 | return error(_("make_script: unhandled options")); |
| 4099 | |
| 4100 | if (prepare_revision_walk(&revs) < 0) |
| 4101 | return error(_("make_script: error preparing revisions")); |
| 4102 | |
Johannes Schindelin | 1644c73 | 2018-04-25 14:29:03 +0200 | [diff] [blame] | 4103 | if (rebase_merges) |
| 4104 | return make_script_with_merges(&pp, &revs, out, flags); |
| 4105 | |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4106 | while ((commit = get_revision(&revs))) { |
Phillip Wood | 76ea235 | 2018-03-20 10:03:14 +0000 | [diff] [blame] | 4107 | int is_empty = is_original_commit_empty(commit); |
| 4108 | |
| 4109 | if (!is_empty && (commit->object.flags & PATCHSAME)) |
| 4110 | continue; |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4111 | strbuf_reset(&buf); |
Phillip Wood | 76ea235 | 2018-03-20 10:03:14 +0000 | [diff] [blame] | 4112 | if (!keep_empty && is_empty) |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4113 | strbuf_addf(&buf, "%c ", comment_line_char); |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 4114 | strbuf_addf(&buf, "%s %s ", insn, |
| 4115 | oid_to_hex(&commit->object.oid)); |
Johannes Schindelin | 62db524 | 2017-07-14 16:44:58 +0200 | [diff] [blame] | 4116 | pretty_print_commit(&pp, commit, &buf); |
| 4117 | strbuf_addch(&buf, '\n'); |
| 4118 | fputs(buf.buf, out); |
| 4119 | } |
| 4120 | strbuf_release(&buf); |
| 4121 | return 0; |
| 4122 | } |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4123 | |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4124 | /* |
| 4125 | * Add commands after pick and (series of) squash/fixup commands |
| 4126 | * in the todo list. |
| 4127 | */ |
| 4128 | int sequencer_add_exec_commands(const char *commands) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4129 | { |
| 4130 | const char *todo_file = rebase_path_todo(); |
| 4131 | struct todo_list todo_list = TODO_LIST_INIT; |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4132 | struct todo_item *item; |
| 4133 | struct strbuf *buf = &todo_list.buf; |
| 4134 | size_t offset = 0, commands_len = strlen(commands); |
| 4135 | int i, first; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4136 | |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4137 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4138 | return error(_("could not read '%s'."), todo_file); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4139 | |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4140 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4141 | todo_list_release(&todo_list); |
| 4142 | return error(_("unusable todo list: '%s'"), todo_file); |
| 4143 | } |
| 4144 | |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4145 | first = 1; |
| 4146 | /* insert <commands> before every pick except the first one */ |
| 4147 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { |
| 4148 | if (item->command == TODO_PICK && !first) { |
| 4149 | strbuf_insert(buf, item->offset_in_buf + offset, |
| 4150 | commands, commands_len); |
| 4151 | offset += commands_len; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4152 | } |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4153 | first = 0; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4154 | } |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4155 | |
| 4156 | /* append final <commands> */ |
| 4157 | strbuf_add(buf, commands, commands_len); |
| 4158 | |
| 4159 | i = write_message(buf->buf, buf->len, todo_file, 0); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4160 | todo_list_release(&todo_list); |
Liam Beguin | 0cce4a2 | 2017-12-05 12:52:33 -0500 | [diff] [blame] | 4161 | return i; |
| 4162 | } |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4163 | |
Liam Beguin | 313a48e | 2017-12-05 12:52:32 -0500 | [diff] [blame] | 4164 | int transform_todos(unsigned flags) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4165 | { |
| 4166 | const char *todo_file = rebase_path_todo(); |
| 4167 | struct todo_list todo_list = TODO_LIST_INIT; |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4168 | struct strbuf buf = STRBUF_INIT; |
| 4169 | struct todo_item *item; |
| 4170 | int i; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4171 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4172 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4173 | return error(_("could not read '%s'."), todo_file); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4174 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4175 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4176 | todo_list_release(&todo_list); |
| 4177 | return error(_("unusable todo list: '%s'"), todo_file); |
| 4178 | } |
| 4179 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4180 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { |
| 4181 | /* if the item is not a command write it and continue */ |
| 4182 | if (item->command >= TODO_COMMENT) { |
| 4183 | strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg); |
| 4184 | continue; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4185 | } |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4186 | |
| 4187 | /* add command to the buffer */ |
Liam Beguin | d8ae6c8 | 2017-12-05 12:52:34 -0500 | [diff] [blame] | 4188 | if (flags & TODO_LIST_ABBREVIATE_CMDS) |
| 4189 | strbuf_addch(&buf, command_to_char(item->command)); |
| 4190 | else |
| 4191 | strbuf_addstr(&buf, command_to_string(item->command)); |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4192 | |
| 4193 | /* add commit id */ |
| 4194 | if (item->commit) { |
Liam Beguin | 313a48e | 2017-12-05 12:52:32 -0500 | [diff] [blame] | 4195 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4196 | short_commit_name(item->commit) : |
| 4197 | oid_to_hex(&item->commit->object.oid); |
| 4198 | |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4199 | if (item->command == TODO_MERGE) { |
| 4200 | if (item->flags & TODO_EDIT_MERGE_MSG) |
| 4201 | strbuf_addstr(&buf, " -c"); |
| 4202 | else |
| 4203 | strbuf_addstr(&buf, " -C"); |
| 4204 | } |
| 4205 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4206 | strbuf_addf(&buf, " %s", oid); |
| 4207 | } |
Johannes Schindelin | 4c68e7d | 2018-04-25 14:28:54 +0200 | [diff] [blame] | 4208 | |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4209 | /* add all the rest */ |
Johannes Schindelin | c7b4d79 | 2017-12-23 00:56:00 +0100 | [diff] [blame] | 4210 | if (!item->arg_len) |
| 4211 | strbuf_addch(&buf, '\n'); |
| 4212 | else |
| 4213 | strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4214 | } |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4215 | |
| 4216 | i = write_message(buf.buf, buf.len, todo_file, 0); |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4217 | todo_list_release(&todo_list); |
Liam Beguin | 8dccc7a | 2017-12-05 12:52:30 -0500 | [diff] [blame] | 4218 | return i; |
Johannes Schindelin | 3546c8d | 2017-07-14 16:45:11 +0200 | [diff] [blame] | 4219 | } |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4220 | |
| 4221 | enum check_level { |
| 4222 | CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR |
| 4223 | }; |
| 4224 | |
| 4225 | static enum check_level get_missing_commit_check_level(void) |
| 4226 | { |
| 4227 | const char *value; |
| 4228 | |
| 4229 | if (git_config_get_value("rebase.missingcommitscheck", &value) || |
| 4230 | !strcasecmp("ignore", value)) |
| 4231 | return CHECK_IGNORE; |
| 4232 | if (!strcasecmp("warn", value)) |
| 4233 | return CHECK_WARN; |
| 4234 | if (!strcasecmp("error", value)) |
| 4235 | return CHECK_ERROR; |
Jean-Noel Avila | dfab1ea | 2017-10-08 14:18:39 +0200 | [diff] [blame] | 4236 | warning(_("unrecognized setting %s for option " |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4237 | "rebase.missingCommitsCheck. Ignoring."), value); |
| 4238 | return CHECK_IGNORE; |
| 4239 | } |
| 4240 | |
| 4241 | /* |
| 4242 | * Check if the user dropped some commits by mistake |
| 4243 | * Behaviour determined by rebase.missingCommitsCheck. |
| 4244 | * Check if there is an unrecognized command or a |
| 4245 | * bad SHA-1 in a command. |
| 4246 | */ |
| 4247 | int check_todo_list(void) |
| 4248 | { |
| 4249 | enum check_level check_level = get_missing_commit_check_level(); |
| 4250 | struct strbuf todo_file = STRBUF_INIT; |
| 4251 | struct todo_list todo_list = TODO_LIST_INIT; |
| 4252 | struct strbuf missing = STRBUF_INIT; |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4253 | int advise_to_edit_todo = 0, res = 0, i; |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4254 | |
| 4255 | strbuf_addstr(&todo_file, rebase_path_todo()); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4256 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) { |
| 4257 | res = -1; |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4258 | goto leave_check; |
| 4259 | } |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4260 | advise_to_edit_todo = res = |
| 4261 | parse_insn_buffer(todo_list.buf.buf, &todo_list); |
| 4262 | |
| 4263 | if (res || check_level == CHECK_IGNORE) |
| 4264 | goto leave_check; |
| 4265 | |
| 4266 | /* Mark the commits in git-rebase-todo as seen */ |
| 4267 | for (i = 0; i < todo_list.nr; i++) { |
| 4268 | struct commit *commit = todo_list.items[i].commit; |
| 4269 | if (commit) |
| 4270 | commit->util = (void *)1; |
| 4271 | } |
| 4272 | |
| 4273 | todo_list_release(&todo_list); |
| 4274 | strbuf_addstr(&todo_file, ".backup"); |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4275 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) { |
| 4276 | res = -1; |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4277 | goto leave_check; |
| 4278 | } |
Johannes Schindelin | 9439994 | 2017-07-14 16:45:21 +0200 | [diff] [blame] | 4279 | strbuf_release(&todo_file); |
| 4280 | res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list); |
| 4281 | |
| 4282 | /* Find commits in git-rebase-todo.backup yet unseen */ |
| 4283 | for (i = todo_list.nr - 1; i >= 0; i--) { |
| 4284 | struct todo_item *item = todo_list.items + i; |
| 4285 | struct commit *commit = item->commit; |
| 4286 | if (commit && !commit->util) { |
| 4287 | strbuf_addf(&missing, " - %s %.*s\n", |
| 4288 | short_commit_name(commit), |
| 4289 | item->arg_len, item->arg); |
| 4290 | commit->util = (void *)1; |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | /* Warn about missing commits */ |
| 4295 | if (!missing.len) |
| 4296 | goto leave_check; |
| 4297 | |
| 4298 | if (check_level == CHECK_ERROR) |
| 4299 | advise_to_edit_todo = res = 1; |
| 4300 | |
| 4301 | fprintf(stderr, |
| 4302 | _("Warning: some commits may have been dropped accidentally.\n" |
| 4303 | "Dropped commits (newer to older):\n")); |
| 4304 | |
| 4305 | /* Make the list user-friendly and display */ |
| 4306 | fputs(missing.buf, stderr); |
| 4307 | strbuf_release(&missing); |
| 4308 | |
| 4309 | fprintf(stderr, _("To avoid this message, use \"drop\" to " |
| 4310 | "explicitly remove a commit.\n\n" |
| 4311 | "Use 'git config rebase.missingCommitsCheck' to change " |
| 4312 | "the level of warnings.\n" |
| 4313 | "The possible behaviours are: ignore, warn, error.\n\n")); |
| 4314 | |
| 4315 | leave_check: |
| 4316 | strbuf_release(&todo_file); |
| 4317 | todo_list_release(&todo_list); |
| 4318 | |
| 4319 | if (advise_to_edit_todo) |
| 4320 | fprintf(stderr, |
| 4321 | _("You can fix this with 'git rebase --edit-todo' " |
| 4322 | "and then run 'git rebase --continue'.\n" |
| 4323 | "Or you can abort the rebase with 'git rebase" |
| 4324 | " --abort'.\n")); |
| 4325 | |
| 4326 | return res; |
| 4327 | } |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4328 | |
René Scharfe | 73646bf | 2017-10-31 10:54:21 +0100 | [diff] [blame] | 4329 | static int rewrite_file(const char *path, const char *buf, size_t len) |
| 4330 | { |
| 4331 | int rc = 0; |
René Scharfe | c8cee96 | 2017-10-31 10:58:16 +0100 | [diff] [blame] | 4332 | int fd = open(path, O_WRONLY | O_TRUNC); |
René Scharfe | 73646bf | 2017-10-31 10:54:21 +0100 | [diff] [blame] | 4333 | if (fd < 0) |
| 4334 | return error_errno(_("could not open '%s' for writing"), path); |
| 4335 | if (write_in_full(fd, buf, len) < 0) |
| 4336 | rc = error_errno(_("could not write to '%s'"), path); |
Simon Ruderich | 9360ec0 | 2017-11-01 15:45:42 +0100 | [diff] [blame] | 4337 | if (close(fd) && !rc) |
| 4338 | rc = error_errno(_("could not close '%s'"), path); |
René Scharfe | 73646bf | 2017-10-31 10:54:21 +0100 | [diff] [blame] | 4339 | return rc; |
| 4340 | } |
| 4341 | |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4342 | /* skip picking commits whose parents are unchanged */ |
| 4343 | int skip_unnecessary_picks(void) |
| 4344 | { |
| 4345 | const char *todo_file = rebase_path_todo(); |
| 4346 | struct strbuf buf = STRBUF_INIT; |
| 4347 | struct todo_list todo_list = TODO_LIST_INIT; |
| 4348 | struct object_id onto_oid, *oid = &onto_oid, *parent_oid; |
| 4349 | int fd, i; |
| 4350 | |
| 4351 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) |
| 4352 | return error(_("could not read 'onto'")); |
| 4353 | if (get_oid(buf.buf, &onto_oid)) { |
| 4354 | strbuf_release(&buf); |
| 4355 | return error(_("need a HEAD to fixup")); |
| 4356 | } |
| 4357 | strbuf_release(&buf); |
| 4358 | |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4359 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0) |
| 4360 | return -1; |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4361 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { |
| 4362 | todo_list_release(&todo_list); |
| 4363 | return -1; |
| 4364 | } |
| 4365 | |
| 4366 | for (i = 0; i < todo_list.nr; i++) { |
| 4367 | struct todo_item *item = todo_list.items + i; |
| 4368 | |
| 4369 | if (item->command >= TODO_NOOP) |
| 4370 | continue; |
| 4371 | if (item->command != TODO_PICK) |
| 4372 | break; |
| 4373 | if (parse_commit(item->commit)) { |
| 4374 | todo_list_release(&todo_list); |
| 4375 | return error(_("could not parse commit '%s'"), |
| 4376 | oid_to_hex(&item->commit->object.oid)); |
| 4377 | } |
| 4378 | if (!item->commit->parents) |
| 4379 | break; /* root commit */ |
| 4380 | if (item->commit->parents->next) |
| 4381 | break; /* merge commit */ |
| 4382 | parent_oid = &item->commit->parents->item->object.oid; |
| 4383 | if (hashcmp(parent_oid->hash, oid->hash)) |
| 4384 | break; |
| 4385 | oid = &item->commit->object.oid; |
| 4386 | } |
| 4387 | if (i > 0) { |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 4388 | int offset = get_item_line_offset(&todo_list, i); |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4389 | const char *done_path = rebase_path_done(); |
| 4390 | |
| 4391 | fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); |
| 4392 | if (fd < 0) { |
| 4393 | error_errno(_("could not open '%s' for writing"), |
| 4394 | done_path); |
| 4395 | todo_list_release(&todo_list); |
| 4396 | return -1; |
| 4397 | } |
| 4398 | if (write_in_full(fd, todo_list.buf.buf, offset) < 0) { |
| 4399 | error_errno(_("could not write to '%s'"), done_path); |
| 4400 | todo_list_release(&todo_list); |
| 4401 | close(fd); |
| 4402 | return -1; |
| 4403 | } |
| 4404 | close(fd); |
| 4405 | |
René Scharfe | 73646bf | 2017-10-31 10:54:21 +0100 | [diff] [blame] | 4406 | if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset, |
| 4407 | todo_list.buf.len - offset) < 0) { |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4408 | todo_list_release(&todo_list); |
| 4409 | return -1; |
| 4410 | } |
Johannes Schindelin | cdac2b0 | 2017-07-14 16:45:25 +0200 | [diff] [blame] | 4411 | |
| 4412 | todo_list.current = i; |
| 4413 | if (is_fixup(peek_command(&todo_list, 0))) |
| 4414 | record_in_rewritten(oid, peek_command(&todo_list, 0)); |
| 4415 | } |
| 4416 | |
| 4417 | todo_list_release(&todo_list); |
| 4418 | printf("%s\n", oid_to_hex(oid)); |
| 4419 | |
| 4420 | return 0; |
| 4421 | } |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4422 | |
| 4423 | struct subject2item_entry { |
| 4424 | struct hashmap_entry entry; |
| 4425 | int i; |
| 4426 | char subject[FLEX_ARRAY]; |
| 4427 | }; |
| 4428 | |
| 4429 | static int subject2item_cmp(const void *fndata, |
| 4430 | const struct subject2item_entry *a, |
| 4431 | const struct subject2item_entry *b, const void *key) |
| 4432 | { |
| 4433 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); |
| 4434 | } |
| 4435 | |
| 4436 | /* |
| 4437 | * Rearrange the todo list that has both "pick commit-id msg" and "pick |
| 4438 | * commit-id fixup!/squash! msg" in it so that the latter is put immediately |
| 4439 | * after the former, and change "pick" to "fixup"/"squash". |
| 4440 | * |
| 4441 | * Note that if the config has specified a custom instruction format, each log |
| 4442 | * message will have to be retrieved from the commit (as the oneline in the |
| 4443 | * script cannot be trusted) in order to normalize the autosquash arrangement. |
| 4444 | */ |
| 4445 | int rearrange_squash(void) |
| 4446 | { |
| 4447 | const char *todo_file = rebase_path_todo(); |
| 4448 | struct todo_list todo_list = TODO_LIST_INIT; |
| 4449 | struct hashmap subject2item; |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4450 | int res = 0, rearranged = 0, *next, *tail, i; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4451 | char **subjects; |
| 4452 | |
René Scharfe | 8780560 | 2018-02-22 20:29:25 +0100 | [diff] [blame] | 4453 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0) |
| 4454 | return -1; |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4455 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { |
| 4456 | todo_list_release(&todo_list); |
| 4457 | return -1; |
| 4458 | } |
| 4459 | |
| 4460 | /* |
| 4461 | * The hashmap maps onelines to the respective todo list index. |
| 4462 | * |
| 4463 | * If any items need to be rearranged, the next[i] value will indicate |
| 4464 | * which item was moved directly after the i'th. |
| 4465 | * |
| 4466 | * In that case, last[i] will indicate the index of the latest item to |
| 4467 | * be moved to appear after the i'th. |
| 4468 | */ |
| 4469 | hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp, |
| 4470 | NULL, todo_list.nr); |
| 4471 | ALLOC_ARRAY(next, todo_list.nr); |
| 4472 | ALLOC_ARRAY(tail, todo_list.nr); |
| 4473 | ALLOC_ARRAY(subjects, todo_list.nr); |
| 4474 | for (i = 0; i < todo_list.nr; i++) { |
| 4475 | struct strbuf buf = STRBUF_INIT; |
| 4476 | struct todo_item *item = todo_list.items + i; |
| 4477 | const char *commit_buffer, *subject, *p; |
| 4478 | size_t subject_len; |
| 4479 | int i2 = -1; |
| 4480 | struct subject2item_entry *entry; |
| 4481 | |
| 4482 | next[i] = tail[i] = -1; |
Johannes Schindelin | 2f6b1d1 | 2018-04-25 14:28:25 +0200 | [diff] [blame] | 4483 | if (!item->commit || item->command == TODO_DROP) { |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4484 | subjects[i] = NULL; |
| 4485 | continue; |
| 4486 | } |
| 4487 | |
| 4488 | if (is_fixup(item->command)) { |
| 4489 | todo_list_release(&todo_list); |
| 4490 | return error(_("the script was already rearranged.")); |
| 4491 | } |
| 4492 | |
| 4493 | item->commit->util = item; |
| 4494 | |
| 4495 | parse_commit(item->commit); |
| 4496 | commit_buffer = get_commit_buffer(item->commit, NULL); |
| 4497 | find_commit_subject(commit_buffer, &subject); |
| 4498 | format_subject(&buf, subject, " "); |
| 4499 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); |
| 4500 | unuse_commit_buffer(item->commit, commit_buffer); |
| 4501 | if ((skip_prefix(subject, "fixup! ", &p) || |
| 4502 | skip_prefix(subject, "squash! ", &p))) { |
| 4503 | struct commit *commit2; |
| 4504 | |
| 4505 | for (;;) { |
| 4506 | while (isspace(*p)) |
| 4507 | p++; |
| 4508 | if (!skip_prefix(p, "fixup! ", &p) && |
| 4509 | !skip_prefix(p, "squash! ", &p)) |
| 4510 | break; |
| 4511 | } |
| 4512 | |
| 4513 | if ((entry = hashmap_get_from_hash(&subject2item, |
| 4514 | strhash(p), p))) |
| 4515 | /* found by title */ |
| 4516 | i2 = entry->i; |
| 4517 | else if (!strchr(p, ' ') && |
| 4518 | (commit2 = |
| 4519 | lookup_commit_reference_by_name(p)) && |
| 4520 | commit2->util) |
| 4521 | /* found by commit name */ |
| 4522 | i2 = (struct todo_item *)commit2->util |
| 4523 | - todo_list.items; |
| 4524 | else { |
| 4525 | /* copy can be a prefix of the commit subject */ |
| 4526 | for (i2 = 0; i2 < i; i2++) |
| 4527 | if (subjects[i2] && |
| 4528 | starts_with(subjects[i2], p)) |
| 4529 | break; |
| 4530 | if (i2 == i) |
| 4531 | i2 = -1; |
| 4532 | } |
| 4533 | } |
| 4534 | if (i2 >= 0) { |
| 4535 | rearranged = 1; |
| 4536 | todo_list.items[i].command = |
| 4537 | starts_with(subject, "fixup!") ? |
| 4538 | TODO_FIXUP : TODO_SQUASH; |
| 4539 | if (next[i2] < 0) |
| 4540 | next[i2] = i; |
| 4541 | else |
| 4542 | next[tail[i2]] = i; |
| 4543 | tail[i2] = i; |
| 4544 | } else if (!hashmap_get_from_hash(&subject2item, |
| 4545 | strhash(subject), subject)) { |
| 4546 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); |
| 4547 | entry->i = i; |
| 4548 | hashmap_entry_init(entry, strhash(entry->subject)); |
| 4549 | hashmap_put(&subject2item, entry); |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | if (rearranged) { |
| 4554 | struct strbuf buf = STRBUF_INIT; |
| 4555 | |
| 4556 | for (i = 0; i < todo_list.nr; i++) { |
| 4557 | enum todo_command command = todo_list.items[i].command; |
| 4558 | int cur = i; |
| 4559 | |
| 4560 | /* |
| 4561 | * Initially, all commands are 'pick's. If it is a |
| 4562 | * fixup or a squash now, we have rearranged it. |
| 4563 | */ |
| 4564 | if (is_fixup(command)) |
| 4565 | continue; |
| 4566 | |
| 4567 | while (cur >= 0) { |
Johannes Schindelin | a01c2a5 | 2018-04-25 14:28:29 +0200 | [diff] [blame] | 4568 | const char *bol = |
| 4569 | get_item_line(&todo_list, cur); |
| 4570 | const char *eol = |
| 4571 | get_item_line(&todo_list, cur + 1); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4572 | |
| 4573 | /* replace 'pick', by 'fixup' or 'squash' */ |
| 4574 | command = todo_list.items[cur].command; |
| 4575 | if (is_fixup(command)) { |
| 4576 | strbuf_addstr(&buf, |
| 4577 | todo_command_info[command].str); |
| 4578 | bol += strcspn(bol, " \t"); |
| 4579 | } |
| 4580 | |
| 4581 | strbuf_add(&buf, bol, eol - bol); |
| 4582 | |
| 4583 | cur = next[cur]; |
| 4584 | } |
| 4585 | } |
| 4586 | |
René Scharfe | 73646bf | 2017-10-31 10:54:21 +0100 | [diff] [blame] | 4587 | res = rewrite_file(todo_file, buf.buf, buf.len); |
Johannes Schindelin | c44a4c6 | 2017-07-14 16:45:31 +0200 | [diff] [blame] | 4588 | strbuf_release(&buf); |
| 4589 | } |
| 4590 | |
| 4591 | free(next); |
| 4592 | free(tail); |
| 4593 | for (i = 0; i < todo_list.nr; i++) |
| 4594 | free(subjects[i]); |
| 4595 | free(subjects); |
| 4596 | hashmap_free(&subject2item, 1); |
| 4597 | todo_list_release(&todo_list); |
| 4598 | |
| 4599 | return res; |
| 4600 | } |