@matheusbsbs / matheus-bagliano / commits / 1830d9cb62
Fork 0

Fix rebase -p --onto

In a rebase with --onto, the correct test for whether we can skip rewriting a commit is if it is already on top of $ONTO, not $UPSTREAM. Without --onto, this distinction does not exist and the behavior does not change. In a situation with two merged branches on a common base X: X---o---o---o---M \ / x---x---x---x Y if we try to move the branches from their base on X to be based on Y, so as to get X Y---o'--o'--o'--M' \ / x'--x'--x'--x' then we fail. The command `git rebase -p --onto Y X M` moves only the first-parent chain, like so: X \ x---x---x---x \ Y---o'--o'--o'--M' because it mistakenly drops the other branch(es) x---x---x---x from the TODO file. This tests and fixes this behavior. Signed-off-by: Greg Price <price@ksplice.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Greg Price committed Jul 22, 2009 at 12:38 UTC 1830d9cb62772c0626297e4bb6e537664283ebfa
2 files changed +81 -1
git-rebase--interactive.sh
+1 -1
@@ -703,7 +703,7 @@ first and then run 'git rebase --continue' again."
703 preserve=t
704 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
705 do
706 - if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
706 + if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
707 then
708 preserve=f
709 fi
t/t3414-rebase-preserve-onto.sh new
+80
@@ -0,0 +1,80 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2009 Greg Price
4 +#
5 +
6 +test_description='git rebase -p should respect --onto
7 +
8 +In a rebase with --onto, we should rewrite all the commits that
9 +aren'"'"'t on top of $ONTO, even if they are on top of $UPSTREAM.
10 +'
11 +. ./test-lib.sh
12 +
13 +. ../lib-rebase.sh
14 +
15 +# Set up branches like this:
16 +# A1---B1---E1---F1---G1
17 +# \ \ /
18 +# \ \--C1---D1--/
19 +# H1
20 +
21 +test_expect_success 'setup' '
22 + test_commit A1 &&
23 + test_commit B1 &&
24 + test_commit C1 &&
25 + test_commit D1 &&
26 + git reset --hard B1 &&
27 + test_commit E1 &&
28 + test_commit F1 &&
29 + test_merge G1 D1 &&
30 + git reset --hard A1 &&
31 + test_commit H1
32 +'
33 +
34 +# Now rebase merge G1 from both branches' base B1, both should move:
35 +# A1---B1---E1---F1---G1
36 +# \ \ /
37 +# \ \--C1---D1--/
38 +# \
39 +# H1---E2---F2---G2
40 +# \ /
41 +# \--C2---D2--/
42 +
43 +test_expect_success 'rebase from B1 onto H1' '
44 + git checkout G1 &&
45 + git rebase -p --onto H1 B1 &&
46 + test "$(git rev-parse HEAD^1^1^1)" = "$(git rev-parse H1)" &&
47 + test "$(git rev-parse HEAD^2^1^1)" = "$(git rev-parse H1)"
48 +'
49 +
50 +# On the other hand if rebase from E1 which is within one branch,
51 +# then the other branch stays:
52 +# A1---B1---E1---F1---G1
53 +# \ \ /
54 +# \ \--C1---D1--/
55 +# \ \
56 +# H1-----F3-----G3
57 +
58 +test_expect_success 'rebase from E1 onto H1' '
59 + git checkout G1 &&
60 + git rebase -p --onto H1 E1 &&
61 + test "$(git rev-parse HEAD^1^1)" = "$(git rev-parse H1)" &&
62 + test "$(git rev-parse HEAD^2)" = "$(git rev-parse D1)"
63 +'
64 +
65 +# And the same if we rebase from a commit in the second-parent branch.
66 +# A1---B1---E1---F1----G1
67 +# \ \ \ /
68 +# \ \--C1---D1-\-/
69 +# \ \
70 +# H1------D3------G4
71 +
72 +test_expect_success 'rebase from C1 onto H1' '
73 + git checkout G1 &&
74 + git rev-list --first-parent --pretty=oneline C1..G1 &&
75 + git rebase -p --onto H1 C1 &&
76 + test "$(git rev-parse HEAD^2^1)" = "$(git rev-parse H1)" &&
77 + test "$(git rev-parse HEAD^1)" = "$(git rev-parse F1)"
78 +'
79 +
80 +test_done