Subject: [WIP PATCH 01/26] rev-parse --git-dir: print relative gitdir correctly

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 16 Feb 2010 23:04:52 +0700

When git_dir is relative, it is relative to Git's current working
directory, which is worktree top directory. "git rev-parse --git-dir"
is expected to output relative to user's current working directory.

Catch this case and make gitdir absolute (the easy way).

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 This one is probably not needed for now. In my later patches, set_git_dir()
 is used more, and this becomes necessary.

 builtin-rev-parse.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index a8c5043..a054256 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -638,6 +638,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 				static char cwd[PATH_MAX];
 				if (gitdir) {
+					if (prefix && !is_absolute_path(gitdir)) {
+						int len;
+						if (!getcwd(cwd, PATH_MAX))
+							die_errno("unable to get current working directory");
+						len = strlen(cwd);
+						printf("%s%s%s\n", cwd, len && cwd[len-1] != '/' ? "/" : "", gitdir);
+						continue;
+					}
 					puts(gitdir);
 					continue;
 				}
-- 
1.7.0.195.g637a2