hash.h: move some oid-related declarations from cache.h
[git/debian.git] / submodule-config.c
blobbb7c35fc317d9db622ae2ad43fd7c5f60688011b
1 #include "cache.h"
2 #include "alloc.h"
3 #include "dir.h"
4 #include "repository.h"
5 #include "config.h"
6 #include "submodule-config.h"
7 #include "submodule.h"
8 #include "strbuf.h"
9 #include "object-store.h"
10 #include "parse-options.h"
11 #include "tree-walk.h"
14 * submodule cache lookup structure
15 * There is one shared set of 'struct submodule' entries which can be
16 * looked up by their sha1 blob id of the .gitmodules file and either
17 * using path or name as key.
18 * for_path stores submodule entries with path as key
19 * for_name stores submodule entries with name as key
21 struct submodule_cache {
22 struct hashmap for_path;
23 struct hashmap for_name;
24 unsigned initialized:1;
25 unsigned gitmodules_read:1;
29 * thin wrapper struct needed to insert 'struct submodule' entries to
30 * the hashmap
32 struct submodule_entry {
33 struct hashmap_entry ent;
34 struct submodule *config;
37 enum lookup_type {
38 lookup_name,
39 lookup_path
42 static int config_path_cmp(const void *cmp_data UNUSED,
43 const struct hashmap_entry *eptr,
44 const struct hashmap_entry *entry_or_key,
45 const void *keydata UNUSED)
47 const struct submodule_entry *a, *b;
49 a = container_of(eptr, const struct submodule_entry, ent);
50 b = container_of(entry_or_key, const struct submodule_entry, ent);
52 return strcmp(a->config->path, b->config->path) ||
53 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
56 static int config_name_cmp(const void *cmp_data UNUSED,
57 const struct hashmap_entry *eptr,
58 const struct hashmap_entry *entry_or_key,
59 const void *keydata UNUSED)
61 const struct submodule_entry *a, *b;
63 a = container_of(eptr, const struct submodule_entry, ent);
64 b = container_of(entry_or_key, const struct submodule_entry, ent);
66 return strcmp(a->config->name, b->config->name) ||
67 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
70 static struct submodule_cache *submodule_cache_alloc(void)
72 return xcalloc(1, sizeof(struct submodule_cache));
75 static void submodule_cache_init(struct submodule_cache *cache)
77 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
78 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
79 cache->initialized = 1;
82 static void free_one_config(struct submodule_entry *entry)
84 free((void *) entry->config->path);
85 free((void *) entry->config->name);
86 free((void *) entry->config->branch);
87 free((void *) entry->config->update_strategy.command);
88 free(entry->config);
91 static void submodule_cache_clear(struct submodule_cache *cache)
93 struct hashmap_iter iter;
94 struct submodule_entry *entry;
96 if (!cache->initialized)
97 return;
100 * We iterate over the name hash here to be symmetric with the
101 * allocation of struct submodule entries. Each is allocated by
102 * their .gitmodules blob sha1 and submodule name.
104 hashmap_for_each_entry(&cache->for_name, &iter, entry,
105 ent /* member name */)
106 free_one_config(entry);
108 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
109 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
110 cache->initialized = 0;
111 cache->gitmodules_read = 0;
114 void submodule_cache_free(struct submodule_cache *cache)
116 submodule_cache_clear(cache);
117 free(cache);
120 static unsigned int hash_oid_string(const struct object_id *oid,
121 const char *string)
123 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
126 static void cache_put_path(struct submodule_cache *cache,
127 struct submodule *submodule)
129 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
130 submodule->path);
131 struct submodule_entry *e = xmalloc(sizeof(*e));
132 hashmap_entry_init(&e->ent, hash);
133 e->config = submodule;
134 hashmap_put(&cache->for_path, &e->ent);
137 static void cache_remove_path(struct submodule_cache *cache,
138 struct submodule *submodule)
140 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
141 submodule->path);
142 struct submodule_entry e;
143 struct submodule_entry *removed;
144 hashmap_entry_init(&e.ent, hash);
145 e.config = submodule;
146 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
147 free(removed);
150 static void cache_add(struct submodule_cache *cache,
151 struct submodule *submodule)
153 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
154 submodule->name);
155 struct submodule_entry *e = xmalloc(sizeof(*e));
156 hashmap_entry_init(&e->ent, hash);
157 e->config = submodule;
158 hashmap_add(&cache->for_name, &e->ent);
161 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
162 const struct object_id *gitmodules_oid, const char *path)
164 struct submodule_entry *entry;
165 unsigned int hash = hash_oid_string(gitmodules_oid, path);
166 struct submodule_entry key;
167 struct submodule key_config;
169 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
170 key_config.path = path;
172 hashmap_entry_init(&key.ent, hash);
173 key.config = &key_config;
175 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
176 if (entry)
177 return entry->config;
178 return NULL;
181 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
182 const struct object_id *gitmodules_oid, const char *name)
184 struct submodule_entry *entry;
185 unsigned int hash = hash_oid_string(gitmodules_oid, name);
186 struct submodule_entry key;
187 struct submodule key_config;
189 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
190 key_config.name = name;
192 hashmap_entry_init(&key.ent, hash);
193 key.config = &key_config;
195 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
196 if (entry)
197 return entry->config;
198 return NULL;
201 int check_submodule_name(const char *name)
203 /* Disallow empty names */
204 if (!*name)
205 return -1;
208 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
209 * separators rather than is_dir_sep(), because we want the name rules
210 * to be consistent across platforms.
212 goto in_component; /* always start inside component */
213 while (*name) {
214 char c = *name++;
215 if (is_xplatform_dir_sep(c)) {
216 in_component:
217 if (name[0] == '.' && name[1] == '.' &&
218 (!name[2] || is_xplatform_dir_sep(name[2])))
219 return -1;
223 return 0;
226 static int name_and_item_from_var(const char *var, struct strbuf *name,
227 struct strbuf *item)
229 const char *subsection, *key;
230 size_t subsection_len;
231 int parse;
232 parse = parse_config_key(var, "submodule", &subsection,
233 &subsection_len, &key);
234 if (parse < 0 || !subsection)
235 return 0;
237 strbuf_add(name, subsection, subsection_len);
238 if (check_submodule_name(name->buf) < 0) {
239 warning(_("ignoring suspicious submodule name: %s"), name->buf);
240 strbuf_release(name);
241 return 0;
244 strbuf_addstr(item, key);
246 return 1;
249 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
250 const struct object_id *gitmodules_oid, const char *name)
252 struct submodule *submodule;
253 struct strbuf name_buf = STRBUF_INIT;
255 submodule = cache_lookup_name(cache, gitmodules_oid, name);
256 if (submodule)
257 return submodule;
259 submodule = xmalloc(sizeof(*submodule));
261 strbuf_addstr(&name_buf, name);
262 submodule->name = strbuf_detach(&name_buf, NULL);
264 submodule->path = NULL;
265 submodule->url = NULL;
266 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
267 submodule->update_strategy.command = NULL;
268 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
269 submodule->ignore = NULL;
270 submodule->branch = NULL;
271 submodule->recommend_shallow = -1;
273 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
275 cache_add(cache, submodule);
277 return submodule;
280 static int parse_fetch_recurse(const char *opt, const char *arg,
281 int die_on_error)
283 switch (git_parse_maybe_bool(arg)) {
284 case 1:
285 return RECURSE_SUBMODULES_ON;
286 case 0:
287 return RECURSE_SUBMODULES_OFF;
288 default:
289 if (!strcmp(arg, "on-demand"))
290 return RECURSE_SUBMODULES_ON_DEMAND;
292 * Please update $__git_fetch_recurse_submodules in
293 * git-completion.bash when you add new options.
295 if (die_on_error)
296 die("bad %s argument: %s", opt, arg);
297 else
298 return RECURSE_SUBMODULES_ERROR;
302 int parse_submodule_fetchjobs(const char *var, const char *value)
304 int fetchjobs = git_config_int(var, value);
305 if (fetchjobs < 0)
306 die(_("negative values not allowed for submodule.fetchJobs"));
307 if (!fetchjobs)
308 fetchjobs = online_cpus();
309 return fetchjobs;
312 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
314 return parse_fetch_recurse(opt, arg, 1);
317 int option_fetch_parse_recurse_submodules(const struct option *opt,
318 const char *arg, int unset)
320 int *v;
322 if (!opt->value)
323 return -1;
325 v = opt->value;
327 if (unset) {
328 *v = RECURSE_SUBMODULES_OFF;
329 } else {
330 if (arg)
331 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
332 else
333 *v = RECURSE_SUBMODULES_ON;
335 return 0;
338 static int parse_update_recurse(const char *opt, const char *arg,
339 int die_on_error)
341 switch (git_parse_maybe_bool(arg)) {
342 case 1:
343 return RECURSE_SUBMODULES_ON;
344 case 0:
345 return RECURSE_SUBMODULES_OFF;
346 default:
347 if (die_on_error)
348 die("bad %s argument: %s", opt, arg);
349 return RECURSE_SUBMODULES_ERROR;
353 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
355 return parse_update_recurse(opt, arg, 1);
358 static int parse_push_recurse(const char *opt, const char *arg,
359 int die_on_error)
361 switch (git_parse_maybe_bool(arg)) {
362 case 1:
363 /* There's no simple "on" value when pushing */
364 if (die_on_error)
365 die("bad %s argument: %s", opt, arg);
366 else
367 return RECURSE_SUBMODULES_ERROR;
368 case 0:
369 return RECURSE_SUBMODULES_OFF;
370 default:
371 if (!strcmp(arg, "on-demand"))
372 return RECURSE_SUBMODULES_ON_DEMAND;
373 else if (!strcmp(arg, "check"))
374 return RECURSE_SUBMODULES_CHECK;
375 else if (!strcmp(arg, "only"))
376 return RECURSE_SUBMODULES_ONLY;
378 * Please update $__git_push_recurse_submodules in
379 * git-completion.bash when you add new modes.
381 else if (die_on_error)
382 die("bad %s argument: %s", opt, arg);
383 else
384 return RECURSE_SUBMODULES_ERROR;
388 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
390 return parse_push_recurse(opt, arg, 1);
393 static void warn_multiple_config(const struct object_id *treeish_name,
394 const char *name, const char *option)
396 const char *commit_string = "WORKTREE";
397 if (treeish_name)
398 commit_string = oid_to_hex(treeish_name);
399 warning("%s:.gitmodules, multiple configurations found for "
400 "'submodule.%s.%s'. Skipping second one!",
401 commit_string, name, option);
404 static void warn_command_line_option(const char *var, const char *value)
406 warning(_("ignoring '%s' which may be interpreted as"
407 " a command-line option: %s"), var, value);
410 struct parse_config_parameter {
411 struct submodule_cache *cache;
412 const struct object_id *treeish_name;
413 const struct object_id *gitmodules_oid;
414 int overwrite;
418 * Parse a config item from .gitmodules.
420 * This does not handle submodule-related configuration from the main
421 * config store (.git/config, etc). Callers are responsible for
422 * checking for overrides in the main config store when appropriate.
424 static int parse_config(const char *var, const char *value, void *data)
426 struct parse_config_parameter *me = data;
427 struct submodule *submodule;
428 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
429 int ret = 0;
431 /* this also ensures that we only parse submodule entries */
432 if (!name_and_item_from_var(var, &name, &item))
433 return 0;
435 submodule = lookup_or_create_by_name(me->cache,
436 me->gitmodules_oid,
437 name.buf);
439 if (!strcmp(item.buf, "path")) {
440 if (!value)
441 ret = config_error_nonbool(var);
442 else if (looks_like_command_line_option(value))
443 warn_command_line_option(var, value);
444 else if (!me->overwrite && submodule->path)
445 warn_multiple_config(me->treeish_name, submodule->name,
446 "path");
447 else {
448 if (submodule->path)
449 cache_remove_path(me->cache, submodule);
450 free((void *) submodule->path);
451 submodule->path = xstrdup(value);
452 cache_put_path(me->cache, submodule);
454 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
455 /* when parsing worktree configurations we can die early */
456 int die_on_error = is_null_oid(me->gitmodules_oid);
457 if (!me->overwrite &&
458 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
459 warn_multiple_config(me->treeish_name, submodule->name,
460 "fetchrecursesubmodules");
461 else
462 submodule->fetch_recurse = parse_fetch_recurse(
463 var, value,
464 die_on_error);
465 } else if (!strcmp(item.buf, "ignore")) {
466 if (!value)
467 ret = config_error_nonbool(var);
468 else if (!me->overwrite && submodule->ignore)
469 warn_multiple_config(me->treeish_name, submodule->name,
470 "ignore");
471 else if (strcmp(value, "untracked") &&
472 strcmp(value, "dirty") &&
473 strcmp(value, "all") &&
474 strcmp(value, "none"))
475 warning("Invalid parameter '%s' for config option "
476 "'submodule.%s.ignore'", value, name.buf);
477 else {
478 free((void *) submodule->ignore);
479 submodule->ignore = xstrdup(value);
481 } else if (!strcmp(item.buf, "url")) {
482 if (!value) {
483 ret = config_error_nonbool(var);
484 } else if (looks_like_command_line_option(value)) {
485 warn_command_line_option(var, value);
486 } else if (!me->overwrite && submodule->url) {
487 warn_multiple_config(me->treeish_name, submodule->name,
488 "url");
489 } else {
490 free((void *) submodule->url);
491 submodule->url = xstrdup(value);
493 } else if (!strcmp(item.buf, "update")) {
494 if (!value)
495 ret = config_error_nonbool(var);
496 else if (!me->overwrite &&
497 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
498 warn_multiple_config(me->treeish_name, submodule->name,
499 "update");
500 else if (parse_submodule_update_strategy(value,
501 &submodule->update_strategy) < 0 ||
502 submodule->update_strategy.type == SM_UPDATE_COMMAND)
503 die(_("invalid value for '%s'"), var);
504 } else if (!strcmp(item.buf, "shallow")) {
505 if (!me->overwrite && submodule->recommend_shallow != -1)
506 warn_multiple_config(me->treeish_name, submodule->name,
507 "shallow");
508 else
509 submodule->recommend_shallow =
510 git_config_bool(var, value);
511 } else if (!strcmp(item.buf, "branch")) {
512 if (!me->overwrite && submodule->branch)
513 warn_multiple_config(me->treeish_name, submodule->name,
514 "branch");
515 else {
516 free((void *)submodule->branch);
517 submodule->branch = xstrdup(value);
521 strbuf_release(&name);
522 strbuf_release(&item);
524 return ret;
527 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
528 struct object_id *gitmodules_oid,
529 struct strbuf *rev)
531 int ret = 0;
533 if (is_null_oid(treeish_name)) {
534 oidclr(gitmodules_oid);
535 return 1;
538 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
539 if (get_oid(rev->buf, gitmodules_oid) >= 0)
540 ret = 1;
542 return ret;
545 /* This does a lookup of a submodule configuration by name or by path
546 * (key) with on-demand reading of the appropriate .gitmodules from
547 * revisions.
549 static const struct submodule *config_from(struct submodule_cache *cache,
550 const struct object_id *treeish_name, const char *key,
551 enum lookup_type lookup_type)
553 struct strbuf rev = STRBUF_INIT;
554 unsigned long config_size;
555 char *config = NULL;
556 struct object_id oid;
557 enum object_type type;
558 const struct submodule *submodule = NULL;
559 struct parse_config_parameter parameter;
562 * If any parameter except the cache is a NULL pointer just
563 * return the first submodule. Can be used to check whether
564 * there are any submodules parsed.
566 if (!treeish_name || !key) {
567 struct hashmap_iter iter;
568 struct submodule_entry *entry;
570 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
571 struct submodule_entry,
572 ent /* member name */);
573 if (!entry)
574 return NULL;
575 return entry->config;
578 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
579 goto out;
581 switch (lookup_type) {
582 case lookup_name:
583 submodule = cache_lookup_name(cache, &oid, key);
584 break;
585 case lookup_path:
586 submodule = cache_lookup_path(cache, &oid, key);
587 break;
589 if (submodule)
590 goto out;
592 config = read_object_file(&oid, &type, &config_size);
593 if (!config || type != OBJ_BLOB)
594 goto out;
596 /* fill the submodule config into the cache */
597 parameter.cache = cache;
598 parameter.treeish_name = treeish_name;
599 parameter.gitmodules_oid = &oid;
600 parameter.overwrite = 0;
601 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
602 config, config_size, &parameter, NULL);
603 strbuf_release(&rev);
604 free(config);
606 switch (lookup_type) {
607 case lookup_name:
608 return cache_lookup_name(cache, &oid, key);
609 case lookup_path:
610 return cache_lookup_path(cache, &oid, key);
611 default:
612 return NULL;
615 out:
616 strbuf_release(&rev);
617 free(config);
618 return submodule;
621 static void submodule_cache_check_init(struct repository *repo)
623 if (repo->submodule_cache && repo->submodule_cache->initialized)
624 return;
626 if (!repo->submodule_cache)
627 repo->submodule_cache = submodule_cache_alloc();
629 submodule_cache_init(repo->submodule_cache);
633 * Note: This function is private for a reason, the '.gitmodules' file should
634 * not be used as a mechanism to retrieve arbitrary configuration stored in
635 * the repository.
637 * Runs the provided config function on the '.gitmodules' file found in the
638 * working directory.
640 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
642 if (repo->worktree) {
643 struct git_config_source config_source = {
644 0, .scope = CONFIG_SCOPE_SUBMODULE
646 const struct config_options opts = { 0 };
647 struct object_id oid;
648 char *file;
649 char *oidstr = NULL;
651 file = repo_worktree_path(repo, GITMODULES_FILE);
652 if (file_exists(file)) {
653 config_source.file = file;
654 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
655 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
656 config_source.repo = repo;
657 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
658 if (repo != the_repository)
659 add_submodule_odb_by_path(repo->objects->odb->path);
660 } else {
661 goto out;
664 config_with_options(fn, data, &config_source, &opts);
666 out:
667 free(oidstr);
668 free(file);
672 static int gitmodules_cb(const char *var, const char *value, void *data)
674 struct repository *repo = data;
675 struct parse_config_parameter parameter;
677 parameter.cache = repo->submodule_cache;
678 parameter.treeish_name = NULL;
679 parameter.gitmodules_oid = null_oid();
680 parameter.overwrite = 1;
682 return parse_config(var, value, &parameter);
685 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
687 submodule_cache_check_init(repo);
689 if (repo->submodule_cache->gitmodules_read && skip_if_read)
690 return;
692 if (repo_read_index(repo) < 0)
693 return;
695 if (!is_gitmodules_unmerged(repo->index))
696 config_from_gitmodules(gitmodules_cb, repo, repo);
698 repo->submodule_cache->gitmodules_read = 1;
701 void gitmodules_config_oid(const struct object_id *commit_oid)
703 struct strbuf rev = STRBUF_INIT;
704 struct object_id oid;
706 submodule_cache_check_init(the_repository);
708 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
709 git_config_from_blob_oid(gitmodules_cb, rev.buf,
710 the_repository, &oid, the_repository);
712 strbuf_release(&rev);
714 the_repository->submodule_cache->gitmodules_read = 1;
717 const struct submodule *submodule_from_name(struct repository *r,
718 const struct object_id *treeish_name,
719 const char *name)
721 repo_read_gitmodules(r, 1);
722 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
725 const struct submodule *submodule_from_path(struct repository *r,
726 const struct object_id *treeish_name,
727 const char *path)
729 repo_read_gitmodules(r, 1);
730 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
734 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
735 * and appends submodule entries to 'out'. The submodule_cache expects
736 * a root-level treeish_name and paths, so keep track of these values
737 * with 'root_tree' and 'prefix'.
739 static void traverse_tree_submodules(struct repository *r,
740 const struct object_id *root_tree,
741 char *prefix,
742 const struct object_id *treeish_name,
743 struct submodule_entry_list *out)
745 struct tree_desc tree;
746 struct submodule_tree_entry *st_entry;
747 struct name_entry *name_entry;
748 char *tree_path = NULL;
750 name_entry = xmalloc(sizeof(*name_entry));
752 fill_tree_descriptor(r, &tree, treeish_name);
753 while (tree_entry(&tree, name_entry)) {
754 if (prefix)
755 tree_path =
756 mkpathdup("%s/%s", prefix, name_entry->path);
757 else
758 tree_path = xstrdup(name_entry->path);
760 if (S_ISGITLINK(name_entry->mode) &&
761 is_tree_submodule_active(r, root_tree, tree_path)) {
762 ALLOC_GROW(out->entries, out->entry_nr + 1,
763 out->entry_alloc);
764 st_entry = &out->entries[out->entry_nr++];
766 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
767 *st_entry->name_entry = *name_entry;
768 st_entry->submodule =
769 submodule_from_path(r, root_tree, tree_path);
770 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
771 if (repo_submodule_init(st_entry->repo, r, tree_path,
772 root_tree))
773 FREE_AND_NULL(st_entry->repo);
775 } else if (S_ISDIR(name_entry->mode))
776 traverse_tree_submodules(r, root_tree, tree_path,
777 &name_entry->oid, out);
778 free(tree_path);
782 void submodules_of_tree(struct repository *r,
783 const struct object_id *treeish_name,
784 struct submodule_entry_list *out)
786 CALLOC_ARRAY(out->entries, 0);
787 out->entry_nr = 0;
788 out->entry_alloc = 0;
790 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
793 void submodule_free(struct repository *r)
795 if (r->submodule_cache)
796 submodule_cache_clear(r->submodule_cache);
799 static int config_print_callback(const char *var, const char *value, void *cb_data)
801 char *wanted_key = cb_data;
803 if (!strcmp(wanted_key, var))
804 printf("%s\n", value);
806 return 0;
809 int print_config_from_gitmodules(struct repository *repo, const char *key)
811 int ret;
812 char *store_key;
814 ret = git_config_parse_key(key, &store_key, NULL);
815 if (ret < 0)
816 return CONFIG_INVALID_KEY;
818 config_from_gitmodules(config_print_callback, repo, store_key);
820 free(store_key);
821 return 0;
824 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
826 int ret;
828 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
829 if (ret < 0)
830 /* Maybe the user already did that, don't error out here */
831 warning(_("Could not update .gitmodules entry %s"), key);
833 return ret;
836 struct fetch_config {
837 int *max_children;
838 int *recurse_submodules;
841 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
843 struct fetch_config *config = cb;
844 if (!strcmp(var, "submodule.fetchjobs")) {
845 if (config->max_children)
846 *(config->max_children) =
847 parse_submodule_fetchjobs(var, value);
848 return 0;
849 } else if (!strcmp(var, "fetch.recursesubmodules")) {
850 if (config->recurse_submodules)
851 *(config->recurse_submodules) =
852 parse_fetch_recurse_submodules_arg(var, value);
853 return 0;
856 return 0;
859 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
861 struct fetch_config config = {
862 .max_children = max_children,
863 .recurse_submodules = recurse_submodules
865 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
868 static int gitmodules_update_clone_config(const char *var, const char *value,
869 void *cb)
871 int *max_jobs = cb;
872 if (!strcmp(var, "submodule.fetchjobs"))
873 *max_jobs = parse_submodule_fetchjobs(var, value);
874 return 0;
877 void update_clone_config_from_gitmodules(int *max_jobs)
879 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);