Skip to content

Commit

Permalink
Merge branch 'ps/fix-reinit-includeif-onbranch' into maint-2.45
Browse files Browse the repository at this point in the history
"git init" in an already created directory, when the user
configuration has includeif.onbranch, started to fail recently,
which has been corrected.

* ps/fix-reinit-includeif-onbranch:
  setup: fix bug with "includeIf.onbranch" when initializing dir
  • Loading branch information
gitster committed Jun 28, 2024
2 parents 903b4da + 407997c commit 6e3eb34
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
21 changes: 12 additions & 9 deletions setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2286,12 +2286,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
}
startup_info->have_repository = 1;

/* Ensure `core.hidedotfiles` is processed */
git_config(platform_core_config, NULL);

safe_create_dir(git_dir, 0);


/* Check to see if the repository version is right.
* Note that a newly created repository does not have
* config file, so this will not fail. What we are catching
Expand All @@ -2302,16 +2296,25 @@ int init_db(const char *git_dir, const char *real_git_dir,
validate_hash_algorithm(&repo_fmt, hash);
validate_ref_storage_format(&repo_fmt, ref_storage_format);

reinit = create_default_files(template_dir, original_git_dir,
&repo_fmt, init_shared_repository);

/*
* Now that we have set up both the hash algorithm and the ref storage
* format we can update the repository's settings accordingly.
*/
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format);

/*
* Ensure `core.hidedotfiles` is processed. This must happen after we
* have set up the repository format such that we can evaluate
* includeIf conditions correctly in the case of re-initialization.
*/
git_config(platform_core_config, NULL);

safe_create_dir(git_dir, 0);

reinit = create_default_files(template_dir, original_git_dir,
&repo_fmt, init_shared_repository);

if (!(flags & INIT_DB_SKIP_REFDB))
create_reference_database(repo_fmt.ref_storage_format,
initial_branch, flags & INIT_DB_QUIET);
Expand Down
101 changes: 93 additions & 8 deletions t/t0001-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,39 @@ test_expect_success 'init with --ref-format=files' '
test_cmp expect actual
'

test_expect_success 're-init with same format' '
test_when_finished "rm -rf refformat" &&
git init --ref-format=files refformat &&
git init --ref-format=files refformat &&
echo files >expect &&
git -C refformat rev-parse --show-ref-format >actual &&
test_cmp expect actual
'
backends="files reftable"
for from_format in $backends
do
test_expect_success "re-init with same format ($from_format)" '
test_when_finished "rm -rf refformat" &&
git init --ref-format=$from_format refformat &&
git init --ref-format=$from_format refformat &&
echo $from_format >expect &&
git -C refformat rev-parse --show-ref-format >actual &&
test_cmp expect actual
'

for to_format in $backends
do
if test "$from_format" = "$to_format"
then
continue
fi

test_expect_success "re-init with different format fails ($from_format -> $to_format)" '
test_when_finished "rm -rf refformat" &&
git init --ref-format=$from_format refformat &&
cat >expect <<-EOF &&
fatal: attempt to reinitialize repository with different reference storage format
EOF
test_must_fail git init --ref-format=$to_format refformat 2>err &&
test_cmp expect err &&
echo $from_format >expect &&
git -C refformat rev-parse --show-ref-format >actual &&
test_cmp expect actual
'
done
done

test_expect_success 'init with --ref-format=garbage' '
test_when_finished "rm -rf refformat" &&
Expand Down Expand Up @@ -678,4 +703,64 @@ test_expect_success 'branch -m with the initial branch' '
test_cmp expect actual
'

test_expect_success 'init with includeIf.onbranch condition' '
test_when_finished "rm -rf repo" &&
git -c includeIf.onbranch:main.path=nonexistent init repo &&
echo $GIT_DEFAULT_REF_FORMAT >expect &&
git -C repo rev-parse --show-ref-format >actual &&
test_cmp expect actual
'

test_expect_success 'init with includeIf.onbranch condition with existing directory' '
test_when_finished "rm -rf repo" &&
mkdir repo &&
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
echo $GIT_DEFAULT_REF_FORMAT >expect &&
git -C repo rev-parse --show-ref-format >actual &&
test_cmp expect actual
'

test_expect_success 're-init with includeIf.onbranch condition' '
test_when_finished "rm -rf repo" &&
git init repo &&
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
echo $GIT_DEFAULT_REF_FORMAT >expect &&
git -C repo rev-parse --show-ref-format >actual &&
test_cmp expect actual
'

test_expect_success 're-init with includeIf.onbranch condition' '
test_when_finished "rm -rf repo" &&
git init repo &&
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
echo $GIT_DEFAULT_REF_FORMAT >expect &&
git -C repo rev-parse --show-ref-format >actual &&
test_cmp expect actual
'

test_expect_success 're-init skips non-matching includeIf.onbranch' '
test_when_finished "rm -rf repo config" &&
cat >config <<-EOF &&
[
garbage
EOF
git init repo &&
git -c includeIf.onbranch:nonexistent.path="$(test-tool path-utils absolute_path config)" init repo
'

test_expect_success 're-init reads matching includeIf.onbranch' '
test_when_finished "rm -rf repo config" &&
cat >config <<-EOF &&
[
garbage
EOF
path="$(test-tool path-utils absolute_path config)" &&
git init --initial-branch=branch repo &&
cat >expect <<-EOF &&
fatal: bad config line 1 in file $path
EOF
test_must_fail git -c includeIf.onbranch:branch.path="$path" init repo 2>err &&
test_cmp expect err
'

test_done

0 comments on commit 6e3eb34

Please sign in to comment.