Skip to content

Commit c75e31e

Browse files
committed
Merge branch 'jk/submodule-remote-lookup-cleanup' into seen
Updating submodules from the upstream did not work well when submodule's HEAD is detached, which has been improved. Comments? * 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 083fedc + cea257d commit c75e31e

File tree

8 files changed

+225
-130
lines changed

8 files changed

+225
-130
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
@@ -43,61 +43,9 @@
4343
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
4444
void *cb_data);
4545

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-
9246
static char *get_default_remote(void)
9347
{
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));
10149
}
10250

10351
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
12472
return resolved_url;
12573
}
12674

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+
127115
/* the result should be freed by the caller. */
128116
static char *get_submodule_displaypath(const char *path, const char *prefix,
129117
const char *super_prefix)
@@ -440,18 +428,6 @@ static int module_foreach(int argc, const char **argv, const char *prefix,
440428
return ret;
441429
}
442430

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-
455431
struct init_cb {
456432
const char *prefix;
457433
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)