Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "builtin.h" |
| 3 | #include "object.h" |
| 4 | #include "commit.h" |
| 5 | #include "tag.h" |
| 6 | #include "wt-status.h" |
| 7 | #include "run-command.h" |
| 8 | #include "exec_cmd.h" |
| 9 | #include "utf8.h" |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 10 | #include "parse-options.h" |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 11 | #include "cache-tree.h" |
Jeff King | 0f2d447 | 2008-03-03 01:30:56 -0500 | [diff] [blame] | 12 | #include "diff.h" |
| 13 | #include "revision.h" |
Abhijit Menon-Sen | aa1a011 | 2008-08-10 17:18:55 +0530 | [diff] [blame] | 14 | #include "rerere.h" |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 15 | #include "merge-recursive.h" |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * This implements the builtins revert and cherry-pick. |
| 19 | * |
| 20 | * Copyright (c) 2007 Johannes E. Schindelin |
| 21 | * |
| 22 | * Based on git-revert.sh, which is |
| 23 | * |
| 24 | * Copyright (c) 2005 Linus Torvalds |
| 25 | * Copyright (c) 2005 Junio C Hamano |
| 26 | */ |
| 27 | |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 28 | static const char * const revert_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 29 | "git revert [options] <commit-ish>", |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 30 | NULL |
| 31 | }; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 32 | |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 33 | static const char * const cherry_pick_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 34 | "git cherry-pick [options] <commit-ish>", |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 35 | NULL |
| 36 | }; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 37 | |
Dan McGee | cfd9c27 | 2008-04-26 15:14:28 -0500 | [diff] [blame] | 38 | static int edit, no_replay, no_commit, mainline, signoff; |
Junio C Hamano | 4175e9e | 2007-06-13 01:42:05 -0700 | [diff] [blame] | 39 | static enum { REVERT, CHERRY_PICK } action; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 40 | static struct commit *commit; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 41 | |
| 42 | static const char *me; |
| 43 | |
| 44 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" |
| 45 | |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 46 | static void parse_args(int argc, const char **argv) |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 47 | { |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 48 | const char * const * usage_str = |
| 49 | action == REVERT ? revert_usage : cherry_pick_usage; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 50 | unsigned char sha1[20]; |
| 51 | const char *arg; |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 52 | int noop; |
| 53 | struct option options[] = { |
| 54 | OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"), |
| 55 | OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"), |
| 56 | OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"), |
| 57 | OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"), |
Dan McGee | cfd9c27 | 2008-04-26 15:14:28 -0500 | [diff] [blame] | 58 | OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), |
Junio C Hamano | 02273fd | 2007-11-04 01:26:02 -0700 | [diff] [blame] | 59 | OPT_INTEGER('m', "mainline", &mainline, "parent number"), |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 60 | OPT_END(), |
| 61 | }; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 62 | |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 63 | if (parse_options(argc, argv, options, usage_str, 0) != 1) |
| 64 | usage_with_options(usage_str, options); |
| 65 | arg = argv[0]; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 66 | |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 67 | if (get_sha1(arg, sha1)) |
| 68 | die ("Cannot find '%s'", arg); |
| 69 | commit = (struct commit *)parse_object(sha1); |
| 70 | if (!commit) |
| 71 | die ("Could not find %s", sha1_to_hex(sha1)); |
| 72 | if (commit->object.type == OBJ_TAG) { |
| 73 | commit = (struct commit *) |
| 74 | deref_tag((struct object *)commit, arg, strlen(arg)); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 75 | } |
| 76 | if (commit->object.type != OBJ_COMMIT) |
| 77 | die ("'%s' does not point to a commit", arg); |
| 78 | } |
| 79 | |
| 80 | static char *get_oneline(const char *message) |
| 81 | { |
| 82 | char *result; |
| 83 | const char *p = message, *abbrev, *eol; |
| 84 | int abbrev_len, oneline_len; |
| 85 | |
| 86 | if (!p) |
| 87 | die ("Could not read commit message of %s", |
| 88 | sha1_to_hex(commit->object.sha1)); |
| 89 | while (*p && (*p != '\n' || p[1] != '\n')) |
| 90 | p++; |
| 91 | |
| 92 | if (*p) { |
| 93 | p += 2; |
| 94 | for (eol = p + 1; *eol && *eol != '\n'; eol++) |
| 95 | ; /* do nothing */ |
| 96 | } else |
| 97 | eol = p; |
| 98 | abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); |
| 99 | abbrev_len = strlen(abbrev); |
| 100 | oneline_len = eol - p; |
| 101 | result = xmalloc(abbrev_len + 5 + oneline_len); |
| 102 | memcpy(result, abbrev, abbrev_len); |
| 103 | memcpy(result + abbrev_len, "... ", 4); |
| 104 | memcpy(result + abbrev_len + 4, p, oneline_len); |
| 105 | result[abbrev_len + 4 + oneline_len] = '\0'; |
| 106 | return result; |
| 107 | } |
| 108 | |
Pierre Habouzit | 52fae7d | 2007-06-07 22:45:00 +0200 | [diff] [blame] | 109 | static char *get_encoding(const char *message) |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 110 | { |
| 111 | const char *p = message, *eol; |
| 112 | |
| 113 | if (!p) |
| 114 | die ("Could not read commit message of %s", |
| 115 | sha1_to_hex(commit->object.sha1)); |
| 116 | while (*p && *p != '\n') { |
| 117 | for (eol = p + 1; *eol && *eol != '\n'; eol++) |
| 118 | ; /* do nothing */ |
| 119 | if (!prefixcmp(p, "encoding ")) { |
| 120 | char *result = xmalloc(eol - 8 - p); |
| 121 | strlcpy(result, p + 9, eol - 8 - p); |
| 122 | return result; |
| 123 | } |
| 124 | p = eol; |
| 125 | if (*p == '\n') |
| 126 | p++; |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Junio C Hamano | 4175e9e | 2007-06-13 01:42:05 -0700 | [diff] [blame] | 131 | static struct lock_file msg_file; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 132 | static int msg_fd; |
| 133 | |
| 134 | static void add_to_msg(const char *string) |
| 135 | { |
| 136 | int len = strlen(string); |
| 137 | if (write_in_full(msg_fd, string, len) < 0) |
Shawn O. Pearce | 1dcb3b6 | 2007-05-10 18:10:36 -0400 | [diff] [blame] | 138 | die ("Could not write to MERGE_MSG"); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void add_message_to_msg(const char *message) |
| 142 | { |
| 143 | const char *p = message; |
| 144 | while (*p && (*p != '\n' || p[1] != '\n')) |
| 145 | p++; |
| 146 | |
| 147 | if (!*p) |
| 148 | add_to_msg(sha1_to_hex(commit->object.sha1)); |
| 149 | |
| 150 | p += 2; |
| 151 | add_to_msg(p); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | static void set_author_ident_env(const char *message) |
| 156 | { |
| 157 | const char *p = message; |
| 158 | if (!p) |
| 159 | die ("Could not read commit message of %s", |
| 160 | sha1_to_hex(commit->object.sha1)); |
| 161 | while (*p && *p != '\n') { |
| 162 | const char *eol; |
| 163 | |
| 164 | for (eol = p; *eol && *eol != '\n'; eol++) |
| 165 | ; /* do nothing */ |
| 166 | if (!prefixcmp(p, "author ")) { |
| 167 | char *line, *pend, *email, *timestamp; |
| 168 | |
| 169 | p += 7; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 170 | line = xmemdupz(p, eol - p); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 171 | email = strchr(line, '<'); |
| 172 | if (!email) |
| 173 | die ("Could not extract author email from %s", |
| 174 | sha1_to_hex(commit->object.sha1)); |
| 175 | if (email == line) |
| 176 | pend = line; |
| 177 | else |
| 178 | for (pend = email; pend != line + 1 && |
| 179 | isspace(pend[-1]); pend--); |
| 180 | ; /* do nothing */ |
| 181 | *pend = '\0'; |
| 182 | email++; |
| 183 | timestamp = strchr(email, '>'); |
| 184 | if (!timestamp) |
Stephan Beyer | 1e5f7ad | 2008-08-02 03:51:21 +0200 | [diff] [blame] | 185 | die ("Could not extract author time from %s", |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 186 | sha1_to_hex(commit->object.sha1)); |
| 187 | *timestamp = '\0'; |
| 188 | for (timestamp++; *timestamp && isspace(*timestamp); |
| 189 | timestamp++) |
| 190 | ; /* do nothing */ |
| 191 | setenv("GIT_AUTHOR_NAME", line, 1); |
| 192 | setenv("GIT_AUTHOR_EMAIL", email, 1); |
| 193 | setenv("GIT_AUTHOR_DATE", timestamp, 1); |
| 194 | free(line); |
| 195 | return; |
| 196 | } |
| 197 | p = eol; |
| 198 | if (*p == '\n') |
| 199 | p++; |
| 200 | } |
| 201 | die ("No author information found in %s", |
| 202 | sha1_to_hex(commit->object.sha1)); |
| 203 | } |
| 204 | |
Wincent Colaiuta | 804c717 | 2007-11-28 00:06:36 -0800 | [diff] [blame] | 205 | static char *help_msg(const unsigned char *sha1) |
| 206 | { |
| 207 | static char helpbuf[1024]; |
| 208 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
| 209 | |
| 210 | if (msg) |
| 211 | return msg; |
| 212 | |
| 213 | strcpy(helpbuf, " After resolving the conflicts,\n" |
| 214 | "mark the corrected paths with 'git add <paths>' " |
David Symonds | 0d81358 | 2007-11-24 00:38:50 +1100 | [diff] [blame] | 215 | "or 'git rm <paths>' and commit the result."); |
Wincent Colaiuta | 804c717 | 2007-11-28 00:06:36 -0800 | [diff] [blame] | 216 | |
| 217 | if (action == CHERRY_PICK) { |
| 218 | sprintf(helpbuf + strlen(helpbuf), |
| 219 | "\nWhen commiting, use the option " |
| 220 | "'-c %s' to retain authorship and message.", |
| 221 | find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
| 222 | } |
| 223 | return helpbuf; |
| 224 | } |
| 225 | |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 226 | static struct tree *empty_tree(void) |
| 227 | { |
| 228 | struct tree *tree = xcalloc(1, sizeof(struct tree)); |
| 229 | |
| 230 | tree->object.parsed = 1; |
| 231 | tree->object.type = OBJ_TREE; |
| 232 | pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); |
| 233 | return tree; |
| 234 | } |
| 235 | |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 236 | static int revert_or_cherry_pick(int argc, const char **argv) |
| 237 | { |
| 238 | unsigned char head[20]; |
Junio C Hamano | 7791ecb | 2007-10-23 13:33:26 -0700 | [diff] [blame] | 239 | struct commit *base, *next, *parent; |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 240 | int i, index_fd, clean; |
Shawn O. Pearce | 1a8f274 | 2007-03-12 15:33:18 -0400 | [diff] [blame] | 241 | char *oneline, *reencoded_message = NULL; |
| 242 | const char *message, *encoding; |
Junio C Hamano | a1a846a | 2008-11-05 11:35:53 -0800 | [diff] [blame] | 243 | char *defmsg = git_pathdup("MERGE_MSG"); |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 244 | struct merge_options o; |
| 245 | struct tree *result, *next_tree, *base_tree, *head_tree; |
| 246 | static struct lock_file index_lock; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 247 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 248 | git_config(git_default_config, NULL); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 249 | me = action == REVERT ? "revert" : "cherry-pick"; |
| 250 | setenv(GIT_REFLOG_ACTION, me, 0); |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 251 | parse_args(argc, argv); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 252 | |
| 253 | /* this is copied from the shell script, but it's never triggered... */ |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 254 | if (action == REVERT && !no_replay) |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 255 | die("revert is incompatible with replay"); |
| 256 | |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 257 | if (read_cache() < 0) |
| 258 | die("git %s: failed to read the index", me); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 259 | if (no_commit) { |
| 260 | /* |
| 261 | * We do not intend to commit immediately. We just want to |
Junio C Hamano | 71aa2b8 | 2007-11-13 13:45:11 -0800 | [diff] [blame] | 262 | * merge the differences in, so let's compute the tree |
| 263 | * that represents the "current" state for merge-recursive |
| 264 | * to work on. |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 265 | */ |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 266 | if (write_cache_as_tree(head, 0, NULL)) |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 267 | die ("Your index file is unmerged."); |
| 268 | } else { |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 269 | if (get_sha1("HEAD", head)) |
| 270 | die ("You do not have a valid HEAD"); |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 271 | if (index_differs_from("HEAD", 0)) |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 272 | die ("Dirty index: cannot %s", me); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 273 | } |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 274 | discard_cache(); |
| 275 | |
| 276 | index_fd = hold_locked_index(&index_lock, 1); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 277 | |
Johannes Schindelin | f95ebf7 | 2008-07-04 16:19:52 +0100 | [diff] [blame] | 278 | if (!commit->parents) { |
| 279 | if (action == REVERT) |
| 280 | die ("Cannot revert a root commit"); |
| 281 | parent = NULL; |
| 282 | } |
| 283 | else if (commit->parents->next) { |
Junio C Hamano | 7791ecb | 2007-10-23 13:33:26 -0700 | [diff] [blame] | 284 | /* Reverting or cherry-picking a merge commit */ |
| 285 | int cnt; |
| 286 | struct commit_list *p; |
| 287 | |
| 288 | if (!mainline) |
| 289 | die("Commit %s is a merge but no -m option was given.", |
| 290 | sha1_to_hex(commit->object.sha1)); |
| 291 | |
| 292 | for (cnt = 1, p = commit->parents; |
| 293 | cnt != mainline && p; |
| 294 | cnt++) |
| 295 | p = p->next; |
| 296 | if (cnt != mainline || !p) |
| 297 | die("Commit %s does not have parent %d", |
| 298 | sha1_to_hex(commit->object.sha1), mainline); |
| 299 | parent = p->item; |
| 300 | } else if (0 < mainline) |
| 301 | die("Mainline was specified but commit %s is not a merge.", |
| 302 | sha1_to_hex(commit->object.sha1)); |
| 303 | else |
| 304 | parent = commit->parents->item; |
| 305 | |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 306 | if (!(message = commit->buffer)) |
| 307 | die ("Cannot get commit message for %s", |
| 308 | sha1_to_hex(commit->object.sha1)); |
| 309 | |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 310 | if (parent && parse_commit(parent) < 0) |
| 311 | die("%s: cannot parse parent commit %s", |
| 312 | me, sha1_to_hex(parent->object.sha1)); |
| 313 | |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 314 | /* |
| 315 | * "commit" is an existing commit. We would want to apply |
| 316 | * the difference it introduces since its first parent "prev" |
| 317 | * on top of the current HEAD if we are cherry-pick. Or the |
| 318 | * reverse of it if we are revert. |
| 319 | */ |
| 320 | |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 321 | msg_fd = hold_lock_file_for_update(&msg_file, defmsg, |
| 322 | LOCK_DIE_ON_ERROR); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 323 | |
| 324 | encoding = get_encoding(message); |
| 325 | if (!encoding) |
| 326 | encoding = "utf-8"; |
| 327 | if (!git_commit_encoding) |
| 328 | git_commit_encoding = "utf-8"; |
| 329 | if ((reencoded_message = reencode_string(message, |
| 330 | git_commit_encoding, encoding))) |
| 331 | message = reencoded_message; |
| 332 | |
| 333 | oneline = get_oneline(message); |
| 334 | |
| 335 | if (action == REVERT) { |
Johannes Schindelin | e43b010 | 2007-03-23 17:06:11 +0100 | [diff] [blame] | 336 | char *oneline_body = strchr(oneline, ' '); |
| 337 | |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 338 | base = commit; |
Junio C Hamano | 7791ecb | 2007-10-23 13:33:26 -0700 | [diff] [blame] | 339 | next = parent; |
Johannes Schindelin | e43b010 | 2007-03-23 17:06:11 +0100 | [diff] [blame] | 340 | add_to_msg("Revert \""); |
| 341 | add_to_msg(oneline_body + 1); |
| 342 | add_to_msg("\"\n\nThis reverts commit "); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 343 | add_to_msg(sha1_to_hex(commit->object.sha1)); |
Robin Rosenberg | d5be89d | 2008-12-21 14:17:12 -0800 | [diff] [blame] | 344 | |
| 345 | if (commit->parents->next) { |
| 346 | add_to_msg(", reversing\nchanges made to "); |
| 347 | add_to_msg(sha1_to_hex(parent->object.sha1)); |
| 348 | } |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 349 | add_to_msg(".\n"); |
| 350 | } else { |
Junio C Hamano | 7791ecb | 2007-10-23 13:33:26 -0700 | [diff] [blame] | 351 | base = parent; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 352 | next = commit; |
| 353 | set_author_ident_env(message); |
| 354 | add_message_to_msg(message); |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 355 | if (no_replay) { |
Shawn O. Pearce | 0e62404 | 2007-03-06 00:46:00 -0500 | [diff] [blame] | 356 | add_to_msg("(cherry picked from commit "); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 357 | add_to_msg(sha1_to_hex(commit->object.sha1)); |
| 358 | add_to_msg(")\n"); |
| 359 | } |
| 360 | } |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 361 | |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 362 | read_cache(); |
| 363 | init_merge_options(&o); |
| 364 | o.branch1 = "HEAD"; |
| 365 | o.branch2 = oneline; |
| 366 | |
| 367 | head_tree = parse_tree_indirect(head); |
| 368 | next_tree = next ? next->tree : empty_tree(); |
| 369 | base_tree = base ? base->tree : empty_tree(); |
| 370 | |
| 371 | clean = merge_trees(&o, |
| 372 | head_tree, |
| 373 | next_tree, base_tree, &result); |
| 374 | |
| 375 | if (active_cache_changed && |
| 376 | (write_cache(index_fd, active_cache, active_nr) || |
| 377 | commit_locked_index(&index_lock))) |
| 378 | die("%s: Unable to write new index file", me); |
Chris Johnsen | 0d66e95 | 2009-03-07 03:30:51 -0600 | [diff] [blame] | 379 | rollback_lock_file(&index_lock); |
Miklos Vajna | 6eb1b43 | 2008-09-02 14:11:15 -0700 | [diff] [blame] | 380 | |
| 381 | if (!clean) { |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 382 | add_to_msg("\nConflicts:\n\n"); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 383 | for (i = 0; i < active_nr;) { |
| 384 | struct cache_entry *ce = active_cache[i++]; |
| 385 | if (ce_stage(ce)) { |
| 386 | add_to_msg("\t"); |
| 387 | add_to_msg(ce->name); |
| 388 | add_to_msg("\n"); |
| 389 | while (i < active_nr && !strcmp(ce->name, |
| 390 | active_cache[i]->name)) |
| 391 | i++; |
| 392 | } |
| 393 | } |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 394 | if (commit_lock_file(&msg_file) < 0) |
Shawn O. Pearce | abda522 | 2007-05-07 22:57:15 -0400 | [diff] [blame] | 395 | die ("Error wrapping up %s", defmsg); |
Wincent Colaiuta | 804c717 | 2007-11-28 00:06:36 -0800 | [diff] [blame] | 396 | fprintf(stderr, "Automatic %s failed.%s\n", |
| 397 | me, help_msg(commit->object.sha1)); |
Abhijit Menon-Sen | aa1a011 | 2008-08-10 17:18:55 +0530 | [diff] [blame] | 398 | rerere(); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 399 | exit(1); |
| 400 | } |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 401 | if (commit_lock_file(&msg_file) < 0) |
Shawn O. Pearce | abda522 | 2007-05-07 22:57:15 -0400 | [diff] [blame] | 402 | die ("Error wrapping up %s", defmsg); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 403 | fprintf(stderr, "Finished one %s.\n", me); |
| 404 | |
| 405 | /* |
| 406 | * |
| 407 | * If we are cherry-pick, and if the merge did not result in |
| 408 | * hand-editing, we will hit this commit and inherit the original |
| 409 | * author date and name. |
| 410 | * If we are revert, or if our cherry-pick results in a hand merge, |
| 411 | * we had better say that the current user is responsible for that. |
| 412 | */ |
| 413 | |
| 414 | if (!no_commit) { |
Dan McGee | cfd9c27 | 2008-04-26 15:14:28 -0500 | [diff] [blame] | 415 | /* 6 is max possible length of our args array including NULL */ |
| 416 | const char *args[6]; |
| 417 | int i = 0; |
| 418 | args[i++] = "commit"; |
| 419 | args[i++] = "-n"; |
| 420 | if (signoff) |
| 421 | args[i++] = "-s"; |
| 422 | if (!edit) { |
| 423 | args[i++] = "-F"; |
| 424 | args[i++] = defmsg; |
| 425 | } |
| 426 | args[i] = NULL; |
| 427 | return execv_git_cmd(args); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 428 | } |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 429 | free(reencoded_message); |
Alex Riesen | d258b25 | 2008-10-28 18:27:33 +0100 | [diff] [blame] | 430 | free(defmsg); |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | int cmd_revert(int argc, const char **argv, const char *prefix) |
| 436 | { |
| 437 | if (isatty(0)) |
| 438 | edit = 1; |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 439 | no_replay = 1; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 440 | action = REVERT; |
| 441 | return revert_or_cherry_pick(argc, argv); |
| 442 | } |
| 443 | |
| 444 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) |
| 445 | { |
Pierre Habouzit | f810379 | 2007-10-07 23:02:29 +0200 | [diff] [blame] | 446 | no_replay = 0; |
Johannes Schindelin | 9509af6 | 2007-03-01 05:26:30 +0100 | [diff] [blame] | 447 | action = CHERRY_PICK; |
| 448 | return revert_or_cherry_pick(argc, argv); |
| 449 | } |