Skip to content

Commit

Permalink
Merge branch 'sb/submodule-url-to-absolute'
Browse files Browse the repository at this point in the history
Some codepaths failed to form a proper URL when .gitmodules record
the URL to a submodule repository as relative to the repository of
superproject, which has been corrected.

* sb/submodule-url-to-absolute:
  submodule helper: convert relative URL to absolute URL if needed
  • Loading branch information
gitster committed Nov 6, 2018
2 parents ea100b6 + e0a862f commit 3fc8522
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
51 changes: 34 additions & 17 deletions builtin/submodule--helper.c
Expand Up @@ -584,6 +584,26 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
return 0;
}

static char *compute_submodule_clone_url(const char *rel_url)
{
char *remoteurl, *relurl;
char *remote = get_default_remote();
struct strbuf remotesb = STRBUF_INIT;

strbuf_addf(&remotesb, "remote.%s.url", remote);
if (git_config_get_string(remotesb.buf, &remoteurl)) {
warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
remoteurl = xgetcwd();
}
relurl = relative_url(remoteurl, rel_url, NULL);

free(remote);
free(remoteurl);
strbuf_release(&remotesb);

return relurl;
}

struct init_cb {
const char *prefix;
unsigned int flags;
Expand Down Expand Up @@ -634,21 +654,9 @@ static void init_submodule(const char *path, const char *prefix,
/* Possibly a url relative to parent */
if (starts_with_dot_dot_slash(url) ||
starts_with_dot_slash(url)) {
char *remoteurl, *relurl;
char *remote = get_default_remote();
struct strbuf remotesb = STRBUF_INIT;
strbuf_addf(&remotesb, "remote.%s.url", remote);
free(remote);

if (git_config_get_string(remotesb.buf, &remoteurl)) {
warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
remoteurl = xgetcwd();
}
relurl = relative_url(remoteurl, url, NULL);
strbuf_release(&remotesb);
free(remoteurl);
free(url);
url = relurl;
char *oldurl = url;
url = compute_submodule_clone_url(oldurl);
free(oldurl);
}

if (git_config_set_gently(sb.buf, url))
Expand Down Expand Up @@ -1582,6 +1590,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
struct strbuf sb = STRBUF_INIT;
const char *displaypath = NULL;
int needs_cloning = 0;
int need_free_url = 0;

if (ce_stage(ce)) {
if (suc->recursive_prefix)
Expand Down Expand Up @@ -1630,8 +1639,14 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,

strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
if (repo_config_get_string_const(the_repository, sb.buf, &url))
url = sub->url;
if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
if (starts_with_dot_slash(sub->url) ||
starts_with_dot_dot_slash(sub->url)) {
url = compute_submodule_clone_url(sub->url);
need_free_url = 1;
} else
url = sub->url;
}

strbuf_reset(&sb);
strbuf_addf(&sb, "%s/.git", ce->name);
Expand Down Expand Up @@ -1677,6 +1692,8 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
cleanup:
strbuf_reset(&displaypath_sb);
strbuf_reset(&sb);
if (need_free_url)
free((void*)url);

return needs_cloning;
}
Expand Down
24 changes: 24 additions & 0 deletions t/t7400-submodule-basic.sh
Expand Up @@ -1224,6 +1224,30 @@ test_expect_success 'submodule update and setting submodule.<name>.active' '
test_cmp expect actual
'

test_expect_success 'clone active submodule without submodule url set' '
test_when_finished "rm -rf test/test" &&
mkdir test &&
# another dir breaks accidental relative paths still being correct
git clone file://"$pwd"/multisuper test/test &&
(
cd test/test &&
git config submodule.active "." &&
# do not pass --init flag, as the submodule is already active:
git submodule update &&
git submodule status >actual_raw &&
cut -c 1,43- actual_raw >actual &&
cat >expect <<-\EOF &&
sub0 (test2)
sub1 (test2)
sub2 (test2)
sub3 (test2)
EOF
test_cmp expect actual
)
'

test_expect_success 'clone --recurse-submodules with a pathspec works' '
test_when_finished "rm -rf multisuper_clone" &&
cat >expected <<-\EOF &&
Expand Down

0 comments on commit 3fc8522

Please sign in to comment.