Skip to content

Commit ccf236a

Browse files
sunshinecogitster
authored andcommitted
init: disallow --separate-git-dir with bare repository
The purpose of "git init --separate-git-dir" is to separate the repository from the worktree. This is true even when --separate-git-dir is used on an existing worktree, in which case, it moves the .git/ subdirectory to a new location outside the worktree. However, an outright bare repository (such as one created by "git init --bare"), has no worktree, so using --separate-git-dir to separate it from its non-existent worktree is nonsensical. Therefore, make it an error to use --separate-git-dir on a bare repository. Implementation note: "git init" considers a repository bare if told so explicitly via --bare or if it guesses it to be so based upon heuristics. In the explicit --bare case, a conflict with --separate-git-dir is easy to detect early. In the guessed case, however, the conflict can only be detected once "bareness" is guessed, which happens after "git init" has begun creating the repository. Technically, we can get by with a single late check which would cover both cases, however, erroring out early, when possible, without leaving detritus provides a better user experience. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 47ae905 commit ccf236a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

builtin/init-db.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
568568

569569
argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
570570

571+
if (real_git_dir && is_bare_repository_cfg == 1)
572+
die(_("--separate-git-dir and --bare are mutually exclusive"));
573+
571574
if (real_git_dir && !is_absolute_path(real_git_dir))
572575
real_git_dir = real_pathdup(real_git_dir, 1);
573576

@@ -663,6 +666,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
663666
get_git_work_tree());
664667
}
665668
else {
669+
if (real_git_dir)
670+
die(_("--separate-git-dir incompatible with bare repository"));
666671
if (work_tree)
667672
set_git_work_tree(work_tree);
668673
}

t/t0001-init.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ test_expect_success 'init with separate gitdir' '
316316
test_path_is_dir realgitdir/refs
317317
'
318318

319+
test_expect_success 'explicit bare & --separate-git-dir incompatible' '
320+
test_must_fail git init --bare --separate-git-dir goop.git bare.git 2>err &&
321+
test_i18ngrep "mutually exclusive" err
322+
'
323+
324+
test_expect_success 'implicit bare & --separate-git-dir incompatible' '
325+
test_when_finished "rm -rf bare.git" &&
326+
mkdir -p bare.git &&
327+
test_must_fail env GIT_DIR=. \
328+
git -C bare.git init --separate-git-dir goop.git 2>err &&
329+
test_i18ngrep "incompatible" err
330+
'
331+
319332
test_lazy_prereq GETCWD_IGNORES_PERMS '
320333
base=GETCWD_TEST_BASE_DIR &&
321334
mkdir -p $base/dir &&

0 commit comments

Comments
 (0)