Skip to content

Commit 448dfae

Browse files
committed
repo: add paths.toplevel to repo info
Expose the working tree root via `git repo info` as paths.toplevel, matching the semantics of `git rev-parse --show-toplevel`. For bare repositories, this value is empty, consistent with other non-applicable fields. This allows scripts to retrieve the repository root through a structured interface without invoking rev-parse. Signed-off-by: Jayesh Daga <jayeshdaga99@gmail.com>
1 parent 2565546 commit 448dfae

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

builtin/repo.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf)
6262
return 0;
6363
}
6464

65+
static int get_paths_toplevel(struct repository *repo, struct strbuf *buf)
66+
{
67+
const char *wt = repo_get_work_tree(repo);
68+
69+
if (!wt)
70+
return -1; /* match existing error style */
71+
72+
strbuf_addstr(buf, wt);
73+
return 0;
74+
}
75+
6576
static int get_layout_shallow(struct repository *repo, struct strbuf *buf)
6677
{
6778
strbuf_addstr(buf,
@@ -87,6 +98,7 @@ static const struct repo_info_field repo_info_field[] = {
8798
{ "layout.bare", get_layout_bare },
8899
{ "layout.shallow", get_layout_shallow },
89100
{ "object.format", get_object_format },
101+
{ "paths.toplevel", get_paths_toplevel },
90102
{ "references.format", get_references_format },
91103
};
92104

t/t1900-repo-info.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,20 @@ test_expect_success 'git repo info -h shows only repo info usage' '
155155
test_grep ! "git repo structure" actual
156156
'
157157

158+
test_expect_success 'repo info paths.toplevel' '
159+
git repo info paths.toplevel >actual &&
160+
echo "paths.toplevel=$(git rev-parse --show-toplevel)" >expected &&
161+
test_cmp expected actual
162+
'
163+
164+
test_expect_success 'repo info paths.toplevel (bare repo)' '
165+
git init --bare bare.git &&
166+
(
167+
cd bare.git &&
168+
git repo info paths.toplevel >actual &&
169+
echo "paths.toplevel=" >expected &&
170+
test_cmp expected actual
171+
)
172+
'
173+
158174
test_done

0 commit comments

Comments
 (0)