Skip to content

Commit

Permalink
submodule--helper: introduce get_submodule_displaypath()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pratham-pc authored and gitster committed Oct 2, 2017
1 parent 59c0ea1 commit 74a1064
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions builtin/submodule--helper.c
Expand Up @@ -220,6 +220,26 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr
return 0;
}

/* the result should be freed by the caller. */
static char *get_submodule_displaypath(const char *path, const char *prefix)
{
const char *super_prefix = get_super_prefix();

if (prefix && super_prefix) {
BUG("cannot have prefix '%s' and superprefix '%s'",
prefix, super_prefix);
} else if (prefix) {
struct strbuf sb = STRBUF_INIT;
char *displaypath = xstrdup(relative_path(path, prefix, &sb));
strbuf_release(&sb);
return displaypath;
} else if (super_prefix) {
return xstrfmt("%s%s", super_prefix, path);
} else {
return xstrdup(path);
}
}

struct module_list {
const struct cache_entry **entries;
int alloc, nr;
Expand Down Expand Up @@ -335,15 +355,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
struct strbuf sb = STRBUF_INIT;
char *upd = NULL, *url = NULL, *displaypath;

if (prefix && get_super_prefix())
die("BUG: cannot have prefix and superprefix");
else if (prefix)
displaypath = xstrdup(relative_path(path, prefix, &sb));
else if (get_super_prefix()) {
strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
displaypath = strbuf_detach(&sb, NULL);
} else
displaypath = xstrdup(path);
displaypath = get_submodule_displaypath(path, prefix);

sub = submodule_from_path(&null_oid, path);

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

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

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

0 comments on commit 74a1064

Please sign in to comment.