]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0000-basic.sh
Merge branch 'jn/grep-open'
[thirdparty/git.git] / t / t0000-basic.sh
CommitLineData
e1970ce4
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6test_description='Test the very basics part #1.
7
8The rest of the test suite does not check the basic operation of git
9plumbing commands to work very carefully. Their job is to concentrate
10on tricky features that caused bugs in the past to detect regression.
11
12This test runs very basic features, like registering things in cache,
13writing tree, etc.
14
15Note that this test *deliberately* hard-codes many expected object
16IDs. When object ID computation changes, like in the previous case of
17swapping compression and hashing order, the person who is making the
18modification *should* take notice and update the test vectors here.
19'
eea42069
JH
20
21################################################################
e2b70087 22# It appears that people try to run tests without building...
eea42069
JH
23
24../git >/dev/null
25if test $? != 1
26then
27 echo >&2 'You do not seem to have built git yet.'
28 exit 1
29fi
30
e1970ce4
JH
31. ./test-lib.sh
32
33################################################################
5be60078 34# git init has been done in an empty repository.
e1970ce4
JH
35# make sure it is empty.
36
37find .git/objects -type f -print >should-be-empty
38test_expect_success \
5be60078 39 '.git/objects should be empty after git init in an empty repo.' \
a6080a0a 40 'cmp -s /dev/null should-be-empty'
e1970ce4 41
9106c097
LT
42# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
43# 3 is counting "objects" itself
e1970ce4
JH
44find .git/objects -type d -print >full-of-directories
45test_expect_success \
9106c097
LT
46 '.git/objects should have 3 subdirectories.' \
47 'test $(wc -l < full-of-directories) = 3'
e1970ce4 48
41ac414e
JH
49################################################################
50# Test harness
51test_expect_success 'success is reported like this' '
52 :
53'
54test_expect_failure 'pretend we have a known breakage' '
55 false
56'
57test_expect_failure 'pretend we have fixed a known breakage' '
58 :
59'
a7bb3940
JS
60test_set_prereq HAVEIT
61haveit=no
62test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
63 test_have_prereq HAVEIT &&
64 haveit=yes
65'
66donthaveit=yes
67test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
68 donthaveit=no
69'
70if test $haveit$donthaveit != yesyes
71then
72 say "bug in test framework: prerequisite tags do not work reliably"
73 exit 1
74fi
41ac414e 75
b6b0afdc
JN
76clean=no
77test_expect_success 'tests clean up after themselves' '
78 test_when_finished clean=yes
79'
80
81cleaner=no
82test_expect_code 1 'tests clean up even after a failure' '
83 test_when_finished cleaner=yes &&
84 (exit 1)
85'
86
87if test $clean$cleaner != yesyes
88then
89 say "bug in test framework: cleanup commands do not work reliably"
90 exit 1
91fi
92
93test_expect_code 2 'failure to clean up causes the test to fail' '
94 test_when_finished "(exit 2)"
95'
96
e1970ce4
JH
97################################################################
98# Basics of the basics
99
100# updating a new file without --add should fail.
41ac414e 101test_expect_success 'git update-index without --add should fail adding.' '
d492b31c 102 test_must_fail git update-index should-be-empty
41ac414e 103'
e1970ce4
JH
104
105# and with --add it should succeed, even if it is empty (it used to fail).
106test_expect_success \
5be60078
JH
107 'git update-index with --add should succeed.' \
108 'git update-index --add should-be-empty'
e1970ce4
JH
109
110test_expect_success \
5be60078
JH
111 'writing tree out with git write-tree' \
112 'tree=$(git write-tree)'
e1970ce4
JH
113
114# we know the shape and contents of the tree and know the object ID for it.
115test_expect_success \
116 'validate object ID of a known tree.' \
117 'test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a'
118
119# Removing paths.
120rm -f should-be-empty full-of-directories
41ac414e 121test_expect_success 'git update-index without --remove should fail removing.' '
d492b31c 122 test_must_fail git update-index should-be-empty
41ac414e 123'
e1970ce4
JH
124
125test_expect_success \
5be60078
JH
126 'git update-index with --remove should be able to remove.' \
127 'git update-index --remove should-be-empty'
e1970ce4
JH
128
129# Empty tree can be written with recent write-tree.
130test_expect_success \
5be60078
JH
131 'git write-tree should be able to write an empty tree.' \
132 'tree=$(git write-tree)'
e1970ce4
JH
133
134test_expect_success \
135 'validate object ID of a known tree.' \
136 'test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904'
137
138# Various types of objects
704a3143
JS
139# Some filesystems do not support symblic links; on such systems
140# some expected values are different
e1970ce4 141mkdir path2 path3 path3/subp3
704a3143
JS
142paths='path0 path2/file2 path3/file3 path3/subp3/file3'
143for p in $paths
e1970ce4
JH
144do
145 echo "hello $p" >$p
e1970ce4 146done
704a3143
JS
147if test_have_prereq SYMLINKS
148then
149 for p in $paths
150 do
151 ln -s "hello $p" ${p}sym
152 done
153 expectfilter=cat
154 expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
155 expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
156 expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
157else
158 expectfilter='grep -v sym'
159 expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
160 expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
161 expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
162fi
163
e1970ce4 164test_expect_success \
5be60078
JH
165 'adding various types of objects with git update-index --add.' \
166 'find path* ! -type d -print | xargs git update-index --add'
e1970ce4
JH
167
168# Show them and see that matches what we expect.
169test_expect_success \
5be60078
JH
170 'showing stage with git ls-files --stage' \
171 'git ls-files --stage >current'
e1970ce4 172
704a3143 173$expectfilter >expected <<\EOF
2eab945e
JH
174100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
175120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
176100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
177120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
178100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
179120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
180100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
181120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
e1970ce4
JH
182EOF
183test_expect_success \
5be60078 184 'validate git ls-files output for a known tree.' \
188c3827 185 'test_cmp expected current'
e1970ce4
JH
186
187test_expect_success \
5be60078
JH
188 'writing tree out with git write-tree.' \
189 'tree=$(git write-tree)'
e1970ce4
JH
190test_expect_success \
191 'validate object ID for a known tree.' \
704a3143 192 'test "$tree" = "$expectedtree"'
e1970ce4
JH
193
194test_expect_success \
5be60078
JH
195 'showing tree with git ls-tree' \
196 'git ls-tree $tree >current'
e1970ce4 197cat >expected <<\EOF
2eab945e
JH
198100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
199120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
200040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
201040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
e1970ce4 202EOF
704a3143 203test_expect_success SYMLINKS \
5be60078 204 'git ls-tree output for a known tree.' \
188c3827 205 'test_cmp expected current'
e1970ce4 206
246cc52f
JH
207# This changed in ls-tree pathspec change -- recursive does
208# not show tree nodes anymore.
e1970ce4 209test_expect_success \
5be60078
JH
210 'showing tree with git ls-tree -r' \
211 'git ls-tree -r $tree >current'
704a3143 212$expectfilter >expected <<\EOF
2eab945e
JH
213100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
214120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
2eab945e
JH
215100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
216120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
2eab945e
JH
217100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
218120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
2eab945e
JH
219100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
220120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
e1970ce4
JH
221EOF
222test_expect_success \
5be60078 223 'git ls-tree -r output for a known tree.' \
188c3827 224 'test_cmp expected current'
e1970ce4 225
fdeb6bf5
JH
226# But with -r -t we can have both.
227test_expect_success \
5be60078
JH
228 'showing tree with git ls-tree -r -t' \
229 'git ls-tree -r -t $tree >current'
fdeb6bf5
JH
230cat >expected <<\EOF
231100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
232120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
233040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
234100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
235120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
236040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
237100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
238120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
239040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
240100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
241120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
242EOF
704a3143 243test_expect_success SYMLINKS \
5be60078 244 'git ls-tree -r output for a known tree.' \
188c3827 245 'test_cmp expected current'
fdeb6bf5 246
6bd20358 247test_expect_success \
5be60078
JH
248 'writing partial tree out with git write-tree --prefix.' \
249 'ptree=$(git write-tree --prefix=path3)'
6bd20358
JH
250test_expect_success \
251 'validate object ID for a known tree.' \
704a3143 252 'test "$ptree" = "$expectedptree1"'
6bd20358
JH
253
254test_expect_success \
5be60078
JH
255 'writing partial tree out with git write-tree --prefix.' \
256 'ptree=$(git write-tree --prefix=path3/subp3)'
6bd20358
JH
257test_expect_success \
258 'validate object ID for a known tree.' \
704a3143 259 'test "$ptree" = "$expectedptree2"'
6bd20358 260
3d12d0cf
JS
261cat >badobjects <<EOF
262100644 blob 1000000000000000000000000000000000000000 dir/file1
263100644 blob 2000000000000000000000000000000000000000 dir/file2
264100644 blob 3000000000000000000000000000000000000000 dir/file3
265100644 blob 4000000000000000000000000000000000000000 dir/file4
266100644 blob 5000000000000000000000000000000000000000 dir/file5
267EOF
268
269rm .git/index
270test_expect_success \
271 'put invalid objects into the index.' \
5be60078 272 'git update-index --index-info < badobjects'
3d12d0cf 273
41ac414e 274test_expect_success 'writing this tree without --missing-ok.' '
d492b31c 275 test_must_fail git write-tree
41ac414e 276'
3d12d0cf
JS
277
278test_expect_success \
279 'writing this tree with --missing-ok.' \
5be60078 280 'git write-tree --missing-ok'
3d12d0cf
JS
281
282
e1970ce4
JH
283################################################################
284rm .git/index
285test_expect_success \
5be60078
JH
286 'git read-tree followed by write-tree should be idempotent.' \
287 'git read-tree $tree &&
e1970ce4 288 test -f .git/index &&
5be60078 289 newtree=$(git write-tree) &&
e1970ce4
JH
290 test "$newtree" = "$tree"'
291
704a3143 292$expectfilter >expected <<\EOF
b6d8f309
JH
293:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
294:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
295:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
296:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
297:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
298:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
299:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
300:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
e1970ce4
JH
301EOF
302test_expect_success \
5be60078 303 'validate git diff-files output for a know cache/work tree state.' \
4fdf71be 304 'git diff-files >current && test_cmp current expected >/dev/null'
e1970ce4
JH
305
306test_expect_success \
5be60078
JH
307 'git update-index --refresh should succeed.' \
308 'git update-index --refresh'
e1970ce4
JH
309
310test_expect_success \
5be60078
JH
311 'no diff after checkout and git update-index --refresh.' \
312 'git diff-files >current && cmp -s current /dev/null'
e1970ce4 313
9af0b8db 314################################################################
704a3143 315P=$expectedtree
9af0b8db 316test_expect_success \
5be60078
JH
317 'git commit-tree records the correct tree in a commit.' \
318 'commit0=$(echo NO | git commit-tree $P) &&
9af0b8db
JH
319 tree=$(git show --pretty=raw $commit0 |
320 sed -n -e "s/^tree //p" -e "/^author /q") &&
321 test "z$tree" = "z$P"'
322
323test_expect_success \
5be60078
JH
324 'git commit-tree records the correct parent in a commit.' \
325 'commit1=$(echo NO | git commit-tree $P -p $commit0) &&
9af0b8db
JH
326 parent=$(git show --pretty=raw $commit1 |
327 sed -n -e "s/^parent //p" -e "/^author /q") &&
328 test "z$commit0" = "z$parent"'
329
330test_expect_success \
5be60078
JH
331 'git commit-tree omits duplicated parent in a commit.' \
332 'commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
9af0b8db
JH
333 parent=$(git show --pretty=raw $commit2 |
334 sed -n -e "s/^parent //p" -e "/^author /q" |
335 sort -u) &&
336 test "z$commit0" = "z$parent" &&
337 numparent=$(git show --pretty=raw $commit2 |
338 sed -n -e "s/^parent //p" -e "/^author /q" |
339 wc -l) &&
340 test $numparent = 1'
341
81a361be
JH
342test_expect_success 'update-index D/F conflict' '
343 mv path0 tmp &&
344 mv path2 path0 &&
345 mv tmp path2 &&
346 git update-index --add --replace path2 path0/file2 &&
347 numpath0=$(git ls-files path0 | wc -l) &&
348 test $numpath0 = 1
349'
350
704a3143 351test_expect_success SYMLINKS 'absolute path works as expected' '
e5392c51
JS
352 mkdir first &&
353 ln -s ../.git first/.git &&
354 mkdir second &&
355 ln -s ../first second/other &&
356 mkdir third &&
357 dir="$(cd .git; pwd -P)" &&
358 dir2=third/../second/other/.git &&
d553e737 359 test "$dir" = "$(test-path-utils make_absolute_path $dir2)" &&
e5392c51 360 file="$dir"/index &&
d553e737 361 test "$file" = "$(test-path-utils make_absolute_path $dir2/index)" &&
e371a4c6 362 basename=blub &&
d553e737 363 test "$dir/$basename" = "$(cd .git && test-path-utils make_absolute_path "$basename")" &&
e5392c51
JS
364 ln -s ../first/file .git/syml &&
365 sym="$(cd first; pwd -P)"/file &&
d553e737 366 test "$sym" = "$(test-path-utils make_absolute_path "$dir2/syml")"
e5392c51
JS
367'
368
7fec10b7
JH
369test_expect_success 'very long name in the index handled sanely' '
370
371 a=a && # 1
372 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
373 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
374 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
375 a=${a}q &&
376
377 >path4 &&
378 git update-index --add path4 &&
379 (
380 git ls-files -s path4 |
381 sed -e "s/ .*/ /" |
382 tr -d "\012"
383 echo "$a"
384 ) | git update-index --index-info &&
385 len=$(git ls-files "a*" | wc -c) &&
386 test $len = 4098
387'
388
e1970ce4 389test_done