t / t0001-init.shon commit standardize and improve lookup rules for external local repos (b3256eb)
   1#!/bin/sh
   2
   3test_description='git init'
   4
   5. ./test-lib.sh
   6
   7check_config () {
   8        if test -d "$1" && test -f "$1/config" && test -d "$1/refs"
   9        then
  10                : happy
  11        else
  12                echo "expected a directory $1, a file $1/config and $1/refs"
  13                return 1
  14        fi
  15        bare=$(GIT_CONFIG="$1/config" git config --bool core.bare)
  16        worktree=$(GIT_CONFIG="$1/config" git config core.worktree) ||
  17        worktree=unset
  18
  19        test "$bare" = "$2" && test "$worktree" = "$3" || {
  20                echo "expected bare=$2 worktree=$3"
  21                echo "     got bare=$bare worktree=$worktree"
  22                return 1
  23        }
  24}
  25
  26test_expect_success 'plain' '
  27        (
  28                sane_unset GIT_DIR GIT_WORK_TREE &&
  29                mkdir plain &&
  30                cd plain &&
  31                git init
  32        ) &&
  33        check_config plain/.git false unset
  34'
  35
  36test_expect_success 'plain nested in bare' '
  37        (
  38                sane_unset GIT_DIR GIT_WORK_TREE &&
  39                git init --bare bare-ancestor.git &&
  40                cd bare-ancestor.git &&
  41                mkdir plain-nested &&
  42                cd plain-nested &&
  43                git init
  44        ) &&
  45        check_config bare-ancestor.git/plain-nested/.git false unset
  46'
  47
  48test_expect_success 'plain through aliased command, outside any git repo' '
  49        (
  50                sane_unset GIT_DIR GIT_WORK_TREE &&
  51                HOME=$(pwd)/alias-config &&
  52                export HOME &&
  53                mkdir alias-config &&
  54                echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
  55
  56                GIT_CEILING_DIRECTORIES=$(pwd) &&
  57                export GIT_CEILING_DIRECTORIES &&
  58
  59                mkdir plain-aliased &&
  60                cd plain-aliased &&
  61                git aliasedinit
  62        ) &&
  63        check_config plain-aliased/.git false unset
  64'
  65
  66test_expect_failure 'plain nested through aliased command' '
  67        (
  68                sane_unset GIT_DIR GIT_WORK_TREE &&
  69                git init plain-ancestor-aliased &&
  70                cd plain-ancestor-aliased &&
  71                echo "[alias] aliasedinit = init" >>.git/config &&
  72                mkdir plain-nested &&
  73                cd plain-nested &&
  74                git aliasedinit
  75        ) &&
  76        check_config plain-ancestor-aliased/plain-nested/.git false unset
  77'
  78
  79test_expect_failure 'plain nested in bare through aliased command' '
  80        (
  81                sane_unset GIT_DIR GIT_WORK_TREE &&
  82                git init --bare bare-ancestor-aliased.git &&
  83                cd bare-ancestor-aliased.git &&
  84                echo "[alias] aliasedinit = init" >>config &&
  85                mkdir plain-nested &&
  86                cd plain-nested &&
  87                git aliasedinit
  88        ) &&
  89        check_config bare-ancestor-aliased.git/plain-nested/.git false unset
  90'
  91
  92test_expect_success 'plain with GIT_WORK_TREE' '
  93        if (
  94                sane_unset GIT_DIR &&
  95                mkdir plain-wt &&
  96                cd plain-wt &&
  97                GIT_WORK_TREE=$(pwd) git init
  98        )
  99        then
 100                echo Should have failed -- GIT_WORK_TREE should not be used
 101                false
 102        fi
 103'
 104
 105test_expect_success 'plain bare' '
 106        (
 107                sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
 108                mkdir plain-bare-1 &&
 109                cd plain-bare-1 &&
 110                git --bare init
 111        ) &&
 112        check_config plain-bare-1 true unset
 113'
 114
 115test_expect_success 'plain bare with GIT_WORK_TREE' '
 116        if (
 117                sane_unset GIT_DIR GIT_CONFIG &&
 118                mkdir plain-bare-2 &&
 119                cd plain-bare-2 &&
 120                GIT_WORK_TREE=$(pwd) git --bare init
 121        )
 122        then
 123                echo Should have failed -- GIT_WORK_TREE should not be used
 124                false
 125        fi
 126'
 127
 128test_expect_success 'GIT_DIR bare' '
 129
 130        (
 131                sane_unset GIT_CONFIG &&
 132                mkdir git-dir-bare.git &&
 133                GIT_DIR=git-dir-bare.git git init
 134        ) &&
 135        check_config git-dir-bare.git true unset
 136'
 137
 138test_expect_success 'init --bare' '
 139
 140        (
 141                sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
 142                mkdir init-bare.git &&
 143                cd init-bare.git &&
 144                git init --bare
 145        ) &&
 146        check_config init-bare.git true unset
 147'
 148
 149test_expect_success 'GIT_DIR non-bare' '
 150
 151        (
 152                sane_unset GIT_CONFIG &&
 153                mkdir non-bare &&
 154                cd non-bare &&
 155                GIT_DIR=.git git init
 156        ) &&
 157        check_config non-bare/.git false unset
 158'
 159
 160test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
 161
 162        (
 163                sane_unset GIT_CONFIG &&
 164                mkdir git-dir-wt-1.git &&
 165                GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
 166        ) &&
 167        check_config git-dir-wt-1.git false "$(pwd)"
 168'
 169
 170test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
 171
 172        if (
 173                sane_unset GIT_CONFIG &&
 174                mkdir git-dir-wt-2.git &&
 175                GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-2.git git --bare init
 176        )
 177        then
 178                echo Should have failed -- --bare should not be used
 179                false
 180        fi
 181'
 182
 183test_expect_success 'reinit' '
 184
 185        (
 186                sane_unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG &&
 187
 188                mkdir again &&
 189                cd again &&
 190                git init >out1 2>err1 &&
 191                git init >out2 2>err2
 192        ) &&
 193        test_i18ngrep "Initialized empty" again/out1 &&
 194        test_i18ngrep "Reinitialized existing" again/out2 &&
 195        >again/empty &&
 196        test_i18ncmp again/empty again/err1 &&
 197        test_i18ncmp again/empty again/err2
 198'
 199
 200test_expect_success 'init with --template' '
 201        mkdir template-source &&
 202        echo content >template-source/file &&
 203        (
 204                mkdir template-custom &&
 205                cd template-custom &&
 206                git init --template=../template-source
 207        ) &&
 208        test_cmp template-source/file template-custom/.git/file
 209'
 210
 211test_expect_success 'init with --template (blank)' '
 212        (
 213                mkdir template-plain &&
 214                cd template-plain &&
 215                git init
 216        ) &&
 217        test -f template-plain/.git/info/exclude &&
 218        (
 219                mkdir template-blank &&
 220                cd template-blank &&
 221                git init --template=
 222        ) &&
 223        ! test -f template-blank/.git/info/exclude
 224'
 225
 226test_expect_success 'init with init.templatedir set' '
 227        mkdir templatedir-source &&
 228        echo Content >templatedir-source/file &&
 229        (
 230                test_config="${HOME}/.gitconfig" &&
 231                git config -f "$test_config"  init.templatedir "${HOME}/templatedir-source" &&
 232                mkdir templatedir-set &&
 233                cd templatedir-set &&
 234                sane_unset GIT_TEMPLATE_DIR &&
 235                NO_SET_GIT_TEMPLATE_DIR=t &&
 236                export NO_SET_GIT_TEMPLATE_DIR &&
 237                git init
 238        ) &&
 239        test_cmp templatedir-source/file templatedir-set/.git/file
 240'
 241
 242test_expect_success 'init --bare/--shared overrides system/global config' '
 243        (
 244                test_config="$HOME"/.gitconfig &&
 245                git config -f "$test_config" core.bare false &&
 246                git config -f "$test_config" core.sharedRepository 0640 &&
 247                mkdir init-bare-shared-override &&
 248                cd init-bare-shared-override &&
 249                git init --bare --shared=0666
 250        ) &&
 251        check_config init-bare-shared-override true unset &&
 252        test x0666 = \
 253        x`git config -f init-bare-shared-override/config core.sharedRepository`
 254'
 255
 256test_expect_success 'init honors global core.sharedRepository' '
 257        (
 258                test_config="$HOME"/.gitconfig &&
 259                git config -f "$test_config" core.sharedRepository 0666 &&
 260                mkdir shared-honor-global &&
 261                cd shared-honor-global &&
 262                git init
 263        ) &&
 264        test x0666 = \
 265        x`git config -f shared-honor-global/.git/config core.sharedRepository`
 266'
 267
 268test_expect_success 'init rejects insanely long --template' '
 269        (
 270                insane=$(printf "x%09999dx" 1) &&
 271                mkdir test &&
 272                cd test &&
 273                test_must_fail git init --template=$insane
 274        )
 275'
 276
 277test_expect_success 'init creates a new directory' '
 278        rm -fr newdir &&
 279        (
 280                git init newdir &&
 281                test -d newdir/.git/refs
 282        )
 283'
 284
 285test_expect_success 'init creates a new bare directory' '
 286        rm -fr newdir &&
 287        (
 288                git init --bare newdir &&
 289                test -d newdir/refs
 290        )
 291'
 292
 293test_expect_success 'init recreates a directory' '
 294        rm -fr newdir &&
 295        (
 296                mkdir newdir &&
 297                git init newdir &&
 298                test -d newdir/.git/refs
 299        )
 300'
 301
 302test_expect_success 'init recreates a new bare directory' '
 303        rm -fr newdir &&
 304        (
 305                mkdir newdir &&
 306                git init --bare newdir &&
 307                test -d newdir/refs
 308        )
 309'
 310
 311test_expect_success 'init creates a new deep directory' '
 312        rm -fr newdir &&
 313        git init newdir/a/b/c &&
 314        test -d newdir/a/b/c/.git/refs
 315'
 316
 317test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
 318        rm -fr newdir &&
 319        (
 320                # Leading directories should honor umask while
 321                # the repository itself should follow "shared"
 322                umask 002 &&
 323                git init --bare --shared=0660 newdir/a/b/c &&
 324                test -d newdir/a/b/c/refs &&
 325                ls -ld newdir/a newdir/a/b > lsab.out &&
 326                ! grep -v "^drwxrw[sx]r-x" lsab.out &&
 327                ls -ld newdir/a/b/c > lsc.out &&
 328                ! grep -v "^drwxrw[sx]---" lsc.out
 329        )
 330'
 331
 332test_expect_success 'init notices EEXIST (1)' '
 333        rm -fr newdir &&
 334        (
 335                >newdir &&
 336                test_must_fail git init newdir &&
 337                test -f newdir
 338        )
 339'
 340
 341test_expect_success 'init notices EEXIST (2)' '
 342        rm -fr newdir &&
 343        (
 344                mkdir newdir &&
 345                >newdir/a
 346                test_must_fail git init newdir/a/b &&
 347                test -f newdir/a
 348        )
 349'
 350
 351test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
 352        rm -fr newdir &&
 353        (
 354                mkdir newdir &&
 355                chmod -w newdir &&
 356                test_must_fail git init newdir/a/b
 357        )
 358'
 359
 360test_expect_success 'init creates a new bare directory with global --bare' '
 361        rm -rf newdir &&
 362        git --bare init newdir &&
 363        test -d newdir/refs
 364'
 365
 366test_expect_success 'init prefers command line to GIT_DIR' '
 367        rm -rf newdir &&
 368        mkdir otherdir &&
 369        GIT_DIR=otherdir git --bare init newdir &&
 370        test -d newdir/refs &&
 371        ! test -d otherdir/refs
 372'
 373
 374test_expect_success 'init with separate gitdir' '
 375        rm -rf newdir &&
 376        git init --separate-git-dir realgitdir newdir &&
 377        echo "gitdir: `pwd`/realgitdir" >expected &&
 378        test_cmp expected newdir/.git &&
 379        test -d realgitdir/refs
 380'
 381
 382test_expect_success 're-init to update git link' '
 383        (
 384        cd newdir &&
 385        git init --separate-git-dir ../surrealgitdir
 386        ) &&
 387        echo "gitdir: `pwd`/surrealgitdir" >expected &&
 388        test_cmp expected newdir/.git &&
 389        test -d surrealgitdir/refs &&
 390        ! test -d realgitdir/refs
 391'
 392
 393test_expect_success 're-init to move gitdir' '
 394        rm -rf newdir realgitdir surrealgitdir &&
 395        git init newdir &&
 396        (
 397        cd newdir &&
 398        git init --separate-git-dir ../realgitdir
 399        ) &&
 400        echo "gitdir: `pwd`/realgitdir" >expected &&
 401        test_cmp expected newdir/.git &&
 402        test -d realgitdir/refs
 403'
 404
 405test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 406        rm -rf newdir realgitdir &&
 407        git init newdir &&
 408        (
 409        cd newdir &&
 410        mv .git here &&
 411        ln -s here .git &&
 412        git init --separate-git-dir ../realgitdir
 413        ) &&
 414        echo "gitdir: `pwd`/realgitdir" >expected &&
 415        test_cmp expected newdir/.git &&
 416        test -d realgitdir/refs &&
 417        ! test -d newdir/here
 418'
 419
 420test_done