3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='Test the very basics part #1.
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.
12 This test runs very basic features, like registering things in cache,
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.
21 ################################################################
22 # It appears that people try to run tests without building...
27 echo >&2 'You do not seem to have built git yet.'
33 ################################################################
34 # git init has been done in an empty repository.
35 # make sure it is empty.
37 test_expect_success
'.git/objects should be empty after git init in an empty repo' '
38 find .git/objects -type f -print >should-be-empty &&
39 test_line_count = 0 should-be-empty
42 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
43 # 3 is counting "objects" itself
44 test_expect_success
'.git/objects should have 3 subdirectories' '
45 find .git/objects -type d -print >full-of-directories &&
46 test_line_count = 3 full-of-directories
49 ################################################################
51 test_expect_success
'success is reported like this' '
54 test_expect_failure
'pretend we have a known breakage' '
58 run_sub_test_lib_test
() {
59 name
="$1" descr
="$2" # stdin is the body of the test code
63 cat >"$name.sh" <<-EOF &&
66 test_description='$descr (run in sub test-lib)
68 This is run in a sub test-lib so that we do not get incorrect
72 # Point to the t/test-lib.sh, which isn't in ../ as usual
73 . "\$TEST_DIRECTORY"/test-lib.sh
76 chmod +x
"$name.sh" &&
77 export TEST_DIRECTORY
&&
78 .
/"$name.sh" >out
2>err
82 check_sub_test_lib_test
() {
83 name
="$1" # stdin is the expected output from the test
87 sed -e 's/^> //' -e 's/Z$//' >expect
&&
92 test_expect_success
'pretend we have fixed a known breakage' "
93 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
94 test_expect_failure 'pretend we have fixed a known breakage' 'true'
97 check_sub_test_lib_test passing-todo <<-\\EOF
98 > ok 1 - pretend we have fixed a known breakage # TODO known breakage
99 > # fixed 1 known breakage(s)
100 > # passed all 1 test(s)
105 test_set_prereq HAVEIT
107 test_expect_success HAVEIT
'test runs if prerequisite is satisfied' '
108 test_have_prereq HAVEIT &&
112 test_expect_success DONTHAVEIT
'unmet prerequisite causes test to be skipped' '
115 if test $haveit$donthaveit != yesyes
117 say
"bug in test framework: prerequisite tags do not work reliably"
121 test_set_prereq HAVETHIS
123 test_expect_success HAVETHIS
,HAVEIT
'test runs if prerequisites are satisfied' '
124 test_have_prereq HAVEIT &&
125 test_have_prereq HAVETHIS &&
129 test_expect_success HAVEIT
,DONTHAVEIT
'unmet prerequisites causes test to be skipped' '
133 test_expect_success DONTHAVEIT
,HAVEIT
'unmet prerequisites causes test to be skipped' '
136 if test $haveit$donthaveit$donthaveiteither != yesyesyes
138 say
"bug in test framework: multiple prerequisite tags do not work reliably"
143 test_expect_success
'tests clean up after themselves' '
144 test_when_finished clean=yes
147 if test $clean != yes
149 say
"bug in test framework: basic cleanup command does not work reliably"
153 test_expect_success
'tests clean up even on failures' "
154 test_must_fail run_sub_test_lib_test \
155 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
156 test_expect_success 'tests clean up even after a failure' '
157 touch clean-after-failure &&
158 test_when_finished rm clean-after-failure &&
161 test_expect_success 'failure to clean up causes the test to fail' '
162 test_when_finished \"(exit 2)\"
166 check_sub_test_lib_test failing-cleanup <<-\\EOF
167 > not ok 1 - tests clean up even after a failure
169 > # touch clean-after-failure &&
170 > # test_when_finished rm clean-after-failure &&
173 > not ok 2 - failure to clean up causes the test to fail
175 > # test_when_finished \"(exit 2)\"
177 > # failed 2 among 2 test(s)
182 ################################################################
183 # Basics of the basics
185 # updating a new file without --add should fail.
186 test_expect_success
'git update-index without --add should fail adding' '
187 test_must_fail git update-index should-be-empty
190 # and with --add it should succeed, even if it is empty (it used to fail).
191 test_expect_success
'git update-index with --add should succeed' '
192 git update-index --add should-be-empty
195 test_expect_success
'writing tree out with git write-tree' '
196 tree=$(git write-tree)
199 # we know the shape and contents of the tree and know the object ID for it.
200 test_expect_success
'validate object ID of a known tree' '
201 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
205 test_expect_success
'git update-index without --remove should fail removing' '
206 rm -f should-be-empty full-of-directories &&
207 test_must_fail git update-index should-be-empty
210 test_expect_success
'git update-index with --remove should be able to remove' '
211 git update-index --remove should-be-empty
214 # Empty tree can be written with recent write-tree.
215 test_expect_success
'git write-tree should be able to write an empty tree' '
216 tree=$(git write-tree)
219 test_expect_success
'validate object ID of a known tree' '
220 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
223 # Various types of objects
225 # Some filesystems do not support symblic links; on such systems
226 # some expected values are different
227 if test_have_prereq SYMLINKS
230 expectedtree
=087704a96baf1c2d1c869a8b084481e121c88b5b
231 expectedptree1
=21ae8269cacbe57ae09138dcc3a2887f904d02b3
232 expectedptree2
=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
234 expectfilter
='grep -v sym'
235 expectedtree
=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
236 expectedptree1
=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
237 expectedptree2
=ce580448f0148b985a513b693fdf7d802cacb44f
241 test_expect_success
'adding various types of objects with git update-index --add' '
242 mkdir path2 path3 path3/subp3 &&
243 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
247 echo "hello $p" >$p || exit 1
248 if test_have_prereq SYMLINKS
250 ln -s "hello $p" ${p}sym || exit 1
254 find path* ! -type d -print | xargs git update-index --add
257 # Show them and see that matches what we expect.
258 test_expect_success
'showing stage with git ls-files --stage' '
259 git ls-files --stage >current
262 test_expect_success
'validate git ls-files output for a known tree' '
263 $expectfilter >expected <<-\EOF &&
264 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
265 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
266 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
267 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
268 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
269 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
270 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
271 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
273 test_cmp expected current
276 test_expect_success
'writing tree out with git write-tree' '
277 tree=$(git write-tree)
280 test_expect_success
'validate object ID for a known tree' '
281 test "$tree" = "$expectedtree"
284 test_expect_success
'showing tree with git ls-tree' '
285 git ls-tree $tree >current
288 test_expect_success SYMLINKS
'git ls-tree output for a known tree' '
289 cat >expected <<-\EOF &&
290 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
291 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
292 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
293 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
295 test_cmp expected current
298 # This changed in ls-tree pathspec change -- recursive does
299 # not show tree nodes anymore.
300 test_expect_success
'showing tree with git ls-tree -r' '
301 git ls-tree -r $tree >current
304 test_expect_success
'git ls-tree -r output for a known tree' '
305 $expectfilter >expected <<-\EOF &&
306 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
307 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
308 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
309 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
310 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
311 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
312 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
313 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
315 test_cmp expected current
318 # But with -r -t we can have both.
319 test_expect_success
'showing tree with git ls-tree -r -t' '
320 git ls-tree -r -t $tree >current
323 test_expect_success SYMLINKS
'git ls-tree -r output for a known tree' '
324 cat >expected <<-\EOF &&
325 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
326 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
327 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
328 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
329 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
330 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
331 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
332 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
333 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
334 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
335 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
337 test_cmp expected current
340 test_expect_success
'writing partial tree out with git write-tree --prefix' '
341 ptree=$(git write-tree --prefix=path3)
344 test_expect_success
'validate object ID for a known tree' '
345 test "$ptree" = "$expectedptree1"
348 test_expect_success
'writing partial tree out with git write-tree --prefix' '
349 ptree=$(git write-tree --prefix=path3/subp3)
352 test_expect_success
'validate object ID for a known tree' '
353 test "$ptree" = "$expectedptree2"
356 test_expect_success
'put invalid objects into the index' '
358 cat >badobjects <<-\EOF &&
359 100644 blob 1000000000000000000000000000000000000000 dir/file1
360 100644 blob 2000000000000000000000000000000000000000 dir/file2
361 100644 blob 3000000000000000000000000000000000000000 dir/file3
362 100644 blob 4000000000000000000000000000000000000000 dir/file4
363 100644 blob 5000000000000000000000000000000000000000 dir/file5
365 git update-index --index-info <badobjects
368 test_expect_success
'writing this tree without --missing-ok' '
369 test_must_fail git write-tree
372 test_expect_success
'writing this tree with --missing-ok' '
373 git write-tree --missing-ok
377 ################################################################
378 test_expect_success
'git read-tree followed by write-tree should be idempotent' '
380 git read-tree $tree &&
381 test -f .git/index &&
382 newtree=$(git write-tree) &&
383 test "$newtree" = "$tree"
386 test_expect_success
'validate git diff-files output for a know cache/work tree state' '
387 $expectfilter >expected <<\EOF &&
388 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
389 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
390 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
391 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
392 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
393 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
394 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
395 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
397 git diff-files >current &&
398 test_cmp current expected
401 test_expect_success
'git update-index --refresh should succeed' '
402 git update-index --refresh
405 test_expect_success
'no diff after checkout and git update-index --refresh' '
406 git diff-files >current &&
407 cmp -s current /dev/null
410 ################################################################
413 test_expect_success
'git commit-tree records the correct tree in a commit' '
414 commit0=$(echo NO | git commit-tree $P) &&
415 tree=$(git show --pretty=raw $commit0 |
416 sed -n -e "s/^tree //p" -e "/^author /q") &&
417 test "z$tree" = "z$P"
420 test_expect_success
'git commit-tree records the correct parent in a commit' '
421 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
422 parent=$(git show --pretty=raw $commit1 |
423 sed -n -e "s/^parent //p" -e "/^author /q") &&
424 test "z$commit0" = "z$parent"
427 test_expect_success
'git commit-tree omits duplicated parent in a commit' '
428 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
429 parent=$(git show --pretty=raw $commit2 |
430 sed -n -e "s/^parent //p" -e "/^author /q" |
432 test "z$commit0" = "z$parent" &&
433 numparent=$(git show --pretty=raw $commit2 |
434 sed -n -e "s/^parent //p" -e "/^author /q" |
439 test_expect_success
'update-index D/F conflict' '
443 git update-index --add --replace path2 path0/file2 &&
444 numpath0=$(git ls-files path0 | wc -l) &&
448 test_expect_success
'very long name in the index handled sanely' '
451 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
452 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
453 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
457 git update-index --add path4 &&
459 git ls-files -s path4 |
463 ) | git update-index --index-info &&
464 len=$(git ls-files "a*" | wc -c) &&