summaryrefslogtreecommitdiff
path: root/builtin/submodule--helper.c
diff options
context:
space:
mode:
authorLibravatar Atharva Raykar <raykar.ath@gmail.com>2021-08-10 17:16:34 +0530
committerLibravatar Junio C Hamano <gitster@pobox.com>2021-08-10 11:45:11 -0700
commitab6f23b75129bc20681c0941cdd01d2694bc54f4 (patch)
tree23a93ee459f04a476d1e8deba36950a5cc64ca58 /builtin/submodule--helper.c
parentsubmodule--helper: add options for compute_submodule_clone_url() (diff)
downloadtgif-ab6f23b75129bc20681c0941cdd01d2694bc54f4.tar.xz
submodule--helper: refactor resolve_relative_url() helper
Refactor the helper function to resolve a relative url, by reusing the existing `compute_submodule_clone_url()` function. `compute_submodule_clone_url()` performs the same work that `resolve_relative_url()` is doing, so we eliminate this code repetition by moving the former function's definition up, and calling it inside `resolve_relative_url()`. Signed-off-by: Atharva Raykar <raykar.ath@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Shourya Shukla <periperidip@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/submodule--helper.c')
-rw-r--r--builtin/submodule--helper.c61
1 files changed, 25 insertions, 36 deletions
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index c9a4862a77..75179fca89 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -199,33 +199,46 @@ static char *relative_url(const char *remote_url,
return strbuf_detach(&sb, NULL);
}
-static int resolve_relative_url(int argc, const char **argv, const char *prefix)
+static char *compute_submodule_clone_url(const char *rel_url, const char *up_path, int quiet)
{
- char *remoteurl = NULL;
+ char *remoteurl, *resolved_url;
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)) {
+ if (!quiet)
+ warning(_("could not look up configuration '%s'. "
+ "Assuming this repository is its own "
+ "authoritative upstream."),
+ remotesb.buf);
+ remoteurl = xgetcwd();
+ }
+ resolved_url = relative_url(remoteurl, rel_url, up_path);
+
+ free(remote);
+ free(remoteurl);
+ strbuf_release(&remotesb);
+
+ return resolved_url;
+}
+
+static int resolve_relative_url(int argc, const char **argv, const char *prefix)
+{
const char *up_path = NULL;
char *res;
const char *url;
- struct strbuf sb = STRBUF_INIT;
if (argc != 2 && argc != 3)
die("resolve-relative-url only accepts one or two arguments");
url = argv[1];
- strbuf_addf(&sb, "remote.%s.url", remote);
- free(remote);
-
- if (git_config_get_string(sb.buf, &remoteurl))
- /* the repository is its own authoritative upstream */
- remoteurl = xgetcwd();
-
if (argc == 3)
up_path = argv[2];
- res = relative_url(remoteurl, url, up_path);
+ res = compute_submodule_clone_url(url, up_path, 1);
puts(res);
free(res);
- free(remoteurl);
return 0;
}
@@ -590,30 +603,6 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
return 0;
}
-static char *compute_submodule_clone_url(const char *rel_url, const char *up_path, int quiet)
-{
- char *remoteurl, *resolved_url;
- 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)) {
- if (!quiet)
- warning(_("could not look up configuration '%s'. "
- "Assuming this repository is its own "
- "authoritative upstream."),
- remotesb.buf);
- remoteurl = xgetcwd();
- }
- resolved_url = relative_url(remoteurl, rel_url, up_path);
-
- free(remote);
- free(remoteurl);
- strbuf_release(&remotesb);
-
- return resolved_url;
-}
-
struct init_cb {
const char *prefix;
unsigned int flags;