]> git.ipfire.org Git - thirdparty/git.git/commitdiff
path: extract format_path() and use in rev-parse
authorK Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Wed, 24 Jun 2026 03:37:46 +0000 (09:07 +0530)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2026 04:15:52 +0000 (21:15 -0700)
Path formatting logic in builtin/rev-parse.c writes directly to
stdout. Other builtins cannot reuse it.

Extract this logic into format_path() in path.c and expose
a path_format enum in path.h.

Convert rev-parse to use the new helper in the same step to validate
the API against existing tests and avoid introducing dead code.

Mentored-by: Justin Tobler <jltobler@gmail.com>
Mentored-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
path.c
path.h

index bb882678fe2a9e3caf538f3cd99015323c79702f..7d6ac920381dab743becc5eb07d5ba67032f06f1 100644 (file)
@@ -653,53 +653,46 @@ enum default_type {
        DEFAULT_UNMODIFIED,
 };
 
-static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
+static void print_path(const char *path, const char *prefix,
+                      enum format_type format, enum default_type def)
 {
-       char *cwd = NULL;
-       /*
-        * We don't ever produce a relative path if prefix is NULL, so set the
-        * prefix to the current directory so that we can produce a relative
-        * path whenever possible.  If we're using RELATIVE_IF_SHARED mode, then
-        * we want an absolute path unless the two share a common prefix, so don't
-        * set it in that case, since doing so causes a relative path to always
-        * be produced if possible.
-        */
-       if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
-               prefix = cwd = xgetcwd();
-       if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
-               puts(path);
-       } else if (format == FORMAT_RELATIVE ||
-                 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
-               /*
-                * In order for relative_path to work as expected, we need to
-                * make sure that both paths are absolute paths.  If we don't,
-                * we can end up with an unexpected absolute path that the user
-                * didn't want.
-                */
-               struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
-               if (!is_absolute_path(path)) {
-                       strbuf_realpath_forgiving(&realbuf, path,  1);
-                       path = realbuf.buf;
-               }
-               if (!is_absolute_path(prefix)) {
-                       strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
-                       prefix = prefixbuf.buf;
+       struct strbuf sb = STRBUF_INIT;
+       enum path_format fmt;
+
+       if (format == FORMAT_DEFAULT) {
+               switch (def) {
+               case DEFAULT_RELATIVE:
+                       fmt = PATH_FORMAT_RELATIVE;
+                       break;
+               case DEFAULT_RELATIVE_IF_SHARED:
+                       fmt = PATH_FORMAT_RELATIVE_IF_SHARED;
+                       break;
+               case DEFAULT_CANONICAL:
+                       fmt = PATH_FORMAT_CANONICAL;
+                       break;
+               case DEFAULT_UNMODIFIED:
+               default:
+                       fmt = PATH_FORMAT_UNMODIFIED;
+                       break;
                }
-               puts(relative_path(path, prefix, &buf));
-               strbuf_release(&buf);
-               strbuf_release(&realbuf);
-               strbuf_release(&prefixbuf);
-       } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
-               struct strbuf buf = STRBUF_INIT;
-               puts(relative_path(path, prefix, &buf));
-               strbuf_release(&buf);
        } else {
-               struct strbuf buf = STRBUF_INIT;
-               strbuf_realpath_forgiving(&buf, path, 1);
-               puts(buf.buf);
-               strbuf_release(&buf);
+               switch (format) {
+               case FORMAT_RELATIVE:
+                       fmt = PATH_FORMAT_RELATIVE;
+                       break;
+               case FORMAT_CANONICAL:
+                       fmt = PATH_FORMAT_CANONICAL;
+                       break;
+               default:
+                       fmt = PATH_FORMAT_UNMODIFIED;
+                       break;
+               }
        }
-       free(cwd);
+
+       format_path(&sb, path, prefix, fmt);
+       puts(sb.buf);
+
+       strbuf_release(&sb);
 }
 
 int cmd_rev_parse(int argc,
diff --git a/path.c b/path.c
index d7e17bf17404de0cccec984e866cd9f7e36edab4..c3a709a9284b7ff21703f487ac93e0eaf49f95b8 100644 (file)
--- a/path.c
+++ b/path.c
@@ -1579,6 +1579,75 @@ char *xdg_cache_home(const char *filename)
        return NULL;
 }
 
+void format_path(struct strbuf *dest, const char *path,
+                const char *prefix, enum path_format format)
+{
+       strbuf_reset(dest);
+
+       switch (format) {
+       case PATH_FORMAT_UNMODIFIED:
+               strbuf_addstr(dest, path);
+               break;
+
+       case PATH_FORMAT_RELATIVE: {
+               struct strbuf relative_buf = STRBUF_INIT;
+               struct strbuf real_path = STRBUF_INIT;
+               struct strbuf real_prefix = STRBUF_INIT;
+               char *cwd = NULL;
+
+               /*
+                * We don't ever produce a relative path if prefix is NULL,
+                * so set the prefix to the current directory so that we can
+                * produce a relative path whenever possible.
+                */
+               if (!prefix)
+                       prefix = cwd = xgetcwd();
+
+               if (!is_absolute_path(path)) {
+                       strbuf_realpath_forgiving(&real_path, path, 1);
+                       path = real_path.buf;
+               }
+               if (!is_absolute_path(prefix)) {
+                       strbuf_realpath_forgiving(&real_prefix, prefix, 1);
+                       prefix = real_prefix.buf;
+               }
+
+               strbuf_addstr(dest, relative_path(path, prefix, &relative_buf));
+
+               strbuf_release(&relative_buf);
+               strbuf_release(&real_path);
+               strbuf_release(&real_prefix);
+               free(cwd);
+               break;
+       }
+
+       case PATH_FORMAT_RELATIVE_IF_SHARED: {
+               struct strbuf relative_buf = STRBUF_INIT;
+
+               /*
+                * If we're using RELATIVE_IF_SHARED mode, then we want an
+                * absolute path unless the two share a common prefix, so don't
+                * default the prefix to the current working directory. Doing so
+                * would cause a relative path to always be produced if possible.
+                */
+               strbuf_addstr(dest, relative_path(path, prefix, &relative_buf));
+               strbuf_release(&relative_buf);
+               break;
+       }
+
+       case PATH_FORMAT_CANONICAL:
+               /*
+                * strbuf_realpath_forgiving inherently resets the destination
+                * buffer, safely aligning with our replace semantics.
+                */
+               strbuf_realpath_forgiving(dest, path, 1);
+               break;
+
+       default:
+               BUG("unknown path_format value %d", format);
+       }
+}
+
 REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG")
 REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
 REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
diff --git a/path.h b/path.h
index 0434ba5e07e806013a0040e94860257244bce843..2b4089bb7cdbf630717509ecfda107b889e124e7 100644 (file)
--- a/path.h
+++ b/path.h
@@ -262,6 +262,36 @@ enum scld_error safe_create_leading_directories_no_share(char *path);
 int safe_create_file_with_leading_directories(struct repository *repo,
                                              const char *path);
 
+/**
+ * The formatting strategy to apply when writing a path into a buffer.
+ */
+enum path_format {
+       /* Output the path exactly as-is without any modifications. */
+       PATH_FORMAT_UNMODIFIED,
+
+       /* Output a path relative to the provided directory prefix. */
+       PATH_FORMAT_RELATIVE,
+
+       /* Output a relative path only if the path shares a root with the prefix. */
+       PATH_FORMAT_RELATIVE_IF_SHARED,
+
+       /* Output a fully resolved, absolute canonical path. */
+       PATH_FORMAT_CANONICAL
+};
+
+/**
+ * Format a path according to the specified formatting strategy and store
+ * the result in the given strbuf, replacing any existing contents.
+ *
+ * `dest`   : The string buffer to store the formatted path into.
+ * `path`   : The path string that needs to be formatted.
+ * `prefix` : The directory prefix to calculate relative offsets against.
+ * Pass NULL to default to the current working directory where applicable.
+ * `format` : The formatting behavior rule to execute.
+ */
+void format_path(struct strbuf *dest, const char *path,
+                const char *prefix, enum path_format format);
+
 # ifdef USE_THE_REPOSITORY_VARIABLE
 #  include "strbuf.h"
 #  include "repository.h"