The function we introduced earlier needs to return both the path to the .git/ directory as well as the "cd-up" path to allow setup_git_directory() to retain its previous behavior as if it changed the current working directory on its quest for the .git/ directory. Let's rename it and export a function with an easier signature that *just* discovers the .git/ directory. We will use it in a subsequent patch to support early config reading better. Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx> --- cache.h | 1 + setup.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cache.h b/cache.h index 80b6372cf76..a104b76c02e 100644 --- a/cache.h +++ b/cache.h @@ -518,6 +518,7 @@ extern void set_git_work_tree(const char *tree); #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" extern void setup_work_tree(void); +extern const char *discover_git_directory(struct strbuf *gitdir); extern const char *setup_git_directory_gently(int *); extern const char *setup_git_directory(void); extern char *prefix_path(const char *prefix, int len, const char *path); diff --git a/setup.c b/setup.c index edac3c27dc1..7ceca6cc6ef 100644 --- a/setup.c +++ b/setup.c @@ -839,8 +839,8 @@ enum discovery_result { * the discovered .git/ directory, if any. This path may be relative against * `dir` (i.e. *not* necessarily the cwd). */ -static enum discovery_result discover_git_directory(struct strbuf *dir, - struct strbuf *gitdir) +static enum discovery_result discover_git_directory_1(struct strbuf *dir, + struct strbuf *gitdir) { const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; @@ -921,6 +921,29 @@ static enum discovery_result discover_git_directory(struct strbuf *dir, } } +const char *discover_git_directory(struct strbuf *gitdir) +{ + struct strbuf dir = STRBUF_INIT; + int len; + + if (strbuf_getcwd(&dir)) + return NULL; + + len = dir.len; + if (discover_git_directory_1(&dir, gitdir) < 0) { + strbuf_release(&dir); + return NULL; + } + + if (dir.len < len && !is_absolute_path(gitdir->buf)) { + strbuf_addch(&dir, '/'); + strbuf_insert(gitdir, 0, dir.buf, dir.len); + } + strbuf_release(&dir); + + return gitdir->buf; +} + const char *setup_git_directory_gently(int *nongit_ok) { struct strbuf cwd = STRBUF_INIT, dir = STRBUF_INIT, gitdir = STRBUF_INIT; @@ -947,7 +970,7 @@ const char *setup_git_directory_gently(int *nongit_ok) die_errno(_("Unable to read current working directory")); strbuf_addbuf(&dir, &cwd); - switch (discover_git_directory(&dir, &gitdir)) { + switch (discover_git_directory_1(&dir, &gitdir)) { case GIT_DIR_NONE: prefix = NULL; break; -- 2.12.0.windows.1.3.g8a117c48243