@matheusbsbs / matheus-bagliano / commits / 7791ecbc62
Fork 0

revert/cherry-pick: work on merge commits as well

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline (iow, what change to reverse). With this patch, cherry-pick and revert learn -m (--mainline) option that lets you specify the parent number (starting from 1) of the mainline, so that you can: git revert -m 1 $merge to reverse the changes introduced by the $merge commit relative to its first parent, and: git cherry-pick -m 2 $merge to replay the changes introduced by the $merge commit relative to its second parent. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 23, 2007 at 13:33 UTC 7791ecbc62b792b3eaa6d722b6dadcea4d0f322d
4 files changed +64 -9
Documentation/git-cherry-pick.txt
+8 -1
@@ -7,7 +7,7 @@ git-cherry-pick - Apply the change introduced by an existing commit
7
8 SYNOPSIS
9 --------
10 -'git-cherry-pick' [--edit] [-n] [-x] <commit>
10 +'git-cherry-pick' [--edit] [-n] [-m parent-number] [-x] <commit>
11
12 DESCRIPTION
13 -----------
@@ -44,6 +44,13 @@ OPTIONS
44 described above, and `-r` was to disable it. Now the
45 default is not to do `-x` so this option is a no-op.
46
47 +-m parent-number|--mainline parent-number::
48 + Usually you cannot revert a merge because you do not know which
49 + side of the merge should be considered the mainline. This
50 + option specifies the parent number (starting from 1) of
51 + the mainline and allows cherry-pick to replay the change
52 + relative to the specified parent.
53 +
54 -n|--no-commit::
55 Usually the command automatically creates a commit with
56 a commit log message stating which commit was
Documentation/git-revert.txt
+8 -1
@@ -7,7 +7,7 @@ git-revert - Revert an existing commit
7
8 SYNOPSIS
9 --------
10 -'git-revert' [--edit | --no-edit] [-n] <commit>
10 +'git-revert' [--edit | --no-edit] [-n] [-m parent-number] <commit>
11
12 DESCRIPTION
13 -----------
@@ -27,6 +27,13 @@ OPTIONS
27 message prior committing the revert. This is the default if
28 you run the command from a terminal.
29
30 +-m parent-number|--mainline parent-number::
31 + Usually you cannot revert a merge because you do not know which
32 + side of the merge should be considered the mainline. This
33 + option specifies the parent number (starting from 1) of
34 + the mainline and allows revert to reverse the change
35 + relative to the specified parent.
36 +
37 --no-edit::
38 With this option, `git-revert` will not start the commit
39 message editor.
builtin-revert.c
+35 -7
@@ -19,9 +19,9 @@
19 * Copyright (c) 2005 Junio C Hamano
20 */
21
22 -static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] <commit-ish>";
22 +static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] [-m parent-number] <commit-ish>";
23
24 -static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
24 +static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-m parent-number] [-r] [-x] <commit-ish>";
25
26 static int edit;
27 static int replay;
@@ -29,6 +29,7 @@ static enum { REVERT, CHERRY_PICK } action;
29 static int no_commit;
30 static struct commit *commit;
31 static int needed_deref;
32 +static int mainline;
33
34 static const char *me;
35
@@ -58,6 +59,12 @@ static void parse_options(int argc, const char **argv)
59 else if (!strcmp(arg, "-x") || !strcmp(arg, "--i-really-want-"
60 "to-expose-my-private-commit-object-name"))
61 replay = 0;
62 + else if (!strcmp(arg, "-m") || !strcmp(arg, "--mainline")) {
63 + if (++i >= argc ||
64 + strtol_i(argv[i], 10, &mainline) ||
65 + mainline <= 0)
66 + usage(usage_str);
67 + }
68 else if (strcmp(arg, "-r"))
69 usage(usage_str);
70 }
@@ -234,7 +241,7 @@ static int merge_recursive(const char *base_sha1,
241 static int revert_or_cherry_pick(int argc, const char **argv)
242 {
243 unsigned char head[20];
237 - struct commit *base, *next;
244 + struct commit *base, *next, *parent;
245 int i;
246 char *oneline, *reencoded_message = NULL;
247 const char *message, *encoding;
@@ -269,8 +276,29 @@ static int revert_or_cherry_pick(int argc, const char **argv)
276
277 if (!commit->parents)
278 die ("Cannot %s a root commit", me);
272 - if (commit->parents->next)
273 - die ("Cannot %s a multi-parent commit.", me);
279 + if (commit->parents->next) {
280 + /* Reverting or cherry-picking a merge commit */
281 + int cnt;
282 + struct commit_list *p;
283 +
284 + if (!mainline)
285 + die("Commit %s is a merge but no -m option was given.",
286 + sha1_to_hex(commit->object.sha1));
287 +
288 + for (cnt = 1, p = commit->parents;
289 + cnt != mainline && p;
290 + cnt++)
291 + p = p->next;
292 + if (cnt != mainline || !p)
293 + die("Commit %s does not have parent %d",
294 + sha1_to_hex(commit->object.sha1), mainline);
295 + parent = p->item;
296 + } else if (0 < mainline)
297 + die("Mainline was specified but commit %s is not a merge.",
298 + sha1_to_hex(commit->object.sha1));
299 + else
300 + parent = commit->parents->item;
301 +
302 if (!(message = commit->buffer))
303 die ("Cannot get commit message for %s",
304 sha1_to_hex(commit->object.sha1));
@@ -299,14 +327,14 @@ static int revert_or_cherry_pick(int argc, const char **argv)
327 char *oneline_body = strchr(oneline, ' ');
328
329 base = commit;
302 - next = commit->parents->item;
330 + next = parent;
331 add_to_msg("Revert \"");
332 add_to_msg(oneline_body + 1);
333 add_to_msg("\"\n\nThis reverts commit ");
334 add_to_msg(sha1_to_hex(commit->object.sha1));
335 add_to_msg(".\n");
336 } else {
309 - base = commit->parents->item;
337 + base = parent;
338 next = commit;
339 set_author_ident_env(message);
340 add_message_to_msg(message);
git-compat-util.h
+13
@@ -381,4 +381,17 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
381 return 0;
382 }
383
384 +static inline int strtol_i(char const *s, int base, int *result)
385 +{
386 + long ul;
387 + char *p;
388 +
389 + errno = 0;
390 + ul = strtol(s, &p, base);
391 + if (errno || *p || p == s || (int) ul != ul)
392 + return -1;
393 + *result = ul;
394 + return 0;
395 +}
396 +
397 #endif