Merge branch 'master' of git://repo.or.cz/alt-git
[git/dscho.git] / t / t7110-reset-merge.sh
blob8704d0019655d591785c0cf0eadb7d846c6b4469
1 #!/bin/sh
3 # Copyright (c) 2009 Christian Couder
6 test_description='Tests for "git reset --merge"'
8 . ./test-lib.sh
10 test_expect_success setup '
11 for i in 1 2 3; do echo line $i; done >file1 &&
12 cat file1 >file2 &&
13 git add file1 file2 &&
14 test_tick &&
15 git commit -m "Initial commit" &&
16 git tag initial &&
17 echo line 4 >>file1 &&
18 cat file1 >file2 &&
19 test_tick &&
20 git commit -m "add line 4 to file1" file1 &&
21 git tag second
24 # The next test will test the following:
26 # working index HEAD target working index HEAD
27 # ----------------------------------------------------
28 # file1: C C C D --merge D D D
29 # file2: C D D D --merge C D D
30 test_expect_success 'reset --merge is ok with changes in file it does not touch' '
31 git reset --merge HEAD^ &&
32 ! grep 4 file1 &&
33 grep 4 file2 &&
34 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
35 test -z "$(git diff --cached)"
38 test_expect_success 'reset --merge is ok when switching back' '
39 git reset --merge second &&
40 grep 4 file1 &&
41 grep 4 file2 &&
42 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
43 test -z "$(git diff --cached)"
46 # The next test will test the following:
48 # working index HEAD target working index HEAD
49 # ----------------------------------------------------
50 # file1: B B C D --merge D D D
51 # file2: C D D D --merge C D D
52 test_expect_success 'reset --merge discards changes added to index (1)' '
53 git reset --hard second &&
54 cat file1 >file2 &&
55 echo "line 5" >> file1 &&
56 git add file1 &&
57 git reset --merge HEAD^ &&
58 ! grep 4 file1 &&
59 ! grep 5 file1 &&
60 grep 4 file2 &&
61 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
62 test -z "$(git diff --cached)"
65 test_expect_success 'reset --merge is ok again when switching back (1)' '
66 git reset --hard initial &&
67 echo "line 5" >> file2 &&
68 git add file2 &&
69 git reset --merge second &&
70 ! grep 4 file2 &&
71 ! grep 5 file1 &&
72 grep 4 file1 &&
73 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
74 test -z "$(git diff --cached)"
77 # The next test will test the following:
79 # working index HEAD target working index HEAD
80 # ----------------------------------------------------
81 # file1: C C C D --merge D D D
82 # file2: C C D D --merge D D D
83 test_expect_success 'reset --merge discards changes added to index (2)' '
84 git reset --hard second &&
85 echo "line 4" >> file2 &&
86 git add file2 &&
87 git reset --merge HEAD^ &&
88 ! grep 4 file2 &&
89 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
90 test -z "$(git diff)" &&
91 test -z "$(git diff --cached)"
94 test_expect_success 'reset --merge is ok again when switching back (2)' '
95 git reset --hard initial &&
96 git reset --merge second &&
97 ! grep 4 file2 &&
98 grep 4 file1 &&
99 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
100 test -z "$(git diff --cached)"
103 # The next test will test the following:
105 # working index HEAD target working index HEAD
106 # ----------------------------------------------------
107 # file1: A B B C --merge (disallowed)
108 test_expect_success 'reset --merge fails with changes in file it touches' '
109 git reset --hard second &&
110 echo "line 5" >> file1 &&
111 test_tick &&
112 git commit -m "add line 5" file1 &&
113 sed -e "s/line 1/changed line 1/" <file1 >file3 &&
114 mv file3 file1 &&
115 test_must_fail git reset --merge HEAD^ 2>err.log &&
116 grep file1 err.log | grep "not uptodate"
119 test_expect_success 'setup 3 different branches' '
120 git reset --hard second &&
121 git branch branch1 &&
122 git branch branch2 &&
123 git branch branch3 &&
124 git checkout branch1 &&
125 echo "line 5 in branch1" >> file1 &&
126 test_tick &&
127 git commit -a -m "change in branch1" &&
128 git checkout branch2 &&
129 echo "line 5 in branch2" >> file1 &&
130 test_tick &&
131 git commit -a -m "change in branch2" &&
132 git tag third &&
133 git checkout branch3 &&
134 echo a new file >file3 &&
135 rm -f file1 &&
136 git add file3 &&
137 test_tick &&
138 git commit -a -m "change in branch3"
141 # The next test will test the following:
143 # working index HEAD target working index HEAD
144 # ----------------------------------------------------
145 # file1: X U B C --merge C C C
146 test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
147 git checkout third &&
148 test_must_fail git merge branch1 &&
149 git reset --merge HEAD^ &&
150 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
151 test -z "$(git diff --cached)" &&
152 test -z "$(git diff)"
155 # The next test will test the following:
157 # working index HEAD target working index HEAD
158 # ----------------------------------------------------
159 # file1: X U B B --merge B B B
160 test_expect_success '"reset --merge HEAD" is ok with pending merge' '
161 git reset --hard third &&
162 test_must_fail git merge branch1 &&
163 git reset --merge HEAD &&
164 test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
165 test -z "$(git diff --cached)" &&
166 test -z "$(git diff)"
169 test_expect_success '--merge with added/deleted' '
170 git reset --hard third &&
171 rm -f file2 &&
172 test_must_fail git merge branch3 &&
173 ! test -f file2 &&
174 test -f file3 &&
175 git diff --exit-code file3 &&
176 git diff --exit-code branch3 file3 &&
177 git reset --merge HEAD &&
178 ! test -f file3 &&
179 ! test -f file2 &&
180 git diff --exit-code --cached
183 test_done