903ebc53d730a3e5bd2481adb716246c7f2e8c93
[git.git] / t / t6050-replace.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Christian Couder
4 #
5 test_description='Tests replace refs functionality'
6
7 exec </dev/null
8
9 . ./test-lib.sh
10
11 add_and_commit_file()
12 {
13     _file="$1"
14     _msg="$2"
15
16     git add $_file || return $?
17     test_tick || return $?
18     git commit --quiet -m "$_file: $_msg"
19 }
20
21 HASH1=
22 HASH2=
23 HASH3=
24 HASH4=
25 HASH5=
26 HASH6=
27 HASH7=
28
29 test_expect_success 'set up buggy branch' '
30      echo "line 1" >> hello &&
31      echo "line 2" >> hello &&
32      echo "line 3" >> hello &&
33      echo "line 4" >> hello &&
34      add_and_commit_file hello "4 lines" &&
35      HASH1=$(git rev-parse --verify HEAD) &&
36      echo "line BUG" >> hello &&
37      echo "line 6" >> hello &&
38      echo "line 7" >> hello &&
39      echo "line 8" >> hello &&
40      add_and_commit_file hello "4 more lines with a BUG" &&
41      HASH2=$(git rev-parse --verify HEAD) &&
42      echo "line 9" >> hello &&
43      echo "line 10" >> hello &&
44      add_and_commit_file hello "2 more lines" &&
45      HASH3=$(git rev-parse --verify HEAD) &&
46      echo "line 11" >> hello &&
47      add_and_commit_file hello "1 more line" &&
48      HASH4=$(git rev-parse --verify HEAD) &&
49      sed -e "s/BUG/5/" hello > hello.new &&
50      mv hello.new hello &&
51      add_and_commit_file hello "BUG fixed" &&
52      HASH5=$(git rev-parse --verify HEAD) &&
53      echo "line 12" >> hello &&
54      echo "line 13" >> hello &&
55      add_and_commit_file hello "2 more lines" &&
56      HASH6=$(git rev-parse --verify HEAD)
57      echo "line 14" >> hello &&
58      echo "line 15" >> hello &&
59      echo "line 16" >> hello &&
60      add_and_commit_file hello "again 3 more lines" &&
61      HASH7=$(git rev-parse --verify HEAD)
62 '
63
64 test_expect_success 'replace the author' '
65      git cat-file commit $HASH2 | grep "author A U Thor" &&
66      R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
67      git cat-file commit $R | grep "author O Thor" &&
68      git update-ref refs/replace/$HASH2 $R &&
69      git show HEAD~5 | grep "O Thor" &&
70      git show $HASH2 | grep "O Thor"
71 '
72
73 test_expect_success 'test --no-replace-objects option' '
74      git cat-file commit $HASH2 | grep "author O Thor" &&
75      git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
76      git show $HASH2 | grep "O Thor" &&
77      git --no-replace-objects show $HASH2 | grep "A U Thor"
78 '
79
80 test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
81      GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
82      GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
83 '
84
85 cat >tag.sig <<EOF
86 object $HASH2
87 type commit
88 tag mytag
89 tagger T A Gger <> 0 +0000
90
91 EOF
92
93 test_expect_success 'tag replaced commit' '
94      git mktag <tag.sig >.git/refs/tags/mytag 2>message
95 '
96
97 test_expect_success '"git fsck" works' '
98      git fsck master > fsck_master.out &&
99      grep "dangling commit $R" fsck_master.out &&
100      grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
101      test -z "$(git fsck)"
102 '
103
104 test_expect_success 'repack, clone and fetch work' '
105      git repack -a -d &&
106      git clone --no-hardlinks . clone_dir &&
107      cd clone_dir &&
108      git show HEAD~5 | grep "A U Thor" &&
109      git show $HASH2 | grep "A U Thor" &&
110      git cat-file commit $R &&
111      git repack -a -d &&
112      test_must_fail git cat-file commit $R &&
113      git fetch ../ "refs/replace/*:refs/replace/*" &&
114      git show HEAD~5 | grep "O Thor" &&
115      git show $HASH2 | grep "O Thor" &&
116      git cat-file commit $R &&
117      cd ..
118 '
119
120 test_expect_success '"git replace" listing and deleting' '
121      test "$HASH2" = "$(git replace -l)" &&
122      test "$HASH2" = "$(git replace)" &&
123      aa=${HASH2%??????????????????????????????????????} &&
124      test "$HASH2" = "$(git replace -l "$aa*")" &&
125      test_must_fail git replace -d $R &&
126      test_must_fail git replace -d &&
127      test_must_fail git replace -l -d $HASH2 &&
128      git replace -d $HASH2 &&
129      git show $HASH2 | grep "A U Thor" &&
130      test -z "$(git replace -l)"
131 '
132
133 test_expect_success '"git replace" replacing' '
134      git replace $HASH2 $R &&
135      git show $HASH2 | grep "O Thor" &&
136      test_must_fail git replace $HASH2 $R &&
137      git replace -f $HASH2 $R &&
138      test_must_fail git replace -f &&
139      test "$HASH2" = "$(git replace)"
140 '
141
142 # This creates a side branch where the bug in H2
143 # does not appear because P2 is created by applying
144 # H2 and squashing H5 into it.
145 # P3, P4 and P6 are created by cherry-picking H3, H4
146 # and H6 respectively.
147 #
148 # At this point, we should have the following:
149 #
150 #    P2--P3--P4--P6
151 #   /
152 # H1-H2-H3-H4-H5-H6-H7
153 #
154 # Then we replace H6 with P6.
155 #
156 test_expect_success 'create parallel branch without the bug' '
157      git replace -d $HASH2 &&
158      git show $HASH2 | grep "A U Thor" &&
159      git checkout $HASH1 &&
160      git cherry-pick $HASH2 &&
161      git show $HASH5 | git apply &&
162      git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
163      PARA2=$(git rev-parse --verify HEAD) &&
164      git cherry-pick $HASH3 &&
165      PARA3=$(git rev-parse --verify HEAD) &&
166      git cherry-pick $HASH4 &&
167      PARA4=$(git rev-parse --verify HEAD) &&
168      git cherry-pick $HASH6 &&
169      PARA6=$(git rev-parse --verify HEAD) &&
170      git replace $HASH6 $PARA6 &&
171      git checkout master &&
172      cur=$(git rev-parse --verify HEAD) &&
173      test "$cur" = "$HASH7" &&
174      git log --pretty=oneline | grep $PARA2 &&
175      git remote add cloned ./clone_dir
176 '
177
178 test_expect_success 'push to cloned repo' '
179      git push cloned $HASH6^:refs/heads/parallel &&
180      cd clone_dir &&
181      git checkout parallel &&
182      git log --pretty=oneline | grep $PARA2 &&
183      cd ..
184 '
185
186 test_expect_success 'push branch with replacement' '
187      git cat-file commit $PARA3 | grep "author A U Thor" &&
188      S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
189      git cat-file commit $S | grep "author O Thor" &&
190      git replace $PARA3 $S &&
191      git show $HASH6~2 | grep "O Thor" &&
192      git show $PARA3 | grep "O Thor" &&
193      git push cloned $HASH6^:refs/heads/parallel2 &&
194      cd clone_dir &&
195      git checkout parallel2 &&
196      git log --pretty=oneline | grep $PARA3 &&
197      git show $PARA3 | grep "A U Thor" &&
198      cd ..
199 '
200
201 test_expect_success 'fetch branch with replacement' '
202      git branch tofetch $HASH6 &&
203      cd clone_dir &&
204      git fetch origin refs/heads/tofetch:refs/heads/parallel3
205      git log --pretty=oneline parallel3 | grep $PARA3
206      git show $PARA3 | grep "A U Thor"
207      cd ..
208 '
209
210 test_expect_success 'bisect and replacements' '
211      git bisect start $HASH7 $HASH1 &&
212      test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
213      git bisect reset &&
214      GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
215      test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
216      git bisect reset &&
217      git --no-replace-objects bisect start $HASH7 $HASH1 &&
218      test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
219      git bisect reset
220 '
221
222 #
223 #
224 test_done