interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / submodule-config.c

1047 lines · 28048 bytes Raw Blame History
1#define DISABLE_SIGN_COMPARE_WARNINGS
2
3#include "git-compat-util.h"
4#include "dir.h"
5#include "environment.h"
6#include "gettext.h"
7#include "hex.h"
8#include "path.h"
9#include "repository.h"
10#include "config.h"
11#include "submodule-config.h"
12#include "submodule.h"
13#include "strbuf.h"
14#include "object-name.h"
15#include "odb.h"
16#include "odb/source.h"
17#include "parse-options.h"
18#include "thread-utils.h"
19#include "tree-walk.h"
20#include "url.h"
21#include "urlmatch.h"
22
23/*
24 * submodule cache lookup structure
25 * There is one shared set of 'struct submodule' entries which can be
26 * looked up by their sha1 blob id of the .gitmodules file and either
27 * using path or name as key.
28 * for_path stores submodule entries with path as key
29 * for_name stores submodule entries with name as key
30 */
31struct submodule_cache {
32 struct hashmap for_path;
33 struct hashmap for_name;
34 unsigned initialized:1;
35 unsigned gitmodules_read:1;
36};
37
38/*
39 * thin wrapper struct needed to insert 'struct submodule' entries to
40 * the hashmap
41 */
42struct submodule_entry {
43 struct hashmap_entry ent;
44 struct submodule *config;
45};
46
47enum lookup_type {
48 lookup_name,
49 lookup_path
50};
51
52static int config_path_cmp(const void *cmp_data UNUSED,
53 const struct hashmap_entry *eptr,
54 const struct hashmap_entry *entry_or_key,
55 const void *keydata UNUSED)
56{
57 const struct submodule_entry *a, *b;
58
59 a = container_of(eptr, const struct submodule_entry, ent);
60 b = container_of(entry_or_key, const struct submodule_entry, ent);
61
62 return strcmp(a->config->path, b->config->path) ||
63 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
64}
65
66static int config_name_cmp(const void *cmp_data UNUSED,
67 const struct hashmap_entry *eptr,
68 const struct hashmap_entry *entry_or_key,
69 const void *keydata UNUSED)
70{
71 const struct submodule_entry *a, *b;
72
73 a = container_of(eptr, const struct submodule_entry, ent);
74 b = container_of(entry_or_key, const struct submodule_entry, ent);
75
76 return strcmp(a->config->name, b->config->name) ||
77 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
78}
79
80static struct submodule_cache *submodule_cache_alloc(void)
81{
82 return xcalloc(1, sizeof(struct submodule_cache));
83}
84
85static void submodule_cache_init(struct submodule_cache *cache)
86{
87 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
88 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
89 cache->initialized = 1;
90}
91
92static void free_one_config(struct submodule_entry *entry)
93{
94 free((void *) entry->config->path);
95 free((void *) entry->config->name);
96 free((void *) entry->config->branch);
97 free((void *) entry->config->url);
98 free((void *) entry->config->ignore);
99 submodule_update_strategy_release(&entry->config->update_strategy);
100 free(entry->config);
101}
102
103static void submodule_cache_clear(struct submodule_cache *cache)
104{
105 struct hashmap_iter iter;
106 struct submodule_entry *entry;
107
108 if (!cache->initialized)
109 return;
110
111 /*
112 * We iterate over the name hash here to be symmetric with the
113 * allocation of struct submodule entries. Each is allocated by
114 * their .gitmodules blob sha1 and submodule name.
115 */
116 hashmap_for_each_entry(&cache->for_name, &iter, entry,
117 ent /* member name */)
118 free_one_config(entry);
119
120 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
121 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
122 cache->initialized = 0;
123 cache->gitmodules_read = 0;
124}
125
126void submodule_cache_free(struct submodule_cache *cache)
127{
128 submodule_cache_clear(cache);
129 free(cache);
130}
131
132static unsigned int hash_oid_string(const struct object_id *oid,
133 const char *string)
134{
135 if (oid->algo == GIT_HASH_UNKNOWN)
136 BUG("hashing an object ID with unknown algorithm");
137 return memhash(oid->hash, hash_algos[oid->algo].rawsz) + strhash(string);
138}
139
140static void cache_put_path(struct submodule_cache *cache,
141 struct submodule *submodule)
142{
143 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
144 submodule->path);
145 struct submodule_entry *e = xmalloc(sizeof(*e));
146 hashmap_entry_init(&e->ent, hash);
147 e->config = submodule;
148 hashmap_put(&cache->for_path, &e->ent);
149}
150
151static void cache_remove_path(struct submodule_cache *cache,
152 struct submodule *submodule)
153{
154 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
155 submodule->path);
156 struct submodule_entry e;
157 struct submodule_entry *removed;
158 hashmap_entry_init(&e.ent, hash);
159 e.config = submodule;
160 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
161 free(removed);
162}
163
164static void cache_add(struct submodule_cache *cache,
165 struct submodule *submodule)
166{
167 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
168 submodule->name);
169 struct submodule_entry *e = xmalloc(sizeof(*e));
170 hashmap_entry_init(&e->ent, hash);
171 e->config = submodule;
172 hashmap_add(&cache->for_name, &e->ent);
173}
174
175static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
176 const struct object_id *gitmodules_oid, const char *path)
177{
178 struct submodule_entry *entry;
179 unsigned int hash = hash_oid_string(gitmodules_oid, path);
180 struct submodule_entry key;
181 struct submodule key_config;
182
183 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
184 key_config.path = path;
185
186 hashmap_entry_init(&key.ent, hash);
187 key.config = &key_config;
188
189 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
190 if (entry)
191 return entry->config;
192 return NULL;
193}
194
195static struct submodule *cache_lookup_name(struct submodule_cache *cache,
196 const struct object_id *gitmodules_oid, const char *name)
197{
198 struct submodule_entry *entry;
199 unsigned int hash = hash_oid_string(gitmodules_oid, name);
200 struct submodule_entry key;
201 struct submodule key_config;
202
203 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
204 key_config.name = name;
205
206 hashmap_entry_init(&key.ent, hash);
207 key.config = &key_config;
208
209 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
210 if (entry)
211 return entry->config;
212 return NULL;
213}
214
215int check_submodule_name(const char *name)
216{
217 /* Disallow empty names */
218 if (!*name)
219 return -1;
220
221 /*
222 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
223 * separators rather than is_dir_sep(), because we want the name rules
224 * to be consistent across platforms.
225 */
226 goto in_component; /* always start inside component */
227 while (*name) {
228 char c = *name++;
229 if (is_xplatform_dir_sep(c)) {
230in_component:
231 if (name[0] == '.' && name[1] == '.' &&
232 (!name[2] || is_xplatform_dir_sep(name[2])))
233 return -1;
234 }
235 }
236
237 return 0;
238}
239
240static int submodule_url_is_relative(const char *url)
241{
242 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
243}
244
245/*
246 * Count directory components that a relative submodule URL should chop
247 * from the remote_url it is to be resolved against.
248 *
249 * In other words, this counts "../" components at the start of a
250 * submodule URL.
251 *
252 * Returns the number of directory components to chop and writes a
253 * pointer to the next character of url after all leading "./" and
254 * "../" components to out.
255 */
256static int count_leading_dotdots(const char *url, const char **out)
257{
258 int result = 0;
259 while (1) {
260 if (starts_with_dot_dot_slash(url)) {
261 result++;
262 url += strlen("../");
263 continue;
264 }
265 if (starts_with_dot_slash(url)) {
266 url += strlen("./");
267 continue;
268 }
269 *out = url;
270 return result;
271 }
272}
273/*
274 * Check whether a transport is implemented by git-remote-curl.
275 *
276 * If it is, returns 1 and writes the URL that would be passed to
277 * git-remote-curl to the "out" parameter.
278 *
279 * Otherwise, returns 0 and leaves "out" untouched.
280 *
281 * Examples:
282 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
283 * https://example.com/repo.git -> 1, https://example.com/repo.git
284 * git://example.com/repo.git -> 0
285 *
286 * This is for use in checking for previously exploitable bugs that
287 * required a submodule URL to be passed to git-remote-curl.
288 */
289static int url_to_curl_url(const char *url, const char **out)
290{
291 /*
292 * We don't need to check for case-aliases, "http.exe", and so
293 * on because in the default configuration, is_transport_allowed
294 * prevents URLs with those schemes from being cloned
295 * automatically.
296 */
297 if (skip_prefix(url, "http::", out) ||
298 skip_prefix(url, "https::", out) ||
299 skip_prefix(url, "ftp::", out) ||
300 skip_prefix(url, "ftps::", out))
301 return 1;
302 if (starts_with(url, "http://") ||
303 starts_with(url, "https://") ||
304 starts_with(url, "ftp://") ||
305 starts_with(url, "ftps://")) {
306 *out = url;
307 return 1;
308 }
309 return 0;
310}
311
312int check_submodule_url(const char *url)
313{
314 const char *curl_url;
315
316 if (looks_like_command_line_option(url))
317 return -1;
318
319 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
320 char *decoded;
321 const char *next;
322 int has_nl;
323
324 /*
325 * This could be appended to an http URL and url-decoded;
326 * check for malicious characters.
327 */
328 decoded = url_decode(url);
329 has_nl = !!strchr(decoded, '\n');
330
331 free(decoded);
332 if (has_nl)
333 return -1;
334
335 /*
336 * URLs which escape their root via "../" can overwrite
337 * the host field and previous components, resolving to
338 * URLs like https::example.com/submodule.git and
339 * https:///example.com/submodule.git that were
340 * susceptible to CVE-2020-11008.
341 */
342 if (count_leading_dotdots(url, &next) > 0 &&
343 (*next == ':' || *next == '/'))
344 return -1;
345 }
346
347 else if (url_to_curl_url(url, &curl_url)) {
348 int ret = 0;
349 char *normalized = url_normalize(curl_url, NULL);
350 if (normalized) {
351 char *decoded = url_decode(normalized);
352 if (strchr(decoded, '\n'))
353 ret = -1;
354 free(normalized);
355 free(decoded);
356 } else {
357 ret = -1;
358 }
359
360 return ret;
361 }
362
363 return 0;
364}
365
366static int name_and_item_from_var(const char *var, struct strbuf *name,
367 struct strbuf *item)
368{
369 const char *subsection, *key;
370 size_t subsection_len;
371 int parse;
372 parse = parse_config_key(var, "submodule", &subsection,
373 &subsection_len, &key);
374 if (parse < 0 || !subsection)
375 return 0;
376
377 strbuf_add(name, subsection, subsection_len);
378 if (check_submodule_name(name->buf) < 0) {
379 warning(_("ignoring suspicious submodule name: %s"), name->buf);
380 strbuf_release(name);
381 return 0;
382 }
383
384 strbuf_addstr(item, key);
385
386 return 1;
387}
388
389static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
390 const struct object_id *gitmodules_oid, const char *name)
391{
392 struct submodule *submodule;
393 struct strbuf name_buf = STRBUF_INIT;
394
395 submodule = cache_lookup_name(cache, gitmodules_oid, name);
396 if (submodule)
397 return submodule;
398
399 submodule = xmalloc(sizeof(*submodule));
400
401 strbuf_addstr(&name_buf, name);
402 submodule->name = strbuf_detach(&name_buf, NULL);
403
404 submodule->path = NULL;
405 submodule->url = NULL;
406 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
407 submodule->update_strategy.command = NULL;
408 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
409 submodule->ignore = NULL;
410 submodule->branch = NULL;
411 submodule->recommend_shallow = -1;
412
413 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
414
415 cache_add(cache, submodule);
416
417 return submodule;
418}
419
420static int parse_fetch_recurse(const char *opt, const char *arg,
421 int die_on_error)
422{
423 switch (git_parse_maybe_bool(arg)) {
424 case 1:
425 return RECURSE_SUBMODULES_ON;
426 case 0:
427 return RECURSE_SUBMODULES_OFF;
428 default:
429 if (!strcmp(arg, "on-demand"))
430 return RECURSE_SUBMODULES_ON_DEMAND;
431 /*
432 * Please update $__git_fetch_recurse_submodules in
433 * git-completion.bash when you add new options.
434 */
435 if (die_on_error)
436 die("bad %s argument: %s", opt, arg);
437 else
438 return RECURSE_SUBMODULES_ERROR;
439 }
440}
441
442int parse_submodule_fetchjobs(const char *var, const char *value,
443 const struct key_value_info *kvi)
444{
445 int fetchjobs = git_config_int(var, value, kvi);
446 if (fetchjobs < 0)
447 die(_("negative values not allowed for submodule.fetchJobs"));
448 if (!fetchjobs)
449 fetchjobs = online_cpus();
450 return fetchjobs;
451}
452
453int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
454{
455 return parse_fetch_recurse(opt, arg, 1);
456}
457
458int option_fetch_parse_recurse_submodules(const struct option *opt,
459 const char *arg, int unset)
460{
461 int *v;
462
463 if (!opt->value)
464 return -1;
465
466 v = opt->value;
467
468 if (unset) {
469 *v = RECURSE_SUBMODULES_OFF;
470 } else {
471 if (arg)
472 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
473 else
474 *v = RECURSE_SUBMODULES_ON;
475 }
476 return 0;
477}
478
479static int parse_update_recurse(const char *opt, const char *arg,
480 int die_on_error)
481{
482 switch (git_parse_maybe_bool(arg)) {
483 case 1:
484 return RECURSE_SUBMODULES_ON;
485 case 0:
486 return RECURSE_SUBMODULES_OFF;
487 default:
488 if (die_on_error)
489 die("bad %s argument: %s", opt, arg);
490 return RECURSE_SUBMODULES_ERROR;
491 }
492}
493
494int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
495{
496 return parse_update_recurse(opt, arg, 1);
497}
498
499static int parse_push_recurse(const char *opt, const char *arg,
500 int die_on_error)
501{
502 switch (git_parse_maybe_bool(arg)) {
503 case 1:
504 /* There's no simple "on" value when pushing */
505 if (die_on_error)
506 die("bad %s argument: %s", opt, arg);
507 else
508 return RECURSE_SUBMODULES_ERROR;
509 case 0:
510 return RECURSE_SUBMODULES_OFF;
511 default:
512 if (!strcmp(arg, "on-demand"))
513 return RECURSE_SUBMODULES_ON_DEMAND;
514 else if (!strcmp(arg, "check"))
515 return RECURSE_SUBMODULES_CHECK;
516 else if (!strcmp(arg, "only"))
517 return RECURSE_SUBMODULES_ONLY;
518 /*
519 * Please update $__git_push_recurse_submodules in
520 * git-completion.bash when you add new modes.
521 */
522 else if (die_on_error)
523 die("bad %s argument: %s", opt, arg);
524 else
525 return RECURSE_SUBMODULES_ERROR;
526 }
527}
528
529int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
530{
531 return parse_push_recurse(opt, arg, 1);
532}
533
534static void warn_multiple_config(const struct object_id *treeish_name,
535 const char *name, const char *option)
536{
537 const char *commit_string = "WORKTREE";
538 if (treeish_name)
539 commit_string = oid_to_hex(treeish_name);
540 warning("%s:.gitmodules, multiple configurations found for "
541 "'submodule.%s.%s'. Skipping second one!",
542 commit_string, name, option);
543}
544
545static void warn_command_line_option(const char *var, const char *value)
546{
547 warning(_("ignoring '%s' which may be interpreted as"
548 " a command-line option: %s"), var, value);
549}
550
551struct parse_config_parameter {
552 struct submodule_cache *cache;
553 const struct object_id *treeish_name;
554 const struct object_id *gitmodules_oid;
555 int overwrite;
556};
557
558/*
559 * Parse a config item from .gitmodules.
560 *
561 * This does not handle submodule-related configuration from the main
562 * config store (.git/config, etc). Callers are responsible for
563 * checking for overrides in the main config store when appropriate.
564 */
565static int parse_config(const char *var, const char *value,
566 const struct config_context *ctx UNUSED, void *data)
567{
568 struct parse_config_parameter *me = data;
569 struct submodule *submodule;
570 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
571 int ret = 0;
572
573 /* this also ensures that we only parse submodule entries */
574 if (!name_and_item_from_var(var, &name, &item))
575 return 0;
576
577 submodule = lookup_or_create_by_name(me->cache,
578 me->gitmodules_oid,
579 name.buf);
580
581 if (!strcmp(item.buf, "path")) {
582 if (!value)
583 ret = config_error_nonbool(var);
584 else if (looks_like_command_line_option(value))
585 warn_command_line_option(var, value);
586 else if (!me->overwrite && submodule->path)
587 warn_multiple_config(me->treeish_name, submodule->name,
588 "path");
589 else {
590 if (submodule->path)
591 cache_remove_path(me->cache, submodule);
592 free((void *) submodule->path);
593 submodule->path = xstrdup(value);
594 cache_put_path(me->cache, submodule);
595 }
596 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
597 /* when parsing worktree configurations we can die early */
598 int die_on_error = is_null_oid(me->gitmodules_oid);
599 if (!me->overwrite &&
600 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
601 warn_multiple_config(me->treeish_name, submodule->name,
602 "fetchrecursesubmodules");
603 else
604 submodule->fetch_recurse = parse_fetch_recurse(
605 var, value,
606 die_on_error);
607 } else if (!strcmp(item.buf, "ignore")) {
608 if (!value)
609 ret = config_error_nonbool(var);
610 else if (!me->overwrite && submodule->ignore)
611 warn_multiple_config(me->treeish_name, submodule->name,
612 "ignore");
613 else if (strcmp(value, "untracked") &&
614 strcmp(value, "dirty") &&
615 strcmp(value, "all") &&
616 strcmp(value, "none"))
617 warning("Invalid parameter '%s' for config option "
618 "'submodule.%s.ignore'", value, name.buf);
619 else {
620 free((void *) submodule->ignore);
621 submodule->ignore = xstrdup(value);
622 }
623 } else if (!strcmp(item.buf, "url")) {
624 if (!value) {
625 ret = config_error_nonbool(var);
626 } else if (looks_like_command_line_option(value)) {
627 warn_command_line_option(var, value);
628 } else if (!me->overwrite && submodule->url) {
629 warn_multiple_config(me->treeish_name, submodule->name,
630 "url");
631 } else {
632 free((void *) submodule->url);
633 submodule->url = xstrdup(value);
634 }
635 } else if (!strcmp(item.buf, "update")) {
636 if (!value)
637 ret = config_error_nonbool(var);
638 else if (!me->overwrite &&
639 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
640 warn_multiple_config(me->treeish_name, submodule->name,
641 "update");
642 else if (parse_submodule_update_strategy(value,
643 &submodule->update_strategy) < 0 ||
644 submodule->update_strategy.type == SM_UPDATE_COMMAND)
645 die(_("invalid value for '%s'"), var);
646 } else if (!strcmp(item.buf, "shallow")) {
647 if (!me->overwrite && submodule->recommend_shallow != -1)
648 warn_multiple_config(me->treeish_name, submodule->name,
649 "shallow");
650 else
651 submodule->recommend_shallow =
652 git_config_bool(var, value);
653 } else if (!strcmp(item.buf, "branch")) {
654 if (!value)
655 ret = config_error_nonbool(var);
656 else if (!me->overwrite && submodule->branch)
657 warn_multiple_config(me->treeish_name, submodule->name,
658 "branch");
659 else {
660 free((void *)submodule->branch);
661 submodule->branch = xstrdup(value);
662 }
663 }
664
665 strbuf_release(&name);
666 strbuf_release(&item);
667
668 return ret;
669}
670
671static int gitmodule_oid_from_commit(struct repository *repo,
672 const struct object_id *treeish_name,
673 struct object_id *gitmodules_oid,
674 struct strbuf *rev)
675{
676 int ret = 0;
677
678 if (is_null_oid(treeish_name)) {
679 oidclr(gitmodules_oid, repo->hash_algo);
680 return 1;
681 }
682
683 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
684 if (repo_get_oid(repo, rev->buf, gitmodules_oid) >= 0)
685 ret = 1;
686
687 return ret;
688}
689
690/* This does a lookup of a submodule configuration by name or by path
691 * (key) with on-demand reading of the appropriate .gitmodules from
692 * revisions.
693 */
694static const struct submodule *config_from(struct repository *repo,
695 struct submodule_cache *cache,
696 const struct object_id *treeish_name,
697 const char *key,
698 enum lookup_type lookup_type)
699{
700 struct strbuf rev = STRBUF_INIT;
701 size_t config_size;
702 char *config = NULL;
703 struct object_id oid;
704 enum object_type type;
705 const struct submodule *submodule = NULL;
706 struct parse_config_parameter parameter;
707
708 /*
709 * If any parameter except the cache is a NULL pointer just
710 * return the first submodule. Can be used to check whether
711 * there are any submodules parsed.
712 */
713 if (!treeish_name || !key) {
714 struct hashmap_iter iter;
715 struct submodule_entry *entry;
716
717 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
718 struct submodule_entry,
719 ent /* member name */);
720 if (!entry)
721 return NULL;
722 return entry->config;
723 }
724
725 if (!gitmodule_oid_from_commit(repo, treeish_name, &oid, &rev))
726 goto out;
727
728 switch (lookup_type) {
729 case lookup_name:
730 submodule = cache_lookup_name(cache, &oid, key);
731 break;
732 case lookup_path:
733 submodule = cache_lookup_path(cache, &oid, key);
734 break;
735 }
736 if (submodule)
737 goto out;
738
739 config = odb_read_object(repo->objects, &oid,
740 &type, &config_size);
741 if (!config || type != OBJ_BLOB)
742 goto out;
743
744 /* fill the submodule config into the cache */
745 parameter.cache = cache;
746 parameter.treeish_name = treeish_name;
747 parameter.gitmodules_oid = &oid;
748 parameter.overwrite = 0;
749 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
750 config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
751 strbuf_release(&rev);
752 free(config);
753
754 switch (lookup_type) {
755 case lookup_name:
756 return cache_lookup_name(cache, &oid, key);
757 case lookup_path:
758 return cache_lookup_path(cache, &oid, key);
759 default:
760 return NULL;
761 }
762
763out:
764 strbuf_release(&rev);
765 free(config);
766 return submodule;
767}
768
769static void submodule_cache_check_init(struct repository *repo)
770{
771 if (repo->submodule_cache && repo->submodule_cache->initialized)
772 return;
773
774 if (!repo->submodule_cache)
775 repo->submodule_cache = submodule_cache_alloc();
776
777 submodule_cache_init(repo->submodule_cache);
778}
779
780/*
781 * Note: This function is private for a reason, the '.gitmodules' file should
782 * not be used as a mechanism to retrieve arbitrary configuration stored in
783 * the repository.
784 *
785 * Runs the provided config function on the '.gitmodules' file found in the
786 * working directory.
787 */
788static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
789{
790 if (repo->worktree) {
791 struct git_config_source config_source = {
792 0, .scope = CONFIG_SCOPE_SUBMODULE
793 };
794 const struct config_options opts = { 0 };
795 struct object_id oid;
796 char *file;
797 char *oidstr = NULL;
798
799 file = repo_worktree_path(repo, GITMODULES_FILE);
800 if (file_exists(file)) {
801 config_source.file = file;
802 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
803 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
804 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
805 } else {
806 goto out;
807 }
808
809 config_with_options(fn, data, &config_source, repo, &opts);
810
811out:
812 free(oidstr);
813 free(file);
814 }
815}
816
817static int gitmodules_cb(const char *var, const char *value,
818 const struct config_context *ctx, void *data)
819{
820 struct repository *repo = data;
821 struct parse_config_parameter parameter;
822
823 parameter.cache = repo->submodule_cache;
824 parameter.treeish_name = NULL;
825 parameter.gitmodules_oid = null_oid(repo->hash_algo);
826 parameter.overwrite = 1;
827
828 return parse_config(var, value, ctx, &parameter);
829}
830
831void repo_read_gitmodules(struct repository *repo, int skip_if_read)
832{
833 submodule_cache_check_init(repo);
834
835 if (repo->submodule_cache->gitmodules_read && skip_if_read)
836 return;
837
838 if (repo_read_index(repo) < 0)
839 return;
840
841 if (!is_gitmodules_unmerged(repo->index))
842 config_from_gitmodules(gitmodules_cb, repo, repo);
843
844 repo->submodule_cache->gitmodules_read = 1;
845}
846
847void gitmodules_config_oid(struct repository *repo,
848 const struct object_id *commit_oid)
849{
850 struct strbuf rev = STRBUF_INIT;
851 struct object_id oid;
852
853 submodule_cache_check_init(repo);
854
855 if (gitmodule_oid_from_commit(repo, commit_oid, &oid, &rev)) {
856 git_config_from_blob_oid(gitmodules_cb, rev.buf,
857 repo, &oid, repo,
858 CONFIG_SCOPE_UNKNOWN);
859 }
860 strbuf_release(&rev);
861
862 repo->submodule_cache->gitmodules_read = 1;
863}
864
865const struct submodule *submodule_from_name(struct repository *r,
866 const struct object_id *treeish_name,
867 const char *name)
868{
869 repo_read_gitmodules(r, 1);
870 return config_from(r, r->submodule_cache, treeish_name, name, lookup_name);
871}
872
873const struct submodule *submodule_from_path(struct repository *r,
874 const struct object_id *treeish_name,
875 const char *path)
876{
877 repo_read_gitmodules(r, 1);
878 return config_from(r, r->submodule_cache, treeish_name, path, lookup_path);
879}
880
881/**
882 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
883 * and appends submodule entries to 'out'. The submodule_cache expects
884 * a root-level treeish_name and paths, so keep track of these values
885 * with 'root_tree' and 'prefix'.
886 */
887static void traverse_tree_submodules(struct repository *r,
888 const struct object_id *root_tree,
889 char *prefix,
890 const struct object_id *treeish_name,
891 struct submodule_entry_list *out)
892{
893 struct tree_desc tree;
894 struct submodule_tree_entry *st_entry;
895 struct name_entry name_entry;
896 char *tree_path = NULL;
897 char *tree_buf;
898
899 tree_buf = fill_tree_descriptor(r, &tree, treeish_name);
900 while (tree_entry(&tree, &name_entry)) {
901 if (prefix)
902 tree_path =
903 mkpathdup("%s/%s", prefix, name_entry.path);
904 else
905 tree_path = xstrdup(name_entry.path);
906
907 if (S_ISGITLINK(name_entry.mode) &&
908 is_tree_submodule_active(r, root_tree, tree_path)) {
909 ALLOC_GROW(out->entries, out->entry_nr + 1,
910 out->entry_alloc);
911 st_entry = &out->entries[out->entry_nr++];
912
913 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
914 *st_entry->name_entry = name_entry;
915 st_entry->submodule =
916 submodule_from_path(r, root_tree, tree_path);
917 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
918 if (repo_submodule_init(st_entry->repo, r, tree_path,
919 root_tree))
920 FREE_AND_NULL(st_entry->repo);
921
922 } else if (S_ISDIR(name_entry.mode))
923 traverse_tree_submodules(r, root_tree, tree_path,
924 &name_entry.oid, out);
925 free(tree_path);
926 }
927
928 free(tree_buf);
929}
930
931void submodules_of_tree(struct repository *r,
932 const struct object_id *treeish_name,
933 struct submodule_entry_list *out)
934{
935 CALLOC_ARRAY(out->entries, 0);
936 out->entry_nr = 0;
937 out->entry_alloc = 0;
938
939 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
940}
941
942void submodule_entry_list_release(struct submodule_entry_list *list)
943{
944 for (size_t i = 0; i < list->entry_nr; i++) {
945 free(list->entries[i].name_entry);
946 repo_clear(list->entries[i].repo);
947 free(list->entries[i].repo);
948 }
949 free(list->entries);
950}
951
952void submodule_free(struct repository *r)
953{
954 if (r->submodule_cache)
955 submodule_cache_clear(r->submodule_cache);
956}
957
958static int config_print_callback(const char *var, const char *value,
959 const struct config_context *ctx UNUSED,
960 void *cb_data)
961{
962 char *wanted_key = cb_data;
963
964 if (!strcmp(wanted_key, var))
965 printf("%s\n", value);
966
967 return 0;
968}
969
970int print_config_from_gitmodules(struct repository *repo, const char *key)
971{
972 int ret;
973 char *store_key;
974
975 ret = git_config_parse_key(key, &store_key, NULL);
976 if (ret < 0)
977 return CONFIG_INVALID_KEY;
978
979 config_from_gitmodules(config_print_callback, repo, store_key);
980
981 free(store_key);
982 return 0;
983}
984
985int config_set_in_gitmodules_file_gently(struct repository *repo,
986 const char *key, const char *value)
987{
988 int ret;
989
990 ret = repo_config_set_in_file_gently(repo, GITMODULES_FILE, key, NULL, value);
991 if (ret < 0)
992 /* Maybe the user already did that, don't error out here */
993 warning(_("Could not update .gitmodules entry %s"), key);
994
995 return ret;
996}
997
998struct fetch_config {
999 int *max_children;
1000 int *recurse_submodules;
1001};
1002
1003static int gitmodules_fetch_config(const char *var, const char *value,
1004 const struct config_context *ctx,
1005 void *cb)
1006{
1007 struct fetch_config *config = cb;
1008 if (!strcmp(var, "submodule.fetchjobs")) {
1009 if (config->max_children)
1010 *(config->max_children) =
1011 parse_submodule_fetchjobs(var, value, ctx->kvi);
1012 return 0;
1013 } else if (!strcmp(var, "fetch.recursesubmodules")) {
1014 if (config->recurse_submodules)
1015 *(config->recurse_submodules) =
1016 parse_fetch_recurse_submodules_arg(var, value);
1017 return 0;
1018 }
1019
1020 return 0;
1021}
1022
1023void fetch_config_from_gitmodules(struct repository *repo,
1024 int *max_children,
1025 int *recurse_submodules)
1026{
1027 struct fetch_config config = {
1028 .max_children = max_children,
1029 .recurse_submodules = recurse_submodules
1030 };
1031 config_from_gitmodules(gitmodules_fetch_config, repo, &config);
1032}
1033
1034static int gitmodules_update_clone_config(const char *var, const char *value,
1035 const struct config_context *ctx,
1036 void *cb)
1037{
1038 int *max_jobs = cb;
1039 if (!strcmp(var, "submodule.fetchjobs"))
1040 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
1041 return 0;
1042}
1043
1044void update_clone_config_from_gitmodules(struct repository *repo, int *max_jobs)
1045{
1046 config_from_gitmodules(gitmodules_update_clone_config, repo, max_jobs);
1047}