]> asedeno.scripts.mit.edu Git - git.git/blob - t/t0000-basic.sh
test-lib: Infrastructure to test and check for prerequisites
[git.git] / t / t0000-basic.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 test_description='Test the very basics part #1.
7
8 The rest of the test suite does not check the basic operation of git
9 plumbing commands to work very carefully.  Their job is to concentrate
10 on tricky features that caused bugs in the past to detect regression.
11
12 This test runs very basic features, like registering things in cache,
13 writing tree, etc.
14
15 Note that this test *deliberately* hard-codes many expected object
16 IDs.  When object ID computation changes, like in the previous case of
17 swapping compression and hashing order, the person who is making the
18 modification *should* take notice and update the test vectors here.
19 '
20
21 ################################################################
22 # It appears that people try to run tests without building...
23
24 ../git >/dev/null
25 if test $? != 1
26 then
27         echo >&2 'You do not seem to have built git yet.'
28         exit 1
29 fi
30
31 . ./test-lib.sh
32
33 ################################################################
34 # git init has been done in an empty repository.
35 # make sure it is empty.
36
37 find .git/objects -type f -print >should-be-empty
38 test_expect_success \
39     '.git/objects should be empty after git init in an empty repo.' \
40     'cmp -s /dev/null should-be-empty'
41
42 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
43 # 3 is counting "objects" itself
44 find .git/objects -type d -print >full-of-directories
45 test_expect_success \
46     '.git/objects should have 3 subdirectories.' \
47     'test $(wc -l < full-of-directories) = 3'
48
49 ################################################################
50 # Test harness
51 test_expect_success 'success is reported like this' '
52     :
53 '
54 test_expect_failure 'pretend we have a known breakage' '
55     false
56 '
57 test_expect_failure 'pretend we have fixed a known breakage' '
58     :
59 '
60 test_set_prereq HAVEIT
61 haveit=no
62 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
63     test_have_prereq HAVEIT &&
64     haveit=yes
65 '
66 donthaveit=yes
67 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
68     donthaveit=no
69 '
70 if test $haveit$donthaveit != yesyes
71 then
72         say "bug in test framework: prerequisite tags do not work reliably"
73         exit 1
74 fi
75
76 ################################################################
77 # Basics of the basics
78
79 # updating a new file without --add should fail.
80 test_expect_success 'git update-index without --add should fail adding.' '
81     test_must_fail git update-index should-be-empty
82 '
83
84 # and with --add it should succeed, even if it is empty (it used to fail).
85 test_expect_success \
86     'git update-index with --add should succeed.' \
87     'git update-index --add should-be-empty'
88
89 test_expect_success \
90     'writing tree out with git write-tree' \
91     'tree=$(git write-tree)'
92
93 # we know the shape and contents of the tree and know the object ID for it.
94 test_expect_success \
95     'validate object ID of a known tree.' \
96     'test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a'
97
98 # Removing paths.
99 rm -f should-be-empty full-of-directories
100 test_expect_success 'git update-index without --remove should fail removing.' '
101     test_must_fail git update-index should-be-empty
102 '
103
104 test_expect_success \
105     'git update-index with --remove should be able to remove.' \
106     'git update-index --remove should-be-empty'
107
108 # Empty tree can be written with recent write-tree.
109 test_expect_success \
110     'git write-tree should be able to write an empty tree.' \
111     'tree=$(git write-tree)'
112
113 test_expect_success \
114     'validate object ID of a known tree.' \
115     'test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904'
116
117 # Various types of objects
118 mkdir path2 path3 path3/subp3
119 for p in path0 path2/file2 path3/file3 path3/subp3/file3
120 do
121     echo "hello $p" >$p
122     ln -s "hello $p" ${p}sym
123 done
124 test_expect_success \
125     'adding various types of objects with git update-index --add.' \
126     'find path* ! -type d -print | xargs git update-index --add'
127
128 # Show them and see that matches what we expect.
129 test_expect_success \
130     'showing stage with git ls-files --stage' \
131     'git ls-files --stage >current'
132
133 cat >expected <<\EOF
134 100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
135 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
136 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
137 120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
138 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
139 120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
140 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
141 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
142 EOF
143 test_expect_success \
144     'validate git ls-files output for a known tree.' \
145     'test_cmp expected current'
146
147 test_expect_success \
148     'writing tree out with git write-tree.' \
149     'tree=$(git write-tree)'
150 test_expect_success \
151     'validate object ID for a known tree.' \
152     'test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b'
153
154 test_expect_success \
155     'showing tree with git ls-tree' \
156     'git ls-tree $tree >current'
157 cat >expected <<\EOF
158 100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
159 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
160 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
161 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
162 EOF
163 test_expect_success \
164     'git ls-tree output for a known tree.' \
165     'test_cmp expected current'
166
167 # This changed in ls-tree pathspec change -- recursive does
168 # not show tree nodes anymore.
169 test_expect_success \
170     'showing tree with git ls-tree -r' \
171     'git ls-tree -r $tree >current'
172 cat >expected <<\EOF
173 100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
174 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
175 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
176 120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
177 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
178 120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
179 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
180 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
181 EOF
182 test_expect_success \
183     'git ls-tree -r output for a known tree.' \
184     'test_cmp expected current'
185
186 # But with -r -t we can have both.
187 test_expect_success \
188     'showing tree with git ls-tree -r -t' \
189     'git ls-tree -r -t $tree >current'
190 cat >expected <<\EOF
191 100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
192 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
193 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
194 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
195 120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
196 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
197 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
198 120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
199 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
200 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
201 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
202 EOF
203 test_expect_success \
204     'git ls-tree -r output for a known tree.' \
205     'test_cmp expected current'
206
207 test_expect_success \
208     'writing partial tree out with git write-tree --prefix.' \
209     'ptree=$(git write-tree --prefix=path3)'
210 test_expect_success \
211     'validate object ID for a known tree.' \
212     'test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3'
213
214 test_expect_success \
215     'writing partial tree out with git write-tree --prefix.' \
216     'ptree=$(git write-tree --prefix=path3/subp3)'
217 test_expect_success \
218     'validate object ID for a known tree.' \
219     'test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2'
220
221 cat >badobjects <<EOF
222 100644 blob 1000000000000000000000000000000000000000    dir/file1
223 100644 blob 2000000000000000000000000000000000000000    dir/file2
224 100644 blob 3000000000000000000000000000000000000000    dir/file3
225 100644 blob 4000000000000000000000000000000000000000    dir/file4
226 100644 blob 5000000000000000000000000000000000000000    dir/file5
227 EOF
228
229 rm .git/index
230 test_expect_success \
231     'put invalid objects into the index.' \
232     'git update-index --index-info < badobjects'
233
234 test_expect_success 'writing this tree without --missing-ok.' '
235     test_must_fail git write-tree
236 '
237
238 test_expect_success \
239     'writing this tree with --missing-ok.' \
240     'git write-tree --missing-ok'
241
242
243 ################################################################
244 rm .git/index
245 test_expect_success \
246     'git read-tree followed by write-tree should be idempotent.' \
247     'git read-tree $tree &&
248      test -f .git/index &&
249      newtree=$(git write-tree) &&
250      test "$newtree" = "$tree"'
251
252 cat >expected <<\EOF
253 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
254 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
255 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
256 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
257 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
258 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
259 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
260 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
261 EOF
262 test_expect_success \
263     'validate git diff-files output for a know cache/work tree state.' \
264     'git diff-files >current && diff >/dev/null -b current expected'
265
266 test_expect_success \
267     'git update-index --refresh should succeed.' \
268     'git update-index --refresh'
269
270 test_expect_success \
271     'no diff after checkout and git update-index --refresh.' \
272     'git diff-files >current && cmp -s current /dev/null'
273
274 ################################################################
275 P=087704a96baf1c2d1c869a8b084481e121c88b5b
276 test_expect_success \
277     'git commit-tree records the correct tree in a commit.' \
278     'commit0=$(echo NO | git commit-tree $P) &&
279      tree=$(git show --pretty=raw $commit0 |
280          sed -n -e "s/^tree //p" -e "/^author /q") &&
281      test "z$tree" = "z$P"'
282
283 test_expect_success \
284     'git commit-tree records the correct parent in a commit.' \
285     'commit1=$(echo NO | git commit-tree $P -p $commit0) &&
286      parent=$(git show --pretty=raw $commit1 |
287          sed -n -e "s/^parent //p" -e "/^author /q") &&
288      test "z$commit0" = "z$parent"'
289
290 test_expect_success \
291     'git commit-tree omits duplicated parent in a commit.' \
292     'commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
293      parent=$(git show --pretty=raw $commit2 |
294          sed -n -e "s/^parent //p" -e "/^author /q" |
295          sort -u) &&
296      test "z$commit0" = "z$parent" &&
297      numparent=$(git show --pretty=raw $commit2 |
298          sed -n -e "s/^parent //p" -e "/^author /q" |
299          wc -l) &&
300      test $numparent = 1'
301
302 test_expect_success 'update-index D/F conflict' '
303         mv path0 tmp &&
304         mv path2 path0 &&
305         mv tmp path2 &&
306         git update-index --add --replace path2 path0/file2 &&
307         numpath0=$(git ls-files path0 | wc -l) &&
308         test $numpath0 = 1
309 '
310
311 test_expect_success 'absolute path works as expected' '
312         mkdir first &&
313         ln -s ../.git first/.git &&
314         mkdir second &&
315         ln -s ../first second/other &&
316         mkdir third &&
317         dir="$(cd .git; pwd -P)" &&
318         dir2=third/../second/other/.git &&
319         test "$dir" = "$(test-path-utils make_absolute_path $dir2)" &&
320         file="$dir"/index &&
321         test "$file" = "$(test-path-utils make_absolute_path $dir2/index)" &&
322         basename=blub &&
323         test "$dir/$basename" = "$(cd .git && test-path-utils make_absolute_path "$basename")" &&
324         ln -s ../first/file .git/syml &&
325         sym="$(cd first; pwd -P)"/file &&
326         test "$sym" = "$(test-path-utils make_absolute_path "$dir2/syml")"
327 '
328
329 test_expect_success 'very long name in the index handled sanely' '
330
331         a=a && # 1
332         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
333         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
334         a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
335         a=${a}q &&
336
337         >path4 &&
338         git update-index --add path4 &&
339         (
340                 git ls-files -s path4 |
341                 sed -e "s/      .*/     /" |
342                 tr -d "\012"
343                 echo "$a"
344         ) | git update-index --index-info &&
345         len=$(git ls-files "a*" | wc -c) &&
346         test $len = 4098
347 '
348
349 test_done