Skip to content

Commit 4fa4f90

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. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68372c8 commit 4fa4f90

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

submodule.c

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

1535+
void submodule_unset_core_worktree(const struct submodule *sub)
1536+
{
1537+
char *config_path = xstrfmt("%s/modules/%s/config",
1538+
get_git_common_dir(), sub->name);
1539+
1540+
if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
1541+
warning(_("Could not unset core.worktree setting in submodule '%s'"),
1542+
sub->path);
1543+
1544+
free(config_path);
1545+
}
1546+
15351547
static const char *get_super_prefix_or_empty(void)
15361548
{
15371549
const char *s = get_super_prefix();
@@ -1697,6 +1709,8 @@ int submodule_move_head(const char *path,
16971709

16981710
if (is_empty_dir(path))
16991711
rmdir_or_warn(path);
1712+
1713+
submodule_unset_core_worktree(sub);
17001714
}
17011715
}
17021716
out:

submodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ extern int submodule_move_head(const char *path,
121121
const char *new_head,
122122
unsigned flags);
123123

124+
void submodule_unset_core_worktree(const struct submodule *sub);
125+
124126
/*
125127
* Prepare the "env_array" parameter of a "struct child_process" for executing
126128
* a submodule by clearing any repo-specific environment variables, but

t/lib-submodule-update.sh

Lines changed: 2 additions & 1 deletion
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)