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