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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jul 24, 2017 at 10:36 UTC 3ef253803211f5ec38c88635bfd50f91ccd98e16
2 files changed +19 -1
submodule.c
+2 -1
@@ -1641,7 +1641,8 @@ int submodule_move_head(const char *path,
1641 cp.dir = path;
1642
1643 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);
1646
1647 if (run_command(&cp)) {
1648 ret = -1;
t/lib-submodule-update.sh
+17
@@ -848,6 +848,23 @@ test_submodule_switch_recursing_with_args () {
848 test_submodule_content sub1 origin/add_sub1
849 )
850 '
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 +
868 # Replacing a tracked file with a submodule produces a checked out submodule
869 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
870 prolog &&