dir.h: move DTYPE defines from cache.h
[git.git] / submodule-config.c
blob7fc0812b644ff307e958cb7c172476719b4c3d23
1 #include "git-compat-util.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-name.h"
13 #include "object-store.h"
14 #include "parse-options.h"
15 #include "tree-walk.h"
18 * submodule cache lookup structure
19 * There is one shared set of 'struct submodule' entries which can be
20 * looked up by their sha1 blob id of the .gitmodules file and either
21 * using path or name as key.
22 * for_path stores submodule entries with path as key
23 * for_name stores submodule entries with name as key
25 struct submodule_cache {
26 struct hashmap for_path;
27 struct hashmap for_name;
28 unsigned initialized:1;
29 unsigned gitmodules_read:1;
33 * thin wrapper struct needed to insert 'struct submodule' entries to
34 * the hashmap
36 struct submodule_entry {
37 struct hashmap_entry ent;
38 struct submodule *config;
41 enum lookup_type {
42 lookup_name,
43 lookup_path
46 static int config_path_cmp(const void *cmp_data UNUSED,
47 const struct hashmap_entry *eptr,
48 const struct hashmap_entry *entry_or_key,
49 const void *keydata UNUSED)
51 const struct submodule_entry *a, *b;
53 a = container_of(eptr, const struct submodule_entry, ent);
54 b = container_of(entry_or_key, const struct submodule_entry, ent);
56 return strcmp(a->config->path, b->config->path) ||
57 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
60 static int config_name_cmp(const void *cmp_data UNUSED,
61 const struct hashmap_entry *eptr,
62 const struct hashmap_entry *entry_or_key,
63 const void *keydata UNUSED)
65 const struct submodule_entry *a, *b;
67 a = container_of(eptr, const struct submodule_entry, ent);
68 b = container_of(entry_or_key, const struct submodule_entry, ent);
70 return strcmp(a->config->name, b->config->name) ||
71 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
74 static struct submodule_cache *submodule_cache_alloc(void)
76 return xcalloc(1, sizeof(struct submodule_cache));
79 static void submodule_cache_init(struct submodule_cache *cache)
81 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
82 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
83 cache->initialized = 1;
86 static void free_one_config(struct submodule_entry *entry)
88 free((void *) entry->config->path);
89 free((void *) entry->config->name);
90 free((void *) entry->config->branch);
91 free((void *) entry->config->update_strategy.command);
92 free(entry->config);
95 static void submodule_cache_clear(struct submodule_cache *cache)
97 struct hashmap_iter iter;
98 struct submodule_entry *entry;
100 if (!cache->initialized)
101 return;
104 * We iterate over the name hash here to be symmetric with the
105 * allocation of struct submodule entries. Each is allocated by
106 * their .gitmodules blob sha1 and submodule name.
108 hashmap_for_each_entry(&cache->for_name, &iter, entry,
109 ent /* member name */)
110 free_one_config(entry);
112 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
113 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
114 cache->initialized = 0;
115 cache->gitmodules_read = 0;
118 void submodule_cache_free(struct submodule_cache *cache)
120 submodule_cache_clear(cache);
121 free(cache);
124 static unsigned int hash_oid_string(const struct object_id *oid,
125 const char *string)
127 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
130 static void cache_put_path(struct submodule_cache *cache,
131 struct submodule *submodule)
133 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
134 submodule->path);
135 struct submodule_entry *e = xmalloc(sizeof(*e));
136 hashmap_entry_init(&e->ent, hash);
137 e->config = submodule;
138 hashmap_put(&cache->for_path, &e->ent);
141 static void cache_remove_path(struct submodule_cache *cache,
142 struct submodule *submodule)
144 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
145 submodule->path);
146 struct submodule_entry e;
147 struct submodule_entry *removed;
148 hashmap_entry_init(&e.ent, hash);
149 e.config = submodule;
150 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
151 free(removed);
154 static void cache_add(struct submodule_cache *cache,
155 struct submodule *submodule)
157 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
158 submodule->name);
159 struct submodule_entry *e = xmalloc(sizeof(*e));
160 hashmap_entry_init(&e->ent, hash);
161 e->config = submodule;
162 hashmap_add(&cache->for_name, &e->ent);
165 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
166 const struct object_id *gitmodules_oid, const char *path)
168 struct submodule_entry *entry;
169 unsigned int hash = hash_oid_string(gitmodules_oid, path);
170 struct submodule_entry key;
171 struct submodule key_config;
173 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
174 key_config.path = path;
176 hashmap_entry_init(&key.ent, hash);
177 key.config = &key_config;
179 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
180 if (entry)
181 return entry->config;
182 return NULL;
185 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
186 const struct object_id *gitmodules_oid, const char *name)
188 struct submodule_entry *entry;
189 unsigned int hash = hash_oid_string(gitmodules_oid, name);
190 struct submodule_entry key;
191 struct submodule key_config;
193 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
194 key_config.name = name;
196 hashmap_entry_init(&key.ent, hash);
197 key.config = &key_config;
199 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
200 if (entry)
201 return entry->config;
202 return NULL;
205 int check_submodule_name(const char *name)
207 /* Disallow empty names */
208 if (!*name)
209 return -1;
212 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
213 * separators rather than is_dir_sep(), because we want the name rules
214 * to be consistent across platforms.
216 goto in_component; /* always start inside component */
217 while (*name) {
218 char c = *name++;
219 if (is_xplatform_dir_sep(c)) {
220 in_component:
221 if (name[0] == '.' && name[1] == '.' &&
222 (!name[2] || is_xplatform_dir_sep(name[2])))
223 return -1;
227 return 0;
230 static int name_and_item_from_var(const char *var, struct strbuf *name,
231 struct strbuf *item)
233 const char *subsection, *key;
234 size_t subsection_len;
235 int parse;
236 parse = parse_config_key(var, "submodule", &subsection,
237 &subsection_len, &key);
238 if (parse < 0 || !subsection)
239 return 0;
241 strbuf_add(name, subsection, subsection_len);
242 if (check_submodule_name(name->buf) < 0) {
243 warning(_("ignoring suspicious submodule name: %s"), name->buf);
244 strbuf_release(name);
245 return 0;
248 strbuf_addstr(item, key);
250 return 1;
253 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
254 const struct object_id *gitmodules_oid, const char *name)
256 struct submodule *submodule;
257 struct strbuf name_buf = STRBUF_INIT;
259 submodule = cache_lookup_name(cache, gitmodules_oid, name);
260 if (submodule)
261 return submodule;
263 submodule = xmalloc(sizeof(*submodule));
265 strbuf_addstr(&name_buf, name);
266 submodule->name = strbuf_detach(&name_buf, NULL);
268 submodule->path = NULL;
269 submodule->url = NULL;
270 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
271 submodule->update_strategy.command = NULL;
272 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
273 submodule->ignore = NULL;
274 submodule->branch = NULL;
275 submodule->recommend_shallow = -1;
277 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
279 cache_add(cache, submodule);
281 return submodule;
284 static int parse_fetch_recurse(const char *opt, const char *arg,
285 int die_on_error)
287 switch (git_parse_maybe_bool(arg)) {
288 case 1:
289 return RECURSE_SUBMODULES_ON;
290 case 0:
291 return RECURSE_SUBMODULES_OFF;
292 default:
293 if (!strcmp(arg, "on-demand"))
294 return RECURSE_SUBMODULES_ON_DEMAND;
296 * Please update $__git_fetch_recurse_submodules in
297 * git-completion.bash when you add new options.
299 if (die_on_error)
300 die("bad %s argument: %s", opt, arg);
301 else
302 return RECURSE_SUBMODULES_ERROR;
306 int parse_submodule_fetchjobs(const char *var, const char *value)
308 int fetchjobs = git_config_int(var, value);
309 if (fetchjobs < 0)
310 die(_("negative values not allowed for submodule.fetchJobs"));
311 if (!fetchjobs)
312 fetchjobs = online_cpus();
313 return fetchjobs;
316 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
318 return parse_fetch_recurse(opt, arg, 1);
321 int option_fetch_parse_recurse_submodules(const struct option *opt,
322 const char *arg, int unset)
324 int *v;
326 if (!opt->value)
327 return -1;
329 v = opt->value;
331 if (unset) {
332 *v = RECURSE_SUBMODULES_OFF;
333 } else {
334 if (arg)
335 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
336 else
337 *v = RECURSE_SUBMODULES_ON;
339 return 0;
342 static int parse_update_recurse(const char *opt, const char *arg,
343 int die_on_error)
345 switch (git_parse_maybe_bool(arg)) {
346 case 1:
347 return RECURSE_SUBMODULES_ON;
348 case 0:
349 return RECURSE_SUBMODULES_OFF;
350 default:
351 if (die_on_error)
352 die("bad %s argument: %s", opt, arg);
353 return RECURSE_SUBMODULES_ERROR;
357 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
359 return parse_update_recurse(opt, arg, 1);
362 static int parse_push_recurse(const char *opt, const char *arg,
363 int die_on_error)
365 switch (git_parse_maybe_bool(arg)) {
366 case 1:
367 /* There's no simple "on" value when pushing */
368 if (die_on_error)
369 die("bad %s argument: %s", opt, arg);
370 else
371 return RECURSE_SUBMODULES_ERROR;
372 case 0:
373 return RECURSE_SUBMODULES_OFF;
374 default:
375 if (!strcmp(arg, "on-demand"))
376 return RECURSE_SUBMODULES_ON_DEMAND;
377 else if (!strcmp(arg, "check"))
378 return RECURSE_SUBMODULES_CHECK;
379 else if (!strcmp(arg, "only"))
380 return RECURSE_SUBMODULES_ONLY;
382 * Please update $__git_push_recurse_submodules in
383 * git-completion.bash when you add new modes.
385 else if (die_on_error)
386 die("bad %s argument: %s", opt, arg);
387 else
388 return RECURSE_SUBMODULES_ERROR;
392 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
394 return parse_push_recurse(opt, arg, 1);
397 static void warn_multiple_config(const struct object_id *treeish_name,
398 const char *name, const char *option)
400 const char *commit_string = "WORKTREE";
401 if (treeish_name)
402 commit_string = oid_to_hex(treeish_name);
403 warning("%s:.gitmodules, multiple configurations found for "
404 "'submodule.%s.%s'. Skipping second one!",
405 commit_string, name, option);
408 static void warn_command_line_option(const char *var, const char *value)
410 warning(_("ignoring '%s' which may be interpreted as"
411 " a command-line option: %s"), var, value);
414 struct parse_config_parameter {
415 struct submodule_cache *cache;
416 const struct object_id *treeish_name;
417 const struct object_id *gitmodules_oid;
418 int overwrite;
422 * Parse a config item from .gitmodules.
424 * This does not handle submodule-related configuration from the main
425 * config store (.git/config, etc). Callers are responsible for
426 * checking for overrides in the main config store when appropriate.
428 static int parse_config(const char *var, const char *value, void *data)
430 struct parse_config_parameter *me = data;
431 struct submodule *submodule;
432 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
433 int ret = 0;
435 /* this also ensures that we only parse submodule entries */
436 if (!name_and_item_from_var(var, &name, &item))
437 return 0;
439 submodule = lookup_or_create_by_name(me->cache,
440 me->gitmodules_oid,
441 name.buf);
443 if (!strcmp(item.buf, "path")) {
444 if (!value)
445 ret = config_error_nonbool(var);
446 else if (looks_like_command_line_option(value))
447 warn_command_line_option(var, value);
448 else if (!me->overwrite && submodule->path)
449 warn_multiple_config(me->treeish_name, submodule->name,
450 "path");
451 else {
452 if (submodule->path)
453 cache_remove_path(me->cache, submodule);
454 free((void *) submodule->path);
455 submodule->path = xstrdup(value);
456 cache_put_path(me->cache, submodule);
458 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
459 /* when parsing worktree configurations we can die early */
460 int die_on_error = is_null_oid(me->gitmodules_oid);
461 if (!me->overwrite &&
462 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
463 warn_multiple_config(me->treeish_name, submodule->name,
464 "fetchrecursesubmodules");
465 else
466 submodule->fetch_recurse = parse_fetch_recurse(
467 var, value,
468 die_on_error);
469 } else if (!strcmp(item.buf, "ignore")) {
470 if (!value)
471 ret = config_error_nonbool(var);
472 else if (!me->overwrite && submodule->ignore)
473 warn_multiple_config(me->treeish_name, submodule->name,
474 "ignore");
475 else if (strcmp(value, "untracked") &&
476 strcmp(value, "dirty") &&
477 strcmp(value, "all") &&
478 strcmp(value, "none"))
479 warning("Invalid parameter '%s' for config option "
480 "'submodule.%s.ignore'", value, name.buf);
481 else {
482 free((void *) submodule->ignore);
483 submodule->ignore = xstrdup(value);
485 } else if (!strcmp(item.buf, "url")) {
486 if (!value) {
487 ret = config_error_nonbool(var);
488 } else if (looks_like_command_line_option(value)) {
489 warn_command_line_option(var, value);
490 } else if (!me->overwrite && submodule->url) {
491 warn_multiple_config(me->treeish_name, submodule->name,
492 "url");
493 } else {
494 free((void *) submodule->url);
495 submodule->url = xstrdup(value);
497 } else if (!strcmp(item.buf, "update")) {
498 if (!value)
499 ret = config_error_nonbool(var);
500 else if (!me->overwrite &&
501 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
502 warn_multiple_config(me->treeish_name, submodule->name,
503 "update");
504 else if (parse_submodule_update_strategy(value,
505 &submodule->update_strategy) < 0 ||
506 submodule->update_strategy.type == SM_UPDATE_COMMAND)
507 die(_("invalid value for '%s'"), var);
508 } else if (!strcmp(item.buf, "shallow")) {
509 if (!me->overwrite && submodule->recommend_shallow != -1)
510 warn_multiple_config(me->treeish_name, submodule->name,
511 "shallow");
512 else
513 submodule->recommend_shallow =
514 git_config_bool(var, value);
515 } else if (!strcmp(item.buf, "branch")) {
516 if (!me->overwrite && submodule->branch)
517 warn_multiple_config(me->treeish_name, submodule->name,
518 "branch");
519 else {
520 free((void *)submodule->branch);
521 submodule->branch = xstrdup(value);
525 strbuf_release(&name);
526 strbuf_release(&item);
528 return ret;
531 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
532 struct object_id *gitmodules_oid,
533 struct strbuf *rev)
535 int ret = 0;
537 if (is_null_oid(treeish_name)) {
538 oidclr(gitmodules_oid);
539 return 1;
542 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
543 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
544 ret = 1;
546 return ret;
549 /* This does a lookup of a submodule configuration by name or by path
550 * (key) with on-demand reading of the appropriate .gitmodules from
551 * revisions.
553 static const struct submodule *config_from(struct submodule_cache *cache,
554 const struct object_id *treeish_name, const char *key,
555 enum lookup_type lookup_type)
557 struct strbuf rev = STRBUF_INIT;
558 unsigned long config_size;
559 char *config = NULL;
560 struct object_id oid;
561 enum object_type type;
562 const struct submodule *submodule = NULL;
563 struct parse_config_parameter parameter;
566 * If any parameter except the cache is a NULL pointer just
567 * return the first submodule. Can be used to check whether
568 * there are any submodules parsed.
570 if (!treeish_name || !key) {
571 struct hashmap_iter iter;
572 struct submodule_entry *entry;
574 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
575 struct submodule_entry,
576 ent /* member name */);
577 if (!entry)
578 return NULL;
579 return entry->config;
582 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
583 goto out;
585 switch (lookup_type) {
586 case lookup_name:
587 submodule = cache_lookup_name(cache, &oid, key);
588 break;
589 case lookup_path:
590 submodule = cache_lookup_path(cache, &oid, key);
591 break;
593 if (submodule)
594 goto out;
596 config = repo_read_object_file(the_repository, &oid, &type,
597 &config_size);
598 if (!config || type != OBJ_BLOB)
599 goto out;
601 /* fill the submodule config into the cache */
602 parameter.cache = cache;
603 parameter.treeish_name = treeish_name;
604 parameter.gitmodules_oid = &oid;
605 parameter.overwrite = 0;
606 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
607 config, config_size, &parameter, NULL);
608 strbuf_release(&rev);
609 free(config);
611 switch (lookup_type) {
612 case lookup_name:
613 return cache_lookup_name(cache, &oid, key);
614 case lookup_path:
615 return cache_lookup_path(cache, &oid, key);
616 default:
617 return NULL;
620 out:
621 strbuf_release(&rev);
622 free(config);
623 return submodule;
626 static void submodule_cache_check_init(struct repository *repo)
628 if (repo->submodule_cache && repo->submodule_cache->initialized)
629 return;
631 if (!repo->submodule_cache)
632 repo->submodule_cache = submodule_cache_alloc();
634 submodule_cache_init(repo->submodule_cache);
638 * Note: This function is private for a reason, the '.gitmodules' file should
639 * not be used as a mechanism to retrieve arbitrary configuration stored in
640 * the repository.
642 * Runs the provided config function on the '.gitmodules' file found in the
643 * working directory.
645 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
647 if (repo->worktree) {
648 struct git_config_source config_source = {
649 0, .scope = CONFIG_SCOPE_SUBMODULE
651 const struct config_options opts = { 0 };
652 struct object_id oid;
653 char *file;
654 char *oidstr = NULL;
656 file = repo_worktree_path(repo, GITMODULES_FILE);
657 if (file_exists(file)) {
658 config_source.file = file;
659 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
660 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
661 config_source.repo = repo;
662 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
663 if (repo != the_repository)
664 add_submodule_odb_by_path(repo->objects->odb->path);
665 } else {
666 goto out;
669 config_with_options(fn, data, &config_source, &opts);
671 out:
672 free(oidstr);
673 free(file);
677 static int gitmodules_cb(const char *var, const char *value, void *data)
679 struct repository *repo = data;
680 struct parse_config_parameter parameter;
682 parameter.cache = repo->submodule_cache;
683 parameter.treeish_name = NULL;
684 parameter.gitmodules_oid = null_oid();
685 parameter.overwrite = 1;
687 return parse_config(var, value, &parameter);
690 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
692 submodule_cache_check_init(repo);
694 if (repo->submodule_cache->gitmodules_read && skip_if_read)
695 return;
697 if (repo_read_index(repo) < 0)
698 return;
700 if (!is_gitmodules_unmerged(repo->index))
701 config_from_gitmodules(gitmodules_cb, repo, repo);
703 repo->submodule_cache->gitmodules_read = 1;
706 void gitmodules_config_oid(const struct object_id *commit_oid)
708 struct strbuf rev = STRBUF_INIT;
709 struct object_id oid;
711 submodule_cache_check_init(the_repository);
713 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
714 git_config_from_blob_oid(gitmodules_cb, rev.buf,
715 the_repository, &oid, the_repository);
717 strbuf_release(&rev);
719 the_repository->submodule_cache->gitmodules_read = 1;
722 const struct submodule *submodule_from_name(struct repository *r,
723 const struct object_id *treeish_name,
724 const char *name)
726 repo_read_gitmodules(r, 1);
727 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
730 const struct submodule *submodule_from_path(struct repository *r,
731 const struct object_id *treeish_name,
732 const char *path)
734 repo_read_gitmodules(r, 1);
735 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
739 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
740 * and appends submodule entries to 'out'. The submodule_cache expects
741 * a root-level treeish_name and paths, so keep track of these values
742 * with 'root_tree' and 'prefix'.
744 static void traverse_tree_submodules(struct repository *r,
745 const struct object_id *root_tree,
746 char *prefix,
747 const struct object_id *treeish_name,
748 struct submodule_entry_list *out)
750 struct tree_desc tree;
751 struct submodule_tree_entry *st_entry;
752 struct name_entry *name_entry;
753 char *tree_path = NULL;
755 name_entry = xmalloc(sizeof(*name_entry));
757 fill_tree_descriptor(r, &tree, treeish_name);
758 while (tree_entry(&tree, name_entry)) {
759 if (prefix)
760 tree_path =
761 mkpathdup("%s/%s", prefix, name_entry->path);
762 else
763 tree_path = xstrdup(name_entry->path);
765 if (S_ISGITLINK(name_entry->mode) &&
766 is_tree_submodule_active(r, root_tree, tree_path)) {
767 ALLOC_GROW(out->entries, out->entry_nr + 1,
768 out->entry_alloc);
769 st_entry = &out->entries[out->entry_nr++];
771 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
772 *st_entry->name_entry = *name_entry;
773 st_entry->submodule =
774 submodule_from_path(r, root_tree, tree_path);
775 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
776 if (repo_submodule_init(st_entry->repo, r, tree_path,
777 root_tree))
778 FREE_AND_NULL(st_entry->repo);
780 } else if (S_ISDIR(name_entry->mode))
781 traverse_tree_submodules(r, root_tree, tree_path,
782 &name_entry->oid, out);
783 free(tree_path);
787 void submodules_of_tree(struct repository *r,
788 const struct object_id *treeish_name,
789 struct submodule_entry_list *out)
791 CALLOC_ARRAY(out->entries, 0);
792 out->entry_nr = 0;
793 out->entry_alloc = 0;
795 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
798 void submodule_free(struct repository *r)
800 if (r->submodule_cache)
801 submodule_cache_clear(r->submodule_cache);
804 static int config_print_callback(const char *var, const char *value, void *cb_data)
806 char *wanted_key = cb_data;
808 if (!strcmp(wanted_key, var))
809 printf("%s\n", value);
811 return 0;
814 int print_config_from_gitmodules(struct repository *repo, const char *key)
816 int ret;
817 char *store_key;
819 ret = git_config_parse_key(key, &store_key, NULL);
820 if (ret < 0)
821 return CONFIG_INVALID_KEY;
823 config_from_gitmodules(config_print_callback, repo, store_key);
825 free(store_key);
826 return 0;
829 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
831 int ret;
833 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
834 if (ret < 0)
835 /* Maybe the user already did that, don't error out here */
836 warning(_("Could not update .gitmodules entry %s"), key);
838 return ret;
841 struct fetch_config {
842 int *max_children;
843 int *recurse_submodules;
846 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
848 struct fetch_config *config = cb;
849 if (!strcmp(var, "submodule.fetchjobs")) {
850 if (config->max_children)
851 *(config->max_children) =
852 parse_submodule_fetchjobs(var, value);
853 return 0;
854 } else if (!strcmp(var, "fetch.recursesubmodules")) {
855 if (config->recurse_submodules)
856 *(config->recurse_submodules) =
857 parse_fetch_recurse_submodules_arg(var, value);
858 return 0;
861 return 0;
864 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
866 struct fetch_config config = {
867 .max_children = max_children,
868 .recurse_submodules = recurse_submodules
870 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
873 static int gitmodules_update_clone_config(const char *var, const char *value,
874 void *cb)
876 int *max_jobs = cb;
877 if (!strcmp(var, "submodule.fetchjobs"))
878 *max_jobs = parse_submodule_fetchjobs(var, value);
879 return 0;
882 void update_clone_config_from_gitmodules(int *max_jobs)
884 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);