Skip to content

Commit 348e361

Browse files
repo: add path keys to repo info
Add a path category to git repo info with key-value pairs that mirror repository paths users commonly retrieve via rev-parse and git-path lookups. This makes scripting against repo metadata more direct and avoids shelling out to multiple commands for related paths. The new keys are introduced as explicit path.* entries in repo_info_fields and are resolved through dedicated helpers. This keeps lookup behavior predictable and makes future path additions straightforward. Signed-off-by: Eslam reda ragheb <eslam.reda.div@gmail.com>
1 parent e9ea572 commit 348e361

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

builtin/repo.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#define USE_THE_REPOSITORY_VARIABLE
22

33
#include "builtin.h"
4+
#include "abspath.h"
45
#include "environment.h"
56
#include "hex.h"
67
#include "odb.h"
78
#include "parse-options.h"
9+
#include "path.h"
810
#include "path-walk.h"
911
#include "progress.h"
1012
#include "quote.h"
@@ -14,6 +16,7 @@
1416
#include "strbuf.h"
1517
#include "string-list.h"
1618
#include "shallow.h"
19+
#include "submodule.h"
1720
#include "utf8.h"
1821

1922
static const char *const repo_usage[] = {
@@ -40,6 +43,13 @@ struct field {
4043
get_value_fn *get_value;
4144
};
4245

46+
static void repo_info_add_path(struct repo_info *info,
47+
struct strbuf *buf,
48+
const char *path)
49+
{
50+
strbuf_add_absolute_path(buf, path);
51+
}
52+
4353
static int get_layout_bare(struct repo_info *info UNUSED, struct strbuf *buf)
4454
{
4555
strbuf_addstr(buf, is_bare_repository() ? "true" : "false");
@@ -61,6 +71,119 @@ static int get_object_format(struct repo_info *info, struct strbuf *buf)
6171
return 0;
6272
}
6373

74+
static int get_path_common_dir(struct repo_info *info, struct strbuf *buf)
75+
{
76+
repo_info_add_path(info, buf, repo_get_common_dir(info->repo));
77+
return 0;
78+
}
79+
80+
static int get_path_config_file(struct repo_info *info, struct strbuf *buf)
81+
{
82+
struct strbuf path = STRBUF_INIT;
83+
84+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "config"));
85+
strbuf_release(&path);
86+
return 0;
87+
}
88+
89+
static int get_path_git_dir(struct repo_info *info, struct strbuf *buf)
90+
{
91+
repo_info_add_path(info, buf, repo_get_git_dir(info->repo));
92+
return 0;
93+
}
94+
95+
static int get_path_git_prefix(struct repo_info *info, struct strbuf *buf)
96+
{
97+
if (info->prefix)
98+
strbuf_addstr(buf, info->prefix);
99+
return 0;
100+
}
101+
102+
static int get_path_grafts_file(struct repo_info *info, struct strbuf *buf)
103+
{
104+
repo_info_add_path(info, buf, repo_get_graft_file(info->repo));
105+
return 0;
106+
}
107+
108+
static int get_path_hooks_directory(struct repo_info *info, struct strbuf *buf)
109+
{
110+
struct strbuf path = STRBUF_INIT;
111+
112+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "hooks"));
113+
strbuf_release(&path);
114+
return 0;
115+
}
116+
117+
static int get_path_index_file(struct repo_info *info, struct strbuf *buf)
118+
{
119+
repo_info_add_path(info, buf, repo_get_index_file(info->repo));
120+
return 0;
121+
}
122+
123+
static int get_path_logs_directory(struct repo_info *info, struct strbuf *buf)
124+
{
125+
struct strbuf path = STRBUF_INIT;
126+
127+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "logs"));
128+
strbuf_release(&path);
129+
return 0;
130+
}
131+
132+
static int get_path_objects_directory(struct repo_info *info, struct strbuf *buf)
133+
{
134+
repo_info_add_path(info, buf, repo_get_object_directory(info->repo));
135+
return 0;
136+
}
137+
138+
static int get_path_packed_refs_file(struct repo_info *info, struct strbuf *buf)
139+
{
140+
struct strbuf path = STRBUF_INIT;
141+
142+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "packed-refs"));
143+
strbuf_release(&path);
144+
return 0;
145+
}
146+
147+
static int get_path_refs_directory(struct repo_info *info, struct strbuf *buf)
148+
{
149+
struct strbuf path = STRBUF_INIT;
150+
151+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "refs"));
152+
strbuf_release(&path);
153+
return 0;
154+
}
155+
156+
static int get_path_shallow_file(struct repo_info *info, struct strbuf *buf)
157+
{
158+
struct strbuf path = STRBUF_INIT;
159+
160+
repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "shallow"));
161+
strbuf_release(&path);
162+
return 0;
163+
}
164+
165+
static int get_path_superproject_working_tree(struct repo_info *info,
166+
struct strbuf *buf)
167+
{
168+
struct strbuf superproject = STRBUF_INIT;
169+
170+
if (get_superproject_working_tree(&superproject))
171+
repo_info_add_path(info, buf, superproject.buf);
172+
173+
strbuf_release(&superproject);
174+
return 0;
175+
}
176+
177+
static int get_path_toplevel(struct repo_info *info, struct strbuf *buf)
178+
{
179+
const char *work_tree = repo_get_work_tree(info->repo);
180+
181+
if (work_tree)
182+
repo_info_add_path(info, buf, work_tree);
183+
184+
return 0;
185+
}
186+
64187
static int get_references_format(struct repo_info *info, struct strbuf *buf)
65188
{
66189
struct repository *repo = info->repo;
@@ -74,6 +197,20 @@ static const struct field repo_info_fields[] = {
74197
{ "layout.bare", get_layout_bare },
75198
{ "layout.shallow", get_layout_shallow },
76199
{ "object.format", get_object_format },
200+
{ "path.common-dir", get_path_common_dir },
201+
{ "path.config-file", get_path_config_file },
202+
{ "path.git-dir", get_path_git_dir },
203+
{ "path.git-prefix", get_path_git_prefix },
204+
{ "path.grafts-file", get_path_grafts_file },
205+
{ "path.hooks-directory", get_path_hooks_directory },
206+
{ "path.index-file", get_path_index_file },
207+
{ "path.logs-directory", get_path_logs_directory },
208+
{ "path.objects-directory", get_path_objects_directory },
209+
{ "path.packed-refs-file", get_path_packed_refs_file },
210+
{ "path.refs-directory", get_path_refs_directory },
211+
{ "path.shallow-file", get_path_shallow_file },
212+
{ "path.superproject-working-tree", get_path_superproject_working_tree },
213+
{ "path.toplevel", get_path_toplevel },
77214
{ "references.format", get_references_format },
78215
};
79216

0 commit comments

Comments
 (0)