repo: add path.toplevel with absolute and relative suffix formatting
Scripts frequently need to find the root directory of a repository's working tree. Currently, this requires using `git rev-parse --show-toplevel` or inferring it from other path components. Introduce `path.toplevel.absolute` and `path.toplevel.relative` keys to `git repo info`. This allows scripts to retrieve the top-level working tree path in a predictable, strictly formatted manner without relying on `rev-parse`. If requested in a bare repository where no working tree exists, the command returns an empty string. Mentored-by: Justin Tobler <jltobler@gmail.com> Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
K Jayatheerth committed
Jul 17, 2026 at 19:00 UTC
e986e189f6a971e64c93440e899a798bd90902ce
3 files changed
+68
Documentation/git-repo.adoc
+10
@@ -119,6 +119,16 @@ values that they return:
119
`path.gitdir.relative`::
120
The path to the Git repository directory relative to the current working directory.
121
122
+`path.toplevel.absolute`::
123
+ The canonical absolute path to the top-level directory of the
124
+ repository's working tree. Outputs an empty string if the repository
125
+ is bare.
126
+
127
+`path.toplevel.relative`::
128
+ The path to the top-level directory of the repository's working
129
+ tree relative to the current working directory. Outputs an empty
130
+ string if the repository is bare.
131
+
132
`references.format`::
133
The reference storage format. The valid values are:
134
+
builtin/repo.c
+28
@@ -121,6 +121,32 @@ static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
121
return 0;
122
}
123
124
+static int get_path_toplevel_absolute(struct repository *repo, struct strbuf *buf)
125
+{
126
+ const char *work_tree = repo_get_work_tree(repo);
127
+
128
+ if (!work_tree) {
129
+ strbuf_addstr(buf, "");
130
+ return 0;
131
+ }
132
+
133
+ format_path(buf, work_tree, startup_info->prefix, PATH_FORMAT_CANONICAL);
134
+ return 0;
135
+}
136
+
137
+static int get_path_toplevel_relative(struct repository *repo, struct strbuf *buf)
138
+{
139
+ const char *work_tree = repo_get_work_tree(repo);
140
+
141
+ if (!work_tree) {
142
+ strbuf_addstr(buf, "");
143
+ return 0;
144
+ }
145
+
146
+ format_path(buf, work_tree, startup_info->prefix, PATH_FORMAT_RELATIVE);
147
+ return 0;
148
+}
149
+
150
static int get_references_format(struct repository *repo, struct strbuf *buf)
151
{
152
strbuf_addstr(buf,
@@ -137,6 +163,8 @@ static const struct repo_info_field repo_info_field[] = {
163
{ "path.commondir.relative", get_path_commondir_relative },
164
{ "path.gitdir.absolute", get_path_gitdir_absolute },
165
{ "path.gitdir.relative", get_path_gitdir_relative },
166
+ { "path.toplevel.absolute", get_path_toplevel_absolute },
167
+ { "path.toplevel.relative", get_path_toplevel_relative },
168
{ "references.format", get_references_format },
169
};
170
t/t1900-repo-info.sh
+30
@@ -213,4 +213,34 @@ test_repo_info_path 'gitdir with explicit GIT_DIR' 'gitdir' \
213
'.git' \
214
'GIT_DIR="../.git" && export GIT_DIR'
215
216
+test_expect_success 'path.toplevel absolute and relative' '
217
+ test_when_finished "rm -rf repo" &&
218
+ git init repo &&
219
+ (
220
+ mkdir -p repo/sub &&
221
+ cd repo/sub &&
222
+
223
+ ROOT="$(test-tool path-utils real_path ..)" &&
224
+
225
+ echo "path.toplevel.absolute=$ROOT" >expect.abs &&
226
+ git repo info path.toplevel.absolute >actual.abs &&
227
+ test_cmp expect.abs actual.abs &&
228
+
229
+ echo "path.toplevel.relative=../" >expect.rel &&
230
+ git repo info path.toplevel.relative >actual.rel &&
231
+ test_cmp expect.rel actual.rel
232
+ )
233
+'
234
+
235
+test_expect_success 'path.toplevel returns empty in a bare repository' '
236
+ test_when_finished "rm -rf bare.git" &&
237
+ git init --bare bare.git &&
238
+ (
239
+ cd bare.git &&
240
+ echo "path.toplevel.absolute=" >expect &&
241
+ git repo info path.toplevel.absolute >actual &&
242
+ test_cmp expect actual
243
+ )
244
+'
245
+
246
test_done