Merge branch 'sg/index-format-doc-update' into maint
[git/debian.git] / submodule-config.c
blobc2ac7e7bf39b2eb9c273a11fb457b0dbc7369aa6
1 #include "cache.h"
2 #include "dir.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "submodule-config.h"
6 #include "submodule.h"
7 #include "strbuf.h"
8 #include "object-store.h"
9 #include "parse-options.h"
10 #include "tree-walk.h"
13 * submodule cache lookup structure
14 * There is one shared set of 'struct submodule' entries which can be
15 * looked up by their sha1 blob id of the .gitmodules file and either
16 * using path or name as key.
17 * for_path stores submodule entries with path as key
18 * for_name stores submodule entries with name as key
20 struct submodule_cache {
21 struct hashmap for_path;
22 struct hashmap for_name;
23 unsigned initialized:1;
24 unsigned gitmodules_read:1;
28 * thin wrapper struct needed to insert 'struct submodule' entries to
29 * the hashmap
31 struct submodule_entry {
32 struct hashmap_entry ent;
33 struct submodule *config;
36 enum lookup_type {
37 lookup_name,
38 lookup_path
41 static int config_path_cmp(const void *unused_cmp_data,
42 const struct hashmap_entry *eptr,
43 const struct hashmap_entry *entry_or_key,
44 const void *unused_keydata)
46 const struct submodule_entry *a, *b;
48 a = container_of(eptr, const struct submodule_entry, ent);
49 b = container_of(entry_or_key, const struct submodule_entry, ent);
51 return strcmp(a->config->path, b->config->path) ||
52 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
55 static int config_name_cmp(const void *unused_cmp_data,
56 const struct hashmap_entry *eptr,
57 const struct hashmap_entry *entry_or_key,
58 const void *unused_keydata)
60 const struct submodule_entry *a, *b;
62 a = container_of(eptr, const struct submodule_entry, ent);
63 b = container_of(entry_or_key, const struct submodule_entry, ent);
65 return strcmp(a->config->name, b->config->name) ||
66 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
69 static struct submodule_cache *submodule_cache_alloc(void)
71 return xcalloc(1, sizeof(struct submodule_cache));
74 static void submodule_cache_init(struct submodule_cache *cache)
76 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
77 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
78 cache->initialized = 1;
81 static void free_one_config(struct submodule_entry *entry)
83 free((void *) entry->config->path);
84 free((void *) entry->config->name);
85 free((void *) entry->config->branch);
86 free((void *) entry->config->update_strategy.command);
87 free(entry->config);
90 static void submodule_cache_clear(struct submodule_cache *cache)
92 struct hashmap_iter iter;
93 struct submodule_entry *entry;
95 if (!cache->initialized)
96 return;
99 * We iterate over the name hash here to be symmetric with the
100 * allocation of struct submodule entries. Each is allocated by
101 * their .gitmodules blob sha1 and submodule name.
103 hashmap_for_each_entry(&cache->for_name, &iter, entry,
104 ent /* member name */)
105 free_one_config(entry);
107 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
108 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
109 cache->initialized = 0;
110 cache->gitmodules_read = 0;
113 void submodule_cache_free(struct submodule_cache *cache)
115 submodule_cache_clear(cache);
116 free(cache);
119 static unsigned int hash_oid_string(const struct object_id *oid,
120 const char *string)
122 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
125 static void cache_put_path(struct submodule_cache *cache,
126 struct submodule *submodule)
128 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
129 submodule->path);
130 struct submodule_entry *e = xmalloc(sizeof(*e));
131 hashmap_entry_init(&e->ent, hash);
132 e->config = submodule;
133 hashmap_put(&cache->for_path, &e->ent);
136 static void cache_remove_path(struct submodule_cache *cache,
137 struct submodule *submodule)
139 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
140 submodule->path);
141 struct submodule_entry e;
142 struct submodule_entry *removed;
143 hashmap_entry_init(&e.ent, hash);
144 e.config = submodule;
145 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
146 free(removed);
149 static void cache_add(struct submodule_cache *cache,
150 struct submodule *submodule)
152 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
153 submodule->name);
154 struct submodule_entry *e = xmalloc(sizeof(*e));
155 hashmap_entry_init(&e->ent, hash);
156 e->config = submodule;
157 hashmap_add(&cache->for_name, &e->ent);
160 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
161 const struct object_id *gitmodules_oid, const char *path)
163 struct submodule_entry *entry;
164 unsigned int hash = hash_oid_string(gitmodules_oid, path);
165 struct submodule_entry key;
166 struct submodule key_config;
168 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
169 key_config.path = path;
171 hashmap_entry_init(&key.ent, hash);
172 key.config = &key_config;
174 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
175 if (entry)
176 return entry->config;
177 return NULL;
180 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
181 const struct object_id *gitmodules_oid, const char *name)
183 struct submodule_entry *entry;
184 unsigned int hash = hash_oid_string(gitmodules_oid, name);
185 struct submodule_entry key;
186 struct submodule key_config;
188 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
189 key_config.name = name;
191 hashmap_entry_init(&key.ent, hash);
192 key.config = &key_config;
194 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
195 if (entry)
196 return entry->config;
197 return NULL;
200 int check_submodule_name(const char *name)
202 /* Disallow empty names */
203 if (!*name)
204 return -1;
207 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
208 * separators rather than is_dir_sep(), because we want the name rules
209 * to be consistent across platforms.
211 goto in_component; /* always start inside component */
212 while (*name) {
213 char c = *name++;
214 if (is_xplatform_dir_sep(c)) {
215 in_component:
216 if (name[0] == '.' && name[1] == '.' &&
217 (!name[2] || is_xplatform_dir_sep(name[2])))
218 return -1;
222 return 0;
225 static int name_and_item_from_var(const char *var, struct strbuf *name,
226 struct strbuf *item)
228 const char *subsection, *key;
229 size_t subsection_len;
230 int parse;
231 parse = parse_config_key(var, "submodule", &subsection,
232 &subsection_len, &key);
233 if (parse < 0 || !subsection)
234 return 0;
236 strbuf_add(name, subsection, subsection_len);
237 if (check_submodule_name(name->buf) < 0) {
238 warning(_("ignoring suspicious submodule name: %s"), name->buf);
239 strbuf_release(name);
240 return 0;
243 strbuf_addstr(item, key);
245 return 1;
248 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
249 const struct object_id *gitmodules_oid, const char *name)
251 struct submodule *submodule;
252 struct strbuf name_buf = STRBUF_INIT;
254 submodule = cache_lookup_name(cache, gitmodules_oid, name);
255 if (submodule)
256 return submodule;
258 submodule = xmalloc(sizeof(*submodule));
260 strbuf_addstr(&name_buf, name);
261 submodule->name = strbuf_detach(&name_buf, NULL);
263 submodule->path = NULL;
264 submodule->url = NULL;
265 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
266 submodule->update_strategy.command = NULL;
267 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
268 submodule->ignore = NULL;
269 submodule->branch = NULL;
270 submodule->recommend_shallow = -1;
272 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
274 cache_add(cache, submodule);
276 return submodule;
279 static int parse_fetch_recurse(const char *opt, const char *arg,
280 int die_on_error)
282 switch (git_parse_maybe_bool(arg)) {
283 case 1:
284 return RECURSE_SUBMODULES_ON;
285 case 0:
286 return RECURSE_SUBMODULES_OFF;
287 default:
288 if (!strcmp(arg, "on-demand"))
289 return RECURSE_SUBMODULES_ON_DEMAND;
291 * Please update $__git_fetch_recurse_submodules in
292 * git-completion.bash when you add new options.
294 if (die_on_error)
295 die("bad %s argument: %s", opt, arg);
296 else
297 return RECURSE_SUBMODULES_ERROR;
301 int parse_submodule_fetchjobs(const char *var, const char *value)
303 int fetchjobs = git_config_int(var, value);
304 if (fetchjobs < 0)
305 die(_("negative values not allowed for submodule.fetchJobs"));
306 return fetchjobs;
309 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
311 return parse_fetch_recurse(opt, arg, 1);
314 int option_fetch_parse_recurse_submodules(const struct option *opt,
315 const char *arg, int unset)
317 int *v;
319 if (!opt->value)
320 return -1;
322 v = opt->value;
324 if (unset) {
325 *v = RECURSE_SUBMODULES_OFF;
326 } else {
327 if (arg)
328 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
329 else
330 *v = RECURSE_SUBMODULES_ON;
332 return 0;
335 static int parse_update_recurse(const char *opt, const char *arg,
336 int die_on_error)
338 switch (git_parse_maybe_bool(arg)) {
339 case 1:
340 return RECURSE_SUBMODULES_ON;
341 case 0:
342 return RECURSE_SUBMODULES_OFF;
343 default:
344 if (die_on_error)
345 die("bad %s argument: %s", opt, arg);
346 return RECURSE_SUBMODULES_ERROR;
350 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
352 return parse_update_recurse(opt, arg, 1);
355 static int parse_push_recurse(const char *opt, const char *arg,
356 int die_on_error)
358 switch (git_parse_maybe_bool(arg)) {
359 case 1:
360 /* There's no simple "on" value when pushing */
361 if (die_on_error)
362 die("bad %s argument: %s", opt, arg);
363 else
364 return RECURSE_SUBMODULES_ERROR;
365 case 0:
366 return RECURSE_SUBMODULES_OFF;
367 default:
368 if (!strcmp(arg, "on-demand"))
369 return RECURSE_SUBMODULES_ON_DEMAND;
370 else if (!strcmp(arg, "check"))
371 return RECURSE_SUBMODULES_CHECK;
372 else if (!strcmp(arg, "only"))
373 return RECURSE_SUBMODULES_ONLY;
375 * Please update $__git_push_recurse_submodules in
376 * git-completion.bash when you add new modes.
378 else if (die_on_error)
379 die("bad %s argument: %s", opt, arg);
380 else
381 return RECURSE_SUBMODULES_ERROR;
385 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
387 return parse_push_recurse(opt, arg, 1);
390 static void warn_multiple_config(const struct object_id *treeish_name,
391 const char *name, const char *option)
393 const char *commit_string = "WORKTREE";
394 if (treeish_name)
395 commit_string = oid_to_hex(treeish_name);
396 warning("%s:.gitmodules, multiple configurations found for "
397 "'submodule.%s.%s'. Skipping second one!",
398 commit_string, name, option);
401 static void warn_command_line_option(const char *var, const char *value)
403 warning(_("ignoring '%s' which may be interpreted as"
404 " a command-line option: %s"), var, value);
407 struct parse_config_parameter {
408 struct submodule_cache *cache;
409 const struct object_id *treeish_name;
410 const struct object_id *gitmodules_oid;
411 int overwrite;
415 * Parse a config item from .gitmodules.
417 * This does not handle submodule-related configuration from the main
418 * config store (.git/config, etc). Callers are responsible for
419 * checking for overrides in the main config store when appropriate.
421 static int parse_config(const char *var, const char *value, void *data)
423 struct parse_config_parameter *me = data;
424 struct submodule *submodule;
425 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
426 int ret = 0;
428 /* this also ensures that we only parse submodule entries */
429 if (!name_and_item_from_var(var, &name, &item))
430 return 0;
432 submodule = lookup_or_create_by_name(me->cache,
433 me->gitmodules_oid,
434 name.buf);
436 if (!strcmp(item.buf, "path")) {
437 if (!value)
438 ret = config_error_nonbool(var);
439 else if (looks_like_command_line_option(value))
440 warn_command_line_option(var, value);
441 else if (!me->overwrite && submodule->path)
442 warn_multiple_config(me->treeish_name, submodule->name,
443 "path");
444 else {
445 if (submodule->path)
446 cache_remove_path(me->cache, submodule);
447 free((void *) submodule->path);
448 submodule->path = xstrdup(value);
449 cache_put_path(me->cache, submodule);
451 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
452 /* when parsing worktree configurations we can die early */
453 int die_on_error = is_null_oid(me->gitmodules_oid);
454 if (!me->overwrite &&
455 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
456 warn_multiple_config(me->treeish_name, submodule->name,
457 "fetchrecursesubmodules");
458 else
459 submodule->fetch_recurse = parse_fetch_recurse(
460 var, value,
461 die_on_error);
462 } else if (!strcmp(item.buf, "ignore")) {
463 if (!value)
464 ret = config_error_nonbool(var);
465 else if (!me->overwrite && submodule->ignore)
466 warn_multiple_config(me->treeish_name, submodule->name,
467 "ignore");
468 else if (strcmp(value, "untracked") &&
469 strcmp(value, "dirty") &&
470 strcmp(value, "all") &&
471 strcmp(value, "none"))
472 warning("Invalid parameter '%s' for config option "
473 "'submodule.%s.ignore'", value, name.buf);
474 else {
475 free((void *) submodule->ignore);
476 submodule->ignore = xstrdup(value);
478 } else if (!strcmp(item.buf, "url")) {
479 if (!value) {
480 ret = config_error_nonbool(var);
481 } else if (looks_like_command_line_option(value)) {
482 warn_command_line_option(var, value);
483 } else if (!me->overwrite && submodule->url) {
484 warn_multiple_config(me->treeish_name, submodule->name,
485 "url");
486 } else {
487 free((void *) submodule->url);
488 submodule->url = xstrdup(value);
490 } else if (!strcmp(item.buf, "update")) {
491 if (!value)
492 ret = config_error_nonbool(var);
493 else if (!me->overwrite &&
494 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
495 warn_multiple_config(me->treeish_name, submodule->name,
496 "update");
497 else if (parse_submodule_update_strategy(value,
498 &submodule->update_strategy) < 0 ||
499 submodule->update_strategy.type == SM_UPDATE_COMMAND)
500 die(_("invalid value for '%s'"), var);
501 } else if (!strcmp(item.buf, "shallow")) {
502 if (!me->overwrite && submodule->recommend_shallow != -1)
503 warn_multiple_config(me->treeish_name, submodule->name,
504 "shallow");
505 else
506 submodule->recommend_shallow =
507 git_config_bool(var, value);
508 } else if (!strcmp(item.buf, "branch")) {
509 if (!me->overwrite && submodule->branch)
510 warn_multiple_config(me->treeish_name, submodule->name,
511 "branch");
512 else {
513 free((void *)submodule->branch);
514 submodule->branch = xstrdup(value);
518 strbuf_release(&name);
519 strbuf_release(&item);
521 return ret;
524 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
525 struct object_id *gitmodules_oid,
526 struct strbuf *rev)
528 int ret = 0;
530 if (is_null_oid(treeish_name)) {
531 oidclr(gitmodules_oid);
532 return 1;
535 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
536 if (get_oid(rev->buf, gitmodules_oid) >= 0)
537 ret = 1;
539 return ret;
542 /* This does a lookup of a submodule configuration by name or by path
543 * (key) with on-demand reading of the appropriate .gitmodules from
544 * revisions.
546 static const struct submodule *config_from(struct submodule_cache *cache,
547 const struct object_id *treeish_name, const char *key,
548 enum lookup_type lookup_type)
550 struct strbuf rev = STRBUF_INIT;
551 unsigned long config_size;
552 char *config = NULL;
553 struct object_id oid;
554 enum object_type type;
555 const struct submodule *submodule = NULL;
556 struct parse_config_parameter parameter;
559 * If any parameter except the cache is a NULL pointer just
560 * return the first submodule. Can be used to check whether
561 * there are any submodules parsed.
563 if (!treeish_name || !key) {
564 struct hashmap_iter iter;
565 struct submodule_entry *entry;
567 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
568 struct submodule_entry,
569 ent /* member name */);
570 if (!entry)
571 return NULL;
572 return entry->config;
575 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
576 goto out;
578 switch (lookup_type) {
579 case lookup_name:
580 submodule = cache_lookup_name(cache, &oid, key);
581 break;
582 case lookup_path:
583 submodule = cache_lookup_path(cache, &oid, key);
584 break;
586 if (submodule)
587 goto out;
589 config = read_object_file(&oid, &type, &config_size);
590 if (!config || type != OBJ_BLOB)
591 goto out;
593 /* fill the submodule config into the cache */
594 parameter.cache = cache;
595 parameter.treeish_name = treeish_name;
596 parameter.gitmodules_oid = &oid;
597 parameter.overwrite = 0;
598 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
599 config, config_size, &parameter, NULL);
600 strbuf_release(&rev);
601 free(config);
603 switch (lookup_type) {
604 case lookup_name:
605 return cache_lookup_name(cache, &oid, key);
606 case lookup_path:
607 return cache_lookup_path(cache, &oid, key);
608 default:
609 return NULL;
612 out:
613 strbuf_release(&rev);
614 free(config);
615 return submodule;
618 static void submodule_cache_check_init(struct repository *repo)
620 if (repo->submodule_cache && repo->submodule_cache->initialized)
621 return;
623 if (!repo->submodule_cache)
624 repo->submodule_cache = submodule_cache_alloc();
626 submodule_cache_init(repo->submodule_cache);
630 * Note: This function is private for a reason, the '.gitmodules' file should
631 * not be used as a mechanism to retrieve arbitrary configuration stored in
632 * the repository.
634 * Runs the provided config function on the '.gitmodules' file found in the
635 * working directory.
637 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
639 if (repo->worktree) {
640 struct git_config_source config_source = {
641 0, .scope = CONFIG_SCOPE_SUBMODULE
643 const struct config_options opts = { 0 };
644 struct object_id oid;
645 char *file;
646 char *oidstr = NULL;
648 file = repo_worktree_path(repo, GITMODULES_FILE);
649 if (file_exists(file)) {
650 config_source.file = file;
651 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
652 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
653 config_source.repo = repo;
654 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
655 if (repo != the_repository)
656 add_submodule_odb_by_path(repo->objects->odb->path);
657 } else {
658 goto out;
661 config_with_options(fn, data, &config_source, &opts);
663 out:
664 free(oidstr);
665 free(file);
669 static int gitmodules_cb(const char *var, const char *value, void *data)
671 struct repository *repo = data;
672 struct parse_config_parameter parameter;
674 parameter.cache = repo->submodule_cache;
675 parameter.treeish_name = NULL;
676 parameter.gitmodules_oid = null_oid();
677 parameter.overwrite = 1;
679 return parse_config(var, value, &parameter);
682 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
684 submodule_cache_check_init(repo);
686 if (repo->submodule_cache->gitmodules_read && skip_if_read)
687 return;
689 if (repo_read_index(repo) < 0)
690 return;
692 if (!is_gitmodules_unmerged(repo->index))
693 config_from_gitmodules(gitmodules_cb, repo, repo);
695 repo->submodule_cache->gitmodules_read = 1;
698 void gitmodules_config_oid(const struct object_id *commit_oid)
700 struct strbuf rev = STRBUF_INIT;
701 struct object_id oid;
703 submodule_cache_check_init(the_repository);
705 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
706 git_config_from_blob_oid(gitmodules_cb, rev.buf,
707 the_repository, &oid, the_repository);
709 strbuf_release(&rev);
711 the_repository->submodule_cache->gitmodules_read = 1;
714 const struct submodule *submodule_from_name(struct repository *r,
715 const struct object_id *treeish_name,
716 const char *name)
718 repo_read_gitmodules(r, 1);
719 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
722 const struct submodule *submodule_from_path(struct repository *r,
723 const struct object_id *treeish_name,
724 const char *path)
726 repo_read_gitmodules(r, 1);
727 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
731 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
732 * and appends submodule entries to 'out'. The submodule_cache expects
733 * a root-level treeish_name and paths, so keep track of these values
734 * with 'root_tree' and 'prefix'.
736 static void traverse_tree_submodules(struct repository *r,
737 const struct object_id *root_tree,
738 char *prefix,
739 const struct object_id *treeish_name,
740 struct submodule_entry_list *out)
742 struct tree_desc tree;
743 struct submodule_tree_entry *st_entry;
744 struct name_entry *name_entry;
745 char *tree_path = NULL;
747 name_entry = xmalloc(sizeof(*name_entry));
749 fill_tree_descriptor(r, &tree, treeish_name);
750 while (tree_entry(&tree, name_entry)) {
751 if (prefix)
752 tree_path =
753 mkpathdup("%s/%s", prefix, name_entry->path);
754 else
755 tree_path = xstrdup(name_entry->path);
757 if (S_ISGITLINK(name_entry->mode) &&
758 is_tree_submodule_active(r, root_tree, tree_path)) {
759 ALLOC_GROW(out->entries, out->entry_nr + 1,
760 out->entry_alloc);
761 st_entry = &out->entries[out->entry_nr++];
763 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
764 *st_entry->name_entry = *name_entry;
765 st_entry->submodule =
766 submodule_from_path(r, root_tree, tree_path);
767 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
768 if (repo_submodule_init(st_entry->repo, r, tree_path,
769 root_tree))
770 FREE_AND_NULL(st_entry->repo);
772 } else if (S_ISDIR(name_entry->mode))
773 traverse_tree_submodules(r, root_tree, tree_path,
774 &name_entry->oid, out);
775 free(tree_path);
779 void submodules_of_tree(struct repository *r,
780 const struct object_id *treeish_name,
781 struct submodule_entry_list *out)
783 CALLOC_ARRAY(out->entries, 0);
784 out->entry_nr = 0;
785 out->entry_alloc = 0;
787 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
790 void submodule_free(struct repository *r)
792 if (r->submodule_cache)
793 submodule_cache_clear(r->submodule_cache);
796 static int config_print_callback(const char *var, const char *value, void *cb_data)
798 char *wanted_key = cb_data;
800 if (!strcmp(wanted_key, var))
801 printf("%s\n", value);
803 return 0;
806 int print_config_from_gitmodules(struct repository *repo, const char *key)
808 int ret;
809 char *store_key;
811 ret = git_config_parse_key(key, &store_key, NULL);
812 if (ret < 0)
813 return CONFIG_INVALID_KEY;
815 config_from_gitmodules(config_print_callback, repo, store_key);
817 free(store_key);
818 return 0;
821 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
823 int ret;
825 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
826 if (ret < 0)
827 /* Maybe the user already did that, don't error out here */
828 warning(_("Could not update .gitmodules entry %s"), key);
830 return ret;
833 struct fetch_config {
834 int *max_children;
835 int *recurse_submodules;
838 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
840 struct fetch_config *config = cb;
841 if (!strcmp(var, "submodule.fetchjobs")) {
842 if (config->max_children)
843 *(config->max_children) =
844 parse_submodule_fetchjobs(var, value);
845 return 0;
846 } else if (!strcmp(var, "fetch.recursesubmodules")) {
847 if (config->recurse_submodules)
848 *(config->recurse_submodules) =
849 parse_fetch_recurse_submodules_arg(var, value);
850 return 0;
853 return 0;
856 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
858 struct fetch_config config = {
859 .max_children = max_children,
860 .recurse_submodules = recurse_submodules
862 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
865 static int gitmodules_update_clone_config(const char *var, const char *value,
866 void *cb)
868 int *max_jobs = cb;
869 if (!strcmp(var, "submodule.fetchjobs"))
870 *max_jobs = parse_submodule_fetchjobs(var, value);
871 return 0;
874 void update_clone_config_from_gitmodules(int *max_jobs)
876 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);