Skip to content

Commit dc97afd

Browse files
committed
Merge branch 'jc/safe-implicit-bare'
Users with safe.bareRepository=explicit can still work from within $GIT_DIR of a seconary worktree (which resides at .git/worktrees/$name/) of the primary worktree without explicitly specifying the $GIT_DIR environment variable or the --git-dir=<path> option. * jc/safe-implicit-bare: setup: notice more types of implicit bare repositories
2 parents 8be51c1 + 30b7c4b commit dc97afd

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

setup.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,32 @@ static const char *allowed_bare_repo_to_string(
12431243
return NULL;
12441244
}
12451245

1246+
static int is_implicit_bare_repo(const char *path)
1247+
{
1248+
/*
1249+
* what we found is a ".git" directory at the root of
1250+
* the working tree.
1251+
*/
1252+
if (ends_with_path_components(path, ".git"))
1253+
return 1;
1254+
1255+
/*
1256+
* we are inside $GIT_DIR of a secondary worktree of a
1257+
* non-bare repository.
1258+
*/
1259+
if (strstr(path, "/.git/worktrees/"))
1260+
return 1;
1261+
1262+
/*
1263+
* we are inside $GIT_DIR of a worktree of a non-embedded
1264+
* submodule, whose superproject is not a bare repository.
1265+
*/
1266+
if (strstr(path, "/.git/modules/"))
1267+
return 1;
1268+
1269+
return 0;
1270+
}
1271+
12461272
/*
12471273
* We cannot decide in this function whether we are in the work tree or
12481274
* not, since the config can only be read _after_ this function was called.
@@ -1372,7 +1398,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
13721398
if (is_git_directory(dir->buf)) {
13731399
trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf);
13741400
if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT &&
1375-
!ends_with_path_components(dir->buf, ".git"))
1401+
!is_implicit_bare_repo(dir->buf))
13761402
return GIT_DIR_DISALLOWED_BARE;
13771403
if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
13781404
return GIT_DIR_INVALID_OWNERSHIP;

t/t0035-safe-bare-repository.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,20 @@ expect_rejected () {
2929
grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf"
3030
}
3131

32-
test_expect_success 'setup bare repo in worktree' '
32+
test_expect_success 'setup an embedded bare repo, secondary worktree and submodule' '
3333
git init outer-repo &&
34-
git init --bare outer-repo/bare-repo
34+
git init --bare --initial-branch=main outer-repo/bare-repo &&
35+
git -C outer-repo worktree add ../outer-secondary &&
36+
test_path_is_dir outer-secondary &&
37+
(
38+
cd outer-repo &&
39+
test_commit A &&
40+
git push bare-repo +HEAD:refs/heads/main &&
41+
git -c protocol.file.allow=always \
42+
submodule add --name subn -- ./bare-repo subd
43+
) &&
44+
test_path_is_dir outer-repo/.git/worktrees/outer-secondary &&
45+
test_path_is_dir outer-repo/.git/modules/subn
3546
'
3647

3748
test_expect_success 'safe.bareRepository unset' '
@@ -53,8 +64,7 @@ test_expect_success 'safe.bareRepository in the repository' '
5364
# safe.bareRepository must not be "explicit", otherwise
5465
# git config fails with "fatal: not in a git directory" (like
5566
# safe.directory)
56-
test_config -C outer-repo/bare-repo safe.bareRepository \
57-
all &&
67+
test_config -C outer-repo/bare-repo safe.bareRepository all &&
5868
test_config_global safe.bareRepository explicit &&
5969
expect_rejected -C outer-repo/bare-repo
6070
'
@@ -86,4 +96,12 @@ test_expect_success 'no trace when "bare repository" is a subdir of .git' '
8696
expect_accepted_implicit -C outer-repo/.git/objects
8797
'
8898

99+
test_expect_success 'no trace in $GIT_DIR of secondary worktree' '
100+
expect_accepted_implicit -C outer-repo/.git/worktrees/outer-secondary
101+
'
102+
103+
test_expect_success 'no trace in $GIT_DIR of a submodule' '
104+
expect_accepted_implicit -C outer-repo/.git/modules/subn
105+
'
106+
89107
test_done

0 commit comments

Comments
 (0)