From: Michael Rappazzo Date: Fri, 22 Apr 2016 21:53:09 GMT Subject: [PATCH v2 1/4] rev-parse: fix some options when executed from subpath of main tree Message-ID: <1461361992-91918-2-git-send-email-rappazzo@gmail.com> In-Reply-To: <1461361992-91918-1-git-send-email-rappazzo@gmail.com> Executing `git-rev-parse` with `--git-common-dir`, `--git-path `, or `--shared-index-path` from the root of the main worktree results in a relative path to the git dir. When executed from a subdirectory of the main tree, however, it incorrectly returns a path which starts 'sub/path/.git'. Change this to return the proper relative path to the git directory. Signed-off-by: Michael Rappazzo --- builtin/rev-parse.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index c961b74..1da2e10 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -564,10 +564,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) } if (!strcmp(arg, "--git-path")) { + struct strbuf sb = STRBUF_INIT; if (!argv[i + 1]) die("--git-path requires an argument"); - puts(git_path("%s", argv[i + 1])); - i++; + + puts(relative_path(xstrfmt("%s/%s", get_git_dir(), argv[++i]), + prefix, &sb)); + strbuf_release(&sb); continue; } if (as_is) { @@ -787,8 +790,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (!strcmp(arg, "--git-common-dir")) { - const char *pfx = prefix ? prefix : ""; - puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir())); + struct strbuf sb = STRBUF_INIT; + puts(relative_path(get_git_common_dir(), prefix, &sb)); + strbuf_release(&sb); continue; } if (!strcmp(arg, "--is-inside-git-dir")) { @@ -811,7 +815,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) die(_("Could not read the index")); if (the_index.split_index) { const unsigned char *sha1 = the_index.split_index->base_sha1; - puts(git_path("sharedindex.%s", sha1_to_hex(sha1))); + struct strbuf sb = STRBUF_INIT; + + puts(relative_path( + xstrfmt("%s/sharedindex.%s", get_git_dir(), sha1_to_hex(sha1)), + prefix, &sb)); + strbuf_release(&sb); } continue; } -- 2.8.0