repository-version.txt: partialClone casing change
[git/debian.git] / submodule-config.c
blob4dc61b3a78a2916d737edf613af0f9c0e03d39c8
1 #include "cache.h"
2 #include "dir.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "submodule-config.h"
6 #include "submodule.h"
7 #include "strbuf.h"
8 #include "object-store.h"
9 #include "parse-options.h"
10 #include "tree-walk.h"
13 * submodule cache lookup structure
14 * There is one shared set of 'struct submodule' entries which can be
15 * looked up by their sha1 blob id of the .gitmodules file and either
16 * using path or name as key.
17 * for_path stores submodule entries with path as key
18 * for_name stores submodule entries with name as key
20 struct submodule_cache {
21 struct hashmap for_path;
22 struct hashmap for_name;
23 unsigned initialized:1;
24 unsigned gitmodules_read:1;
28 * thin wrapper struct needed to insert 'struct submodule' entries to
29 * the hashmap
31 struct submodule_entry {
32 struct hashmap_entry ent;
33 struct submodule *config;
36 enum lookup_type {
37 lookup_name,
38 lookup_path
41 static int config_path_cmp(const void *cmp_data UNUSED,
42 const struct hashmap_entry *eptr,
43 const struct hashmap_entry *entry_or_key,
44 const void *keydata UNUSED)
46 const struct submodule_entry *a, *b;
48 a = container_of(eptr, const struct submodule_entry, ent);
49 b = container_of(entry_or_key, const struct submodule_entry, ent);
51 return strcmp(a->config->path, b->config->path) ||
52 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
55 static int config_name_cmp(const void *cmp_data UNUSED,
56 const struct hashmap_entry *eptr,
57 const struct hashmap_entry *entry_or_key,
58 const void *keydata UNUSED)
60 const struct submodule_entry *a, *b;
62 a = container_of(eptr, const struct submodule_entry, ent);
63 b = container_of(entry_or_key, const struct submodule_entry, ent);
65 return strcmp(a->config->name, b->config->name) ||
66 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
69 static struct submodule_cache *submodule_cache_alloc(void)
71 return xcalloc(1, sizeof(struct submodule_cache));
74 static void submodule_cache_init(struct submodule_cache *cache)
76 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
77 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
78 cache->initialized = 1;
81 static void free_one_config(struct submodule_entry *entry)
83 free((void *) entry->config->path);
84 free((void *) entry->config->name);
85 free((void *) entry->config->branch);
86 free((void *) entry->config->update_strategy.command);
87 free(entry->config);
90 static void submodule_cache_clear(struct submodule_cache *cache)
92 struct hashmap_iter iter;
93 struct submodule_entry *entry;
95 if (!cache->initialized)
96 return;
99 * We iterate over the name hash here to be symmetric with the
100 * allocation of struct submodule entries. Each is allocated by
101 * their .gitmodules blob sha1 and submodule name.
103 hashmap_for_each_entry(&cache->for_name, &iter, entry,
104 ent /* member name */)
105 free_one_config(entry);
107 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
108 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
109 cache->initialized = 0;
110 cache->gitmodules_read = 0;
113 void submodule_cache_free(struct submodule_cache *cache)
115 submodule_cache_clear(cache);
116 free(cache);
119 static unsigned int hash_oid_string(const struct object_id *oid,
120 const char *string)
122 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
125 static void cache_put_path(struct submodule_cache *cache,
126 struct submodule *submodule)
128 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
129 submodule->path);
130 struct submodule_entry *e = xmalloc(sizeof(*e));
131 hashmap_entry_init(&e->ent, hash);
132 e->config = submodule;
133 hashmap_put(&cache->for_path, &e->ent);
136 static void cache_remove_path(struct submodule_cache *cache,
137 struct submodule *submodule)
139 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
140 submodule->path);
141 struct submodule_entry e;
142 struct submodule_entry *removed;
143 hashmap_entry_init(&e.ent, hash);
144 e.config = submodule;
145 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
146 free(removed);
149 static void cache_add(struct submodule_cache *cache,
150 struct submodule *submodule)
152 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
153 submodule->name);
154 struct submodule_entry *e = xmalloc(sizeof(*e));
155 hashmap_entry_init(&e->ent, hash);
156 e->config = submodule;
157 hashmap_add(&cache->for_name, &e->ent);
160 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
161 const struct object_id *gitmodules_oid, const char *path)
163 struct submodule_entry *entry;
164 unsigned int hash = hash_oid_string(gitmodules_oid, path);
165 struct submodule_entry key;
166 struct submodule key_config;
168 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
169 key_config.path = path;
171 hashmap_entry_init(&key.ent, hash);
172 key.config = &key_config;
174 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
175 if (entry)
176 return entry->config;
177 return NULL;
180 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
181 const struct object_id *gitmodules_oid, const char *name)
183 struct submodule_entry *entry;
184 unsigned int hash = hash_oid_string(gitmodules_oid, name);
185 struct submodule_entry key;
186 struct submodule key_config;
188 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
189 key_config.name = name;
191 hashmap_entry_init(&key.ent, hash);
192 key.config = &key_config;
194 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
195 if (entry)
196 return entry->config;
197 return NULL;
200 int check_submodule_name(const char *name)
202 /* Disallow empty names */
203 if (!*name)
204 return -1;
207 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
208 * separators rather than is_dir_sep(), because we want the name rules
209 * to be consistent across platforms.
211 goto in_component; /* always start inside component */
212 while (*name) {
213 char c = *name++;
214 if (is_xplatform_dir_sep(c)) {
215 in_component:
216 if (name[0] == '.' && name[1] == '.' &&
217 (!name[2] || is_xplatform_dir_sep(name[2])))
218 return -1;
222 return 0;
225 static int name_and_item_from_var(const char *var, struct strbuf *name,
226 struct strbuf *item)
228 const char *subsection, *key;
229 size_t subsection_len;
230 int parse;
231 parse = parse_config_key(var, "submodule", &subsection,
232 &subsection_len, &key);
233 if (parse < 0 || !subsection)
234 return 0;
236 strbuf_add(name, subsection, subsection_len);
237 if (check_submodule_name(name->buf) < 0) {
238 warning(_("ignoring suspicious submodule name: %s"), name->buf);
239 strbuf_release(name);
240 return 0;
243 strbuf_addstr(item, key);
245 return 1;
248 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
249 const struct object_id *gitmodules_oid, const char *name)
251 struct submodule *submodule;
252 struct strbuf name_buf = STRBUF_INIT;
254 submodule = cache_lookup_name(cache, gitmodules_oid, name);
255 if (submodule)
256 return submodule;
258 submodule = xmalloc(sizeof(*submodule));
260 strbuf_addstr(&name_buf, name);
261 submodule->name = strbuf_detach(&name_buf, NULL);
263 submodule->path = NULL;
264 submodule->url = NULL;
265 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
266 submodule->update_strategy.command = NULL;
267 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
268 submodule->ignore = NULL;
269 submodule->branch = NULL;
270 submodule->recommend_shallow = -1;
272 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
274 cache_add(cache, submodule);
276 return submodule;
279 static int parse_fetch_recurse(const char *opt, const char *arg,
280 int die_on_error)
282 switch (git_parse_maybe_bool(arg)) {
283 case 1:
284 return RECURSE_SUBMODULES_ON;
285 case 0:
286 return RECURSE_SUBMODULES_OFF;
287 default:
288 if (!strcmp(arg, "on-demand"))
289 return RECURSE_SUBMODULES_ON_DEMAND;
291 * Please update $__git_fetch_recurse_submodules in
292 * git-completion.bash when you add new options.
294 if (die_on_error)
295 die("bad %s argument: %s", opt, arg);
296 else
297 return RECURSE_SUBMODULES_ERROR;
301 int parse_submodule_fetchjobs(const char *var, const char *value)
303 int fetchjobs = git_config_int(var, value);
304 if (fetchjobs < 0)
305 die(_("negative values not allowed for submodule.fetchJobs"));
306 if (!fetchjobs)
307 fetchjobs = online_cpus();
308 return fetchjobs;
311 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
313 return parse_fetch_recurse(opt, arg, 1);
316 int option_fetch_parse_recurse_submodules(const struct option *opt,
317 const char *arg, int unset)
319 int *v;
321 if (!opt->value)
322 return -1;
324 v = opt->value;
326 if (unset) {
327 *v = RECURSE_SUBMODULES_OFF;
328 } else {
329 if (arg)
330 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
331 else
332 *v = RECURSE_SUBMODULES_ON;
334 return 0;
337 static int parse_update_recurse(const char *opt, const char *arg,
338 int die_on_error)
340 switch (git_parse_maybe_bool(arg)) {
341 case 1:
342 return RECURSE_SUBMODULES_ON;
343 case 0:
344 return RECURSE_SUBMODULES_OFF;
345 default:
346 if (die_on_error)
347 die("bad %s argument: %s", opt, arg);
348 return RECURSE_SUBMODULES_ERROR;
352 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
354 return parse_update_recurse(opt, arg, 1);
357 static int parse_push_recurse(const char *opt, const char *arg,
358 int die_on_error)
360 switch (git_parse_maybe_bool(arg)) {
361 case 1:
362 /* There's no simple "on" value when pushing */
363 if (die_on_error)
364 die("bad %s argument: %s", opt, arg);
365 else
366 return RECURSE_SUBMODULES_ERROR;
367 case 0:
368 return RECURSE_SUBMODULES_OFF;
369 default:
370 if (!strcmp(arg, "on-demand"))
371 return RECURSE_SUBMODULES_ON_DEMAND;
372 else if (!strcmp(arg, "check"))
373 return RECURSE_SUBMODULES_CHECK;
374 else if (!strcmp(arg, "only"))
375 return RECURSE_SUBMODULES_ONLY;
377 * Please update $__git_push_recurse_submodules in
378 * git-completion.bash when you add new modes.
380 else if (die_on_error)
381 die("bad %s argument: %s", opt, arg);
382 else
383 return RECURSE_SUBMODULES_ERROR;
387 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
389 return parse_push_recurse(opt, arg, 1);
392 static void warn_multiple_config(const struct object_id *treeish_name,
393 const char *name, const char *option)
395 const char *commit_string = "WORKTREE";
396 if (treeish_name)
397 commit_string = oid_to_hex(treeish_name);
398 warning("%s:.gitmodules, multiple configurations found for "
399 "'submodule.%s.%s'. Skipping second one!",
400 commit_string, name, option);
403 static void warn_command_line_option(const char *var, const char *value)
405 warning(_("ignoring '%s' which may be interpreted as"
406 " a command-line option: %s"), var, value);
409 struct parse_config_parameter {
410 struct submodule_cache *cache;
411 const struct object_id *treeish_name;
412 const struct object_id *gitmodules_oid;
413 int overwrite;
417 * Parse a config item from .gitmodules.
419 * This does not handle submodule-related configuration from the main
420 * config store (.git/config, etc). Callers are responsible for
421 * checking for overrides in the main config store when appropriate.
423 static int parse_config(const char *var, const char *value, void *data)
425 struct parse_config_parameter *me = data;
426 struct submodule *submodule;
427 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
428 int ret = 0;
430 /* this also ensures that we only parse submodule entries */
431 if (!name_and_item_from_var(var, &name, &item))
432 return 0;
434 submodule = lookup_or_create_by_name(me->cache,
435 me->gitmodules_oid,
436 name.buf);
438 if (!strcmp(item.buf, "path")) {
439 if (!value)
440 ret = config_error_nonbool(var);
441 else if (looks_like_command_line_option(value))
442 warn_command_line_option(var, value);
443 else if (!me->overwrite && submodule->path)
444 warn_multiple_config(me->treeish_name, submodule->name,
445 "path");
446 else {
447 if (submodule->path)
448 cache_remove_path(me->cache, submodule);
449 free((void *) submodule->path);
450 submodule->path = xstrdup(value);
451 cache_put_path(me->cache, submodule);
453 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
454 /* when parsing worktree configurations we can die early */
455 int die_on_error = is_null_oid(me->gitmodules_oid);
456 if (!me->overwrite &&
457 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
458 warn_multiple_config(me->treeish_name, submodule->name,
459 "fetchrecursesubmodules");
460 else
461 submodule->fetch_recurse = parse_fetch_recurse(
462 var, value,
463 die_on_error);
464 } else if (!strcmp(item.buf, "ignore")) {
465 if (!value)
466 ret = config_error_nonbool(var);
467 else if (!me->overwrite && submodule->ignore)
468 warn_multiple_config(me->treeish_name, submodule->name,
469 "ignore");
470 else if (strcmp(value, "untracked") &&
471 strcmp(value, "dirty") &&
472 strcmp(value, "all") &&
473 strcmp(value, "none"))
474 warning("Invalid parameter '%s' for config option "
475 "'submodule.%s.ignore'", value, name.buf);
476 else {
477 free((void *) submodule->ignore);
478 submodule->ignore = xstrdup(value);
480 } else if (!strcmp(item.buf, "url")) {
481 if (!value) {
482 ret = config_error_nonbool(var);
483 } else if (looks_like_command_line_option(value)) {
484 warn_command_line_option(var, value);
485 } else if (!me->overwrite && submodule->url) {
486 warn_multiple_config(me->treeish_name, submodule->name,
487 "url");
488 } else {
489 free((void *) submodule->url);
490 submodule->url = xstrdup(value);
492 } else if (!strcmp(item.buf, "update")) {
493 if (!value)
494 ret = config_error_nonbool(var);
495 else if (!me->overwrite &&
496 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
497 warn_multiple_config(me->treeish_name, submodule->name,
498 "update");
499 else if (parse_submodule_update_strategy(value,
500 &submodule->update_strategy) < 0 ||
501 submodule->update_strategy.type == SM_UPDATE_COMMAND)
502 die(_("invalid value for '%s'"), var);
503 } else if (!strcmp(item.buf, "shallow")) {
504 if (!me->overwrite && submodule->recommend_shallow != -1)
505 warn_multiple_config(me->treeish_name, submodule->name,
506 "shallow");
507 else
508 submodule->recommend_shallow =
509 git_config_bool(var, value);
510 } else if (!strcmp(item.buf, "branch")) {
511 if (!me->overwrite && submodule->branch)
512 warn_multiple_config(me->treeish_name, submodule->name,
513 "branch");
514 else {
515 free((void *)submodule->branch);
516 submodule->branch = xstrdup(value);
520 strbuf_release(&name);
521 strbuf_release(&item);
523 return ret;
526 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
527 struct object_id *gitmodules_oid,
528 struct strbuf *rev)
530 int ret = 0;
532 if (is_null_oid(treeish_name)) {
533 oidclr(gitmodules_oid);
534 return 1;
537 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
538 if (get_oid(rev->buf, gitmodules_oid) >= 0)
539 ret = 1;
541 return ret;
544 /* This does a lookup of a submodule configuration by name or by path
545 * (key) with on-demand reading of the appropriate .gitmodules from
546 * revisions.
548 static const struct submodule *config_from(struct submodule_cache *cache,
549 const struct object_id *treeish_name, const char *key,
550 enum lookup_type lookup_type)
552 struct strbuf rev = STRBUF_INIT;
553 unsigned long config_size;
554 char *config = NULL;
555 struct object_id oid;
556 enum object_type type;
557 const struct submodule *submodule = NULL;
558 struct parse_config_parameter parameter;
561 * If any parameter except the cache is a NULL pointer just
562 * return the first submodule. Can be used to check whether
563 * there are any submodules parsed.
565 if (!treeish_name || !key) {
566 struct hashmap_iter iter;
567 struct submodule_entry *entry;
569 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
570 struct submodule_entry,
571 ent /* member name */);
572 if (!entry)
573 return NULL;
574 return entry->config;
577 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
578 goto out;
580 switch (lookup_type) {
581 case lookup_name:
582 submodule = cache_lookup_name(cache, &oid, key);
583 break;
584 case lookup_path:
585 submodule = cache_lookup_path(cache, &oid, key);
586 break;
588 if (submodule)
589 goto out;
591 config = read_object_file(&oid, &type, &config_size);
592 if (!config || type != OBJ_BLOB)
593 goto out;
595 /* fill the submodule config into the cache */
596 parameter.cache = cache;
597 parameter.treeish_name = treeish_name;
598 parameter.gitmodules_oid = &oid;
599 parameter.overwrite = 0;
600 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
601 config, config_size, &parameter, NULL);
602 strbuf_release(&rev);
603 free(config);
605 switch (lookup_type) {
606 case lookup_name:
607 return cache_lookup_name(cache, &oid, key);
608 case lookup_path:
609 return cache_lookup_path(cache, &oid, key);
610 default:
611 return NULL;
614 out:
615 strbuf_release(&rev);
616 free(config);
617 return submodule;
620 static void submodule_cache_check_init(struct repository *repo)
622 if (repo->submodule_cache && repo->submodule_cache->initialized)
623 return;
625 if (!repo->submodule_cache)
626 repo->submodule_cache = submodule_cache_alloc();
628 submodule_cache_init(repo->submodule_cache);
632 * Note: This function is private for a reason, the '.gitmodules' file should
633 * not be used as a mechanism to retrieve arbitrary configuration stored in
634 * the repository.
636 * Runs the provided config function on the '.gitmodules' file found in the
637 * working directory.
639 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
641 if (repo->worktree) {
642 struct git_config_source config_source = {
643 0, .scope = CONFIG_SCOPE_SUBMODULE
645 const struct config_options opts = { 0 };
646 struct object_id oid;
647 char *file;
648 char *oidstr = NULL;
650 file = repo_worktree_path(repo, GITMODULES_FILE);
651 if (file_exists(file)) {
652 config_source.file = file;
653 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
654 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
655 config_source.repo = repo;
656 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
657 if (repo != the_repository)
658 add_submodule_odb_by_path(repo->objects->odb->path);
659 } else {
660 goto out;
663 config_with_options(fn, data, &config_source, &opts);
665 out:
666 free(oidstr);
667 free(file);
671 static int gitmodules_cb(const char *var, const char *value, void *data)
673 struct repository *repo = data;
674 struct parse_config_parameter parameter;
676 parameter.cache = repo->submodule_cache;
677 parameter.treeish_name = NULL;
678 parameter.gitmodules_oid = null_oid();
679 parameter.overwrite = 1;
681 return parse_config(var, value, &parameter);
684 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
686 submodule_cache_check_init(repo);
688 if (repo->submodule_cache->gitmodules_read && skip_if_read)
689 return;
691 if (repo_read_index(repo) < 0)
692 return;
694 if (!is_gitmodules_unmerged(repo->index))
695 config_from_gitmodules(gitmodules_cb, repo, repo);
697 repo->submodule_cache->gitmodules_read = 1;
700 void gitmodules_config_oid(const struct object_id *commit_oid)
702 struct strbuf rev = STRBUF_INIT;
703 struct object_id oid;
705 submodule_cache_check_init(the_repository);
707 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
708 git_config_from_blob_oid(gitmodules_cb, rev.buf,
709 the_repository, &oid, the_repository);
711 strbuf_release(&rev);
713 the_repository->submodule_cache->gitmodules_read = 1;
716 const struct submodule *submodule_from_name(struct repository *r,
717 const struct object_id *treeish_name,
718 const char *name)
720 repo_read_gitmodules(r, 1);
721 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
724 const struct submodule *submodule_from_path(struct repository *r,
725 const struct object_id *treeish_name,
726 const char *path)
728 repo_read_gitmodules(r, 1);
729 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
733 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
734 * and appends submodule entries to 'out'. The submodule_cache expects
735 * a root-level treeish_name and paths, so keep track of these values
736 * with 'root_tree' and 'prefix'.
738 static void traverse_tree_submodules(struct repository *r,
739 const struct object_id *root_tree,
740 char *prefix,
741 const struct object_id *treeish_name,
742 struct submodule_entry_list *out)
744 struct tree_desc tree;
745 struct submodule_tree_entry *st_entry;
746 struct name_entry *name_entry;
747 char *tree_path = NULL;
749 name_entry = xmalloc(sizeof(*name_entry));
751 fill_tree_descriptor(r, &tree, treeish_name);
752 while (tree_entry(&tree, name_entry)) {
753 if (prefix)
754 tree_path =
755 mkpathdup("%s/%s", prefix, name_entry->path);
756 else
757 tree_path = xstrdup(name_entry->path);
759 if (S_ISGITLINK(name_entry->mode) &&
760 is_tree_submodule_active(r, root_tree, tree_path)) {
761 ALLOC_GROW(out->entries, out->entry_nr + 1,
762 out->entry_alloc);
763 st_entry = &out->entries[out->entry_nr++];
765 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
766 *st_entry->name_entry = *name_entry;
767 st_entry->submodule =
768 submodule_from_path(r, root_tree, tree_path);
769 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
770 if (repo_submodule_init(st_entry->repo, r, tree_path,
771 root_tree))
772 FREE_AND_NULL(st_entry->repo);
774 } else if (S_ISDIR(name_entry->mode))
775 traverse_tree_submodules(r, root_tree, tree_path,
776 &name_entry->oid, out);
777 free(tree_path);
781 void submodules_of_tree(struct repository *r,
782 const struct object_id *treeish_name,
783 struct submodule_entry_list *out)
785 CALLOC_ARRAY(out->entries, 0);
786 out->entry_nr = 0;
787 out->entry_alloc = 0;
789 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
792 void submodule_free(struct repository *r)
794 if (r->submodule_cache)
795 submodule_cache_clear(r->submodule_cache);
798 static int config_print_callback(const char *var, const char *value, void *cb_data)
800 char *wanted_key = cb_data;
802 if (!strcmp(wanted_key, var))
803 printf("%s\n", value);
805 return 0;
808 int print_config_from_gitmodules(struct repository *repo, const char *key)
810 int ret;
811 char *store_key;
813 ret = git_config_parse_key(key, &store_key, NULL);
814 if (ret < 0)
815 return CONFIG_INVALID_KEY;
817 config_from_gitmodules(config_print_callback, repo, store_key);
819 free(store_key);
820 return 0;
823 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
825 int ret;
827 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
828 if (ret < 0)
829 /* Maybe the user already did that, don't error out here */
830 warning(_("Could not update .gitmodules entry %s"), key);
832 return ret;
835 struct fetch_config {
836 int *max_children;
837 int *recurse_submodules;
840 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
842 struct fetch_config *config = cb;
843 if (!strcmp(var, "submodule.fetchjobs")) {
844 if (config->max_children)
845 *(config->max_children) =
846 parse_submodule_fetchjobs(var, value);
847 return 0;
848 } else if (!strcmp(var, "fetch.recursesubmodules")) {
849 if (config->recurse_submodules)
850 *(config->recurse_submodules) =
851 parse_fetch_recurse_submodules_arg(var, value);
852 return 0;
855 return 0;
858 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
860 struct fetch_config config = {
861 .max_children = max_children,
862 .recurse_submodules = recurse_submodules
864 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
867 static int gitmodules_update_clone_config(const char *var, const char *value,
868 void *cb)
870 int *max_jobs = cb;
871 if (!strcmp(var, "submodule.fetchjobs"))
872 *max_jobs = parse_submodule_fetchjobs(var, value);
873 return 0;
876 void update_clone_config_from_gitmodules(int *max_jobs)
878 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);