From: Jayesh Daga via GitGitGadget Date: Thu, 09 Apr 2026 06:01:32 GMT Subject: [PATCH v3] repo: add paths.toplevel to repo info Message-ID: In-Reply-To: From: Jayesh Daga repo info currently does not expose the repository's working tree root, even though this information is available via `repo_get_work_tree()` and `git rev-parse --show-toplevel`. Add a new field `paths.toplevel` to expose this value. While doing so, document the correspondence between `git rev-parse` options and `repo info` fields to make it easier to identify missing or future additions. For bare repositories, this value is empty, consistent with other non-applicable fields. Signed-off-by: Jayesh Daga jayeshdaga99@gmail.com --- repo: add paths.toplevel to repo info repo info currently does not expose the repository's working tree root, even though this information is available via repo_get_work_tree(). This makes it harder for scripts to retrieve the repository root through a structured interface, often requiring the use of git rev-parse --show-toplevel. Add a new field paths.toplevel to git repo info that returns the working tree root. For bare repositories, this value is empty, consistent with other non-applicable fields. This provides a consistent and script-friendly way to query repository paths without invoking additional commands. Signed-off-by: Jayesh Daga jayeshdaga99@gmail.com Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2264%2Fjayesh0104%2Frepo-toplevel-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2264/jayesh0104/repo-toplevel-v3 Pull-Request: https://github.com/git/git/pull/2264 Range-diff vs v2: 1: 05e34bfe2c ! 1: 8a58b6ce03 repo: add paths.toplevel to repo info @@ Commit message For bare repositories, this value is empty, consistent with other non-applicable fields. - Signed-off-by: Jayesh Daga [jayeshdaga99@gmail.com](mailto:jayeshdaga99@gmail.com) + Signed-off-by: Jayesh Daga jayeshdaga99@gmail.com ## builtin/repo.c ## @@ builtin/repo.c: static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) @@ builtin/repo.c: static int get_layout_bare(struct repository *repo UNUSED, struc +static int get_paths_toplevel(struct repository *repo, struct strbuf *buf) +{ -+ const char *wt = repo_get_work_tree(repo); ++ const char *wt = repo_get_work_tree(repo); + -+ if (!wt) -+ return -1; /* match existing error style */ ++ if (!wt) ++ return -1; /* match existing error style */ + -+ strbuf_addstr(buf, wt); -+ return 0; ++ strbuf_addstr(buf, wt); ++ return 0; +} + static int get_layout_shallow(struct repository *repo, struct strbuf *buf) { strbuf_addstr(buf, -@@ builtin/repo.c: static const struct repo_info_field repo_info_field[] = { +@@ builtin/repo.c: static int get_references_format(struct repository *repo, struct strbuf *buf) + return 0; + } + ++/* ++ * rev-parse -- repo info ++ * ++ * is-bare-repository layout.bare ++ * is-shallow-repository layout.shallow ++ * show-object-format object.format ++ * show-ref-format references.format ++ * show-toplevel paths.toplevel ++ * ++ * show-cdup ++ * show-prefix ++ * ++ * Some entries may be candidates for future ++ * implementation, while others may not be needed. ++ */ ++ + /* repo_info_field keys must be in lexicographical order */ + static const struct repo_info_field repo_info_field[] = { { "layout.bare", get_layout_bare }, { "layout.shallow", get_layout_shallow }, { "object.format", get_object_format }, builtin/repo.c | 28 ++++++++++++++++++++++++++++ t/t1900-repo-info.sh | 16 ++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/builtin/repo.c b/builtin/repo.c index 71a5c1c29c..8c9afc1150 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -62,6 +62,17 @@ static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) return 0; } +static int get_paths_toplevel(struct repository *repo, struct strbuf *buf) +{ + const char *wt = repo_get_work_tree(repo); + + if (!wt) + return -1; /* match existing error style */ + + strbuf_addstr(buf, wt); + return 0; +} + static int get_layout_shallow(struct repository *repo, struct strbuf *buf) { strbuf_addstr(buf, @@ -82,11 +93,28 @@ static int get_references_format(struct repository *repo, struct strbuf *buf) return 0; } +/* + * rev-parse -- repo info + * + * is-bare-repository layout.bare + * is-shallow-repository layout.shallow + * show-object-format object.format + * show-ref-format references.format + * show-toplevel paths.toplevel + * + * show-cdup + * show-prefix + * + * Some entries may be candidates for future + * implementation, while others may not be needed. + */ + /* repo_info_field keys must be in lexicographical order */ static const struct repo_info_field repo_info_field[] = { { "layout.bare", get_layout_bare }, { "layout.shallow", get_layout_shallow }, { "object.format", get_object_format }, + { "paths.toplevel", get_paths_toplevel }, { "references.format", get_references_format }, }; diff --git a/t/t1900-repo-info.sh b/t/t1900-repo-info.sh index 39bb77dda0..470e06e8c2 100755 --- a/t/t1900-repo-info.sh +++ b/t/t1900-repo-info.sh @@ -155,4 +155,20 @@ test_expect_success 'git repo info -h shows only repo info usage' ' test_grep ! "git repo structure" actual ' +test_expect_success 'repo info paths.toplevel' ' + git repo info paths.toplevel >actual && + echo "paths.toplevel=$(git rev-parse --show-toplevel)" >expected && + test_cmp expected actual +' + +test_expect_success 'repo info paths.toplevel (bare repo)' ' + git init --bare bare.git && + ( + cd bare.git && + git repo info paths.toplevel >actual && + echo "paths.toplevel=" >expected && + test_cmp expected actual + ) +' + test_done base-commit: 256554692df0685b45e60778b08802b720880c50 -- gitgitgadget