Skip to content

Commit 5c7c063

Browse files
committed
Merge branch 'ps/fix-reinit-includeif-onbranch'
"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 9eaef58 + 407997c commit 5c7c063

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
@@ -2342,12 +2342,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
23422342
}
23432343
startup_info->have_repository = 1;
23442344

2345-
/* Ensure `core.hidedotfiles` is processed */
2346-
git_config(platform_core_config, NULL);
2347-
2348-
safe_create_dir(git_dir, 0);
2349-
2350-
23512345
/* Check to see if the repository version is right.
23522346
* Note that a newly created repository does not have
23532347
* config file, so this will not fail. What we are catching
@@ -2358,16 +2352,25 @@ int init_db(const char *git_dir, const char *real_git_dir,
23582352
validate_hash_algorithm(&repo_fmt, hash);
23592353
validate_ref_storage_format(&repo_fmt, ref_storage_format);
23602354

2361-
reinit = create_default_files(template_dir, original_git_dir,
2362-
&repo_fmt, init_shared_repository);
2363-
23642355
/*
23652356
* Now that we have set up both the hash algorithm and the ref storage
23662357
* format we can update the repository's settings accordingly.
23672358
*/
23682359
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
23692360
repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format);
23702361

2362+
/*
2363+
* Ensure `core.hidedotfiles` is processed. This must happen after we
2364+
* have set up the repository format such that we can evaluate
2365+
* includeIf conditions correctly in the case of re-initialization.
2366+
*/
2367+
git_config(platform_core_config, NULL);
2368+
2369+
safe_create_dir(git_dir, 0);
2370+
2371+
reinit = create_default_files(template_dir, original_git_dir,
2372+
&repo_fmt, init_shared_repository);
2373+
23712374
if (!(flags & INIT_DB_SKIP_REFDB))
23722375
create_reference_database(repo_fmt.ref_storage_format,
23732376
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)