object-store-ll.h: split this header out of object-store.h
[alt-git.git] / submodule-config.c
blob515ff5bba41c272e90bf9fd1f4681b4c65a16dd7
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 "path.h"
8 #include "repository.h"
9 #include "config.h"
10 #include "submodule-config.h"
11 #include "submodule.h"
12 #include "strbuf.h"
13 #include "object-name.h"
14 #include "object-store-ll.h"
15 #include "parse-options.h"
16 #include "thread-utils.h"
17 #include "tree-walk.h"
20 * submodule cache lookup structure
21 * There is one shared set of 'struct submodule' entries which can be
22 * looked up by their sha1 blob id of the .gitmodules file and either
23 * using path or name as key.
24 * for_path stores submodule entries with path as key
25 * for_name stores submodule entries with name as key
27 struct submodule_cache {
28 struct hashmap for_path;
29 struct hashmap for_name;
30 unsigned initialized:1;
31 unsigned gitmodules_read:1;
35 * thin wrapper struct needed to insert 'struct submodule' entries to
36 * the hashmap
38 struct submodule_entry {
39 struct hashmap_entry ent;
40 struct submodule *config;
43 enum lookup_type {
44 lookup_name,
45 lookup_path
48 static int config_path_cmp(const void *cmp_data UNUSED,
49 const struct hashmap_entry *eptr,
50 const struct hashmap_entry *entry_or_key,
51 const void *keydata UNUSED)
53 const struct submodule_entry *a, *b;
55 a = container_of(eptr, const struct submodule_entry, ent);
56 b = container_of(entry_or_key, const struct submodule_entry, ent);
58 return strcmp(a->config->path, b->config->path) ||
59 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
62 static int config_name_cmp(const void *cmp_data UNUSED,
63 const struct hashmap_entry *eptr,
64 const struct hashmap_entry *entry_or_key,
65 const void *keydata UNUSED)
67 const struct submodule_entry *a, *b;
69 a = container_of(eptr, const struct submodule_entry, ent);
70 b = container_of(entry_or_key, const struct submodule_entry, ent);
72 return strcmp(a->config->name, b->config->name) ||
73 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
76 static struct submodule_cache *submodule_cache_alloc(void)
78 return xcalloc(1, sizeof(struct submodule_cache));
81 static void submodule_cache_init(struct submodule_cache *cache)
83 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
84 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
85 cache->initialized = 1;
88 static void free_one_config(struct submodule_entry *entry)
90 free((void *) entry->config->path);
91 free((void *) entry->config->name);
92 free((void *) entry->config->branch);
93 free((void *) entry->config->update_strategy.command);
94 free(entry->config);
97 static void submodule_cache_clear(struct submodule_cache *cache)
99 struct hashmap_iter iter;
100 struct submodule_entry *entry;
102 if (!cache->initialized)
103 return;
106 * We iterate over the name hash here to be symmetric with the
107 * allocation of struct submodule entries. Each is allocated by
108 * their .gitmodules blob sha1 and submodule name.
110 hashmap_for_each_entry(&cache->for_name, &iter, entry,
111 ent /* member name */)
112 free_one_config(entry);
114 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
115 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
116 cache->initialized = 0;
117 cache->gitmodules_read = 0;
120 void submodule_cache_free(struct submodule_cache *cache)
122 submodule_cache_clear(cache);
123 free(cache);
126 static unsigned int hash_oid_string(const struct object_id *oid,
127 const char *string)
129 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
132 static void cache_put_path(struct submodule_cache *cache,
133 struct submodule *submodule)
135 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
136 submodule->path);
137 struct submodule_entry *e = xmalloc(sizeof(*e));
138 hashmap_entry_init(&e->ent, hash);
139 e->config = submodule;
140 hashmap_put(&cache->for_path, &e->ent);
143 static void cache_remove_path(struct submodule_cache *cache,
144 struct submodule *submodule)
146 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
147 submodule->path);
148 struct submodule_entry e;
149 struct submodule_entry *removed;
150 hashmap_entry_init(&e.ent, hash);
151 e.config = submodule;
152 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
153 free(removed);
156 static void cache_add(struct submodule_cache *cache,
157 struct submodule *submodule)
159 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
160 submodule->name);
161 struct submodule_entry *e = xmalloc(sizeof(*e));
162 hashmap_entry_init(&e->ent, hash);
163 e->config = submodule;
164 hashmap_add(&cache->for_name, &e->ent);
167 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
168 const struct object_id *gitmodules_oid, const char *path)
170 struct submodule_entry *entry;
171 unsigned int hash = hash_oid_string(gitmodules_oid, path);
172 struct submodule_entry key;
173 struct submodule key_config;
175 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
176 key_config.path = path;
178 hashmap_entry_init(&key.ent, hash);
179 key.config = &key_config;
181 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
182 if (entry)
183 return entry->config;
184 return NULL;
187 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
188 const struct object_id *gitmodules_oid, const char *name)
190 struct submodule_entry *entry;
191 unsigned int hash = hash_oid_string(gitmodules_oid, name);
192 struct submodule_entry key;
193 struct submodule key_config;
195 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
196 key_config.name = name;
198 hashmap_entry_init(&key.ent, hash);
199 key.config = &key_config;
201 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
202 if (entry)
203 return entry->config;
204 return NULL;
207 int check_submodule_name(const char *name)
209 /* Disallow empty names */
210 if (!*name)
211 return -1;
214 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
215 * separators rather than is_dir_sep(), because we want the name rules
216 * to be consistent across platforms.
218 goto in_component; /* always start inside component */
219 while (*name) {
220 char c = *name++;
221 if (is_xplatform_dir_sep(c)) {
222 in_component:
223 if (name[0] == '.' && name[1] == '.' &&
224 (!name[2] || is_xplatform_dir_sep(name[2])))
225 return -1;
229 return 0;
232 static int name_and_item_from_var(const char *var, struct strbuf *name,
233 struct strbuf *item)
235 const char *subsection, *key;
236 size_t subsection_len;
237 int parse;
238 parse = parse_config_key(var, "submodule", &subsection,
239 &subsection_len, &key);
240 if (parse < 0 || !subsection)
241 return 0;
243 strbuf_add(name, subsection, subsection_len);
244 if (check_submodule_name(name->buf) < 0) {
245 warning(_("ignoring suspicious submodule name: %s"), name->buf);
246 strbuf_release(name);
247 return 0;
250 strbuf_addstr(item, key);
252 return 1;
255 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
256 const struct object_id *gitmodules_oid, const char *name)
258 struct submodule *submodule;
259 struct strbuf name_buf = STRBUF_INIT;
261 submodule = cache_lookup_name(cache, gitmodules_oid, name);
262 if (submodule)
263 return submodule;
265 submodule = xmalloc(sizeof(*submodule));
267 strbuf_addstr(&name_buf, name);
268 submodule->name = strbuf_detach(&name_buf, NULL);
270 submodule->path = NULL;
271 submodule->url = NULL;
272 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
273 submodule->update_strategy.command = NULL;
274 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
275 submodule->ignore = NULL;
276 submodule->branch = NULL;
277 submodule->recommend_shallow = -1;
279 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
281 cache_add(cache, submodule);
283 return submodule;
286 static int parse_fetch_recurse(const char *opt, const char *arg,
287 int die_on_error)
289 switch (git_parse_maybe_bool(arg)) {
290 case 1:
291 return RECURSE_SUBMODULES_ON;
292 case 0:
293 return RECURSE_SUBMODULES_OFF;
294 default:
295 if (!strcmp(arg, "on-demand"))
296 return RECURSE_SUBMODULES_ON_DEMAND;
298 * Please update $__git_fetch_recurse_submodules in
299 * git-completion.bash when you add new options.
301 if (die_on_error)
302 die("bad %s argument: %s", opt, arg);
303 else
304 return RECURSE_SUBMODULES_ERROR;
308 int parse_submodule_fetchjobs(const char *var, const char *value)
310 int fetchjobs = git_config_int(var, value);
311 if (fetchjobs < 0)
312 die(_("negative values not allowed for submodule.fetchJobs"));
313 if (!fetchjobs)
314 fetchjobs = online_cpus();
315 return fetchjobs;
318 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
320 return parse_fetch_recurse(opt, arg, 1);
323 int option_fetch_parse_recurse_submodules(const struct option *opt,
324 const char *arg, int unset)
326 int *v;
328 if (!opt->value)
329 return -1;
331 v = opt->value;
333 if (unset) {
334 *v = RECURSE_SUBMODULES_OFF;
335 } else {
336 if (arg)
337 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
338 else
339 *v = RECURSE_SUBMODULES_ON;
341 return 0;
344 static int parse_update_recurse(const char *opt, const char *arg,
345 int die_on_error)
347 switch (git_parse_maybe_bool(arg)) {
348 case 1:
349 return RECURSE_SUBMODULES_ON;
350 case 0:
351 return RECURSE_SUBMODULES_OFF;
352 default:
353 if (die_on_error)
354 die("bad %s argument: %s", opt, arg);
355 return RECURSE_SUBMODULES_ERROR;
359 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
361 return parse_update_recurse(opt, arg, 1);
364 static int parse_push_recurse(const char *opt, const char *arg,
365 int die_on_error)
367 switch (git_parse_maybe_bool(arg)) {
368 case 1:
369 /* There's no simple "on" value when pushing */
370 if (die_on_error)
371 die("bad %s argument: %s", opt, arg);
372 else
373 return RECURSE_SUBMODULES_ERROR;
374 case 0:
375 return RECURSE_SUBMODULES_OFF;
376 default:
377 if (!strcmp(arg, "on-demand"))
378 return RECURSE_SUBMODULES_ON_DEMAND;
379 else if (!strcmp(arg, "check"))
380 return RECURSE_SUBMODULES_CHECK;
381 else if (!strcmp(arg, "only"))
382 return RECURSE_SUBMODULES_ONLY;
384 * Please update $__git_push_recurse_submodules in
385 * git-completion.bash when you add new modes.
387 else if (die_on_error)
388 die("bad %s argument: %s", opt, arg);
389 else
390 return RECURSE_SUBMODULES_ERROR;
394 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
396 return parse_push_recurse(opt, arg, 1);
399 static void warn_multiple_config(const struct object_id *treeish_name,
400 const char *name, const char *option)
402 const char *commit_string = "WORKTREE";
403 if (treeish_name)
404 commit_string = oid_to_hex(treeish_name);
405 warning("%s:.gitmodules, multiple configurations found for "
406 "'submodule.%s.%s'. Skipping second one!",
407 commit_string, name, option);
410 static void warn_command_line_option(const char *var, const char *value)
412 warning(_("ignoring '%s' which may be interpreted as"
413 " a command-line option: %s"), var, value);
416 struct parse_config_parameter {
417 struct submodule_cache *cache;
418 const struct object_id *treeish_name;
419 const struct object_id *gitmodules_oid;
420 int overwrite;
424 * Parse a config item from .gitmodules.
426 * This does not handle submodule-related configuration from the main
427 * config store (.git/config, etc). Callers are responsible for
428 * checking for overrides in the main config store when appropriate.
430 static int parse_config(const char *var, const char *value, void *data)
432 struct parse_config_parameter *me = data;
433 struct submodule *submodule;
434 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
435 int ret = 0;
437 /* this also ensures that we only parse submodule entries */
438 if (!name_and_item_from_var(var, &name, &item))
439 return 0;
441 submodule = lookup_or_create_by_name(me->cache,
442 me->gitmodules_oid,
443 name.buf);
445 if (!strcmp(item.buf, "path")) {
446 if (!value)
447 ret = config_error_nonbool(var);
448 else if (looks_like_command_line_option(value))
449 warn_command_line_option(var, value);
450 else if (!me->overwrite && submodule->path)
451 warn_multiple_config(me->treeish_name, submodule->name,
452 "path");
453 else {
454 if (submodule->path)
455 cache_remove_path(me->cache, submodule);
456 free((void *) submodule->path);
457 submodule->path = xstrdup(value);
458 cache_put_path(me->cache, submodule);
460 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
461 /* when parsing worktree configurations we can die early */
462 int die_on_error = is_null_oid(me->gitmodules_oid);
463 if (!me->overwrite &&
464 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
465 warn_multiple_config(me->treeish_name, submodule->name,
466 "fetchrecursesubmodules");
467 else
468 submodule->fetch_recurse = parse_fetch_recurse(
469 var, value,
470 die_on_error);
471 } else if (!strcmp(item.buf, "ignore")) {
472 if (!value)
473 ret = config_error_nonbool(var);
474 else if (!me->overwrite && submodule->ignore)
475 warn_multiple_config(me->treeish_name, submodule->name,
476 "ignore");
477 else if (strcmp(value, "untracked") &&
478 strcmp(value, "dirty") &&
479 strcmp(value, "all") &&
480 strcmp(value, "none"))
481 warning("Invalid parameter '%s' for config option "
482 "'submodule.%s.ignore'", value, name.buf);
483 else {
484 free((void *) submodule->ignore);
485 submodule->ignore = xstrdup(value);
487 } else if (!strcmp(item.buf, "url")) {
488 if (!value) {
489 ret = config_error_nonbool(var);
490 } else if (looks_like_command_line_option(value)) {
491 warn_command_line_option(var, value);
492 } else if (!me->overwrite && submodule->url) {
493 warn_multiple_config(me->treeish_name, submodule->name,
494 "url");
495 } else {
496 free((void *) submodule->url);
497 submodule->url = xstrdup(value);
499 } else if (!strcmp(item.buf, "update")) {
500 if (!value)
501 ret = config_error_nonbool(var);
502 else if (!me->overwrite &&
503 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
504 warn_multiple_config(me->treeish_name, submodule->name,
505 "update");
506 else if (parse_submodule_update_strategy(value,
507 &submodule->update_strategy) < 0 ||
508 submodule->update_strategy.type == SM_UPDATE_COMMAND)
509 die(_("invalid value for '%s'"), var);
510 } else if (!strcmp(item.buf, "shallow")) {
511 if (!me->overwrite && submodule->recommend_shallow != -1)
512 warn_multiple_config(me->treeish_name, submodule->name,
513 "shallow");
514 else
515 submodule->recommend_shallow =
516 git_config_bool(var, value);
517 } else if (!strcmp(item.buf, "branch")) {
518 if (!me->overwrite && submodule->branch)
519 warn_multiple_config(me->treeish_name, submodule->name,
520 "branch");
521 else {
522 free((void *)submodule->branch);
523 submodule->branch = xstrdup(value);
527 strbuf_release(&name);
528 strbuf_release(&item);
530 return ret;
533 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
534 struct object_id *gitmodules_oid,
535 struct strbuf *rev)
537 int ret = 0;
539 if (is_null_oid(treeish_name)) {
540 oidclr(gitmodules_oid);
541 return 1;
544 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
545 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
546 ret = 1;
548 return ret;
551 /* This does a lookup of a submodule configuration by name or by path
552 * (key) with on-demand reading of the appropriate .gitmodules from
553 * revisions.
555 static const struct submodule *config_from(struct submodule_cache *cache,
556 const struct object_id *treeish_name, const char *key,
557 enum lookup_type lookup_type)
559 struct strbuf rev = STRBUF_INIT;
560 unsigned long config_size;
561 char *config = NULL;
562 struct object_id oid;
563 enum object_type type;
564 const struct submodule *submodule = NULL;
565 struct parse_config_parameter parameter;
568 * If any parameter except the cache is a NULL pointer just
569 * return the first submodule. Can be used to check whether
570 * there are any submodules parsed.
572 if (!treeish_name || !key) {
573 struct hashmap_iter iter;
574 struct submodule_entry *entry;
576 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
577 struct submodule_entry,
578 ent /* member name */);
579 if (!entry)
580 return NULL;
581 return entry->config;
584 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
585 goto out;
587 switch (lookup_type) {
588 case lookup_name:
589 submodule = cache_lookup_name(cache, &oid, key);
590 break;
591 case lookup_path:
592 submodule = cache_lookup_path(cache, &oid, key);
593 break;
595 if (submodule)
596 goto out;
598 config = repo_read_object_file(the_repository, &oid, &type,
599 &config_size);
600 if (!config || type != OBJ_BLOB)
601 goto out;
603 /* fill the submodule config into the cache */
604 parameter.cache = cache;
605 parameter.treeish_name = treeish_name;
606 parameter.gitmodules_oid = &oid;
607 parameter.overwrite = 0;
608 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
609 config, config_size, &parameter, NULL);
610 strbuf_release(&rev);
611 free(config);
613 switch (lookup_type) {
614 case lookup_name:
615 return cache_lookup_name(cache, &oid, key);
616 case lookup_path:
617 return cache_lookup_path(cache, &oid, key);
618 default:
619 return NULL;
622 out:
623 strbuf_release(&rev);
624 free(config);
625 return submodule;
628 static void submodule_cache_check_init(struct repository *repo)
630 if (repo->submodule_cache && repo->submodule_cache->initialized)
631 return;
633 if (!repo->submodule_cache)
634 repo->submodule_cache = submodule_cache_alloc();
636 submodule_cache_init(repo->submodule_cache);
640 * Note: This function is private for a reason, the '.gitmodules' file should
641 * not be used as a mechanism to retrieve arbitrary configuration stored in
642 * the repository.
644 * Runs the provided config function on the '.gitmodules' file found in the
645 * working directory.
647 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
649 if (repo->worktree) {
650 struct git_config_source config_source = {
651 0, .scope = CONFIG_SCOPE_SUBMODULE
653 const struct config_options opts = { 0 };
654 struct object_id oid;
655 char *file;
656 char *oidstr = NULL;
658 file = repo_worktree_path(repo, GITMODULES_FILE);
659 if (file_exists(file)) {
660 config_source.file = file;
661 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
662 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
663 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
664 if (repo != the_repository)
665 add_submodule_odb_by_path(repo->objects->odb->path);
666 } else {
667 goto out;
670 config_with_options(fn, data, &config_source, repo, &opts);
672 out:
673 free(oidstr);
674 free(file);
678 static int gitmodules_cb(const char *var, const char *value, void *data)
680 struct repository *repo = data;
681 struct parse_config_parameter parameter;
683 parameter.cache = repo->submodule_cache;
684 parameter.treeish_name = NULL;
685 parameter.gitmodules_oid = null_oid();
686 parameter.overwrite = 1;
688 return parse_config(var, value, &parameter);
691 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
693 submodule_cache_check_init(repo);
695 if (repo->submodule_cache->gitmodules_read && skip_if_read)
696 return;
698 if (repo_read_index(repo) < 0)
699 return;
701 if (!is_gitmodules_unmerged(repo->index))
702 config_from_gitmodules(gitmodules_cb, repo, repo);
704 repo->submodule_cache->gitmodules_read = 1;
707 void gitmodules_config_oid(const struct object_id *commit_oid)
709 struct strbuf rev = STRBUF_INIT;
710 struct object_id oid;
712 submodule_cache_check_init(the_repository);
714 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
715 git_config_from_blob_oid(gitmodules_cb, rev.buf,
716 the_repository, &oid, the_repository);
718 strbuf_release(&rev);
720 the_repository->submodule_cache->gitmodules_read = 1;
723 const struct submodule *submodule_from_name(struct repository *r,
724 const struct object_id *treeish_name,
725 const char *name)
727 repo_read_gitmodules(r, 1);
728 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
731 const struct submodule *submodule_from_path(struct repository *r,
732 const struct object_id *treeish_name,
733 const char *path)
735 repo_read_gitmodules(r, 1);
736 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
740 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
741 * and appends submodule entries to 'out'. The submodule_cache expects
742 * a root-level treeish_name and paths, so keep track of these values
743 * with 'root_tree' and 'prefix'.
745 static void traverse_tree_submodules(struct repository *r,
746 const struct object_id *root_tree,
747 char *prefix,
748 const struct object_id *treeish_name,
749 struct submodule_entry_list *out)
751 struct tree_desc tree;
752 struct submodule_tree_entry *st_entry;
753 struct name_entry *name_entry;
754 char *tree_path = NULL;
756 name_entry = xmalloc(sizeof(*name_entry));
758 fill_tree_descriptor(r, &tree, treeish_name);
759 while (tree_entry(&tree, name_entry)) {
760 if (prefix)
761 tree_path =
762 mkpathdup("%s/%s", prefix, name_entry->path);
763 else
764 tree_path = xstrdup(name_entry->path);
766 if (S_ISGITLINK(name_entry->mode) &&
767 is_tree_submodule_active(r, root_tree, tree_path)) {
768 ALLOC_GROW(out->entries, out->entry_nr + 1,
769 out->entry_alloc);
770 st_entry = &out->entries[out->entry_nr++];
772 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
773 *st_entry->name_entry = *name_entry;
774 st_entry->submodule =
775 submodule_from_path(r, root_tree, tree_path);
776 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
777 if (repo_submodule_init(st_entry->repo, r, tree_path,
778 root_tree))
779 FREE_AND_NULL(st_entry->repo);
781 } else if (S_ISDIR(name_entry->mode))
782 traverse_tree_submodules(r, root_tree, tree_path,
783 &name_entry->oid, out);
784 free(tree_path);
788 void submodules_of_tree(struct repository *r,
789 const struct object_id *treeish_name,
790 struct submodule_entry_list *out)
792 CALLOC_ARRAY(out->entries, 0);
793 out->entry_nr = 0;
794 out->entry_alloc = 0;
796 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
799 void submodule_free(struct repository *r)
801 if (r->submodule_cache)
802 submodule_cache_clear(r->submodule_cache);
805 static int config_print_callback(const char *var, const char *value, void *cb_data)
807 char *wanted_key = cb_data;
809 if (!strcmp(wanted_key, var))
810 printf("%s\n", value);
812 return 0;
815 int print_config_from_gitmodules(struct repository *repo, const char *key)
817 int ret;
818 char *store_key;
820 ret = git_config_parse_key(key, &store_key, NULL);
821 if (ret < 0)
822 return CONFIG_INVALID_KEY;
824 config_from_gitmodules(config_print_callback, repo, store_key);
826 free(store_key);
827 return 0;
830 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
832 int ret;
834 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
835 if (ret < 0)
836 /* Maybe the user already did that, don't error out here */
837 warning(_("Could not update .gitmodules entry %s"), key);
839 return ret;
842 struct fetch_config {
843 int *max_children;
844 int *recurse_submodules;
847 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
849 struct fetch_config *config = cb;
850 if (!strcmp(var, "submodule.fetchjobs")) {
851 if (config->max_children)
852 *(config->max_children) =
853 parse_submodule_fetchjobs(var, value);
854 return 0;
855 } else if (!strcmp(var, "fetch.recursesubmodules")) {
856 if (config->recurse_submodules)
857 *(config->recurse_submodules) =
858 parse_fetch_recurse_submodules_arg(var, value);
859 return 0;
862 return 0;
865 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
867 struct fetch_config config = {
868 .max_children = max_children,
869 .recurse_submodules = recurse_submodules
871 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
874 static int gitmodules_update_clone_config(const char *var, const char *value,
875 void *cb)
877 int *max_jobs = cb;
878 if (!strcmp(var, "submodule.fetchjobs"))
879 *max_jobs = parse_submodule_fetchjobs(var, value);
880 return 0;
883 void update_clone_config_from_gitmodules(int *max_jobs)
885 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);