Skip to content

Commit ab6f23b

Browse files
tfidfwastakengitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Shourya Shukla <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6baf4e4 commit ab6f23b

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

builtin/submodule--helper.c

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -199,33 +199,46 @@ static char *relative_url(const char *remote_url,
199199
return strbuf_detach(&sb, NULL);
200200
}
201201

202-
static int resolve_relative_url(int argc, const char **argv, const char *prefix)
202+
static char *compute_submodule_clone_url(const char *rel_url, const char *up_path, int quiet)
203203
{
204-
char *remoteurl = NULL;
204+
char *remoteurl, *resolved_url;
205205
char *remote = get_default_remote();
206+
struct strbuf remotesb = STRBUF_INIT;
207+
208+
strbuf_addf(&remotesb, "remote.%s.url", remote);
209+
if (git_config_get_string(remotesb.buf, &remoteurl)) {
210+
if (!quiet)
211+
warning(_("could not look up configuration '%s'. "
212+
"Assuming this repository is its own "
213+
"authoritative upstream."),
214+
remotesb.buf);
215+
remoteurl = xgetcwd();
216+
}
217+
resolved_url = relative_url(remoteurl, rel_url, up_path);
218+
219+
free(remote);
220+
free(remoteurl);
221+
strbuf_release(&remotesb);
222+
223+
return resolved_url;
224+
}
225+
226+
static int resolve_relative_url(int argc, const char **argv, const char *prefix)
227+
{
206228
const char *up_path = NULL;
207229
char *res;
208230
const char *url;
209-
struct strbuf sb = STRBUF_INIT;
210231

211232
if (argc != 2 && argc != 3)
212233
die("resolve-relative-url only accepts one or two arguments");
213234

214235
url = argv[1];
215-
strbuf_addf(&sb, "remote.%s.url", remote);
216-
free(remote);
217-
218-
if (git_config_get_string(sb.buf, &remoteurl))
219-
/* the repository is its own authoritative upstream */
220-
remoteurl = xgetcwd();
221-
222236
if (argc == 3)
223237
up_path = argv[2];
224238

225-
res = relative_url(remoteurl, url, up_path);
239+
res = compute_submodule_clone_url(url, up_path, 1);
226240
puts(res);
227241
free(res);
228-
free(remoteurl);
229242
return 0;
230243
}
231244

@@ -590,30 +603,6 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
590603
return 0;
591604
}
592605

593-
static char *compute_submodule_clone_url(const char *rel_url, const char *up_path, int quiet)
594-
{
595-
char *remoteurl, *resolved_url;
596-
char *remote = get_default_remote();
597-
struct strbuf remotesb = STRBUF_INIT;
598-
599-
strbuf_addf(&remotesb, "remote.%s.url", remote);
600-
if (git_config_get_string(remotesb.buf, &remoteurl)) {
601-
if (!quiet)
602-
warning(_("could not look up configuration '%s'. "
603-
"Assuming this repository is its own "
604-
"authoritative upstream."),
605-
remotesb.buf);
606-
remoteurl = xgetcwd();
607-
}
608-
resolved_url = relative_url(remoteurl, rel_url, up_path);
609-
610-
free(remote);
611-
free(remoteurl);
612-
strbuf_release(&remotesb);
613-
614-
return resolved_url;
615-
}
616-
617606
struct init_cb {
618607
const char *prefix;
619608
unsigned int flags;

0 commit comments

Comments
 (0)