@@ -1214,6 +1214,22 @@ static int module_summary(int argc, const char **argv, const char *prefix,
12141214 return ret ;
12151215}
12161216
1217+ static int module_gitdir (int argc , const char * * argv , const char * prefix UNUSED ,
1218+ struct repository * repo UNUSED )
1219+ {
1220+ struct strbuf gitdir = STRBUF_INIT ;
1221+
1222+ if (argc != 2 )
1223+ usage (_ ("git submodule--helper gitdir <name>" ));
1224+
1225+ submodule_name_to_gitdir (& gitdir , the_repository , argv [1 ]);
1226+
1227+ printf ("%s\n" , gitdir .buf );
1228+
1229+ strbuf_release (& gitdir );
1230+ return 0 ;
1231+ }
1232+
12171233struct sync_cb {
12181234 const char * prefix ;
12191235 const char * super_prefix ;
@@ -1709,10 +1725,6 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17091725 clone_data_path = to_free = xstrfmt ("%s/%s" , repo_get_work_tree (the_repository ),
17101726 clone_data -> path );
17111727
1712- if (validate_submodule_git_dir (sm_gitdir , clone_data -> name ) < 0 )
1713- die (_ ("refusing to create/use '%s' in another submodule's "
1714- "git dir" ), sm_gitdir );
1715-
17161728 if (!file_exists (sm_gitdir )) {
17171729 if (clone_data -> require_init && !stat (clone_data_path , & st ) &&
17181730 !is_empty_dir (clone_data_path ))
@@ -1786,23 +1798,6 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17861798 free (path );
17871799 }
17881800
1789- /*
1790- * We already performed this check at the beginning of this function,
1791- * before cloning the objects. This tries to detect racy behavior e.g.
1792- * in parallel clones, where another process could easily have made the
1793- * gitdir nested _after_ it was created.
1794- *
1795- * To prevent further harm coming from this unintentionally-nested
1796- * gitdir, let's disable it by deleting the `HEAD` file.
1797- */
1798- if (validate_submodule_git_dir (sm_gitdir , clone_data -> name ) < 0 ) {
1799- char * head = xstrfmt ("%s/HEAD" , sm_gitdir );
1800- unlink (head );
1801- free (head );
1802- die (_ ("refusing to create/use '%s' in another submodule's "
1803- "git dir" ), sm_gitdir );
1804- }
1805-
18061801 connect_work_tree_and_git_dir (clone_data_path , sm_gitdir , 0 );
18071802
18081803 p = repo_submodule_path (the_repository , clone_data_path , "config" );
@@ -3213,10 +3208,11 @@ static int add_submodule(const struct add_data *add_data)
32133208 free (submod_gitdir_path );
32143209 } else {
32153210 struct child_process cp = CHILD_PROCESS_INIT ;
3211+ struct strbuf submod_gitdir = STRBUF_INIT ;
32163212
3217- submod_gitdir_path = xstrfmt ( ".git/modules/%s" , add_data -> sm_name );
3213+ submodule_name_to_gitdir ( & submod_gitdir , the_repository , add_data -> sm_name );
32183214
3219- if (is_directory (submod_gitdir_path )) {
3215+ if (is_directory (submod_gitdir . buf )) {
32203216 if (!add_data -> force ) {
32213217 struct strbuf msg = STRBUF_INIT ;
32223218 char * die_msg ;
@@ -3225,8 +3221,8 @@ static int add_submodule(const struct add_data *add_data)
32253221 "locally with remote(s):\n" ),
32263222 add_data -> sm_name );
32273223
3228- append_fetch_remotes (& msg , submod_gitdir_path );
3229- free ( submod_gitdir_path );
3224+ append_fetch_remotes (& msg , submod_gitdir . buf );
3225+ strbuf_release ( & submod_gitdir );
32303226
32313227 strbuf_addf (& msg , _ ("If you want to reuse this local git "
32323228 "directory instead of cloning again from\n"
@@ -3244,7 +3240,7 @@ static int add_submodule(const struct add_data *add_data)
32443240 "submodule '%s'\n" ), add_data -> sm_name );
32453241 }
32463242 }
3247- free ( submod_gitdir_path );
3243+ strbuf_release ( & submod_gitdir );
32483244
32493245 clone_data .prefix = add_data -> prefix ;
32503246 clone_data .path = add_data -> sm_path ;
@@ -3596,6 +3592,7 @@ int cmd_submodule__helper(int argc,
35963592 NULL
35973593 };
35983594 struct option options [] = {
3595+ OPT_SUBCOMMAND ("gitdir" , & fn , module_gitdir ),
35993596 OPT_SUBCOMMAND ("clone" , & fn , module_clone ),
36003597 OPT_SUBCOMMAND ("add" , & fn , module_add ),
36013598 OPT_SUBCOMMAND ("update" , & fn , module_update ),
0 commit comments