2015-08-18 02:21:57 +02:00
|
|
|
#include "cache.h"
|
2017-06-22 20:43:44 +02:00
|
|
|
#include "repository.h"
|
2017-06-14 20:07:36 +02:00
|
|
|
#include "config.h"
|
2015-08-18 02:21:57 +02:00
|
|
|
#include "submodule-config.h"
|
|
|
|
#include "submodule.h"
|
|
|
|
#include "strbuf.h"
|
2017-06-23 21:13:00 +02:00
|
|
|
#include "parse-options.h"
|
2015-08-18 02:21:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* submodule cache lookup structure
|
|
|
|
* There is one shared set of 'struct submodule' entries which can be
|
|
|
|
* looked up by their sha1 blob id of the .gitmodule file and either
|
|
|
|
* using path or name as key.
|
|
|
|
* for_path stores submodule entries with path as key
|
|
|
|
* for_name stores submodule entries with name as key
|
|
|
|
*/
|
|
|
|
struct submodule_cache {
|
|
|
|
struct hashmap for_path;
|
|
|
|
struct hashmap for_name;
|
2017-06-22 20:43:44 +02:00
|
|
|
unsigned initialized:1;
|
2015-08-18 02:21:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* thin wrapper struct needed to insert 'struct submodule' entries to
|
|
|
|
* the hashmap
|
|
|
|
*/
|
|
|
|
struct submodule_entry {
|
|
|
|
struct hashmap_entry ent;
|
|
|
|
struct submodule *config;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum lookup_type {
|
|
|
|
lookup_name,
|
|
|
|
lookup_path
|
|
|
|
};
|
|
|
|
|
2017-06-30 21:14:05 +02:00
|
|
|
static int config_path_cmp(const void *unused_cmp_data,
|
|
|
|
const struct submodule_entry *a,
|
2015-08-18 02:21:57 +02:00
|
|
|
const struct submodule_entry *b,
|
2017-06-30 21:14:05 +02:00
|
|
|
const void *unused_keydata)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
|
|
|
return strcmp(a->config->path, b->config->path) ||
|
|
|
|
hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:14:05 +02:00
|
|
|
static int config_name_cmp(const void *unused_cmp_data,
|
|
|
|
const struct submodule_entry *a,
|
2015-08-18 02:21:57 +02:00
|
|
|
const struct submodule_entry *b,
|
2017-06-30 21:14:05 +02:00
|
|
|
const void *unused_keydata)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
|
|
|
return strcmp(a->config->name, b->config->name) ||
|
|
|
|
hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
static struct submodule_cache *submodule_cache_alloc(void)
|
|
|
|
{
|
|
|
|
return xcalloc(1, sizeof(struct submodule_cache));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void submodule_cache_init(struct submodule_cache *cache)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
2017-06-30 21:14:05 +02:00
|
|
|
hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
|
|
|
|
hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
|
2017-06-22 20:43:44 +02:00
|
|
|
cache->initialized = 1;
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_one_config(struct submodule_entry *entry)
|
|
|
|
{
|
|
|
|
free((void *) entry->config->path);
|
|
|
|
free((void *) entry->config->name);
|
2016-07-29 02:44:07 +02:00
|
|
|
free((void *) entry->config->branch);
|
2016-03-01 03:07:11 +01:00
|
|
|
free((void *) entry->config->update_strategy.command);
|
2015-08-18 02:21:57 +02:00
|
|
|
free(entry->config);
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
static void submodule_cache_clear(struct submodule_cache *cache)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
|
|
|
struct hashmap_iter iter;
|
|
|
|
struct submodule_entry *entry;
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
if (!cache->initialized)
|
|
|
|
return;
|
|
|
|
|
2015-08-18 02:21:57 +02:00
|
|
|
/*
|
|
|
|
* We iterate over the name hash here to be symmetric with the
|
|
|
|
* allocation of struct submodule entries. Each is allocated by
|
|
|
|
* their .gitmodule blob sha1 and submodule name.
|
|
|
|
*/
|
|
|
|
hashmap_iter_init(&cache->for_name, &iter);
|
|
|
|
while ((entry = hashmap_iter_next(&iter)))
|
|
|
|
free_one_config(entry);
|
|
|
|
|
|
|
|
hashmap_free(&cache->for_path, 1);
|
|
|
|
hashmap_free(&cache->for_name, 1);
|
2017-06-22 20:43:44 +02:00
|
|
|
cache->initialized = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void submodule_cache_free(struct submodule_cache *cache)
|
|
|
|
{
|
|
|
|
submodule_cache_clear(cache);
|
|
|
|
free(cache);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int hash_sha1_string(const unsigned char *sha1,
|
|
|
|
const char *string)
|
|
|
|
{
|
|
|
|
return memhash(sha1, 20) + strhash(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_put_path(struct submodule_cache *cache,
|
|
|
|
struct submodule *submodule)
|
|
|
|
{
|
|
|
|
unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
|
|
|
|
submodule->path);
|
|
|
|
struct submodule_entry *e = xmalloc(sizeof(*e));
|
|
|
|
hashmap_entry_init(e, hash);
|
|
|
|
e->config = submodule;
|
|
|
|
hashmap_put(&cache->for_path, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_remove_path(struct submodule_cache *cache,
|
|
|
|
struct submodule *submodule)
|
|
|
|
{
|
|
|
|
unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
|
|
|
|
submodule->path);
|
|
|
|
struct submodule_entry e;
|
|
|
|
struct submodule_entry *removed;
|
|
|
|
hashmap_entry_init(&e, hash);
|
|
|
|
e.config = submodule;
|
|
|
|
removed = hashmap_remove(&cache->for_path, &e, NULL);
|
|
|
|
free(removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_add(struct submodule_cache *cache,
|
|
|
|
struct submodule *submodule)
|
|
|
|
{
|
|
|
|
unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
|
|
|
|
submodule->name);
|
|
|
|
struct submodule_entry *e = xmalloc(sizeof(*e));
|
|
|
|
hashmap_entry_init(e, hash);
|
|
|
|
e->config = submodule;
|
|
|
|
hashmap_add(&cache->for_name, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
|
|
|
|
const unsigned char *gitmodules_sha1, const char *path)
|
|
|
|
{
|
|
|
|
struct submodule_entry *entry;
|
|
|
|
unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
|
|
|
|
struct submodule_entry key;
|
|
|
|
struct submodule key_config;
|
|
|
|
|
|
|
|
hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
|
|
|
|
key_config.path = path;
|
|
|
|
|
|
|
|
hashmap_entry_init(&key, hash);
|
|
|
|
key.config = &key_config;
|
|
|
|
|
|
|
|
entry = hashmap_get(&cache->for_path, &key, NULL);
|
|
|
|
if (entry)
|
|
|
|
return entry->config;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct submodule *cache_lookup_name(struct submodule_cache *cache,
|
|
|
|
const unsigned char *gitmodules_sha1, const char *name)
|
|
|
|
{
|
|
|
|
struct submodule_entry *entry;
|
|
|
|
unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
|
|
|
|
struct submodule_entry key;
|
|
|
|
struct submodule key_config;
|
|
|
|
|
|
|
|
hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
|
|
|
|
key_config.name = name;
|
|
|
|
|
|
|
|
hashmap_entry_init(&key, hash);
|
|
|
|
key.config = &key_config;
|
|
|
|
|
|
|
|
entry = hashmap_get(&cache->for_name, &key, NULL);
|
|
|
|
if (entry)
|
|
|
|
return entry->config;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int name_and_item_from_var(const char *var, struct strbuf *name,
|
|
|
|
struct strbuf *item)
|
|
|
|
{
|
|
|
|
const char *subsection, *key;
|
|
|
|
int subsection_len, parse;
|
|
|
|
parse = parse_config_key(var, "submodule", &subsection,
|
|
|
|
&subsection_len, &key);
|
|
|
|
if (parse < 0 || !subsection)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
strbuf_add(name, subsection, subsection_len);
|
|
|
|
strbuf_addstr(item, key);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
|
|
|
|
const unsigned char *gitmodules_sha1, const char *name)
|
|
|
|
{
|
|
|
|
struct submodule *submodule;
|
|
|
|
struct strbuf name_buf = STRBUF_INIT;
|
|
|
|
|
|
|
|
submodule = cache_lookup_name(cache, gitmodules_sha1, name);
|
|
|
|
if (submodule)
|
|
|
|
return submodule;
|
|
|
|
|
|
|
|
submodule = xmalloc(sizeof(*submodule));
|
|
|
|
|
|
|
|
strbuf_addstr(&name_buf, name);
|
|
|
|
submodule->name = strbuf_detach(&name_buf, NULL);
|
|
|
|
|
|
|
|
submodule->path = NULL;
|
|
|
|
submodule->url = NULL;
|
2016-03-01 03:07:11 +01:00
|
|
|
submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
|
|
|
|
submodule->update_strategy.command = NULL;
|
2015-08-18 02:21:57 +02:00
|
|
|
submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
|
|
|
|
submodule->ignore = NULL;
|
2016-07-29 02:44:07 +02:00
|
|
|
submodule->branch = NULL;
|
2016-05-26 23:59:42 +02:00
|
|
|
submodule->recommend_shallow = -1;
|
2015-08-18 02:21:57 +02:00
|
|
|
|
|
|
|
hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
|
|
|
|
|
|
|
|
cache_add(cache, submodule);
|
|
|
|
|
|
|
|
return submodule;
|
|
|
|
}
|
|
|
|
|
2015-08-18 02:22:00 +02:00
|
|
|
static int parse_fetch_recurse(const char *opt, const char *arg,
|
|
|
|
int die_on_error)
|
|
|
|
{
|
|
|
|
switch (git_config_maybe_bool(opt, arg)) {
|
|
|
|
case 1:
|
|
|
|
return RECURSE_SUBMODULES_ON;
|
|
|
|
case 0:
|
|
|
|
return RECURSE_SUBMODULES_OFF;
|
|
|
|
default:
|
|
|
|
if (!strcmp(arg, "on-demand"))
|
|
|
|
return RECURSE_SUBMODULES_ON_DEMAND;
|
|
|
|
|
|
|
|
if (die_on_error)
|
|
|
|
die("bad %s argument: %s", opt, arg);
|
|
|
|
else
|
|
|
|
return RECURSE_SUBMODULES_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 21:49:18 +02:00
|
|
|
int parse_submodule_fetchjobs(const char *var, const char *value)
|
|
|
|
{
|
|
|
|
int fetchjobs = git_config_int(var, value);
|
|
|
|
if (fetchjobs < 0)
|
|
|
|
die(_("negative values not allowed for submodule.fetchjobs"));
|
|
|
|
return fetchjobs;
|
|
|
|
}
|
|
|
|
|
2015-08-18 02:22:00 +02:00
|
|
|
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
|
|
|
|
{
|
|
|
|
return parse_fetch_recurse(opt, arg, 1);
|
|
|
|
}
|
|
|
|
|
2017-06-23 21:13:00 +02:00
|
|
|
int option_fetch_parse_recurse_submodules(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
int *v;
|
|
|
|
|
|
|
|
if (!opt->value)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
v = opt->value;
|
|
|
|
|
|
|
|
if (unset) {
|
|
|
|
*v = RECURSE_SUBMODULES_OFF;
|
|
|
|
} else {
|
|
|
|
if (arg)
|
|
|
|
*v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
|
|
|
|
else
|
|
|
|
*v = RECURSE_SUBMODULES_ON;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:46:32 +01:00
|
|
|
static int parse_update_recurse(const char *opt, const char *arg,
|
|
|
|
int die_on_error)
|
|
|
|
{
|
|
|
|
switch (git_config_maybe_bool(opt, arg)) {
|
|
|
|
case 1:
|
|
|
|
return RECURSE_SUBMODULES_ON;
|
|
|
|
case 0:
|
|
|
|
return RECURSE_SUBMODULES_OFF;
|
|
|
|
default:
|
|
|
|
if (die_on_error)
|
|
|
|
die("bad %s argument: %s", opt, arg);
|
|
|
|
return RECURSE_SUBMODULES_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
|
|
|
|
{
|
|
|
|
return parse_update_recurse(opt, arg, 1);
|
|
|
|
}
|
|
|
|
|
2015-11-17 12:05:56 +01:00
|
|
|
static int parse_push_recurse(const char *opt, const char *arg,
|
|
|
|
int die_on_error)
|
|
|
|
{
|
|
|
|
switch (git_config_maybe_bool(opt, arg)) {
|
|
|
|
case 1:
|
|
|
|
/* There's no simple "on" value when pushing */
|
|
|
|
if (die_on_error)
|
|
|
|
die("bad %s argument: %s", opt, arg);
|
|
|
|
else
|
|
|
|
return RECURSE_SUBMODULES_ERROR;
|
|
|
|
case 0:
|
|
|
|
return RECURSE_SUBMODULES_OFF;
|
|
|
|
default:
|
|
|
|
if (!strcmp(arg, "on-demand"))
|
|
|
|
return RECURSE_SUBMODULES_ON_DEMAND;
|
|
|
|
else if (!strcmp(arg, "check"))
|
|
|
|
return RECURSE_SUBMODULES_CHECK;
|
2016-12-19 19:25:32 +01:00
|
|
|
else if (!strcmp(arg, "only"))
|
|
|
|
return RECURSE_SUBMODULES_ONLY;
|
2015-11-17 12:05:56 +01:00
|
|
|
else if (die_on_error)
|
|
|
|
die("bad %s argument: %s", opt, arg);
|
|
|
|
else
|
|
|
|
return RECURSE_SUBMODULES_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
|
|
|
|
{
|
|
|
|
return parse_push_recurse(opt, arg, 1);
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:14:37 +01:00
|
|
|
static void warn_multiple_config(const unsigned char *treeish_name,
|
2015-08-18 02:21:57 +02:00
|
|
|
const char *name, const char *option)
|
|
|
|
{
|
|
|
|
const char *commit_string = "WORKTREE";
|
2016-11-22 21:14:37 +01:00
|
|
|
if (treeish_name)
|
|
|
|
commit_string = sha1_to_hex(treeish_name);
|
2015-08-18 02:21:57 +02:00
|
|
|
warning("%s:.gitmodules, multiple configurations found for "
|
|
|
|
"'submodule.%s.%s'. Skipping second one!",
|
|
|
|
commit_string, name, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct parse_config_parameter {
|
|
|
|
struct submodule_cache *cache;
|
2016-11-22 21:14:37 +01:00
|
|
|
const unsigned char *treeish_name;
|
2015-08-18 02:21:57 +02:00
|
|
|
const unsigned char *gitmodules_sha1;
|
|
|
|
int overwrite;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int parse_config(const char *var, const char *value, void *data)
|
|
|
|
{
|
|
|
|
struct parse_config_parameter *me = data;
|
|
|
|
struct submodule *submodule;
|
|
|
|
struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* this also ensures that we only parse submodule entries */
|
|
|
|
if (!name_and_item_from_var(var, &name, &item))
|
|
|
|
return 0;
|
|
|
|
|
2015-10-12 19:58:58 +02:00
|
|
|
submodule = lookup_or_create_by_name(me->cache,
|
|
|
|
me->gitmodules_sha1,
|
|
|
|
name.buf);
|
2015-08-18 02:21:57 +02:00
|
|
|
|
|
|
|
if (!strcmp(item.buf, "path")) {
|
2015-10-12 19:58:58 +02:00
|
|
|
if (!value)
|
2015-08-18 02:21:57 +02:00
|
|
|
ret = config_error_nonbool(var);
|
2016-03-01 03:07:12 +01:00
|
|
|
else if (!me->overwrite && submodule->path)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2015-08-18 02:21:57 +02:00
|
|
|
"path");
|
2015-10-12 19:58:58 +02:00
|
|
|
else {
|
|
|
|
if (submodule->path)
|
|
|
|
cache_remove_path(me->cache, submodule);
|
|
|
|
free((void *) submodule->path);
|
|
|
|
submodule->path = xstrdup(value);
|
|
|
|
cache_put_path(me->cache, submodule);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
} else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
|
2015-08-18 02:22:00 +02:00
|
|
|
/* when parsing worktree configurations we can die early */
|
|
|
|
int die_on_error = is_null_sha1(me->gitmodules_sha1);
|
2015-08-18 02:21:57 +02:00
|
|
|
if (!me->overwrite &&
|
2015-10-12 19:58:58 +02:00
|
|
|
submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2015-08-18 02:21:57 +02:00
|
|
|
"fetchrecursesubmodules");
|
2015-10-12 19:58:58 +02:00
|
|
|
else
|
|
|
|
submodule->fetch_recurse = parse_fetch_recurse(
|
|
|
|
var, value,
|
2015-08-18 02:22:00 +02:00
|
|
|
die_on_error);
|
2015-08-18 02:21:57 +02:00
|
|
|
} else if (!strcmp(item.buf, "ignore")) {
|
2015-10-12 19:58:58 +02:00
|
|
|
if (!value)
|
|
|
|
ret = config_error_nonbool(var);
|
2016-03-01 03:07:12 +01:00
|
|
|
else if (!me->overwrite && submodule->ignore)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2015-08-18 02:21:57 +02:00
|
|
|
"ignore");
|
2015-10-12 19:58:58 +02:00
|
|
|
else if (strcmp(value, "untracked") &&
|
|
|
|
strcmp(value, "dirty") &&
|
|
|
|
strcmp(value, "all") &&
|
|
|
|
strcmp(value, "none"))
|
2015-08-18 02:21:57 +02:00
|
|
|
warning("Invalid parameter '%s' for config option "
|
2017-03-14 23:14:40 +01:00
|
|
|
"'submodule.%s.ignore'", value, name.buf);
|
2015-10-12 19:58:58 +02:00
|
|
|
else {
|
|
|
|
free((void *) submodule->ignore);
|
|
|
|
submodule->ignore = xstrdup(value);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
} else if (!strcmp(item.buf, "url")) {
|
|
|
|
if (!value) {
|
|
|
|
ret = config_error_nonbool(var);
|
2016-03-01 03:07:12 +01:00
|
|
|
} else if (!me->overwrite && submodule->url) {
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2015-08-18 02:21:57 +02:00
|
|
|
"url");
|
2015-10-12 19:58:58 +02:00
|
|
|
} else {
|
|
|
|
free((void *) submodule->url);
|
|
|
|
submodule->url = xstrdup(value);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
2016-03-01 03:07:11 +01:00
|
|
|
} else if (!strcmp(item.buf, "update")) {
|
|
|
|
if (!value)
|
|
|
|
ret = config_error_nonbool(var);
|
|
|
|
else if (!me->overwrite &&
|
|
|
|
submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2016-03-01 03:07:11 +01:00
|
|
|
"update");
|
|
|
|
else if (parse_submodule_update_strategy(value,
|
|
|
|
&submodule->update_strategy) < 0)
|
|
|
|
die(_("invalid value for %s"), var);
|
2016-05-26 23:59:42 +02:00
|
|
|
} else if (!strcmp(item.buf, "shallow")) {
|
|
|
|
if (!me->overwrite && submodule->recommend_shallow != -1)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2016-05-26 23:59:42 +02:00
|
|
|
"shallow");
|
2016-07-29 02:44:07 +02:00
|
|
|
else
|
2016-05-26 23:59:42 +02:00
|
|
|
submodule->recommend_shallow =
|
|
|
|
git_config_bool(var, value);
|
2016-07-29 02:44:07 +02:00
|
|
|
} else if (!strcmp(item.buf, "branch")) {
|
|
|
|
if (!me->overwrite && submodule->branch)
|
2016-11-22 21:14:37 +01:00
|
|
|
warn_multiple_config(me->treeish_name, submodule->name,
|
2016-07-29 02:44:07 +02:00
|
|
|
"branch");
|
|
|
|
else {
|
|
|
|
free((void *)submodule->branch);
|
|
|
|
submodule->branch = xstrdup(value);
|
2016-05-26 23:59:42 +02:00
|
|
|
}
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&name);
|
|
|
|
strbuf_release(&item);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
int gitmodule_oid_from_commit(const struct object_id *treeish_name,
|
|
|
|
struct object_id *gitmodules_oid,
|
2016-07-28 14:49:11 +02:00
|
|
|
struct strbuf *rev)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
if (is_null_oid(treeish_name)) {
|
|
|
|
oidclr(gitmodules_oid);
|
2015-08-18 02:21:57 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
|
|
|
|
if (get_oid(rev->buf, gitmodules_oid) >= 0)
|
2015-08-18 02:21:57 +02:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This does a lookup of a submodule configuration by name or by path
|
|
|
|
* (key) with on-demand reading of the appropriate .gitmodules from
|
|
|
|
* revisions.
|
|
|
|
*/
|
|
|
|
static const struct submodule *config_from(struct submodule_cache *cache,
|
2017-07-14 01:49:20 +02:00
|
|
|
const struct object_id *treeish_name, const char *key,
|
2015-08-18 02:21:57 +02:00
|
|
|
enum lookup_type lookup_type)
|
|
|
|
{
|
|
|
|
struct strbuf rev = STRBUF_INIT;
|
|
|
|
unsigned long config_size;
|
2016-07-28 14:49:47 +02:00
|
|
|
char *config = NULL;
|
2017-07-14 01:49:20 +02:00
|
|
|
struct object_id oid;
|
2015-08-18 02:21:57 +02:00
|
|
|
enum object_type type;
|
|
|
|
const struct submodule *submodule = NULL;
|
|
|
|
struct parse_config_parameter parameter;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If any parameter except the cache is a NULL pointer just
|
|
|
|
* return the first submodule. Can be used to check whether
|
|
|
|
* there are any submodules parsed.
|
|
|
|
*/
|
2016-11-22 21:14:37 +01:00
|
|
|
if (!treeish_name || !key) {
|
2015-08-18 02:21:57 +02:00
|
|
|
struct hashmap_iter iter;
|
|
|
|
struct submodule_entry *entry;
|
|
|
|
|
2016-03-16 08:46:31 +01:00
|
|
|
entry = hashmap_iter_first(&cache->for_name, &iter);
|
2015-08-18 02:21:57 +02:00
|
|
|
if (!entry)
|
|
|
|
return NULL;
|
|
|
|
return entry->config;
|
|
|
|
}
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
|
2016-07-28 14:49:47 +02:00
|
|
|
goto out;
|
2015-08-18 02:21:57 +02:00
|
|
|
|
|
|
|
switch (lookup_type) {
|
|
|
|
case lookup_name:
|
2017-07-14 01:49:20 +02:00
|
|
|
submodule = cache_lookup_name(cache, oid.hash, key);
|
2015-08-18 02:21:57 +02:00
|
|
|
break;
|
|
|
|
case lookup_path:
|
2017-07-14 01:49:20 +02:00
|
|
|
submodule = cache_lookup_path(cache, oid.hash, key);
|
2015-08-18 02:21:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (submodule)
|
2016-07-28 14:49:47 +02:00
|
|
|
goto out;
|
2015-08-18 02:21:57 +02:00
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
config = read_sha1_file(oid.hash, &type, &config_size);
|
2016-07-28 14:49:47 +02:00
|
|
|
if (!config || type != OBJ_BLOB)
|
|
|
|
goto out;
|
2015-08-18 02:21:57 +02:00
|
|
|
|
|
|
|
/* fill the submodule config into the cache */
|
|
|
|
parameter.cache = cache;
|
2017-07-14 01:49:20 +02:00
|
|
|
parameter.treeish_name = treeish_name->hash;
|
|
|
|
parameter.gitmodules_sha1 = oid.hash;
|
2015-08-18 02:21:57 +02:00
|
|
|
parameter.overwrite = 0;
|
i18n: config: unfold error messages marked for translation
Introduced in 473166b ("config: add 'origin_type' to config_source
struct", 2016-02-19), Git can inform the user about the origin of a
config error, but the implementation does not allow translators to
translate the keywords 'file', 'blob, 'standard input', and
'submodule-blob'. Moreover, for the second message, a reason for the
error is appended to the message, not allowing translators to translate
that reason either.
Unfold the message into several templates for each known origin_type.
That would result in better translation at the expense of code
verbosity.
Add enum config_oringin_type to ease management of the various
configuration origin types (blob, file, etc). Previously origin type
was considered from command line if cf->origin_type == NULL, i.e.,
uninitialized. Now we set origin_type to CONFIG_ORIGIN_CMDLINE in
git_config_from_parameters() and configset_add_value().
For error message in git_parse_source(), use xstrfmt() function to
prepare the message string, instead of doing something like it's done
for die_bad_number(), because intelligibility and code conciseness are
improved for that instance.
Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-07-28 15:14:03 +02:00
|
|
|
git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
|
2016-02-19 10:16:01 +01:00
|
|
|
config, config_size, ¶meter);
|
2016-07-28 14:49:11 +02:00
|
|
|
strbuf_release(&rev);
|
2015-08-18 02:21:57 +02:00
|
|
|
free(config);
|
|
|
|
|
|
|
|
switch (lookup_type) {
|
|
|
|
case lookup_name:
|
2017-07-14 01:49:20 +02:00
|
|
|
return cache_lookup_name(cache, oid.hash, key);
|
2015-08-18 02:21:57 +02:00
|
|
|
case lookup_path:
|
2017-07-14 01:49:20 +02:00
|
|
|
return cache_lookup_path(cache, oid.hash, key);
|
2015-08-18 02:21:57 +02:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-28 14:49:47 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
strbuf_release(&rev);
|
|
|
|
free(config);
|
|
|
|
return submodule;
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
static void submodule_cache_check_init(struct repository *repo)
|
2015-08-18 02:21:57 +02:00
|
|
|
{
|
2017-06-22 20:43:44 +02:00
|
|
|
if (repo->submodule_cache && repo->submodule_cache->initialized)
|
2015-08-18 02:21:57 +02:00
|
|
|
return;
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
if (!repo->submodule_cache)
|
|
|
|
repo->submodule_cache = submodule_cache_alloc();
|
|
|
|
|
|
|
|
submodule_cache_init(repo->submodule_cache);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
int submodule_config_option(struct repository *repo,
|
|
|
|
const char *var, const char *value)
|
2015-08-18 02:21:59 +02:00
|
|
|
{
|
|
|
|
struct parse_config_parameter parameter;
|
2017-06-22 20:43:44 +02:00
|
|
|
|
|
|
|
submodule_cache_check_init(repo);
|
|
|
|
|
|
|
|
parameter.cache = repo->submodule_cache;
|
2016-11-22 21:14:37 +01:00
|
|
|
parameter.treeish_name = NULL;
|
2015-08-18 02:21:59 +02:00
|
|
|
parameter.gitmodules_sha1 = null_sha1;
|
|
|
|
parameter.overwrite = 1;
|
|
|
|
|
|
|
|
return parse_config(var, value, ¶meter);
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:43:44 +02:00
|
|
|
int parse_submodule_config_option(const char *var, const char *value)
|
|
|
|
{
|
|
|
|
return submodule_config_option(the_repository, var, value);
|
|
|
|
}
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
const struct submodule *submodule_from_name(const struct object_id *treeish_name,
|
2015-08-18 02:21:57 +02:00
|
|
|
const char *name)
|
|
|
|
{
|
2017-06-22 20:43:44 +02:00
|
|
|
submodule_cache_check_init(the_repository);
|
|
|
|
return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 01:49:20 +02:00
|
|
|
const struct submodule *submodule_from_path(const struct object_id *treeish_name,
|
2015-08-18 02:21:57 +02:00
|
|
|
const char *path)
|
|
|
|
{
|
2017-06-22 20:43:44 +02:00
|
|
|
submodule_cache_check_init(the_repository);
|
|
|
|
return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct submodule *submodule_from_cache(struct repository *repo,
|
2017-07-14 01:49:20 +02:00
|
|
|
const struct object_id *treeish_name,
|
2017-06-22 20:43:44 +02:00
|
|
|
const char *key)
|
|
|
|
{
|
|
|
|
submodule_cache_check_init(repo);
|
|
|
|
return config_from(repo->submodule_cache, treeish_name,
|
|
|
|
key, lookup_path);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void submodule_free(void)
|
|
|
|
{
|
2017-06-22 20:43:44 +02:00
|
|
|
if (the_repository->submodule_cache)
|
|
|
|
submodule_cache_clear(the_repository->submodule_cache);
|
2015-08-18 02:21:57 +02:00
|
|
|
}
|