Skip to content

Commit d3fb71b

Browse files
bmwillgitster
authored andcommitted
setup: teach discover_git_directory to respect the commondir
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2141fc commit d3fb71b

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

cache.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,15 @@ extern void set_git_work_tree(const char *tree);
525525

526526
extern void setup_work_tree(void);
527527
/*
528-
* Find GIT_DIR of the repository that contains the current working directory,
529-
* without changing the working directory or other global state. The result is
530-
* appended to gitdir. The return value is either NULL if no repository was
531-
* found, or pointing to the path inside gitdir's buffer.
532-
*/
533-
extern const char *discover_git_directory(struct strbuf *gitdir);
528+
* Find the commondir and gitdir of the repository that contains the current
529+
* working directory, without changing the working directory or other global
530+
* state. The result is appended to commondir and gitdir. If the discovered
531+
* gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
532+
* both have the same result appended to the buffer. The return value is
533+
* either 0 upon success and non-zero if no repository was found.
534+
*/
535+
extern int discover_git_directory(struct strbuf *commondir,
536+
struct strbuf *gitdir);
534537
extern const char *setup_git_directory_gently(int *);
535538
extern const char *setup_git_directory(void);
536539
extern char *prefix_path(const char *prefix, int len, const char *path);

config.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,8 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
16391639
void read_early_config(config_fn_t cb, void *data)
16401640
{
16411641
struct config_options opts = {0};
1642-
struct strbuf buf = STRBUF_INIT;
1642+
struct strbuf commondir = STRBUF_INIT;
1643+
struct strbuf gitdir = STRBUF_INIT;
16431644

16441645
opts.respect_includes = 1;
16451646

@@ -1653,12 +1654,13 @@ void read_early_config(config_fn_t cb, void *data)
16531654
* notably, the current working directory is still the same after the
16541655
* call).
16551656
*/
1656-
else if (discover_git_directory(&buf))
1657-
opts.git_dir = buf.buf;
1657+
else if (!discover_git_directory(&commondir, &gitdir))
1658+
opts.git_dir = gitdir.buf;
16581659

16591660
git_config_with_options(cb, data, NULL, &opts);
16601661

1661-
strbuf_release(&buf);
1662+
strbuf_release(&commondir);
1663+
strbuf_release(&gitdir);
16621664
}
16631665

16641666
static void git_config_check_init(void);

setup.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -941,19 +941,21 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
941941
}
942942
}
943943

944-
const char *discover_git_directory(struct strbuf *gitdir)
944+
int discover_git_directory(struct strbuf *commondir,
945+
struct strbuf *gitdir)
945946
{
946947
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
947948
size_t gitdir_offset = gitdir->len, cwd_len;
949+
size_t commondir_offset = commondir->len;
948950
struct repository_format candidate;
949951

950952
if (strbuf_getcwd(&dir))
951-
return NULL;
953+
return -1;
952954

953955
cwd_len = dir.len;
954956
if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
955957
strbuf_release(&dir);
956-
return NULL;
958+
return -1;
957959
}
958960

959961
/*
@@ -969,20 +971,23 @@ const char *discover_git_directory(struct strbuf *gitdir)
969971
strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len);
970972
}
971973

974+
get_common_dir(commondir, gitdir->buf + gitdir_offset);
975+
972976
strbuf_reset(&dir);
973-
strbuf_addf(&dir, "%s/config", gitdir->buf + gitdir_offset);
977+
strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset);
974978
read_repository_format(&candidate, dir.buf);
975979
strbuf_release(&dir);
976980

977981
if (verify_repository_format(&candidate, &err) < 0) {
978982
warning("ignoring git dir '%s': %s",
979983
gitdir->buf + gitdir_offset, err.buf);
980984
strbuf_release(&err);
985+
strbuf_setlen(commondir, commondir_offset);
981986
strbuf_setlen(gitdir, gitdir_offset);
982-
return NULL;
987+
return -1;
983988
}
984989

985-
return gitdir->buf + gitdir_offset;
990+
return 0;
986991
}
987992

988993
const char *setup_git_directory_gently(int *nongit_ok)

0 commit comments

Comments
 (0)