Merge branch 'ds/reprepare-alternates-when-repreparing-packfiles'
[alt-git.git] / t / t2030-unresolve-info.sh
blob2d8c70b03a50e9ca082f7343d78dbcf6ad456159
1 #!/bin/sh
3 test_description='undoing resolution'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 check_resolve_undo () {
11 msg=$1
12 shift
13 while case $# in
14 0) break ;;
15 1|2|3) die "Bug in check-resolve-undo test" ;;
16 esac
18 path=$1
19 shift
20 for stage in 1 2 3
22 sha1=$1
23 shift
24 case "$sha1" in
25 '') continue ;;
26 esac
27 sha1=$(git rev-parse --verify "$sha1")
28 printf "100644 %s %s\t%s\n" $sha1 $stage $path
29 done
30 done >"$msg.expect" &&
31 git ls-files --resolve-undo >"$msg.actual" &&
32 test_cmp "$msg.expect" "$msg.actual"
35 prime_resolve_undo () {
36 git reset --hard &&
37 git checkout second^0 &&
38 test_tick &&
39 test_must_fail git merge third^0 &&
40 echo merge does not leave anything &&
41 check_resolve_undo empty &&
42 echo different >fi/le &&
43 git add fi/le &&
44 echo resolving records &&
45 check_resolve_undo recorded fi/le initial:fi/le second:fi/le third:fi/le
48 test_expect_success setup '
49 mkdir fi &&
50 printf "a\0a" >binary &&
51 git add binary &&
52 test_commit initial fi/le first &&
53 git branch side &&
54 git branch another &&
55 printf "a\0b" >binary &&
56 git add binary &&
57 test_commit second fi/le second &&
58 git checkout side &&
59 test_commit third fi/le third &&
60 git branch add-add &&
61 git checkout another &&
62 test_commit fourth fi/le fourth &&
63 git checkout add-add &&
64 test_commit fifth add-differently &&
65 git checkout main
68 test_expect_success 'add records switch clears' '
69 prime_resolve_undo &&
70 test_tick &&
71 git commit -m merged &&
72 echo committing keeps &&
73 check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
74 git checkout second^0 &&
75 echo switching clears &&
76 check_resolve_undo cleared
79 test_expect_success 'rm records reset clears' '
80 prime_resolve_undo &&
81 test_tick &&
82 git commit -m merged &&
83 echo committing keeps &&
84 check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
86 echo merge clears upfront &&
87 test_must_fail git merge fourth^0 &&
88 check_resolve_undo nuked &&
90 git rm -f fi/le &&
91 echo resolving records &&
92 check_resolve_undo recorded fi/le initial:fi/le HEAD:fi/le fourth:fi/le &&
94 git reset --hard &&
95 echo resetting discards &&
96 check_resolve_undo discarded
99 test_expect_success 'plumbing clears' '
100 prime_resolve_undo &&
101 test_tick &&
102 git commit -m merged &&
103 echo committing keeps &&
104 check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
106 echo plumbing clear &&
107 git update-index --clear-resolve-undo &&
108 check_resolve_undo cleared
111 test_expect_success 'add records checkout -m undoes' '
112 prime_resolve_undo &&
113 git diff HEAD &&
114 git checkout --conflict=merge fi/le &&
115 echo checkout used the record and removed it &&
116 check_resolve_undo removed &&
117 echo the index and the work tree is unmerged again &&
118 git diff >actual &&
119 grep "^++<<<<<<<" actual
122 test_expect_success 'unmerge with plumbing' '
123 prime_resolve_undo &&
124 git update-index --unresolve fi/le &&
125 git ls-files -u >actual &&
126 test_line_count = 3 actual
129 test_expect_success 'rerere and rerere forget' '
130 mkdir .git/rr-cache &&
131 prime_resolve_undo &&
132 echo record the resolution &&
133 git rerere &&
134 rerere_id=$(cd .git/rr-cache && echo */postimage) &&
135 rerere_id=${rerere_id%/postimage} &&
136 test -f .git/rr-cache/$rerere_id/postimage &&
137 git checkout -m fi/le &&
138 echo resurrect the conflict &&
139 grep "^=======" fi/le &&
140 echo reresolve the conflict &&
141 git rerere &&
142 test "z$(cat fi/le)" = zdifferent &&
143 echo register the resolution again &&
144 git add fi/le &&
145 check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
146 test -z "$(git ls-files -u)" &&
147 git rerere forget fi/le &&
148 ! test -f .git/rr-cache/$rerere_id/postimage &&
149 tr "\0" "\n" <.git/MERGE_RR >actual &&
150 echo "$rerere_id fi/le" >expect &&
151 test_cmp expect actual
154 test_expect_success 'rerere and rerere forget (subdirectory)' '
155 rm -fr .git/rr-cache &&
156 mkdir .git/rr-cache &&
157 prime_resolve_undo &&
158 echo record the resolution &&
159 (cd fi && git rerere) &&
160 rerere_id=$(cd .git/rr-cache && echo */postimage) &&
161 rerere_id=${rerere_id%/postimage} &&
162 test -f .git/rr-cache/$rerere_id/postimage &&
163 (cd fi && git checkout -m le) &&
164 echo resurrect the conflict &&
165 grep "^=======" fi/le &&
166 echo reresolve the conflict &&
167 (cd fi && git rerere) &&
168 test "z$(cat fi/le)" = zdifferent &&
169 echo register the resolution again &&
170 (cd fi && git add le) &&
171 check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
172 test -z "$(git ls-files -u)" &&
173 (cd fi && git rerere forget le) &&
174 ! test -f .git/rr-cache/$rerere_id/postimage &&
175 tr "\0" "\n" <.git/MERGE_RR >actual &&
176 echo "$rerere_id fi/le" >expect &&
177 test_cmp expect actual
180 test_expect_success 'rerere forget (binary)' '
181 git checkout -f side &&
182 test_commit --printf binary binary "a\0c" &&
183 test_must_fail git merge second &&
184 git rerere forget binary
187 test_expect_success 'rerere forget (add-add conflict)' '
188 git checkout -f main &&
189 echo main >add-differently &&
190 git add add-differently &&
191 git commit -m "add differently" &&
192 test_must_fail git merge fifth &&
193 git rerere forget add-differently 2>actual &&
194 test_i18ngrep "no remembered" actual
197 test_expect_success 'resolve-undo keeps blobs from gc' '
198 git checkout -f main &&
200 # First make sure we do not have any cruft left in the object store
201 git repack -a -d &&
202 git prune --expire=now &&
203 git prune-packed &&
204 git gc --prune=now &&
205 git fsck --unreachable >cruft &&
206 test_must_be_empty cruft &&
208 # Now add three otherwise unreferenced blob objects to the index
209 git reset --hard &&
210 B1=$(echo "resolve undo test data 1" | git hash-object -w --stdin) &&
211 B2=$(echo "resolve undo test data 2" | git hash-object -w --stdin) &&
212 B3=$(echo "resolve undo test data 3" | git hash-object -w --stdin) &&
213 git update-index --add --index-info <<-EOF &&
214 100644 $B1 1 frotz
215 100644 $B2 2 frotz
216 100644 $B3 3 frotz
219 # These three blob objects are reachable (only) from the index
220 git fsck --unreachable >cruft &&
221 test_must_be_empty cruft &&
222 # and they should be protected from GC
223 git gc --prune=now &&
224 git cat-file -e $B1 &&
225 git cat-file -e $B2 &&
226 git cat-file -e $B3 &&
228 # Now resolve the conflicted path
229 B0=$(echo "resolve undo test data 0" | git hash-object -w --stdin) &&
230 git update-index --add --cacheinfo 100644,$B0,frotz &&
232 # These three blob objects are now reachable only from the resolve-undo
233 git fsck --unreachable >cruft &&
234 test_must_be_empty cruft &&
236 # and they should survive GC
237 git gc --prune=now &&
238 git cat-file -e $B0 &&
239 git cat-file -e $B1 &&
240 git cat-file -e $B2 &&
241 git cat-file -e $B3 &&
243 # Now we switch away, which nukes resolve-undo, and
244 # blobs B0..B3 would become dangling. fsck should
245 # notice that they are now unreachable.
246 git checkout -f side &&
247 git fsck --unreachable >cruft &&
248 sort cruft >actual &&
249 sort <<-EOF >expect &&
250 unreachable blob $B0
251 unreachable blob $B1
252 unreachable blob $B2
253 unreachable blob $B3
255 test_cmp expect actual &&
257 # And they should go away when gc runs.
258 git gc --prune=now &&
259 git fsck --unreachable >cruft &&
260 test_must_be_empty cruft &&
262 test_must_fail git cat-file -e $B0 &&
263 test_must_fail git cat-file -e $B1 &&
264 test_must_fail git cat-file -e $B2 &&
265 test_must_fail git cat-file -e $B3
268 test_done