cocci: remove 'unused.cocci'
[git.git] / submodule-config.c
blobecf0fcf007481806d9e7f63c6d35f0218bca587e
1 #include "cache.h"
2 #include "alloc.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "repository.h"
8 #include "config.h"
9 #include "submodule-config.h"
10 #include "submodule.h"
11 #include "strbuf.h"
12 #include "object-store.h"
13 #include "parse-options.h"
14 #include "tree-walk.h"
17 * submodule cache lookup structure
18 * There is one shared set of 'struct submodule' entries which can be
19 * looked up by their sha1 blob id of the .gitmodules file and either
20 * using path or name as key.
21 * for_path stores submodule entries with path as key
22 * for_name stores submodule entries with name as key
24 struct submodule_cache {
25 struct hashmap for_path;
26 struct hashmap for_name;
27 unsigned initialized:1;
28 unsigned gitmodules_read:1;
32 * thin wrapper struct needed to insert 'struct submodule' entries to
33 * the hashmap
35 struct submodule_entry {
36 struct hashmap_entry ent;
37 struct submodule *config;
40 enum lookup_type {
41 lookup_name,
42 lookup_path
45 static int config_path_cmp(const void *cmp_data UNUSED,
46 const struct hashmap_entry *eptr,
47 const struct hashmap_entry *entry_or_key,
48 const void *keydata UNUSED)
50 const struct submodule_entry *a, *b;
52 a = container_of(eptr, const struct submodule_entry, ent);
53 b = container_of(entry_or_key, const struct submodule_entry, ent);
55 return strcmp(a->config->path, b->config->path) ||
56 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
59 static int config_name_cmp(const void *cmp_data UNUSED,
60 const struct hashmap_entry *eptr,
61 const struct hashmap_entry *entry_or_key,
62 const void *keydata UNUSED)
64 const struct submodule_entry *a, *b;
66 a = container_of(eptr, const struct submodule_entry, ent);
67 b = container_of(entry_or_key, const struct submodule_entry, ent);
69 return strcmp(a->config->name, b->config->name) ||
70 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
73 static struct submodule_cache *submodule_cache_alloc(void)
75 return xcalloc(1, sizeof(struct submodule_cache));
78 static void submodule_cache_init(struct submodule_cache *cache)
80 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
81 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
82 cache->initialized = 1;
85 static void free_one_config(struct submodule_entry *entry)
87 free((void *) entry->config->path);
88 free((void *) entry->config->name);
89 free((void *) entry->config->branch);
90 free((void *) entry->config->update_strategy.command);
91 free(entry->config);
94 static void submodule_cache_clear(struct submodule_cache *cache)
96 struct hashmap_iter iter;
97 struct submodule_entry *entry;
99 if (!cache->initialized)
100 return;
103 * We iterate over the name hash here to be symmetric with the
104 * allocation of struct submodule entries. Each is allocated by
105 * their .gitmodules blob sha1 and submodule name.
107 hashmap_for_each_entry(&cache->for_name, &iter, entry,
108 ent /* member name */)
109 free_one_config(entry);
111 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
112 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
113 cache->initialized = 0;
114 cache->gitmodules_read = 0;
117 void submodule_cache_free(struct submodule_cache *cache)
119 submodule_cache_clear(cache);
120 free(cache);
123 static unsigned int hash_oid_string(const struct object_id *oid,
124 const char *string)
126 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
129 static void cache_put_path(struct submodule_cache *cache,
130 struct submodule *submodule)
132 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
133 submodule->path);
134 struct submodule_entry *e = xmalloc(sizeof(*e));
135 hashmap_entry_init(&e->ent, hash);
136 e->config = submodule;
137 hashmap_put(&cache->for_path, &e->ent);
140 static void cache_remove_path(struct submodule_cache *cache,
141 struct submodule *submodule)
143 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
144 submodule->path);
145 struct submodule_entry e;
146 struct submodule_entry *removed;
147 hashmap_entry_init(&e.ent, hash);
148 e.config = submodule;
149 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
150 free(removed);
153 static void cache_add(struct submodule_cache *cache,
154 struct submodule *submodule)
156 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
157 submodule->name);
158 struct submodule_entry *e = xmalloc(sizeof(*e));
159 hashmap_entry_init(&e->ent, hash);
160 e->config = submodule;
161 hashmap_add(&cache->for_name, &e->ent);
164 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
165 const struct object_id *gitmodules_oid, const char *path)
167 struct submodule_entry *entry;
168 unsigned int hash = hash_oid_string(gitmodules_oid, path);
169 struct submodule_entry key;
170 struct submodule key_config;
172 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
173 key_config.path = path;
175 hashmap_entry_init(&key.ent, hash);
176 key.config = &key_config;
178 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
179 if (entry)
180 return entry->config;
181 return NULL;
184 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
185 const struct object_id *gitmodules_oid, const char *name)
187 struct submodule_entry *entry;
188 unsigned int hash = hash_oid_string(gitmodules_oid, name);
189 struct submodule_entry key;
190 struct submodule key_config;
192 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
193 key_config.name = name;
195 hashmap_entry_init(&key.ent, hash);
196 key.config = &key_config;
198 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
199 if (entry)
200 return entry->config;
201 return NULL;
204 int check_submodule_name(const char *name)
206 /* Disallow empty names */
207 if (!*name)
208 return -1;
211 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
212 * separators rather than is_dir_sep(), because we want the name rules
213 * to be consistent across platforms.
215 goto in_component; /* always start inside component */
216 while (*name) {
217 char c = *name++;
218 if (is_xplatform_dir_sep(c)) {
219 in_component:
220 if (name[0] == '.' && name[1] == '.' &&
221 (!name[2] || is_xplatform_dir_sep(name[2])))
222 return -1;
226 return 0;
229 static int name_and_item_from_var(const char *var, struct strbuf *name,
230 struct strbuf *item)
232 const char *subsection, *key;
233 size_t subsection_len;
234 int parse;
235 parse = parse_config_key(var, "submodule", &subsection,
236 &subsection_len, &key);
237 if (parse < 0 || !subsection)
238 return 0;
240 strbuf_add(name, subsection, subsection_len);
241 if (check_submodule_name(name->buf) < 0) {
242 warning(_("ignoring suspicious submodule name: %s"), name->buf);
243 strbuf_release(name);
244 return 0;
247 strbuf_addstr(item, key);
249 return 1;
252 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
253 const struct object_id *gitmodules_oid, const char *name)
255 struct submodule *submodule;
256 struct strbuf name_buf = STRBUF_INIT;
258 submodule = cache_lookup_name(cache, gitmodules_oid, name);
259 if (submodule)
260 return submodule;
262 submodule = xmalloc(sizeof(*submodule));
264 strbuf_addstr(&name_buf, name);
265 submodule->name = strbuf_detach(&name_buf, NULL);
267 submodule->path = NULL;
268 submodule->url = NULL;
269 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
270 submodule->update_strategy.command = NULL;
271 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
272 submodule->ignore = NULL;
273 submodule->branch = NULL;
274 submodule->recommend_shallow = -1;
276 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
278 cache_add(cache, submodule);
280 return submodule;
283 static int parse_fetch_recurse(const char *opt, const char *arg,
284 int die_on_error)
286 switch (git_parse_maybe_bool(arg)) {
287 case 1:
288 return RECURSE_SUBMODULES_ON;
289 case 0:
290 return RECURSE_SUBMODULES_OFF;
291 default:
292 if (!strcmp(arg, "on-demand"))
293 return RECURSE_SUBMODULES_ON_DEMAND;
295 * Please update $__git_fetch_recurse_submodules in
296 * git-completion.bash when you add new options.
298 if (die_on_error)
299 die("bad %s argument: %s", opt, arg);
300 else
301 return RECURSE_SUBMODULES_ERROR;
305 int parse_submodule_fetchjobs(const char *var, const char *value)
307 int fetchjobs = git_config_int(var, value);
308 if (fetchjobs < 0)
309 die(_("negative values not allowed for submodule.fetchJobs"));
310 if (!fetchjobs)
311 fetchjobs = online_cpus();
312 return fetchjobs;
315 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
317 return parse_fetch_recurse(opt, arg, 1);
320 int option_fetch_parse_recurse_submodules(const struct option *opt,
321 const char *arg, int unset)
323 int *v;
325 if (!opt->value)
326 return -1;
328 v = opt->value;
330 if (unset) {
331 *v = RECURSE_SUBMODULES_OFF;
332 } else {
333 if (arg)
334 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
335 else
336 *v = RECURSE_SUBMODULES_ON;
338 return 0;
341 static int parse_update_recurse(const char *opt, const char *arg,
342 int die_on_error)
344 switch (git_parse_maybe_bool(arg)) {
345 case 1:
346 return RECURSE_SUBMODULES_ON;
347 case 0:
348 return RECURSE_SUBMODULES_OFF;
349 default:
350 if (die_on_error)
351 die("bad %s argument: %s", opt, arg);
352 return RECURSE_SUBMODULES_ERROR;
356 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
358 return parse_update_recurse(opt, arg, 1);
361 static int parse_push_recurse(const char *opt, const char *arg,
362 int die_on_error)
364 switch (git_parse_maybe_bool(arg)) {
365 case 1:
366 /* There's no simple "on" value when pushing */
367 if (die_on_error)
368 die("bad %s argument: %s", opt, arg);
369 else
370 return RECURSE_SUBMODULES_ERROR;
371 case 0:
372 return RECURSE_SUBMODULES_OFF;
373 default:
374 if (!strcmp(arg, "on-demand"))
375 return RECURSE_SUBMODULES_ON_DEMAND;
376 else if (!strcmp(arg, "check"))
377 return RECURSE_SUBMODULES_CHECK;
378 else if (!strcmp(arg, "only"))
379 return RECURSE_SUBMODULES_ONLY;
381 * Please update $__git_push_recurse_submodules in
382 * git-completion.bash when you add new modes.
384 else if (die_on_error)
385 die("bad %s argument: %s", opt, arg);
386 else
387 return RECURSE_SUBMODULES_ERROR;
391 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
393 return parse_push_recurse(opt, arg, 1);
396 static void warn_multiple_config(const struct object_id *treeish_name,
397 const char *name, const char *option)
399 const char *commit_string = "WORKTREE";
400 if (treeish_name)
401 commit_string = oid_to_hex(treeish_name);
402 warning("%s:.gitmodules, multiple configurations found for "
403 "'submodule.%s.%s'. Skipping second one!",
404 commit_string, name, option);
407 static void warn_command_line_option(const char *var, const char *value)
409 warning(_("ignoring '%s' which may be interpreted as"
410 " a command-line option: %s"), var, value);
413 struct parse_config_parameter {
414 struct submodule_cache *cache;
415 const struct object_id *treeish_name;
416 const struct object_id *gitmodules_oid;
417 int overwrite;
421 * Parse a config item from .gitmodules.
423 * This does not handle submodule-related configuration from the main
424 * config store (.git/config, etc). Callers are responsible for
425 * checking for overrides in the main config store when appropriate.
427 static int parse_config(const char *var, const char *value, void *data)
429 struct parse_config_parameter *me = data;
430 struct submodule *submodule;
431 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
432 int ret = 0;
434 /* this also ensures that we only parse submodule entries */
435 if (!name_and_item_from_var(var, &name, &item))
436 return 0;
438 submodule = lookup_or_create_by_name(me->cache,
439 me->gitmodules_oid,
440 name.buf);
442 if (!strcmp(item.buf, "path")) {
443 if (!value)
444 ret = config_error_nonbool(var);
445 else if (looks_like_command_line_option(value))
446 warn_command_line_option(var, value);
447 else if (!me->overwrite && submodule->path)
448 warn_multiple_config(me->treeish_name, submodule->name,
449 "path");
450 else {
451 if (submodule->path)
452 cache_remove_path(me->cache, submodule);
453 free((void *) submodule->path);
454 submodule->path = xstrdup(value);
455 cache_put_path(me->cache, submodule);
457 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
458 /* when parsing worktree configurations we can die early */
459 int die_on_error = is_null_oid(me->gitmodules_oid);
460 if (!me->overwrite &&
461 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
462 warn_multiple_config(me->treeish_name, submodule->name,
463 "fetchrecursesubmodules");
464 else
465 submodule->fetch_recurse = parse_fetch_recurse(
466 var, value,
467 die_on_error);
468 } else if (!strcmp(item.buf, "ignore")) {
469 if (!value)
470 ret = config_error_nonbool(var);
471 else if (!me->overwrite && submodule->ignore)
472 warn_multiple_config(me->treeish_name, submodule->name,
473 "ignore");
474 else if (strcmp(value, "untracked") &&
475 strcmp(value, "dirty") &&
476 strcmp(value, "all") &&
477 strcmp(value, "none"))
478 warning("Invalid parameter '%s' for config option "
479 "'submodule.%s.ignore'", value, name.buf);
480 else {
481 free((void *) submodule->ignore);
482 submodule->ignore = xstrdup(value);
484 } else if (!strcmp(item.buf, "url")) {
485 if (!value) {
486 ret = config_error_nonbool(var);
487 } else if (looks_like_command_line_option(value)) {
488 warn_command_line_option(var, value);
489 } else if (!me->overwrite && submodule->url) {
490 warn_multiple_config(me->treeish_name, submodule->name,
491 "url");
492 } else {
493 free((void *) submodule->url);
494 submodule->url = xstrdup(value);
496 } else if (!strcmp(item.buf, "update")) {
497 if (!value)
498 ret = config_error_nonbool(var);
499 else if (!me->overwrite &&
500 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
501 warn_multiple_config(me->treeish_name, submodule->name,
502 "update");
503 else if (parse_submodule_update_strategy(value,
504 &submodule->update_strategy) < 0 ||
505 submodule->update_strategy.type == SM_UPDATE_COMMAND)
506 die(_("invalid value for '%s'"), var);
507 } else if (!strcmp(item.buf, "shallow")) {
508 if (!me->overwrite && submodule->recommend_shallow != -1)
509 warn_multiple_config(me->treeish_name, submodule->name,
510 "shallow");
511 else
512 submodule->recommend_shallow =
513 git_config_bool(var, value);
514 } else if (!strcmp(item.buf, "branch")) {
515 if (!me->overwrite && submodule->branch)
516 warn_multiple_config(me->treeish_name, submodule->name,
517 "branch");
518 else {
519 free((void *)submodule->branch);
520 submodule->branch = xstrdup(value);
524 strbuf_release(&name);
525 strbuf_release(&item);
527 return ret;
530 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
531 struct object_id *gitmodules_oid,
532 struct strbuf *rev)
534 int ret = 0;
536 if (is_null_oid(treeish_name)) {
537 oidclr(gitmodules_oid);
538 return 1;
541 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
542 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
543 ret = 1;
545 return ret;
548 /* This does a lookup of a submodule configuration by name or by path
549 * (key) with on-demand reading of the appropriate .gitmodules from
550 * revisions.
552 static const struct submodule *config_from(struct submodule_cache *cache,
553 const struct object_id *treeish_name, const char *key,
554 enum lookup_type lookup_type)
556 struct strbuf rev = STRBUF_INIT;
557 unsigned long config_size;
558 char *config = NULL;
559 struct object_id oid;
560 enum object_type type;
561 const struct submodule *submodule = NULL;
562 struct parse_config_parameter parameter;
565 * If any parameter except the cache is a NULL pointer just
566 * return the first submodule. Can be used to check whether
567 * there are any submodules parsed.
569 if (!treeish_name || !key) {
570 struct hashmap_iter iter;
571 struct submodule_entry *entry;
573 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
574 struct submodule_entry,
575 ent /* member name */);
576 if (!entry)
577 return NULL;
578 return entry->config;
581 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
582 goto out;
584 switch (lookup_type) {
585 case lookup_name:
586 submodule = cache_lookup_name(cache, &oid, key);
587 break;
588 case lookup_path:
589 submodule = cache_lookup_path(cache, &oid, key);
590 break;
592 if (submodule)
593 goto out;
595 config = repo_read_object_file(the_repository, &oid, &type,
596 &config_size);
597 if (!config || type != OBJ_BLOB)
598 goto out;
600 /* fill the submodule config into the cache */
601 parameter.cache = cache;
602 parameter.treeish_name = treeish_name;
603 parameter.gitmodules_oid = &oid;
604 parameter.overwrite = 0;
605 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
606 config, config_size, &parameter, NULL);
607 strbuf_release(&rev);
608 free(config);
610 switch (lookup_type) {
611 case lookup_name:
612 return cache_lookup_name(cache, &oid, key);
613 case lookup_path:
614 return cache_lookup_path(cache, &oid, key);
615 default:
616 return NULL;
619 out:
620 strbuf_release(&rev);
621 free(config);
622 return submodule;
625 static void submodule_cache_check_init(struct repository *repo)
627 if (repo->submodule_cache && repo->submodule_cache->initialized)
628 return;
630 if (!repo->submodule_cache)
631 repo->submodule_cache = submodule_cache_alloc();
633 submodule_cache_init(repo->submodule_cache);
637 * Note: This function is private for a reason, the '.gitmodules' file should
638 * not be used as a mechanism to retrieve arbitrary configuration stored in
639 * the repository.
641 * Runs the provided config function on the '.gitmodules' file found in the
642 * working directory.
644 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
646 if (repo->worktree) {
647 struct git_config_source config_source = {
648 0, .scope = CONFIG_SCOPE_SUBMODULE
650 const struct config_options opts = { 0 };
651 struct object_id oid;
652 char *file;
653 char *oidstr = NULL;
655 file = repo_worktree_path(repo, GITMODULES_FILE);
656 if (file_exists(file)) {
657 config_source.file = file;
658 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
659 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
660 config_source.repo = repo;
661 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
662 if (repo != the_repository)
663 add_submodule_odb_by_path(repo->objects->odb->path);
664 } else {
665 goto out;
668 config_with_options(fn, data, &config_source, &opts);
670 out:
671 free(oidstr);
672 free(file);
676 static int gitmodules_cb(const char *var, const char *value, void *data)
678 struct repository *repo = data;
679 struct parse_config_parameter parameter;
681 parameter.cache = repo->submodule_cache;
682 parameter.treeish_name = NULL;
683 parameter.gitmodules_oid = null_oid();
684 parameter.overwrite = 1;
686 return parse_config(var, value, &parameter);
689 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
691 submodule_cache_check_init(repo);
693 if (repo->submodule_cache->gitmodules_read && skip_if_read)
694 return;
696 if (repo_read_index(repo) < 0)
697 return;
699 if (!is_gitmodules_unmerged(repo->index))
700 config_from_gitmodules(gitmodules_cb, repo, repo);
702 repo->submodule_cache->gitmodules_read = 1;
705 void gitmodules_config_oid(const struct object_id *commit_oid)
707 struct strbuf rev = STRBUF_INIT;
708 struct object_id oid;
710 submodule_cache_check_init(the_repository);
712 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
713 git_config_from_blob_oid(gitmodules_cb, rev.buf,
714 the_repository, &oid, the_repository);
716 strbuf_release(&rev);
718 the_repository->submodule_cache->gitmodules_read = 1;
721 const struct submodule *submodule_from_name(struct repository *r,
722 const struct object_id *treeish_name,
723 const char *name)
725 repo_read_gitmodules(r, 1);
726 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
729 const struct submodule *submodule_from_path(struct repository *r,
730 const struct object_id *treeish_name,
731 const char *path)
733 repo_read_gitmodules(r, 1);
734 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
738 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
739 * and appends submodule entries to 'out'. The submodule_cache expects
740 * a root-level treeish_name and paths, so keep track of these values
741 * with 'root_tree' and 'prefix'.
743 static void traverse_tree_submodules(struct repository *r,
744 const struct object_id *root_tree,
745 char *prefix,
746 const struct object_id *treeish_name,
747 struct submodule_entry_list *out)
749 struct tree_desc tree;
750 struct submodule_tree_entry *st_entry;
751 struct name_entry *name_entry;
752 char *tree_path = NULL;
754 name_entry = xmalloc(sizeof(*name_entry));
756 fill_tree_descriptor(r, &tree, treeish_name);
757 while (tree_entry(&tree, name_entry)) {
758 if (prefix)
759 tree_path =
760 mkpathdup("%s/%s", prefix, name_entry->path);
761 else
762 tree_path = xstrdup(name_entry->path);
764 if (S_ISGITLINK(name_entry->mode) &&
765 is_tree_submodule_active(r, root_tree, tree_path)) {
766 ALLOC_GROW(out->entries, out->entry_nr + 1,
767 out->entry_alloc);
768 st_entry = &out->entries[out->entry_nr++];
770 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
771 *st_entry->name_entry = *name_entry;
772 st_entry->submodule =
773 submodule_from_path(r, root_tree, tree_path);
774 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
775 if (repo_submodule_init(st_entry->repo, r, tree_path,
776 root_tree))
777 FREE_AND_NULL(st_entry->repo);
779 } else if (S_ISDIR(name_entry->mode))
780 traverse_tree_submodules(r, root_tree, tree_path,
781 &name_entry->oid, out);
782 free(tree_path);
786 void submodules_of_tree(struct repository *r,
787 const struct object_id *treeish_name,
788 struct submodule_entry_list *out)
790 CALLOC_ARRAY(out->entries, 0);
791 out->entry_nr = 0;
792 out->entry_alloc = 0;
794 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
797 void submodule_free(struct repository *r)
799 if (r->submodule_cache)
800 submodule_cache_clear(r->submodule_cache);
803 static int config_print_callback(const char *var, const char *value, void *cb_data)
805 char *wanted_key = cb_data;
807 if (!strcmp(wanted_key, var))
808 printf("%s\n", value);
810 return 0;
813 int print_config_from_gitmodules(struct repository *repo, const char *key)
815 int ret;
816 char *store_key;
818 ret = git_config_parse_key(key, &store_key, NULL);
819 if (ret < 0)
820 return CONFIG_INVALID_KEY;
822 config_from_gitmodules(config_print_callback, repo, store_key);
824 free(store_key);
825 return 0;
828 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
830 int ret;
832 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
833 if (ret < 0)
834 /* Maybe the user already did that, don't error out here */
835 warning(_("Could not update .gitmodules entry %s"), key);
837 return ret;
840 struct fetch_config {
841 int *max_children;
842 int *recurse_submodules;
845 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
847 struct fetch_config *config = cb;
848 if (!strcmp(var, "submodule.fetchjobs")) {
849 if (config->max_children)
850 *(config->max_children) =
851 parse_submodule_fetchjobs(var, value);
852 return 0;
853 } else if (!strcmp(var, "fetch.recursesubmodules")) {
854 if (config->recurse_submodules)
855 *(config->recurse_submodules) =
856 parse_fetch_recurse_submodules_arg(var, value);
857 return 0;
860 return 0;
863 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
865 struct fetch_config config = {
866 .max_children = max_children,
867 .recurse_submodules = recurse_submodules
869 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
872 static int gitmodules_update_clone_config(const char *var, const char *value,
873 void *cb)
875 int *max_jobs = cb;
876 if (!strcmp(var, "submodule.fetchjobs"))
877 *max_jobs = parse_submodule_fetchjobs(var, value);
878 return 0;
881 void update_clone_config_from_gitmodules(int *max_jobs)
883 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);