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>

Atharva Raykar committed Aug 10, 2021 at 17:16 UTC ab6f23b75129bc20681c0941cdd01d2694bc54f4
1 file changed +25 -36
builtin/submodule--helper.c
+25 -36
@@ -199,33 +199,46 @@ static char *relative_url(const char *remote_url,
199 return strbuf_detach(&sb, NULL);
200 }
201
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)
203 {
204 - char *remoteurl = NULL;
204 + char *remoteurl, *resolved_url;
205 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 +{
228 const char *up_path = NULL;
229 char *res;
230 const char *url;
209 - struct strbuf sb = STRBUF_INIT;
231
232 if (argc != 2 && argc != 3)
233 die("resolve-relative-url only accepts one or two arguments");
234
235 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 -
236 if (argc == 3)
237 up_path = argv[2];
238
225 - res = relative_url(remoteurl, url, up_path);
239 + res = compute_submodule_clone_url(url, up_path, 1);
240 puts(res);
241 free(res);
228 - free(remoteurl);
242 return 0;
243 }
244
@@ -590,30 +603,6 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
603 return 0;
604 }
605
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 -
606 struct init_cb {
607 const char *prefix;
608 unsigned int flags;