git branch -m: forbid renaming of a symref
[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 cp -f .git/HEAD .git/HEAD.orig
79 test_expect_success "delete symref without dereference" '
80         git update-ref --no-deref -d HEAD &&
81         ! test -f .git/HEAD
82 '
83 cp -f .git/HEAD.orig .git/HEAD
84
85 test_expect_success '(not) create HEAD with old sha1' "
86         test_must_fail git update-ref HEAD $A $B
87 "
88 test_expect_success "(not) prior created .git/$m" "
89         ! test -f .git/$m
90 "
91 rm -f .git/$m
92
93 test_expect_success \
94         "create HEAD" \
95         "git update-ref HEAD $A"
96 test_expect_success '(not) change HEAD with wrong SHA1' "
97         test_must_fail git update-ref HEAD $B $Z
98 "
99 test_expect_success "(not) changed .git/$m" "
100         ! test $B"' = $(cat .git/'"$m"')
101 '
102 rm -f .git/$m
103
104 : a repository with working tree always has reflog these days...
105 : >.git/logs/refs/heads/master
106 test_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"')'
111 test_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"')'
116 test_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
122 cat >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
126 EOF
127 test_expect_success \
128         "verifying $m's log" \
129         "diff expect .git/logs/$m"
130 rm -rf .git/$m .git/logs expect
131
132 test_expect_success \
133         'enable core.logAllRefUpdates' \
134         'git config core.logAllRefUpdates true &&
135          test true = $(git config --bool --get core.logAllRefUpdates)'
136
137 test_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"')'
142 test_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"')'
147 test_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
153 cat >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
157 EOF
158 test_expect_success \
159         "verifying $m's log" \
160         'diff expect .git/logs/$m'
161 rm -f .git/$m .git/logs/$m expect
162
163 git update-ref $m $D
164 cat >.git/logs/$m <<EOF
165 0000000000000000000000000000000000000000 $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
170 EOF
171
172 ed="Thu, 26 May 2005 18:32:00 -0500"
173 gd="Thu, 26 May 2005 18:33:00 -0500"
174 ld="Thu, 26 May 2005 18:43:00 -0500"
175 test_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)"'
181 test_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)"'
187 test_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)"'
193 test_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)"'
199 test_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)"'
205 test_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)"'
211 test_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)"'
217 test_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)"'
223 test_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
231 rm -f .git/$m .git/logs/$m expect
232
233 test_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
256 cat >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.
261 EOF
262 test_expect_success \
263         'git commit logged updates' \
264         "diff expect .git/logs/$m"
265 unset h_TEST h_OTHER h_FIXED h_MERGED
266
267 test_expect_success \
268         'git cat-file blob master:F (expect OTHER)' \
269         'test OTHER = $(git cat-file blob master:F)'
270 test_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")'
273 test_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
277 test_done