Skip to content

Commit 93585ad

Browse files
repo: add --path-format for info path output
Teach git repo info to accept --path-format=(absolute|relative) so scripts can request stable path style explicitly. This aligns path.* output behavior with existing rev-parse usage patterns and reduces ad-hoc path conversion in callers. The option is wired through repo_info context and used by repo_info_add_path(), so path formatting remains centralized and consistent across all path.* keys. Signed-off-by: Eslam reda ragheb <eslam.reda.div@gmail.com>
1 parent 348e361 commit 93585ad

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

builtin/repo.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
#include "utf8.h"
2121

2222
static const char *const repo_usage[] = {
23-
"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
23+
"git repo info [--format=(keyvalue|nul) | -z] [--path-format=(absolute|relative)] [--all | <key>...]",
2424
"git repo structure [--format=(table|keyvalue|nul) | -z]",
2525
NULL
2626
};
2727

28+
enum path_format {
29+
PATH_FORMAT_ABSOLUTE,
30+
PATH_FORMAT_RELATIVE,
31+
};
32+
2833
struct repo_info {
2934
struct repository *repo;
3035
const char *prefix;
36+
enum path_format path_format;
3137
};
3238

3339
typedef int get_value_fn(struct repo_info *info, struct strbuf *buf);
@@ -47,6 +53,16 @@ static void repo_info_add_path(struct repo_info *info,
4753
struct strbuf *buf,
4854
const char *path)
4955
{
56+
if (info->path_format == PATH_FORMAT_RELATIVE) {
57+
char *cwd = xgetcwd();
58+
struct strbuf rel_path = STRBUF_INIT;
59+
60+
strbuf_addstr(buf, relative_path(path, cwd, &rel_path));
61+
strbuf_release(&rel_path);
62+
free(cwd);
63+
return;
64+
}
65+
5066
strbuf_add_absolute_path(buf, path);
5167
}
5268

@@ -340,13 +356,29 @@ static int parse_format_cb(const struct option *opt,
340356
return 0;
341357
}
342358

359+
static int parse_path_format_cb(const struct option *opt,
360+
const char *arg, int unset UNUSED)
361+
{
362+
enum path_format *path_format = opt->value;
363+
364+
if (!strcmp(arg, "absolute"))
365+
*path_format = PATH_FORMAT_ABSOLUTE;
366+
else if (!strcmp(arg, "relative"))
367+
*path_format = PATH_FORMAT_RELATIVE;
368+
else
369+
die(_("invalid path format '%s'"), arg);
370+
371+
return 0;
372+
}
373+
343374
static int cmd_repo_info(int argc, const char **argv, const char *prefix,
344375
struct repository *repo)
345376
{
346377
enum output_format format = FORMAT_KEYVALUE;
347378
struct repo_info info = {
348379
.repo = repo,
349380
.prefix = prefix,
381+
.path_format = PATH_FORMAT_ABSOLUTE,
350382
};
351383
int all_keys = 0;
352384
struct option options[] = {
@@ -357,6 +389,9 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
357389
N_("synonym for --format=nul"),
358390
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
359391
parse_format_cb),
392+
OPT_CALLBACK_F(0, "path-format", &info.path_format,
393+
N_("format"), N_("path output format"),
394+
PARSE_OPT_NONEG, parse_path_format_cb),
360395
OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
361396
OPT_END()
362397
};

0 commit comments

Comments
 (0)