From ec47aa09e5bc8d9a8c07cca9f8ef17a9898819c1 Mon Sep 17 00:00:00 2001 From: Geoffrey Irving Date: Mon, 10 Aug 2009 15:59:21 -0400 Subject: [PATCH] setup.c: fix work tree setup for .git-files and aliases When .git-files and aliases are used together, the setup machinery gets confused and ends up with the wrong work_tree. Specifically, git_work_tree_cfg is set to the correct value first, but set_work_tree resets git_work_tree_cfg to the current directory, which (at least in this case) is incorrect. set_work_tree now detects this case by checking to see if git_work_tree_cfg is already set. If so, it leaves git_work_tree_cfg unchanged and instead uses the current directory to compute and return the correct prefix (where we are relative to the work tree). Signed-off-by: Geoffrey Irving --- setup.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-) diff --git a/setup.c b/setup.c index e3781b6..97f7eb1 100644 --- a/setup.c +++ b/setup.c @@ -198,13 +198,24 @@ int is_inside_work_tree(void) static const char *set_work_tree(const char *dir) { char buffer[PATH_MAX + 1]; + size_t offset; if (!getcwd(buffer, sizeof(buffer))) die ("Could not get the current working directory"); - git_work_tree_cfg = xstrdup(buffer); inside_work_tree = 1; - return NULL; + if (!git_work_tree_cfg) { + git_work_tree_cfg = xstrdup(buffer); + return NULL; + } else { + offset = strlen(git_work_tree_cfg); + if (memcmp(git_work_tree_cfg, buffer, offset) + || (buffer[offset] && buffer[offset] != '/')) + die ("fatal: not inside work tree (should not happen)"); + if (!buffer[offset] || !buffer[offset+1]) + return NULL; + return xstrdup(strcat(buffer + offset + 1, "/")); + } } void setup_work_tree(void) -- 1.6.3.3