Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "sequencer.h" |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 3 | #include "dir.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 4 | #include "object.h" |
| 5 | #include "commit.h" |
| 6 | #include "tag.h" |
| 7 | #include "run-command.h" |
| 8 | #include "exec_cmd.h" |
| 9 | #include "utf8.h" |
| 10 | #include "cache-tree.h" |
| 11 | #include "diff.h" |
| 12 | #include "revision.h" |
| 13 | #include "rerere.h" |
| 14 | #include "merge-recursive.h" |
| 15 | #include "refs.h" |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 16 | #include "argv-array.h" |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 17 | |
| 18 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 19 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 20 | const char sign_off_header[] = "Signed-off-by: "; |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 21 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 22 | |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 23 | static int is_rfc2822_line(const char *buf, int len) |
| 24 | { |
| 25 | int i; |
| 26 | |
| 27 | for (i = 0; i < len; i++) { |
| 28 | int ch = buf[i]; |
| 29 | if (ch == ':') |
| 30 | return 1; |
| 31 | if (!isalnum(ch) && ch != '-') |
| 32 | break; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int is_cherry_picked_from_line(const char *buf, int len) |
| 39 | { |
| 40 | /* |
| 41 | * We only care that it looks roughly like (cherry picked from ...) |
| 42 | */ |
| 43 | return len > strlen(cherry_picked_prefix) + 1 && |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 44 | starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')'; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 47 | /* |
| 48 | * Returns 0 for non-conforming footer |
| 49 | * Returns 1 for conforming footer |
| 50 | * Returns 2 when sob exists within conforming footer |
| 51 | * Returns 3 when sob exists within conforming footer as last entry |
| 52 | */ |
| 53 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, |
| 54 | int ignore_footer) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 55 | { |
| 56 | char prev; |
| 57 | int i, k; |
| 58 | int len = sb->len - ignore_footer; |
| 59 | const char *buf = sb->buf; |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 60 | int found_sob = 0; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 61 | |
| 62 | /* footer must end with newline */ |
| 63 | if (!len || buf[len - 1] != '\n') |
| 64 | return 0; |
| 65 | |
| 66 | prev = '\0'; |
| 67 | for (i = len - 1; i > 0; i--) { |
| 68 | char ch = buf[i]; |
| 69 | if (prev == '\n' && ch == '\n') /* paragraph break */ |
| 70 | break; |
| 71 | prev = ch; |
| 72 | } |
| 73 | |
| 74 | /* require at least one blank line */ |
| 75 | if (prev != '\n' || buf[i] != '\n') |
| 76 | return 0; |
| 77 | |
| 78 | /* advance to start of last paragraph */ |
| 79 | while (i < len - 1 && buf[i] == '\n') |
| 80 | i++; |
| 81 | |
| 82 | for (; i < len; i = k) { |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 83 | int found_rfc2822; |
| 84 | |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 85 | for (k = i; k < len && buf[k] != '\n'; k++) |
| 86 | ; /* do nothing */ |
| 87 | k++; |
| 88 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 89 | found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1); |
| 90 | if (found_rfc2822 && sob && |
| 91 | !strncmp(buf + i, sob->buf, sob->len)) |
| 92 | found_sob = k; |
| 93 | |
| 94 | if (!(found_rfc2822 || |
| 95 | is_cherry_picked_from_line(buf + i, k - i - 1))) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 96 | return 0; |
| 97 | } |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 98 | if (found_sob == i) |
| 99 | return 3; |
| 100 | if (found_sob) |
| 101 | return 2; |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 102 | return 1; |
| 103 | } |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 104 | |
Junio C Hamano | 250f249 | 2012-09-15 23:20:40 -0700 | [diff] [blame] | 105 | static void remove_sequencer_state(void) |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 106 | { |
| 107 | struct strbuf seq_dir = STRBUF_INIT; |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 108 | |
| 109 | strbuf_addf(&seq_dir, "%s", git_path(SEQ_DIR)); |
Jonathan Nieder | d596118 | 2011-12-10 07:06:12 -0600 | [diff] [blame] | 110 | remove_dir_recursively(&seq_dir, 0); |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 111 | strbuf_release(&seq_dir); |
Ramkumar Ramachandra | 26ae337 | 2011-08-04 16:09:11 +0530 | [diff] [blame] | 112 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 113 | |
| 114 | static const char *action_name(const struct replay_opts *opts) |
| 115 | { |
| 116 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
| 117 | } |
| 118 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 119 | struct commit_message { |
| 120 | char *parent_label; |
| 121 | const char *label; |
| 122 | const char *subject; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 123 | const char *message; |
| 124 | }; |
| 125 | |
| 126 | static int get_message(struct commit *commit, struct commit_message *out) |
| 127 | { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 128 | const char *abbrev, *subject; |
| 129 | int abbrev_len, subject_len; |
| 130 | char *q; |
| 131 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 132 | if (!git_commit_encoding) |
| 133 | git_commit_encoding = "UTF-8"; |
| 134 | |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 135 | out->message = logmsg_reencode(commit, NULL, git_commit_encoding); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 136 | abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); |
| 137 | abbrev_len = strlen(abbrev); |
| 138 | |
| 139 | subject_len = find_commit_subject(out->message, &subject); |
| 140 | |
| 141 | out->parent_label = xmalloc(strlen("parent of ") + abbrev_len + |
| 142 | strlen("... ") + subject_len + 1); |
| 143 | q = out->parent_label; |
| 144 | q = mempcpy(q, "parent of ", strlen("parent of ")); |
| 145 | out->label = q; |
| 146 | q = mempcpy(q, abbrev, abbrev_len); |
| 147 | q = mempcpy(q, "... ", strlen("... ")); |
| 148 | out->subject = q; |
| 149 | q = mempcpy(q, subject, subject_len); |
| 150 | *q = '\0'; |
| 151 | return 0; |
| 152 | } |
| 153 | |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 154 | static void free_message(struct commit *commit, struct commit_message *msg) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 155 | { |
| 156 | free(msg->parent_label); |
Jeff King | b66103c | 2014-06-10 17:41:39 -0400 | [diff] [blame] | 157 | unuse_commit_buffer(commit, msg->message); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void write_cherry_pick_head(struct commit *commit, const char *pseudoref) |
| 161 | { |
| 162 | const char *filename; |
| 163 | int fd; |
| 164 | struct strbuf buf = STRBUF_INIT; |
| 165 | |
| 166 | strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1)); |
| 167 | |
| 168 | filename = git_path("%s", pseudoref); |
| 169 | fd = open(filename, O_WRONLY | O_CREAT, 0666); |
| 170 | if (fd < 0) |
| 171 | die_errno(_("Could not open '%s' for writing"), filename); |
| 172 | if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd)) |
| 173 | die_errno(_("Could not write to '%s'"), filename); |
| 174 | strbuf_release(&buf); |
| 175 | } |
| 176 | |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 177 | static void print_advice(int show_hint, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 178 | { |
| 179 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 180 | |
| 181 | if (msg) { |
| 182 | fprintf(stderr, "%s\n", msg); |
| 183 | /* |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 184 | * A conflict has occurred but the porcelain |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 185 | * (typically rebase --interactive) wants to take care |
| 186 | * of the commit itself so remove CHERRY_PICK_HEAD |
| 187 | */ |
| 188 | unlink(git_path("CHERRY_PICK_HEAD")); |
| 189 | return; |
| 190 | } |
| 191 | |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 192 | if (show_hint) { |
| 193 | if (opts->no_commit) |
| 194 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 195 | "with 'git add <paths>' or 'git rm <paths>'")); |
| 196 | else |
| 197 | advise(_("after resolving the conflicts, mark the corrected paths\n" |
| 198 | "with 'git add <paths>' or 'git rm <paths>'\n" |
| 199 | "and commit the result with 'git commit'")); |
| 200 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void write_message(struct strbuf *msgbuf, const char *filename) |
| 204 | { |
| 205 | static struct lock_file msg_file; |
| 206 | |
| 207 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, |
| 208 | LOCK_DIE_ON_ERROR); |
| 209 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) |
| 210 | die_errno(_("Could not write to %s"), filename); |
| 211 | strbuf_release(msgbuf); |
| 212 | if (commit_lock_file(&msg_file) < 0) |
| 213 | die(_("Error wrapping up %s"), filename); |
| 214 | } |
| 215 | |
| 216 | static struct tree *empty_tree(void) |
| 217 | { |
Jeff King | cba595b | 2012-03-22 14:53:24 -0400 | [diff] [blame] | 218 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int error_dirty_index(struct replay_opts *opts) |
| 222 | { |
| 223 | if (read_cache_unmerged()) |
| 224 | return error_resolve_conflict(action_name(opts)); |
| 225 | |
| 226 | /* Different translation strings for cherry-pick and revert */ |
| 227 | if (opts->action == REPLAY_PICK) |
| 228 | error(_("Your local changes would be overwritten by cherry-pick.")); |
| 229 | else |
| 230 | error(_("Your local changes would be overwritten by revert.")); |
| 231 | |
| 232 | if (advice_commit_before_merge) |
| 233 | advise(_("Commit your changes or stash them to proceed.")); |
| 234 | return -1; |
| 235 | } |
| 236 | |
Martin von Zweigbergk | 334ae39 | 2012-12-21 11:10:11 -0800 | [diff] [blame] | 237 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 238 | int unborn, struct replay_opts *opts) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 239 | { |
| 240 | struct ref_lock *ref_lock; |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 241 | struct strbuf sb = STRBUF_INIT; |
| 242 | int ret; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 243 | |
| 244 | read_cache(); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 245 | if (checkout_fast_forward(from, to, 1)) |
Fabian Ruch | 43dee07 | 2014-06-09 17:04:36 +0200 | [diff] [blame] | 246 | exit(128); /* the callee should have complained already */ |
Brad King | 9bbb0fa | 2013-08-30 14:12:00 -0400 | [diff] [blame] | 247 | ref_lock = lock_any_ref_for_update("HEAD", unborn ? null_sha1 : from, |
| 248 | 0, NULL); |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 249 | if (!ref_lock) |
| 250 | return error(_("Failed to lock HEAD during fast_forward_to")); |
| 251 | |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 252 | strbuf_addf(&sb, "%s: fast-forward", action_name(opts)); |
| 253 | ret = write_ref_sha1(ref_lock, to, sb.buf); |
Ronnie Sahlberg | 651ab9f | 2014-04-16 11:56:52 -0700 | [diff] [blame] | 254 | |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 255 | strbuf_release(&sb); |
| 256 | return ret; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int do_recursive_merge(struct commit *base, struct commit *next, |
| 260 | const char *base_label, const char *next_label, |
| 261 | unsigned char *head, struct strbuf *msgbuf, |
| 262 | struct replay_opts *opts) |
| 263 | { |
| 264 | struct merge_options o; |
| 265 | struct tree *result, *next_tree, *base_tree, *head_tree; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 266 | int clean; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 267 | const char **xopt; |
| 268 | static struct lock_file index_lock; |
| 269 | |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 270 | hold_locked_index(&index_lock, 1); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 271 | |
| 272 | read_cache(); |
| 273 | |
| 274 | init_merge_options(&o); |
| 275 | o.ancestor = base ? base_label : "(empty tree)"; |
| 276 | o.branch1 = "HEAD"; |
| 277 | o.branch2 = next ? next_label : "(empty tree)"; |
| 278 | |
| 279 | head_tree = parse_tree_indirect(head); |
| 280 | next_tree = next ? next->tree : empty_tree(); |
| 281 | base_tree = base ? base->tree : empty_tree(); |
| 282 | |
| 283 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
| 284 | parse_merge_opt(&o, *xopt); |
| 285 | |
| 286 | clean = merge_trees(&o, |
| 287 | head_tree, |
| 288 | next_tree, base_tree, &result); |
| 289 | |
| 290 | if (active_cache_changed && |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 291 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 292 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
| 293 | die(_("%s: Unable to write new index file"), action_name(opts)); |
| 294 | rollback_lock_file(&index_lock); |
| 295 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 296 | if (opts->signoff) |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 297 | append_signoff(msgbuf, 0, 0); |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 298 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 299 | if (!clean) { |
| 300 | int i; |
Ralf Thielow | 5112068 | 2012-04-18 23:07:25 +0200 | [diff] [blame] | 301 | strbuf_addstr(msgbuf, "\nConflicts:\n"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 302 | for (i = 0; i < active_nr;) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 303 | const struct cache_entry *ce = active_cache[i++]; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 304 | if (ce_stage(ce)) { |
| 305 | strbuf_addch(msgbuf, '\t'); |
| 306 | strbuf_addstr(msgbuf, ce->name); |
| 307 | strbuf_addch(msgbuf, '\n'); |
| 308 | while (i < active_nr && !strcmp(ce->name, |
| 309 | active_cache[i]->name)) |
| 310 | i++; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return !clean; |
| 316 | } |
| 317 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 318 | static int is_index_unchanged(void) |
| 319 | { |
| 320 | unsigned char head_sha1[20]; |
| 321 | struct commit *head_commit; |
| 322 | |
| 323 | if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL)) |
| 324 | return error(_("Could not resolve HEAD commit\n")); |
| 325 | |
| 326 | head_commit = lookup_commit(head_sha1); |
Neil Horman | 4b58006 | 2012-05-03 08:10:22 -0400 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * If head_commit is NULL, check_commit, called from |
| 330 | * lookup_commit, would have indicated that head_commit is not |
| 331 | * a commit object already. parse_commit() will return failure |
| 332 | * without further complaints in such a case. Otherwise, if |
| 333 | * the commit is invalid, parse_commit() will complain. So |
| 334 | * there is nothing for us to say here. Just return failure. |
| 335 | */ |
| 336 | if (parse_commit(head_commit)) |
| 337 | return -1; |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 338 | |
| 339 | if (!active_cache_tree) |
| 340 | active_cache_tree = cache_tree(); |
| 341 | |
| 342 | if (!cache_tree_fully_valid(active_cache_tree)) |
Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 19:19:32 +0700 | [diff] [blame] | 343 | if (cache_tree_update(&the_index, 0)) |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 344 | return error(_("Unable to update cache tree\n")); |
| 345 | |
| 346 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.sha1); |
| 347 | } |
| 348 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 349 | /* |
| 350 | * If we are cherry-pick, and if the merge did not result in |
| 351 | * hand-editing, we will hit this commit and inherit the original |
| 352 | * author date and name. |
| 353 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 354 | * we had better say that the current user is responsible for that. |
| 355 | */ |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 356 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
| 357 | int allow_empty) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 358 | { |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 359 | struct argv_array array; |
| 360 | int rc; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 361 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 362 | argv_array_init(&array); |
| 363 | argv_array_push(&array, "commit"); |
| 364 | argv_array_push(&array, "-n"); |
| 365 | |
Jeff King | 3bdd552 | 2014-06-19 17:28:20 -0400 | [diff] [blame] | 366 | if (opts->gpg_sign) |
| 367 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 368 | if (opts->signoff) |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 369 | argv_array_push(&array, "-s"); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 370 | if (!opts->edit) { |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 371 | argv_array_push(&array, "-F"); |
| 372 | argv_array_push(&array, defmsg); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 373 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 374 | |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 375 | if (allow_empty) |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 376 | argv_array_push(&array, "--allow-empty"); |
Neil Horman | df478b7 | 2012-04-11 16:21:53 -0400 | [diff] [blame] | 377 | |
Chris Webb | 4bee958 | 2012-08-02 11:38:51 +0100 | [diff] [blame] | 378 | if (opts->allow_empty_message) |
| 379 | argv_array_push(&array, "--allow-empty-message"); |
| 380 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 381 | rc = run_command_v_opt(array.argv, RUN_GIT_CMD); |
| 382 | argv_array_clear(&array); |
| 383 | return rc; |
| 384 | } |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 385 | |
Neil Horman | b27cfb0 | 2012-04-20 10:36:15 -0400 | [diff] [blame] | 386 | static int is_original_commit_empty(struct commit *commit) |
| 387 | { |
| 388 | const unsigned char *ptree_sha1; |
| 389 | |
| 390 | if (parse_commit(commit)) |
| 391 | return error(_("Could not parse commit %s\n"), |
| 392 | sha1_to_hex(commit->object.sha1)); |
| 393 | if (commit->parents) { |
| 394 | struct commit *parent = commit->parents->item; |
| 395 | if (parse_commit(parent)) |
| 396 | return error(_("Could not parse parent commit %s\n"), |
| 397 | sha1_to_hex(parent->object.sha1)); |
| 398 | ptree_sha1 = parent->tree->object.sha1; |
| 399 | } else { |
| 400 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ |
| 401 | } |
| 402 | |
| 403 | return !hashcmp(ptree_sha1, commit->tree->object.sha1); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 404 | } |
| 405 | |
Junio C Hamano | ac2b0e8 | 2012-05-29 17:14:41 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Do we run "git commit" with "--allow-empty"? |
| 408 | */ |
| 409 | static int allow_empty(struct replay_opts *opts, struct commit *commit) |
| 410 | { |
| 411 | int index_unchanged, empty_commit; |
| 412 | |
| 413 | /* |
| 414 | * Three cases: |
| 415 | * |
| 416 | * (1) we do not allow empty at all and error out. |
| 417 | * |
| 418 | * (2) we allow ones that were initially empty, but |
| 419 | * forbid the ones that become empty; |
| 420 | * |
| 421 | * (3) we allow both. |
| 422 | */ |
| 423 | if (!opts->allow_empty) |
| 424 | return 0; /* let "git commit" barf as necessary */ |
| 425 | |
| 426 | index_unchanged = is_index_unchanged(); |
| 427 | if (index_unchanged < 0) |
| 428 | return index_unchanged; |
| 429 | if (!index_unchanged) |
| 430 | return 0; /* we do not have to say --allow-empty */ |
| 431 | |
| 432 | if (opts->keep_redundant_commits) |
| 433 | return 1; |
| 434 | |
| 435 | empty_commit = is_original_commit_empty(commit); |
| 436 | if (empty_commit < 0) |
| 437 | return empty_commit; |
| 438 | if (!empty_commit) |
| 439 | return 0; |
| 440 | else |
| 441 | return 1; |
| 442 | } |
| 443 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 444 | static int do_pick_commit(struct commit *commit, struct replay_opts *opts) |
| 445 | { |
| 446 | unsigned char head[20]; |
| 447 | struct commit *base, *next, *parent; |
| 448 | const char *base_label, *next_label; |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 449 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 450 | char *defmsg = NULL; |
| 451 | struct strbuf msgbuf = STRBUF_INIT; |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 452 | int res, unborn = 0, allow; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 453 | |
| 454 | if (opts->no_commit) { |
| 455 | /* |
| 456 | * We do not intend to commit immediately. We just want to |
| 457 | * merge the differences in, so let's compute the tree |
| 458 | * that represents the "current" state for merge-recursive |
| 459 | * to work on. |
| 460 | */ |
| 461 | if (write_cache_as_tree(head, 0, NULL)) |
| 462 | die (_("Your index file is unmerged.")); |
| 463 | } else { |
Martin von Zweigbergk | 334ae39 | 2012-12-21 11:10:11 -0800 | [diff] [blame] | 464 | unborn = get_sha1("HEAD", head); |
| 465 | if (unborn) |
| 466 | hashcpy(head, EMPTY_TREE_SHA1_BIN); |
| 467 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 468 | return error_dirty_index(opts); |
| 469 | } |
| 470 | discard_cache(); |
| 471 | |
| 472 | if (!commit->parents) { |
| 473 | parent = NULL; |
| 474 | } |
| 475 | else if (commit->parents->next) { |
| 476 | /* Reverting or cherry-picking a merge commit */ |
| 477 | int cnt; |
| 478 | struct commit_list *p; |
| 479 | |
| 480 | if (!opts->mainline) |
| 481 | return error(_("Commit %s is a merge but no -m option was given."), |
| 482 | sha1_to_hex(commit->object.sha1)); |
| 483 | |
| 484 | for (cnt = 1, p = commit->parents; |
| 485 | cnt != opts->mainline && p; |
| 486 | cnt++) |
| 487 | p = p->next; |
| 488 | if (cnt != opts->mainline || !p) |
| 489 | return error(_("Commit %s does not have parent %d"), |
| 490 | sha1_to_hex(commit->object.sha1), opts->mainline); |
| 491 | parent = p->item; |
| 492 | } else if (0 < opts->mainline) |
| 493 | return error(_("Mainline was specified but commit %s is not a merge."), |
| 494 | sha1_to_hex(commit->object.sha1)); |
| 495 | else |
| 496 | parent = commit->parents->item; |
| 497 | |
Martin von Zweigbergk | 334ae39 | 2012-12-21 11:10:11 -0800 | [diff] [blame] | 498 | if (opts->allow_ff && |
| 499 | ((parent && !hashcmp(parent->object.sha1, head)) || |
| 500 | (!parent && unborn))) |
Ramkumar Ramachandra | eb4be1c | 2013-06-19 13:07:09 +0530 | [diff] [blame] | 501 | return fast_forward_to(commit->object.sha1, head, unborn, opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 502 | |
| 503 | if (parent && parse_commit(parent) < 0) |
| 504 | /* TRANSLATORS: The first %s will be "revert" or |
| 505 | "cherry-pick", the second %s a SHA1 */ |
| 506 | return error(_("%s: cannot parse parent commit %s"), |
| 507 | action_name(opts), sha1_to_hex(parent->object.sha1)); |
| 508 | |
| 509 | if (get_message(commit, &msg) != 0) |
| 510 | return error(_("Cannot get commit message for %s"), |
| 511 | sha1_to_hex(commit->object.sha1)); |
| 512 | |
| 513 | /* |
| 514 | * "commit" is an existing commit. We would want to apply |
| 515 | * the difference it introduces since its first parent "prev" |
| 516 | * on top of the current HEAD if we are cherry-pick. Or the |
| 517 | * reverse of it if we are revert. |
| 518 | */ |
| 519 | |
| 520 | defmsg = git_pathdup("MERGE_MSG"); |
| 521 | |
| 522 | if (opts->action == REPLAY_REVERT) { |
| 523 | base = commit; |
| 524 | base_label = msg.label; |
| 525 | next = parent; |
| 526 | next_label = msg.parent_label; |
| 527 | strbuf_addstr(&msgbuf, "Revert \""); |
| 528 | strbuf_addstr(&msgbuf, msg.subject); |
| 529 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); |
| 530 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); |
| 531 | |
| 532 | if (commit->parents && commit->parents->next) { |
| 533 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
| 534 | strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1)); |
| 535 | } |
| 536 | strbuf_addstr(&msgbuf, ".\n"); |
| 537 | } else { |
| 538 | const char *p; |
| 539 | |
| 540 | base = parent; |
| 541 | base_label = msg.parent_label; |
| 542 | next = commit; |
| 543 | next_label = msg.label; |
| 544 | |
| 545 | /* |
| 546 | * Append the commit log message to msgbuf; it starts |
| 547 | * after the tree, parent, author, committer |
| 548 | * information followed by "\n\n". |
| 549 | */ |
| 550 | p = strstr(msg.message, "\n\n"); |
| 551 | if (p) { |
| 552 | p += 2; |
| 553 | strbuf_addstr(&msgbuf, p); |
| 554 | } |
| 555 | |
| 556 | if (opts->record_origin) { |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 557 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
Brandon Casey | b971e04 | 2013-02-12 02:17:34 -0800 | [diff] [blame] | 558 | strbuf_addch(&msgbuf, '\n'); |
Brandon Casey | cd650a4 | 2013-02-12 02:17:32 -0800 | [diff] [blame] | 559 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 560 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); |
| 561 | strbuf_addstr(&msgbuf, ")\n"); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) { |
| 566 | res = do_recursive_merge(base, next, base_label, next_label, |
| 567 | head, &msgbuf, opts); |
| 568 | write_message(&msgbuf, defmsg); |
| 569 | } else { |
| 570 | struct commit_list *common = NULL; |
| 571 | struct commit_list *remotes = NULL; |
| 572 | |
| 573 | write_message(&msgbuf, defmsg); |
| 574 | |
| 575 | commit_list_insert(base, &common); |
| 576 | commit_list_insert(next, &remotes); |
| 577 | res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts, |
| 578 | common, sha1_to_hex(head), remotes); |
| 579 | free_commit_list(common); |
| 580 | free_commit_list(remotes); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * If the merge was clean or if it failed due to conflict, we write |
| 585 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. |
| 586 | * However, if the merge did not even start, then we don't want to |
| 587 | * write it at all. |
| 588 | */ |
| 589 | if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1)) |
| 590 | write_cherry_pick_head(commit, "CHERRY_PICK_HEAD"); |
| 591 | if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1)) |
| 592 | write_cherry_pick_head(commit, "REVERT_HEAD"); |
| 593 | |
| 594 | if (res) { |
| 595 | error(opts->action == REPLAY_REVERT |
| 596 | ? _("could not revert %s... %s") |
| 597 | : _("could not apply %s... %s"), |
| 598 | find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), |
| 599 | msg.subject); |
Phil Hord | ed727b1 | 2012-02-21 19:44:17 -0500 | [diff] [blame] | 600 | print_advice(res == 1, opts); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 601 | rerere(opts->allow_rerere_auto); |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 602 | goto leave; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 603 | } |
| 604 | |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 605 | allow = allow_empty(opts, commit); |
Felipe Contreras | 706728a | 2013-06-06 03:58:57 -0500 | [diff] [blame] | 606 | if (allow < 0) { |
| 607 | res = allow; |
| 608 | goto leave; |
| 609 | } |
Felipe Contreras | c8d1351 | 2013-05-28 22:56:21 -0500 | [diff] [blame] | 610 | if (!opts->no_commit) |
| 611 | res = run_git_commit(defmsg, opts, allow); |
| 612 | |
| 613 | leave: |
Jeff King | d74a4e5 | 2014-06-10 17:39:35 -0400 | [diff] [blame] | 614 | free_message(commit, &msg); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 615 | free(defmsg); |
| 616 | |
| 617 | return res; |
| 618 | } |
| 619 | |
| 620 | static void prepare_revs(struct replay_opts *opts) |
| 621 | { |
Martin von Zweigbergk | a73e22e | 2012-08-28 23:15:56 -0700 | [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 23:45:57 +0530 | [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 17:55:24 +0700 | [diff] [blame] | 643 | if (the_index.cache_changed && index_fd >= 0) { |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 644 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [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 23:45:57 +0530 | [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 17:44:13 -0400 | [diff] [blame] | 660 | const char *commit_buffer = get_commit_buffer(cur->item, NULL); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 661 | sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV); |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 662 | subject_len = find_commit_subject(commit_buffer, &subject); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 663 | strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev, |
| 664 | subject_len, subject); |
Jeff King | bc6b8fc | 2014-06-10 17:41:51 -0400 | [diff] [blame] | 665 | unuse_commit_buffer(cur->item, commit_buffer); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [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 21:55:40 +0100 | [diff] [blame] | 677 | if (starts_with(bol, "pick")) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 678 | action = REPLAY_PICK; |
| 679 | bol += strlen("pick"); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 680 | } else if (starts_with(bol, "revert")) { |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [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) { |
| 703 | const char *action_str; |
| 704 | action_str = action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
| 705 | error(_("Cannot %s during a %s"), action_str, action_name(opts)); |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | if (status < 0) |
| 710 | return NULL; |
| 711 | |
| 712 | return lookup_commit_reference(commit_sha1); |
| 713 | } |
| 714 | |
| 715 | static int parse_insn_buffer(char *buf, struct commit_list **todo_list, |
| 716 | struct replay_opts *opts) |
| 717 | { |
| 718 | struct commit_list **next = todo_list; |
| 719 | struct commit *commit; |
| 720 | char *p = buf; |
| 721 | int i; |
| 722 | |
| 723 | for (i = 1; *p; i++) { |
| 724 | char *eol = strchrnul(p, '\n'); |
| 725 | commit = parse_insn_line(p, eol, opts); |
| 726 | if (!commit) |
| 727 | return error(_("Could not parse line %d."), i); |
| 728 | next = commit_list_append(commit, next); |
| 729 | p = *eol ? eol + 1 : eol; |
| 730 | } |
| 731 | if (!*todo_list) |
| 732 | return error(_("No commits parsed.")); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static void read_populate_todo(struct commit_list **todo_list, |
| 737 | struct replay_opts *opts) |
| 738 | { |
| 739 | const char *todo_file = git_path(SEQ_TODO_FILE); |
| 740 | struct strbuf buf = STRBUF_INIT; |
| 741 | int fd, res; |
| 742 | |
| 743 | fd = open(todo_file, O_RDONLY); |
| 744 | if (fd < 0) |
| 745 | die_errno(_("Could not open %s"), todo_file); |
| 746 | if (strbuf_read(&buf, fd, 0) < 0) { |
| 747 | close(fd); |
| 748 | strbuf_release(&buf); |
| 749 | die(_("Could not read %s."), todo_file); |
| 750 | } |
| 751 | close(fd); |
| 752 | |
| 753 | res = parse_insn_buffer(buf.buf, todo_list, opts); |
| 754 | strbuf_release(&buf); |
| 755 | if (res) |
| 756 | die(_("Unusable instruction sheet: %s"), todo_file); |
| 757 | } |
| 758 | |
| 759 | static int populate_opts_cb(const char *key, const char *value, void *data) |
| 760 | { |
| 761 | struct replay_opts *opts = data; |
| 762 | int error_flag = 1; |
| 763 | |
| 764 | if (!value) |
| 765 | error_flag = 0; |
| 766 | else if (!strcmp(key, "options.no-commit")) |
| 767 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); |
| 768 | else if (!strcmp(key, "options.edit")) |
| 769 | opts->edit = git_config_bool_or_int(key, value, &error_flag); |
| 770 | else if (!strcmp(key, "options.signoff")) |
| 771 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); |
| 772 | else if (!strcmp(key, "options.record-origin")) |
| 773 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); |
| 774 | else if (!strcmp(key, "options.allow-ff")) |
| 775 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); |
| 776 | else if (!strcmp(key, "options.mainline")) |
| 777 | opts->mainline = git_config_int(key, value); |
| 778 | else if (!strcmp(key, "options.strategy")) |
| 779 | git_config_string(&opts->strategy, key, value); |
Nicolas Vigier | 3253553 | 2014-01-24 00:50:58 +0000 | [diff] [blame] | 780 | else if (!strcmp(key, "options.gpg-sign")) |
| 781 | git_config_string(&opts->gpg_sign, key, value); |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [diff] [blame] | 782 | else if (!strcmp(key, "options.strategy-option")) { |
| 783 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
| 784 | opts->xopts[opts->xopts_nr++] = xstrdup(value); |
| 785 | } else |
| 786 | return error(_("Invalid key: %s"), key); |
| 787 | |
| 788 | if (!error_flag) |
| 789 | return error(_("Invalid value for %s: %s"), key, value); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static void read_populate_opts(struct replay_opts **opts_ptr) |
| 795 | { |
| 796 | const char *opts_file = git_path(SEQ_OPTS_FILE); |
| 797 | |
| 798 | if (!file_exists(opts_file)) |
| 799 | return; |
| 800 | if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0) |
| 801 | die(_("Malformed options sheet: %s"), opts_file); |
| 802 | } |
| 803 | |
| 804 | static void walk_revs_populate_todo(struct commit_list **todo_list, |
| 805 | struct replay_opts *opts) |
| 806 | { |
| 807 | struct commit *commit; |
| 808 | struct commit_list **next; |
| 809 | |
| 810 | prepare_revs(opts); |
| 811 | |
| 812 | next = todo_list; |
| 813 | while ((commit = get_revision(opts->revs))) |
| 814 | next = commit_list_append(commit, next); |
| 815 | } |
| 816 | |
| 817 | static int create_seq_dir(void) |
| 818 | { |
| 819 | const char *seq_dir = git_path(SEQ_DIR); |
| 820 | |
| 821 | if (file_exists(seq_dir)) { |
| 822 | error(_("a cherry-pick or revert is already in progress")); |
| 823 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); |
| 824 | return -1; |
| 825 | } |
| 826 | else if (mkdir(seq_dir, 0777) < 0) |
| 827 | die_errno(_("Could not create sequencer directory %s"), seq_dir); |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static void save_head(const char *head) |
| 832 | { |
| 833 | const char *head_file = git_path(SEQ_HEAD_FILE); |
| 834 | static struct lock_file head_lock; |
| 835 | struct strbuf buf = STRBUF_INIT; |
| 836 | int fd; |
| 837 | |
| 838 | fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR); |
| 839 | strbuf_addf(&buf, "%s\n", head); |
| 840 | if (write_in_full(fd, buf.buf, buf.len) < 0) |
| 841 | die_errno(_("Could not write to %s"), head_file); |
| 842 | if (commit_lock_file(&head_lock) < 0) |
| 843 | die(_("Error wrapping up %s."), head_file); |
| 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 | |
| 860 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && |
| 861 | !file_exists(git_path("REVERT_HEAD"))) |
| 862 | return error(_("no cherry-pick or revert in progress")); |
| 863 | if (read_ref_full("HEAD", head_sha1, 0, NULL)) |
| 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 | { |
| 872 | const char *filename; |
| 873 | FILE *f; |
| 874 | unsigned char sha1[20]; |
| 875 | struct strbuf buf = STRBUF_INIT; |
| 876 | |
| 877 | filename = git_path(SEQ_HEAD_FILE); |
| 878 | f = fopen(filename, "r"); |
| 879 | if (!f && errno == ENOENT) { |
| 880 | /* |
| 881 | * There is no multiple-cherry-pick in progress. |
| 882 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates |
| 883 | * a single-cherry-pick in progress, abort that. |
| 884 | */ |
| 885 | return rollback_single_pick(); |
| 886 | } |
| 887 | if (!f) |
| 888 | return error(_("cannot open %s: %s"), filename, |
| 889 | strerror(errno)); |
| 890 | if (strbuf_getline(&buf, f, '\n')) { |
| 891 | error(_("cannot read %s: %s"), filename, ferror(f) ? |
| 892 | strerror(errno) : _("unexpected end of file")); |
| 893 | fclose(f); |
| 894 | goto fail; |
| 895 | } |
| 896 | fclose(f); |
| 897 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { |
| 898 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
| 899 | filename); |
| 900 | goto fail; |
| 901 | } |
| 902 | if (reset_for_rollback(sha1)) |
| 903 | goto fail; |
| 904 | remove_sequencer_state(); |
| 905 | strbuf_release(&buf); |
| 906 | return 0; |
| 907 | fail: |
| 908 | strbuf_release(&buf); |
| 909 | return -1; |
| 910 | } |
| 911 | |
| 912 | static void save_todo(struct commit_list *todo_list, struct replay_opts *opts) |
| 913 | { |
| 914 | const char *todo_file = git_path(SEQ_TODO_FILE); |
| 915 | static struct lock_file todo_lock; |
| 916 | struct strbuf buf = STRBUF_INIT; |
| 917 | int fd; |
| 918 | |
| 919 | fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR); |
| 920 | if (format_todo(&buf, todo_list, opts) < 0) |
| 921 | die(_("Could not format %s."), todo_file); |
| 922 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 923 | strbuf_release(&buf); |
| 924 | die_errno(_("Could not write to %s"), todo_file); |
| 925 | } |
| 926 | if (commit_lock_file(&todo_lock) < 0) { |
| 927 | strbuf_release(&buf); |
| 928 | die(_("Error wrapping up %s."), todo_file); |
| 929 | } |
| 930 | strbuf_release(&buf); |
| 931 | } |
| 932 | |
| 933 | static void save_opts(struct replay_opts *opts) |
| 934 | { |
| 935 | const char *opts_file = git_path(SEQ_OPTS_FILE); |
| 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 +0000 | [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 23:45:57 +0530 | [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 | |
| 996 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && |
| 997 | !file_exists(git_path("REVERT_HEAD"))) |
| 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 | |
| 1006 | if (!file_exists(git_path(SEQ_TODO_FILE))) |
| 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 */ |
| 1012 | if (file_exists(git_path("CHERRY_PICK_HEAD")) || |
| 1013 | file_exists(git_path("REVERT_HEAD"))) { |
| 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 15:06:52 +0200 | [diff] [blame] | 1034 | int i; |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [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 15:06:52 +0200 | [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 13:27:49 -0700 | [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 15:06:52 +0200 | [diff] [blame] | 1066 | die(_("%s: can't cherry-pick a %s"), name, typename(type)); |
Junio C Hamano | 7c0b0d8 | 2013-05-09 13:27:49 -0700 | [diff] [blame] | 1067 | } |
Miklos Vajna | 21246db | 2013-04-11 15:06:52 +0200 | [diff] [blame] | 1068 | } else |
| 1069 | die(_("%s: bad revision"), name); |
| 1070 | } |
| 1071 | |
Ramkumar Ramachandra | 043a449 | 2012-01-11 23:45:57 +0530 | [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; |
| 1101 | if (get_sha1("HEAD", sha1)) { |
| 1102 | if (opts->action == REPLAY_REVERT) |
| 1103 | return error(_("Can't revert as initial commit")); |
| 1104 | return error(_("Can't cherry-pick into empty head")); |
| 1105 | } |
| 1106 | save_head(sha1_to_hex(sha1)); |
| 1107 | save_opts(opts); |
| 1108 | return pick_commits(todo_list, opts); |
| 1109 | } |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1110 | |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1111 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1112 | { |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1113 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1114 | struct strbuf sob = STRBUF_INIT; |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1115 | int has_footer; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1116 | |
| 1117 | strbuf_addstr(&sob, sign_off_header); |
| 1118 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), |
| 1119 | getenv("GIT_COMMITTER_EMAIL"))); |
| 1120 | strbuf_addch(&sob, '\n'); |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | * If the whole message buffer is equal to the sob, pretend that we |
| 1124 | * found a conforming footer with a matching sob |
| 1125 | */ |
| 1126 | if (msgbuf->len - ignore_footer == sob.len && |
| 1127 | !strncmp(msgbuf->buf, sob.buf, sob.len)) |
| 1128 | has_footer = 3; |
| 1129 | else |
| 1130 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); |
| 1131 | |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 1132 | if (!has_footer) { |
| 1133 | const char *append_newlines = NULL; |
| 1134 | size_t len = msgbuf->len - ignore_footer; |
| 1135 | |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 1136 | if (!len) { |
| 1137 | /* |
| 1138 | * The buffer is completely empty. Leave foom for |
| 1139 | * the title and body to be filled in by the user. |
| 1140 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 1141 | append_newlines = "\n\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 1142 | } else if (msgbuf->buf[len - 1] != '\n') { |
| 1143 | /* |
| 1144 | * Incomplete line. Complete the line and add a |
| 1145 | * blank one so that there is an empty line between |
| 1146 | * the message body and the sob. |
| 1147 | */ |
| 1148 | append_newlines = "\n\n"; |
| 1149 | } else if (len == 1) { |
| 1150 | /* |
| 1151 | * Buffer contains a single newline. Add another |
| 1152 | * so that we leave room for the title and body. |
| 1153 | */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 1154 | append_newlines = "\n"; |
Brandon Casey | 8c613fd | 2013-02-22 14:05:27 -0800 | [diff] [blame] | 1155 | } else if (msgbuf->buf[len - 2] != '\n') { |
| 1156 | /* |
| 1157 | * Buffer ends with a single newline. Add another |
| 1158 | * so that there is an empty line between the message |
| 1159 | * body and the sob. |
| 1160 | */ |
| 1161 | append_newlines = "\n"; |
| 1162 | } /* else, the buffer already ends with two newlines. */ |
Brandon Casey | 33f2f9a | 2013-02-12 02:33:42 -0800 | [diff] [blame] | 1163 | |
| 1164 | if (append_newlines) |
| 1165 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1166 | append_newlines, strlen(append_newlines)); |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1167 | } |
Brandon Casey | bab4d10 | 2013-02-12 02:17:35 -0800 | [diff] [blame] | 1168 | |
| 1169 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) |
| 1170 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, |
| 1171 | sob.buf, sob.len); |
| 1172 | |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 1173 | strbuf_release(&sob); |
| 1174 | } |