On Thu, Nov 17, 2016 at 2:42 PM, Stefan Beller <sbeller@xxxxxxxxxx> wrote: > > I think I'll just write this functionality in C and optionally expose > it via the submodule--helper, > such that the user facing git-submodule.sh only has to call that helper. I think it will roughly look like this: (white space mangled) commit e72ef244c667920c874247aa32aa55845500aac8 Author: Stefan Beller <sbeller@xxxxxxxxxx> Date: Thu Nov 17 16:14:46 2016 -0800 submodule--helper: add intern-git-dir function When a submodule has its git dir inside the working dir, the submodule support for checkout that we plan to add in a later patch will fail. Add functionality to migrate the git directory to be embedded into the superprojects git directory. Signed-off-by: Stefan Beller <sbeller@xxxxxxxxxx> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 4beeda5..4f31100 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1076,6 +1076,21 @@ static int resolve_remote_submodule_branch(int argc, const char **argv, return 0; } +static int intern_git_dir(int argc, const char **argv, const char *prefix) +{ + int i; + struct pathspec pathspec; + struct module_list list = MODULE_LIST_INIT; + + if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) + return 1; + + for (i = 0; i < list.nr; i++) + migrate_submodule_gitdir(list.entries[i]->name); + + return 0; +} + struct cmd_struct { const char *cmd; int (*fn)(int, const char **, const char *); @@ -1090,7 +1105,8 @@ static struct cmd_struct commands[] = { {"resolve-relative-url", resolve_relative_url}, {"resolve-relative-url-test", resolve_relative_url_test}, {"init", module_init}, - {"remote-branch", resolve_remote_submodule_branch} + {"remote-branch", resolve_remote_submodule_branch}, + {"intern-git-dir", intern_git_dir} }; int cmd_submodule__helper(int argc, const char **argv, const char *prefix) diff --git a/submodule.c b/submodule.c index 45b9060..e513bba 100644 --- a/submodule.c +++ b/submodule.c @@ -1335,3 +1335,42 @@ void prepare_submodule_repo_env(struct argv_array *out) } argv_array_push(out, "GIT_DIR=.git"); } + +/* + * Migrate the given submodule (and all its submodules recursively) from + * having its git directory within the working tree to the git dir nested + * in its superprojects git dir under modules/. + */ +void migrate_submodule_gitdir(const char *path) +{ + char *old_git_dir; + const char *new_git_dir; + const struct submodule *sub; + + struct child_process cp = CHILD_PROCESS_INIT; + cp.git_cmd = 1; + cp.no_stdin = 1; + cp.dir = path; + argv_array_pushl(&cp.args, "submodule", "foreach", "--recursive", + "git", "submodule--helper" "intern-git-dir", NULL); + + if (run_command(&cp)) + die(_("Could not migrate git directory in submodule '%s'"), + path); + + old_git_dir = xstrfmt("%s/.git", path); + if (read_gitfile(old_git_dir)) + /* If it is an actual gitfile, it doesn't need migration. */ + goto out; + + sub = submodule_from_path(null_sha1, path); + new_git_dir = git_common_path("modules/%s", sub->name); + + if (rename(old_git_dir, new_git_dir) < 0) + die_errno(_("Could not migrate git directory from %s to %s"), + old_git_dir, new_git_dir); + + connect_work_tree_and_git_dir(path, new_git_dir); +out: + free(old_git_dir); +} diff --git a/submodule.h b/submodule.h index aac202c..143ec18 100644 --- a/submodule.h +++ b/submodule.h @@ -90,5 +90,6 @@ extern int parallel_submodules(void); * retaining any config in the environment. */ extern void prepare_submodule_repo_env(struct argv_array *out); +extern void migrate_submodule_gitdir(const char *path); #endif