Merge branch 'sb/reset-recurse-submodules'
[gitweb.git] / submodule.c
index 7eaa3d384e91b13f8380878aaf82e0c0d2caf4ab..54825100b29bd6bb9614bd2ab2431c6d418945b0 100644 (file)
@@ -1402,6 +1402,23 @@ int submodule_move_head(const char *path,
        int ret = 0;
        struct child_process cp = CHILD_PROCESS_INIT;
        const struct submodule *sub;
+       int *error_code_ptr, error_code;
+
+       if (!is_submodule_initialized(path))
+               return 0;
+
+       if (flags & SUBMODULE_MOVE_HEAD_FORCE)
+               /*
+                * Pass non NULL pointer to is_submodule_populated_gently
+                * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
+                * to fixup the submodule in the force case later.
+                */
+               error_code_ptr = &error_code;
+       else
+               error_code_ptr = NULL;
+
+       if (old && !is_submodule_populated_gently(path, error_code_ptr))
+               return 0;
 
        sub = submodule_from_path(null_sha1, path);
 
@@ -1420,15 +1437,21 @@ int submodule_move_head(const char *path,
                                absorb_git_dir_into_superproject("", path,
                                        ABSORB_GITDIR_RECURSE_SUBMODULES);
                } else {
-                       struct strbuf sb = STRBUF_INIT;
-                       strbuf_addf(&sb, "%s/modules/%s",
+                       char *gitdir = xstrfmt("%s/modules/%s",
                                    get_git_common_dir(), sub->name);
-                       connect_work_tree_and_git_dir(path, sb.buf);
-                       strbuf_release(&sb);
+                       connect_work_tree_and_git_dir(path, gitdir);
+                       free(gitdir);
 
                        /* make sure the index is clean as well */
                        submodule_reset_index(path);
                }
+
+               if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
+                       char *gitdir = xstrfmt("%s/modules/%s",
+                                   get_git_common_dir(), sub->name);
+                       connect_work_tree_and_git_dir(path, gitdir);
+                       free(gitdir);
+               }
        }
 
        prepare_submodule_repo_env_no_git_dir(&cp.env_array);