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