From: Eslam reda ragheb via GitGitGadget Date: Thu, 26 Feb 2026 21:14:40 GMT Subject: [PATCH v4 03/10] repo: add --path-format for info path output Message-ID: <5c438d045be1becc10b687a772055df5745372ae.1772140487.git.gitgitgadget@gmail.com> In-Reply-To: From: Eslam reda ragheb 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 --- builtin/repo.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/builtin/repo.c b/builtin/repo.c index 35e1eaf7d7..e5078e5459 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -20,14 +20,20 @@ #include "utf8.h" static const char *const repo_usage[] = { - "git repo info [--format=(keyvalue|nul) | -z] [--all | ...]", + "git repo info [--format=(keyvalue|nul) | -z] [--path-format=(absolute|relative)] [--all | ...]", "git repo structure [--format=(table|keyvalue|nul) | -z]", NULL }; +enum path_format { + PATH_FORMAT_ABSOLUTE, + PATH_FORMAT_RELATIVE, +}; + struct repo_info { struct repository *repo; const char *prefix; + enum path_format path_format; }; 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, struct strbuf *buf, const char *path) { + if (info->path_format == PATH_FORMAT_RELATIVE) { + char *cwd = xgetcwd(); + struct strbuf rel_path = STRBUF_INIT; + + strbuf_addstr(buf, relative_path(path, cwd, &rel_path)); + strbuf_release(&rel_path); + free(cwd); + return; + } + strbuf_add_absolute_path(buf, path); } @@ -340,6 +356,21 @@ static int parse_format_cb(const struct option *opt, return 0; } +static int parse_path_format_cb(const struct option *opt, + const char *arg, int unset UNUSED) +{ + enum path_format *path_format = opt->value; + + if (!strcmp(arg, "absolute")) + *path_format = PATH_FORMAT_ABSOLUTE; + else if (!strcmp(arg, "relative")) + *path_format = PATH_FORMAT_RELATIVE; + else + die(_("invalid path format '%s'"), arg); + + return 0; +} + static int cmd_repo_info(int argc, const char **argv, const char *prefix, struct repository *repo) { @@ -347,6 +378,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix, struct repo_info info = { .repo = repo, .prefix = prefix, + .path_format = PATH_FORMAT_ABSOLUTE, }; int all_keys = 0; struct option options[] = { @@ -357,6 +389,9 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix, N_("synonym for --format=nul"), PARSE_OPT_NONEG | PARSE_OPT_NOARG, parse_format_cb), + OPT_CALLBACK_F(0, "path-format", &info.path_format, + N_("format"), N_("path output format"), + PARSE_OPT_NONEG, parse_path_format_cb), OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")), OPT_END() }; -- gitgitgadget