t / t0000-basic.shon commit diff --stat-count: finishing touches (e5f85df)
   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'
  20
  21################################################################
  22# It appears that people try to run tests without building...
  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
  31. ./test-lib.sh
  32
  33################################################################
  34# git init has been done in an empty repository.
  35# make sure it is empty.
  36
  37find .git/objects -type f -print >should-be-empty
  38test_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
  44find .git/objects -type d -print >full-of-directories
  45test_expect_success \
  46    '.git/objects should have 3 subdirectories.' \
  47    'test $(wc -l < full-of-directories) = 3'
  48
  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'
  57
  58test_expect_success 'pretend we have fixed a known breakage (run in sub test-lib)' "
  59    mkdir passing-todo &&
  60    (cd passing-todo &&
  61    cat >passing-todo.sh <<EOF &&
  62#!$SHELL_PATH
  63
  64test_description='A passing TODO test
  65
  66This is run in a sub test-lib so that we do not get incorrect passing
  67metrics
  68'
  69
  70# Point to the t/test-lib.sh, which isn't in ../ as usual
  71TEST_DIRECTORY=\"$TEST_DIRECTORY\"
  72. \"\$TEST_DIRECTORY\"/test-lib.sh
  73
  74test_expect_failure 'pretend we have fixed a known breakage' '
  75    :
  76'
  77
  78test_done
  79EOF
  80    chmod +x passing-todo.sh &&
  81    ./passing-todo.sh >out 2>err &&
  82    ! test -s err &&
  83sed -e 's/^> //' >expect <<EOF &&
  84> ok 1 - pretend we have fixed a known breakage # TODO known breakage
  85> # fixed 1 known breakage(s)
  86> # passed all 1 test(s)
  87> 1..1
  88EOF
  89    test_cmp expect out)
  90"
  91test_set_prereq HAVEIT
  92haveit=no
  93test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
  94    test_have_prereq HAVEIT &&
  95    haveit=yes
  96'
  97donthaveit=yes
  98test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
  99    donthaveit=no
 100'
 101if test $haveit$donthaveit != yesyes
 102then
 103        say "bug in test framework: prerequisite tags do not work reliably"
 104        exit 1
 105fi
 106
 107test_set_prereq HAVETHIS
 108haveit=no
 109test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
 110    test_have_prereq HAVEIT &&
 111    test_have_prereq HAVETHIS &&
 112    haveit=yes
 113'
 114donthaveit=yes
 115test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
 116    donthaveit=no
 117'
 118donthaveiteither=yes
 119test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
 120    donthaveiteither=no
 121'
 122if test $haveit$donthaveit$donthaveiteither != yesyesyes
 123then
 124        say "bug in test framework: multiple prerequisite tags do not work reliably"
 125        exit 1
 126fi
 127
 128clean=no
 129test_expect_success 'tests clean up after themselves' '
 130    test_when_finished clean=yes
 131'
 132
 133if test $clean != yes
 134then
 135        say "bug in test framework: basic cleanup command does not work reliably"
 136        exit 1
 137fi
 138
 139test_expect_success 'tests clean up even on failures' "
 140    mkdir failing-cleanup &&
 141    (cd failing-cleanup &&
 142    cat >failing-cleanup.sh <<EOF &&
 143#!$SHELL_PATH
 144
 145test_description='Failing tests with cleanup commands'
 146
 147# Point to the t/test-lib.sh, which isn't in ../ as usual
 148TEST_DIRECTORY=\"$TEST_DIRECTORY\"
 149. \"\$TEST_DIRECTORY\"/test-lib.sh
 150
 151test_expect_success 'tests clean up even after a failure' '
 152    touch clean-after-failure &&
 153    test_when_finished rm clean-after-failure &&
 154    (exit 1)
 155'
 156
 157test_expect_success 'failure to clean up causes the test to fail' '
 158    test_when_finished \"(exit 2)\"
 159'
 160
 161test_done
 162EOF
 163    chmod +x failing-cleanup.sh &&
 164    test_must_fail ./failing-cleanup.sh >out 2>err &&
 165    ! test -s err &&
 166    ! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
 167sed -e 's/Z$//' -e 's/^> //' >expect <<\EOF &&
 168> not ok - 1 tests clean up even after a failure
 169> #     Z
 170> #         touch clean-after-failure &&
 171> #         test_when_finished rm clean-after-failure &&
 172> #         (exit 1)
 173> #     Z
 174> not ok - 2 failure to clean up causes the test to fail
 175> #     Z
 176> #         test_when_finished \"(exit 2)\"
 177> #     Z
 178> # failed 2 among 2 test(s)
 179> 1..2
 180EOF
 181    test_cmp expect out)
 182"
 183
 184################################################################
 185# Basics of the basics
 186
 187# updating a new file without --add should fail.
 188test_expect_success 'git update-index without --add should fail adding.' '
 189    test_must_fail git update-index should-be-empty
 190'
 191
 192# and with --add it should succeed, even if it is empty (it used to fail).
 193test_expect_success \
 194    'git update-index with --add should succeed.' \
 195    'git update-index --add should-be-empty'
 196
 197test_expect_success \
 198    'writing tree out with git write-tree' \
 199    'tree=$(git write-tree)'
 200
 201# we know the shape and contents of the tree and know the object ID for it.
 202test_expect_success \
 203    'validate object ID of a known tree.' \
 204    'test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a'
 205
 206# Removing paths.
 207rm -f should-be-empty full-of-directories
 208test_expect_success 'git update-index without --remove should fail removing.' '
 209    test_must_fail git update-index should-be-empty
 210'
 211
 212test_expect_success \
 213    'git update-index with --remove should be able to remove.' \
 214    'git update-index --remove should-be-empty'
 215
 216# Empty tree can be written with recent write-tree.
 217test_expect_success \
 218    'git write-tree should be able to write an empty tree.' \
 219    'tree=$(git write-tree)'
 220
 221test_expect_success \
 222    'validate object ID of a known tree.' \
 223    'test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904'
 224
 225# Various types of objects
 226# Some filesystems do not support symblic links; on such systems
 227# some expected values are different
 228mkdir path2 path3 path3/subp3
 229paths='path0 path2/file2 path3/file3 path3/subp3/file3'
 230for p in $paths
 231do
 232    echo "hello $p" >$p
 233done
 234if test_have_prereq SYMLINKS
 235then
 236        for p in $paths
 237        do
 238                ln -s "hello $p" ${p}sym
 239        done
 240        expectfilter=cat
 241        expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
 242        expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
 243        expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 244else
 245        expectfilter='grep -v sym'
 246        expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
 247        expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
 248        expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
 249fi
 250
 251test_expect_success \
 252    'adding various types of objects with git update-index --add.' \
 253    'find path* ! -type d -print | xargs git update-index --add'
 254
 255# Show them and see that matches what we expect.
 256test_expect_success \
 257    'showing stage with git ls-files --stage' \
 258    'git ls-files --stage >current'
 259
 260$expectfilter >expected <<\EOF
 261100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 262120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 263100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 264120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 265100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 266120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 267100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 268120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 269EOF
 270test_expect_success \
 271    'validate git ls-files output for a known tree.' \
 272    'test_cmp expected current'
 273
 274test_expect_success \
 275    'writing tree out with git write-tree.' \
 276    'tree=$(git write-tree)'
 277test_expect_success \
 278    'validate object ID for a known tree.' \
 279    'test "$tree" = "$expectedtree"'
 280
 281test_expect_success \
 282    'showing tree with git ls-tree' \
 283    'git ls-tree $tree >current'
 284cat >expected <<\EOF
 285100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 286120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 287040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 288040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 289EOF
 290test_expect_success SYMLINKS \
 291    'git ls-tree output for a known tree.' \
 292    'test_cmp expected current'
 293
 294# This changed in ls-tree pathspec change -- recursive does
 295# not show tree nodes anymore.
 296test_expect_success \
 297    'showing tree with git ls-tree -r' \
 298    'git ls-tree -r $tree >current'
 299$expectfilter >expected <<\EOF
 300100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 301120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 302100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 303120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 304100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 305120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 306100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 307120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 308EOF
 309test_expect_success \
 310    'git ls-tree -r output for a known tree.' \
 311    'test_cmp expected current'
 312
 313# But with -r -t we can have both.
 314test_expect_success \
 315    'showing tree with git ls-tree -r -t' \
 316    'git ls-tree -r -t $tree >current'
 317cat >expected <<\EOF
 318100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 319120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 320040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 321100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 322120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 323040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 324100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 325120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 326040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 327100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 328120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 329EOF
 330test_expect_success SYMLINKS \
 331    'git ls-tree -r output for a known tree.' \
 332    'test_cmp expected current'
 333
 334test_expect_success \
 335    'writing partial tree out with git write-tree --prefix.' \
 336    'ptree=$(git write-tree --prefix=path3)'
 337test_expect_success \
 338    'validate object ID for a known tree.' \
 339    'test "$ptree" = "$expectedptree1"'
 340
 341test_expect_success \
 342    'writing partial tree out with git write-tree --prefix.' \
 343    'ptree=$(git write-tree --prefix=path3/subp3)'
 344test_expect_success \
 345    'validate object ID for a known tree.' \
 346    'test "$ptree" = "$expectedptree2"'
 347
 348cat >badobjects <<EOF
 349100644 blob 1000000000000000000000000000000000000000    dir/file1
 350100644 blob 2000000000000000000000000000000000000000    dir/file2
 351100644 blob 3000000000000000000000000000000000000000    dir/file3
 352100644 blob 4000000000000000000000000000000000000000    dir/file4
 353100644 blob 5000000000000000000000000000000000000000    dir/file5
 354EOF
 355
 356rm .git/index
 357test_expect_success \
 358    'put invalid objects into the index.' \
 359    'git update-index --index-info < badobjects'
 360
 361test_expect_success 'writing this tree without --missing-ok.' '
 362    test_must_fail git write-tree
 363'
 364
 365test_expect_success \
 366    'writing this tree with --missing-ok.' \
 367    'git write-tree --missing-ok'
 368
 369
 370################################################################
 371rm .git/index
 372test_expect_success \
 373    'git read-tree followed by write-tree should be idempotent.' \
 374    'git read-tree $tree &&
 375     test -f .git/index &&
 376     newtree=$(git write-tree) &&
 377     test "$newtree" = "$tree"'
 378
 379$expectfilter >expected <<\EOF
 380:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 381:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 382:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 383:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 384:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 385:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 386:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 387:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 388EOF
 389test_expect_success \
 390    'validate git diff-files output for a know cache/work tree state.' \
 391    'git diff-files >current && test_cmp current expected >/dev/null'
 392
 393test_expect_success \
 394    'git update-index --refresh should succeed.' \
 395    'git update-index --refresh'
 396
 397test_expect_success \
 398    'no diff after checkout and git update-index --refresh.' \
 399    'git diff-files >current && cmp -s current /dev/null'
 400
 401################################################################
 402P=$expectedtree
 403test_expect_success \
 404    'git commit-tree records the correct tree in a commit.' \
 405    'commit0=$(echo NO | git commit-tree $P) &&
 406     tree=$(git show --pretty=raw $commit0 |
 407         sed -n -e "s/^tree //p" -e "/^author /q") &&
 408     test "z$tree" = "z$P"'
 409
 410test_expect_success \
 411    'git commit-tree records the correct parent in a commit.' \
 412    'commit1=$(echo NO | git commit-tree $P -p $commit0) &&
 413     parent=$(git show --pretty=raw $commit1 |
 414         sed -n -e "s/^parent //p" -e "/^author /q") &&
 415     test "z$commit0" = "z$parent"'
 416
 417test_expect_success \
 418    'git commit-tree omits duplicated parent in a commit.' \
 419    'commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
 420     parent=$(git show --pretty=raw $commit2 |
 421         sed -n -e "s/^parent //p" -e "/^author /q" |
 422         sort -u) &&
 423     test "z$commit0" = "z$parent" &&
 424     numparent=$(git show --pretty=raw $commit2 |
 425         sed -n -e "s/^parent //p" -e "/^author /q" |
 426         wc -l) &&
 427     test $numparent = 1'
 428
 429test_expect_success 'update-index D/F conflict' '
 430        mv path0 tmp &&
 431        mv path2 path0 &&
 432        mv tmp path2 &&
 433        git update-index --add --replace path2 path0/file2 &&
 434        numpath0=$(git ls-files path0 | wc -l) &&
 435        test $numpath0 = 1
 436'
 437
 438test_expect_success SYMLINKS 'real path works as expected' '
 439        mkdir first &&
 440        ln -s ../.git first/.git &&
 441        mkdir second &&
 442        ln -s ../first second/other &&
 443        mkdir third &&
 444        dir="$(cd .git; pwd -P)" &&
 445        dir2=third/../second/other/.git &&
 446        test "$dir" = "$(test-path-utils real_path $dir2)" &&
 447        file="$dir"/index &&
 448        test "$file" = "$(test-path-utils real_path $dir2/index)" &&
 449        basename=blub &&
 450        test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
 451        ln -s ../first/file .git/syml &&
 452        sym="$(cd first; pwd -P)"/file &&
 453        test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 454'
 455
 456test_expect_success 'very long name in the index handled sanely' '
 457
 458        a=a && # 1
 459        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
 460        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
 461        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
 462        a=${a}q &&
 463
 464        >path4 &&
 465        git update-index --add path4 &&
 466        (
 467                git ls-files -s path4 |
 468                sed -e "s/      .*/     /" |
 469                tr -d "\012"
 470                echo "$a"
 471        ) | git update-index --index-info &&
 472        len=$(git ls-files "a*" | wc -c) &&
 473        test $len = 4098
 474'
 475
 476test_done