Skip to content

Commit 6e3eb34

Browse files
committed
Merge branch 'ps/fix-reinit-includeif-onbranch' into maint-2.45
"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
2 parents 903b4da + 407997c commit 6e3eb34

File tree

2 files changed

+105
-17
lines changed

2 files changed

+105
-17
lines changed

setup.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,12 +2286,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
22862286
}
22872287
startup_info->have_repository = 1;
22882288

2289-
/* Ensure `core.hidedotfiles` is processed */
2290-
git_config(platform_core_config, NULL);
2291-
2292-
safe_create_dir(git_dir, 0);
2293-
2294-
22952289
/* Check to see if the repository version is right.
22962290
* Note that a newly created repository does not have
22972291
* config file, so this will not fail. What we are catching
@@ -2302,16 +2296,25 @@ int init_db(const char *git_dir, const char *real_git_dir,
23022296
validate_hash_algorithm(&repo_fmt, hash);
23032297
validate_ref_storage_format(&repo_fmt, ref_storage_format);
23042298

2305-
reinit = create_default_files(template_dir, original_git_dir,
2306-
&repo_fmt, init_shared_repository);
2307-
23082299
/*
23092300
* Now that we have set up both the hash algorithm and the ref storage
23102301
* format we can update the repository's settings accordingly.
23112302
*/
23122303
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
23132304
repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format);
23142305

2306+
/*
2307+
* Ensure `core.hidedotfiles` is processed. This must happen after we
2308+
* have set up the repository format such that we can evaluate
2309+
* includeIf conditions correctly in the case of re-initialization.
2310+
*/
2311+
git_config(platform_core_config, NULL);
2312+
2313+
safe_create_dir(git_dir, 0);
2314+
2315+
reinit = create_default_files(template_dir, original_git_dir,
2316+
&repo_fmt, init_shared_repository);
2317+
23152318
if (!(flags & INIT_DB_SKIP_REFDB))
23162319
create_reference_database(repo_fmt.ref_storage_format,
23172320
initial_branch, flags & INIT_DB_QUIET);

t/t0001-init.sh

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,39 @@ test_expect_success 'init with --ref-format=files' '
584584
test_cmp expect actual
585585
'
586586

587-
test_expect_success 're-init with same format' '
588-
test_when_finished "rm -rf refformat" &&
589-
git init --ref-format=files refformat &&
590-
git init --ref-format=files refformat &&
591-
echo files >expect &&
592-
git -C refformat rev-parse --show-ref-format >actual &&
593-
test_cmp expect actual
594-
'
587+
backends="files reftable"
588+
for from_format in $backends
589+
do
590+
test_expect_success "re-init with same format ($from_format)" '
591+
test_when_finished "rm -rf refformat" &&
592+
git init --ref-format=$from_format refformat &&
593+
git init --ref-format=$from_format refformat &&
594+
echo $from_format >expect &&
595+
git -C refformat rev-parse --show-ref-format >actual &&
596+
test_cmp expect actual
597+
'
598+
599+
for to_format in $backends
600+
do
601+
if test "$from_format" = "$to_format"
602+
then
603+
continue
604+
fi
605+
606+
test_expect_success "re-init with different format fails ($from_format -> $to_format)" '
607+
test_when_finished "rm -rf refformat" &&
608+
git init --ref-format=$from_format refformat &&
609+
cat >expect <<-EOF &&
610+
fatal: attempt to reinitialize repository with different reference storage format
611+
EOF
612+
test_must_fail git init --ref-format=$to_format refformat 2>err &&
613+
test_cmp expect err &&
614+
echo $from_format >expect &&
615+
git -C refformat rev-parse --show-ref-format >actual &&
616+
test_cmp expect actual
617+
'
618+
done
619+
done
595620

596621
test_expect_success 'init with --ref-format=garbage' '
597622
test_when_finished "rm -rf refformat" &&
@@ -678,4 +703,64 @@ test_expect_success 'branch -m with the initial branch' '
678703
test_cmp expect actual
679704
'
680705

706+
test_expect_success 'init with includeIf.onbranch condition' '
707+
test_when_finished "rm -rf repo" &&
708+
git -c includeIf.onbranch:main.path=nonexistent init repo &&
709+
echo $GIT_DEFAULT_REF_FORMAT >expect &&
710+
git -C repo rev-parse --show-ref-format >actual &&
711+
test_cmp expect actual
712+
'
713+
714+
test_expect_success 'init with includeIf.onbranch condition with existing directory' '
715+
test_when_finished "rm -rf repo" &&
716+
mkdir repo &&
717+
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
718+
echo $GIT_DEFAULT_REF_FORMAT >expect &&
719+
git -C repo rev-parse --show-ref-format >actual &&
720+
test_cmp expect actual
721+
'
722+
723+
test_expect_success 're-init with includeIf.onbranch condition' '
724+
test_when_finished "rm -rf repo" &&
725+
git init repo &&
726+
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
727+
echo $GIT_DEFAULT_REF_FORMAT >expect &&
728+
git -C repo rev-parse --show-ref-format >actual &&
729+
test_cmp expect actual
730+
'
731+
732+
test_expect_success 're-init with includeIf.onbranch condition' '
733+
test_when_finished "rm -rf repo" &&
734+
git init repo &&
735+
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
736+
echo $GIT_DEFAULT_REF_FORMAT >expect &&
737+
git -C repo rev-parse --show-ref-format >actual &&
738+
test_cmp expect actual
739+
'
740+
741+
test_expect_success 're-init skips non-matching includeIf.onbranch' '
742+
test_when_finished "rm -rf repo config" &&
743+
cat >config <<-EOF &&
744+
[
745+
garbage
746+
EOF
747+
git init repo &&
748+
git -c includeIf.onbranch:nonexistent.path="$(test-tool path-utils absolute_path config)" init repo
749+
'
750+
751+
test_expect_success 're-init reads matching includeIf.onbranch' '
752+
test_when_finished "rm -rf repo config" &&
753+
cat >config <<-EOF &&
754+
[
755+
garbage
756+
EOF
757+
path="$(test-tool path-utils absolute_path config)" &&
758+
git init --initial-branch=branch repo &&
759+
cat >expect <<-EOF &&
760+
fatal: bad config line 1 in file $path
761+
EOF
762+
test_must_fail git -c includeIf.onbranch:branch.path="$path" init repo 2>err &&
763+
test_cmp expect err
764+
'
765+
681766
test_done

0 commit comments

Comments
 (0)