| 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" |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 18 | |
| 19 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 20 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 21 | const char sign_off_header[] = "Signed-off-by: "; |
| Brandon Casey | cd650a4 | 2013-02-12 10:17:32 | [diff] [blame] | 22 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 23 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 24 | static GIT_PATH_FUNC(git_path_todo_file, SEQ_TODO_FILE) |
| 25 | static GIT_PATH_FUNC(git_path_opts_file, SEQ_OPTS_FILE) |
| 26 | static GIT_PATH_FUNC(git_path_seq_dir, SEQ_DIR) |
| 27 | static GIT_PATH_FUNC(git_path_head_file, SEQ_HEAD_FILE) |
| 28 | |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 29 | static int is_rfc2822_line(const char *buf, int len) |
| 30 | { |
| 31 | int i; |
| 32 | |
| 33 | for (i = 0; i < len; i++) { |
| 34 | int ch = buf[i]; |
| 35 | if (ch == ':') |
| 36 | return 1; |
| 37 | if (!isalnum(ch) && ch != '-') |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int is_cherry_picked_from_line(const char *buf, int len) |
| 45 | { |
| 46 | /* |
| 47 | * We only care that it looks roughly like (cherry picked from ...) |
| 48 | */ |
| 49 | return len > strlen(cherry_picked_prefix) + 1 && |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 50 | starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')'; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 51 | } |
| 52 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 53 | /* |
| 54 | * Returns 0 for non-conforming footer |
| 55 | * Returns 1 for conforming footer |
| 56 | * Returns 2 when sob exists within conforming footer |
| 57 | * Returns 3 when sob exists within conforming footer as last entry |
| 58 | */ |
| 59 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
| 60 | int ignore_footer) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 61 | { |
| 62 | char prev; |
| 63 | int i, k; |
| 64 | int len = sb->len - ignore_footer; |
| 65 | const char *buf = sb->buf; |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 66 | int found_sob = 0; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 67 | |
| 68 | /* footer must end with newline */ |
| 69 | if (!len || buf[len - 1] != '\n') |
| 70 | return 0; |
| 71 | |
| 72 | prev = '\0'; |
| 73 | for (i = len - 1; i > 0; i--) { |
| 74 | char ch = buf[i]; |
| 75 | if (prev == '\n' && ch == '\n') /* paragraph break */ |
| 76 | break; |
| 77 | prev = ch; |
| 78 | } |
| 79 | |
| 80 | /* require at least one blank line */ |
| 81 | if (prev != '\n' || buf[i] != '\n') |
| 82 | return 0; |
| 83 | |
| 84 | /* advance to start of last paragraph */ |
| 85 | while (i < len - 1 && buf[i] == '\n') |
| 86 | i++; |
| 87 | |
| 88 | for (; i < len; i = k) { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 89 | int found_rfc2822; |
| 90 | |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 91 | for (k = i; k < len && buf[k] != '\n'; k++) |
| 92 | ; /* do nothing */ |
| 93 | k++; |
| 94 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 95 | found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1); |
| 96 | if (found_rfc2822 && sob && |
| 97 | !strncmp(buf + i, sob->buf, sob->len)) |
| 98 | found_sob = k; |
| 99 | |
| 100 | if (!(found_rfc2822 || |
| 101 | is_cherry_picked_from_line(buf + i, k - i - 1))) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 104 | if (found_sob == i) |
| 105 | return 3; |
| 106 | if (found_sob) |
| 107 | return 2; |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 108 | return 1; |
| 109 | } |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 110 | |
| Junio C Hamano | 250f249 | 2012-09-16 06:20:40 | [diff] [blame] | 111 | static void remove_sequencer_state(void) |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 112 | { |
| 113 | struct strbuf seq_dir = STRBUF_INIT; |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 114 | |
| René Scharfe | bc57b9c | 2016-08-05 20:37:11 | [diff] [blame] | 115 | strbuf_addstr(&seq_dir, git_path(SEQ_DIR)); |
| Jonathan Nieder | d596118 | 2011-12-10 13:06:12 | [diff] [blame] | 116 | remove_dir_recursively(&seq_dir, 0); |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 117 | strbuf_release(&seq_dir); |
| Ramkumar Ramachandra | 26ae337 | 2011-08-04 10:39:11 | [diff] [blame] | 118 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 119 | |
| 120 | static const char *action_name(const struct replay_opts *opts) |
| 121 | { |
| 122 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
| 123 | } |
| 124 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 125 | struct commit_message { |
| 126 | char *parent_label; |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 127 | char *label; |
| 128 | char *subject; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 129 | const char *message; |
| 130 | }; |
| 131 | |
| 132 | static int get_message(struct commit *commit, struct commit_message *out) |
| 133 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 134 | const char *abbrev, *subject; |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 135 | int subject_len; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 136 | |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 137 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 138 | abbrev = find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 139 | |
| 140 | subject_len = find_commit_subject(out->message, &subject); |
| 141 | |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 142 | out->subject = xmemdupz(subject, subject_len); |
| 143 | out->label = xstrfmt("%s... %s", abbrev, out->subject); |
| 144 | out->parent_label = xstrfmt("parent of %s", out->label); |
| 145 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 149 | static void free_message(struct commit *commit, struct commit_message *msg) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 150 | { |
| 151 | free(msg->parent_label); |
| Jeff King | 7b35eaf | 2016-02-22 22:44:57 | [diff] [blame] | 152 | free(msg->label); |
| 153 | free(msg->subject); |
| Jeff King | b66103c | 2014-06-10 21:41:39 | [diff] [blame] | 154 | unuse_commit_buffer(commit, msg->message); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 155 | } |
| 156 | |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 157 | static void print_advice(int show_hint, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 158 | { |
| 159 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 160 | |
| 161 | if (msg) { |
| 162 | fprintf(stderr, "%s\n", msg); |
| 163 | /* |
| Stefano Lattarini | 41ccfdd | 2013-04-11 22:36:10 | [diff] [blame] | 164 | * A conflict has occurred but the porcelain |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 165 | * (typically rebase --interactive) wants to take care |
| 166 | * of the commit itself so remove CHERRY_PICK_HEAD |
| 167 | */ |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 168 | unlink(git_path_cherry_pick_head()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 172 | if (show_hint) { |
| 173 | if (opts->no_commit) |
| 174 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 175 | "with 'git add <paths>' or 'git rm <paths>'")); |
| 176 | else |
| 177 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 178 | "with 'git add <paths>' or 'git rm <paths>'\n" |
| 179 | "and commit the result with 'git commit'")); |
| 180 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void write_message(struct strbuf *msgbuf, const char *filename) |
| 184 | { |
| 185 | static struct lock_file msg_file; |
| 186 | |
| 187 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, |
| 188 | LOCK_DIE_ON_ERROR); |
| 189 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) |
| 190 | die_errno(_("Could not write to %s"), filename); |
| 191 | strbuf_release(msgbuf); |
| 192 | if (commit_lock_file(&msg_file) < 0) |
| Vasco Almeida | 62d09ef | 2016-06-17 20:21:19 | [diff] [blame] | 193 | die(_("Error wrapping up %s."), filename); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static struct tree *empty_tree(void) |
| 197 | { |
| Jeff King | cba595b | 2012-03-22 18:53:24 | [diff] [blame] | 198 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int error_dirty_index(struct replay_opts *opts) |
| 202 | { |
| 203 | if (read_cache_unmerged()) |
| 204 | return error_resolve_conflict(action_name(opts)); |
| 205 | |
| 206 | /* Different translation strings for cherry-pick and revert */ |
| 207 | if (opts->action == REPLAY_PICK) |
| 208 | error(_("Your local changes would be overwritten by cherry-pick.")); |
| 209 | else |
| 210 | error(_("Your local changes would be overwritten by revert.")); |
| 211 | |
| 212 | if (advice_commit_before_merge) |
| 213 | advise(_("Commit your changes or stash them to proceed.")); |
| 214 | return -1; |
| 215 | } |
| 216 | |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 217 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 218 | int unborn, struct replay_opts *opts) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 219 | { |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 220 | struct ref_transaction *transaction; |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 221 | struct strbuf sb = STRBUF_INIT; |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 222 | struct strbuf err = STRBUF_INIT; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 223 | |
| 224 | read_cache(); |
| Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 15:53:49 | [diff] [blame] | 225 | if (checkout_fast_forward(from, to, 1)) |
| Fabian Ruch | 43dee07 | 2014-06-09 15:04:36 | [diff] [blame] | 226 | exit(128); /* the callee should have complained already */ |
| Ronnie Sahlberg | 651ab9f | 2014-04-16 18:56:52 | [diff] [blame] | 227 | |
| Vasco Almeida | e8d7c39 | 2016-06-17 20:20:55 | [diff] [blame] | 228 | strbuf_addf(&sb, _("%s: fast-forward"), action_name(opts)); |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 229 | |
| 230 | transaction = ref_transaction_begin(&err); |
| 231 | if (!transaction || |
| 232 | ref_transaction_update(transaction, "HEAD", |
| 233 | to, unborn ? null_sha1 : from, |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 234 | 0, sb.buf, &err) || |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 235 | ref_transaction_commit(transaction, &err)) { |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 236 | ref_transaction_free(transaction); |
| 237 | error("%s", err.buf); |
| 238 | strbuf_release(&sb); |
| 239 | strbuf_release(&err); |
| 240 | return -1; |
| 241 | } |
| Ronnie Sahlberg | 651ab9f | 2014-04-16 18:56:52 | [diff] [blame] | 242 | |
| Ramkumar Ramachandra | eb4be1c | 2013-06-19 07:37:09 | [diff] [blame] | 243 | strbuf_release(&sb); |
| Ronnie Sahlberg | d668d16 | 2014-04-16 22:37:45 | [diff] [blame] | 244 | strbuf_release(&err); |
| 245 | ref_transaction_free(transaction); |
| 246 | return 0; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 247 | } |
| 248 | |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 249 | void append_conflicts_hint(struct strbuf *msgbuf) |
| 250 | { |
| 251 | int i; |
| 252 | |
| Junio C Hamano | 261f315 | 2014-10-28 20:04:38 | [diff] [blame] | 253 | strbuf_addch(msgbuf, '\n'); |
| 254 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 255 | for (i = 0; i < active_nr;) { |
| 256 | const struct cache_entry *ce = active_cache[i++]; |
| 257 | if (ce_stage(ce)) { |
| Junio C Hamano | 261f315 | 2014-10-28 20:04:38 | [diff] [blame] | 258 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 259 | while (i < active_nr && !strcmp(ce->name, |
| 260 | active_cache[i]->name)) |
| 261 | i++; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 266 | static int do_recursive_merge(struct commit *base, struct commit *next, |
| 267 | const char *base_label, const char *next_label, |
| 268 | unsigned char *head, struct strbuf *msgbuf, |
| 269 | struct replay_opts *opts) |
| 270 | { |
| 271 | struct merge_options o; |
| 272 | 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] | 273 | int clean; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 274 | const char **xopt; |
| 275 | static struct lock_file index_lock; |
| 276 | |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 277 | hold_locked_index(&index_lock, 1); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 278 | |
| 279 | read_cache(); |
| 280 | |
| 281 | init_merge_options(&o); |
| 282 | o.ancestor = base ? base_label : "(empty tree)"; |
| 283 | o.branch1 = "HEAD"; |
| 284 | o.branch2 = next ? next_label : "(empty tree)"; |
| 285 | |
| 286 | head_tree = parse_tree_indirect(head); |
| 287 | next_tree = next ? next->tree : empty_tree(); |
| 288 | base_tree = base ? base->tree : empty_tree(); |
| 289 | |
| 290 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
| 291 | parse_merge_opt(&o, *xopt); |
| 292 | |
| 293 | clean = merge_trees(&o, |
| 294 | head_tree, |
| 295 | next_tree, base_tree, &result); |
| Johannes Schindelin | 548009c | 2016-08-01 11:44:53 | [diff] [blame] | 296 | strbuf_release(&o.obuf); |
| Johannes Schindelin | f241ff0 | 2016-07-26 16:06:02 | [diff] [blame] | 297 | if (clean < 0) |
| 298 | return clean; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 299 | |
| 300 | if (active_cache_changed && |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 301 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 302 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
| 303 | die(_("%s: Unable to write new index file"), action_name(opts)); |
| 304 | rollback_lock_file(&index_lock); |
| 305 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 306 | if (opts->signoff) |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 307 | append_signoff(msgbuf, 0, 0); |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 308 | |
| Junio C Hamano | 75c961b | 2014-10-24 18:34:59 | [diff] [blame] | 309 | if (!clean) |
| 310 | append_conflicts_hint(msgbuf); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 311 | |
| 312 | return !clean; |
| 313 | } |
| 314 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 315 | static int is_index_unchanged(void) |
| 316 | { |
| 317 | unsigned char head_sha1[20]; |
| 318 | struct commit *head_commit; |
| 319 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 320 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 321 | return error(_("Could not resolve HEAD commit\n")); |
| 322 | |
| 323 | head_commit = lookup_commit(head_sha1); |
| Neil Horman | 4b58006 | 2012-05-03 12:10:22 | [diff] [blame] | 324 | |
| 325 | /* |
| 326 | * If head_commit is NULL, check_commit, called from |
| 327 | * lookup_commit, would have indicated that head_commit is not |
| 328 | * a commit object already. parse_commit() will return failure |
| 329 | * without further complaints in such a case. Otherwise, if |
| 330 | * the commit is invalid, parse_commit() will complain. So |
| 331 | * there is nothing for us to say here. Just return failure. |
| 332 | */ |
| 333 | if (parse_commit(head_commit)) |
| 334 | return -1; |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 335 | |
| 336 | if (!active_cache_tree) |
| 337 | active_cache_tree = cache_tree(); |
| 338 | |
| 339 | if (!cache_tree_fully_valid(active_cache_tree)) |
| Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 12:19:32 | [diff] [blame] | 340 | if (cache_tree_update(&the_index, 0)) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 341 | return error(_("Unable to update cache tree\n")); |
| 342 | |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 343 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 344 | } |
| 345 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 346 | /* |
| 347 | * If we are cherry-pick, and if the merge did not result in |
| 348 | * hand-editing, we will hit this commit and inherit the original |
| 349 | * author date and name. |
| 350 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 351 | * we had better say that the current user is responsible for that. |
| 352 | */ |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 353 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
| 354 | int allow_empty) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 355 | { |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 356 | struct argv_array array; |
| 357 | int rc; |
| Michael J Gruber | 17d65f0 | 2015-03-06 13:55:32 | [diff] [blame] | 358 | const char *value; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 359 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 360 | argv_array_init(&array); |
| 361 | argv_array_push(&array, "commit"); |
| 362 | argv_array_push(&array, "-n"); |
| 363 | |
| Jeff King | 3bdd552 | 2014-06-19 21:28:20 | [diff] [blame] | 364 | if (opts->gpg_sign) |
| 365 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 366 | if (opts->signoff) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 367 | argv_array_push(&array, "-s"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 368 | if (!opts->edit) { |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 369 | argv_array_push(&array, "-F"); |
| 370 | argv_array_push(&array, defmsg); |
| Michael J Gruber | 17d65f0 | 2015-03-06 13:55:32 | [diff] [blame] | 371 | if (!opts->signoff && |
| 372 | !opts->record_origin && |
| 373 | git_config_get_value("commit.cleanup", &value)) |
| 374 | argv_array_push(&array, "--cleanup=verbatim"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 375 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 376 | |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 377 | if (allow_empty) |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 378 | argv_array_push(&array, "--allow-empty"); |
| Neil Horman | df478b7 | 2012-04-11 20:21:53 | [diff] [blame] | 379 | |
| Chris Webb | 4bee958 | 2012-08-02 10:38:51 | [diff] [blame] | 380 | if (opts->allow_empty_message) |
| 381 | argv_array_push(&array, "--allow-empty-message"); |
| 382 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 383 | rc = run_command_v_opt(array.argv, RUN_GIT_CMD); |
| 384 | argv_array_clear(&array); |
| 385 | return rc; |
| 386 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 387 | |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 388 | static int is_original_commit_empty(struct commit *commit) |
| 389 | { |
| 390 | const unsigned char *ptree_sha1; |
| 391 | |
| 392 | if (parse_commit(commit)) |
| 393 | return error(_("Could not parse commit %s\n"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 394 | oid_to_hex(&commit->object.oid)); |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 395 | if (commit->parents) { |
| 396 | struct commit *parent = commit->parents->item; |
| 397 | if (parse_commit(parent)) |
| 398 | return error(_("Could not parse parent commit %s\n"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 399 | oid_to_hex(&parent->object.oid)); |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 400 | ptree_sha1 = parent->tree->object.oid.hash; |
| Neil Horman | b27cfb0 | 2012-04-20 14:36:15 | [diff] [blame] | 401 | } else { |
| 402 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ |
| 403 | } |
| 404 | |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 405 | return !hashcmp(ptree_sha1, commit->tree->object.oid.hash); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 406 | } |
| 407 | |
| Junio C Hamano | ac2b0e8 | 2012-05-30 00:14:41 | [diff] [blame] | 408 | /* |
| 409 | * Do we run "git commit" with "--allow-empty"? |
| 410 | */ |
| 411 | static int allow_empty(struct replay_opts *opts, struct commit *commit) |
| 412 | { |
| 413 | int index_unchanged, empty_commit; |
| 414 | |
| 415 | /* |
| 416 | * Three cases: |
| 417 | * |
| 418 | * (1) we do not allow empty at all and error out. |
| 419 | * |
| 420 | * (2) we allow ones that were initially empty, but |
| 421 | * forbid the ones that become empty; |
| 422 | * |
| 423 | * (3) we allow both. |
| 424 | */ |
| 425 | if (!opts->allow_empty) |
| 426 | return 0; /* let "git commit" barf as necessary */ |
| 427 | |
| 428 | index_unchanged = is_index_unchanged(); |
| 429 | if (index_unchanged < 0) |
| 430 | return index_unchanged; |
| 431 | if (!index_unchanged) |
| 432 | return 0; /* we do not have to say --allow-empty */ |
| 433 | |
| 434 | if (opts->keep_redundant_commits) |
| 435 | return 1; |
| 436 | |
| 437 | empty_commit = is_original_commit_empty(commit); |
| 438 | if (empty_commit < 0) |
| 439 | return empty_commit; |
| 440 | if (!empty_commit) |
| 441 | return 0; |
| 442 | else |
| 443 | return 1; |
| 444 | } |
| 445 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 446 | static int do_pick_commit(struct commit *commit, struct replay_opts *opts) |
| 447 | { |
| 448 | unsigned char head[20]; |
| 449 | struct commit *base, *next, *parent; |
| 450 | const char *base_label, *next_label; |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 451 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 452 | struct strbuf msgbuf = STRBUF_INIT; |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 453 | int res, unborn = 0, allow; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 454 | |
| 455 | if (opts->no_commit) { |
| 456 | /* |
| 457 | * We do not intend to commit immediately. We just want to |
| 458 | * merge the differences in, so let's compute the tree |
| 459 | * that represents the "current" state for merge-recursive |
| 460 | * to work on. |
| 461 | */ |
| 462 | if (write_cache_as_tree(head, 0, NULL)) |
| 463 | die (_("Your index file is unmerged.")); |
| 464 | } else { |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 465 | unborn = get_sha1("HEAD", head); |
| 466 | if (unborn) |
| 467 | hashcpy(head, EMPTY_TREE_SHA1_BIN); |
| 468 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 469 | return error_dirty_index(opts); |
| 470 | } |
| 471 | discard_cache(); |
| 472 | |
| 473 | if (!commit->parents) { |
| 474 | parent = NULL; |
| 475 | } |
| 476 | else if (commit->parents->next) { |
| 477 | /* Reverting or cherry-picking a merge commit */ |
| 478 | int cnt; |
| 479 | struct commit_list *p; |
| 480 | |
| 481 | if (!opts->mainline) |
| 482 | 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] | 483 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 484 | |
| 485 | for (cnt = 1, p = commit->parents; |
| 486 | cnt != opts->mainline && p; |
| 487 | cnt++) |
| 488 | p = p->next; |
| 489 | if (cnt != opts->mainline || !p) |
| 490 | return error(_("Commit %s does not have parent %d"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 491 | oid_to_hex(&commit->object.oid), opts->mainline); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 492 | parent = p->item; |
| 493 | } else if (0 < opts->mainline) |
| 494 | return error(_("Mainline was specified but commit %s is not a merge."), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 495 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 496 | else |
| 497 | parent = commit->parents->item; |
| 498 | |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 499 | if (opts->allow_ff && |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 500 | ((parent && !hashcmp(parent->object.oid.hash, head)) || |
| Martin von Zweigbergk | 334ae39 | 2012-12-21 19:10:11 | [diff] [blame] | 501 | (!parent && unborn))) |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 502 | return fast_forward_to(commit->object.oid.hash, head, unborn, opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 503 | |
| 504 | if (parent && parse_commit(parent) < 0) |
| 505 | /* TRANSLATORS: The first %s will be "revert" or |
| 506 | "cherry-pick", the second %s a SHA1 */ |
| 507 | return error(_("%s: cannot parse parent commit %s"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 508 | action_name(opts), oid_to_hex(&parent->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 509 | |
| 510 | if (get_message(commit, &msg) != 0) |
| 511 | return error(_("Cannot get commit message for %s"), |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 512 | oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * "commit" is an existing commit. We would want to apply |
| 516 | * the difference it introduces since its first parent "prev" |
| 517 | * on top of the current HEAD if we are cherry-pick. Or the |
| 518 | * reverse of it if we are revert. |
| 519 | */ |
| 520 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 521 | if (opts->action == REPLAY_REVERT) { |
| 522 | base = commit; |
| 523 | base_label = msg.label; |
| 524 | next = parent; |
| 525 | next_label = msg.parent_label; |
| 526 | strbuf_addstr(&msgbuf, "Revert \""); |
| 527 | strbuf_addstr(&msgbuf, msg.subject); |
| 528 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 529 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 530 | |
| 531 | if (commit->parents && commit->parents->next) { |
| 532 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 533 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 534 | } |
| 535 | strbuf_addstr(&msgbuf, ".\n"); |
| 536 | } else { |
| 537 | const char *p; |
| 538 | |
| 539 | base = parent; |
| 540 | base_label = msg.parent_label; |
| 541 | next = commit; |
| 542 | next_label = msg.label; |
| 543 | |
| 544 | /* |
| 545 | * Append the commit log message to msgbuf; it starts |
| 546 | * after the tree, parent, author, committer |
| 547 | * information followed by "\n\n". |
| 548 | */ |
| 549 | p = strstr(msg.message, "\n\n"); |
| Johannes Schindelin | 88ef402 | 2016-06-29 14:14:46 | [diff] [blame] | 550 | if (p) |
| 551 | strbuf_addstr(&msgbuf, skip_blank_lines(p + 2)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 552 | |
| 553 | if (opts->record_origin) { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 554 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
| Brandon Casey | b971e04 | 2013-02-12 10:17:34 | [diff] [blame] | 555 | strbuf_addch(&msgbuf, '\n'); |
| Brandon Casey | cd650a4 | 2013-02-12 10:17:32 | [diff] [blame] | 556 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 557 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 558 | strbuf_addstr(&msgbuf, ")\n"); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) { |
| 563 | res = do_recursive_merge(base, next, base_label, next_label, |
| 564 | head, &msgbuf, opts); |
| Johannes Schindelin | f241ff0 | 2016-07-26 16:06:02 | [diff] [blame] | 565 | if (res < 0) |
| 566 | return res; |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 567 | write_message(&msgbuf, git_path_merge_msg()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 568 | } else { |
| 569 | struct commit_list *common = NULL; |
| 570 | struct commit_list *remotes = NULL; |
| 571 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 572 | write_message(&msgbuf, git_path_merge_msg()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 573 | |
| 574 | commit_list_insert(base, &common); |
| 575 | commit_list_insert(next, &remotes); |
| 576 | res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts, |
| 577 | common, sha1_to_hex(head), remotes); |
| 578 | free_commit_list(common); |
| 579 | free_commit_list(remotes); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * If the merge was clean or if it failed due to conflict, we write |
| 584 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
| 585 | * However, if the merge did not even start, then we don't want to |
| 586 | * write it at all. |
| 587 | */ |
| 588 | if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1)) |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 589 | update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL, |
| David Turner | d96a539 | 2015-07-31 06:06:21 | [diff] [blame] | 590 | REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 591 | if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1)) |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 592 | update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL, |
| David Turner | d96a539 | 2015-07-31 06:06:21 | [diff] [blame] | 593 | REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 594 | |
| 595 | if (res) { |
| 596 | error(opts->action == REPLAY_REVERT |
| 597 | ? _("could not revert %s... %s") |
| 598 | : _("could not apply %s... %s"), |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 599 | find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 600 | msg.subject); |
| Phil Hord | ed727b1 | 2012-02-22 00:44:17 | [diff] [blame] | 601 | print_advice(res == 1, opts); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 602 | rerere(opts->allow_rerere_auto); |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 603 | goto leave; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 604 | } |
| 605 | |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 606 | allow = allow_empty(opts, commit); |
| Felipe Contreras | 706728a | 2013-06-06 08:58:57 | [diff] [blame] | 607 | if (allow < 0) { |
| 608 | res = allow; |
| 609 | goto leave; |
| 610 | } |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 611 | if (!opts->no_commit) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 612 | res = run_git_commit(git_path_merge_msg(), opts, allow); |
| Felipe Contreras | c8d1351 | 2013-05-29 03:56:21 | [diff] [blame] | 613 | |
| 614 | leave: |
| Jeff King | d74a4e5 | 2014-06-10 21:39:35 | [diff] [blame] | 615 | free_message(commit, &msg); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 616 | |
| 617 | return res; |
| 618 | } |
| 619 | |
| 620 | static void prepare_revs(struct replay_opts *opts) |
| 621 | { |
| Martin von Zweigbergk | a73e22e | 2012-08-29 06:15:56 | [diff] [blame] | 622 | /* |
| 623 | * picking (but not reverting) ranges (but not individual revisions) |
| 624 | * should be done in reverse |
| 625 | */ |
| 626 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 627 | opts->revs->reverse ^= 1; |
| 628 | |
| 629 | if (prepare_revision_walk(opts->revs)) |
| 630 | die(_("revision walk setup failed")); |
| 631 | |
| 632 | if (!opts->revs->commits) |
| 633 | die(_("empty commit set passed")); |
| 634 | } |
| 635 | |
| 636 | static void read_and_refresh_cache(struct replay_opts *opts) |
| 637 | { |
| 638 | static struct lock_file index_lock; |
| 639 | int index_fd = hold_locked_index(&index_lock, 0); |
| 640 | if (read_index_preload(&the_index, NULL) < 0) |
| 641 | die(_("git %s: failed to read the index"), action_name(opts)); |
| 642 | 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] | 643 | if (the_index.cache_changed && index_fd >= 0) { |
| Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 12:19:23 | [diff] [blame] | 644 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 645 | die(_("git %s: failed to refresh the index"), action_name(opts)); |
| 646 | } |
| 647 | rollback_lock_file(&index_lock); |
| 648 | } |
| 649 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 650 | static int format_todo(struct strbuf *buf, struct commit_list *todo_list, |
| 651 | struct replay_opts *opts) |
| 652 | { |
| 653 | struct commit_list *cur = NULL; |
| 654 | const char *sha1_abbrev = NULL; |
| 655 | const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick"; |
| 656 | const char *subject; |
| 657 | int subject_len; |
| 658 | |
| 659 | for (cur = todo_list; cur; cur = cur->next) { |
| Jeff King | 8597ea3 | 2014-06-10 21:44:13 | [diff] [blame] | 660 | const char *commit_buffer = get_commit_buffer(cur->item, NULL); |
| brian m. carlson | ed1c997 | 2015-11-10 02:22:29 | [diff] [blame] | 661 | sha1_abbrev = find_unique_abbrev(cur->item->object.oid.hash, DEFAULT_ABBREV); |
| Jeff King | bc6b8fc | 2014-06-10 21:41:51 | [diff] [blame] | 662 | subject_len = find_commit_subject(commit_buffer, &subject); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 663 | strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev, |
| 664 | subject_len, subject); |
| Jeff King | bc6b8fc | 2014-06-10 21:41:51 | [diff] [blame] | 665 | unuse_commit_buffer(cur->item, commit_buffer); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 666 | } |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts) |
| 671 | { |
| 672 | unsigned char commit_sha1[20]; |
| 673 | enum replay_action action; |
| 674 | char *end_of_object_name; |
| 675 | int saved, status, padding; |
| 676 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 677 | if (starts_with(bol, "pick")) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 678 | action = REPLAY_PICK; |
| 679 | bol += strlen("pick"); |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 680 | } else if (starts_with(bol, "revert")) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 681 | action = REPLAY_REVERT; |
| 682 | bol += strlen("revert"); |
| 683 | } else |
| 684 | return NULL; |
| 685 | |
| 686 | /* Eat up extra spaces/ tabs before object name */ |
| 687 | padding = strspn(bol, " \t"); |
| 688 | if (!padding) |
| 689 | return NULL; |
| 690 | bol += padding; |
| 691 | |
| 692 | end_of_object_name = bol + strcspn(bol, " \t\n"); |
| 693 | saved = *end_of_object_name; |
| 694 | *end_of_object_name = '\0'; |
| 695 | status = get_sha1(bol, commit_sha1); |
| 696 | *end_of_object_name = saved; |
| 697 | |
| 698 | /* |
| 699 | * Verify that the action matches up with the one in |
| 700 | * opts; we don't support arbitrary instructions |
| 701 | */ |
| 702 | if (action != opts->action) { |
| Vasco Almeida | 9b0df09 | 2016-06-17 20:20:54 | [diff] [blame] | 703 | if (action == REPLAY_REVERT) |
| 704 | error((opts->action == REPLAY_REVERT) |
| Jean-Noel Avila | cd3e467 | 2016-08-21 14:50:37 | [diff] [blame] | 705 | ? _("Cannot revert during another revert.") |
| Vasco Almeida | 9b0df09 | 2016-06-17 20:20:54 | [diff] [blame] | 706 | : _("Cannot revert during a cherry-pick.")); |
| 707 | else |
| 708 | error((opts->action == REPLAY_REVERT) |
| 709 | ? _("Cannot cherry-pick during a revert.") |
| 710 | : _("Cannot cherry-pick during another cherry-pick.")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | if (status < 0) |
| 715 | return NULL; |
| 716 | |
| 717 | return lookup_commit_reference(commit_sha1); |
| 718 | } |
| 719 | |
| 720 | static int parse_insn_buffer(char *buf, struct commit_list **todo_list, |
| 721 | struct replay_opts *opts) |
| 722 | { |
| 723 | struct commit_list **next = todo_list; |
| 724 | struct commit *commit; |
| 725 | char *p = buf; |
| 726 | int i; |
| 727 | |
| 728 | for (i = 1; *p; i++) { |
| 729 | char *eol = strchrnul(p, '\n'); |
| 730 | commit = parse_insn_line(p, eol, opts); |
| 731 | if (!commit) |
| 732 | return error(_("Could not parse line %d."), i); |
| 733 | next = commit_list_append(commit, next); |
| 734 | p = *eol ? eol + 1 : eol; |
| 735 | } |
| 736 | if (!*todo_list) |
| 737 | return error(_("No commits parsed.")); |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static void read_populate_todo(struct commit_list **todo_list, |
| 742 | struct replay_opts *opts) |
| 743 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 744 | struct strbuf buf = STRBUF_INIT; |
| 745 | int fd, res; |
| 746 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 747 | fd = open(git_path_todo_file(), O_RDONLY); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 748 | if (fd < 0) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 749 | die_errno(_("Could not open %s"), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 750 | if (strbuf_read(&buf, fd, 0) < 0) { |
| 751 | close(fd); |
| 752 | strbuf_release(&buf); |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 753 | die(_("Could not read %s."), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 754 | } |
| 755 | close(fd); |
| 756 | |
| 757 | res = parse_insn_buffer(buf.buf, todo_list, opts); |
| 758 | strbuf_release(&buf); |
| 759 | if (res) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 760 | die(_("Unusable instruction sheet: %s"), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static int populate_opts_cb(const char *key, const char *value, void *data) |
| 764 | { |
| 765 | struct replay_opts *opts = data; |
| 766 | int error_flag = 1; |
| 767 | |
| 768 | if (!value) |
| 769 | error_flag = 0; |
| 770 | else if (!strcmp(key, "options.no-commit")) |
| 771 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); |
| 772 | else if (!strcmp(key, "options.edit")) |
| 773 | opts->edit = git_config_bool_or_int(key, value, &error_flag); |
| 774 | else if (!strcmp(key, "options.signoff")) |
| 775 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); |
| 776 | else if (!strcmp(key, "options.record-origin")) |
| 777 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); |
| 778 | else if (!strcmp(key, "options.allow-ff")) |
| 779 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); |
| 780 | else if (!strcmp(key, "options.mainline")) |
| 781 | opts->mainline = git_config_int(key, value); |
| 782 | else if (!strcmp(key, "options.strategy")) |
| 783 | git_config_string(&opts->strategy, key, value); |
| Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 | [diff] [blame] | 784 | else if (!strcmp(key, "options.gpg-sign")) |
| 785 | git_config_string(&opts->gpg_sign, key, value); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 786 | else if (!strcmp(key, "options.strategy-option")) { |
| 787 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
| 788 | opts->xopts[opts->xopts_nr++] = xstrdup(value); |
| 789 | } else |
| 790 | return error(_("Invalid key: %s"), key); |
| 791 | |
| 792 | if (!error_flag) |
| 793 | return error(_("Invalid value for %s: %s"), key, value); |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static void read_populate_opts(struct replay_opts **opts_ptr) |
| 799 | { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 800 | if (!file_exists(git_path_opts_file())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 801 | return; |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 802 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts_ptr) < 0) |
| 803 | die(_("Malformed options sheet: %s"), git_path_opts_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void walk_revs_populate_todo(struct commit_list **todo_list, |
| 807 | struct replay_opts *opts) |
| 808 | { |
| 809 | struct commit *commit; |
| 810 | struct commit_list **next; |
| 811 | |
| 812 | prepare_revs(opts); |
| 813 | |
| 814 | next = todo_list; |
| 815 | while ((commit = get_revision(opts->revs))) |
| 816 | next = commit_list_append(commit, next); |
| 817 | } |
| 818 | |
| 819 | static int create_seq_dir(void) |
| 820 | { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 821 | if (file_exists(git_path_seq_dir())) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 822 | error(_("a cherry-pick or revert is already in progress")); |
| 823 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); |
| 824 | return -1; |
| 825 | } |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 826 | else if (mkdir(git_path_seq_dir(), 0777) < 0) |
| 827 | die_errno(_("Could not create sequencer directory %s"), |
| 828 | git_path_seq_dir()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static void save_head(const char *head) |
| 833 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 834 | static struct lock_file head_lock; |
| 835 | struct strbuf buf = STRBUF_INIT; |
| 836 | int fd; |
| 837 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 838 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), LOCK_DIE_ON_ERROR); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 839 | strbuf_addf(&buf, "%s\n", head); |
| 840 | if (write_in_full(fd, buf.buf, buf.len) < 0) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 841 | die_errno(_("Could not write to %s"), git_path_head_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 842 | if (commit_lock_file(&head_lock) < 0) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 843 | die(_("Error wrapping up %s."), git_path_head_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static int reset_for_rollback(const unsigned char *sha1) |
| 847 | { |
| 848 | const char *argv[4]; /* reset --merge <arg> + NULL */ |
| 849 | argv[0] = "reset"; |
| 850 | argv[1] = "--merge"; |
| 851 | argv[2] = sha1_to_hex(sha1); |
| 852 | argv[3] = NULL; |
| 853 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 854 | } |
| 855 | |
| 856 | static int rollback_single_pick(void) |
| 857 | { |
| 858 | unsigned char head_sha1[20]; |
| 859 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 860 | if (!file_exists(git_path_cherry_pick_head()) && |
| 861 | !file_exists(git_path_revert_head())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 862 | return error(_("no cherry-pick or revert in progress")); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 863 | if (read_ref_full("HEAD", 0, head_sha1, NULL)) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 864 | return error(_("cannot resolve HEAD")); |
| 865 | if (is_null_sha1(head_sha1)) |
| 866 | return error(_("cannot abort from a branch yet to be born")); |
| 867 | return reset_for_rollback(head_sha1); |
| 868 | } |
| 869 | |
| 870 | static int sequencer_rollback(struct replay_opts *opts) |
| 871 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 872 | FILE *f; |
| 873 | unsigned char sha1[20]; |
| 874 | struct strbuf buf = STRBUF_INIT; |
| 875 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 876 | f = fopen(git_path_head_file(), "r"); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 877 | if (!f && errno == ENOENT) { |
| 878 | /* |
| 879 | * There is no multiple-cherry-pick in progress. |
| 880 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
| 881 | * a single-cherry-pick in progress, abort that. |
| 882 | */ |
| 883 | return rollback_single_pick(); |
| 884 | } |
| 885 | if (!f) |
| Nguyễn Thái Ngọc Duy | 6c979c7 | 2016-05-08 09:47:54 | [diff] [blame] | 886 | return error_errno(_("cannot open %s"), git_path_head_file()); |
| Junio C Hamano | 8f309ae | 2016-01-13 23:31:17 | [diff] [blame] | 887 | if (strbuf_getline_lf(&buf, f)) { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 888 | error(_("cannot read %s: %s"), git_path_head_file(), |
| 889 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 890 | fclose(f); |
| 891 | goto fail; |
| 892 | } |
| 893 | fclose(f); |
| 894 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { |
| 895 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 896 | git_path_head_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 897 | goto fail; |
| 898 | } |
| Michael J Gruber | 0f974e2 | 2016-06-06 13:23:54 | [diff] [blame] | 899 | if (is_null_sha1(sha1)) { |
| 900 | error(_("cannot abort from a branch yet to be born")); |
| 901 | goto fail; |
| 902 | } |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 903 | if (reset_for_rollback(sha1)) |
| 904 | goto fail; |
| 905 | remove_sequencer_state(); |
| 906 | strbuf_release(&buf); |
| 907 | return 0; |
| 908 | fail: |
| 909 | strbuf_release(&buf); |
| 910 | return -1; |
| 911 | } |
| 912 | |
| 913 | static void save_todo(struct commit_list *todo_list, struct replay_opts *opts) |
| 914 | { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 915 | static struct lock_file todo_lock; |
| 916 | struct strbuf buf = STRBUF_INIT; |
| 917 | int fd; |
| 918 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 919 | fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), LOCK_DIE_ON_ERROR); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 920 | if (format_todo(&buf, todo_list, opts) < 0) |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 921 | die(_("Could not format %s."), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 922 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 923 | strbuf_release(&buf); |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 924 | die_errno(_("Could not write to %s"), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 925 | } |
| 926 | if (commit_lock_file(&todo_lock) < 0) { |
| 927 | strbuf_release(&buf); |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 928 | die(_("Error wrapping up %s."), git_path_todo_file()); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 929 | } |
| 930 | strbuf_release(&buf); |
| 931 | } |
| 932 | |
| 933 | static void save_opts(struct replay_opts *opts) |
| 934 | { |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 935 | const char *opts_file = git_path_opts_file(); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 936 | |
| 937 | if (opts->no_commit) |
| 938 | git_config_set_in_file(opts_file, "options.no-commit", "true"); |
| 939 | if (opts->edit) |
| 940 | git_config_set_in_file(opts_file, "options.edit", "true"); |
| 941 | if (opts->signoff) |
| 942 | git_config_set_in_file(opts_file, "options.signoff", "true"); |
| 943 | if (opts->record_origin) |
| 944 | git_config_set_in_file(opts_file, "options.record-origin", "true"); |
| 945 | if (opts->allow_ff) |
| 946 | git_config_set_in_file(opts_file, "options.allow-ff", "true"); |
| 947 | if (opts->mainline) { |
| 948 | struct strbuf buf = STRBUF_INIT; |
| 949 | strbuf_addf(&buf, "%d", opts->mainline); |
| 950 | git_config_set_in_file(opts_file, "options.mainline", buf.buf); |
| 951 | strbuf_release(&buf); |
| 952 | } |
| 953 | if (opts->strategy) |
| 954 | git_config_set_in_file(opts_file, "options.strategy", opts->strategy); |
| Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 | [diff] [blame] | 955 | if (opts->gpg_sign) |
| 956 | git_config_set_in_file(opts_file, "options.gpg-sign", opts->gpg_sign); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 957 | if (opts->xopts) { |
| 958 | int i; |
| 959 | for (i = 0; i < opts->xopts_nr; i++) |
| 960 | git_config_set_multivar_in_file(opts_file, |
| 961 | "options.strategy-option", |
| 962 | opts->xopts[i], "^$", 0); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) |
| 967 | { |
| 968 | struct commit_list *cur; |
| 969 | int res; |
| 970 | |
| 971 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
| 972 | if (opts->allow_ff) |
| 973 | assert(!(opts->signoff || opts->no_commit || |
| 974 | opts->record_origin || opts->edit)); |
| 975 | read_and_refresh_cache(opts); |
| 976 | |
| 977 | for (cur = todo_list; cur; cur = cur->next) { |
| 978 | save_todo(cur, opts); |
| 979 | res = do_pick_commit(cur->item, opts); |
| 980 | if (res) |
| 981 | return res; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Sequence of picks finished successfully; cleanup by |
| 986 | * removing the .git/sequencer directory |
| 987 | */ |
| 988 | remove_sequencer_state(); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int continue_single_pick(void) |
| 993 | { |
| 994 | const char *argv[] = { "commit", NULL }; |
| 995 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 996 | if (!file_exists(git_path_cherry_pick_head()) && |
| 997 | !file_exists(git_path_revert_head())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 998 | return error(_("no cherry-pick or revert in progress")); |
| 999 | return run_command_v_opt(argv, RUN_GIT_CMD); |
| 1000 | } |
| 1001 | |
| 1002 | static int sequencer_continue(struct replay_opts *opts) |
| 1003 | { |
| 1004 | struct commit_list *todo_list = NULL; |
| 1005 | |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1006 | if (!file_exists(git_path_todo_file())) |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1007 | return continue_single_pick(); |
| 1008 | read_populate_opts(&opts); |
| 1009 | read_populate_todo(&todo_list, opts); |
| 1010 | |
| 1011 | /* Verify that the conflict has been resolved */ |
| Jeff King | f932729 | 2015-08-10 09:38:57 | [diff] [blame] | 1012 | if (file_exists(git_path_cherry_pick_head()) || |
| 1013 | file_exists(git_path_revert_head())) { |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1014 | int ret = continue_single_pick(); |
| 1015 | if (ret) |
| 1016 | return ret; |
| 1017 | } |
| 1018 | if (index_differs_from("HEAD", 0)) |
| 1019 | return error_dirty_index(opts); |
| 1020 | todo_list = todo_list->next; |
| 1021 | return pick_commits(todo_list, opts); |
| 1022 | } |
| 1023 | |
| 1024 | static int single_pick(struct commit *cmit, struct replay_opts *opts) |
| 1025 | { |
| 1026 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
| 1027 | return do_pick_commit(cmit, opts); |
| 1028 | } |
| 1029 | |
| 1030 | int sequencer_pick_revisions(struct replay_opts *opts) |
| 1031 | { |
| 1032 | struct commit_list *todo_list = NULL; |
| 1033 | unsigned char sha1[20]; |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1034 | int i; |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1035 | |
| 1036 | if (opts->subcommand == REPLAY_NONE) |
| 1037 | assert(opts->revs); |
| 1038 | |
| 1039 | read_and_refresh_cache(opts); |
| 1040 | |
| 1041 | /* |
| 1042 | * Decide what to do depending on the arguments; a fresh |
| 1043 | * cherry-pick should be handled differently from an existing |
| 1044 | * one that is being continued |
| 1045 | */ |
| 1046 | if (opts->subcommand == REPLAY_REMOVE_STATE) { |
| 1047 | remove_sequencer_state(); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | if (opts->subcommand == REPLAY_ROLLBACK) |
| 1051 | return sequencer_rollback(opts); |
| 1052 | if (opts->subcommand == REPLAY_CONTINUE) |
| 1053 | return sequencer_continue(opts); |
| 1054 | |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1055 | for (i = 0; i < opts->revs->pending.nr; i++) { |
| 1056 | unsigned char sha1[20]; |
| 1057 | const char *name = opts->revs->pending.objects[i].name; |
| 1058 | |
| 1059 | /* This happens when using --stdin. */ |
| 1060 | if (!strlen(name)) |
| 1061 | continue; |
| 1062 | |
| 1063 | if (!get_sha1(name, sha1)) { |
| Junio C Hamano | 7c0b0d8 | 2013-05-09 20:27:49 | [diff] [blame] | 1064 | if (!lookup_commit_reference_gently(sha1, 1)) { |
| 1065 | enum object_type type = sha1_object_info(sha1, NULL); |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1066 | die(_("%s: can't cherry-pick a %s"), name, typename(type)); |
| Junio C Hamano | 7c0b0d8 | 2013-05-09 20:27:49 | [diff] [blame] | 1067 | } |
| Miklos Vajna | 21246db | 2013-04-11 13:06:52 | [diff] [blame] | 1068 | } else |
| 1069 | die(_("%s: bad revision"), name); |
| 1070 | } |
| 1071 | |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1072 | /* |
| 1073 | * If we were called as "git cherry-pick <commit>", just |
| 1074 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / |
| 1075 | * REVERT_HEAD, and don't touch the sequencer state. |
| 1076 | * This means it is possible to cherry-pick in the middle |
| 1077 | * of a cherry-pick sequence. |
| 1078 | */ |
| 1079 | if (opts->revs->cmdline.nr == 1 && |
| 1080 | opts->revs->cmdline.rev->whence == REV_CMD_REV && |
| 1081 | opts->revs->no_walk && |
| 1082 | !opts->revs->cmdline.rev->flags) { |
| 1083 | struct commit *cmit; |
| 1084 | if (prepare_revision_walk(opts->revs)) |
| 1085 | die(_("revision walk setup failed")); |
| 1086 | cmit = get_revision(opts->revs); |
| 1087 | if (!cmit || get_revision(opts->revs)) |
| 1088 | die("BUG: expected exactly one commit from walk"); |
| 1089 | return single_pick(cmit, opts); |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Start a new cherry-pick/ revert sequence; but |
| 1094 | * first, make sure that an existing one isn't in |
| 1095 | * progress |
| 1096 | */ |
| 1097 | |
| 1098 | walk_revs_populate_todo(&todo_list, opts); |
| 1099 | if (create_seq_dir() < 0) |
| 1100 | return -1; |
| Michael J Gruber | 0f974e2 | 2016-06-06 13:23:54 | [diff] [blame] | 1101 | if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT)) |
| 1102 | return error(_("Can't revert as initial commit")); |
| Ramkumar Ramachandra | 043a449 | 2012-01-11 18:15:57 | [diff] [blame] | 1103 | save_head(sha1_to_hex(sha1)); |
| 1104 | save_opts(opts); |
| 1105 | return pick_commits(todo_list, opts); |
| 1106 | } |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1107 | |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1108 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1109 | { |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1110 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1111 | struct strbuf sob = STRBUF_INIT; |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1112 | int has_footer; |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1113 | |
| 1114 | strbuf_addstr(&sob, sign_off_header); |
| 1115 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
| 1116 | getenv("GIT_COMMITTER_EMAIL"))); |
| 1117 | strbuf_addch(&sob, '\n'); |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * If the whole message buffer is equal to the sob, pretend that we |
| 1121 | * found a conforming footer with a matching sob |
| 1122 | */ |
| 1123 | if (msgbuf->len - ignore_footer == sob.len && |
| 1124 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
| 1125 | has_footer = 3; |
| 1126 | else |
| 1127 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
| 1128 | |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1129 | if (!has_footer) { |
| 1130 | const char *append_newlines = NULL; |
| 1131 | size_t len = msgbuf->len - ignore_footer; |
| 1132 | |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1133 | if (!len) { |
| 1134 | /* |
| 1135 | * The buffer is completely empty. Leave foom for |
| 1136 | * the title and body to be filled in by the user. |
| 1137 | */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1138 | append_newlines = "\n\n"; |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1139 | } else if (msgbuf->buf[len - 1] != '\n') { |
| 1140 | /* |
| 1141 | * Incomplete line. Complete the line and add a |
| 1142 | * blank one so that there is an empty line between |
| 1143 | * the message body and the sob. |
| 1144 | */ |
| 1145 | append_newlines = "\n\n"; |
| 1146 | } else if (len == 1) { |
| 1147 | /* |
| 1148 | * Buffer contains a single newline. Add another |
| 1149 | * so that we leave room for the title and body. |
| 1150 | */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1151 | append_newlines = "\n"; |
| Brandon Casey | 8c613fd | 2013-02-22 22:05:27 | [diff] [blame] | 1152 | } else if (msgbuf->buf[len - 2] != '\n') { |
| 1153 | /* |
| 1154 | * Buffer ends with a single newline. Add another |
| 1155 | * so that there is an empty line between the message |
| 1156 | * body and the sob. |
| 1157 | */ |
| 1158 | append_newlines = "\n"; |
| 1159 | } /* else, the buffer already ends with two newlines. */ |
| Brandon Casey | 33f2f9a | 2013-02-12 10:33:42 | [diff] [blame] | 1160 | |
| 1161 | if (append_newlines) |
| 1162 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1163 | append_newlines, strlen(append_newlines)); |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1164 | } |
| Brandon Casey | bab4d10 | 2013-02-12 10:17:35 | [diff] [blame] | 1165 | |
| 1166 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
| 1167 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1168 | sob.buf, sob.len); |
| 1169 | |
| Miklos Vajna | 5ed75e2 | 2012-09-14 06:52:03 | [diff] [blame] | 1170 | strbuf_release(&sob); |
| 1171 | } |