submodule-config.c 20.5 KB
Newer Older
1
#include "cache.h"
2
#include "dir.h"
3
#include "repository.h"
4
#include "config.h"
5 6 7
#include "submodule-config.h"
#include "submodule.h"
#include "strbuf.h"
8
#include "object-store.h"
9
#include "parse-options.h"
10 11 12 13

/*
 * submodule cache lookup structure
 * There is one shared set of 'struct submodule' entries which can be
14
 * looked up by their sha1 blob id of the .gitmodules file and either
15 16 17 18 19 20 21
 * 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;
22
	unsigned initialized:1;
23
	unsigned gitmodules_read:1;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
};

/*
 * 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
};

40
static int config_path_cmp(const void *unused_cmp_data,
41 42
			   const void *entry,
			   const void *entry_or_key,
43
			   const void *unused_keydata)
44
{
45 46 47
	const struct submodule_entry *a = entry;
	const struct submodule_entry *b = entry_or_key;

48
	return strcmp(a->config->path, b->config->path) ||
49
	       !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
50 51
}

52
static int config_name_cmp(const void *unused_cmp_data,
53 54
			   const void *entry,
			   const void *entry_or_key,
55
			   const void *unused_keydata)
56
{
57 58 59
	const struct submodule_entry *a = entry;
	const struct submodule_entry *b = entry_or_key;

60
	return strcmp(a->config->name, b->config->name) ||
61
	       !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
62 63
}

64 65 66 67 68 69
static struct submodule_cache *submodule_cache_alloc(void)
{
	return xcalloc(1, sizeof(struct submodule_cache));
}

static void submodule_cache_init(struct submodule_cache *cache)
70
{
71 72
	hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
	hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
73
	cache->initialized = 1;
74 75 76 77 78 79
}

static void free_one_config(struct submodule_entry *entry)
{
	free((void *) entry->config->path);
	free((void *) entry->config->name);
80
	free((void *) entry->config->branch);
81
	free((void *) entry->config->update_strategy.command);
82 83 84
	free(entry->config);
}

85
static void submodule_cache_clear(struct submodule_cache *cache)
86 87 88 89
{
	struct hashmap_iter iter;
	struct submodule_entry *entry;

90 91 92
	if (!cache->initialized)
		return;

93 94 95
	/*
	 * We iterate over the name hash here to be symmetric with the
	 * allocation of struct submodule entries. Each is allocated by
96
	 * their .gitmodules blob sha1 and submodule name.
97 98 99 100 101 102 103
	 */
	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);
104
	cache->initialized = 0;
105
	cache->gitmodules_read = 0;
106 107 108 109 110 111
}

void submodule_cache_free(struct submodule_cache *cache)
{
	submodule_cache_clear(cache);
	free(cache);
112 113
}

114 115
static unsigned int hash_oid_string(const struct object_id *oid,
				    const char *string)
116
{
117
	return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
118 119 120 121 122
}

static void cache_put_path(struct submodule_cache *cache,
			   struct submodule *submodule)
{
123 124
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
					    submodule->path);
125 126 127 128 129 130 131 132 133
	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)
{
134 135
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
					    submodule->path);
136 137 138 139 140 141 142 143 144 145 146
	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)
{
147 148
	unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
					    submodule->name);
149 150 151 152 153 154 155
	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,
156
		const struct object_id *gitmodules_oid, const char *path)
157 158
{
	struct submodule_entry *entry;
159
	unsigned int hash = hash_oid_string(gitmodules_oid, path);
160 161 162
	struct submodule_entry key;
	struct submodule key_config;

163
	oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
164 165 166 167 168 169 170 171 172 173 174 175
	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,
176
		const struct object_id *gitmodules_oid, const char *name)
177 178
{
	struct submodule_entry *entry;
179
	unsigned int hash = hash_oid_string(gitmodules_oid, name);
180 181 182
	struct submodule_entry key;
	struct submodule key_config;

183
	oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
184 185 186 187 188 189 190 191 192 193 194
	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;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
int check_submodule_name(const char *name)
{
	/* Disallow empty names */
	if (!*name)
		return -1;

	/*
	 * Look for '..' as a path component. Check both '/' and '\\' as
	 * separators rather than is_dir_sep(), because we want the name rules
	 * to be consistent across platforms.
	 */
	goto in_component; /* always start inside component */
	while (*name) {
		char c = *name++;
		if (c == '/' || c == '\\') {
in_component:
			if (name[0] == '.' && name[1] == '.' &&
			    (!name[2] || name[2] == '/' || name[2] == '\\'))
				return -1;
		}
	}

	return 0;
}

220 221 222 223 224 225 226 227 228 229 230
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);
231 232 233 234 235 236
	if (check_submodule_name(name->buf) < 0) {
		warning(_("ignoring suspicious submodule name: %s"), name->buf);
		strbuf_release(name);
		return 0;
	}

237 238 239 240 241 242
	strbuf_addstr(item, key);

	return 1;
}

static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
243
		const struct object_id *gitmodules_oid, const char *name)
244 245 246 247
{
	struct submodule *submodule;
	struct strbuf name_buf = STRBUF_INIT;

248
	submodule = cache_lookup_name(cache, gitmodules_oid, name);
249 250 251 252 253 254 255 256 257 258
	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;
259 260
	submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
	submodule->update_strategy.command = NULL;
261 262
	submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
	submodule->ignore = NULL;
263
	submodule->branch = NULL;
264
	submodule->recommend_shallow = -1;
265

266
	oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
267 268 269 270 271 272

	cache_add(cache, submodule);

	return submodule;
}

273 274 275
static int parse_fetch_recurse(const char *opt, const char *arg,
			       int die_on_error)
{
276
	switch (git_parse_maybe_bool(arg)) {
277 278 279 280 281 282 283
	case 1:
		return RECURSE_SUBMODULES_ON;
	case 0:
		return RECURSE_SUBMODULES_OFF;
	default:
		if (!strcmp(arg, "on-demand"))
			return RECURSE_SUBMODULES_ON_DEMAND;
284 285 286 287
		/*
		 * Please update $__git_fetch_recurse_submodules in
		 * git-completion.bash when you add new options.
		 */
288 289 290 291 292 293 294
		if (die_on_error)
			die("bad %s argument: %s", opt, arg);
		else
			return RECURSE_SUBMODULES_ERROR;
	}
}

295 296 297 298 299 300 301 302
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;
}

303 304 305 306 307
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
{
	return parse_fetch_recurse(opt, arg, 1);
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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;
}

329 330 331
static int parse_update_recurse(const char *opt, const char *arg,
				int die_on_error)
{
332
	switch (git_parse_maybe_bool(arg)) {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	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);
}

349 350 351
static int parse_push_recurse(const char *opt, const char *arg,
			       int die_on_error)
{
352
	switch (git_parse_maybe_bool(arg)) {
353 354 355 356 357 358 359 360 361 362 363 364 365
	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;
366 367
		else if (!strcmp(arg, "only"))
			return RECURSE_SUBMODULES_ONLY;
368 369 370 371
		/*
		 * Please update $__git_push_recurse_submodules in
		 * git-completion.bash when you add new modes.
		 */
372 373 374 375 376 377 378 379 380 381 382 383
		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);
}

384
static void warn_multiple_config(const struct object_id *treeish_name,
385 386 387
				 const char *name, const char *option)
{
	const char *commit_string = "WORKTREE";
388
	if (treeish_name)
389
		commit_string = oid_to_hex(treeish_name);
390 391 392 393 394
	warning("%s:.gitmodules, multiple configurations found for "
			"'submodule.%s.%s'. Skipping second one!",
			commit_string, name, option);
}

395 396 397 398 399 400
static void warn_command_line_option(const char *var, const char *value)
{
	warning(_("ignoring '%s' which may be interpreted as"
		  " a command-line option: %s"), var, value);
}

401 402
struct parse_config_parameter {
	struct submodule_cache *cache;
403 404
	const struct object_id *treeish_name;
	const struct object_id *gitmodules_oid;
405 406 407 408 409 410 411 412 413 414 415 416 417 418
	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;

419
	submodule = lookup_or_create_by_name(me->cache,
420
					     me->gitmodules_oid,
421
					     name.buf);
422 423

	if (!strcmp(item.buf, "path")) {
424
		if (!value)
425
			ret = config_error_nonbool(var);
426 427
		else if (looks_like_command_line_option(value))
			warn_command_line_option(var, value);
428
		else if (!me->overwrite && submodule->path)
429
			warn_multiple_config(me->treeish_name, submodule->name,
430
					"path");
431 432 433 434 435 436
		else {
			if (submodule->path)
				cache_remove_path(me->cache, submodule);
			free((void *) submodule->path);
			submodule->path = xstrdup(value);
			cache_put_path(me->cache, submodule);
437 438
		}
	} else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
439
		/* when parsing worktree configurations we can die early */
440
		int die_on_error = is_null_oid(me->gitmodules_oid);
441
		if (!me->overwrite &&
442
		    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
443
			warn_multiple_config(me->treeish_name, submodule->name,
444
					"fetchrecursesubmodules");
445 446 447
		else
			submodule->fetch_recurse = parse_fetch_recurse(
								var, value,
448
								die_on_error);
449
	} else if (!strcmp(item.buf, "ignore")) {
450 451
		if (!value)
			ret = config_error_nonbool(var);
452
		else if (!me->overwrite && submodule->ignore)
453
			warn_multiple_config(me->treeish_name, submodule->name,
454
					"ignore");
455 456 457 458
		else if (strcmp(value, "untracked") &&
			 strcmp(value, "dirty") &&
			 strcmp(value, "all") &&
			 strcmp(value, "none"))
459
			warning("Invalid parameter '%s' for config option "
460
					"'submodule.%s.ignore'", value, name.buf);
461 462 463
		else {
			free((void *) submodule->ignore);
			submodule->ignore = xstrdup(value);
464 465 466 467
		}
	} else if (!strcmp(item.buf, "url")) {
		if (!value) {
			ret = config_error_nonbool(var);
468 469
		} else if (looks_like_command_line_option(value)) {
			warn_command_line_option(var, value);
470
		} else if (!me->overwrite && submodule->url) {
471
			warn_multiple_config(me->treeish_name, submodule->name,
472
					"url");
473 474 475
		} else {
			free((void *) submodule->url);
			submodule->url = xstrdup(value);
476
		}
477 478 479 480 481
	} else if (!strcmp(item.buf, "update")) {
		if (!value)
			ret = config_error_nonbool(var);
		else if (!me->overwrite &&
			 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
482
			warn_multiple_config(me->treeish_name, submodule->name,
483 484 485 486
					     "update");
		else if (parse_submodule_update_strategy(value,
			 &submodule->update_strategy) < 0)
				die(_("invalid value for %s"), var);
487 488
	} else if (!strcmp(item.buf, "shallow")) {
		if (!me->overwrite && submodule->recommend_shallow != -1)
489
			warn_multiple_config(me->treeish_name, submodule->name,
490
					     "shallow");
491
		else
492 493
			submodule->recommend_shallow =
				git_config_bool(var, value);
494 495
	} else if (!strcmp(item.buf, "branch")) {
		if (!me->overwrite && submodule->branch)
496
			warn_multiple_config(me->treeish_name, submodule->name,
497 498 499 500
					     "branch");
		else {
			free((void *)submodule->branch);
			submodule->branch = xstrdup(value);
501
		}
502 503 504 505 506 507 508 509
	}

	strbuf_release(&name);
	strbuf_release(&item);

	return ret;
}

510 511 512
static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
				     struct object_id *gitmodules_oid,
				     struct strbuf *rev)
513 514 515
{
	int ret = 0;

516 517
	if (is_null_oid(treeish_name)) {
		oidclr(gitmodules_oid);
518 519 520
		return 1;
	}

521 522
	strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
	if (get_oid(rev->buf, gitmodules_oid) >= 0)
523 524 525 526 527 528 529 530 531 532
		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,
533
		const struct object_id *treeish_name, const char *key,
534 535 536 537
		enum lookup_type lookup_type)
{
	struct strbuf rev = STRBUF_INIT;
	unsigned long config_size;
538
	char *config = NULL;
539
	struct object_id oid;
540 541 542 543 544 545 546 547 548
	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.
	 */
549
	if (!treeish_name || !key) {
550 551 552
		struct hashmap_iter iter;
		struct submodule_entry *entry;

553
		entry = hashmap_iter_first(&cache->for_name, &iter);
554 555 556 557 558
		if (!entry)
			return NULL;
		return entry->config;
	}

559
	if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
560
		goto out;
561 562 563

	switch (lookup_type) {
	case lookup_name:
564
		submodule = cache_lookup_name(cache, &oid, key);
565 566
		break;
	case lookup_path:
567
		submodule = cache_lookup_path(cache, &oid, key);
568 569 570
		break;
	}
	if (submodule)
571
		goto out;
572

573
	config = read_object_file(&oid, &type, &config_size);
574 575
	if (!config || type != OBJ_BLOB)
		goto out;
576 577 578

	/* fill the submodule config into the cache */
	parameter.cache = cache;
579 580
	parameter.treeish_name = treeish_name;
	parameter.gitmodules_oid = &oid;
581
	parameter.overwrite = 0;
582
	git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
583
			config, config_size, &parameter, NULL);
584
	strbuf_release(&rev);
585 586 587 588
	free(config);

	switch (lookup_type) {
	case lookup_name:
589
		return cache_lookup_name(cache, &oid, key);
590
	case lookup_path:
591
		return cache_lookup_path(cache, &oid, key);
592 593 594
	default:
		return NULL;
	}
595 596 597 598 599

out:
	strbuf_release(&rev);
	free(config);
	return submodule;
600 601
}

602
static void submodule_cache_check_init(struct repository *repo)
603
{
604
	if (repo->submodule_cache && repo->submodule_cache->initialized)
605 606
		return;

607 608 609 610
	if (!repo->submodule_cache)
		repo->submodule_cache = submodule_cache_alloc();

	submodule_cache_init(repo->submodule_cache);
611 612
}

613 614 615 616 617 618 619 620 621 622 623
/*
 * Note: This function is private for a reason, the '.gitmodules' file should
 * not be used as as a mechanism to retrieve arbitrary configuration stored in
 * the repository.
 *
 * Runs the provided config function on the '.gitmodules' file found in the
 * working directory.
 */
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
{
	if (repo->worktree) {
624 625 626 627
		struct git_config_source config_source = { 0 };
		const struct config_options opts = { 0 };
		struct object_id oid;
		char *file;
628
		char *oidstr = NULL;
629 630 631 632

		file = repo_worktree_path(repo, GITMODULES_FILE);
		if (file_exists(file)) {
			config_source.file = file;
633 634 635 636 637
		} else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
			   repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
			config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
			if (repo != the_repository)
				add_to_alternates_memory(repo->objects->odb->path);
638 639 640 641 642 643 644
		} else {
			goto out;
		}

		config_with_options(fn, data, &config_source, &opts);

out:
645
		free(oidstr);
646 647 648 649
		free(file);
	}
}

650
static int gitmodules_cb(const char *var, const char *value, void *data)
651
{
652
	struct repository *repo = data;
653
	struct parse_config_parameter parameter;
654 655

	parameter.cache = repo->submodule_cache;
656
	parameter.treeish_name = NULL;
657
	parameter.gitmodules_oid = &null_oid;
658 659 660 661 662
	parameter.overwrite = 1;

	return parse_config(var, value, &parameter);
}

663
void repo_read_gitmodules(struct repository *repo)
664
{
665 666
	submodule_cache_check_init(repo);

667 668
	if (repo_read_index(repo) < 0)
		return;
669

670 671
	if (!is_gitmodules_unmerged(repo->index))
		config_from_gitmodules(gitmodules_cb, repo, repo);
672 673

	repo->submodule_cache->gitmodules_read = 1;
674 675 676 677 678 679 680
}

void gitmodules_config_oid(const struct object_id *commit_oid)
{
	struct strbuf rev = STRBUF_INIT;
	struct object_id oid;

681 682
	submodule_cache_check_init(the_repository);

683 684 685 686 687
	if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
		git_config_from_blob_oid(gitmodules_cb, rev.buf,
					 &oid, the_repository);
	}
	strbuf_release(&rev);
688 689 690 691 692 693 694 695 696 697 698

	the_repository->submodule_cache->gitmodules_read = 1;
}

static void gitmodules_read_check(struct repository *repo)
{
	submodule_cache_check_init(repo);

	/* read the repo's .gitmodules file if it hasn't been already */
	if (!repo->submodule_cache->gitmodules_read)
		repo_read_gitmodules(repo);
699 700
}

701 702
const struct submodule *submodule_from_name(struct repository *r,
					    const struct object_id *treeish_name,
703 704
		const char *name)
{
705 706
	gitmodules_read_check(r);
	return config_from(r->submodule_cache, treeish_name, name, lookup_name);
707 708
}

709 710
const struct submodule *submodule_from_path(struct repository *r,
					    const struct object_id *treeish_name,
711 712
		const char *path)
{
713 714
	gitmodules_read_check(r);
	return config_from(r->submodule_cache, treeish_name, path, lookup_path);
715 716
}

717
void submodule_free(struct repository *r)
718
{
719 720
	if (r->submodule_cache)
		submodule_cache_clear(r->submodule_cache);
721
}
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static int config_print_callback(const char *var, const char *value, void *cb_data)
{
	char *wanted_key = cb_data;

	if (!strcmp(wanted_key, var))
		printf("%s\n", value);

	return 0;
}

int print_config_from_gitmodules(struct repository *repo, const char *key)
{
	int ret;
	char *store_key;

	ret = git_config_parse_key(key, &store_key, NULL);
	if (ret < 0)
		return CONFIG_INVALID_KEY;

	config_from_gitmodules(config_print_callback, repo, store_key);

	free(store_key);
	return 0;
}

748 749 750 751 752 753 754 755 756 757 758 759
int config_set_in_gitmodules_file_gently(const char *key, const char *value)
{
	int ret;

	ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
	if (ret < 0)
		/* Maybe the user already did that, don't error out here */
		warning(_("Could not update .gitmodules entry %s"), key);

	return ret;
}

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
struct fetch_config {
	int *max_children;
	int *recurse_submodules;
};

static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
{
	struct fetch_config *config = cb;
	if (!strcmp(var, "submodule.fetchjobs")) {
		*(config->max_children) = parse_submodule_fetchjobs(var, value);
		return 0;
	} else if (!strcmp(var, "fetch.recursesubmodules")) {
		*(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
		return 0;
	}

	return 0;
}

void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
{
	struct fetch_config config = {
		.max_children = max_children,
		.recurse_submodules = recurse_submodules
	};
785
	config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
786
}
787 788 789 790 791 792 793 794 795 796 797 798

static int gitmodules_update_clone_config(const char *var, const char *value,
					  void *cb)
{
	int *max_jobs = cb;
	if (!strcmp(var, "submodule.fetchjobs"))
		*max_jobs = parse_submodule_fetchjobs(var, value);
	return 0;
}

void update_clone_config_from_gitmodules(int *max_jobs)
{
799
	config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
800
}