Blame submodule-config.c

Packit 4511e4
#include "cache.h"
Packit 4511e4
#include "repository.h"
Packit 4511e4
#include "config.h"
Packit 4511e4
#include "submodule-config.h"
Packit 4511e4
#include "submodule.h"
Packit 4511e4
#include "strbuf.h"
Packit 4511e4
#include "parse-options.h"
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * submodule cache lookup structure
Packit 4511e4
 * There is one shared set of 'struct submodule' entries which can be
Packit 4511e4
 * looked up by their sha1 blob id of the .gitmodules file and either
Packit 4511e4
 * using path or name as key.
Packit 4511e4
 * for_path stores submodule entries with path as key
Packit 4511e4
 * for_name stores submodule entries with name as key
Packit 4511e4
 */
Packit 4511e4
struct submodule_cache {
Packit 4511e4
	struct hashmap for_path;
Packit 4511e4
	struct hashmap for_name;
Packit 4511e4
	unsigned initialized:1;
Packit 4511e4
	unsigned gitmodules_read:1;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * thin wrapper struct needed to insert 'struct submodule' entries to
Packit 4511e4
 * the hashmap
Packit 4511e4
 */
Packit 4511e4
struct submodule_entry {
Packit 4511e4
	struct hashmap_entry ent;
Packit 4511e4
	struct submodule *config;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
enum lookup_type {
Packit 4511e4
	lookup_name,
Packit 4511e4
	lookup_path
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static int config_path_cmp(const void *unused_cmp_data,
Packit 4511e4
			   const void *entry,
Packit 4511e4
			   const void *entry_or_key,
Packit 4511e4
			   const void *unused_keydata)
Packit 4511e4
{
Packit 4511e4
	const struct submodule_entry *a = entry;
Packit 4511e4
	const struct submodule_entry *b = entry_or_key;
Packit 4511e4
Packit 4511e4
	return strcmp(a->config->path, b->config->path) ||
Packit 4511e4
	       oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int config_name_cmp(const void *unused_cmp_data,
Packit 4511e4
			   const void *entry,
Packit 4511e4
			   const void *entry_or_key,
Packit 4511e4
			   const void *unused_keydata)
Packit 4511e4
{
Packit 4511e4
	const struct submodule_entry *a = entry;
Packit 4511e4
	const struct submodule_entry *b = entry_or_key;
Packit 4511e4
Packit 4511e4
	return strcmp(a->config->name, b->config->name) ||
Packit 4511e4
	       oidcmp(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct submodule_cache *submodule_cache_alloc(void)
Packit 4511e4
{
Packit 4511e4
	return xcalloc(1, sizeof(struct submodule_cache));
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void submodule_cache_init(struct submodule_cache *cache)
Packit 4511e4
{
Packit 4511e4
	hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
Packit 4511e4
	hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
Packit 4511e4
	cache->initialized = 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void free_one_config(struct submodule_entry *entry)
Packit 4511e4
{
Packit 4511e4
	free((void *) entry->config->path);
Packit 4511e4
	free((void *) entry->config->name);
Packit 4511e4
	free((void *) entry->config->branch);
Packit 4511e4
	free((void *) entry->config->update_strategy.command);
Packit 4511e4
	free(entry->config);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void submodule_cache_clear(struct submodule_cache *cache)
Packit 4511e4
{
Packit 4511e4
	struct hashmap_iter iter;
Packit 4511e4
	struct submodule_entry *entry;
Packit 4511e4
Packit 4511e4
	if (!cache->initialized)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * We iterate over the name hash here to be symmetric with the
Packit 4511e4
	 * allocation of struct submodule entries. Each is allocated by
Packit 4511e4
	 * their .gitmodules blob sha1 and submodule name.
Packit 4511e4
	 */
Packit 4511e4
	hashmap_iter_init(&cache->for_name, &iter);
Packit 4511e4
	while ((entry = hashmap_iter_next(&iter)))
Packit 4511e4
		free_one_config(entry);
Packit 4511e4
Packit 4511e4
	hashmap_free(&cache->for_path, 1);
Packit 4511e4
	hashmap_free(&cache->for_name, 1);
Packit 4511e4
	cache->initialized = 0;
Packit 4511e4
	cache->gitmodules_read = 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void submodule_cache_free(struct submodule_cache *cache)
Packit 4511e4
{
Packit 4511e4
	submodule_cache_clear(cache);
Packit 4511e4
	free(cache);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static unsigned int hash_oid_string(const struct object_id *oid,
Packit 4511e4
				    const char *string)
Packit 4511e4
{
Packit 4511e4
	return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void cache_put_path(struct submodule_cache *cache,
Packit 4511e4
			   struct submodule *submodule)
Packit 4511e4
{
Packit 4511e4
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
Packit 4511e4
					    submodule->path);
Packit 4511e4
	struct submodule_entry *e = xmalloc(sizeof(*e));
Packit 4511e4
	hashmap_entry_init(e, hash);
Packit 4511e4
	e->config = submodule;
Packit 4511e4
	hashmap_put(&cache->for_path, e);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void cache_remove_path(struct submodule_cache *cache,
Packit 4511e4
			      struct submodule *submodule)
Packit 4511e4
{
Packit 4511e4
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
Packit 4511e4
					    submodule->path);
Packit 4511e4
	struct submodule_entry e;
Packit 4511e4
	struct submodule_entry *removed;
Packit 4511e4
	hashmap_entry_init(&e, hash);
Packit 4511e4
	e.config = submodule;
Packit 4511e4
	removed = hashmap_remove(&cache->for_path, &e, NULL);
Packit 4511e4
	free(removed);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void cache_add(struct submodule_cache *cache,
Packit 4511e4
		      struct submodule *submodule)
Packit 4511e4
{
Packit 4511e4
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
Packit 4511e4
					    submodule->name);
Packit 4511e4
	struct submodule_entry *e = xmalloc(sizeof(*e));
Packit 4511e4
	hashmap_entry_init(e, hash);
Packit 4511e4
	e->config = submodule;
Packit 4511e4
	hashmap_add(&cache->for_name, e);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
Packit 4511e4
		const struct object_id *gitmodules_oid, const char *path)
Packit 4511e4
{
Packit 4511e4
	struct submodule_entry *entry;
Packit 4511e4
	unsigned int hash = hash_oid_string(gitmodules_oid, path);
Packit 4511e4
	struct submodule_entry key;
Packit 4511e4
	struct submodule key_config;
Packit 4511e4
Packit 4511e4
	oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Packit 4511e4
	key_config.path = path;
Packit 4511e4
Packit 4511e4
	hashmap_entry_init(&key, hash);
Packit 4511e4
	key.config = &key_config;
Packit 4511e4
Packit 4511e4
	entry = hashmap_get(&cache->for_path, &key, NULL);
Packit 4511e4
	if (entry)
Packit 4511e4
		return entry->config;
Packit 4511e4
	return NULL;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct submodule *cache_lookup_name(struct submodule_cache *cache,
Packit 4511e4
		const struct object_id *gitmodules_oid, const char *name)
Packit 4511e4
{
Packit 4511e4
	struct submodule_entry *entry;
Packit 4511e4
	unsigned int hash = hash_oid_string(gitmodules_oid, name);
Packit 4511e4
	struct submodule_entry key;
Packit 4511e4
	struct submodule key_config;
Packit 4511e4
Packit 4511e4
	oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Packit 4511e4
	key_config.name = name;
Packit 4511e4
Packit 4511e4
	hashmap_entry_init(&key, hash);
Packit 4511e4
	key.config = &key_config;
Packit 4511e4
Packit 4511e4
	entry = hashmap_get(&cache->for_name, &key, NULL);
Packit 4511e4
	if (entry)
Packit 4511e4
		return entry->config;
Packit 4511e4
	return NULL;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int check_submodule_name(const char *name)
Packit 4511e4
{
Packit 4511e4
	/* Disallow empty names */
Packit 4511e4
	if (!*name)
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * Look for '..' as a path component. Check both '/' and '\\' as
Packit 4511e4
	 * separators rather than is_dir_sep(), because we want the name rules
Packit 4511e4
	 * to be consistent across platforms.
Packit 4511e4
	 */
Packit 4511e4
	goto in_component; /* always start inside component */
Packit 4511e4
	while (*name) {
Packit 4511e4
		char c = *name++;
Packit 4511e4
		if (c == '/' || c == '\\') {
Packit 4511e4
in_component:
Packit 4511e4
			if (name[0] == '.' && name[1] == '.' &&
Packit 4511e4
			    (!name[2] || name[2] == '/' || name[2] == '\\'))
Packit 4511e4
				return -1;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int name_and_item_from_var(const char *var, struct strbuf *name,
Packit 4511e4
				  struct strbuf *item)
Packit 4511e4
{
Packit 4511e4
	const char *subsection, *key;
Packit 4511e4
	int subsection_len, parse;
Packit 4511e4
	parse = parse_config_key(var, "submodule", &subsection,
Packit 4511e4
			&subsection_len, &key);
Packit 4511e4
	if (parse < 0 || !subsection)
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	strbuf_add(name, subsection, subsection_len);
Packit 4511e4
	if (check_submodule_name(name->buf) < 0) {
Packit 4511e4
		warning(_("ignoring suspicious submodule name: %s"), name->buf);
Packit 4511e4
		strbuf_release(name);
Packit 4511e4
		return 0;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_addstr(item, key);
Packit 4511e4
Packit 4511e4
	return 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
Packit 4511e4
		const struct object_id *gitmodules_oid, const char *name)
Packit 4511e4
{
Packit 4511e4
	struct submodule *submodule;
Packit 4511e4
	struct strbuf name_buf = STRBUF_INIT;
Packit 4511e4
Packit 4511e4
	submodule = cache_lookup_name(cache, gitmodules_oid, name);
Packit 4511e4
	if (submodule)
Packit 4511e4
		return submodule;
Packit 4511e4
Packit 4511e4
	submodule = xmalloc(sizeof(*submodule));
Packit 4511e4
Packit 4511e4
	strbuf_addstr(&name_buf, name);
Packit 4511e4
	submodule->name = strbuf_detach(&name_buf, NULL);
Packit 4511e4
Packit 4511e4
	submodule->path = NULL;
Packit 4511e4
	submodule->url = NULL;
Packit 4511e4
	submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
Packit 4511e4
	submodule->update_strategy.command = NULL;
Packit 4511e4
	submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
Packit 4511e4
	submodule->ignore = NULL;
Packit 4511e4
	submodule->branch = NULL;
Packit 4511e4
	submodule->recommend_shallow = -1;
Packit 4511e4
Packit 4511e4
	oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
Packit 4511e4
Packit 4511e4
	cache_add(cache, submodule);
Packit 4511e4
Packit 4511e4
	return submodule;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int parse_fetch_recurse(const char *opt, const char *arg,
Packit 4511e4
			       int die_on_error)
Packit 4511e4
{
Packit 4511e4
	switch (git_parse_maybe_bool(arg)) {
Packit 4511e4
	case 1:
Packit 4511e4
		return RECURSE_SUBMODULES_ON;
Packit 4511e4
	case 0:
Packit 4511e4
		return RECURSE_SUBMODULES_OFF;
Packit 4511e4
	default:
Packit 4511e4
		if (!strcmp(arg, "on-demand"))
Packit 4511e4
			return RECURSE_SUBMODULES_ON_DEMAND;
Packit 4511e4
Packit 4511e4
		if (die_on_error)
Packit 4511e4
			die("bad %s argument: %s", opt, arg);
Packit 4511e4
		else
Packit 4511e4
			return RECURSE_SUBMODULES_ERROR;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int parse_submodule_fetchjobs(const char *var, const char *value)
Packit 4511e4
{
Packit 4511e4
	int fetchjobs = git_config_int(var, value);
Packit 4511e4
	if (fetchjobs < 0)
Packit 4511e4
		die(_("negative values not allowed for submodule.fetchjobs"));
Packit 4511e4
	return fetchjobs;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
Packit 4511e4
{
Packit 4511e4
	return parse_fetch_recurse(opt, arg, 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int option_fetch_parse_recurse_submodules(const struct option *opt,
Packit 4511e4
					  const char *arg, int unset)
Packit 4511e4
{
Packit 4511e4
	int *v;
Packit 4511e4
Packit 4511e4
	if (!opt->value)
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	v = opt->value;
Packit 4511e4
Packit 4511e4
	if (unset) {
Packit 4511e4
		*v = RECURSE_SUBMODULES_OFF;
Packit 4511e4
	} else {
Packit 4511e4
		if (arg)
Packit 4511e4
			*v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
Packit 4511e4
		else
Packit 4511e4
			*v = RECURSE_SUBMODULES_ON;
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int parse_update_recurse(const char *opt, const char *arg,
Packit 4511e4
				int die_on_error)
Packit 4511e4
{
Packit 4511e4
	switch (git_parse_maybe_bool(arg)) {
Packit 4511e4
	case 1:
Packit 4511e4
		return RECURSE_SUBMODULES_ON;
Packit 4511e4
	case 0:
Packit 4511e4
		return RECURSE_SUBMODULES_OFF;
Packit 4511e4
	default:
Packit 4511e4
		if (die_on_error)
Packit 4511e4
			die("bad %s argument: %s", opt, arg);
Packit 4511e4
		return RECURSE_SUBMODULES_ERROR;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
Packit 4511e4
{
Packit 4511e4
	return parse_update_recurse(opt, arg, 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int parse_push_recurse(const char *opt, const char *arg,
Packit 4511e4
			       int die_on_error)
Packit 4511e4
{
Packit 4511e4
	switch (git_parse_maybe_bool(arg)) {
Packit 4511e4
	case 1:
Packit 4511e4
		/* There's no simple "on" value when pushing */
Packit 4511e4
		if (die_on_error)
Packit 4511e4
			die("bad %s argument: %s", opt, arg);
Packit 4511e4
		else
Packit 4511e4
			return RECURSE_SUBMODULES_ERROR;
Packit 4511e4
	case 0:
Packit 4511e4
		return RECURSE_SUBMODULES_OFF;
Packit 4511e4
	default:
Packit 4511e4
		if (!strcmp(arg, "on-demand"))
Packit 4511e4
			return RECURSE_SUBMODULES_ON_DEMAND;
Packit 4511e4
		else if (!strcmp(arg, "check"))
Packit 4511e4
			return RECURSE_SUBMODULES_CHECK;
Packit 4511e4
		else if (!strcmp(arg, "only"))
Packit 4511e4
			return RECURSE_SUBMODULES_ONLY;
Packit 4511e4
		else if (die_on_error)
Packit 4511e4
			die("bad %s argument: %s", opt, arg);
Packit 4511e4
		else
Packit 4511e4
			return RECURSE_SUBMODULES_ERROR;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
Packit 4511e4
{
Packit 4511e4
	return parse_push_recurse(opt, arg, 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void warn_multiple_config(const struct object_id *treeish_name,
Packit 4511e4
				 const char *name, const char *option)
Packit 4511e4
{
Packit 4511e4
	const char *commit_string = "WORKTREE";
Packit 4511e4
	if (treeish_name)
Packit 4511e4
		commit_string = oid_to_hex(treeish_name);
Packit 4511e4
	warning("%s:.gitmodules, multiple configurations found for "
Packit 4511e4
			"'submodule.%s.%s'. Skipping second one!",
Packit 4511e4
			commit_string, name, option);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void warn_command_line_option(const char *var, const char *value)
Packit 4511e4
{
Packit 4511e4
	warning(_("ignoring '%s' which may be interpreted as"
Packit 4511e4
		  " a command-line option: %s"), var, value);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
struct parse_config_parameter {
Packit 4511e4
	struct submodule_cache *cache;
Packit 4511e4
	const struct object_id *treeish_name;
Packit 4511e4
	const struct object_id *gitmodules_oid;
Packit 4511e4
	int overwrite;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Parse a config item from .gitmodules.
Packit 4511e4
 *
Packit 4511e4
 * This does not handle submodule-related configuration from the main
Packit 4511e4
 * config store (.git/config, etc).  Callers are responsible for
Packit 4511e4
 * checking for overrides in the main config store when appropriate.
Packit 4511e4
 */
Packit 4511e4
static int parse_config(const char *var, const char *value, void *data)
Packit 4511e4
{
Packit 4511e4
	struct parse_config_parameter *me = data;
Packit 4511e4
	struct submodule *submodule;
Packit 4511e4
	struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
Packit 4511e4
	int ret = 0;
Packit 4511e4
Packit 4511e4
	/* this also ensures that we only parse submodule entries */
Packit 4511e4
	if (!name_and_item_from_var(var, &name, &item))
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	submodule = lookup_or_create_by_name(me->cache,
Packit 4511e4
					     me->gitmodules_oid,
Packit 4511e4
					     name.buf);
Packit 4511e4
Packit 4511e4
	if (!strcmp(item.buf, "path")) {
Packit 4511e4
		if (!value)
Packit 4511e4
			ret = config_error_nonbool(var);
Packit 4511e4
		else if (looks_like_command_line_option(value))
Packit 4511e4
			warn_command_line_option(var, value);
Packit 4511e4
		else if (!me->overwrite && submodule->path)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					"path");
Packit 4511e4
		else {
Packit 4511e4
			if (submodule->path)
Packit 4511e4
				cache_remove_path(me->cache, submodule);
Packit 4511e4
			free((void *) submodule->path);
Packit 4511e4
			submodule->path = xstrdup(value);
Packit 4511e4
			cache_put_path(me->cache, submodule);
Packit 4511e4
		}
Packit 4511e4
	} else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
Packit 4511e4
		/* when parsing worktree configurations we can die early */
Packit 4511e4
		int die_on_error = is_null_oid(me->gitmodules_oid);
Packit 4511e4
		if (!me->overwrite &&
Packit 4511e4
		    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					"fetchrecursesubmodules");
Packit 4511e4
		else
Packit 4511e4
			submodule->fetch_recurse = parse_fetch_recurse(
Packit 4511e4
								var, value,
Packit 4511e4
								die_on_error);
Packit 4511e4
	} else if (!strcmp(item.buf, "ignore")) {
Packit 4511e4
		if (!value)
Packit 4511e4
			ret = config_error_nonbool(var);
Packit 4511e4
		else if (!me->overwrite && submodule->ignore)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					"ignore");
Packit 4511e4
		else if (strcmp(value, "untracked") &&
Packit 4511e4
			 strcmp(value, "dirty") &&
Packit 4511e4
			 strcmp(value, "all") &&
Packit 4511e4
			 strcmp(value, "none"))
Packit 4511e4
			warning("Invalid parameter '%s' for config option "
Packit 4511e4
					"'submodule.%s.ignore'", value, name.buf);
Packit 4511e4
		else {
Packit 4511e4
			free((void *) submodule->ignore);
Packit 4511e4
			submodule->ignore = xstrdup(value);
Packit 4511e4
		}
Packit 4511e4
	} else if (!strcmp(item.buf, "url")) {
Packit 4511e4
		if (!value) {
Packit 4511e4
			ret = config_error_nonbool(var);
Packit 4511e4
		} else if (looks_like_command_line_option(value)) {
Packit 4511e4
			warn_command_line_option(var, value);
Packit 4511e4
		} else if (!me->overwrite && submodule->url) {
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					"url");
Packit 4511e4
		} else {
Packit 4511e4
			free((void *) submodule->url);
Packit 4511e4
			submodule->url = xstrdup(value);
Packit 4511e4
		}
Packit 4511e4
	} else if (!strcmp(item.buf, "update")) {
Packit 4511e4
		if (!value)
Packit 4511e4
			ret = config_error_nonbool(var);
Packit 4511e4
		else if (!me->overwrite &&
Packit 4511e4
			 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					     "update");
Packit 4511e4
		else if (parse_submodule_update_strategy(value,
Packit 4511e4
			 &submodule->update_strategy) < 0 ||
Packit 4511e4
			 submodule->update_strategy.type == SM_UPDATE_COMMAND)
Packit 4511e4
			die(_("invalid value for %s"), var);
Packit 4511e4
	} else if (!strcmp(item.buf, "shallow")) {
Packit 4511e4
		if (!me->overwrite && submodule->recommend_shallow != -1)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					     "shallow");
Packit 4511e4
		else
Packit 4511e4
			submodule->recommend_shallow =
Packit 4511e4
				git_config_bool(var, value);
Packit 4511e4
	} else if (!strcmp(item.buf, "branch")) {
Packit 4511e4
		if (!me->overwrite && submodule->branch)
Packit 4511e4
			warn_multiple_config(me->treeish_name, submodule->name,
Packit 4511e4
					     "branch");
Packit 4511e4
		else {
Packit 4511e4
			free((void *)submodule->branch);
Packit 4511e4
			submodule->branch = xstrdup(value);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_release(&name);
Packit 4511e4
	strbuf_release(&item);
Packit 4511e4
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
Packit 4511e4
				     struct object_id *gitmodules_oid,
Packit 4511e4
				     struct strbuf *rev)
Packit 4511e4
{
Packit 4511e4
	int ret = 0;
Packit 4511e4
Packit 4511e4
	if (is_null_oid(treeish_name)) {
Packit 4511e4
		oidclr(gitmodules_oid);
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
Packit 4511e4
	if (get_oid(rev->buf, gitmodules_oid) >= 0)
Packit 4511e4
		ret = 1;
Packit 4511e4
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/* This does a lookup of a submodule configuration by name or by path
Packit 4511e4
 * (key) with on-demand reading of the appropriate .gitmodules from
Packit 4511e4
 * revisions.
Packit 4511e4
 */
Packit 4511e4
static const struct submodule *config_from(struct submodule_cache *cache,
Packit 4511e4
		const struct object_id *treeish_name, const char *key,
Packit 4511e4
		enum lookup_type lookup_type)
Packit 4511e4
{
Packit 4511e4
	struct strbuf rev = STRBUF_INIT;
Packit 4511e4
	unsigned long config_size;
Packit 4511e4
	char *config = NULL;
Packit 4511e4
	struct object_id oid;
Packit 4511e4
	enum object_type type;
Packit 4511e4
	const struct submodule *submodule = NULL;
Packit 4511e4
	struct parse_config_parameter parameter;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * If any parameter except the cache is a NULL pointer just
Packit 4511e4
	 * return the first submodule. Can be used to check whether
Packit 4511e4
	 * there are any submodules parsed.
Packit 4511e4
	 */
Packit 4511e4
	if (!treeish_name || !key) {
Packit 4511e4
		struct hashmap_iter iter;
Packit 4511e4
		struct submodule_entry *entry;
Packit 4511e4
Packit 4511e4
		entry = hashmap_iter_first(&cache->for_name, &iter);
Packit 4511e4
		if (!entry)
Packit 4511e4
			return NULL;
Packit 4511e4
		return entry->config;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
Packit 4511e4
		goto out;
Packit 4511e4
Packit 4511e4
	switch (lookup_type) {
Packit 4511e4
	case lookup_name:
Packit 4511e4
		submodule = cache_lookup_name(cache, &oid, key);
Packit 4511e4
		break;
Packit 4511e4
	case lookup_path:
Packit 4511e4
		submodule = cache_lookup_path(cache, &oid, key);
Packit 4511e4
		break;
Packit 4511e4
	}
Packit 4511e4
	if (submodule)
Packit 4511e4
		goto out;
Packit 4511e4
Packit 4511e4
	config = read_object_file(&oid, &type, &config_size);
Packit 4511e4
	if (!config || type != OBJ_BLOB)
Packit 4511e4
		goto out;
Packit 4511e4
Packit 4511e4
	/* fill the submodule config into the cache */
Packit 4511e4
	parameter.cache = cache;
Packit 4511e4
	parameter.treeish_name = treeish_name;
Packit 4511e4
	parameter.gitmodules_oid = &oid;
Packit 4511e4
	parameter.overwrite = 0;
Packit 4511e4
	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
Packit 4511e4
			config, config_size, &parameter);
Packit 4511e4
	strbuf_release(&rev;;
Packit 4511e4
	free(config);
Packit 4511e4
Packit 4511e4
	switch (lookup_type) {
Packit 4511e4
	case lookup_name:
Packit 4511e4
		return cache_lookup_name(cache, &oid, key);
Packit 4511e4
	case lookup_path:
Packit 4511e4
		return cache_lookup_path(cache, &oid, key);
Packit 4511e4
	default:
Packit 4511e4
		return NULL;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
out:
Packit 4511e4
	strbuf_release(&rev;;
Packit 4511e4
	free(config);
Packit 4511e4
	return submodule;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void submodule_cache_check_init(struct repository *repo)
Packit 4511e4
{
Packit 4511e4
	if (repo->submodule_cache && repo->submodule_cache->initialized)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	if (!repo->submodule_cache)
Packit 4511e4
		repo->submodule_cache = submodule_cache_alloc();
Packit 4511e4
Packit 4511e4
	submodule_cache_init(repo->submodule_cache);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int gitmodules_cb(const char *var, const char *value, void *data)
Packit 4511e4
{
Packit 4511e4
	struct repository *repo = data;
Packit 4511e4
	struct parse_config_parameter parameter;
Packit 4511e4
Packit 4511e4
	parameter.cache = repo->submodule_cache;
Packit 4511e4
	parameter.treeish_name = NULL;
Packit 4511e4
	parameter.gitmodules_oid = &null_oid;
Packit 4511e4
	parameter.overwrite = 1;
Packit 4511e4
Packit 4511e4
	return parse_config(var, value, &parameter);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void repo_read_gitmodules(struct repository *repo)
Packit 4511e4
{
Packit 4511e4
	submodule_cache_check_init(repo);
Packit 4511e4
Packit 4511e4
	if (repo->worktree) {
Packit 4511e4
		char *gitmodules;
Packit 4511e4
Packit 4511e4
		if (repo_read_index(repo) < 0)
Packit 4511e4
			return;
Packit 4511e4
Packit 4511e4
		gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
Packit 4511e4
Packit 4511e4
		if (!is_gitmodules_unmerged(repo->index))
Packit 4511e4
			git_config_from_file(gitmodules_cb, gitmodules, repo);
Packit 4511e4
Packit 4511e4
		free(gitmodules);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	repo->submodule_cache->gitmodules_read = 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void gitmodules_config_oid(const struct object_id *commit_oid)
Packit 4511e4
{
Packit 4511e4
	struct strbuf rev = STRBUF_INIT;
Packit 4511e4
	struct object_id oid;
Packit 4511e4
Packit 4511e4
	submodule_cache_check_init(the_repository);
Packit 4511e4
Packit 4511e4
	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
Packit 4511e4
		git_config_from_blob_oid(gitmodules_cb, rev.buf,
Packit 4511e4
					 &oid, the_repository);
Packit 4511e4
	}
Packit 4511e4
	strbuf_release(&rev;;
Packit 4511e4
Packit 4511e4
	the_repository->submodule_cache->gitmodules_read = 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void gitmodules_read_check(struct repository *repo)
Packit 4511e4
{
Packit 4511e4
	submodule_cache_check_init(repo);
Packit 4511e4
Packit 4511e4
	/* read the repo's .gitmodules file if it hasn't been already */
Packit 4511e4
	if (!repo->submodule_cache->gitmodules_read)
Packit 4511e4
		repo_read_gitmodules(repo);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const struct submodule *submodule_from_name(struct repository *r,
Packit 4511e4
					    const struct object_id *treeish_name,
Packit 4511e4
		const char *name)
Packit 4511e4
{
Packit 4511e4
	gitmodules_read_check(r);
Packit 4511e4
	return config_from(r->submodule_cache, treeish_name, name, lookup_name);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const struct submodule *submodule_from_path(struct repository *r,
Packit 4511e4
					    const struct object_id *treeish_name,
Packit 4511e4
		const char *path)
Packit 4511e4
{
Packit 4511e4
	gitmodules_read_check(r);
Packit 4511e4
	return config_from(r->submodule_cache, treeish_name, path, lookup_path);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void submodule_free(struct repository *r)
Packit 4511e4
{
Packit 4511e4
	if (r->submodule_cache)
Packit 4511e4
		submodule_cache_clear(r->submodule_cache);
Packit 4511e4
}