parse-options: cast long name for OPTION_ALIAS
[git.git] / submodule-config.c
blobec45ea67b95618d4ec626e80af145a5282e0ac0d
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->url);
95 free((void *) entry->config->ignore);
96 free((void *) entry->config->update_strategy.command);
97 free(entry->config);
100 static void submodule_cache_clear(struct submodule_cache *cache)
102 struct hashmap_iter iter;
103 struct submodule_entry *entry;
105 if (!cache->initialized)
106 return;
109 * We iterate over the name hash here to be symmetric with the
110 * allocation of struct submodule entries. Each is allocated by
111 * their .gitmodules blob sha1 and submodule name.
113 hashmap_for_each_entry(&cache->for_name, &iter, entry,
114 ent /* member name */)
115 free_one_config(entry);
117 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
118 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
119 cache->initialized = 0;
120 cache->gitmodules_read = 0;
123 void submodule_cache_free(struct submodule_cache *cache)
125 submodule_cache_clear(cache);
126 free(cache);
129 static unsigned int hash_oid_string(const struct object_id *oid,
130 const char *string)
132 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
135 static void cache_put_path(struct submodule_cache *cache,
136 struct submodule *submodule)
138 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
139 submodule->path);
140 struct submodule_entry *e = xmalloc(sizeof(*e));
141 hashmap_entry_init(&e->ent, hash);
142 e->config = submodule;
143 hashmap_put(&cache->for_path, &e->ent);
146 static void cache_remove_path(struct submodule_cache *cache,
147 struct submodule *submodule)
149 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
150 submodule->path);
151 struct submodule_entry e;
152 struct submodule_entry *removed;
153 hashmap_entry_init(&e.ent, hash);
154 e.config = submodule;
155 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
156 free(removed);
159 static void cache_add(struct submodule_cache *cache,
160 struct submodule *submodule)
162 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
163 submodule->name);
164 struct submodule_entry *e = xmalloc(sizeof(*e));
165 hashmap_entry_init(&e->ent, hash);
166 e->config = submodule;
167 hashmap_add(&cache->for_name, &e->ent);
170 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
171 const struct object_id *gitmodules_oid, const char *path)
173 struct submodule_entry *entry;
174 unsigned int hash = hash_oid_string(gitmodules_oid, path);
175 struct submodule_entry key;
176 struct submodule key_config;
178 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
179 key_config.path = path;
181 hashmap_entry_init(&key.ent, hash);
182 key.config = &key_config;
184 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
185 if (entry)
186 return entry->config;
187 return NULL;
190 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
191 const struct object_id *gitmodules_oid, const char *name)
193 struct submodule_entry *entry;
194 unsigned int hash = hash_oid_string(gitmodules_oid, name);
195 struct submodule_entry key;
196 struct submodule key_config;
198 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
199 key_config.name = name;
201 hashmap_entry_init(&key.ent, hash);
202 key.config = &key_config;
204 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
205 if (entry)
206 return entry->config;
207 return NULL;
210 int check_submodule_name(const char *name)
212 /* Disallow empty names */
213 if (!*name)
214 return -1;
217 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
218 * separators rather than is_dir_sep(), because we want the name rules
219 * to be consistent across platforms.
221 goto in_component; /* always start inside component */
222 while (*name) {
223 char c = *name++;
224 if (is_xplatform_dir_sep(c)) {
225 in_component:
226 if (name[0] == '.' && name[1] == '.' &&
227 (!name[2] || is_xplatform_dir_sep(name[2])))
228 return -1;
232 return 0;
235 static int starts_with_dot_slash(const char *const path)
237 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
238 PATH_MATCH_XPLATFORM);
241 static int starts_with_dot_dot_slash(const char *const path)
243 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
244 PATH_MATCH_XPLATFORM);
247 static int submodule_url_is_relative(const char *url)
249 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
253 * Count directory components that a relative submodule URL should chop
254 * from the remote_url it is to be resolved against.
256 * In other words, this counts "../" components at the start of a
257 * submodule URL.
259 * Returns the number of directory components to chop and writes a
260 * pointer to the next character of url after all leading "./" and
261 * "../" components to out.
263 static int count_leading_dotdots(const char *url, const char **out)
265 int result = 0;
266 while (1) {
267 if (starts_with_dot_dot_slash(url)) {
268 result++;
269 url += strlen("../");
270 continue;
272 if (starts_with_dot_slash(url)) {
273 url += strlen("./");
274 continue;
276 *out = url;
277 return result;
281 * Check whether a transport is implemented by git-remote-curl.
283 * If it is, returns 1 and writes the URL that would be passed to
284 * git-remote-curl to the "out" parameter.
286 * Otherwise, returns 0 and leaves "out" untouched.
288 * Examples:
289 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
290 * https://example.com/repo.git -> 1, https://example.com/repo.git
291 * git://example.com/repo.git -> 0
293 * This is for use in checking for previously exploitable bugs that
294 * required a submodule URL to be passed to git-remote-curl.
296 static int url_to_curl_url(const char *url, const char **out)
299 * We don't need to check for case-aliases, "http.exe", and so
300 * on because in the default configuration, is_transport_allowed
301 * prevents URLs with those schemes from being cloned
302 * automatically.
304 if (skip_prefix(url, "http::", out) ||
305 skip_prefix(url, "https::", out) ||
306 skip_prefix(url, "ftp::", out) ||
307 skip_prefix(url, "ftps::", out))
308 return 1;
309 if (starts_with(url, "http://") ||
310 starts_with(url, "https://") ||
311 starts_with(url, "ftp://") ||
312 starts_with(url, "ftps://")) {
313 *out = url;
314 return 1;
316 return 0;
319 int check_submodule_url(const char *url)
321 const char *curl_url;
323 if (looks_like_command_line_option(url))
324 return -1;
326 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
327 char *decoded;
328 const char *next;
329 int has_nl;
332 * This could be appended to an http URL and url-decoded;
333 * check for malicious characters.
335 decoded = url_decode(url);
336 has_nl = !!strchr(decoded, '\n');
338 free(decoded);
339 if (has_nl)
340 return -1;
343 * URLs which escape their root via "../" can overwrite
344 * the host field and previous components, resolving to
345 * URLs like https::example.com/submodule.git and
346 * https:///example.com/submodule.git that were
347 * susceptible to CVE-2020-11008.
349 if (count_leading_dotdots(url, &next) > 0 &&
350 (*next == ':' || *next == '/'))
351 return -1;
354 else if (url_to_curl_url(url, &curl_url)) {
355 int ret = 0;
356 char *normalized = url_normalize(curl_url, NULL);
357 if (normalized) {
358 char *decoded = url_decode(normalized);
359 if (strchr(decoded, '\n'))
360 ret = -1;
361 free(normalized);
362 free(decoded);
363 } else {
364 ret = -1;
367 return ret;
370 return 0;
373 static int name_and_item_from_var(const char *var, struct strbuf *name,
374 struct strbuf *item)
376 const char *subsection, *key;
377 size_t subsection_len;
378 int parse;
379 parse = parse_config_key(var, "submodule", &subsection,
380 &subsection_len, &key);
381 if (parse < 0 || !subsection)
382 return 0;
384 strbuf_add(name, subsection, subsection_len);
385 if (check_submodule_name(name->buf) < 0) {
386 warning(_("ignoring suspicious submodule name: %s"), name->buf);
387 strbuf_release(name);
388 return 0;
391 strbuf_addstr(item, key);
393 return 1;
396 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
397 const struct object_id *gitmodules_oid, const char *name)
399 struct submodule *submodule;
400 struct strbuf name_buf = STRBUF_INIT;
402 submodule = cache_lookup_name(cache, gitmodules_oid, name);
403 if (submodule)
404 return submodule;
406 submodule = xmalloc(sizeof(*submodule));
408 strbuf_addstr(&name_buf, name);
409 submodule->name = strbuf_detach(&name_buf, NULL);
411 submodule->path = NULL;
412 submodule->url = NULL;
413 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
414 submodule->update_strategy.command = NULL;
415 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
416 submodule->ignore = NULL;
417 submodule->branch = NULL;
418 submodule->recommend_shallow = -1;
420 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
422 cache_add(cache, submodule);
424 return submodule;
427 static int parse_fetch_recurse(const char *opt, const char *arg,
428 int die_on_error)
430 switch (git_parse_maybe_bool(arg)) {
431 case 1:
432 return RECURSE_SUBMODULES_ON;
433 case 0:
434 return RECURSE_SUBMODULES_OFF;
435 default:
436 if (!strcmp(arg, "on-demand"))
437 return RECURSE_SUBMODULES_ON_DEMAND;
439 * Please update $__git_fetch_recurse_submodules in
440 * git-completion.bash when you add new options.
442 if (die_on_error)
443 die("bad %s argument: %s", opt, arg);
444 else
445 return RECURSE_SUBMODULES_ERROR;
449 int parse_submodule_fetchjobs(const char *var, const char *value,
450 const struct key_value_info *kvi)
452 int fetchjobs = git_config_int(var, value, kvi);
453 if (fetchjobs < 0)
454 die(_("negative values not allowed for submodule.fetchJobs"));
455 if (!fetchjobs)
456 fetchjobs = online_cpus();
457 return fetchjobs;
460 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
462 return parse_fetch_recurse(opt, arg, 1);
465 int option_fetch_parse_recurse_submodules(const struct option *opt,
466 const char *arg, int unset)
468 int *v;
470 if (!opt->value)
471 return -1;
473 v = opt->value;
475 if (unset) {
476 *v = RECURSE_SUBMODULES_OFF;
477 } else {
478 if (arg)
479 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
480 else
481 *v = RECURSE_SUBMODULES_ON;
483 return 0;
486 static int parse_update_recurse(const char *opt, const char *arg,
487 int die_on_error)
489 switch (git_parse_maybe_bool(arg)) {
490 case 1:
491 return RECURSE_SUBMODULES_ON;
492 case 0:
493 return RECURSE_SUBMODULES_OFF;
494 default:
495 if (die_on_error)
496 die("bad %s argument: %s", opt, arg);
497 return RECURSE_SUBMODULES_ERROR;
501 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
503 return parse_update_recurse(opt, arg, 1);
506 static int parse_push_recurse(const char *opt, const char *arg,
507 int die_on_error)
509 switch (git_parse_maybe_bool(arg)) {
510 case 1:
511 /* There's no simple "on" value when pushing */
512 if (die_on_error)
513 die("bad %s argument: %s", opt, arg);
514 else
515 return RECURSE_SUBMODULES_ERROR;
516 case 0:
517 return RECURSE_SUBMODULES_OFF;
518 default:
519 if (!strcmp(arg, "on-demand"))
520 return RECURSE_SUBMODULES_ON_DEMAND;
521 else if (!strcmp(arg, "check"))
522 return RECURSE_SUBMODULES_CHECK;
523 else if (!strcmp(arg, "only"))
524 return RECURSE_SUBMODULES_ONLY;
526 * Please update $__git_push_recurse_submodules in
527 * git-completion.bash when you add new modes.
529 else if (die_on_error)
530 die("bad %s argument: %s", opt, arg);
531 else
532 return RECURSE_SUBMODULES_ERROR;
536 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
538 return parse_push_recurse(opt, arg, 1);
541 static void warn_multiple_config(const struct object_id *treeish_name,
542 const char *name, const char *option)
544 const char *commit_string = "WORKTREE";
545 if (treeish_name)
546 commit_string = oid_to_hex(treeish_name);
547 warning("%s:.gitmodules, multiple configurations found for "
548 "'submodule.%s.%s'. Skipping second one!",
549 commit_string, name, option);
552 static void warn_command_line_option(const char *var, const char *value)
554 warning(_("ignoring '%s' which may be interpreted as"
555 " a command-line option: %s"), var, value);
558 struct parse_config_parameter {
559 struct submodule_cache *cache;
560 const struct object_id *treeish_name;
561 const struct object_id *gitmodules_oid;
562 int overwrite;
566 * Parse a config item from .gitmodules.
568 * This does not handle submodule-related configuration from the main
569 * config store (.git/config, etc). Callers are responsible for
570 * checking for overrides in the main config store when appropriate.
572 static int parse_config(const char *var, const char *value,
573 const struct config_context *ctx UNUSED, void *data)
575 struct parse_config_parameter *me = data;
576 struct submodule *submodule;
577 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
578 int ret = 0;
580 /* this also ensures that we only parse submodule entries */
581 if (!name_and_item_from_var(var, &name, &item))
582 return 0;
584 submodule = lookup_or_create_by_name(me->cache,
585 me->gitmodules_oid,
586 name.buf);
588 if (!strcmp(item.buf, "path")) {
589 if (!value)
590 ret = config_error_nonbool(var);
591 else if (looks_like_command_line_option(value))
592 warn_command_line_option(var, value);
593 else if (!me->overwrite && submodule->path)
594 warn_multiple_config(me->treeish_name, submodule->name,
595 "path");
596 else {
597 if (submodule->path)
598 cache_remove_path(me->cache, submodule);
599 free((void *) submodule->path);
600 submodule->path = xstrdup(value);
601 cache_put_path(me->cache, submodule);
603 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
604 /* when parsing worktree configurations we can die early */
605 int die_on_error = is_null_oid(me->gitmodules_oid);
606 if (!me->overwrite &&
607 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
608 warn_multiple_config(me->treeish_name, submodule->name,
609 "fetchrecursesubmodules");
610 else
611 submodule->fetch_recurse = parse_fetch_recurse(
612 var, value,
613 die_on_error);
614 } else if (!strcmp(item.buf, "ignore")) {
615 if (!value)
616 ret = config_error_nonbool(var);
617 else if (!me->overwrite && submodule->ignore)
618 warn_multiple_config(me->treeish_name, submodule->name,
619 "ignore");
620 else if (strcmp(value, "untracked") &&
621 strcmp(value, "dirty") &&
622 strcmp(value, "all") &&
623 strcmp(value, "none"))
624 warning("Invalid parameter '%s' for config option "
625 "'submodule.%s.ignore'", value, name.buf);
626 else {
627 free((void *) submodule->ignore);
628 submodule->ignore = xstrdup(value);
630 } else if (!strcmp(item.buf, "url")) {
631 if (!value) {
632 ret = config_error_nonbool(var);
633 } else if (looks_like_command_line_option(value)) {
634 warn_command_line_option(var, value);
635 } else if (!me->overwrite && submodule->url) {
636 warn_multiple_config(me->treeish_name, submodule->name,
637 "url");
638 } else {
639 free((void *) submodule->url);
640 submodule->url = xstrdup(value);
642 } else if (!strcmp(item.buf, "update")) {
643 if (!value)
644 ret = config_error_nonbool(var);
645 else if (!me->overwrite &&
646 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
647 warn_multiple_config(me->treeish_name, submodule->name,
648 "update");
649 else if (parse_submodule_update_strategy(value,
650 &submodule->update_strategy) < 0 ||
651 submodule->update_strategy.type == SM_UPDATE_COMMAND)
652 die(_("invalid value for '%s'"), var);
653 } else if (!strcmp(item.buf, "shallow")) {
654 if (!me->overwrite && submodule->recommend_shallow != -1)
655 warn_multiple_config(me->treeish_name, submodule->name,
656 "shallow");
657 else
658 submodule->recommend_shallow =
659 git_config_bool(var, value);
660 } else if (!strcmp(item.buf, "branch")) {
661 if (!value)
662 ret = config_error_nonbool(var);
663 else if (!me->overwrite && submodule->branch)
664 warn_multiple_config(me->treeish_name, submodule->name,
665 "branch");
666 else {
667 free((void *)submodule->branch);
668 submodule->branch = xstrdup(value);
672 strbuf_release(&name);
673 strbuf_release(&item);
675 return ret;
678 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
679 struct object_id *gitmodules_oid,
680 struct strbuf *rev)
682 int ret = 0;
684 if (is_null_oid(treeish_name)) {
685 oidclr(gitmodules_oid);
686 return 1;
689 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
690 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
691 ret = 1;
693 return ret;
696 /* This does a lookup of a submodule configuration by name or by path
697 * (key) with on-demand reading of the appropriate .gitmodules from
698 * revisions.
700 static const struct submodule *config_from(struct submodule_cache *cache,
701 const struct object_id *treeish_name, const char *key,
702 enum lookup_type lookup_type)
704 struct strbuf rev = STRBUF_INIT;
705 unsigned long config_size;
706 char *config = NULL;
707 struct object_id oid;
708 enum object_type type;
709 const struct submodule *submodule = NULL;
710 struct parse_config_parameter parameter;
713 * If any parameter except the cache is a NULL pointer just
714 * return the first submodule. Can be used to check whether
715 * there are any submodules parsed.
717 if (!treeish_name || !key) {
718 struct hashmap_iter iter;
719 struct submodule_entry *entry;
721 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
722 struct submodule_entry,
723 ent /* member name */);
724 if (!entry)
725 return NULL;
726 return entry->config;
729 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
730 goto out;
732 switch (lookup_type) {
733 case lookup_name:
734 submodule = cache_lookup_name(cache, &oid, key);
735 break;
736 case lookup_path:
737 submodule = cache_lookup_path(cache, &oid, key);
738 break;
740 if (submodule)
741 goto out;
743 config = repo_read_object_file(the_repository, &oid, &type,
744 &config_size);
745 if (!config || type != OBJ_BLOB)
746 goto out;
748 /* fill the submodule config into the cache */
749 parameter.cache = cache;
750 parameter.treeish_name = treeish_name;
751 parameter.gitmodules_oid = &oid;
752 parameter.overwrite = 0;
753 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
754 config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
755 strbuf_release(&rev);
756 free(config);
758 switch (lookup_type) {
759 case lookup_name:
760 return cache_lookup_name(cache, &oid, key);
761 case lookup_path:
762 return cache_lookup_path(cache, &oid, key);
763 default:
764 return NULL;
767 out:
768 strbuf_release(&rev);
769 free(config);
770 return submodule;
773 static void submodule_cache_check_init(struct repository *repo)
775 if (repo->submodule_cache && repo->submodule_cache->initialized)
776 return;
778 if (!repo->submodule_cache)
779 repo->submodule_cache = submodule_cache_alloc();
781 submodule_cache_init(repo->submodule_cache);
785 * Note: This function is private for a reason, the '.gitmodules' file should
786 * not be used as a mechanism to retrieve arbitrary configuration stored in
787 * the repository.
789 * Runs the provided config function on the '.gitmodules' file found in the
790 * working directory.
792 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
794 if (repo->worktree) {
795 struct git_config_source config_source = {
796 0, .scope = CONFIG_SCOPE_SUBMODULE
798 const struct config_options opts = { 0 };
799 struct object_id oid;
800 char *file;
801 char *oidstr = NULL;
803 file = repo_worktree_path(repo, GITMODULES_FILE);
804 if (file_exists(file)) {
805 config_source.file = file;
806 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
807 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
808 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
809 if (repo != the_repository)
810 add_submodule_odb_by_path(repo->objects->odb->path);
811 } else {
812 goto out;
815 config_with_options(fn, data, &config_source, repo, &opts);
817 out:
818 free(oidstr);
819 free(file);
823 static int gitmodules_cb(const char *var, const char *value,
824 const struct config_context *ctx, void *data)
826 struct repository *repo = data;
827 struct parse_config_parameter parameter;
829 parameter.cache = repo->submodule_cache;
830 parameter.treeish_name = NULL;
831 parameter.gitmodules_oid = null_oid();
832 parameter.overwrite = 1;
834 return parse_config(var, value, ctx, &parameter);
837 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
839 submodule_cache_check_init(repo);
841 if (repo->submodule_cache->gitmodules_read && skip_if_read)
842 return;
844 if (repo_read_index(repo) < 0)
845 return;
847 if (!is_gitmodules_unmerged(repo->index))
848 config_from_gitmodules(gitmodules_cb, repo, repo);
850 repo->submodule_cache->gitmodules_read = 1;
853 void gitmodules_config_oid(const struct object_id *commit_oid)
855 struct strbuf rev = STRBUF_INIT;
856 struct object_id oid;
858 submodule_cache_check_init(the_repository);
860 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
861 git_config_from_blob_oid(gitmodules_cb, rev.buf,
862 the_repository, &oid, the_repository,
863 CONFIG_SCOPE_UNKNOWN);
865 strbuf_release(&rev);
867 the_repository->submodule_cache->gitmodules_read = 1;
870 const struct submodule *submodule_from_name(struct repository *r,
871 const struct object_id *treeish_name,
872 const char *name)
874 repo_read_gitmodules(r, 1);
875 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
878 const struct submodule *submodule_from_path(struct repository *r,
879 const struct object_id *treeish_name,
880 const char *path)
882 repo_read_gitmodules(r, 1);
883 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
887 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
888 * and appends submodule entries to 'out'. The submodule_cache expects
889 * a root-level treeish_name and paths, so keep track of these values
890 * with 'root_tree' and 'prefix'.
892 static void traverse_tree_submodules(struct repository *r,
893 const struct object_id *root_tree,
894 char *prefix,
895 const struct object_id *treeish_name,
896 struct submodule_entry_list *out)
898 struct tree_desc tree;
899 struct submodule_tree_entry *st_entry;
900 struct name_entry *name_entry;
901 char *tree_path = NULL;
903 name_entry = xmalloc(sizeof(*name_entry));
905 fill_tree_descriptor(r, &tree, treeish_name);
906 while (tree_entry(&tree, name_entry)) {
907 if (prefix)
908 tree_path =
909 mkpathdup("%s/%s", prefix, name_entry->path);
910 else
911 tree_path = xstrdup(name_entry->path);
913 if (S_ISGITLINK(name_entry->mode) &&
914 is_tree_submodule_active(r, root_tree, tree_path)) {
915 ALLOC_GROW(out->entries, out->entry_nr + 1,
916 out->entry_alloc);
917 st_entry = &out->entries[out->entry_nr++];
919 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
920 *st_entry->name_entry = *name_entry;
921 st_entry->submodule =
922 submodule_from_path(r, root_tree, tree_path);
923 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
924 if (repo_submodule_init(st_entry->repo, r, tree_path,
925 root_tree))
926 FREE_AND_NULL(st_entry->repo);
928 } else if (S_ISDIR(name_entry->mode))
929 traverse_tree_submodules(r, root_tree, tree_path,
930 &name_entry->oid, out);
931 free(tree_path);
935 void submodules_of_tree(struct repository *r,
936 const struct object_id *treeish_name,
937 struct submodule_entry_list *out)
939 CALLOC_ARRAY(out->entries, 0);
940 out->entry_nr = 0;
941 out->entry_alloc = 0;
943 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
946 void submodule_free(struct repository *r)
948 if (r->submodule_cache)
949 submodule_cache_clear(r->submodule_cache);
952 static int config_print_callback(const char *var, const char *value,
953 const struct config_context *ctx UNUSED,
954 void *cb_data)
956 char *wanted_key = cb_data;
958 if (!strcmp(wanted_key, var))
959 printf("%s\n", value);
961 return 0;
964 int print_config_from_gitmodules(struct repository *repo, const char *key)
966 int ret;
967 char *store_key;
969 ret = git_config_parse_key(key, &store_key, NULL);
970 if (ret < 0)
971 return CONFIG_INVALID_KEY;
973 config_from_gitmodules(config_print_callback, repo, store_key);
975 free(store_key);
976 return 0;
979 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
981 int ret;
983 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, NULL, value);
984 if (ret < 0)
985 /* Maybe the user already did that, don't error out here */
986 warning(_("Could not update .gitmodules entry %s"), key);
988 return ret;
991 struct fetch_config {
992 int *max_children;
993 int *recurse_submodules;
996 static int gitmodules_fetch_config(const char *var, const char *value,
997 const struct config_context *ctx,
998 void *cb)
1000 struct fetch_config *config = cb;
1001 if (!strcmp(var, "submodule.fetchjobs")) {
1002 if (config->max_children)
1003 *(config->max_children) =
1004 parse_submodule_fetchjobs(var, value, ctx->kvi);
1005 return 0;
1006 } else if (!strcmp(var, "fetch.recursesubmodules")) {
1007 if (config->recurse_submodules)
1008 *(config->recurse_submodules) =
1009 parse_fetch_recurse_submodules_arg(var, value);
1010 return 0;
1013 return 0;
1016 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
1018 struct fetch_config config = {
1019 .max_children = max_children,
1020 .recurse_submodules = recurse_submodules
1022 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
1025 static int gitmodules_update_clone_config(const char *var, const char *value,
1026 const struct config_context *ctx,
1027 void *cb)
1029 int *max_jobs = cb;
1030 if (!strcmp(var, "submodule.fetchjobs"))
1031 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
1032 return 0;
1035 void update_clone_config_from_gitmodules(int *max_jobs)
1037 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);