Skip to content

Commit 8a58b6c

Browse files
committed
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()` 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
1 parent 2565546 commit 8a58b6c

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

builtin/repo.c

Lines changed: 28 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,
@@ -82,11 +93,28 @@ static int get_references_format(struct repository *repo, struct strbuf *buf)
8293
return 0;
8394
}
8495

96+
/*
97+
* rev-parse --<opt> repo info <field>
98+
*
99+
* is-bare-repository layout.bare
100+
* is-shallow-repository layout.shallow
101+
* show-object-format object.format
102+
* show-ref-format references.format
103+
* show-toplevel paths.toplevel
104+
*
105+
* show-cdup <missing>
106+
* show-prefix <missing>
107+
*
108+
* Some <missing> entries may be candidates for future
109+
* implementation, while others may not be needed.
110+
*/
111+
85112
/* repo_info_field keys must be in lexicographical order */
86113
static const struct repo_info_field repo_info_field[] = {
87114
{ "layout.bare", get_layout_bare },
88115
{ "layout.shallow", get_layout_shallow },
89116
{ "object.format", get_object_format },
117+
{ "paths.toplevel", get_paths_toplevel },
90118
{ "references.format", get_references_format },
91119
};
92120

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)