Browse Source

Merge branch 'sb/submodule-config-cleanup'

Minor code clean-up.

* sb/submodule-config-cleanup:
  submodule-config: clarify parsing of null_sha1 element
  submodule-config: rename commit_sha1 to treeish_name
  submodule config: inline config_from_{name, path}
maint
Junio C Hamano 8 years ago
parent
commit
95d0367b2e
  1. 14
      Documentation/technical/api-submodule-config.txt
  2. 58
      submodule-config.c
  3. 4
      submodule-config.h
  4. 14
      t/t7411-submodule-config.sh

14
Documentation/technical/api-submodule-config.txt

@ -47,16 +47,20 @@ Functions
Can be passed to the config parsing infrastructure to parse Can be passed to the config parsing infrastructure to parse
local (worktree) submodule configurations. local (worktree) submodule configurations.


`const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`:: `const struct submodule *submodule_from_path(const unsigned char *treeish_name, const char *path)`::


Lookup values for one submodule by its commit_sha1 and path. Given a tree-ish in the superproject and a path, return the
submodule that is bound at the path in the named tree.


`const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const char *name)`:: `const struct submodule *submodule_from_name(const unsigned char *treeish_name, const char *name)`::


The same as above but lookup by name. The same as above but lookup by name.


If given the null_sha1 as commit_sha1 the local configuration of a Whenever a submodule configuration is parsed in `parse_submodule_config_option`
submodule will be returned (e.g. consolidated values from local git via e.g. `gitmodules_config()`, it will overwrite the null_sha1 entry.
So in the normal case, when HEAD:.gitmodules is parsed first and then overlayed
with the repository configuration, the null_sha1 entry contains the local
configuration of a submodule (e.g. consolidated values from local git
configuration and the .gitmodules file in the worktree). configuration and the .gitmodules file in the worktree).


For an example usage see test-submodule-config.c. For an example usage see test-submodule-config.c.

58
submodule-config.c

@ -263,12 +263,12 @@ int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
return parse_push_recurse(opt, arg, 1); return parse_push_recurse(opt, arg, 1);
} }


static void warn_multiple_config(const unsigned char *commit_sha1, static void warn_multiple_config(const unsigned char *treeish_name,
const char *name, const char *option) const char *name, const char *option)
{ {
const char *commit_string = "WORKTREE"; const char *commit_string = "WORKTREE";
if (commit_sha1) if (treeish_name)
commit_string = sha1_to_hex(commit_sha1); commit_string = sha1_to_hex(treeish_name);
warning("%s:.gitmodules, multiple configurations found for " warning("%s:.gitmodules, multiple configurations found for "
"'submodule.%s.%s'. Skipping second one!", "'submodule.%s.%s'. Skipping second one!",
commit_string, name, option); commit_string, name, option);
@ -276,7 +276,7 @@ static void warn_multiple_config(const unsigned char *commit_sha1,


struct parse_config_parameter { struct parse_config_parameter {
struct submodule_cache *cache; struct submodule_cache *cache;
const unsigned char *commit_sha1; const unsigned char *treeish_name;
const unsigned char *gitmodules_sha1; const unsigned char *gitmodules_sha1;
int overwrite; int overwrite;
}; };
@ -300,7 +300,7 @@ static int parse_config(const char *var, const char *value, void *data)
if (!value) if (!value)
ret = config_error_nonbool(var); ret = config_error_nonbool(var);
else if (!me->overwrite && submodule->path) else if (!me->overwrite && submodule->path)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"path"); "path");
else { else {
if (submodule->path) if (submodule->path)
@ -314,7 +314,7 @@ static int parse_config(const char *var, const char *value, void *data)
int die_on_error = is_null_sha1(me->gitmodules_sha1); int die_on_error = is_null_sha1(me->gitmodules_sha1);
if (!me->overwrite && if (!me->overwrite &&
submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"fetchrecursesubmodules"); "fetchrecursesubmodules");
else else
submodule->fetch_recurse = parse_fetch_recurse( submodule->fetch_recurse = parse_fetch_recurse(
@ -324,7 +324,7 @@ static int parse_config(const char *var, const char *value, void *data)
if (!value) if (!value)
ret = config_error_nonbool(var); ret = config_error_nonbool(var);
else if (!me->overwrite && submodule->ignore) else if (!me->overwrite && submodule->ignore)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"ignore"); "ignore");
else if (strcmp(value, "untracked") && else if (strcmp(value, "untracked") &&
strcmp(value, "dirty") && strcmp(value, "dirty") &&
@ -340,7 +340,7 @@ static int parse_config(const char *var, const char *value, void *data)
if (!value) { if (!value) {
ret = config_error_nonbool(var); ret = config_error_nonbool(var);
} else if (!me->overwrite && submodule->url) { } else if (!me->overwrite && submodule->url) {
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"url"); "url");
} else { } else {
free((void *) submodule->url); free((void *) submodule->url);
@ -351,21 +351,21 @@ static int parse_config(const char *var, const char *value, void *data)
ret = config_error_nonbool(var); ret = config_error_nonbool(var);
else if (!me->overwrite && else if (!me->overwrite &&
submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"update"); "update");
else if (parse_submodule_update_strategy(value, else if (parse_submodule_update_strategy(value,
&submodule->update_strategy) < 0) &submodule->update_strategy) < 0)
die(_("invalid value for %s"), var); die(_("invalid value for %s"), var);
} else if (!strcmp(item.buf, "shallow")) { } else if (!strcmp(item.buf, "shallow")) {
if (!me->overwrite && submodule->recommend_shallow != -1) if (!me->overwrite && submodule->recommend_shallow != -1)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"shallow"); "shallow");
else else
submodule->recommend_shallow = submodule->recommend_shallow =
git_config_bool(var, value); git_config_bool(var, value);
} else if (!strcmp(item.buf, "branch")) { } else if (!strcmp(item.buf, "branch")) {
if (!me->overwrite && submodule->branch) if (!me->overwrite && submodule->branch)
warn_multiple_config(me->commit_sha1, submodule->name, warn_multiple_config(me->treeish_name, submodule->name,
"branch"); "branch");
else { else {
free((void *)submodule->branch); free((void *)submodule->branch);
@ -379,18 +379,18 @@ static int parse_config(const char *var, const char *value, void *data)
return ret; return ret;
} }


static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1, static int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
unsigned char *gitmodules_sha1, unsigned char *gitmodules_sha1,
struct strbuf *rev) struct strbuf *rev)
{ {
int ret = 0; int ret = 0;


if (is_null_sha1(commit_sha1)) { if (is_null_sha1(treeish_name)) {
hashclr(gitmodules_sha1); hashclr(gitmodules_sha1);
return 1; return 1;
} }


strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1)); strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
if (get_sha1(rev->buf, gitmodules_sha1) >= 0) if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
ret = 1; ret = 1;


@ -402,7 +402,7 @@ static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
* revisions. * revisions.
*/ */
static const struct submodule *config_from(struct submodule_cache *cache, static const struct submodule *config_from(struct submodule_cache *cache,
const unsigned char *commit_sha1, const char *key, const unsigned char *treeish_name, const char *key,
enum lookup_type lookup_type) enum lookup_type lookup_type)
{ {
struct strbuf rev = STRBUF_INIT; struct strbuf rev = STRBUF_INIT;
@ -418,7 +418,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
* return the first submodule. Can be used to check whether * return the first submodule. Can be used to check whether
* there are any submodules parsed. * there are any submodules parsed.
*/ */
if (!commit_sha1 || !key) { if (!treeish_name || !key) {
struct hashmap_iter iter; struct hashmap_iter iter;
struct submodule_entry *entry; struct submodule_entry *entry;


@ -428,7 +428,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
return entry->config; return entry->config;
} }


if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
goto out; goto out;


switch (lookup_type) { switch (lookup_type) {
@ -448,7 +448,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,


/* fill the submodule config into the cache */ /* fill the submodule config into the cache */
parameter.cache = cache; parameter.cache = cache;
parameter.commit_sha1 = commit_sha1; parameter.treeish_name = treeish_name;
parameter.gitmodules_sha1 = sha1; parameter.gitmodules_sha1 = sha1;
parameter.overwrite = 0; parameter.overwrite = 0;
git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
@ -471,18 +471,6 @@ out:
return submodule; return submodule;
} }


static const struct submodule *config_from_path(struct submodule_cache *cache,
const unsigned char *commit_sha1, const char *path)
{
return config_from(cache, commit_sha1, path, lookup_path);
}

static const struct submodule *config_from_name(struct submodule_cache *cache,
const unsigned char *commit_sha1, const char *name)
{
return config_from(cache, commit_sha1, name, lookup_name);
}

static void ensure_cache_init(void) static void ensure_cache_init(void)
{ {
if (is_cache_init) if (is_cache_init)
@ -496,7 +484,7 @@ int parse_submodule_config_option(const char *var, const char *value)
{ {
struct parse_config_parameter parameter; struct parse_config_parameter parameter;
parameter.cache = &the_submodule_cache; parameter.cache = &the_submodule_cache;
parameter.commit_sha1 = NULL; parameter.treeish_name = NULL;
parameter.gitmodules_sha1 = null_sha1; parameter.gitmodules_sha1 = null_sha1;
parameter.overwrite = 1; parameter.overwrite = 1;


@ -504,18 +492,18 @@ int parse_submodule_config_option(const char *var, const char *value)
return parse_config(var, value, &parameter); return parse_config(var, value, &parameter);
} }


const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const struct submodule *submodule_from_name(const unsigned char *treeish_name,
const char *name) const char *name)
{ {
ensure_cache_init(); ensure_cache_init();
return config_from_name(&the_submodule_cache, commit_sha1, name); return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
} }


const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const struct submodule *submodule_from_path(const unsigned char *treeish_name,
const char *path) const char *path)
{ {
ensure_cache_init(); ensure_cache_init();
return config_from_path(&the_submodule_cache, commit_sha1, path); return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
} }


void submodule_free(void) void submodule_free(void)

4
submodule-config.h

@ -25,9 +25,9 @@ struct submodule {
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg); int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
int parse_push_recurse_submodules_arg(const char *opt, const char *arg); int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
int parse_submodule_config_option(const char *var, const char *value); int parse_submodule_config_option(const char *var, const char *value);
const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const struct submodule *submodule_from_name(const unsigned char *commit_or_tree,
const char *name); const char *name);
const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const struct submodule *submodule_from_path(const unsigned char *commit_or_tree,
const char *path); const char *path);
void submodule_free(void); void submodule_free(void);



14
t/t7411-submodule-config.sh

@ -93,6 +93,20 @@ test_expect_success 'error message contains blob reference' '
) )
' '


test_expect_success 'using different treeishs works' '
(
cd super &&
git tag new_tag &&
tree=$(git rev-parse HEAD^{tree}) &&
commit=$(git rev-parse HEAD^{commit}) &&
test-submodule-config $commit b >expect &&
test-submodule-config $tree b >actual.1 &&
test-submodule-config new_tag b >actual.2 &&
test_cmp expect actual.1 &&
test_cmp expect actual.2
)
'

cat >super/expect_url <<EOF cat >super/expect_url <<EOF
Submodule url: 'git@somewhere.else.net:a.git' for path 'b' Submodule url: 'git@somewhere.else.net:a.git' for path 'b'
Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule' Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'

Loading…
Cancel
Save