at commit e4ad79fd

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