Merge branch 'jc/calloc-fix'
[git] / t / t0002-gitfile.sh
1 #!/bin/sh
2
3 test_description='.git file
4
5 Verify that plumbing commands work when .git is a file
6 '
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
12 objpath() {
13         echo "$1" | sed -e 's|\(..\)|\1/|'
14 }
15
16 test_expect_success 'initial setup' '
17         REAL="$(pwd)/.real" &&
18         mv .git "$REAL"
19 '
20
21 test_expect_success 'bad setup: invalid .git file format' '
22         echo "gitdir $REAL" >.git &&
23         test_must_fail git rev-parse 2>.err &&
24         test_i18ngrep "invalid gitfile format" .err
25 '
26
27 test_expect_success 'bad setup: invalid .git file path' '
28         echo "gitdir: $REAL.not" >.git &&
29         test_must_fail git rev-parse 2>.err &&
30         test_i18ngrep "not a git repository" .err
31 '
32
33 test_expect_success 'final setup + check rev-parse --git-dir' '
34         echo "gitdir: $REAL" >.git &&
35         test "$REAL" = "$(git rev-parse --git-dir)"
36 '
37
38 test_expect_success 'check hash-object' '
39         echo "foo" >bar &&
40         SHA=$(cat bar | git hash-object -w --stdin) &&
41         test_path_is_file "$REAL/objects/$(objpath $SHA)"
42 '
43
44 test_expect_success 'check cat-file' '
45         git cat-file blob $SHA >actual &&
46         test_cmp bar actual
47 '
48
49 test_expect_success 'check update-index' '
50         test_path_is_missing "$REAL/index" &&
51         rm -f "$REAL/objects/$(objpath $SHA)" &&
52         git update-index --add bar &&
53         test_path_is_file "$REAL/index" &&
54         test_path_is_file "$REAL/objects/$(objpath $SHA)"
55 '
56
57 test_expect_success 'check write-tree' '
58         SHA=$(git write-tree) &&
59         test_path_is_file "$REAL/objects/$(objpath $SHA)"
60 '
61
62 test_expect_success 'check commit-tree' '
63         SHA=$(echo "commit bar" | git commit-tree $SHA) &&
64         test_path_is_file "$REAL/objects/$(objpath $SHA)"
65 '
66
67 test_expect_success 'check rev-list' '
68         git update-ref "HEAD" "$SHA" &&
69         test "$SHA" = "$(git rev-list HEAD)"
70 '
71
72 test_expect_success 'setup_git_dir twice in subdir' '
73         git init sgd &&
74         (
75                 cd sgd &&
76                 git config alias.lsfi ls-files &&
77                 mv .git .realgit &&
78                 echo "gitdir: .realgit" >.git &&
79                 mkdir subdir &&
80                 cd subdir &&
81                 >foo &&
82                 git add foo &&
83                 git lsfi >actual &&
84                 echo foo >expected &&
85                 test_cmp expected actual
86         )
87 '
88
89 test_expect_success 'enter_repo non-strict mode' '
90         test_create_repo enter_repo &&
91         (
92                 cd enter_repo &&
93                 test_tick &&
94                 test_commit foo &&
95                 mv .git .realgit &&
96                 echo "gitdir: .realgit" >.git
97         ) &&
98         head=$(git -C enter_repo rev-parse HEAD) &&
99         git ls-remote enter_repo >actual &&
100         cat >expected <<-EOF &&
101         $head   HEAD
102         $head   refs/heads/main
103         $head   refs/tags/foo
104         EOF
105         test_cmp expected actual
106 '
107
108 test_expect_success 'enter_repo linked checkout' '
109         (
110                 cd enter_repo &&
111                 git worktree add  ../foo refs/tags/foo
112         ) &&
113         head=$(git -C enter_repo rev-parse HEAD) &&
114         git ls-remote foo >actual &&
115         cat >expected <<-EOF &&
116         $head   HEAD
117         $head   refs/heads/main
118         $head   refs/tags/foo
119         EOF
120         test_cmp expected actual
121 '
122
123 test_expect_success 'enter_repo strict mode' '
124         head=$(git -C enter_repo rev-parse HEAD) &&
125         git ls-remote --upload-pack="git upload-pack --strict" foo/.git >actual &&
126         cat >expected <<-EOF &&
127         $head   HEAD
128         $head   refs/heads/main
129         $head   refs/tags/foo
130         EOF
131         test_cmp expected actual
132 '
133
134 test_done