@@ -1204,6 +1204,22 @@ static int module_summary(int argc, const char **argv, const char *prefix,
12041204 return ret ;
12051205}
12061206
1207+ static int module_gitdir (int argc , const char * * argv , const char * prefix UNUSED ,
1208+ struct repository * repo )
1209+ {
1210+ struct strbuf gitdir = STRBUF_INIT ;
1211+
1212+ if (argc != 2 )
1213+ usage (_ ("git submodule--helper gitdir <name>" ));
1214+
1215+ submodule_name_to_gitdir (& gitdir , repo , argv [1 ]);
1216+
1217+ printf ("%s\n" , gitdir .buf );
1218+
1219+ strbuf_release (& gitdir );
1220+ return 0 ;
1221+ }
1222+
12071223struct sync_cb {
12081224 const char * prefix ;
12091225 const char * super_prefix ;
@@ -1699,10 +1715,6 @@ static int clone_submodule(const struct module_clone_data *clone_data,
16991715 clone_data_path = to_free = xstrfmt ("%s/%s" , repo_get_work_tree (the_repository ),
17001716 clone_data -> path );
17011717
1702- if (validate_submodule_git_dir (sm_gitdir , clone_data -> name ) < 0 )
1703- die (_ ("refusing to create/use '%s' in another submodule's "
1704- "git dir" ), sm_gitdir );
1705-
17061718 if (!file_exists (sm_gitdir )) {
17071719 if (clone_data -> require_init && !stat (clone_data_path , & st ) &&
17081720 !is_empty_dir (clone_data_path ))
@@ -1776,23 +1788,6 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17761788 free (path );
17771789 }
17781790
1779- /*
1780- * We already performed this check at the beginning of this function,
1781- * before cloning the objects. This tries to detect racy behavior e.g.
1782- * in parallel clones, where another process could easily have made the
1783- * gitdir nested _after_ it was created.
1784- *
1785- * To prevent further harm coming from this unintentionally-nested
1786- * gitdir, let's disable it by deleting the `HEAD` file.
1787- */
1788- if (validate_submodule_git_dir (sm_gitdir , clone_data -> name ) < 0 ) {
1789- char * head = xstrfmt ("%s/HEAD" , sm_gitdir );
1790- unlink (head );
1791- free (head );
1792- die (_ ("refusing to create/use '%s' in another submodule's "
1793- "git dir" ), sm_gitdir );
1794- }
1795-
17961791 connect_work_tree_and_git_dir (clone_data_path , sm_gitdir , 0 );
17971792
17981793 p = repo_submodule_path (the_repository , clone_data_path , "config" );
@@ -3190,13 +3185,13 @@ static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
31903185
31913186static int add_submodule (const struct add_data * add_data )
31923187{
3193- char * submod_gitdir_path ;
31943188 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT ;
31953189 struct string_list reference = STRING_LIST_INIT_NODUP ;
31963190 int ret = -1 ;
31973191
31983192 /* perhaps the path already exists and is already a git repo, else clone it */
31993193 if (is_directory (add_data -> sm_path )) {
3194+ char * submod_gitdir_path ;
32003195 struct strbuf sm_path = STRBUF_INIT ;
32013196 strbuf_addstr (& sm_path , add_data -> sm_path );
32023197 submod_gitdir_path = xstrfmt ("%s/.git" , add_data -> sm_path );
@@ -3210,10 +3205,11 @@ static int add_submodule(const struct add_data *add_data)
32103205 free (submod_gitdir_path );
32113206 } else {
32123207 struct child_process cp = CHILD_PROCESS_INIT ;
3208+ struct strbuf submod_gitdir = STRBUF_INIT ;
32133209
3214- submod_gitdir_path = xstrfmt ( ".git/modules/%s" , add_data -> sm_name );
3210+ submodule_name_to_gitdir ( & submod_gitdir , the_repository , add_data -> sm_name );
32153211
3216- if (is_directory (submod_gitdir_path )) {
3212+ if (is_directory (submod_gitdir . buf )) {
32173213 if (!add_data -> force ) {
32183214 struct strbuf msg = STRBUF_INIT ;
32193215 char * die_msg ;
@@ -3222,8 +3218,8 @@ static int add_submodule(const struct add_data *add_data)
32223218 "locally with remote(s):\n" ),
32233219 add_data -> sm_name );
32243220
3225- append_fetch_remotes (& msg , submod_gitdir_path );
3226- free ( submod_gitdir_path );
3221+ append_fetch_remotes (& msg , submod_gitdir . buf );
3222+ strbuf_release ( & submod_gitdir );
32273223
32283224 strbuf_addf (& msg , _ ("If you want to reuse this local git "
32293225 "directory instead of cloning again from\n"
@@ -3241,7 +3237,7 @@ static int add_submodule(const struct add_data *add_data)
32413237 "submodule '%s'\n" ), add_data -> sm_name );
32423238 }
32433239 }
3244- free ( submod_gitdir_path );
3240+ strbuf_release ( & submod_gitdir );
32453241
32463242 clone_data .prefix = add_data -> prefix ;
32473243 clone_data .path = add_data -> sm_path ;
@@ -3594,6 +3590,7 @@ int cmd_submodule__helper(int argc,
35943590 NULL
35953591 };
35963592 struct option options [] = {
3593+ OPT_SUBCOMMAND ("gitdir" , & fn , module_gitdir ),
35973594 OPT_SUBCOMMAND ("clone" , & fn , module_clone ),
35983595 OPT_SUBCOMMAND ("add" , & fn , module_add ),
35993596 OPT_SUBCOMMAND ("update" , & fn , module_update ),
0 commit comments