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