@matheusbsbs / matheus-bagliano / commits / c62f6ec341
Fork 0

revert: add --ff option to allow fast forward when cherry-picking

As "git merge" fast forwards if possible, it seems sensible to have such a feature for "git cherry-pick" too, especially as it could be used in git-rebase--interactive.sh. Maybe this option could be made the default in the long run, with another --no-ff option to disable this default behavior, but that could make some scripts backward incompatible and/or that would require testing if some GIT_AUTHOR_* environment variables are set. So we don't do that for now. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 6, 2010 at 21:34 UTC c62f6ec341b1a768be7c9adf84d07c5b3b113a81
1 file changed +40 -3
builtin-revert.c
+40 -3
@@ -13,6 +13,7 @@
13 #include "revision.h"
14 #include "rerere.h"
15 #include "merge-recursive.h"
16 +#include "refs.h"
17
18 /*
19 * This implements the builtins revert and cherry-pick.
@@ -35,7 +36,7 @@ static const char * const cherry_pick_usage[] = {
36 NULL
37 };
38
38 -static int edit, no_replay, no_commit, mainline, signoff;
39 +static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
40 static enum { REVERT, CHERRY_PICK } action;
41 static struct commit *commit;
42 static const char *commit_name;
@@ -60,8 +61,19 @@ static void parse_args(int argc, const char **argv)
61 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
62 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
63 OPT_END(),
64 + OPT_END(),
65 + OPT_END(),
66 };
67
68 + if (action == CHERRY_PICK) {
69 + struct option cp_extra[] = {
70 + OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"),
71 + OPT_END(),
72 + };
73 + if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
74 + die("program error");
75 + }
76 +
77 if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
78 usage_with_options(usage_str, options);
79
@@ -244,6 +256,17 @@ static NORETURN void die_dirty_index(const char *me)
256 }
257 }
258
259 +static int fast_forward_to(const unsigned char *to, const unsigned char *from)
260 +{
261 + struct ref_lock *ref_lock;
262 +
263 + read_cache();
264 + if (checkout_fast_forward(from, to))
265 + exit(1); /* the callee should have complained already */
266 + ref_lock = lock_any_ref_for_update("HEAD", from, 0);
267 + return write_ref_sha1(ref_lock, to, "cherry-pick");
268 +}
269 +
270 static int revert_or_cherry_pick(int argc, const char **argv)
271 {
272 unsigned char head[20];
@@ -265,6 +288,17 @@ static int revert_or_cherry_pick(int argc, const char **argv)
288 if (action == REVERT && !no_replay)
289 die("revert is incompatible with replay");
290
291 + if (allow_ff) {
292 + if (signoff)
293 + die("cherry-pick --ff cannot be used with --signoff");
294 + if (no_commit)
295 + die("cherry-pick --ff cannot be used with --no-commit");
296 + if (no_replay)
297 + die("cherry-pick --ff cannot be used with -x");
298 + if (edit)
299 + die("cherry-pick --ff cannot be used with --edit");
300 + }
301 +
302 if (read_cache() < 0)
303 die("git %s: failed to read the index", me);
304 if (no_commit) {
@@ -284,8 +318,6 @@ static int revert_or_cherry_pick(int argc, const char **argv)
318 }
319 discard_cache();
320
287 - index_fd = hold_locked_index(&index_lock, 1);
288 -
321 if (!commit->parents) {
322 if (action == REVERT)
323 die ("Cannot revert a root commit");
@@ -314,6 +346,9 @@ static int revert_or_cherry_pick(int argc, const char **argv)
346 else
347 parent = commit->parents->item;
348
349 + if (allow_ff && !hashcmp(parent->object.sha1, head))
350 + return fast_forward_to(commit->object.sha1, head);
351 +
352 if (!(message = commit->buffer))
353 die ("Cannot get commit message for %s",
354 sha1_to_hex(commit->object.sha1));
@@ -343,6 +378,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
378
379 oneline = get_oneline(message);
380
381 + index_fd = hold_locked_index(&index_lock, 1);
382 +
383 if (action == REVERT) {
384 char *oneline_body = strchr(oneline, ' ');
385