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
10A=1111111111111111111111111111111111111111
11B=2222222222222222222222222222222222222222
12C=3333333333333333333333333333333333333333
13D=4444444444444444444444444444444444444444
14E=5555555555555555555555555555555555555555
15F=6666666666666666666666666666666666666666
16m=refs/heads/master
17n_dir=refs/heads/gu
18n=$n_dir/fixes
19
20test_expect_success \
21 "create $m" \
22 "git-update-ref $m $A &&
23 test $A"' = $(cat .git/'"$m"')'
24test_expect_success \
25 "create $m" \
26 "git-update-ref $m $B $A &&
27 test $B"' = $(cat .git/'"$m"')'
28rm -f .git/$m
29
30test_expect_success \
31 "fail to create $n" \
32 "touch .git/$n_dir
33 git-update-ref $n $A >out 2>err
34 test "'$? = 1 &&
35 test "" = "$(cat out)" &&
36 grep "error: unable to resolve reference" err &&
37 grep '"$n err"
38rm -f .git/$n_dir out err
39
40test_expect_success \
41 "create $m (by HEAD)" \
42 "git-update-ref HEAD $A &&
43 test $A"' = $(cat .git/'"$m"')'
44test_expect_success \
45 "create $m (by HEAD)" \
46 "git-update-ref HEAD $B $A &&
47 test $B"' = $(cat .git/'"$m"')'
48rm -f .git/$m
49
50test_expect_failure \
51 '(not) create HEAD with old sha1' \
52 "git-update-ref HEAD $A $B"
53test_expect_failure \
54 "(not) prior created .git/$m" \
55 "test -f .git/$m"
56rm -f .git/$m
57
58test_expect_success \
59 "create HEAD" \
60 "git-update-ref HEAD $A"
61test_expect_failure \
62 '(not) change HEAD with wrong SHA1' \
63 "git-update-ref HEAD $B $Z"
64test_expect_failure \
65 "(not) changed .git/$m" \
66 "test $B"' = $(cat .git/'"$m"')'
67rm -f .git/$m
68
69mkdir -p .git/logs/refs/heads
70touch .git/logs/refs/heads/master
71test_expect_success \
72 "create $m (logged by touch)" \
73 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
74 git-update-ref HEAD '"$A"' -m "Initial Creation" &&
75 test '"$A"' = $(cat .git/'"$m"')'
76test_expect_success \
77 "update $m (logged by touch)" \
78 'GIT_COMMITTER_DATE="2005-05-26 23:31" \
79 git-update-ref HEAD'" $B $A "'-m "Switch" &&
80 test '"$B"' = $(cat .git/'"$m"')'
81test_expect_success \
82 "set $m (logged by touch)" \
83 'GIT_COMMITTER_DATE="2005-05-26 23:41" \
84 git-update-ref HEAD'" $A &&
85 test $A"' = $(cat .git/'"$m"')'
86
87cat >expect <<EOF
88$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
89$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
90$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
91EOF
92test_expect_success \
93 "verifying $m's log" \
94 "diff expect .git/logs/$m"
95rm -rf .git/$m .git/logs expect
96
97test_expect_success \
98 'enable core.logAllRefUpdates' \
99 'git-repo-config core.logAllRefUpdates true &&
100 test true = $(git-repo-config --bool --get core.logAllRefUpdates)'
101
102test_expect_success \
103 "create $m (logged by config)" \
104 'GIT_COMMITTER_DATE="2005-05-26 23:32" \
105 git-update-ref HEAD'" $A "'-m "Initial Creation" &&
106 test '"$A"' = $(cat .git/'"$m"')'
107test_expect_success \
108 "update $m (logged by config)" \
109 'GIT_COMMITTER_DATE="2005-05-26 23:33" \
110 git-update-ref HEAD'" $B $A "'-m "Switch" &&
111 test '"$B"' = $(cat .git/'"$m"')'
112test_expect_success \
113 "set $m (logged by config)" \
114 'GIT_COMMITTER_DATE="2005-05-26 23:43" \
115 git-update-ref HEAD '"$A &&
116 test $A"' = $(cat .git/'"$m"')'
117
118cat >expect <<EOF
119$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
120$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
121$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
122EOF
123test_expect_success \
124 "verifying $m's log" \
125 'diff expect .git/logs/$m'
126rm -f .git/$m .git/logs/$m expect
127
128git-update-ref $m $D
129cat >.git/logs/$m <<EOF
130$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
131$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
132$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
133$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
134EOF
135
136ed="Thu, 26 May 2005 18:32:00 -0500"
137gd="Thu, 26 May 2005 18:33:00 -0500"
138ld="Thu, 26 May 2005 18:43:00 -0500"
139test_expect_success \
140 'Query "master@{May 25 2005}" (before history)' \
141 'rm -f o e
142 git-rev-parse --verify "master@{May 25 2005}" >o 2>e &&
143 test '"$C"' = $(cat o) &&
144 test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
145test_expect_success \
146 "Query master@{2005-05-25} (before history)" \
147 'rm -f o e
148 git-rev-parse --verify master@{2005-05-25} >o 2>e &&
149 test '"$C"' = $(cat o) &&
150 echo test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
151test_expect_success \
152 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
153 'rm -f o e
154 git-rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
155 test '"$C"' = $(cat o) &&
156 test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
157test_expect_success \
158 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
159 'rm -f o e
160 git-rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
161 test '"$A"' = $(cat o) &&
162 test "" = "$(cat e)"'
163test_expect_success \
164 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
165 'rm -f o e
166 git-rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
167 test '"$B"' = $(cat o) &&
168 test "warning: Log .git/logs/'"$m has gap after $gd"'." = "$(cat e)"'
169test_expect_success \
170 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
171 'rm -f o e
172 git-rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
173 test '"$Z"' = $(cat o) &&
174 test "" = "$(cat e)"'
175test_expect_success \
176 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
177 'rm -f o e
178 git-rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
179 test '"$E"' = $(cat o) &&
180 test "" = "$(cat e)"'
181test_expect_success \
182 'Query "master@{2005-05-28}" (past end of history)' \
183 'rm -f o e
184 git-rev-parse --verify "master@{2005-05-28}" >o 2>e &&
185 test '"$D"' = $(cat o) &&
186 test "warning: Log .git/logs/'"$m unexpectedly ended on $ld"'." = "$(cat e)"'
187
188
189rm -f .git/$m .git/logs/$m expect
190
191test_expect_success \
192 'creating initial files' \
193 'echo TEST >F &&
194 git-add F &&
195 GIT_AUTHOR_DATE="2005-05-26 23:30" \
196 GIT_COMMITTER_DATE="2005-05-26 23:30" git-commit -m add -a &&
197 h_TEST=$(git-rev-parse --verify HEAD)
198 echo The other day this did not work. >M &&
199 echo And then Bob told me how to fix it. >>M &&
200 echo OTHER >F &&
201 GIT_AUTHOR_DATE="2005-05-26 23:41" \
202 GIT_COMMITTER_DATE="2005-05-26 23:41" git-commit -F M -a &&
203 h_OTHER=$(git-rev-parse --verify HEAD) &&
204 echo FIXED >F &&
205 GIT_AUTHOR_DATE="2005-05-26 23:44" \
206 GIT_COMMITTER_DATE="2005-05-26 23:44" git-commit --amend &&
207 h_FIXED=$(git-rev-parse --verify HEAD) &&
208 echo TEST+FIXED >F &&
209 echo Merged initial commit and a later commit. >M &&
210 echo $h_TEST >.git/MERGE_HEAD &&
211 GIT_AUTHOR_DATE="2005-05-26 23:45" \
212 GIT_COMMITTER_DATE="2005-05-26 23:45" git-commit -F M &&
213 h_MERGED=$(git-rev-parse --verify HEAD)
214 rm -f M'
215
216cat >expect <<EOF
217$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
218$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
219$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
220$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
221EOF
222test_expect_success \
223 'git-commit logged updates' \
224 "diff expect .git/logs/$m"
225unset h_TEST h_OTHER h_FIXED h_MERGED
226
227test_expect_success \
228 'git-cat-file blob master:F (expect OTHER)' \
229 'test OTHER = $(git-cat-file blob master:F)'
230test_expect_success \
231 'git-cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
232 'test TEST = $(git-cat-file blob "master@{2005-05-26 23:30}:F")'
233test_expect_success \
234 'git-cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
235 'test OTHER = $(git-cat-file blob "master@{2005-05-26 23:42}:F")'
236
237test_done