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