Skip to content

Commit 7310e53

Browse files
committed
Merge branch 'jk/submodule-remote-lookup-cleanup'
Updating submodules from the upstream did not work well when submodule's HEAD is detached, which has been improved. * jk/submodule-remote-lookup-cleanup: submodule: look up remotes by URL first submodule: move get_default_remote_submodule() submodule--helper: improve logic for fallback remote name remote: remove the_repository from some functions dir: move starts_with_dot(_dot)_slash to dir.h remote: fix tear down of struct remote remote: remove branch->merge_name and fix branch_release()
2 parents 8b6f19c + ca62f52 commit 7310e53

File tree

8 files changed

+226
-131
lines changed

8 files changed

+226
-131
lines changed

branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
230230
return -1;
231231
}
232232

233-
if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
233+
if (branch->merge_nr < 1 || !branch->merge || !branch->merge[0] || !branch->merge[0]->src) {
234234
warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
235235
bare_ref);
236236
return -1;
237237
}
238238

239239
tracking->remote = branch->remote_name;
240240
for (i = 0; i < branch->merge_nr; i++)
241-
string_list_append(tracking->srcs, branch->merge_name[i]);
241+
string_list_append(tracking->srcs, branch->merge[i]->src);
242242
return 0;
243243
}
244244

builtin/pull.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static void NORETURN die_no_merge_candidates(const char *repo, const char **refs
490490
} else
491491
fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
492492
"from the remote, but no such ref was fetched."),
493-
*curr_branch->merge_name);
493+
curr_branch->merge[0]->src);
494494
exit(1);
495495
}
496496

builtin/submodule--helper.c

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,9 @@
4141
typedef 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-
9044
static 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

10149
static 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. */
126114
static 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-
453429
struct init_cb {
454430
const char *prefix;
455431
const char *super_prefix;

dir.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,27 @@ static inline int starts_with_dot_dot_slash_native(const char *const path)
676676
return path_match_flags(path, what | PATH_MATCH_NATIVE);
677677
}
678678

679+
/**
680+
* starts_with_dot_slash: convenience wrapper for
681+
* patch_match_flags() with PATH_MATCH_STARTS_WITH_DOT_SLASH and
682+
* PATH_MATCH_XPLATFORM.
683+
*/
684+
static inline int starts_with_dot_slash(const char *const path)
685+
{
686+
const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_SLASH;
687+
688+
return path_match_flags(path, what | PATH_MATCH_XPLATFORM);
689+
}
690+
691+
/**
692+
* starts_with_dot_dot_slash: convenience wrapper for
693+
* patch_match_flags() with PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH and
694+
* PATH_MATCH_XPLATFORM.
695+
*/
696+
static inline int starts_with_dot_dot_slash(const char *const path)
697+
{
698+
const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH;
699+
700+
return path_match_flags(path, what | PATH_MATCH_XPLATFORM);
701+
}
679702
#endif

0 commit comments

Comments
 (0)