9515da3024ebfee7c3af1d87dcd09a4fe2836dc0
   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        if (
  89                mkdir plain-wt &&
  90                cd plain-wt &&
  91                GIT_WORK_TREE=$(pwd) git init
  92        )
  93        then
  94                echo Should have failed -- GIT_WORK_TREE should not be used
  95                false
  96        fi
  97'
  98
  99test_expect_success 'plain bare' '
 100        (
 101                mkdir plain-bare-1 &&
 102                cd plain-bare-1 &&
 103                git --bare init
 104        ) &&
 105        check_config plain-bare-1 true unset
 106'
 107
 108test_expect_success 'plain bare with GIT_WORK_TREE' '
 109        if (
 110                mkdir plain-bare-2 &&
 111                cd plain-bare-2 &&
 112                GIT_WORK_TREE=$(pwd) git --bare init
 113        )
 114        then
 115                echo Should have failed -- GIT_WORK_TREE should not be used
 116                false
 117        fi
 118'
 119
 120test_expect_success 'GIT_DIR bare' '
 121
 122        (
 123                mkdir git-dir-bare.git &&
 124                GIT_DIR=git-dir-bare.git git init
 125        ) &&
 126        check_config git-dir-bare.git true unset
 127'
 128
 129test_expect_success 'init --bare' '
 130
 131        (
 132                mkdir init-bare.git &&
 133                cd init-bare.git &&
 134                git init --bare
 135        ) &&
 136        check_config init-bare.git true unset
 137'
 138
 139test_expect_success 'GIT_DIR non-bare' '
 140
 141        (
 142                mkdir non-bare &&
 143                cd non-bare &&
 144                GIT_DIR=.git git init
 145        ) &&
 146        check_config non-bare/.git false unset
 147'
 148
 149test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
 150
 151        (
 152                mkdir git-dir-wt-1.git &&
 153                GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
 154        ) &&
 155        check_config git-dir-wt-1.git false "$(pwd)"
 156'
 157
 158test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
 159
 160        if (
 161                mkdir git-dir-wt-2.git &&
 162                GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-2.git git --bare init
 163        )
 164        then
 165                echo Should have failed -- --bare should not be used
 166                false
 167        fi
 168'
 169
 170test_expect_success 'reinit' '
 171
 172        (
 173                mkdir again &&
 174                cd again &&
 175                git init >out1 2>err1 &&
 176                git init >out2 2>err2
 177        ) &&
 178        test_i18ngrep "Initialized empty" again/out1 &&
 179        test_i18ngrep "Reinitialized existing" again/out2 &&
 180        >again/empty &&
 181        test_i18ncmp again/empty again/err1 &&
 182        test_i18ncmp again/empty again/err2
 183'
 184
 185test_expect_success 'init with --template' '
 186        mkdir template-source &&
 187        echo content >template-source/file &&
 188        (
 189                mkdir template-custom &&
 190                cd template-custom &&
 191                git init --template=../template-source
 192        ) &&
 193        test_cmp template-source/file template-custom/.git/file
 194'
 195
 196test_expect_success 'init with --template (blank)' '
 197        (
 198                mkdir template-plain &&
 199                cd template-plain &&
 200                git init
 201        ) &&
 202        test_path_is_file template-plain/.git/info/exclude &&
 203        (
 204                mkdir template-blank &&
 205                cd template-blank &&
 206                git init --template=
 207        ) &&
 208        test_path_is_missing template-blank/.git/info/exclude
 209'
 210
 211test_expect_success 'init with init.templatedir set' '
 212        mkdir templatedir-source &&
 213        echo Content >templatedir-source/file &&
 214        test_config_global init.templatedir "${HOME}/templatedir-source" &&
 215        (
 216                mkdir templatedir-set &&
 217                cd templatedir-set &&
 218                sane_unset GIT_TEMPLATE_DIR &&
 219                NO_SET_GIT_TEMPLATE_DIR=t &&
 220                export NO_SET_GIT_TEMPLATE_DIR &&
 221                git init
 222        ) &&
 223        test_cmp templatedir-source/file templatedir-set/.git/file
 224'
 225
 226test_expect_success 'init --bare/--shared overrides system/global config' '
 227        test_config_global core.bare false &&
 228        test_config_global core.sharedRepository 0640 &&
 229        (
 230                mkdir init-bare-shared-override &&
 231                cd init-bare-shared-override &&
 232                git init --bare --shared=0666
 233        ) &&
 234        check_config init-bare-shared-override true unset &&
 235        test x0666 = \
 236        x`git config -f init-bare-shared-override/config core.sharedRepository`
 237'
 238
 239test_expect_success 'init honors global core.sharedRepository' '
 240        test_config_global core.sharedRepository 0666 &&
 241        (
 242                mkdir shared-honor-global &&
 243                cd shared-honor-global &&
 244                git init
 245        ) &&
 246        test x0666 = \
 247        x`git config -f shared-honor-global/.git/config core.sharedRepository`
 248'
 249
 250test_expect_success 'init rejects insanely long --template' '
 251        (
 252                insane=$(printf "x%09999dx" 1) &&
 253                mkdir test &&
 254                cd test &&
 255                test_must_fail git init --template=$insane
 256        )
 257'
 258
 259test_expect_success 'init creates a new directory' '
 260        rm -fr newdir &&
 261        (
 262                git init newdir &&
 263                test_path_is_dir newdir/.git/refs
 264        )
 265'
 266
 267test_expect_success 'init creates a new bare directory' '
 268        rm -fr newdir &&
 269        (
 270                git init --bare newdir &&
 271                test_path_is_dir newdir/refs
 272        )
 273'
 274
 275test_expect_success 'init recreates a directory' '
 276        rm -fr newdir &&
 277        (
 278                mkdir newdir &&
 279                git init newdir &&
 280                test_path_is_dir newdir/.git/refs
 281        )
 282'
 283
 284test_expect_success 'init recreates a new bare directory' '
 285        rm -fr newdir &&
 286        (
 287                mkdir newdir &&
 288                git init --bare newdir &&
 289                test_path_is_dir newdir/refs
 290        )
 291'
 292
 293test_expect_success 'init creates a new deep directory' '
 294        rm -fr newdir &&
 295        git init newdir/a/b/c &&
 296        test_path_is_dir newdir/a/b/c/.git/refs
 297'
 298
 299test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
 300        rm -fr newdir &&
 301        (
 302                # Leading directories should honor umask while
 303                # the repository itself should follow "shared"
 304                umask 002 &&
 305                git init --bare --shared=0660 newdir/a/b/c &&
 306                test_path_is_dir newdir/a/b/c/refs &&
 307                ls -ld newdir/a newdir/a/b > lsab.out &&
 308                ! grep -v "^drwxrw[sx]r-x" lsab.out &&
 309                ls -ld newdir/a/b/c > lsc.out &&
 310                ! grep -v "^drwxrw[sx]---" lsc.out
 311        )
 312'
 313
 314test_expect_success 'init notices EEXIST (1)' '
 315        rm -fr newdir &&
 316        (
 317                >newdir &&
 318                test_must_fail git init newdir &&
 319                test_path_is_file newdir
 320        )
 321'
 322
 323test_expect_success 'init notices EEXIST (2)' '
 324        rm -fr newdir &&
 325        (
 326                mkdir newdir &&
 327                >newdir/a
 328                test_must_fail git init newdir/a/b &&
 329                test_path_is_file newdir/a
 330        )
 331'
 332
 333test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
 334        rm -fr newdir &&
 335        (
 336                mkdir newdir &&
 337                chmod -w newdir &&
 338                test_must_fail git init newdir/a/b
 339        )
 340'
 341
 342test_expect_success 'init creates a new bare directory with global --bare' '
 343        rm -rf newdir &&
 344        git --bare init newdir &&
 345        test_path_is_dir newdir/refs
 346'
 347
 348test_expect_success 'init prefers command line to GIT_DIR' '
 349        rm -rf newdir &&
 350        mkdir otherdir &&
 351        GIT_DIR=otherdir git --bare init newdir &&
 352        test_path_is_dir newdir/refs &&
 353        test_path_is_missing otherdir/refs
 354'
 355
 356test_expect_success 'init with separate gitdir' '
 357        rm -rf newdir &&
 358        git init --separate-git-dir realgitdir newdir &&
 359        echo "gitdir: `pwd`/realgitdir" >expected &&
 360        test_cmp expected newdir/.git &&
 361        test_path_is_dir realgitdir/refs
 362'
 363
 364test_expect_success 're-init on .git file' '
 365        ( cd newdir && git init )
 366'
 367
 368test_expect_success 're-init to update git link' '
 369        (
 370        cd newdir &&
 371        git init --separate-git-dir ../surrealgitdir
 372        ) &&
 373        echo "gitdir: `pwd`/surrealgitdir" >expected &&
 374        test_cmp expected newdir/.git &&
 375        test_path_is_dir surrealgitdir/refs &&
 376        test_path_is_missing realgitdir/refs
 377'
 378
 379test_expect_success 're-init to move gitdir' '
 380        rm -rf newdir realgitdir surrealgitdir &&
 381        git init newdir &&
 382        (
 383        cd newdir &&
 384        git init --separate-git-dir ../realgitdir
 385        ) &&
 386        echo "gitdir: `pwd`/realgitdir" >expected &&
 387        test_cmp expected newdir/.git &&
 388        test_path_is_dir realgitdir/refs
 389'
 390
 391test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 392        rm -rf newdir realgitdir &&
 393        git init newdir &&
 394        (
 395        cd newdir &&
 396        mv .git here &&
 397        ln -s here .git &&
 398        git init --separate-git-dir ../realgitdir
 399        ) &&
 400        echo "gitdir: `pwd`/realgitdir" >expected &&
 401        test_cmp expected newdir/.git &&
 402        test_cmp expected newdir/here &&
 403        test_path_is_dir realgitdir/refs
 404'
 405
 406test_done