doc: remove manpage-base-url workaround
[git.git] / submodule-config.c
blob89a7bf0a93dd176d0d6f8e92bc84287484483428
1 #include "cache.h"
2 #include "alloc.h"
3 #include "dir.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "submodule-config.h"
8 #include "submodule.h"
9 #include "strbuf.h"
10 #include "object-store.h"
11 #include "parse-options.h"
12 #include "tree-walk.h"
15 * submodule cache lookup structure
16 * There is one shared set of 'struct submodule' entries which can be
17 * looked up by their sha1 blob id of the .gitmodules file and either
18 * using path or name as key.
19 * for_path stores submodule entries with path as key
20 * for_name stores submodule entries with name as key
22 struct submodule_cache {
23 struct hashmap for_path;
24 struct hashmap for_name;
25 unsigned initialized:1;
26 unsigned gitmodules_read:1;
30 * thin wrapper struct needed to insert 'struct submodule' entries to
31 * the hashmap
33 struct submodule_entry {
34 struct hashmap_entry ent;
35 struct submodule *config;
38 enum lookup_type {
39 lookup_name,
40 lookup_path
43 static int config_path_cmp(const void *cmp_data UNUSED,
44 const struct hashmap_entry *eptr,
45 const struct hashmap_entry *entry_or_key,
46 const void *keydata UNUSED)
48 const struct submodule_entry *a, *b;
50 a = container_of(eptr, const struct submodule_entry, ent);
51 b = container_of(entry_or_key, const struct submodule_entry, ent);
53 return strcmp(a->config->path, b->config->path) ||
54 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
57 static int config_name_cmp(const void *cmp_data UNUSED,
58 const struct hashmap_entry *eptr,
59 const struct hashmap_entry *entry_or_key,
60 const void *keydata UNUSED)
62 const struct submodule_entry *a, *b;
64 a = container_of(eptr, const struct submodule_entry, ent);
65 b = container_of(entry_or_key, const struct submodule_entry, ent);
67 return strcmp(a->config->name, b->config->name) ||
68 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
71 static struct submodule_cache *submodule_cache_alloc(void)
73 return xcalloc(1, sizeof(struct submodule_cache));
76 static void submodule_cache_init(struct submodule_cache *cache)
78 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
79 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
80 cache->initialized = 1;
83 static void free_one_config(struct submodule_entry *entry)
85 free((void *) entry->config->path);
86 free((void *) entry->config->name);
87 free((void *) entry->config->branch);
88 free((void *) entry->config->update_strategy.command);
89 free(entry->config);
92 static void submodule_cache_clear(struct submodule_cache *cache)
94 struct hashmap_iter iter;
95 struct submodule_entry *entry;
97 if (!cache->initialized)
98 return;
101 * We iterate over the name hash here to be symmetric with the
102 * allocation of struct submodule entries. Each is allocated by
103 * their .gitmodules blob sha1 and submodule name.
105 hashmap_for_each_entry(&cache->for_name, &iter, entry,
106 ent /* member name */)
107 free_one_config(entry);
109 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
110 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
111 cache->initialized = 0;
112 cache->gitmodules_read = 0;
115 void submodule_cache_free(struct submodule_cache *cache)
117 submodule_cache_clear(cache);
118 free(cache);
121 static unsigned int hash_oid_string(const struct object_id *oid,
122 const char *string)
124 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
127 static void cache_put_path(struct submodule_cache *cache,
128 struct submodule *submodule)
130 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
131 submodule->path);
132 struct submodule_entry *e = xmalloc(sizeof(*e));
133 hashmap_entry_init(&e->ent, hash);
134 e->config = submodule;
135 hashmap_put(&cache->for_path, &e->ent);
138 static void cache_remove_path(struct submodule_cache *cache,
139 struct submodule *submodule)
141 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
142 submodule->path);
143 struct submodule_entry e;
144 struct submodule_entry *removed;
145 hashmap_entry_init(&e.ent, hash);
146 e.config = submodule;
147 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
148 free(removed);
151 static void cache_add(struct submodule_cache *cache,
152 struct submodule *submodule)
154 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
155 submodule->name);
156 struct submodule_entry *e = xmalloc(sizeof(*e));
157 hashmap_entry_init(&e->ent, hash);
158 e->config = submodule;
159 hashmap_add(&cache->for_name, &e->ent);
162 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
163 const struct object_id *gitmodules_oid, const char *path)
165 struct submodule_entry *entry;
166 unsigned int hash = hash_oid_string(gitmodules_oid, path);
167 struct submodule_entry key;
168 struct submodule key_config;
170 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
171 key_config.path = path;
173 hashmap_entry_init(&key.ent, hash);
174 key.config = &key_config;
176 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
177 if (entry)
178 return entry->config;
179 return NULL;
182 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
183 const struct object_id *gitmodules_oid, const char *name)
185 struct submodule_entry *entry;
186 unsigned int hash = hash_oid_string(gitmodules_oid, name);
187 struct submodule_entry key;
188 struct submodule key_config;
190 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
191 key_config.name = name;
193 hashmap_entry_init(&key.ent, hash);
194 key.config = &key_config;
196 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
197 if (entry)
198 return entry->config;
199 return NULL;
202 int check_submodule_name(const char *name)
204 /* Disallow empty names */
205 if (!*name)
206 return -1;
209 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
210 * separators rather than is_dir_sep(), because we want the name rules
211 * to be consistent across platforms.
213 goto in_component; /* always start inside component */
214 while (*name) {
215 char c = *name++;
216 if (is_xplatform_dir_sep(c)) {
217 in_component:
218 if (name[0] == '.' && name[1] == '.' &&
219 (!name[2] || is_xplatform_dir_sep(name[2])))
220 return -1;
224 return 0;
227 static int name_and_item_from_var(const char *var, struct strbuf *name,
228 struct strbuf *item)
230 const char *subsection, *key;
231 size_t subsection_len;
232 int parse;
233 parse = parse_config_key(var, "submodule", &subsection,
234 &subsection_len, &key);
235 if (parse < 0 || !subsection)
236 return 0;
238 strbuf_add(name, subsection, subsection_len);
239 if (check_submodule_name(name->buf) < 0) {
240 warning(_("ignoring suspicious submodule name: %s"), name->buf);
241 strbuf_release(name);
242 return 0;
245 strbuf_addstr(item, key);
247 return 1;
250 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
251 const struct object_id *gitmodules_oid, const char *name)
253 struct submodule *submodule;
254 struct strbuf name_buf = STRBUF_INIT;
256 submodule = cache_lookup_name(cache, gitmodules_oid, name);
257 if (submodule)
258 return submodule;
260 submodule = xmalloc(sizeof(*submodule));
262 strbuf_addstr(&name_buf, name);
263 submodule->name = strbuf_detach(&name_buf, NULL);
265 submodule->path = NULL;
266 submodule->url = NULL;
267 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
268 submodule->update_strategy.command = NULL;
269 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
270 submodule->ignore = NULL;
271 submodule->branch = NULL;
272 submodule->recommend_shallow = -1;
274 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
276 cache_add(cache, submodule);
278 return submodule;
281 static int parse_fetch_recurse(const char *opt, const char *arg,
282 int die_on_error)
284 switch (git_parse_maybe_bool(arg)) {
285 case 1:
286 return RECURSE_SUBMODULES_ON;
287 case 0:
288 return RECURSE_SUBMODULES_OFF;
289 default:
290 if (!strcmp(arg, "on-demand"))
291 return RECURSE_SUBMODULES_ON_DEMAND;
293 * Please update $__git_fetch_recurse_submodules in
294 * git-completion.bash when you add new options.
296 if (die_on_error)
297 die("bad %s argument: %s", opt, arg);
298 else
299 return RECURSE_SUBMODULES_ERROR;
303 int parse_submodule_fetchjobs(const char *var, const char *value)
305 int fetchjobs = git_config_int(var, value);
306 if (fetchjobs < 0)
307 die(_("negative values not allowed for submodule.fetchJobs"));
308 if (!fetchjobs)
309 fetchjobs = online_cpus();
310 return fetchjobs;
313 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
315 return parse_fetch_recurse(opt, arg, 1);
318 int option_fetch_parse_recurse_submodules(const struct option *opt,
319 const char *arg, int unset)
321 int *v;
323 if (!opt->value)
324 return -1;
326 v = opt->value;
328 if (unset) {
329 *v = RECURSE_SUBMODULES_OFF;
330 } else {
331 if (arg)
332 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
333 else
334 *v = RECURSE_SUBMODULES_ON;
336 return 0;
339 static int parse_update_recurse(const char *opt, const char *arg,
340 int die_on_error)
342 switch (git_parse_maybe_bool(arg)) {
343 case 1:
344 return RECURSE_SUBMODULES_ON;
345 case 0:
346 return RECURSE_SUBMODULES_OFF;
347 default:
348 if (die_on_error)
349 die("bad %s argument: %s", opt, arg);
350 return RECURSE_SUBMODULES_ERROR;
354 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
356 return parse_update_recurse(opt, arg, 1);
359 static int parse_push_recurse(const char *opt, const char *arg,
360 int die_on_error)
362 switch (git_parse_maybe_bool(arg)) {
363 case 1:
364 /* There's no simple "on" value when pushing */
365 if (die_on_error)
366 die("bad %s argument: %s", opt, arg);
367 else
368 return RECURSE_SUBMODULES_ERROR;
369 case 0:
370 return RECURSE_SUBMODULES_OFF;
371 default:
372 if (!strcmp(arg, "on-demand"))
373 return RECURSE_SUBMODULES_ON_DEMAND;
374 else if (!strcmp(arg, "check"))
375 return RECURSE_SUBMODULES_CHECK;
376 else if (!strcmp(arg, "only"))
377 return RECURSE_SUBMODULES_ONLY;
379 * Please update $__git_push_recurse_submodules in
380 * git-completion.bash when you add new modes.
382 else if (die_on_error)
383 die("bad %s argument: %s", opt, arg);
384 else
385 return RECURSE_SUBMODULES_ERROR;
389 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
391 return parse_push_recurse(opt, arg, 1);
394 static void warn_multiple_config(const struct object_id *treeish_name,
395 const char *name, const char *option)
397 const char *commit_string = "WORKTREE";
398 if (treeish_name)
399 commit_string = oid_to_hex(treeish_name);
400 warning("%s:.gitmodules, multiple configurations found for "
401 "'submodule.%s.%s'. Skipping second one!",
402 commit_string, name, option);
405 static void warn_command_line_option(const char *var, const char *value)
407 warning(_("ignoring '%s' which may be interpreted as"
408 " a command-line option: %s"), var, value);
411 struct parse_config_parameter {
412 struct submodule_cache *cache;
413 const struct object_id *treeish_name;
414 const struct object_id *gitmodules_oid;
415 int overwrite;
419 * Parse a config item from .gitmodules.
421 * This does not handle submodule-related configuration from the main
422 * config store (.git/config, etc). Callers are responsible for
423 * checking for overrides in the main config store when appropriate.
425 static int parse_config(const char *var, const char *value, void *data)
427 struct parse_config_parameter *me = data;
428 struct submodule *submodule;
429 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
430 int ret = 0;
432 /* this also ensures that we only parse submodule entries */
433 if (!name_and_item_from_var(var, &name, &item))
434 return 0;
436 submodule = lookup_or_create_by_name(me->cache,
437 me->gitmodules_oid,
438 name.buf);
440 if (!strcmp(item.buf, "path")) {
441 if (!value)
442 ret = config_error_nonbool(var);
443 else if (looks_like_command_line_option(value))
444 warn_command_line_option(var, value);
445 else if (!me->overwrite && submodule->path)
446 warn_multiple_config(me->treeish_name, submodule->name,
447 "path");
448 else {
449 if (submodule->path)
450 cache_remove_path(me->cache, submodule);
451 free((void *) submodule->path);
452 submodule->path = xstrdup(value);
453 cache_put_path(me->cache, submodule);
455 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
456 /* when parsing worktree configurations we can die early */
457 int die_on_error = is_null_oid(me->gitmodules_oid);
458 if (!me->overwrite &&
459 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
460 warn_multiple_config(me->treeish_name, submodule->name,
461 "fetchrecursesubmodules");
462 else
463 submodule->fetch_recurse = parse_fetch_recurse(
464 var, value,
465 die_on_error);
466 } else if (!strcmp(item.buf, "ignore")) {
467 if (!value)
468 ret = config_error_nonbool(var);
469 else if (!me->overwrite && submodule->ignore)
470 warn_multiple_config(me->treeish_name, submodule->name,
471 "ignore");
472 else if (strcmp(value, "untracked") &&
473 strcmp(value, "dirty") &&
474 strcmp(value, "all") &&
475 strcmp(value, "none"))
476 warning("Invalid parameter '%s' for config option "
477 "'submodule.%s.ignore'", value, name.buf);
478 else {
479 free((void *) submodule->ignore);
480 submodule->ignore = xstrdup(value);
482 } else if (!strcmp(item.buf, "url")) {
483 if (!value) {
484 ret = config_error_nonbool(var);
485 } else if (looks_like_command_line_option(value)) {
486 warn_command_line_option(var, value);
487 } else if (!me->overwrite && submodule->url) {
488 warn_multiple_config(me->treeish_name, submodule->name,
489 "url");
490 } else {
491 free((void *) submodule->url);
492 submodule->url = xstrdup(value);
494 } else if (!strcmp(item.buf, "update")) {
495 if (!value)
496 ret = config_error_nonbool(var);
497 else if (!me->overwrite &&
498 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
499 warn_multiple_config(me->treeish_name, submodule->name,
500 "update");
501 else if (parse_submodule_update_strategy(value,
502 &submodule->update_strategy) < 0 ||
503 submodule->update_strategy.type == SM_UPDATE_COMMAND)
504 die(_("invalid value for '%s'"), var);
505 } else if (!strcmp(item.buf, "shallow")) {
506 if (!me->overwrite && submodule->recommend_shallow != -1)
507 warn_multiple_config(me->treeish_name, submodule->name,
508 "shallow");
509 else
510 submodule->recommend_shallow =
511 git_config_bool(var, value);
512 } else if (!strcmp(item.buf, "branch")) {
513 if (!me->overwrite && submodule->branch)
514 warn_multiple_config(me->treeish_name, submodule->name,
515 "branch");
516 else {
517 free((void *)submodule->branch);
518 submodule->branch = xstrdup(value);
522 strbuf_release(&name);
523 strbuf_release(&item);
525 return ret;
528 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
529 struct object_id *gitmodules_oid,
530 struct strbuf *rev)
532 int ret = 0;
534 if (is_null_oid(treeish_name)) {
535 oidclr(gitmodules_oid);
536 return 1;
539 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
540 if (get_oid(rev->buf, gitmodules_oid) >= 0)
541 ret = 1;
543 return ret;
546 /* This does a lookup of a submodule configuration by name or by path
547 * (key) with on-demand reading of the appropriate .gitmodules from
548 * revisions.
550 static const struct submodule *config_from(struct submodule_cache *cache,
551 const struct object_id *treeish_name, const char *key,
552 enum lookup_type lookup_type)
554 struct strbuf rev = STRBUF_INIT;
555 unsigned long config_size;
556 char *config = NULL;
557 struct object_id oid;
558 enum object_type type;
559 const struct submodule *submodule = NULL;
560 struct parse_config_parameter parameter;
563 * If any parameter except the cache is a NULL pointer just
564 * return the first submodule. Can be used to check whether
565 * there are any submodules parsed.
567 if (!treeish_name || !key) {
568 struct hashmap_iter iter;
569 struct submodule_entry *entry;
571 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
572 struct submodule_entry,
573 ent /* member name */);
574 if (!entry)
575 return NULL;
576 return entry->config;
579 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
580 goto out;
582 switch (lookup_type) {
583 case lookup_name:
584 submodule = cache_lookup_name(cache, &oid, key);
585 break;
586 case lookup_path:
587 submodule = cache_lookup_path(cache, &oid, key);
588 break;
590 if (submodule)
591 goto out;
593 config = read_object_file(&oid, &type, &config_size);
594 if (!config || type != OBJ_BLOB)
595 goto out;
597 /* fill the submodule config into the cache */
598 parameter.cache = cache;
599 parameter.treeish_name = treeish_name;
600 parameter.gitmodules_oid = &oid;
601 parameter.overwrite = 0;
602 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
603 config, config_size, &parameter, NULL);
604 strbuf_release(&rev);
605 free(config);
607 switch (lookup_type) {
608 case lookup_name:
609 return cache_lookup_name(cache, &oid, key);
610 case lookup_path:
611 return cache_lookup_path(cache, &oid, key);
612 default:
613 return NULL;
616 out:
617 strbuf_release(&rev);
618 free(config);
619 return submodule;
622 static void submodule_cache_check_init(struct repository *repo)
624 if (repo->submodule_cache && repo->submodule_cache->initialized)
625 return;
627 if (!repo->submodule_cache)
628 repo->submodule_cache = submodule_cache_alloc();
630 submodule_cache_init(repo->submodule_cache);
634 * Note: This function is private for a reason, the '.gitmodules' file should
635 * not be used as a mechanism to retrieve arbitrary configuration stored in
636 * the repository.
638 * Runs the provided config function on the '.gitmodules' file found in the
639 * working directory.
641 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
643 if (repo->worktree) {
644 struct git_config_source config_source = {
645 0, .scope = CONFIG_SCOPE_SUBMODULE
647 const struct config_options opts = { 0 };
648 struct object_id oid;
649 char *file;
650 char *oidstr = NULL;
652 file = repo_worktree_path(repo, GITMODULES_FILE);
653 if (file_exists(file)) {
654 config_source.file = file;
655 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
656 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
657 config_source.repo = repo;
658 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
659 if (repo != the_repository)
660 add_submodule_odb_by_path(repo->objects->odb->path);
661 } else {
662 goto out;
665 config_with_options(fn, data, &config_source, &opts);
667 out:
668 free(oidstr);
669 free(file);
673 static int gitmodules_cb(const char *var, const char *value, void *data)
675 struct repository *repo = data;
676 struct parse_config_parameter parameter;
678 parameter.cache = repo->submodule_cache;
679 parameter.treeish_name = NULL;
680 parameter.gitmodules_oid = null_oid();
681 parameter.overwrite = 1;
683 return parse_config(var, value, &parameter);
686 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
688 submodule_cache_check_init(repo);
690 if (repo->submodule_cache->gitmodules_read && skip_if_read)
691 return;
693 if (repo_read_index(repo) < 0)
694 return;
696 if (!is_gitmodules_unmerged(repo->index))
697 config_from_gitmodules(gitmodules_cb, repo, repo);
699 repo->submodule_cache->gitmodules_read = 1;
702 void gitmodules_config_oid(const struct object_id *commit_oid)
704 struct strbuf rev = STRBUF_INIT;
705 struct object_id oid;
707 submodule_cache_check_init(the_repository);
709 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
710 git_config_from_blob_oid(gitmodules_cb, rev.buf,
711 the_repository, &oid, the_repository);
713 strbuf_release(&rev);
715 the_repository->submodule_cache->gitmodules_read = 1;
718 const struct submodule *submodule_from_name(struct repository *r,
719 const struct object_id *treeish_name,
720 const char *name)
722 repo_read_gitmodules(r, 1);
723 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
726 const struct submodule *submodule_from_path(struct repository *r,
727 const struct object_id *treeish_name,
728 const char *path)
730 repo_read_gitmodules(r, 1);
731 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
735 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
736 * and appends submodule entries to 'out'. The submodule_cache expects
737 * a root-level treeish_name and paths, so keep track of these values
738 * with 'root_tree' and 'prefix'.
740 static void traverse_tree_submodules(struct repository *r,
741 const struct object_id *root_tree,
742 char *prefix,
743 const struct object_id *treeish_name,
744 struct submodule_entry_list *out)
746 struct tree_desc tree;
747 struct submodule_tree_entry *st_entry;
748 struct name_entry *name_entry;
749 char *tree_path = NULL;
751 name_entry = xmalloc(sizeof(*name_entry));
753 fill_tree_descriptor(r, &tree, treeish_name);
754 while (tree_entry(&tree, name_entry)) {
755 if (prefix)
756 tree_path =
757 mkpathdup("%s/%s", prefix, name_entry->path);
758 else
759 tree_path = xstrdup(name_entry->path);
761 if (S_ISGITLINK(name_entry->mode) &&
762 is_tree_submodule_active(r, root_tree, tree_path)) {
763 ALLOC_GROW(out->entries, out->entry_nr + 1,
764 out->entry_alloc);
765 st_entry = &out->entries[out->entry_nr++];
767 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
768 *st_entry->name_entry = *name_entry;
769 st_entry->submodule =
770 submodule_from_path(r, root_tree, tree_path);
771 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
772 if (repo_submodule_init(st_entry->repo, r, tree_path,
773 root_tree))
774 FREE_AND_NULL(st_entry->repo);
776 } else if (S_ISDIR(name_entry->mode))
777 traverse_tree_submodules(r, root_tree, tree_path,
778 &name_entry->oid, out);
779 free(tree_path);
783 void submodules_of_tree(struct repository *r,
784 const struct object_id *treeish_name,
785 struct submodule_entry_list *out)
787 CALLOC_ARRAY(out->entries, 0);
788 out->entry_nr = 0;
789 out->entry_alloc = 0;
791 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
794 void submodule_free(struct repository *r)
796 if (r->submodule_cache)
797 submodule_cache_clear(r->submodule_cache);
800 static int config_print_callback(const char *var, const char *value, void *cb_data)
802 char *wanted_key = cb_data;
804 if (!strcmp(wanted_key, var))
805 printf("%s\n", value);
807 return 0;
810 int print_config_from_gitmodules(struct repository *repo, const char *key)
812 int ret;
813 char *store_key;
815 ret = git_config_parse_key(key, &store_key, NULL);
816 if (ret < 0)
817 return CONFIG_INVALID_KEY;
819 config_from_gitmodules(config_print_callback, repo, store_key);
821 free(store_key);
822 return 0;
825 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
827 int ret;
829 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
830 if (ret < 0)
831 /* Maybe the user already did that, don't error out here */
832 warning(_("Could not update .gitmodules entry %s"), key);
834 return ret;
837 struct fetch_config {
838 int *max_children;
839 int *recurse_submodules;
842 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
844 struct fetch_config *config = cb;
845 if (!strcmp(var, "submodule.fetchjobs")) {
846 if (config->max_children)
847 *(config->max_children) =
848 parse_submodule_fetchjobs(var, value);
849 return 0;
850 } else if (!strcmp(var, "fetch.recursesubmodules")) {
851 if (config->recurse_submodules)
852 *(config->recurse_submodules) =
853 parse_fetch_recurse_submodules_arg(var, value);
854 return 0;
857 return 0;
860 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
862 struct fetch_config config = {
863 .max_children = max_children,
864 .recurse_submodules = recurse_submodules
866 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
869 static int gitmodules_update_clone_config(const char *var, const char *value,
870 void *cb)
872 int *max_jobs = cb;
873 if (!strcmp(var, "submodule.fetchjobs"))
874 *max_jobs = parse_submodule_fetchjobs(var, value);
875 return 0;
878 void update_clone_config_from_gitmodules(int *max_jobs)
880 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);