Skip to content

Commit 3ef2538

Browse files
stefanbellergitster
authored andcommitted
recursive submodules: detach HEAD from new state
When a submodule is on a branch and in its superproject you run a recursive checkout, the branch of the submodule is updated to what the superproject checks out. This is very unexpected in the current model of Git as e.g. 'submodule update' always detaches the submodule HEAD. Despite having plans to have submodule HEADS not detached in the future, the current behavior is really bad as it doesn't match user expectations and it is not checking for loss of commits (only to be recovered via the reflog). Detach the HEAD unconditionally in the submodule when updating it. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5800c63 commit 3ef2538

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

submodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,8 @@ int submodule_move_head(const char *path,
16411641
cp.dir = path;
16421642

16431643
prepare_submodule_repo_env(&cp.env_array);
1644-
argv_array_pushl(&cp.args, "update-ref", "HEAD", new, NULL);
1644+
argv_array_pushl(&cp.args, "update-ref", "HEAD",
1645+
"--no-deref", new, NULL);
16451646

16461647
if (run_command(&cp)) {
16471648
ret = -1;

t/lib-submodule-update.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,23 @@ test_submodule_switch_recursing_with_args () {
848848
test_submodule_content sub1 origin/add_sub1
849849
)
850850
'
851+
test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
852+
prolog &&
853+
reset_work_tree_to_interested add_sub1 &&
854+
(
855+
cd submodule_update &&
856+
git -C sub1 checkout -b keep_branch &&
857+
git -C sub1 rev-parse HEAD >expect &&
858+
git branch -t check-keep origin/modify_sub1 &&
859+
$command check-keep &&
860+
test_superproject_content origin/modify_sub1 &&
861+
test_submodule_content sub1 origin/modify_sub1 &&
862+
git -C sub1 rev-parse keep_branch >actual &&
863+
test_cmp expect actual &&
864+
test_must_fail git -C sub1 symbolic-ref HEAD
865+
)
866+
'
867+
851868
# Replacing a tracked file with a submodule produces a checked out submodule
852869
test_expect_success "$command: replace tracked file with submodule checks out submodule" '
853870
prolog &&

0 commit comments

Comments
 (0)