Skip to content

Commit 898c2e6

Browse files
stefanbellergitster
authored andcommitted
submodule: unset core.worktree if no working tree is present
When a submodules work tree is removed, we should unset its core.worktree setting as the worktree is no longer present. This is not just in line with the conceptual view of submodules, but it fixes an inconvenience for looking at submodules that are not checked out: git clone --recurse-submodules git://github.com/git/git && cd git && git checkout --recurse-submodules v2.13.0 git -C .git/modules/sha1collisiondetection log fatal: cannot chdir to '../../../sha1collisiondetection': \ No such file or directory With this patch applied, the final call to git log works instead of dying in its setup, as the checkout will unset the core.worktree setting such that following log will be run in a bare repository. This patch covers all commands that are in the unpack machinery, i.e. checkout, read-tree, reset. A follow up patch will address "git submodule deinit", which will also make use of the new function submodule_unset_core_worktree(), which is why we expose it in this patch. This patch was authored as 4fa4f90 (submodule: unset core.worktree if no working tree is present, 2018-06-12), which was reverted as part of f178c13 (Revert "Merge branch 'sb/submodule-core-worktree'", 2018-09-07). The revert was needed as the nearby commit e983175 (submodule: ensure core.worktree is set after update, 2018-06-18) is faulty and at the time of 7e25437 (Merge branch 'sb/submodule-core-worktree', 2018-07-18) we could not revert the faulty commit only, as they were depending on each other: If core.worktree is unset, we have to have ways to ensure that it is set again once the working tree reappears again. Now that 4d6d6ef (Merge branch 'sb/submodule-update-in-c', 2018-09-17), specifically 74d4731 (submodule--helper: replace connect-gitdir-workingtree by ensure-core-worktree, 2018-08-13) is present, we already check and ensure core.worktree is set when populating a new work tree, such that we can re-introduce the commits that unset core.worktree when removing the worktree. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98bf667 commit 898c2e6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

submodule.c

+14
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

+2
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -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.

0 commit comments

Comments
 (0)