1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-05 02:26:14 +02:00

Merge branch 'jc/safe-implicit-bare' into next

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
This commit is contained in:
Junio C Hamano 2024-03-14 14:07:20 -07:00
commit e8bdbed1a4
2 changed files with 49 additions and 5 deletions

28
setup.c
View File

@ -1262,6 +1262,32 @@ static const char *allowed_bare_repo_to_string(
return NULL;
}
static int is_implicit_bare_repo(const char *path)
{
/*
* what we found is a ".git" directory at the root of
* the working tree.
*/
if (ends_with_path_components(path, ".git"))
return 1;
/*
* we are inside $GIT_DIR of a secondary worktree of a
* non-bare repository.
*/
if (strstr(path, "/.git/worktrees/"))
return 1;
/*
* we are inside $GIT_DIR of a worktree of a non-embedded
* submodule, whose superproject is not a bare repository.
*/
if (strstr(path, "/.git/modules/"))
return 1;
return 0;
}
/*
* We cannot decide in this function whether we are in the work tree or
* not, since the config can only be read _after_ this function was called.
@ -1391,7 +1417,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
if (is_git_directory(dir->buf)) {
trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf);
if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT &&
!ends_with_path_components(dir->buf, ".git"))
!is_implicit_bare_repo(dir->buf))
return GIT_DIR_DISALLOWED_BARE;
if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
return GIT_DIR_INVALID_OWNERSHIP;

View File

@ -29,9 +29,20 @@ expect_rejected () {
grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf"
}
test_expect_success 'setup bare repo in worktree' '
test_expect_success 'setup an embedded bare repo, secondary worktree and submodule' '
git init outer-repo &&
git init --bare outer-repo/bare-repo
git init --bare --initial-branch=main outer-repo/bare-repo &&
git -C outer-repo worktree add ../outer-secondary &&
test_path_is_dir outer-secondary &&
(
cd outer-repo &&
test_commit A &&
git push bare-repo +HEAD:refs/heads/main &&
git -c protocol.file.allow=always \
submodule add --name subn -- ./bare-repo subd
) &&
test_path_is_dir outer-repo/.git/worktrees/outer-secondary &&
test_path_is_dir outer-repo/.git/modules/subn
'
test_expect_success 'safe.bareRepository unset' '
@ -53,8 +64,7 @@ test_expect_success 'safe.bareRepository in the repository' '
# safe.bareRepository must not be "explicit", otherwise
# git config fails with "fatal: not in a git directory" (like
# safe.directory)
test_config -C outer-repo/bare-repo safe.bareRepository \
all &&
test_config -C outer-repo/bare-repo safe.bareRepository all &&
test_config_global safe.bareRepository explicit &&
expect_rejected -C outer-repo/bare-repo
'
@ -86,4 +96,12 @@ test_expect_success 'no trace when "bare repository" is a subdir of .git' '
expect_accepted_implicit -C outer-repo/.git/objects
'
test_expect_success 'no trace in $GIT_DIR of secondary worktree' '
expect_accepted_implicit -C outer-repo/.git/worktrees/outer-secondary
'
test_expect_success 'no trace in $GIT_DIR of a submodule' '
expect_accepted_implicit -C outer-repo/.git/modules/subn
'
test_done