Skip to content

Commit a577fb5

Browse files
bmwillgitster
authored andcommitted
config: respect commondir
Worktrees present an interesting problem when it comes to the config. Historically we could assume that the per-repository config lives at 'gitdir/config', but since worktrees were introduced this isn't the case anymore. There is currently no way to specify per-worktree configuration, and as such the repository config is shared with all worktrees and is located at 'commondir/config'. Many users of the config machinery correctly set 'config_options.git_dir' with the repository's commondir, allowing the config to be properly loaded when operating in a worktree. But other's, like 'read_early_config()', set 'config_options.git_dir' with the repository's gitdir which can be incorrect when using worktrees. To fix this issue, and to make things less ambiguous, lets add a 'commondir' field to the 'config_options' struct and have all callers properly set both the 'git_dir' and 'commondir' fields so that the config machinery is able to properly find the repository's config. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3fb71b commit a577fb5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

config.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,8 @@ static int do_git_config_sequence(const struct config_options *opts,
15311531
char *user_config = expand_user_path("~/.gitconfig", 0);
15321532
char *repo_config;
15331533

1534-
if (opts->git_dir)
1535-
repo_config = mkpathdup("%s/config", opts->git_dir);
1534+
if (opts->commondir)
1535+
repo_config = mkpathdup("%s/config", opts->commondir);
15361536
else if (have_git_dir())
15371537
repo_config = git_pathdup("config");
15381538
else
@@ -1644,7 +1644,8 @@ void read_early_config(config_fn_t cb, void *data)
16441644

16451645
opts.respect_includes = 1;
16461646

1647-
if (have_git_dir())
1647+
if (have_git_dir()) {
1648+
opts.commondir = get_git_common_dir();
16481649
opts.git_dir = get_git_dir();
16491650
/*
16501651
* When setup_git_directory() was not yet asked to discover the
@@ -1654,8 +1655,10 @@ void read_early_config(config_fn_t cb, void *data)
16541655
* notably, the current working directory is still the same after the
16551656
* call).
16561657
*/
1657-
else if (!discover_git_directory(&commondir, &gitdir))
1658+
} else if (!discover_git_directory(&commondir, &gitdir)) {
1659+
opts.commondir = commondir.buf;
16581660
opts.git_dir = gitdir.buf;
1661+
}
16591662

16601663
git_config_with_options(cb, data, NULL, &opts);
16611664

config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum config_origin_type {
3030

3131
struct config_options {
3232
unsigned int respect_includes : 1;
33+
const char *commondir;
3334
const char *git_dir;
3435
};
3536

0 commit comments

Comments
 (0)