Merge branch 'jc/sign-off'
[git] / t / t1500-rev-parse.sh
1 #!/bin/sh
2
3 test_description='test git rev-parse'
4 . ./test-lib.sh
5
6 test_one () {
7         dir="$1" &&
8         expect="$2" &&
9         shift &&
10         shift &&
11         echo "$expect" >expect &&
12         git -C "$dir" rev-parse "$@" >actual &&
13         test_cmp expect actual
14 }
15
16 # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
17 test_rev_parse () {
18         d=
19         bare=
20         gitdir=
21         while :
22         do
23                 case "$1" in
24                 -C) d="$2"; shift; shift ;;
25                 -b) case "$2" in
26                     [tfu]*) bare="$2"; shift; shift ;;
27                     *) error "test_rev_parse: bogus core.bare value '$2'" ;;
28                     esac ;;
29                 -g) gitdir="$2"; shift; shift ;;
30                 -*) error "test_rev_parse: unrecognized option '$1'" ;;
31                 *) break ;;
32                 esac
33         done
34
35         name=$1
36         shift
37
38         for o in --is-bare-repository \
39                  --is-inside-git-dir \
40                  --is-inside-work-tree \
41                  --show-prefix \
42                  --git-dir \
43                  --absolute-git-dir
44         do
45                 test $# -eq 0 && break
46                 expect="$1"
47                 test_expect_success "$name: $o" '
48                         if test -n "$gitdir"
49                         then
50                                 test_when_finished "unset GIT_DIR" &&
51                                 GIT_DIR="$gitdir" &&
52                                 export GIT_DIR
53                         fi &&
54
55                         case "$bare" in
56                         t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
57                         f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
58                         u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
59                         esac &&
60
61                         echo "$expect" >expect &&
62                         git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
63                         test_cmp expect actual
64                 '
65                 shift
66         done
67 }
68
69 ROOT=$(pwd)
70
71 test_expect_success 'setup' '
72         mkdir -p sub/dir work &&
73         cp -R .git repo.git &&
74         git checkout -B main &&
75         test_commit abc &&
76         git checkout -b side &&
77         test_commit def &&
78         git checkout main &&
79         git worktree add worktree side
80 '
81
82 test_rev_parse toplevel false false true '' .git "$ROOT/.git"
83
84 test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
85 test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
86
87 test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
88
89 test_rev_parse -b t 'core.bare = true' true false false
90
91 test_rev_parse -b u 'core.bare undefined' false false true
92
93
94 test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
95
96 test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
97
98 test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
99
100
101 test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git"
102
103 test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
104
105 test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
106
107 test_expect_success 'rev-parse --path-format=absolute' '
108         test_one "." "$ROOT/.git" --path-format=absolute --git-dir &&
109         test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir &&
110         test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir &&
111         test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir &&
112         test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir &&
113         test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir &&
114         test_one "." "$ROOT" --path-format=absolute --show-toplevel &&
115         test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects &&
116         test_one "." "$ROOT/.git/objects/foo/bar/baz" --path-format=absolute --git-path objects/foo/bar/baz
117 '
118
119 test_expect_success 'rev-parse --path-format=relative' '
120         test_one "." ".git" --path-format=relative --git-dir &&
121         test_one "." ".git" --path-format=relative --git-common-dir &&
122         test_one "sub/dir" "../../.git" --path-format=relative --git-dir &&
123         test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir &&
124         test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir &&
125         test_one "worktree" "../.git" --path-format=relative --git-common-dir &&
126         test_one "." "./" --path-format=relative --show-toplevel &&
127         test_one "." ".git/objects" --path-format=relative --git-path objects &&
128         test_one "." ".git/objects/foo/bar/baz" --path-format=relative --git-path objects/foo/bar/baz
129 '
130
131 test_expect_success '--path-format=relative does not affect --absolute-git-dir' '
132         git rev-parse --path-format=relative --absolute-git-dir >actual &&
133         echo "$ROOT/.git" >expect &&
134         test_cmp expect actual
135 '
136
137 test_expect_success '--path-format can change in the middle of the command line' '
138         git rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual &&
139         cat >expect <<-EOF &&
140         $ROOT/.git
141         .git/objects/foo/bar
142         EOF
143         test_cmp expect actual
144 '
145
146 test_expect_success 'git-common-dir from worktree root' '
147         echo .git >expect &&
148         git rev-parse --git-common-dir >actual &&
149         test_cmp expect actual
150 '
151
152 test_expect_success 'git-common-dir inside sub-dir' '
153         mkdir -p path/to/child &&
154         test_when_finished "rm -rf path" &&
155         echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
156         git -C path/to/child rev-parse --git-common-dir >actual &&
157         test_cmp expect actual
158 '
159
160 test_expect_success 'git-path from worktree root' '
161         echo .git/objects >expect &&
162         git rev-parse --git-path objects >actual &&
163         test_cmp expect actual
164 '
165
166 test_expect_success 'git-path inside sub-dir' '
167         mkdir -p path/to/child &&
168         test_when_finished "rm -rf path" &&
169         echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
170         git -C path/to/child rev-parse --git-path objects >actual &&
171         test_cmp expect actual
172 '
173
174 test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
175         test_commit test_commit &&
176         echo true >expect &&
177         git clone --depth 1 --no-local . shallow &&
178         test_when_finished "rm -rf shallow" &&
179         git -C shallow rev-parse --is-shallow-repository >actual &&
180         test_cmp expect actual
181 '
182
183 test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
184         echo false >expect &&
185         git rev-parse --is-shallow-repository >actual &&
186         test_cmp expect actual
187 '
188
189 test_expect_success 'rev-parse --show-object-format in repo' '
190         echo "$(test_oid algo)" >expect &&
191         git rev-parse --show-object-format >actual &&
192         test_cmp expect actual &&
193         git rev-parse --show-object-format=storage >actual &&
194         test_cmp expect actual &&
195         git rev-parse --show-object-format=input >actual &&
196         test_cmp expect actual &&
197         git rev-parse --show-object-format=output >actual &&
198         test_cmp expect actual &&
199         test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
200         grep "unknown mode for --show-object-format: squeamish-ossifrage" err
201 '
202
203 test_expect_success '--show-toplevel from subdir of working tree' '
204         pwd >expect &&
205         git -C sub/dir rev-parse --show-toplevel >actual &&
206         test_cmp expect actual
207 '
208
209 test_expect_success '--show-toplevel from inside .git' '
210         test_must_fail git -C .git rev-parse --show-toplevel
211 '
212
213 test_expect_success 'showing the superproject correctly' '
214         git rev-parse --show-superproject-working-tree >out &&
215         test_must_be_empty out &&
216
217         test_create_repo super &&
218         test_commit -C super test_commit &&
219         test_create_repo sub &&
220         test_commit -C sub test_commit &&
221         git -C super submodule add ../sub dir/sub &&
222         echo $(pwd)/super >expect  &&
223         git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
224         test_cmp expect out &&
225
226         test_commit -C super submodule_add &&
227         git -C super checkout -b branch1 &&
228         git -C super/dir/sub checkout -b branch1 &&
229         test_commit -C super/dir/sub branch1_commit &&
230         git -C super add dir/sub &&
231         test_commit -C super branch1_commit &&
232         git -C super checkout -b branch2 master &&
233         git -C super/dir/sub checkout -b branch2 master &&
234         test_commit -C super/dir/sub branch2_commit &&
235         git -C super add dir/sub &&
236         test_commit -C super branch2_commit &&
237         test_must_fail git -C super merge branch1 &&
238
239         git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
240         test_cmp expect out
241 '
242
243 test_done