]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: teach discover_git_directory to respect the commondir
authorBrandon Williams <bmwill@google.com>
Wed, 14 Jun 2017 18:07:37 +0000 (11:07 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 15 Jun 2017 19:56:22 +0000 (12:56 -0700)
Currently 'discover_git_directory' only looks at the gitdir to determine
if a git directory was discovered.  This causes a problem in the event
that the gitdir which was discovered was in fact a per-worktree git
directory and not the common git directory.  This is because the
repository config, which is checked to verify the repository's format,
is stored in the commondir and not in the per-worktree gitdir.  Correct
this behavior by checking the config stored in the commondir.

It will also be of use for callers to have access to the commondir, so
lets also return that upon successfully discovering a git directory.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
config.c
setup.c

diff --git a/cache.h b/cache.h
index 160ff81aa74fceb67ab44d7901395507c647b82e..82f39f7a9e9ad17b39dc52c395eabbc70880325e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -525,12 +525,15 @@ extern void set_git_work_tree(const char *tree);
 
 extern void setup_work_tree(void);
 /*
- * Find GIT_DIR of the repository that contains the current working directory,
- * without changing the working directory or other global state. The result is
- * appended to gitdir. The return value is either NULL if no repository was
- * found, or pointing to the path inside gitdir's buffer.
- */
-extern const char *discover_git_directory(struct strbuf *gitdir);
+ * Find the commondir and gitdir of the repository that contains the current
+ * working directory, without changing the working directory or other global
+ * state. The result is appended to commondir and gitdir.  If the discovered
+ * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
+ * both have the same result appended to the buffer.  The return value is
+ * either 0 upon success and non-zero if no repository was found.
+ */
+extern int discover_git_directory(struct strbuf *commondir,
+                                 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);
index eec7613e0494a05b5cfe486d04562a287803aacb..e8dbf9e64a36ab77b4814403d291bd95e1c94843 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1639,7 +1639,8 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
 void read_early_config(config_fn_t cb, void *data)
 {
        struct config_options opts = {0};
-       struct strbuf buf = STRBUF_INIT;
+       struct strbuf commondir = STRBUF_INIT;
+       struct strbuf gitdir = STRBUF_INIT;
 
        opts.respect_includes = 1;
 
@@ -1653,12 +1654,13 @@ void read_early_config(config_fn_t cb, void *data)
         * notably, the current working directory is still the same after the
         * call).
         */
-       else if (discover_git_directory(&buf))
-               opts.git_dir = buf.buf;
+       else if (!discover_git_directory(&commondir, &gitdir))
+               opts.git_dir = gitdir.buf;
 
        git_config_with_options(cb, data, NULL, &opts);
 
-       strbuf_release(&buf);
+       strbuf_release(&commondir);
+       strbuf_release(&gitdir);
 }
 
 static void git_config_check_init(void);
diff --git a/setup.c b/setup.c
index c45443f6966ab8f2f70c9d1d3390e5746a591f96..4e38cf2a4f3782b92d92adf34a9ea83963bff339 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -941,19 +941,21 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
        }
 }
 
-const char *discover_git_directory(struct strbuf *gitdir)
+int discover_git_directory(struct strbuf *commondir,
+                          struct strbuf *gitdir)
 {
        struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
        size_t gitdir_offset = gitdir->len, cwd_len;
+       size_t commondir_offset = commondir->len;
        struct repository_format candidate;
 
        if (strbuf_getcwd(&dir))
-               return NULL;
+               return -1;
 
        cwd_len = dir.len;
        if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
                strbuf_release(&dir);
-               return NULL;
+               return -1;
        }
 
        /*
@@ -969,8 +971,10 @@ const char *discover_git_directory(struct strbuf *gitdir)
                strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len);
        }
 
+       get_common_dir(commondir, gitdir->buf + gitdir_offset);
+
        strbuf_reset(&dir);
-       strbuf_addf(&dir, "%s/config", gitdir->buf + gitdir_offset);
+       strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset);
        read_repository_format(&candidate, dir.buf);
        strbuf_release(&dir);
 
@@ -978,11 +982,12 @@ const char *discover_git_directory(struct strbuf *gitdir)
                warning("ignoring git dir '%s': %s",
                        gitdir->buf + gitdir_offset, err.buf);
                strbuf_release(&err);
+               strbuf_setlen(commondir, commondir_offset);
                strbuf_setlen(gitdir, gitdir_offset);
-               return NULL;
+               return -1;
        }
 
-       return gitdir->buf + gitdir_offset;
+       return 0;
 }
 
 const char *setup_git_directory_gently(int *nongit_ok)