Skip to content

Commit 74a1064

Browse files
pratham-pcgitster
authored andcommitted
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 <[email protected]> Mentored-by: Stefan Beller <[email protected]> Signed-off-by: Prathamesh Chavan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 59c0ea1 commit 74a1064

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

builtin/submodule--helper.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr
220220
return 0;
221221
}
222222

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+
223243
struct module_list {
224244
const struct cache_entry **entries;
225245
int alloc, nr;
@@ -335,15 +355,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
335355
struct strbuf sb = STRBUF_INIT;
336356
char *upd = NULL, *url = NULL, *displaypath;
337357

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);
347359

348360
sub = submodule_from_path(&null_oid, path);
349361

@@ -358,17 +370,16 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
358370
* Set active flag for the submodule being initialized
359371
*/
360372
if (!is_submodule_active(the_repository, path)) {
361-
strbuf_reset(&sb);
362373
strbuf_addf(&sb, "submodule.%s.active", sub->name);
363374
git_config_set_gently(sb.buf, "true");
375+
strbuf_reset(&sb);
364376
}
365377

366378
/*
367379
* Copy url setting when it is not set yet.
368380
* To look up the url in .git/config, we must not fall back to
369381
* .gitmodules, so look it up directly.
370382
*/
371-
strbuf_reset(&sb);
372383
strbuf_addf(&sb, "submodule.%s.url", sub->name);
373384
if (git_config_get_string(sb.buf, &url)) {
374385
if (!sub->url)
@@ -405,9 +416,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
405416
_("Submodule '%s' (%s) registered for path '%s'\n"),
406417
sub->name, url, displaypath);
407418
}
419+
strbuf_reset(&sb);
408420

409421
/* Copy "update" setting when it is not set yet */
410-
strbuf_reset(&sb);
411422
strbuf_addf(&sb, "submodule.%s.update", sub->name);
412423
if (git_config_get_string(sb.buf, &upd) &&
413424
sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {

0 commit comments

Comments
 (0)