at commit 8751a0ff

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