4141typedef void (* each_submodule_fn )(const struct cache_entry * list_item ,
4242 void * cb_data );
4343
44- static int repo_get_default_remote (struct repository * repo , char * * default_remote )
45- {
46- char * dest = NULL ;
47- struct strbuf sb = STRBUF_INIT ;
48- struct ref_store * store = get_main_ref_store (repo );
49- const char * refname = refs_resolve_ref_unsafe (store , "HEAD" , 0 , NULL ,
50- NULL );
51-
52- if (!refname )
53- return die_message (_ ("No such ref: %s" ), "HEAD" );
54-
55- /* detached HEAD */
56- if (!strcmp (refname , "HEAD" )) {
57- * default_remote = xstrdup ("origin" );
58- return 0 ;
59- }
60-
61- if (!skip_prefix (refname , "refs/heads/" , & refname ))
62- return die_message (_ ("Expecting a full ref name, got %s" ),
63- refname );
64-
65- strbuf_addf (& sb , "branch.%s.remote" , refname );
66- if (repo_config_get_string (repo , sb .buf , & dest ))
67- * default_remote = xstrdup ("origin" );
68- else
69- * default_remote = dest ;
70-
71- strbuf_release (& sb );
72- return 0 ;
73- }
74-
75- static int get_default_remote_submodule (const char * module_path , char * * default_remote )
76- {
77- struct repository subrepo ;
78- int ret ;
79-
80- if (repo_submodule_init (& subrepo , the_repository , module_path ,
81- null_oid (the_hash_algo )) < 0 )
82- return die_message (_ ("could not get a repository handle for submodule '%s'" ),
83- module_path );
84- ret = repo_get_default_remote (& subrepo , default_remote );
85- repo_clear (& subrepo );
86-
87- return ret ;
88- }
89-
9044static char * get_default_remote (void )
9145{
92- char * default_remote ;
93- int code = repo_get_default_remote (the_repository , & default_remote );
94-
95- if (code )
96- exit (code );
97-
98- return default_remote ;
46+ return xstrdup (repo_default_remote (the_repository ));
9947}
10048
10149static char * resolve_relative_url (const char * rel_url , const char * up_path , int quiet )
@@ -122,6 +70,46 @@ static char *resolve_relative_url(const char *rel_url, const char *up_path, int
12270 return resolved_url ;
12371}
12472
73+ static int get_default_remote_submodule (const char * module_path , char * * default_remote )
74+ {
75+ const struct submodule * sub ;
76+ struct repository subrepo ;
77+ const char * remote_name = NULL ;
78+ char * url = NULL ;
79+
80+ sub = submodule_from_path (the_repository , null_oid (the_hash_algo ), module_path );
81+ if (sub && sub -> url ) {
82+ url = xstrdup (sub -> url );
83+
84+ /* Possibly a url relative to parent */
85+ if (starts_with_dot_dot_slash (url ) ||
86+ starts_with_dot_slash (url )) {
87+ char * oldurl = url ;
88+
89+ url = resolve_relative_url (oldurl , NULL , 1 );
90+ free (oldurl );
91+ }
92+ }
93+
94+ if (repo_submodule_init (& subrepo , the_repository , module_path ,
95+ null_oid (the_hash_algo )) < 0 )
96+ return die_message (_ ("could not get a repository handle for submodule '%s'" ),
97+ module_path );
98+
99+ /* Look up by URL first */
100+ if (url )
101+ remote_name = repo_remote_from_url (& subrepo , url );
102+ if (!remote_name )
103+ remote_name = repo_default_remote (& subrepo );
104+
105+ * default_remote = xstrdup (remote_name );
106+
107+ repo_clear (& subrepo );
108+ free (url );
109+
110+ return 0 ;
111+ }
112+
125113/* the result should be freed by the caller. */
126114static char * get_submodule_displaypath (const char * path , const char * prefix ,
127115 const char * super_prefix )
@@ -438,18 +426,6 @@ static int module_foreach(int argc, const char **argv, const char *prefix,
438426 return ret ;
439427}
440428
441- static int starts_with_dot_slash (const char * const path )
442- {
443- return path_match_flags (path , PATH_MATCH_STARTS_WITH_DOT_SLASH |
444- PATH_MATCH_XPLATFORM );
445- }
446-
447- static int starts_with_dot_dot_slash (const char * const path )
448- {
449- return path_match_flags (path , PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
450- PATH_MATCH_XPLATFORM );
451- }
452-
453429struct init_cb {
454430 const char * prefix ;
455431 const char * super_prefix ;
0 commit comments