refs/reftable: print errors on compaction failure
[alt-git.git] / submodule-config.c
blob54130f6a38572b613d4b7ee8ae1cf3bc6035055d
1 #include "git-compat-util.h"
2 #include "dir.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "path.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-ll.h"
14 #include "parse-options.h"
15 #include "thread-utils.h"
16 #include "tree-walk.h"
17 #include "url.h"
18 #include "urlmatch.h"
21 * submodule cache lookup structure
22 * There is one shared set of 'struct submodule' entries which can be
23 * looked up by their sha1 blob id of the .gitmodules file and either
24 * using path or name as key.
25 * for_path stores submodule entries with path as key
26 * for_name stores submodule entries with name as key
28 struct submodule_cache {
29 struct hashmap for_path;
30 struct hashmap for_name;
31 unsigned initialized:1;
32 unsigned gitmodules_read:1;
36 * thin wrapper struct needed to insert 'struct submodule' entries to
37 * the hashmap
39 struct submodule_entry {
40 struct hashmap_entry ent;
41 struct submodule *config;
44 enum lookup_type {
45 lookup_name,
46 lookup_path
49 static int config_path_cmp(const void *cmp_data UNUSED,
50 const struct hashmap_entry *eptr,
51 const struct hashmap_entry *entry_or_key,
52 const void *keydata UNUSED)
54 const struct submodule_entry *a, *b;
56 a = container_of(eptr, const struct submodule_entry, ent);
57 b = container_of(entry_or_key, const struct submodule_entry, ent);
59 return strcmp(a->config->path, b->config->path) ||
60 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
63 static int config_name_cmp(const void *cmp_data UNUSED,
64 const struct hashmap_entry *eptr,
65 const struct hashmap_entry *entry_or_key,
66 const void *keydata UNUSED)
68 const struct submodule_entry *a, *b;
70 a = container_of(eptr, const struct submodule_entry, ent);
71 b = container_of(entry_or_key, const struct submodule_entry, ent);
73 return strcmp(a->config->name, b->config->name) ||
74 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
77 static struct submodule_cache *submodule_cache_alloc(void)
79 return xcalloc(1, sizeof(struct submodule_cache));
82 static void submodule_cache_init(struct submodule_cache *cache)
84 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
85 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
86 cache->initialized = 1;
89 static void free_one_config(struct submodule_entry *entry)
91 free((void *) entry->config->path);
92 free((void *) entry->config->name);
93 free((void *) entry->config->branch);
94 free((void *) entry->config->update_strategy.command);
95 free(entry->config);
98 static void submodule_cache_clear(struct submodule_cache *cache)
100 struct hashmap_iter iter;
101 struct submodule_entry *entry;
103 if (!cache->initialized)
104 return;
107 * We iterate over the name hash here to be symmetric with the
108 * allocation of struct submodule entries. Each is allocated by
109 * their .gitmodules blob sha1 and submodule name.
111 hashmap_for_each_entry(&cache->for_name, &iter, entry,
112 ent /* member name */)
113 free_one_config(entry);
115 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
116 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
117 cache->initialized = 0;
118 cache->gitmodules_read = 0;
121 void submodule_cache_free(struct submodule_cache *cache)
123 submodule_cache_clear(cache);
124 free(cache);
127 static unsigned int hash_oid_string(const struct object_id *oid,
128 const char *string)
130 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
133 static void cache_put_path(struct submodule_cache *cache,
134 struct submodule *submodule)
136 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
137 submodule->path);
138 struct submodule_entry *e = xmalloc(sizeof(*e));
139 hashmap_entry_init(&e->ent, hash);
140 e->config = submodule;
141 hashmap_put(&cache->for_path, &e->ent);
144 static void cache_remove_path(struct submodule_cache *cache,
145 struct submodule *submodule)
147 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
148 submodule->path);
149 struct submodule_entry e;
150 struct submodule_entry *removed;
151 hashmap_entry_init(&e.ent, hash);
152 e.config = submodule;
153 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
154 free(removed);
157 static void cache_add(struct submodule_cache *cache,
158 struct submodule *submodule)
160 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
161 submodule->name);
162 struct submodule_entry *e = xmalloc(sizeof(*e));
163 hashmap_entry_init(&e->ent, hash);
164 e->config = submodule;
165 hashmap_add(&cache->for_name, &e->ent);
168 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
169 const struct object_id *gitmodules_oid, const char *path)
171 struct submodule_entry *entry;
172 unsigned int hash = hash_oid_string(gitmodules_oid, path);
173 struct submodule_entry key;
174 struct submodule key_config;
176 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
177 key_config.path = path;
179 hashmap_entry_init(&key.ent, hash);
180 key.config = &key_config;
182 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
183 if (entry)
184 return entry->config;
185 return NULL;
188 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
189 const struct object_id *gitmodules_oid, const char *name)
191 struct submodule_entry *entry;
192 unsigned int hash = hash_oid_string(gitmodules_oid, name);
193 struct submodule_entry key;
194 struct submodule key_config;
196 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
197 key_config.name = name;
199 hashmap_entry_init(&key.ent, hash);
200 key.config = &key_config;
202 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
203 if (entry)
204 return entry->config;
205 return NULL;
208 int check_submodule_name(const char *name)
210 /* Disallow empty names */
211 if (!*name)
212 return -1;
215 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
216 * separators rather than is_dir_sep(), because we want the name rules
217 * to be consistent across platforms.
219 goto in_component; /* always start inside component */
220 while (*name) {
221 char c = *name++;
222 if (is_xplatform_dir_sep(c)) {
223 in_component:
224 if (name[0] == '.' && name[1] == '.' &&
225 (!name[2] || is_xplatform_dir_sep(name[2])))
226 return -1;
230 return 0;
233 static int starts_with_dot_slash(const char *const path)
235 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
236 PATH_MATCH_XPLATFORM);
239 static int starts_with_dot_dot_slash(const char *const path)
241 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
242 PATH_MATCH_XPLATFORM);
245 static int submodule_url_is_relative(const char *url)
247 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
251 * Count directory components that a relative submodule URL should chop
252 * from the remote_url it is to be resolved against.
254 * In other words, this counts "../" components at the start of a
255 * submodule URL.
257 * Returns the number of directory components to chop and writes a
258 * pointer to the next character of url after all leading "./" and
259 * "../" components to out.
261 static int count_leading_dotdots(const char *url, const char **out)
263 int result = 0;
264 while (1) {
265 if (starts_with_dot_dot_slash(url)) {
266 result++;
267 url += strlen("../");
268 continue;
270 if (starts_with_dot_slash(url)) {
271 url += strlen("./");
272 continue;
274 *out = url;
275 return result;
279 * Check whether a transport is implemented by git-remote-curl.
281 * If it is, returns 1 and writes the URL that would be passed to
282 * git-remote-curl to the "out" parameter.
284 * Otherwise, returns 0 and leaves "out" untouched.
286 * Examples:
287 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
288 * https://example.com/repo.git -> 1, https://example.com/repo.git
289 * git://example.com/repo.git -> 0
291 * This is for use in checking for previously exploitable bugs that
292 * required a submodule URL to be passed to git-remote-curl.
294 static int url_to_curl_url(const char *url, const char **out)
297 * We don't need to check for case-aliases, "http.exe", and so
298 * on because in the default configuration, is_transport_allowed
299 * prevents URLs with those schemes from being cloned
300 * automatically.
302 if (skip_prefix(url, "http::", out) ||
303 skip_prefix(url, "https::", out) ||
304 skip_prefix(url, "ftp::", out) ||
305 skip_prefix(url, "ftps::", out))
306 return 1;
307 if (starts_with(url, "http://") ||
308 starts_with(url, "https://") ||
309 starts_with(url, "ftp://") ||
310 starts_with(url, "ftps://")) {
311 *out = url;
312 return 1;
314 return 0;
317 int check_submodule_url(const char *url)
319 const char *curl_url;
321 if (looks_like_command_line_option(url))
322 return -1;
324 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
325 char *decoded;
326 const char *next;
327 int has_nl;
330 * This could be appended to an http URL and url-decoded;
331 * check for malicious characters.
333 decoded = url_decode(url);
334 has_nl = !!strchr(decoded, '\n');
336 free(decoded);
337 if (has_nl)
338 return -1;
341 * URLs which escape their root via "../" can overwrite
342 * the host field and previous components, resolving to
343 * URLs like https::example.com/submodule.git and
344 * https:///example.com/submodule.git that were
345 * susceptible to CVE-2020-11008.
347 if (count_leading_dotdots(url, &next) > 0 &&
348 (*next == ':' || *next == '/'))
349 return -1;
352 else if (url_to_curl_url(url, &curl_url)) {
353 int ret = 0;
354 char *normalized = url_normalize(curl_url, NULL);
355 if (normalized) {
356 char *decoded = url_decode(normalized);
357 if (strchr(decoded, '\n'))
358 ret = -1;
359 free(normalized);
360 free(decoded);
361 } else {
362 ret = -1;
365 return ret;
368 return 0;
371 static int name_and_item_from_var(const char *var, struct strbuf *name,
372 struct strbuf *item)
374 const char *subsection, *key;
375 size_t subsection_len;
376 int parse;
377 parse = parse_config_key(var, "submodule", &subsection,
378 &subsection_len, &key);
379 if (parse < 0 || !subsection)
380 return 0;
382 strbuf_add(name, subsection, subsection_len);
383 if (check_submodule_name(name->buf) < 0) {
384 warning(_("ignoring suspicious submodule name: %s"), name->buf);
385 strbuf_release(name);
386 return 0;
389 strbuf_addstr(item, key);
391 return 1;
394 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
395 const struct object_id *gitmodules_oid, const char *name)
397 struct submodule *submodule;
398 struct strbuf name_buf = STRBUF_INIT;
400 submodule = cache_lookup_name(cache, gitmodules_oid, name);
401 if (submodule)
402 return submodule;
404 submodule = xmalloc(sizeof(*submodule));
406 strbuf_addstr(&name_buf, name);
407 submodule->name = strbuf_detach(&name_buf, NULL);
409 submodule->path = NULL;
410 submodule->url = NULL;
411 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
412 submodule->update_strategy.command = NULL;
413 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
414 submodule->ignore = NULL;
415 submodule->branch = NULL;
416 submodule->recommend_shallow = -1;
418 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
420 cache_add(cache, submodule);
422 return submodule;
425 static int parse_fetch_recurse(const char *opt, const char *arg,
426 int die_on_error)
428 switch (git_parse_maybe_bool(arg)) {
429 case 1:
430 return RECURSE_SUBMODULES_ON;
431 case 0:
432 return RECURSE_SUBMODULES_OFF;
433 default:
434 if (!strcmp(arg, "on-demand"))
435 return RECURSE_SUBMODULES_ON_DEMAND;
437 * Please update $__git_fetch_recurse_submodules in
438 * git-completion.bash when you add new options.
440 if (die_on_error)
441 die("bad %s argument: %s", opt, arg);
442 else
443 return RECURSE_SUBMODULES_ERROR;
447 int parse_submodule_fetchjobs(const char *var, const char *value,
448 const struct key_value_info *kvi)
450 int fetchjobs = git_config_int(var, value, kvi);
451 if (fetchjobs < 0)
452 die(_("negative values not allowed for submodule.fetchJobs"));
453 if (!fetchjobs)
454 fetchjobs = online_cpus();
455 return fetchjobs;
458 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
460 return parse_fetch_recurse(opt, arg, 1);
463 int option_fetch_parse_recurse_submodules(const struct option *opt,
464 const char *arg, int unset)
466 int *v;
468 if (!opt->value)
469 return -1;
471 v = opt->value;
473 if (unset) {
474 *v = RECURSE_SUBMODULES_OFF;
475 } else {
476 if (arg)
477 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
478 else
479 *v = RECURSE_SUBMODULES_ON;
481 return 0;
484 static int parse_update_recurse(const char *opt, const char *arg,
485 int die_on_error)
487 switch (git_parse_maybe_bool(arg)) {
488 case 1:
489 return RECURSE_SUBMODULES_ON;
490 case 0:
491 return RECURSE_SUBMODULES_OFF;
492 default:
493 if (die_on_error)
494 die("bad %s argument: %s", opt, arg);
495 return RECURSE_SUBMODULES_ERROR;
499 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
501 return parse_update_recurse(opt, arg, 1);
504 static int parse_push_recurse(const char *opt, const char *arg,
505 int die_on_error)
507 switch (git_parse_maybe_bool(arg)) {
508 case 1:
509 /* There's no simple "on" value when pushing */
510 if (die_on_error)
511 die("bad %s argument: %s", opt, arg);
512 else
513 return RECURSE_SUBMODULES_ERROR;
514 case 0:
515 return RECURSE_SUBMODULES_OFF;
516 default:
517 if (!strcmp(arg, "on-demand"))
518 return RECURSE_SUBMODULES_ON_DEMAND;
519 else if (!strcmp(arg, "check"))
520 return RECURSE_SUBMODULES_CHECK;
521 else if (!strcmp(arg, "only"))
522 return RECURSE_SUBMODULES_ONLY;
524 * Please update $__git_push_recurse_submodules in
525 * git-completion.bash when you add new modes.
527 else if (die_on_error)
528 die("bad %s argument: %s", opt, arg);
529 else
530 return RECURSE_SUBMODULES_ERROR;
534 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
536 return parse_push_recurse(opt, arg, 1);
539 static void warn_multiple_config(const struct object_id *treeish_name,
540 const char *name, const char *option)
542 const char *commit_string = "WORKTREE";
543 if (treeish_name)
544 commit_string = oid_to_hex(treeish_name);
545 warning("%s:.gitmodules, multiple configurations found for "
546 "'submodule.%s.%s'. Skipping second one!",
547 commit_string, name, option);
550 static void warn_command_line_option(const char *var, const char *value)
552 warning(_("ignoring '%s' which may be interpreted as"
553 " a command-line option: %s"), var, value);
556 struct parse_config_parameter {
557 struct submodule_cache *cache;
558 const struct object_id *treeish_name;
559 const struct object_id *gitmodules_oid;
560 int overwrite;
564 * Parse a config item from .gitmodules.
566 * This does not handle submodule-related configuration from the main
567 * config store (.git/config, etc). Callers are responsible for
568 * checking for overrides in the main config store when appropriate.
570 static int parse_config(const char *var, const char *value,
571 const struct config_context *ctx UNUSED, void *data)
573 struct parse_config_parameter *me = data;
574 struct submodule *submodule;
575 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
576 int ret = 0;
578 /* this also ensures that we only parse submodule entries */
579 if (!name_and_item_from_var(var, &name, &item))
580 return 0;
582 submodule = lookup_or_create_by_name(me->cache,
583 me->gitmodules_oid,
584 name.buf);
586 if (!strcmp(item.buf, "path")) {
587 if (!value)
588 ret = config_error_nonbool(var);
589 else if (looks_like_command_line_option(value))
590 warn_command_line_option(var, value);
591 else if (!me->overwrite && submodule->path)
592 warn_multiple_config(me->treeish_name, submodule->name,
593 "path");
594 else {
595 if (submodule->path)
596 cache_remove_path(me->cache, submodule);
597 free((void *) submodule->path);
598 submodule->path = xstrdup(value);
599 cache_put_path(me->cache, submodule);
601 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
602 /* when parsing worktree configurations we can die early */
603 int die_on_error = is_null_oid(me->gitmodules_oid);
604 if (!me->overwrite &&
605 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
606 warn_multiple_config(me->treeish_name, submodule->name,
607 "fetchrecursesubmodules");
608 else
609 submodule->fetch_recurse = parse_fetch_recurse(
610 var, value,
611 die_on_error);
612 } else if (!strcmp(item.buf, "ignore")) {
613 if (!value)
614 ret = config_error_nonbool(var);
615 else if (!me->overwrite && submodule->ignore)
616 warn_multiple_config(me->treeish_name, submodule->name,
617 "ignore");
618 else if (strcmp(value, "untracked") &&
619 strcmp(value, "dirty") &&
620 strcmp(value, "all") &&
621 strcmp(value, "none"))
622 warning("Invalid parameter '%s' for config option "
623 "'submodule.%s.ignore'", value, name.buf);
624 else {
625 free((void *) submodule->ignore);
626 submodule->ignore = xstrdup(value);
628 } else if (!strcmp(item.buf, "url")) {
629 if (!value) {
630 ret = config_error_nonbool(var);
631 } else if (looks_like_command_line_option(value)) {
632 warn_command_line_option(var, value);
633 } else if (!me->overwrite && submodule->url) {
634 warn_multiple_config(me->treeish_name, submodule->name,
635 "url");
636 } else {
637 free((void *) submodule->url);
638 submodule->url = xstrdup(value);
640 } else if (!strcmp(item.buf, "update")) {
641 if (!value)
642 ret = config_error_nonbool(var);
643 else if (!me->overwrite &&
644 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
645 warn_multiple_config(me->treeish_name, submodule->name,
646 "update");
647 else if (parse_submodule_update_strategy(value,
648 &submodule->update_strategy) < 0 ||
649 submodule->update_strategy.type == SM_UPDATE_COMMAND)
650 die(_("invalid value for '%s'"), var);
651 } else if (!strcmp(item.buf, "shallow")) {
652 if (!me->overwrite && submodule->recommend_shallow != -1)
653 warn_multiple_config(me->treeish_name, submodule->name,
654 "shallow");
655 else
656 submodule->recommend_shallow =
657 git_config_bool(var, value);
658 } else if (!strcmp(item.buf, "branch")) {
659 if (!value)
660 ret = config_error_nonbool(var);
661 else if (!me->overwrite && submodule->branch)
662 warn_multiple_config(me->treeish_name, submodule->name,
663 "branch");
664 else {
665 free((void *)submodule->branch);
666 submodule->branch = xstrdup(value);
670 strbuf_release(&name);
671 strbuf_release(&item);
673 return ret;
676 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
677 struct object_id *gitmodules_oid,
678 struct strbuf *rev)
680 int ret = 0;
682 if (is_null_oid(treeish_name)) {
683 oidclr(gitmodules_oid);
684 return 1;
687 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
688 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
689 ret = 1;
691 return ret;
694 /* This does a lookup of a submodule configuration by name or by path
695 * (key) with on-demand reading of the appropriate .gitmodules from
696 * revisions.
698 static const struct submodule *config_from(struct submodule_cache *cache,
699 const struct object_id *treeish_name, const char *key,
700 enum lookup_type lookup_type)
702 struct strbuf rev = STRBUF_INIT;
703 unsigned long config_size;
704 char *config = NULL;
705 struct object_id oid;
706 enum object_type type;
707 const struct submodule *submodule = NULL;
708 struct parse_config_parameter parameter;
711 * If any parameter except the cache is a NULL pointer just
712 * return the first submodule. Can be used to check whether
713 * there are any submodules parsed.
715 if (!treeish_name || !key) {
716 struct hashmap_iter iter;
717 struct submodule_entry *entry;
719 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
720 struct submodule_entry,
721 ent /* member name */);
722 if (!entry)
723 return NULL;
724 return entry->config;
727 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
728 goto out;
730 switch (lookup_type) {
731 case lookup_name:
732 submodule = cache_lookup_name(cache, &oid, key);
733 break;
734 case lookup_path:
735 submodule = cache_lookup_path(cache, &oid, key);
736 break;
738 if (submodule)
739 goto out;
741 config = repo_read_object_file(the_repository, &oid, &type,
742 &config_size);
743 if (!config || type != OBJ_BLOB)
744 goto out;
746 /* fill the submodule config into the cache */
747 parameter.cache = cache;
748 parameter.treeish_name = treeish_name;
749 parameter.gitmodules_oid = &oid;
750 parameter.overwrite = 0;
751 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
752 config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
753 strbuf_release(&rev);
754 free(config);
756 switch (lookup_type) {
757 case lookup_name:
758 return cache_lookup_name(cache, &oid, key);
759 case lookup_path:
760 return cache_lookup_path(cache, &oid, key);
761 default:
762 return NULL;
765 out:
766 strbuf_release(&rev);
767 free(config);
768 return submodule;
771 static void submodule_cache_check_init(struct repository *repo)
773 if (repo->submodule_cache && repo->submodule_cache->initialized)
774 return;
776 if (!repo->submodule_cache)
777 repo->submodule_cache = submodule_cache_alloc();
779 submodule_cache_init(repo->submodule_cache);
783 * Note: This function is private for a reason, the '.gitmodules' file should
784 * not be used as a mechanism to retrieve arbitrary configuration stored in
785 * the repository.
787 * Runs the provided config function on the '.gitmodules' file found in the
788 * working directory.
790 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
792 if (repo->worktree) {
793 struct git_config_source config_source = {
794 0, .scope = CONFIG_SCOPE_SUBMODULE
796 const struct config_options opts = { 0 };
797 struct object_id oid;
798 char *file;
799 char *oidstr = NULL;
801 file = repo_worktree_path(repo, GITMODULES_FILE);
802 if (file_exists(file)) {
803 config_source.file = file;
804 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
805 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
806 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
807 if (repo != the_repository)
808 add_submodule_odb_by_path(repo->objects->odb->path);
809 } else {
810 goto out;
813 config_with_options(fn, data, &config_source, repo, &opts);
815 out:
816 free(oidstr);
817 free(file);
821 static int gitmodules_cb(const char *var, const char *value,
822 const struct config_context *ctx, void *data)
824 struct repository *repo = data;
825 struct parse_config_parameter parameter;
827 parameter.cache = repo->submodule_cache;
828 parameter.treeish_name = NULL;
829 parameter.gitmodules_oid = null_oid();
830 parameter.overwrite = 1;
832 return parse_config(var, value, ctx, &parameter);
835 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
837 submodule_cache_check_init(repo);
839 if (repo->submodule_cache->gitmodules_read && skip_if_read)
840 return;
842 if (repo_read_index(repo) < 0)
843 return;
845 if (!is_gitmodules_unmerged(repo->index))
846 config_from_gitmodules(gitmodules_cb, repo, repo);
848 repo->submodule_cache->gitmodules_read = 1;
851 void gitmodules_config_oid(const struct object_id *commit_oid)
853 struct strbuf rev = STRBUF_INIT;
854 struct object_id oid;
856 submodule_cache_check_init(the_repository);
858 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
859 git_config_from_blob_oid(gitmodules_cb, rev.buf,
860 the_repository, &oid, the_repository,
861 CONFIG_SCOPE_UNKNOWN);
863 strbuf_release(&rev);
865 the_repository->submodule_cache->gitmodules_read = 1;
868 const struct submodule *submodule_from_name(struct repository *r,
869 const struct object_id *treeish_name,
870 const char *name)
872 repo_read_gitmodules(r, 1);
873 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
876 const struct submodule *submodule_from_path(struct repository *r,
877 const struct object_id *treeish_name,
878 const char *path)
880 repo_read_gitmodules(r, 1);
881 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
885 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
886 * and appends submodule entries to 'out'. The submodule_cache expects
887 * a root-level treeish_name and paths, so keep track of these values
888 * with 'root_tree' and 'prefix'.
890 static void traverse_tree_submodules(struct repository *r,
891 const struct object_id *root_tree,
892 char *prefix,
893 const struct object_id *treeish_name,
894 struct submodule_entry_list *out)
896 struct tree_desc tree;
897 struct submodule_tree_entry *st_entry;
898 struct name_entry *name_entry;
899 char *tree_path = NULL;
901 name_entry = xmalloc(sizeof(*name_entry));
903 fill_tree_descriptor(r, &tree, treeish_name);
904 while (tree_entry(&tree, name_entry)) {
905 if (prefix)
906 tree_path =
907 mkpathdup("%s/%s", prefix, name_entry->path);
908 else
909 tree_path = xstrdup(name_entry->path);
911 if (S_ISGITLINK(name_entry->mode) &&
912 is_tree_submodule_active(r, root_tree, tree_path)) {
913 ALLOC_GROW(out->entries, out->entry_nr + 1,
914 out->entry_alloc);
915 st_entry = &out->entries[out->entry_nr++];
917 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
918 *st_entry->name_entry = *name_entry;
919 st_entry->submodule =
920 submodule_from_path(r, root_tree, tree_path);
921 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
922 if (repo_submodule_init(st_entry->repo, r, tree_path,
923 root_tree))
924 FREE_AND_NULL(st_entry->repo);
926 } else if (S_ISDIR(name_entry->mode))
927 traverse_tree_submodules(r, root_tree, tree_path,
928 &name_entry->oid, out);
929 free(tree_path);
933 void submodules_of_tree(struct repository *r,
934 const struct object_id *treeish_name,
935 struct submodule_entry_list *out)
937 CALLOC_ARRAY(out->entries, 0);
938 out->entry_nr = 0;
939 out->entry_alloc = 0;
941 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
944 void submodule_free(struct repository *r)
946 if (r->submodule_cache)
947 submodule_cache_clear(r->submodule_cache);
950 static int config_print_callback(const char *var, const char *value,
951 const struct config_context *ctx UNUSED,
952 void *cb_data)
954 char *wanted_key = cb_data;
956 if (!strcmp(wanted_key, var))
957 printf("%s\n", value);
959 return 0;
962 int print_config_from_gitmodules(struct repository *repo, const char *key)
964 int ret;
965 char *store_key;
967 ret = git_config_parse_key(key, &store_key, NULL);
968 if (ret < 0)
969 return CONFIG_INVALID_KEY;
971 config_from_gitmodules(config_print_callback, repo, store_key);
973 free(store_key);
974 return 0;
977 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
979 int ret;
981 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
982 if (ret < 0)
983 /* Maybe the user already did that, don't error out here */
984 warning(_("Could not update .gitmodules entry %s"), key);
986 return ret;
989 struct fetch_config {
990 int *max_children;
991 int *recurse_submodules;
994 static int gitmodules_fetch_config(const char *var, const char *value,
995 const struct config_context *ctx,
996 void *cb)
998 struct fetch_config *config = cb;
999 if (!strcmp(var, "submodule.fetchjobs")) {
1000 if (config->max_children)
1001 *(config->max_children) =
1002 parse_submodule_fetchjobs(var, value, ctx->kvi);
1003 return 0;
1004 } else if (!strcmp(var, "fetch.recursesubmodules")) {
1005 if (config->recurse_submodules)
1006 *(config->recurse_submodules) =
1007 parse_fetch_recurse_submodules_arg(var, value);
1008 return 0;
1011 return 0;
1014 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
1016 struct fetch_config config = {
1017 .max_children = max_children,
1018 .recurse_submodules = recurse_submodules
1020 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
1023 static int gitmodules_update_clone_config(const char *var, const char *value,
1024 const struct config_context *ctx,
1025 void *cb)
1027 int *max_jobs = cb;
1028 if (!strcmp(var, "submodule.fetchjobs"))
1029 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
1030 return 0;
1033 void update_clone_config_from_gitmodules(int *max_jobs)
1035 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);