94f2ebac5f28e3fb306857892473f152307a7223
   1#!/bin/sh
   2#
   3# Copyright (C) 2005 Rene Scharfe
   4#
   5
   6test_description='git tar-tree and git get-tar-commit-id test
   7
   8This test covers the topics of file contents, commit date handling and
   9commit id embedding:
  10
  11  The contents of the repository is compared to the extracted tar
  12  archive.  The repository contains simple text files, symlinks and a
  13  binary file (/bin/sh).  Only paths shorter than 99 characters are
  14  used.
  15
  16  git tar-tree applies the commit date to every file in the archive it
  17  creates.  The test sets the commit date to a specific value and checks
  18  if the tar archive contains that value.
  19
  20  When giving git tar-tree a commit id (in contrast to a tree id) it
  21  embeds this commit id into the tar archive as a comment.  The test
  22  checks the ability of git get-tar-commit-id to figure it out from the
  23  tar file.
  24
  25'
  26
  27. ./test-lib.sh
  28UNZIP=${UNZIP:-unzip}
  29GZIP=${GZIP:-gzip}
  30GUNZIP=${GUNZIP:-gzip -d}
  31
  32SUBSTFORMAT=%H%n
  33
  34test_expect_success \
  35    'populate workdir' \
  36    'mkdir a b c &&
  37     echo simple textfile >a/a &&
  38     mkdir a/bin &&
  39     cp /bin/sh a/bin &&
  40     printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
  41     printf "A not substituted O" >a/substfile2 &&
  42     if test_have_prereq SYMLINKS; then
  43        ln -s a a/l1
  44     else
  45        printf %s a > a/l1
  46     fi &&
  47     (p=long_path_to_a_file && cd a &&
  48      for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
  49      echo text >file_with_long_path) &&
  50     (cd a && find .) | sort >a.lst'
  51
  52test_expect_success \
  53    'add ignored file' \
  54    'echo ignore me >a/ignored &&
  55     echo ignored export-ignore >.git/info/attributes'
  56
  57test_expect_success \
  58    'add files to repository' \
  59    'find a -type f | xargs git update-index --add &&
  60     find a -type l | xargs git update-index --add &&
  61     treeid=`git write-tree` &&
  62     echo $treeid >treeid &&
  63     git update-ref HEAD $(TZ=GMT GIT_COMMITTER_DATE="2005-05-27 22:00:00" \
  64     git commit-tree $treeid </dev/null)'
  65
  66test_expect_success \
  67    'create bare clone' \
  68    'git clone --bare . bare.git &&
  69     cp .git/info/attributes bare.git/info/attributes'
  70
  71test_expect_success \
  72    'remove ignored file' \
  73    'rm a/ignored'
  74
  75test_expect_success \
  76    'git archive' \
  77    'git archive HEAD >b.tar'
  78
  79test_expect_success \
  80    'git tar-tree' \
  81    'git tar-tree HEAD >b2.tar'
  82
  83test_expect_success \
  84    'git archive vs. git tar-tree' \
  85    'test_cmp b.tar b2.tar'
  86
  87test_expect_success 'git archive on large files' '
  88    test_config core.bigfilethreshold 1 &&
  89    git archive HEAD >b3.tar &&
  90    test_cmp b.tar b3.tar
  91'
  92
  93test_expect_success \
  94    'git archive in a bare repo' \
  95    '(cd bare.git && git archive HEAD) >b3.tar'
  96
  97test_expect_success \
  98    'git archive vs. the same in a bare repo' \
  99    'test_cmp b.tar b3.tar'
 100
 101test_expect_success 'git archive with --output' \
 102    'git archive --output=b4.tar HEAD &&
 103    test_cmp b.tar b4.tar'
 104
 105test_expect_success 'git archive --remote' \
 106    'git archive --remote=. HEAD >b5.tar &&
 107    test_cmp b.tar b5.tar'
 108
 109test_expect_success \
 110    'validate file modification time' \
 111    'mkdir extract &&
 112     "$TAR" xf b.tar -C extract a/a &&
 113     test-chmtime -v +0 extract/a/a |cut -f 1 >b.mtime &&
 114     echo "1117231200" >expected.mtime &&
 115     test_cmp expected.mtime b.mtime'
 116
 117test_expect_success \
 118    'git get-tar-commit-id' \
 119    'git get-tar-commit-id <b.tar >b.commitid &&
 120     test_cmp .git/$(git symbolic-ref HEAD) b.commitid'
 121
 122test_expect_success \
 123    'extract tar archive' \
 124    '(cd b && "$TAR" xf -) <b.tar'
 125
 126test_expect_success \
 127    'validate filenames' \
 128    '(cd b/a && find .) | sort >b.lst &&
 129     test_cmp a.lst b.lst'
 130
 131test_expect_success \
 132    'validate file contents' \
 133    'diff -r a b/a'
 134
 135test_expect_success \
 136    'git tar-tree with prefix' \
 137    'git tar-tree HEAD prefix >c.tar'
 138
 139test_expect_success \
 140    'extract tar archive with prefix' \
 141    '(cd c && "$TAR" xf -) <c.tar'
 142
 143test_expect_success \
 144    'validate filenames with prefix' \
 145    '(cd c/prefix/a && find .) | sort >c.lst &&
 146     test_cmp a.lst c.lst'
 147
 148test_expect_success \
 149    'validate file contents with prefix' \
 150    'diff -r a c/prefix/a'
 151
 152test_expect_success \
 153    'create archives with substfiles' \
 154    'cp .git/info/attributes .git/info/attributes.before &&
 155     echo "substfile?" export-subst >>.git/info/attributes &&
 156     git archive HEAD >f.tar &&
 157     git archive --prefix=prefix/ HEAD >g.tar &&
 158     mv .git/info/attributes.before .git/info/attributes'
 159
 160test_expect_success \
 161    'extract substfiles' \
 162    '(mkdir f && cd f && "$TAR" xf -) <f.tar'
 163
 164test_expect_success \
 165     'validate substfile contents' \
 166     'git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
 167      >f/a/substfile1.expected &&
 168      test_cmp f/a/substfile1.expected f/a/substfile1 &&
 169      test_cmp a/substfile2 f/a/substfile2
 170'
 171
 172test_expect_success \
 173    'extract substfiles from archive with prefix' \
 174    '(mkdir g && cd g && "$TAR" xf -) <g.tar'
 175
 176test_expect_success \
 177     'validate substfile contents from archive with prefix' \
 178     'git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
 179      >g/prefix/a/substfile1.expected &&
 180      test_cmp g/prefix/a/substfile1.expected g/prefix/a/substfile1 &&
 181      test_cmp a/substfile2 g/prefix/a/substfile2
 182'
 183
 184test_expect_success \
 185    'git archive --format=zip' \
 186    'git archive --format=zip HEAD >d.zip'
 187
 188test_expect_success \
 189    'git archive --format=zip in a bare repo' \
 190    '(cd bare.git && git archive --format=zip HEAD) >d1.zip'
 191
 192test_expect_success \
 193    'git archive --format=zip vs. the same in a bare repo' \
 194    'test_cmp d.zip d1.zip'
 195
 196test_expect_success 'git archive --format=zip with --output' \
 197    'git archive --format=zip --output=d2.zip HEAD &&
 198    test_cmp d.zip d2.zip'
 199
 200test_expect_success 'git archive with --output, inferring format' '
 201        git archive --output=d3.zip HEAD &&
 202        test_cmp d.zip d3.zip
 203'
 204
 205test_expect_success 'git archive with --output, override inferred format' '
 206        git archive --format=tar --output=d4.zip HEAD &&
 207        test_cmp b.tar d4.zip
 208'
 209
 210$UNZIP -v >/dev/null 2>&1
 211if [ $? -eq 127 ]; then
 212        say "Skipping ZIP tests, because unzip was not found"
 213else
 214        test_set_prereq UNZIP
 215fi
 216
 217test_expect_success UNZIP \
 218    'extract ZIP archive' \
 219    '(mkdir d && cd d && $UNZIP ../d.zip)'
 220
 221test_expect_success UNZIP \
 222    'validate filenames' \
 223    '(cd d/a && find .) | sort >d.lst &&
 224     test_cmp a.lst d.lst'
 225
 226test_expect_success UNZIP \
 227    'validate file contents' \
 228    'diff -r a d/a'
 229
 230test_expect_success \
 231    'git archive --format=zip with prefix' \
 232    'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
 233
 234test_expect_success UNZIP \
 235    'extract ZIP archive with prefix' \
 236    '(mkdir e && cd e && $UNZIP ../e.zip)'
 237
 238test_expect_success UNZIP \
 239    'validate filenames with prefix' \
 240    '(cd e/prefix/a && find .) | sort >e.lst &&
 241     test_cmp a.lst e.lst'
 242
 243test_expect_success UNZIP \
 244    'validate file contents with prefix' \
 245    'diff -r a e/prefix/a'
 246
 247test_expect_success UNZIP 'git archive -0 --format=zip on large files' '
 248    test_config core.bigfilethreshold 1 &&
 249    git archive -0 --format=zip HEAD >large.zip &&
 250    (mkdir large && cd large && $UNZIP ../large.zip)
 251'
 252
 253test_expect_success UNZIP 'git archive --format=zip on large files' '
 254    test_config core.bigfilethreshold 1 &&
 255    git archive --format=zip HEAD >large-compressed.zip &&
 256    (mkdir large-compressed && cd large-compressed && $UNZIP ../large-compressed.zip) &&
 257    test_cmp large-compressed/a/bin/sh large/a/bin/sh
 258'
 259
 260test_expect_success \
 261    'git archive --list outside of a git repo' \
 262    'GIT_DIR=some/non-existing/directory git archive --list'
 263
 264test_expect_success 'clients cannot access unreachable commits' '
 265        test_commit unreachable &&
 266        sha1=`git rev-parse HEAD` &&
 267        git reset --hard HEAD^ &&
 268        git archive $sha1 >remote.tar &&
 269        test_must_fail git archive --remote=. $sha1 >remote.tar
 270'
 271
 272test_expect_success 'git-archive --prefix=olde-' '
 273        git archive --prefix=olde- >h.tar HEAD &&
 274        (
 275                mkdir h &&
 276                cd h &&
 277                "$TAR" xf - <../h.tar
 278        ) &&
 279        test -d h/olde-a &&
 280        test -d h/olde-a/bin &&
 281        test -f h/olde-a/bin/sh
 282'
 283
 284test_expect_success 'setup tar filters' '
 285        git config tar.tar.foo.command "tr ab ba" &&
 286        git config tar.bar.command "tr ab ba" &&
 287        git config tar.bar.remote true
 288'
 289
 290test_expect_success 'archive --list mentions user filter' '
 291        git archive --list >output &&
 292        grep "^tar\.foo\$" output &&
 293        grep "^bar\$" output
 294'
 295
 296test_expect_success 'archive --list shows only enabled remote filters' '
 297        git archive --list --remote=. >output &&
 298        ! grep "^tar\.foo\$" output &&
 299        grep "^bar\$" output
 300'
 301
 302test_expect_success 'invoke tar filter by format' '
 303        git archive --format=tar.foo HEAD >config.tar.foo &&
 304        tr ab ba <config.tar.foo >config.tar &&
 305        test_cmp b.tar config.tar &&
 306        git archive --format=bar HEAD >config.bar &&
 307        tr ab ba <config.bar >config.tar &&
 308        test_cmp b.tar config.tar
 309'
 310
 311test_expect_success 'invoke tar filter by extension' '
 312        git archive -o config-implicit.tar.foo HEAD &&
 313        test_cmp config.tar.foo config-implicit.tar.foo &&
 314        git archive -o config-implicit.bar HEAD &&
 315        test_cmp config.tar.foo config-implicit.bar
 316'
 317
 318test_expect_success 'default output format remains tar' '
 319        git archive -o config-implicit.baz HEAD &&
 320        test_cmp b.tar config-implicit.baz
 321'
 322
 323test_expect_success 'extension matching requires dot' '
 324        git archive -o config-implicittar.foo HEAD &&
 325        test_cmp b.tar config-implicittar.foo
 326'
 327
 328test_expect_success 'only enabled filters are available remotely' '
 329        test_must_fail git archive --remote=. --format=tar.foo HEAD \
 330                >remote.tar.foo &&
 331        git archive --remote=. --format=bar >remote.bar HEAD &&
 332        test_cmp remote.bar config.bar
 333'
 334
 335if $GZIP --version >/dev/null 2>&1; then
 336        test_set_prereq GZIP
 337else
 338        say "Skipping some tar.gz tests because gzip not found"
 339fi
 340
 341test_expect_success GZIP 'git archive --format=tgz' '
 342        git archive --format=tgz HEAD >j.tgz
 343'
 344
 345test_expect_success GZIP 'git archive --format=tar.gz' '
 346        git archive --format=tar.gz HEAD >j1.tar.gz &&
 347        test_cmp j.tgz j1.tar.gz
 348'
 349
 350test_expect_success GZIP 'infer tgz from .tgz filename' '
 351        git archive --output=j2.tgz HEAD &&
 352        test_cmp j.tgz j2.tgz
 353'
 354
 355test_expect_success GZIP 'infer tgz from .tar.gz filename' '
 356        git archive --output=j3.tar.gz HEAD &&
 357        test_cmp j.tgz j3.tar.gz
 358'
 359
 360if $GUNZIP --version >/dev/null 2>&1; then
 361        test_set_prereq GUNZIP
 362else
 363        say "Skipping some tar.gz tests because gunzip was not found"
 364fi
 365
 366test_expect_success GZIP,GUNZIP 'extract tgz file' '
 367        $GUNZIP -c <j.tgz >j.tar &&
 368        test_cmp b.tar j.tar
 369'
 370
 371test_expect_success GZIP 'remote tar.gz is allowed by default' '
 372        git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
 373        test_cmp j.tgz remote.tar.gz
 374'
 375
 376test_expect_success GZIP 'remote tar.gz can be disabled' '
 377        git config tar.tar.gz.remote false &&
 378        test_must_fail git archive --remote=. --format=tar.gz HEAD \
 379                >remote.tar.gz
 380'
 381
 382test_done