submodule--helper: introduce get_submodule_displaypath()

Introduce function get_submodule_displaypath() to replace the code occurring in submodule_init() for generating displaypath of the submodule with a call to it. This new function will also be used in other parts of the system in later patches. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Stefan Beller <sbeller@google.com> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Prathamesh Chavan committed Sep 29, 2017 at 15:14 UTC 74a10642aa88c52ebb169489ea212a592d7bf3ce
1 file changed +23 -12
builtin/submodule--helper.c
+23 -12
@@ -220,6 +220,26 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr
220 return 0;
221 }
222
223 +/* the result should be freed by the caller. */
224 +static char *get_submodule_displaypath(const char *path, const char *prefix)
225 +{
226 + const char *super_prefix = get_super_prefix();
227 +
228 + if (prefix && super_prefix) {
229 + BUG("cannot have prefix '%s' and superprefix '%s'",
230 + prefix, super_prefix);
231 + } else if (prefix) {
232 + struct strbuf sb = STRBUF_INIT;
233 + char *displaypath = xstrdup(relative_path(path, prefix, &sb));
234 + strbuf_release(&sb);
235 + return displaypath;
236 + } else if (super_prefix) {
237 + return xstrfmt("%s%s", super_prefix, path);
238 + } else {
239 + return xstrdup(path);
240 + }
241 +}
242 +
243 struct module_list {
244 const struct cache_entry **entries;
245 int alloc, nr;
@@ -335,15 +355,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
355 struct strbuf sb = STRBUF_INIT;
356 char *upd = NULL, *url = NULL, *displaypath;
357
338 - if (prefix && get_super_prefix())
339 - die("BUG: cannot have prefix and superprefix");
340 - else if (prefix)
341 - displaypath = xstrdup(relative_path(path, prefix, &sb));
342 - else if (get_super_prefix()) {
343 - strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
344 - displaypath = strbuf_detach(&sb, NULL);
345 - } else
346 - displaypath = xstrdup(path);
358 + displaypath = get_submodule_displaypath(path, prefix);
359
360 sub = submodule_from_path(&null_oid, path);
361
@@ -358,9 +370,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
370 * Set active flag for the submodule being initialized
371 */
372 if (!is_submodule_active(the_repository, path)) {
361 - strbuf_reset(&sb);
373 strbuf_addf(&sb, "submodule.%s.active", sub->name);
374 git_config_set_gently(sb.buf, "true");
375 + strbuf_reset(&sb);
376 }
377
378 /*
@@ -368,7 +380,6 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
380 * To look up the url in .git/config, we must not fall back to
381 * .gitmodules, so look it up directly.
382 */
371 - strbuf_reset(&sb);
383 strbuf_addf(&sb, "submodule.%s.url", sub->name);
384 if (git_config_get_string(sb.buf, &url)) {
385 if (!sub->url)
@@ -405,9 +416,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
416 _("Submodule '%s' (%s) registered for path '%s'\n"),
417 sub->name, url, displaypath);
418 }
419 + strbuf_reset(&sb);
420
421 /* Copy "update" setting when it is not set yet */
410 - strbuf_reset(&sb);
422 strbuf_addf(&sb, "submodule.%s.update", sub->name);
423 if (git_config_get_string(sb.buf, &upd) &&
424 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {