| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 1 | #include "cache.h" |
| Michael Haggerty | 697cc8e | 2014-10-01 10:28:42 | [diff] [blame] | 2 | #include "lockfile.h" |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 3 | #include "sequencer.h" |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 4 | #include "dir.h" |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 5 | #include "object.h" |
| 6 | #include "commit.h" |
| 7 | #include "tag.h" |
| 8 | #include "run-command.h" |
| 9 | #include "exec_cmd.h" |
| 10 | #include "utf8.h" |
| 11 | #include "cache-tree.h" |
| 12 | #include "diff.h" |
| 13 | #include "revision.h" |
| 14 | #include "rerere.h" |
| 15 | #include "merge-recursive.h" |
| 16 | #include "refs.h" |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 17 | #include "argv-array.h" |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 18 | #include "quote.h" |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 19 | |
| 20 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 21 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 22 | const char sign_off_header[] = "Signed-off-by: "; |
| Brandon Casey | cd650a4 | 2013-02-12 10:17:32 | [diff] [blame] | 23 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 24 | |
| Johannes Schindelin | 8a2a0f5 | 2016-10-14 13:17:12 | [diff] [blame] | 25 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
| 26 | |
| 27 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") |
| 28 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") |
| 29 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 30 | |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 31 | /* |
| 32 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and |
| 33 | * GIT_AUTHOR_DATE that will be used for the commit that is currently |
| 34 | * being rebased. |
| 35 | */ |
| 36 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 37 | /* |
| 38 | * The following files are written by git-rebase just after parsing the |
| 39 | * command-line (and are only consumed, not modified, by the sequencer). |
| 40 | */ |
| 41 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 42 | |
| 43 | /* We will introduce the 'interactive rebase' mode later */ |
| 44 | static inline int is_rebase_i(const struct replay_opts *opts) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| Johannes Schindelin | 285abf5 | 2016-10-14 13:17:20 | [diff] [blame] | 49 | static const char *get_dir(const struct replay_opts *opts) |
| 50 | { |
| 51 | return git_path_seq_dir(); |
| 52 | } |
| 53 | |
| Johannes Schindelin | c024650 | 2016-10-21 12:24:32 | [diff] [blame] | 54 | static const char *get_todo_path(const struct replay_opts *opts) |
| 55 | { |
| 56 | return git_path_todo_file(); |
| 57 | } |
| 58 | |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 59 | static int is_rfc2822_line(const char *buf, int len) |
| 60 | { |
| 61 | int i; |
| 62 | |
| 63 | for (i = 0; i < len; i++) { |
| 64 | int ch = buf[i]; |
| 65 | if (ch == ':') |
| 66 | return 1; |
| 67 | if (!isalnum(ch) && ch != '-') |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int is_cherry_picked_from_line(const char *buf, int len) |
| 75 | { |
| 76 | /* |
| 77 | * We only care that it looks roughly like (cherry picked from ...) |
| 78 | */ |
| 79 | return len > strlen(cherry_picked_prefix) + 1 && |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 80 | starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')'; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 81 | } |
| 82 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 83 | /* |
| 84 | * Returns 0 for non-conforming footer |
| 85 | * Returns 1 for conforming footer |
| 86 | * Returns 2 when sob exists within conforming footer |
| 87 | * Returns 3 when sob exists within conforming footer as last entry |
| 88 | */ |
| 89 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
| 90 | int ignore_footer) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 91 | { |
| 92 | char prev; |
| 93 | int i, k; |
| 94 | int len = sb->len - ignore_footer; |
| 95 | const char *buf = sb->buf; |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 96 | int found_sob = 0; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 97 | |
| 98 | /* footer must end with newline */ |
| 99 | if (!len || buf[len - 1] != '\n') |
| 100 | return 0; |
| 101 | |
| 102 | prev = '\0'; |
| 103 | for (i = len - 1; i > 0; i--) { |
| 104 | char ch = buf[i]; |
| 105 | if (prev == '\n' && ch == '\n') /* paragraph break */ |
| 106 | break; |
| 107 | prev = ch; |
| 108 | } |
| 109 | |
| 110 | /* require at least one blank line */ |
| 111 | if (prev != '\n' || buf[i] != '\n') |
| 112 | return 0; |
| 113 | |
| 114 | /* advance to start of last paragraph */ |
| 115 | while (i < len - 1 && buf[i] == '\n') |
| 116 | i++; |
| 117 | |
| 118 | for (; i < len; i = k) { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 119 | int found_rfc2822; |
| 120 | |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 121 | for (k = i; k < len && buf[k] != '\n'; k++) |
| 122 | ; /* do nothing */ |
| 123 | k++; |
| 124 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 125 | found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1); |
| 126 | if (found_rfc2822 && sob && |
| 127 | !strncmp(buf + i, sob->buf, sob->len)) |
| 128 | found_sob = k; |
| 129 | |
| 130 | if (!(found_rfc2822 || |
| 131 | is_cherry_picked_from_line(buf + i, k - i - 1))) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 134 | if (found_sob == i) |
| 135 | return 3; |
| 136 | if (found_sob) |
| 137 | return 2; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 140 | |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 141 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
| 142 | { |
| 143 | static struct strbuf buf = STRBUF_INIT; |
| 144 | |
| 145 | strbuf_reset(&buf); |
| 146 | if (opts->gpg_sign) |
| 147 | sq_quotef(&buf, "-S%s", opts->gpg_sign); |
| 148 | return buf.buf; |
| 149 | } |
| 150 | |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 151 | int sequencer_remove_state(struct replay_opts *opts) |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 152 | { |
| Johannes Schindelin | 285abf5 | 2016-10-14 13:17:20 | [diff] [blame] | 153 | struct strbuf dir = STRBUF_INIT; |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 154 | int i; |
| 155 | |
| 156 | free(opts->gpg_sign); |
| 157 | free(opts->strategy); |
| 158 | for (i = 0; i < opts->xopts_nr; i++) |
| 159 | free(opts->xopts[i]); |
| 160 | free(opts->xopts); |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 161 | |
| Johannes Schindelin | 285abf5 | 2016-10-14 13:17:20 | [diff] [blame] | 162 | strbuf_addf(&dir, "%s", get_dir(opts)); |
| 163 | remove_dir_recursively(&dir, 0); |
| 164 | strbuf_release(&dir); |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 165 | |
| 166 | return 0; |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 167 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 168 | |
| 169 | static const char *action_name(const struct replay_opts *opts) |
| 170 | { |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 171 | return opts->action == REPLAY_REVERT ? N_("revert") : N_("cherry-pick"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 172 | } |
| 173 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 174 | struct commit_message { |
| 175 | char *parent_label; |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 176 | char *label; |
| 177 | char *subject; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 178 | const char *message; |
| 179 | }; |
| 180 | |
| Johannes Schindelin | 3975596 | 2016-10-21 12:24:37 | [diff] [blame] | 181 | static const char *short_commit_name(struct commit *commit) |
| 182 | { |
| 183 | return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); |
| 184 | } |
| 185 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 186 | static int get_message(struct commit *commit, struct commit_message *out) |
| 187 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 188 | const char *abbrev, *subject; |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 189 | int subject_len; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 190 | |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 191 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
| Johannes Schindelin | 3975596 | 2016-10-21 12:24:37 | [diff] [blame] | 192 | abbrev = short_commit_name(commit); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 193 | |
| 194 | subject_len = find_commit_subject(out->message, &subject); |
| 195 | |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 196 | out->subject = xmemdupz(subject, subject_len); |
| 197 | out->label = xstrfmt("%s... %s", abbrev, out->subject); |
| 198 | out->parent_label = xstrfmt("parent of %s", out->label); |
| 199 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 203 | static void free_message(struct commit *commit, struct commit_message *msg) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 204 | { |
| 205 | free(msg->parent_label); |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 206 | free(msg->label); |
| 207 | free(msg->subject); |
| Jeff King | b66103c | 2014-06-10 21:41:39 | [diff] [blame] | 208 | unuse_commit_buffer(commit, msg->message); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 209 | } |
| 210 | |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 211 | static void print_advice(int show_hint, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 212 | { |
| 213 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 214 | |
| 215 | if (msg) { |
| 216 | fprintf(stderr, "%s\n", msg); |
| 217 | /* |
| Stefano Lattarini | 41ccfdd | 2013-04-11 22:36:10 | [diff] [blame] | 218 | * A conflict has occurred but the porcelain |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 219 | * (typically rebase --interactive) wants to take care |
| 220 | * of the commit itself so remove CHERRY_PICK_HEAD |
| 221 | */ |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 222 | unlink(git_path_cherry_pick_head()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 226 | if (show_hint) { |
| 227 | if (opts->no_commit) |
| 228 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 229 | "with 'git add <paths>' or 'git rm <paths>'")); |
| 230 | else |
| 231 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 232 | "with 'git add <paths>' or 'git rm <paths>'\n" |
| 233 | "and commit the result with 'git commit'")); |
| 234 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 235 | } |
| 236 | |
| Johannes Schindelin | f56fffe | 2016-10-21 12:26:09 | [diff] [blame] | 237 | static int write_message(const void *buf, size_t len, const char *filename, |
| 238 | int append_eol) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 239 | { |
| 240 | static struct lock_file msg_file; |
| 241 | |
| Johannes Schindelin | 4ef3d8f | 2016-09-09 14:37:05 | [diff] [blame] | 242 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
| 243 | if (msg_fd < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 244 | return error_errno(_("could not lock '%s'"), filename); |
| Johannes Schindelin | 7587149 | 2016-10-21 12:26:05 | [diff] [blame] | 245 | if (write_in_full(msg_fd, buf, len) < 0) { |
| Johannes Schindelin | 4f66c83 | 2016-10-21 12:26:00 | [diff] [blame] | 246 | rollback_lock_file(&msg_file); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 247 | return error_errno(_("could not write to '%s'"), filename); |
| Johannes Schindelin | 4f66c83 | 2016-10-21 12:26:00 | [diff] [blame] | 248 | } |
| Johannes Schindelin | f56fffe | 2016-10-21 12:26:09 | [diff] [blame] | 249 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
| 250 | rollback_lock_file(&msg_file); |
| Jiang Xin | 3587180 | 2016-11-20 12:26:17 | [diff] [blame] | 251 | return error_errno(_("could not write eol to '%s'"), filename); |
| Johannes Schindelin | f56fffe | 2016-10-21 12:26:09 | [diff] [blame] | 252 | } |
| Johannes Schindelin | 4f66c83 | 2016-10-21 12:26:00 | [diff] [blame] | 253 | if (commit_lock_file(&msg_file) < 0) { |
| 254 | rollback_lock_file(&msg_file); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 255 | return error(_("failed to finalize '%s'."), filename); |
| Johannes Schindelin | 4f66c83 | 2016-10-21 12:26:00 | [diff] [blame] | 256 | } |
| Johannes Schindelin | 4ef3d8f | 2016-09-09 14:37:05 | [diff] [blame] | 257 | |
| 258 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 259 | } |
| 260 | |
| Johannes Schindelin | 1dfc84e | 2016-10-21 12:25:08 | [diff] [blame] | 261 | /* |
| 262 | * Reads a file that was presumably written by a shell script, i.e. with an |
| 263 | * end-of-line marker that needs to be stripped. |
| 264 | * |
| 265 | * Note that only the last end-of-line marker is stripped, consistent with the |
| 266 | * behavior of "$(cat path)" in a shell script. |
| 267 | * |
| 268 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. |
| 269 | */ |
| 270 | static int read_oneliner(struct strbuf *buf, |
| 271 | const char *path, int skip_if_empty) |
| 272 | { |
| 273 | int orig_len = buf->len; |
| 274 | |
| 275 | if (!file_exists(path)) |
| 276 | return 0; |
| 277 | |
| 278 | if (strbuf_read_file(buf, path, 0) < 0) { |
| 279 | warning_errno(_("could not read '%s'"), path); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { |
| 284 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') |
| 285 | --buf->len; |
| 286 | buf->buf[buf->len] = '\0'; |
| 287 | } |
| 288 | |
| 289 | if (skip_if_empty && buf->len == orig_len) |
| 290 | return 0; |
| 291 | |
| 292 | return 1; |
| 293 | } |
| 294 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 295 | static struct tree *empty_tree(void) |
| 296 | { |
| Jeff King | cba595b | 2012-03-22 18:53:24 | [diff] [blame] | 297 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static int error_dirty_index(struct replay_opts *opts) |
| 301 | { |
| 302 | if (read_cache_unmerged()) |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 303 | return error_resolve_conflict(_(action_name(opts))); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 304 | |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 305 | error(_("your local changes would be overwritten by %s."), |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 306 | _(action_name(opts))); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 307 | |
| 308 | if (advice_commit_before_merge) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 309 | advise(_("commit your changes or stash them to proceed.")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 310 | return -1; |
| 311 | } |
| 312 | |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 313 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 314 | int unborn, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 315 | { |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 316 | struct ref_transaction *transaction; |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 317 | struct strbuf sb = STRBUF_INIT; |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 318 | struct strbuf err = STRBUF_INIT; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 319 | |
| 320 | read_cache(); |
| Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 15:53:49 | [diff] [blame] | 321 | if (checkout_fast_forward(from, to, 1)) |
| Johannes Schindelin | 0e408fc | 2016-09-09 14:37:55 | [diff] [blame] | 322 | return -1; /* the callee should have complained already */ |
| Ronnie Sahlberg | 651ab9f | 2014-04-16 18:56:52 | [diff] [blame] | 323 | |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 324 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 325 | |
| 326 | transaction = ref_transaction_begin(&err); |
| 327 | if (!transaction || |
| 328 | ref_transaction_update(transaction, "HEAD", |
| 329 | to, unborn ? null_sha1 : from, |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 330 | 0, sb.buf, &err) || |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 331 | ref_transaction_commit(transaction, &err)) { |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 332 | ref_transaction_free(transaction); |
| 333 | error("%s", err.buf); |
| 334 | strbuf_release(&sb); |
| 335 | strbuf_release(&err); |
| 336 | return -1; |
| 337 | } |
| Ronnie Sahlberg | 651ab9f | 2014-04-16 18:56:52 | [diff] [blame] | 338 | |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 339 | strbuf_release(&sb); |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 340 | strbuf_release(&err); |
| 341 | ref_transaction_free(transaction); |
| 342 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 343 | } |
| 344 | |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 345 | void append_conflicts_hint(struct strbuf *msgbuf) |
| 346 | { |
| 347 | int i; |
| 348 | |
| Junio C Hamano | 261f315 | 2014-10-28 20:04:38 | [diff] [blame] | 349 | strbuf_addch(msgbuf, '\n'); |
| 350 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 351 | for (i = 0; i < active_nr;) { |
| 352 | const struct cache_entry *ce = active_cache[i++]; |
| 353 | if (ce_stage(ce)) { |
| Junio C Hamano | 261f315 | 2014-10-28 20:04:38 | [diff] [blame] | 354 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 355 | while (i < active_nr && !strcmp(ce->name, |
| 356 | active_cache[i]->name)) |
| 357 | i++; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 362 | static int do_recursive_merge(struct commit *base, struct commit *next, |
| 363 | const char *base_label, const char *next_label, |
| 364 | unsigned char *head, struct strbuf *msgbuf, |
| 365 | struct replay_opts *opts) |
| 366 | { |
| 367 | struct merge_options o; |
| 368 | struct tree *result, *next_tree, *base_tree, *head_tree; |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 369 | int clean; |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 370 | char **xopt; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 371 | static struct lock_file index_lock; |
| 372 | |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 373 | hold_locked_index(&index_lock, 1); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 374 | |
| 375 | read_cache(); |
| 376 | |
| 377 | init_merge_options(&o); |
| 378 | o.ancestor = base ? base_label : "(empty tree)"; |
| 379 | o.branch1 = "HEAD"; |
| 380 | o.branch2 = next ? next_label : "(empty tree)"; |
| 381 | |
| 382 | head_tree = parse_tree_indirect(head); |
| 383 | next_tree = next ? next->tree : empty_tree(); |
| 384 | base_tree = base ? base->tree : empty_tree(); |
| 385 | |
| 386 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
| 387 | parse_merge_opt(&o, *xopt); |
| 388 | |
| 389 | clean = merge_trees(&o, |
| 390 | head_tree, |
| 391 | next_tree, base_tree, &result); |
| Johannes Schindelin | 548009c | 2016-08-01 11:44:53 | [diff] [blame] | 392 | strbuf_release(&o.obuf); |
| Johannes Schindelin | f241ff0 | 2016-07-26 16:06:02 | [diff] [blame] | 393 | if (clean < 0) |
| 394 | return clean; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 395 | |
| 396 | if (active_cache_changed && |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 397 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 398 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
| Johannes Schindelin | c527b55 | 2016-09-09 14:37:10 | [diff] [blame] | 399 | return error(_("%s: Unable to write new index file"), |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 400 | _(action_name(opts))); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 401 | rollback_lock_file(&index_lock); |
| 402 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 403 | if (opts->signoff) |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 404 | append_signoff(msgbuf, 0, 0); |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 405 | |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 406 | if (!clean) |
| 407 | append_conflicts_hint(msgbuf); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 408 | |
| 409 | return !clean; |
| 410 | } |
| 411 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 412 | static int is_index_unchanged(void) |
| 413 | { |
| 414 | unsigned char head_sha1[20]; |
| 415 | struct commit *head_commit; |
| 416 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 417 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 418 | return error(_("could not resolve HEAD commit\n")); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 419 | |
| 420 | head_commit = lookup_commit(head_sha1); |
| Neil Horman | 4b58006 | 2012-05-03 12:10:22 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * If head_commit is NULL, check_commit, called from |
| 424 | * lookup_commit, would have indicated that head_commit is not |
| 425 | * a commit object already. parse_commit() will return failure |
| 426 | * without further complaints in such a case. Otherwise, if |
| 427 | * the commit is invalid, parse_commit() will complain. So |
| 428 | * there is nothing for us to say here. Just return failure. |
| 429 | */ |
| 430 | if (parse_commit(head_commit)) |
| 431 | return -1; |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 432 | |
| 433 | if (!active_cache_tree) |
| 434 | active_cache_tree = cache_tree(); |
| 435 | |
| 436 | if (!cache_tree_fully_valid(active_cache_tree)) |
| Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 12:19:32 | [diff] [blame] | 437 | if (cache_tree_update(&the_index, 0)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 438 | return error(_("unable to update cache tree\n")); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 439 | |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 440 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 441 | } |
| 442 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 443 | /* |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 444 | * Read the author-script file into an environment block, ready for use in |
| 445 | * run_command(), that can be free()d afterwards. |
| 446 | */ |
| 447 | static char **read_author_script(void) |
| 448 | { |
| 449 | struct strbuf script = STRBUF_INIT; |
| 450 | int i, count = 0; |
| 451 | char *p, *p2, **env; |
| 452 | size_t env_size; |
| 453 | |
| 454 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) |
| 455 | return NULL; |
| 456 | |
| 457 | for (p = script.buf; *p; p++) |
| 458 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) |
| 459 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); |
| 460 | else if (*p == '\'') |
| 461 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); |
| 462 | else if (*p == '\n') { |
| 463 | *p = '\0'; |
| 464 | count++; |
| 465 | } |
| 466 | |
| 467 | env_size = (count + 1) * sizeof(*env); |
| 468 | strbuf_grow(&script, env_size); |
| 469 | memmove(script.buf + env_size, script.buf, script.len); |
| 470 | p = script.buf + env_size; |
| 471 | env = (char **)strbuf_detach(&script, NULL); |
| 472 | |
| 473 | for (i = 0; i < count; i++) { |
| 474 | env[i] = p; |
| 475 | p += strlen(p) + 1; |
| 476 | } |
| 477 | env[count] = NULL; |
| 478 | |
| 479 | return env; |
| 480 | } |
| 481 | |
| Johannes Schindelin | 791eb87 | 2016-10-21 12:26:32 | [diff] [blame] | 482 | static const char staged_changes_advice[] = |
| 483 | N_("you have staged changes in your working tree\n" |
| 484 | "If these changes are meant to be squashed into the previous commit, run:\n" |
| 485 | "\n" |
| 486 | " git commit --amend %s\n" |
| 487 | "\n" |
| 488 | "If they are meant to go into a new commit, run:\n" |
| 489 | "\n" |
| 490 | " git commit %s\n" |
| 491 | "\n" |
| 492 | "In both cases, once you're done, continue with:\n" |
| 493 | "\n" |
| 494 | " git rebase --continue\n"); |
| 495 | |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 496 | /* |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 497 | * If we are cherry-pick, and if the merge did not result in |
| 498 | * hand-editing, we will hit this commit and inherit the original |
| 499 | * author date and name. |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 500 | * |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 501 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 502 | * we had better say that the current user is responsible for that. |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 503 | * |
| 504 | * An exception is when run_git_commit() is called during an |
| 505 | * interactive rebase: in that case, we will want to retain the |
| 506 | * author metadata. |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 507 | */ |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 508 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
| Johannes Schindelin | 0009426 | 2016-10-21 12:25:28 | [diff] [blame] | 509 | int allow_empty, int edit, int amend, |
| 510 | int cleanup_commit_message) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 511 | { |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 512 | char **env = NULL; |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 513 | struct argv_array array; |
| 514 | int rc; |
| Michael J Gruber | 17d65f0 | 2015-03-06 13:55:32 | [diff] [blame] | 515 | const char *value; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 516 | |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 517 | if (is_rebase_i(opts)) { |
| 518 | env = read_author_script(); |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 519 | if (!env) { |
| 520 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
| 521 | |
| Johannes Schindelin | 791eb87 | 2016-10-21 12:26:32 | [diff] [blame] | 522 | return error(_(staged_changes_advice), |
| 523 | gpg_opt, gpg_opt); |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 524 | } |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 525 | } |
| 526 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 527 | argv_array_init(&array); |
| 528 | argv_array_push(&array, "commit"); |
| 529 | argv_array_push(&array, "-n"); |
| 530 | |
| Johannes Schindelin | 9240bed | 2016-10-21 12:25:17 | [diff] [blame] | 531 | if (amend) |
| 532 | argv_array_push(&array, "--amend"); |
| Jeff King | 3bdd552 | 2014-06-19 21:28:20 | [diff] [blame] | 533 | if (opts->gpg_sign) |
| 534 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 535 | if (opts->signoff) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 536 | argv_array_push(&array, "-s"); |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 537 | if (defmsg) |
| 538 | argv_array_pushl(&array, "-F", defmsg, NULL); |
| Johannes Schindelin | 0009426 | 2016-10-21 12:25:28 | [diff] [blame] | 539 | if (cleanup_commit_message) |
| 540 | argv_array_push(&array, "--cleanup=strip"); |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 541 | if (edit) |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 542 | argv_array_push(&array, "-e"); |
| Johannes Schindelin | 0009426 | 2016-10-21 12:25:28 | [diff] [blame] | 543 | else if (!cleanup_commit_message && |
| 544 | !opts->signoff && !opts->record_origin && |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 545 | git_config_get_value("commit.cleanup", &value)) |
| 546 | argv_array_push(&array, "--cleanup=verbatim"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 547 | |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 548 | if (allow_empty) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 549 | argv_array_push(&array, "--allow-empty"); |
| Neil Horman | df478b7 | 2012-04-11 20:21:53 | [diff] [blame] | 550 | |
| Chris Webb | 4bee958 | 2012-08-02 10:38:51 | [diff] [blame] | 551 | if (opts->allow_empty_message) |
| 552 | argv_array_push(&array, "--allow-empty-message"); |
| 553 | |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 554 | rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL, |
| 555 | (const char *const *)env); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 556 | argv_array_clear(&array); |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 557 | free(env); |
| 558 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 559 | return rc; |
| 560 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 561 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 562 | static int is_original_commit_empty(struct commit *commit) |
| 563 | { |
| 564 | const unsigned char *ptree_sha1; |
| 565 | |
| 566 | if (parse_commit(commit)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 567 | return error(_("could not parse commit %s\n"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 568 | oid_to_hex(&commit->object.oid)); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 569 | if (commit->parents) { |
| 570 | struct commit *parent = commit->parents->item; |
| 571 | if (parse_commit(parent)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 572 | return error(_("could not parse parent commit %s\n"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 573 | oid_to_hex(&parent->object.oid)); |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 574 | ptree_sha1 = parent->tree->object.oid.hash; |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 575 | } else { |
| 576 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ |
| 577 | } |
| 578 | |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 579 | return !hashcmp(ptree_sha1, commit->tree->object.oid.hash); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 580 | } |
| 581 | |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 582 | /* |
| 583 | * Do we run "git commit" with "--allow-empty"? |
| 584 | */ |
| 585 | static int allow_empty(struct replay_opts *opts, struct commit *commit) |
| 586 | { |
| 587 | int index_unchanged, empty_commit; |
| 588 | |
| 589 | /* |
| 590 | * Three cases: |
| 591 | * |
| 592 | * (1) we do not allow empty at all and error out. |
| 593 | * |
| 594 | * (2) we allow ones that were initially empty, but |
| 595 | * forbid the ones that become empty; |
| 596 | * |
| 597 | * (3) we allow both. |
| 598 | */ |
| 599 | if (!opts->allow_empty) |
| 600 | return 0; /* let "git commit" barf as necessary */ |
| 601 | |
| 602 | index_unchanged = is_index_unchanged(); |
| 603 | if (index_unchanged < 0) |
| 604 | return index_unchanged; |
| 605 | if (!index_unchanged) |
| 606 | return 0; /* we do not have to say --allow-empty */ |
| 607 | |
| 608 | if (opts->keep_redundant_commits) |
| 609 | return 1; |
| 610 | |
| 611 | empty_commit = is_original_commit_empty(commit); |
| 612 | if (empty_commit < 0) |
| 613 | return empty_commit; |
| 614 | if (!empty_commit) |
| 615 | return 0; |
| 616 | else |
| 617 | return 1; |
| 618 | } |
| 619 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 620 | enum todo_command { |
| 621 | TODO_PICK = 0, |
| 622 | TODO_REVERT |
| 623 | }; |
| 624 | |
| 625 | static const char *todo_command_strings[] = { |
| 626 | "pick", |
| 627 | "revert" |
| 628 | }; |
| 629 | |
| 630 | static const char *command_to_string(const enum todo_command command) |
| 631 | { |
| Jeff King | 2ae38f2 | 2016-11-09 03:57:28 | [diff] [blame] | 632 | if ((size_t)command < ARRAY_SIZE(todo_command_strings)) |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 633 | return todo_command_strings[command]; |
| 634 | die("Unknown command: %d", command); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | static int do_pick_commit(enum todo_command command, struct commit *commit, |
| 639 | struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 640 | { |
| 641 | unsigned char head[20]; |
| 642 | struct commit *base, *next, *parent; |
| 643 | const char *base_label, *next_label; |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 644 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 645 | struct strbuf msgbuf = STRBUF_INIT; |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 646 | int res, unborn = 0, allow; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 647 | |
| 648 | if (opts->no_commit) { |
| 649 | /* |
| 650 | * We do not intend to commit immediately. We just want to |
| 651 | * merge the differences in, so let's compute the tree |
| 652 | * that represents the "current" state for merge-recursive |
| 653 | * to work on. |
| 654 | */ |
| 655 | if (write_cache_as_tree(head, 0, NULL)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 656 | return error(_("your index file is unmerged.")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 657 | } else { |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 658 | unborn = get_sha1("HEAD", head); |
| 659 | if (unborn) |
| 660 | hashcpy(head, EMPTY_TREE_SHA1_BIN); |
| Nguyễn Thái Ngọc Duy | 018ec3c | 2016-10-24 10:42:21 | [diff] [blame] | 661 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0, 0)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 662 | return error_dirty_index(opts); |
| 663 | } |
| 664 | discard_cache(); |
| 665 | |
| 666 | if (!commit->parents) { |
| 667 | parent = NULL; |
| 668 | } |
| 669 | else if (commit->parents->next) { |
| 670 | /* Reverting or cherry-picking a merge commit */ |
| 671 | int cnt; |
| 672 | struct commit_list *p; |
| 673 | |
| 674 | if (!opts->mainline) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 675 | return error(_("commit %s is a merge but no -m option was given."), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 676 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 677 | |
| 678 | for (cnt = 1, p = commit->parents; |
| 679 | cnt != opts->mainline && p; |
| 680 | cnt++) |
| 681 | p = p->next; |
| 682 | if (cnt != opts->mainline || !p) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 683 | return error(_("commit %s does not have parent %d"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 684 | oid_to_hex(&commit->object.oid), opts->mainline); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 685 | parent = p->item; |
| 686 | } else if (0 < opts->mainline) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 687 | return error(_("mainline was specified but commit %s is not a merge."), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 688 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 689 | else |
| 690 | parent = commit->parents->item; |
| 691 | |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 692 | if (opts->allow_ff && |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 693 | ((parent && !hashcmp(parent->object.oid.hash, head)) || |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 694 | (!parent && unborn))) |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 695 | return fast_forward_to(commit->object.oid.hash, head, unborn, opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 696 | |
| 697 | if (parent && parse_commit(parent) < 0) |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 698 | /* TRANSLATORS: The first %s will be a "todo" command like |
| 699 | "revert" or "pick", the second %s a SHA1. */ |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 700 | return error(_("%s: cannot parse parent commit %s"), |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 701 | command_to_string(command), |
| 702 | oid_to_hex(&parent->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 703 | |
| 704 | if (get_message(commit, &msg) != 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 705 | return error(_("cannot get commit message for %s"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 706 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 707 | |
| 708 | /* |
| 709 | * "commit" is an existing commit. We would want to apply |
| 710 | * the difference it introduces since its first parent "prev" |
| 711 | * on top of the current HEAD if we are cherry-pick. Or the |
| 712 | * reverse of it if we are revert. |
| 713 | */ |
| 714 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 715 | if (command == TODO_REVERT) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 716 | base = commit; |
| 717 | base_label = msg.label; |
| 718 | next = parent; |
| 719 | next_label = msg.parent_label; |
| 720 | strbuf_addstr(&msgbuf, "Revert \""); |
| 721 | strbuf_addstr(&msgbuf, msg.subject); |
| 722 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 723 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 724 | |
| 725 | if (commit->parents && commit->parents->next) { |
| 726 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 727 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 728 | } |
| 729 | strbuf_addstr(&msgbuf, ".\n"); |
| 730 | } else { |
| 731 | const char *p; |
| 732 | |
| 733 | base = parent; |
| 734 | base_label = msg.parent_label; |
| 735 | next = commit; |
| 736 | next_label = msg.label; |
| 737 | |
| 738 | /* |
| 739 | * Append the commit log message to msgbuf; it starts |
| 740 | * after the tree, parent, author, committer |
| 741 | * information followed by "\n\n". |
| 742 | */ |
| 743 | p = strstr(msg.message, "\n\n"); |
| Johannes Schindelin | 88ef402 | 2016-06-29 14:14:46 | [diff] [blame] | 744 | if (p) |
| 745 | strbuf_addstr(&msgbuf, skip_blank_lines(p + 2)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 746 | |
| 747 | if (opts->record_origin) { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 748 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 749 | strbuf_addch(&msgbuf, '\n'); |
| Brandon Casey | cd650a4 | 2013-02-12 10:17:32 | [diff] [blame] | 750 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 751 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 752 | strbuf_addstr(&msgbuf, ")\n"); |
| 753 | } |
| 754 | } |
| 755 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 756 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 757 | res = do_recursive_merge(base, next, base_label, next_label, |
| 758 | head, &msgbuf, opts); |
| Johannes Schindelin | f241ff0 | 2016-07-26 16:06:02 | [diff] [blame] | 759 | if (res < 0) |
| 760 | return res; |
| Johannes Schindelin | 7587149 | 2016-10-21 12:26:05 | [diff] [blame] | 761 | res |= write_message(msgbuf.buf, msgbuf.len, |
| Johannes Schindelin | f56fffe | 2016-10-21 12:26:09 | [diff] [blame] | 762 | git_path_merge_msg(), 0); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 763 | } else { |
| 764 | struct commit_list *common = NULL; |
| 765 | struct commit_list *remotes = NULL; |
| 766 | |
| Johannes Schindelin | 7587149 | 2016-10-21 12:26:05 | [diff] [blame] | 767 | res = write_message(msgbuf.buf, msgbuf.len, |
| Johannes Schindelin | f56fffe | 2016-10-21 12:26:09 | [diff] [blame] | 768 | git_path_merge_msg(), 0); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 769 | |
| 770 | commit_list_insert(base, &common); |
| 771 | commit_list_insert(next, &remotes); |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 772 | res |= try_merge_command(opts->strategy, |
| 773 | opts->xopts_nr, (const char **)opts->xopts, |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 774 | common, sha1_to_hex(head), remotes); |
| 775 | free_commit_list(common); |
| 776 | free_commit_list(remotes); |
| 777 | } |
| Johannes Schindelin | 452202c | 2016-10-21 12:25:41 | [diff] [blame] | 778 | strbuf_release(&msgbuf); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 779 | |
| 780 | /* |
| 781 | * If the merge was clean or if it failed due to conflict, we write |
| 782 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
| 783 | * However, if the merge did not even start, then we don't want to |
| 784 | * write it at all. |
| 785 | */ |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 786 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
| Johannes Schindelin | dbfad03 | 2016-08-26 13:47:14 | [diff] [blame] | 787 | update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL, |
| 788 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) |
| 789 | res = -1; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 790 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
| Johannes Schindelin | dbfad03 | 2016-08-26 13:47:14 | [diff] [blame] | 791 | update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL, |
| 792 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) |
| 793 | res = -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 794 | |
| 795 | if (res) { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 796 | error(command == TODO_REVERT |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 797 | ? _("could not revert %s... %s") |
| 798 | : _("could not apply %s... %s"), |
| Johannes Schindelin | 3975596 | 2016-10-21 12:24:37 | [diff] [blame] | 799 | short_commit_name(commit), msg.subject); |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 800 | print_advice(res == 1, opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 801 | rerere(opts->allow_rerere_auto); |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 802 | goto leave; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 803 | } |
| 804 | |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 805 | allow = allow_empty(opts, commit); |
| Felipe Contreras | 706728a | 2013-06-06 08:58:57 | [diff] [blame] | 806 | if (allow < 0) { |
| 807 | res = allow; |
| 808 | goto leave; |
| 809 | } |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 810 | if (!opts->no_commit) |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 811 | res = run_git_commit(opts->edit ? NULL : git_path_merge_msg(), |
| Johannes Schindelin | 0009426 | 2016-10-21 12:25:28 | [diff] [blame] | 812 | opts, allow, opts->edit, 0, 0); |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 813 | |
| 814 | leave: |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 815 | free_message(commit, &msg); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 816 | |
| 817 | return res; |
| 818 | } |
| 819 | |
| Johannes Schindelin | c3e8618 | 2016-09-09 14:37:18 | [diff] [blame] | 820 | static int prepare_revs(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 821 | { |
| Martin von Zweigbergk | a73e22e | 2012-08-29 06:15:56 | [diff] [blame] | 822 | /* |
| 823 | * picking (but not reverting) ranges (but not individual revisions) |
| 824 | * should be done in reverse |
| 825 | */ |
| 826 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 827 | opts->revs->reverse ^= 1; |
| 828 | |
| 829 | if (prepare_revision_walk(opts->revs)) |
| Johannes Schindelin | c3e8618 | 2016-09-09 14:37:18 | [diff] [blame] | 830 | return error(_("revision walk setup failed")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 831 | |
| 832 | if (!opts->revs->commits) |
| Johannes Schindelin | c3e8618 | 2016-09-09 14:37:18 | [diff] [blame] | 833 | return error(_("empty commit set passed")); |
| 834 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 835 | } |
| 836 | |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 837 | static int read_and_refresh_cache(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 838 | { |
| 839 | static struct lock_file index_lock; |
| 840 | int index_fd = hold_locked_index(&index_lock, 0); |
| Johannes Schindelin | 49fb937 | 2016-09-09 14:38:20 | [diff] [blame] | 841 | if (read_index_preload(&the_index, NULL) < 0) { |
| 842 | rollback_lock_file(&index_lock); |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 843 | return error(_("git %s: failed to read the index"), |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 844 | _(action_name(opts))); |
| Johannes Schindelin | 49fb937 | 2016-09-09 14:38:20 | [diff] [blame] | 845 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 846 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
| Nguyễn Thái Ngọc Duy | 33c297a | 2014-04-28 10:55:24 | [diff] [blame] | 847 | if (the_index.cache_changed && index_fd >= 0) { |
| Johannes Schindelin | 49fb937 | 2016-09-09 14:38:20 | [diff] [blame] | 848 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { |
| 849 | rollback_lock_file(&index_lock); |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 850 | return error(_("git %s: failed to refresh the index"), |
| Johannes Schindelin | c28cbc5 | 2016-10-21 12:26:17 | [diff] [blame] | 851 | _(action_name(opts))); |
| Johannes Schindelin | 49fb937 | 2016-09-09 14:38:20 | [diff] [blame] | 852 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 853 | } |
| 854 | rollback_lock_file(&index_lock); |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 855 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 856 | } |
| 857 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 858 | struct todo_item { |
| 859 | enum todo_command command; |
| 860 | struct commit *commit; |
| Johannes Schindelin | c22f7df | 2016-10-21 12:25:00 | [diff] [blame] | 861 | const char *arg; |
| 862 | int arg_len; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 863 | size_t offset_in_buf; |
| 864 | }; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 865 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 866 | struct todo_list { |
| 867 | struct strbuf buf; |
| 868 | struct todo_item *items; |
| 869 | int nr, alloc, current; |
| 870 | }; |
| 871 | |
| 872 | #define TODO_LIST_INIT { STRBUF_INIT } |
| 873 | |
| 874 | static void todo_list_release(struct todo_list *todo_list) |
| 875 | { |
| 876 | strbuf_release(&todo_list->buf); |
| 877 | free(todo_list->items); |
| 878 | todo_list->items = NULL; |
| 879 | todo_list->nr = todo_list->alloc = 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 880 | } |
| 881 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 882 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
| 883 | { |
| 884 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); |
| 885 | return todo_list->items + todo_list->nr++; |
| 886 | } |
| 887 | |
| 888 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 889 | { |
| 890 | unsigned char commit_sha1[20]; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 891 | char *end_of_object_name; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 892 | int i, saved, status, padding; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 893 | |
| Johannes Schindelin | 8f8550b | 2016-10-21 12:25:36 | [diff] [blame] | 894 | /* left-trim */ |
| 895 | bol += strspn(bol, " \t"); |
| 896 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 897 | for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++) |
| 898 | if (skip_prefix(bol, todo_command_strings[i], &bol)) { |
| 899 | item->command = i; |
| 900 | break; |
| 901 | } |
| 902 | if (i >= ARRAY_SIZE(todo_command_strings)) |
| 903 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 904 | |
| 905 | /* Eat up extra spaces/ tabs before object name */ |
| 906 | padding = strspn(bol, " \t"); |
| 907 | if (!padding) |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 908 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 909 | bol += padding; |
| 910 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 911 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 912 | saved = *end_of_object_name; |
| 913 | *end_of_object_name = '\0'; |
| 914 | status = get_sha1(bol, commit_sha1); |
| 915 | *end_of_object_name = saved; |
| 916 | |
| Johannes Schindelin | c22f7df | 2016-10-21 12:25:00 | [diff] [blame] | 917 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
| 918 | item->arg_len = (int)(eol - item->arg); |
| 919 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 920 | if (status < 0) |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 921 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 922 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 923 | item->commit = lookup_commit_reference(commit_sha1); |
| 924 | return !item->commit; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 925 | } |
| 926 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 927 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 928 | { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 929 | struct todo_item *item; |
| 930 | char *p = buf, *next_p; |
| 931 | int i, res = 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 932 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 933 | for (i = 1; *p; i++, p = next_p) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 934 | char *eol = strchrnul(p, '\n'); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 935 | |
| 936 | next_p = *eol ? eol + 1 /* skip LF */ : eol; |
| 937 | |
| Johannes Schindelin | 6307041 | 2016-10-21 12:24:46 | [diff] [blame] | 938 | if (p != eol && eol[-1] == '\r') |
| 939 | eol--; /* strip Carriage Return */ |
| 940 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 941 | item = append_new_todo(todo_list); |
| 942 | item->offset_in_buf = p - todo_list->buf.buf; |
| 943 | if (parse_insn_line(item, p, eol)) { |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 944 | res = error(_("invalid line %d: %.*s"), |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 945 | i, (int)(eol - p), p); |
| 946 | item->command = -1; |
| 947 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 948 | } |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 949 | if (!todo_list->nr) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 950 | return error(_("no commits parsed.")); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 951 | return res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 952 | } |
| 953 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 954 | static int read_populate_todo(struct todo_list *todo_list, |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 955 | struct replay_opts *opts) |
| 956 | { |
| Johannes Schindelin | c024650 | 2016-10-21 12:24:32 | [diff] [blame] | 957 | const char *todo_file = get_todo_path(opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 958 | int fd, res; |
| 959 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 960 | strbuf_reset(&todo_list->buf); |
| Johannes Schindelin | c024650 | 2016-10-21 12:24:32 | [diff] [blame] | 961 | fd = open(todo_file, O_RDONLY); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 962 | if (fd < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 963 | return error_errno(_("could not open '%s'"), todo_file); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 964 | if (strbuf_read(&todo_list->buf, fd, 0) < 0) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 965 | close(fd); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 966 | return error(_("could not read '%s'."), todo_file); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 967 | } |
| 968 | close(fd); |
| 969 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 970 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
| Johannes Schindelin | 2eeaf1b | 2016-10-21 12:26:13 | [diff] [blame] | 971 | if (res) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 972 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
| Johannes Schindelin | 2eeaf1b | 2016-10-21 12:26:13 | [diff] [blame] | 973 | |
| 974 | if (!is_rebase_i(opts)) { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 975 | enum todo_command valid = |
| 976 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; |
| 977 | int i; |
| 978 | |
| 979 | for (i = 0; i < todo_list->nr; i++) |
| 980 | if (valid == todo_list->items[i].command) |
| 981 | continue; |
| 982 | else if (valid == TODO_PICK) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 983 | return error(_("cannot cherry-pick during a revert.")); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 984 | else |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 985 | return error(_("cannot revert during a cherry-pick.")); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 986 | } |
| 987 | |
| Johannes Schindelin | 0ae42a0 | 2016-09-09 14:37:24 | [diff] [blame] | 988 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 989 | } |
| 990 | |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 991 | static int git_config_string_dup(char **dest, |
| 992 | const char *var, const char *value) |
| 993 | { |
| 994 | if (!value) |
| 995 | return config_error_nonbool(var); |
| 996 | free(*dest); |
| 997 | *dest = xstrdup(value); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1001 | static int populate_opts_cb(const char *key, const char *value, void *data) |
| 1002 | { |
| 1003 | struct replay_opts *opts = data; |
| 1004 | int error_flag = 1; |
| 1005 | |
| 1006 | if (!value) |
| 1007 | error_flag = 0; |
| 1008 | else if (!strcmp(key, "options.no-commit")) |
| 1009 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); |
| 1010 | else if (!strcmp(key, "options.edit")) |
| 1011 | opts->edit = git_config_bool_or_int(key, value, &error_flag); |
| 1012 | else if (!strcmp(key, "options.signoff")) |
| 1013 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); |
| 1014 | else if (!strcmp(key, "options.record-origin")) |
| 1015 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); |
| 1016 | else if (!strcmp(key, "options.allow-ff")) |
| 1017 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); |
| 1018 | else if (!strcmp(key, "options.mainline")) |
| 1019 | opts->mainline = git_config_int(key, value); |
| 1020 | else if (!strcmp(key, "options.strategy")) |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 1021 | git_config_string_dup(&opts->strategy, key, value); |
| Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 | [diff] [blame] | 1022 | else if (!strcmp(key, "options.gpg-sign")) |
| Johannes Schindelin | 03a4e26 | 2016-10-21 12:24:13 | [diff] [blame] | 1023 | git_config_string_dup(&opts->gpg_sign, key, value); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1024 | else if (!strcmp(key, "options.strategy-option")) { |
| 1025 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
| 1026 | opts->xopts[opts->xopts_nr++] = xstrdup(value); |
| 1027 | } else |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1028 | return error(_("invalid key: %s"), key); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1029 | |
| 1030 | if (!error_flag) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1031 | return error(_("invalid value for %s: %s"), key, value); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| Johannes Schindelin | 5adf9bd | 2016-10-14 13:17:16 | [diff] [blame] | 1036 | static int read_populate_opts(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1037 | { |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 1038 | if (is_rebase_i(opts)) { |
| 1039 | struct strbuf buf = STRBUF_INIT; |
| 1040 | |
| 1041 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { |
| 1042 | if (!starts_with(buf.buf, "-S")) |
| 1043 | strbuf_reset(&buf); |
| 1044 | else { |
| 1045 | free(opts->gpg_sign); |
| 1046 | opts->gpg_sign = xstrdup(buf.buf + 2); |
| 1047 | } |
| 1048 | } |
| 1049 | strbuf_release(&buf); |
| 1050 | |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 1051 | return 0; |
| Johannes Schindelin | a1c7576 | 2016-10-21 12:25:12 | [diff] [blame] | 1052 | } |
| Johannes Schindelin | b5a6704 | 2016-10-21 12:25:04 | [diff] [blame] | 1053 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1054 | if (!file_exists(git_path_opts_file())) |
| Johannes Schindelin | 0d00da7 | 2016-09-09 14:37:27 | [diff] [blame] | 1055 | return 0; |
| 1056 | /* |
| 1057 | * The function git_parse_source(), called from git_config_from_file(), |
| 1058 | * may die() in case of a syntactically incorrect file. We do not care |
| 1059 | * about this case, though, because we wrote that file ourselves, so we |
| 1060 | * are pretty certain that it is syntactically correct. |
| 1061 | */ |
| Johannes Schindelin | 5adf9bd | 2016-10-14 13:17:16 | [diff] [blame] | 1062 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1063 | return error(_("malformed options sheet: '%s'"), |
| Johannes Schindelin | 0d00da7 | 2016-09-09 14:37:27 | [diff] [blame] | 1064 | git_path_opts_file()); |
| 1065 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1066 | } |
| 1067 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1068 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1069 | struct replay_opts *opts) |
| 1070 | { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1071 | enum todo_command command = opts->action == REPLAY_PICK ? |
| 1072 | TODO_PICK : TODO_REVERT; |
| 1073 | const char *command_string = todo_command_strings[command]; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1074 | struct commit *commit; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1075 | |
| Johannes Schindelin | 34b0528 | 2016-09-09 14:37:15 | [diff] [blame] | 1076 | if (prepare_revs(opts)) |
| 1077 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1078 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1079 | while ((commit = get_revision(opts->revs))) { |
| 1080 | struct todo_item *item = append_new_todo(todo_list); |
| 1081 | const char *commit_buffer = get_commit_buffer(commit, NULL); |
| 1082 | const char *subject; |
| 1083 | int subject_len; |
| 1084 | |
| 1085 | item->command = command; |
| 1086 | item->commit = commit; |
| Johannes Schindelin | c22f7df | 2016-10-21 12:25:00 | [diff] [blame] | 1087 | item->arg = NULL; |
| 1088 | item->arg_len = 0; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1089 | item->offset_in_buf = todo_list->buf.len; |
| 1090 | subject_len = find_commit_subject(commit_buffer, &subject); |
| 1091 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, |
| 1092 | short_commit_name(commit), subject_len, subject); |
| 1093 | unuse_commit_buffer(commit, commit_buffer); |
| 1094 | } |
| Johannes Schindelin | 34b0528 | 2016-09-09 14:37:15 | [diff] [blame] | 1095 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static int create_seq_dir(void) |
| 1099 | { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1100 | if (file_exists(git_path_seq_dir())) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1101 | error(_("a cherry-pick or revert is already in progress")); |
| 1102 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); |
| 1103 | return -1; |
| 1104 | } |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1105 | else if (mkdir(git_path_seq_dir(), 0777) < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1106 | return error_errno(_("could not create sequencer directory '%s'"), |
| Johannes Schindelin | f6e82b0 | 2016-09-09 14:37:44 | [diff] [blame] | 1107 | git_path_seq_dir()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1111 | static int save_head(const char *head) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1112 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1113 | static struct lock_file head_lock; |
| 1114 | struct strbuf buf = STRBUF_INIT; |
| 1115 | int fd; |
| 1116 | |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1117 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
| 1118 | if (fd < 0) { |
| 1119 | rollback_lock_file(&head_lock); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1120 | return error_errno(_("could not lock HEAD")); |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1121 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1122 | strbuf_addf(&buf, "%s\n", head); |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1123 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 1124 | rollback_lock_file(&head_lock); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1125 | return error_errno(_("could not write to '%s'"), |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1126 | git_path_head_file()); |
| 1127 | } |
| 1128 | if (commit_lock_file(&head_lock) < 0) { |
| 1129 | rollback_lock_file(&head_lock); |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1130 | return error(_("failed to finalize '%s'."), git_path_head_file()); |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1131 | } |
| 1132 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static int reset_for_rollback(const unsigned char *sha1) |
| 1136 | { |
| 1137 | const char *argv[4]; /* reset --merge <arg> + NULL */ |
| 1138 | argv[0] = "reset"; |
| 1139 | argv[1] = "--merge"; |
| 1140 | argv[2] = sha1_to_hex(sha1); |
| 1141 | argv[3] = NULL; |
| 1142 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 1143 | } |
| 1144 | |
| 1145 | static int rollback_single_pick(void) |
| 1146 | { |
| 1147 | unsigned char head_sha1[20]; |
| 1148 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1149 | if (!file_exists(git_path_cherry_pick_head()) && |
| 1150 | !file_exists(git_path_revert_head())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1151 | return error(_("no cherry-pick or revert in progress")); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1152 | if (read_ref_full("HEAD", 0, head_sha1, NULL)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1153 | return error(_("cannot resolve HEAD")); |
| 1154 | if (is_null_sha1(head_sha1)) |
| 1155 | return error(_("cannot abort from a branch yet to be born")); |
| 1156 | return reset_for_rollback(head_sha1); |
| 1157 | } |
| 1158 | |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1159 | int sequencer_rollback(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1160 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1161 | FILE *f; |
| 1162 | unsigned char sha1[20]; |
| 1163 | struct strbuf buf = STRBUF_INIT; |
| 1164 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1165 | f = fopen(git_path_head_file(), "r"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1166 | if (!f && errno == ENOENT) { |
| 1167 | /* |
| 1168 | * There is no multiple-cherry-pick in progress. |
| 1169 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
| 1170 | * a single-cherry-pick in progress, abort that. |
| 1171 | */ |
| 1172 | return rollback_single_pick(); |
| 1173 | } |
| 1174 | if (!f) |
| Johannes Schindelin | f7ed195 | 2016-10-21 12:26:21 | [diff] [blame] | 1175 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
| Junio C Hamano | 8f309ae | 2016-01-13 23:31:17 | [diff] [blame] | 1176 | if (strbuf_getline_lf(&buf, f)) { |
| Johannes Schindelin | f7ed195 | 2016-10-21 12:26:21 | [diff] [blame] | 1177 | error(_("cannot read '%s': %s"), git_path_head_file(), |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1178 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1179 | fclose(f); |
| 1180 | goto fail; |
| 1181 | } |
| 1182 | fclose(f); |
| 1183 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { |
| 1184 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1185 | git_path_head_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1186 | goto fail; |
| 1187 | } |
| Michael J Gruber | 0f974e2 | 2016-06-06 13:23:54 | [diff] [blame] | 1188 | if (is_null_sha1(sha1)) { |
| 1189 | error(_("cannot abort from a branch yet to be born")); |
| 1190 | goto fail; |
| 1191 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1192 | if (reset_for_rollback(sha1)) |
| 1193 | goto fail; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1194 | strbuf_release(&buf); |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1195 | return sequencer_remove_state(opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1196 | fail: |
| 1197 | strbuf_release(&buf); |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1201 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1202 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1203 | static struct lock_file todo_lock; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1204 | const char *todo_path = get_todo_path(opts); |
| 1205 | int next = todo_list->current, offset, fd; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1206 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1207 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
| Johannes Schindelin | 221675d | 2016-09-09 14:37:50 | [diff] [blame] | 1208 | if (fd < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1209 | return error_errno(_("could not lock '%s'"), todo_path); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1210 | offset = next < todo_list->nr ? |
| 1211 | todo_list->items[next].offset_in_buf : todo_list->buf.len; |
| 1212 | if (write_in_full(fd, todo_list->buf.buf + offset, |
| 1213 | todo_list->buf.len - offset) < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1214 | return error_errno(_("could not write to '%s'"), todo_path); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1215 | if (commit_lock_file(&todo_lock) < 0) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1216 | return error(_("failed to finalize '%s'."), todo_path); |
| Johannes Schindelin | 221675d | 2016-09-09 14:37:50 | [diff] [blame] | 1217 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1218 | } |
| 1219 | |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1220 | static int save_opts(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1221 | { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1222 | const char *opts_file = git_path_opts_file(); |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1223 | int res = 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1224 | |
| 1225 | if (opts->no_commit) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1226 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1227 | if (opts->edit) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1228 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1229 | if (opts->signoff) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1230 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1231 | if (opts->record_origin) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1232 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1233 | if (opts->allow_ff) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1234 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1235 | if (opts->mainline) { |
| 1236 | struct strbuf buf = STRBUF_INIT; |
| 1237 | strbuf_addf(&buf, "%d", opts->mainline); |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1238 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1239 | strbuf_release(&buf); |
| 1240 | } |
| 1241 | if (opts->strategy) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1242 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
| Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 | [diff] [blame] | 1243 | if (opts->gpg_sign) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1244 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1245 | if (opts->xopts) { |
| 1246 | int i; |
| 1247 | for (i = 0; i < opts->xopts_nr; i++) |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1248 | res |= git_config_set_multivar_in_file_gently(opts_file, |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1249 | "options.strategy-option", |
| 1250 | opts->xopts[i], "^$", 0); |
| 1251 | } |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1252 | return res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1253 | } |
| 1254 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1255 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1256 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1257 | int res; |
| 1258 | |
| 1259 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
| 1260 | if (opts->allow_ff) |
| 1261 | assert(!(opts->signoff || opts->no_commit || |
| 1262 | opts->record_origin || opts->edit)); |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 1263 | if (read_and_refresh_cache(opts)) |
| 1264 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1265 | |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1266 | while (todo_list->current < todo_list->nr) { |
| 1267 | struct todo_item *item = todo_list->items + todo_list->current; |
| 1268 | if (save_todo(todo_list, opts)) |
| Johannes Schindelin | 221675d | 2016-09-09 14:37:50 | [diff] [blame] | 1269 | return -1; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1270 | res = do_pick_commit(item->command, item->commit, opts); |
| 1271 | todo_list->current++; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1272 | if (res) |
| 1273 | return res; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Sequence of picks finished successfully; cleanup by |
| 1278 | * removing the .git/sequencer directory |
| 1279 | */ |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1280 | return sequencer_remove_state(opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | static int continue_single_pick(void) |
| 1284 | { |
| 1285 | const char *argv[] = { "commit", NULL }; |
| 1286 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1287 | if (!file_exists(git_path_cherry_pick_head()) && |
| 1288 | !file_exists(git_path_revert_head())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1289 | return error(_("no cherry-pick or revert in progress")); |
| 1290 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 1291 | } |
| 1292 | |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1293 | int sequencer_continue(struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1294 | { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1295 | struct todo_list todo_list = TODO_LIST_INIT; |
| 1296 | int res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1297 | |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1298 | if (read_and_refresh_cache(opts)) |
| 1299 | return -1; |
| 1300 | |
| Johannes Schindelin | c024650 | 2016-10-21 12:24:32 | [diff] [blame] | 1301 | if (!file_exists(get_todo_path(opts))) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1302 | return continue_single_pick(); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1303 | if (read_populate_opts(opts)) |
| Johannes Schindelin | 0ae42a0 | 2016-09-09 14:37:24 | [diff] [blame] | 1304 | return -1; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1305 | if ((res = read_populate_todo(&todo_list, opts))) |
| 1306 | goto release_todo_list; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1307 | |
| 1308 | /* Verify that the conflict has been resolved */ |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1309 | if (file_exists(git_path_cherry_pick_head()) || |
| 1310 | file_exists(git_path_revert_head())) { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1311 | res = continue_single_pick(); |
| 1312 | if (res) |
| 1313 | goto release_todo_list; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1314 | } |
| Junio C Hamano | 6503602 | 2016-10-27 21:58:50 | [diff] [blame] | 1315 | if (index_differs_from("HEAD", 0, 0)) { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1316 | res = error_dirty_index(opts); |
| 1317 | goto release_todo_list; |
| 1318 | } |
| 1319 | todo_list.current++; |
| 1320 | res = pick_commits(&todo_list, opts); |
| 1321 | release_todo_list: |
| 1322 | todo_list_release(&todo_list); |
| 1323 | return res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | static int single_pick(struct commit *cmit, struct replay_opts *opts) |
| 1327 | { |
| 1328 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1329 | return do_pick_commit(opts->action == REPLAY_PICK ? |
| 1330 | TODO_PICK : TODO_REVERT, cmit, opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | int sequencer_pick_revisions(struct replay_opts *opts) |
| 1334 | { |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1335 | struct todo_list todo_list = TODO_LIST_INIT; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1336 | unsigned char sha1[20]; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1337 | int i, res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1338 | |
| Johannes Schindelin | 2863584 | 2016-10-21 12:24:55 | [diff] [blame] | 1339 | assert(opts->revs); |
| Johannes Schindelin | 0d9c6dc | 2016-09-09 14:37:21 | [diff] [blame] | 1340 | if (read_and_refresh_cache(opts)) |
| 1341 | return -1; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1342 | |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1343 | for (i = 0; i < opts->revs->pending.nr; i++) { |
| 1344 | unsigned char sha1[20]; |
| 1345 | const char *name = opts->revs->pending.objects[i].name; |
| 1346 | |
| 1347 | /* This happens when using --stdin. */ |
| 1348 | if (!strlen(name)) |
| 1349 | continue; |
| 1350 | |
| 1351 | if (!get_sha1(name, sha1)) { |
| Junio C Hamano | 7c0b0d8 | 2013-05-09 20:27:49 | [diff] [blame] | 1352 | if (!lookup_commit_reference_gently(sha1, 1)) { |
| 1353 | enum object_type type = sha1_object_info(sha1, NULL); |
| Johannes Schindelin | b9b946d | 2016-08-26 13:47:10 | [diff] [blame] | 1354 | return error(_("%s: can't cherry-pick a %s"), |
| 1355 | name, typename(type)); |
| Junio C Hamano | 7c0b0d8 | 2013-05-09 20:27:49 | [diff] [blame] | 1356 | } |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1357 | } else |
| Johannes Schindelin | b9b946d | 2016-08-26 13:47:10 | [diff] [blame] | 1358 | return error(_("%s: bad revision"), name); |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1359 | } |
| 1360 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1361 | /* |
| 1362 | * If we were called as "git cherry-pick <commit>", just |
| 1363 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / |
| 1364 | * REVERT_HEAD, and don't touch the sequencer state. |
| 1365 | * This means it is possible to cherry-pick in the middle |
| 1366 | * of a cherry-pick sequence. |
| 1367 | */ |
| 1368 | if (opts->revs->cmdline.nr == 1 && |
| 1369 | opts->revs->cmdline.rev->whence == REV_CMD_REV && |
| 1370 | opts->revs->no_walk && |
| 1371 | !opts->revs->cmdline.rev->flags) { |
| 1372 | struct commit *cmit; |
| 1373 | if (prepare_revision_walk(opts->revs)) |
| Johannes Schindelin | b9b946d | 2016-08-26 13:47:10 | [diff] [blame] | 1374 | return error(_("revision walk setup failed")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1375 | cmit = get_revision(opts->revs); |
| 1376 | if (!cmit || get_revision(opts->revs)) |
| Johannes Schindelin | b9b946d | 2016-08-26 13:47:10 | [diff] [blame] | 1377 | return error("BUG: expected exactly one commit from walk"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1378 | return single_pick(cmit, opts); |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * Start a new cherry-pick/ revert sequence; but |
| 1383 | * first, make sure that an existing one isn't in |
| 1384 | * progress |
| 1385 | */ |
| 1386 | |
| Johannes Schindelin | 34b0528 | 2016-09-09 14:37:15 | [diff] [blame] | 1387 | if (walk_revs_populate_todo(&todo_list, opts) || |
| 1388 | create_seq_dir() < 0) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1389 | return -1; |
| Michael J Gruber | 0f974e2 | 2016-06-06 13:23:54 | [diff] [blame] | 1390 | if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT)) |
| Johannes Schindelin | 93b3df6 | 2016-10-21 12:26:25 | [diff] [blame] | 1391 | return error(_("can't revert as initial commit")); |
| Johannes Schindelin | 311fd39 | 2016-09-09 14:37:47 | [diff] [blame] | 1392 | if (save_head(sha1_to_hex(sha1))) |
| 1393 | return -1; |
| Johannes Schindelin | 88d5a27 | 2016-09-09 14:37:53 | [diff] [blame] | 1394 | if (save_opts(opts)) |
| 1395 | return -1; |
| Johannes Schindelin | 004fefa | 2016-10-21 12:24:41 | [diff] [blame] | 1396 | res = pick_commits(&todo_list, opts); |
| 1397 | todo_list_release(&todo_list); |
| 1398 | return res; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1399 | } |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1400 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1401 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1402 | { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1403 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1404 | struct strbuf sob = STRBUF_INIT; |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1405 | int has_footer; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1406 | |
| 1407 | strbuf_addstr(&sob, sign_off_header); |
| 1408 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
| 1409 | getenv("GIT_COMMITTER_EMAIL"))); |
| 1410 | strbuf_addch(&sob, '\n'); |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1411 | |
| 1412 | /* |
| 1413 | * If the whole message buffer is equal to the sob, pretend that we |
| 1414 | * found a conforming footer with a matching sob |
| 1415 | */ |
| 1416 | if (msgbuf->len - ignore_footer == sob.len && |
| 1417 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
| 1418 | has_footer = 3; |
| 1419 | else |
| 1420 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
| 1421 | |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1422 | if (!has_footer) { |
| 1423 | const char *append_newlines = NULL; |
| 1424 | size_t len = msgbuf->len - ignore_footer; |
| 1425 | |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1426 | if (!len) { |
| 1427 | /* |
| 1428 | * The buffer is completely empty. Leave foom for |
| 1429 | * the title and body to be filled in by the user. |
| 1430 | */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1431 | append_newlines = "\n\n"; |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1432 | } else if (msgbuf->buf[len - 1] != '\n') { |
| 1433 | /* |
| 1434 | * Incomplete line. Complete the line and add a |
| 1435 | * blank one so that there is an empty line between |
| 1436 | * the message body and the sob. |
| 1437 | */ |
| 1438 | append_newlines = "\n\n"; |
| 1439 | } else if (len == 1) { |
| 1440 | /* |
| 1441 | * Buffer contains a single newline. Add another |
| 1442 | * so that we leave room for the title and body. |
| 1443 | */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1444 | append_newlines = "\n"; |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1445 | } else if (msgbuf->buf[len - 2] != '\n') { |
| 1446 | /* |
| 1447 | * Buffer ends with a single newline. Add another |
| 1448 | * so that there is an empty line between the message |
| 1449 | * body and the sob. |
| 1450 | */ |
| 1451 | append_newlines = "\n"; |
| 1452 | } /* else, the buffer already ends with two newlines. */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1453 | |
| 1454 | if (append_newlines) |
| 1455 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1456 | append_newlines, strlen(append_newlines)); |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1457 | } |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1458 | |
| 1459 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
| 1460 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1461 | sob.buf, sob.len); |
| 1462 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1463 | strbuf_release(&sob); |
| 1464 | } |