Skip to content

Commit 3942920

Browse files
committed
Merge branch 'sb/submodule-unset-core-worktree-when-worktree-is-lost'
The core.worktree setting in a submodule repository should not be pointing at a directory when the submodule loses its working tree (e.g. getting deinit'ed), but the code did not properly maintain this invariant. * sb/submodule-unset-core-worktree-when-worktree-is-lost: submodule deinit: unset core.worktree submodule--helper: fix BUG message in ensure_core_worktree submodule: unset core.worktree if no working tree is present submodule update: add regression test with old style setups
2 parents 1ed943e + 8eda5ef commit 3942920

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

builtin/submodule--helper.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,8 @@ static void deinit_submodule(const char *path, const char *prefix,
11311131
if (!(flags & OPT_QUIET))
11321132
printf(format, displaypath);
11331133

1134+
submodule_unset_core_worktree(sub);
1135+
11341136
strbuf_release(&sb_rm);
11351137
}
11361138

@@ -2046,7 +2048,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
20462048
struct repository subrepo;
20472049

20482050
if (argc != 2)
2049-
BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
2051+
BUG("submodule--helper ensure-core-worktree <path>");
20502052

20512053
path = argv[1];
20522054

submodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,18 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
15611561
return ret;
15621562
}
15631563

1564+
void submodule_unset_core_worktree(const struct submodule *sub)
1565+
{
1566+
char *config_path = xstrfmt("%s/modules/%s/config",
1567+
get_git_common_dir(), sub->name);
1568+
1569+
if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
1570+
warning(_("Could not unset core.worktree setting in submodule '%s'"),
1571+
sub->path);
1572+
1573+
free(config_path);
1574+
}
1575+
15641576
static const char *get_super_prefix_or_empty(void)
15651577
{
15661578
const char *s = get_super_prefix();
@@ -1726,6 +1738,8 @@ int submodule_move_head(const char *path,
17261738

17271739
if (is_empty_dir(path))
17281740
rmdir_or_warn(path);
1741+
1742+
submodule_unset_core_worktree(sub);
17291743
}
17301744
}
17311745
out:

submodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ int submodule_move_head(const char *path,
131131
const char *new_head,
132132
unsigned flags);
133133

134+
void submodule_unset_core_worktree(const struct submodule *sub);
135+
134136
/*
135137
* Prepare the "env_array" parameter of a "struct child_process" for executing
136138
* a submodule by clearing any repo-specific environment variables, but

t/lib-submodule-update.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ reset_work_tree_to_interested () {
235235
then
236236
mkdir -p submodule_update/.git/modules/sub1/modules &&
237237
cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
238-
GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1/modules/sub2 config --unset core.worktree
238+
# core.worktree is unset for sub2 as it is not checked out
239239
fi &&
240240
# indicate we are interested in the submodule:
241241
git -C submodule_update config submodule.sub1.url "bogus" &&
@@ -709,7 +709,8 @@ test_submodule_recursing_with_args_common() {
709709
git branch -t remove_sub1 origin/remove_sub1 &&
710710
$command remove_sub1 &&
711711
test_superproject_content origin/remove_sub1 &&
712-
! test -e sub1
712+
! test -e sub1 &&
713+
test_must_fail git config -f .git/modules/sub1/config core.worktree
713714
)
714715
'
715716
# ... absorbing a .git directory along the way.

t/t7400-submodule-basic.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,11 @@ test_expect_success 'submodule deinit should remove the whole submodule section
984984
rmdir init
985985
'
986986

987+
test_expect_success 'submodule deinit should unset core.worktree' '
988+
test_path_is_file .git/modules/example/config &&
989+
test_must_fail git config -f .git/modules/example/config core.worktree
990+
'
991+
987992
test_expect_success 'submodule deinit from subdirectory' '
988993
git submodule update --init &&
989994
git config submodule.example.foo bar &&

t/t7412-submodule-absorbgitdirs.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ test_expect_success 're-setup nested submodule' '
7575
GIT_WORK_TREE=../../../nested git -C sub1/.git/modules/nested config \
7676
core.worktree "../../../nested" &&
7777
# make sure this re-setup is correct
78-
git status --ignore-submodules=none
78+
git status --ignore-submodules=none &&
79+
80+
# also make sure this old setup does not regress
81+
git submodule update --init --recursive >out 2>err &&
82+
test_must_be_empty out &&
83+
test_must_be_empty err
7984
'
8085

8186
test_expect_success 'absorb the git dir in a nested submodule' '

0 commit comments

Comments
 (0)