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