1#!/bin/sh
2#
3# Copyright (c) 2006 Shawn Pearce
4#
5
6test_description='Test git update-ref and basic ref logging'
7. ./test-lib.sh
8
9Z=0000000000000000000000000000000000000000
10
11test_expect_success setup '
12
13 for name in A B C D E F
14 do
15 test_tick &&
16 T=$(git write-tree) &&
17 sha1=$(echo $name | git commit-tree $T) &&
18 eval $name=$sha1
19 done
20
21'
22
23m=refs/heads/master
24n_dir=refs/heads/gu
25n=$n_dir/fixes
26
27test_expect_success \
28 "create $m" \
29 "git update-ref $m $A &&
30 test $A"' = $(cat .git/'"$m"')'
31test_expect_success \
32 "create $m" \
33 "git update-ref $m $B $A &&
34 test $B"' = $(cat .git/'"$m"')'
35test_expect_success "fail to delete $m with stale ref" '
36 test_must_fail git update-ref -d $m $A &&
37 test $B = "$(cat .git/$m)"
38'
39test_expect_success "delete $m" '
40 git update-ref -d $m $B &&
41 ! test -f .git/$m
42'
43rm -f .git/$m
44
45test_expect_success "delete $m without oldvalue verification" "
46 git update-ref $m $A &&
47 test $A = \$(cat .git/$m) &&
48 git update-ref -d $m &&
49 ! test -f .git/$m
50"
51rm -f .git/$m
52
53test_expect_success \
54 "fail to create $n" \
55 "touch .git/$n_dir
56 git update-ref $n $A >out 2>err"'
57 test $? != 0'
58rm -f .git/$n_dir out err
59
60test_expect_success \
61 "create $m (by HEAD)" \
62 "git update-ref HEAD $A &&
63 test $A"' = $(cat .git/'"$m"')'
64test_expect_success \
65 "create $m (by HEAD)" \
66 "git update-ref HEAD $B $A &&
67 test $B"' = $(cat .git/'"$m"')'
68test_expect_success "fail to delete $m (by HEAD) with stale ref" '
69 test_must_fail git update-ref -d HEAD $A &&
70 test $B = $(cat .git/$m)
71'
72test_expect_success "delete $m (by HEAD)" '
73 git update-ref -d HEAD $B &&
74 ! test -f .git/$m
75'
76rm -f .git/$m
77
78cp -f .git/HEAD .git/HEAD.orig
79test_expect_success "delete symref without dereference" '
80 git update-ref --no-deref -d HEAD &&
81 ! test -f .git/HEAD
82'
83cp -f .git/HEAD.orig .git/HEAD
84
85test_expect_success '(not) create HEAD with old sha1' "
86 test_must_fail git update-ref HEAD $A $B
87"
88test_expect_success "(not) prior created .git/$m" "
89 ! test -f .git/$m
90"
91rm -f .git/$m
92
93test_expect_success \
94 "create HEAD" \
95 "git update-ref HEAD $A"
96test_expect_success '(not) change HEAD with wrong SHA1' "
97 test_must_fail git update-ref HEAD $B $Z
98"
99test_expect_success "(not) changed .git/$m" "
100 ! test $B"' = $(cat .git/'"$m"')
101'
102rm -f .git/$m
103
104: a repository with working tree always has reflog these days...
105: >.git/logs/refs/heads/master
106test_expect_success \
107 "create $m (logged by touch)" \
108 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
109 git update-ref HEAD '"$A"' -m "Initial Creation" &&
110 test '"$A"' = $(cat .git/'"$m"')'
111test_expect_success \
112 "update $m (logged by touch)" \
113 'GIT_COMMITTER_DATE="2005-05-26 23:31" \
114 git update-ref HEAD'" $B $A "'-m "Switch" &&
115 test '"$B"' = $(cat .git/'"$m"')'
116test_expect_success \
117 "set $m (logged by touch)" \
118 'GIT_COMMITTER_DATE="2005-05-26 23:41" \
119 git update-ref HEAD'" $A &&
120 test $A"' = $(cat .git/'"$m"')'
121
122cat >expect <<EOF
123$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
124$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
125$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
126EOF
127test_expect_success \
128 "verifying $m's log" \
129 "diff expect .git/logs/$m"
130rm -rf .git/$m .git/logs expect
131
132test_expect_success \
133 'enable core.logAllRefUpdates' \
134 'git config core.logAllRefUpdates true &&
135 test true = $(git config --bool --get core.logAllRefUpdates)'
136
137test_expect_success \
138 "create $m (logged by config)" \
139 'GIT_COMMITTER_DATE="2005-05-26 23:32" \
140 git update-ref HEAD'" $A "'-m "Initial Creation" &&
141 test '"$A"' = $(cat .git/'"$m"')'
142test_expect_success \
143 "update $m (logged by config)" \
144 'GIT_COMMITTER_DATE="2005-05-26 23:33" \
145 git update-ref HEAD'" $B $A "'-m "Switch" &&
146 test '"$B"' = $(cat .git/'"$m"')'
147test_expect_success \
148 "set $m (logged by config)" \
149 'GIT_COMMITTER_DATE="2005-05-26 23:43" \
150 git update-ref HEAD '"$A &&
151 test $A"' = $(cat .git/'"$m"')'
152
153cat >expect <<EOF
154$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
155$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
156$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
157EOF
158test_expect_success \
159 "verifying $m's log" \
160 'diff expect .git/logs/$m'
161rm -f .git/$m .git/logs/$m expect
162
163git update-ref $m $D
164cat >.git/logs/$m <<EOF
1650000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
166$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
167$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
168$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
169$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
170EOF
171
172ed="Thu, 26 May 2005 18:32:00 -0500"
173gd="Thu, 26 May 2005 18:33:00 -0500"
174ld="Thu, 26 May 2005 18:43:00 -0500"
175test_expect_success \
176 'Query "master@{May 25 2005}" (before history)' \
177 'rm -f o e
178 git rev-parse --verify "master@{May 25 2005}" >o 2>e &&
179 test '"$C"' = $(cat o) &&
180 test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
181test_expect_success \
182 "Query master@{2005-05-25} (before history)" \
183 'rm -f o e
184 git rev-parse --verify master@{2005-05-25} >o 2>e &&
185 test '"$C"' = $(cat o) &&
186 echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
187test_expect_success \
188 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
189 'rm -f o e
190 git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
191 test '"$C"' = $(cat o) &&
192 test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
193test_expect_success \
194 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
195 'rm -f o e
196 git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
197 test '"$C"' = $(cat o) &&
198 test "" = "$(cat e)"'
199test_expect_success \
200 'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \
201 'rm -f o e
202 git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e &&
203 test '"$A"' = $(cat o) &&
204 test "" = "$(cat e)"'
205test_expect_success \
206 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
207 'rm -f o e
208 git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
209 test '"$B"' = $(cat o) &&
210 test "warning: Log .git/logs/'"$m has gap after $gd"'." = "$(cat e)"'
211test_expect_success \
212 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
213 'rm -f o e
214 git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
215 test '"$Z"' = $(cat o) &&
216 test "" = "$(cat e)"'
217test_expect_success \
218 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
219 'rm -f o e
220 git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
221 test '"$E"' = $(cat o) &&
222 test "" = "$(cat e)"'
223test_expect_success \
224 'Query "master@{2005-05-28}" (past end of history)' \
225 'rm -f o e
226 git rev-parse --verify "master@{2005-05-28}" >o 2>e &&
227 test '"$D"' = $(cat o) &&
228 test "warning: Log .git/logs/'"$m unexpectedly ended on $ld"'." = "$(cat e)"'
229
230
231rm -f .git/$m .git/logs/$m expect
232
233test_expect_success \
234 'creating initial files' \
235 'echo TEST >F &&
236 git add F &&
237 GIT_AUTHOR_DATE="2005-05-26 23:30" \
238 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
239 h_TEST=$(git rev-parse --verify HEAD)
240 echo The other day this did not work. >M &&
241 echo And then Bob told me how to fix it. >>M &&
242 echo OTHER >F &&
243 GIT_AUTHOR_DATE="2005-05-26 23:41" \
244 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
245 h_OTHER=$(git rev-parse --verify HEAD) &&
246 GIT_AUTHOR_DATE="2005-05-26 23:44" \
247 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
248 h_FIXED=$(git rev-parse --verify HEAD) &&
249 echo Merged initial commit and a later commit. >M &&
250 echo $h_TEST >.git/MERGE_HEAD &&
251 GIT_AUTHOR_DATE="2005-05-26 23:45" \
252 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
253 h_MERGED=$(git rev-parse --verify HEAD) &&
254 rm -f M'
255
256cat >expect <<EOF
257$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
258$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
259$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
260$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
261EOF
262test_expect_success \
263 'git commit logged updates' \
264 "diff expect .git/logs/$m"
265unset h_TEST h_OTHER h_FIXED h_MERGED
266
267test_expect_success \
268 'git cat-file blob master:F (expect OTHER)' \
269 'test OTHER = $(git cat-file blob master:F)'
270test_expect_success \
271 'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
272 'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")'
273test_expect_success \
274 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
275 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")'
276
277test_done