]> git.ipfire.org Git - thirdparty/git.git/blob - submodule-config.c
Merge branch 'nd/unpack-trees-with-cache-tree'
[thirdparty/git.git] / submodule-config.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "submodule-config.h"
5 #include "submodule.h"
6 #include "strbuf.h"
7 #include "object-store.h"
8 #include "parse-options.h"
9
10 /*
11 * submodule cache lookup structure
12 * There is one shared set of 'struct submodule' entries which can be
13 * looked up by their sha1 blob id of the .gitmodules file and either
14 * using path or name as key.
15 * for_path stores submodule entries with path as key
16 * for_name stores submodule entries with name as key
17 */
18 struct submodule_cache {
19 struct hashmap for_path;
20 struct hashmap for_name;
21 unsigned initialized:1;
22 unsigned gitmodules_read:1;
23 };
24
25 /*
26 * thin wrapper struct needed to insert 'struct submodule' entries to
27 * the hashmap
28 */
29 struct submodule_entry {
30 struct hashmap_entry ent;
31 struct submodule *config;
32 };
33
34 enum lookup_type {
35 lookup_name,
36 lookup_path
37 };
38
39 static int config_path_cmp(const void *unused_cmp_data,
40 const void *entry,
41 const void *entry_or_key,
42 const void *unused_keydata)
43 {
44 const struct submodule_entry *a = entry;
45 const struct submodule_entry *b = entry_or_key;
46
47 return strcmp(a->config->path, b->config->path) ||
48 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
49 }
50
51 static int config_name_cmp(const void *unused_cmp_data,
52 const void *entry,
53 const void *entry_or_key,
54 const void *unused_keydata)
55 {
56 const struct submodule_entry *a = entry;
57 const struct submodule_entry *b = entry_or_key;
58
59 return strcmp(a->config->name, b->config->name) ||
60 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
61 }
62
63 static struct submodule_cache *submodule_cache_alloc(void)
64 {
65 return xcalloc(1, sizeof(struct submodule_cache));
66 }
67
68 static void submodule_cache_init(struct submodule_cache *cache)
69 {
70 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
71 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
72 cache->initialized = 1;
73 }
74
75 static void free_one_config(struct submodule_entry *entry)
76 {
77 free((void *) entry->config->path);
78 free((void *) entry->config->name);
79 free((void *) entry->config->branch);
80 free((void *) entry->config->update_strategy.command);
81 free(entry->config);
82 }
83
84 static void submodule_cache_clear(struct submodule_cache *cache)
85 {
86 struct hashmap_iter iter;
87 struct submodule_entry *entry;
88
89 if (!cache->initialized)
90 return;
91
92 /*
93 * We iterate over the name hash here to be symmetric with the
94 * allocation of struct submodule entries. Each is allocated by
95 * their .gitmodules blob sha1 and submodule name.
96 */
97 hashmap_iter_init(&cache->for_name, &iter);
98 while ((entry = hashmap_iter_next(&iter)))
99 free_one_config(entry);
100
101 hashmap_free(&cache->for_path, 1);
102 hashmap_free(&cache->for_name, 1);
103 cache->initialized = 0;
104 cache->gitmodules_read = 0;
105 }
106
107 void submodule_cache_free(struct submodule_cache *cache)
108 {
109 submodule_cache_clear(cache);
110 free(cache);
111 }
112
113 static unsigned int hash_oid_string(const struct object_id *oid,
114 const char *string)
115 {
116 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
117 }
118
119 static void cache_put_path(struct submodule_cache *cache,
120 struct submodule *submodule)
121 {
122 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
123 submodule->path);
124 struct submodule_entry *e = xmalloc(sizeof(*e));
125 hashmap_entry_init(e, hash);
126 e->config = submodule;
127 hashmap_put(&cache->for_path, e);
128 }
129
130 static void cache_remove_path(struct submodule_cache *cache,
131 struct submodule *submodule)
132 {
133 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
134 submodule->path);
135 struct submodule_entry e;
136 struct submodule_entry *removed;
137 hashmap_entry_init(&e, hash);
138 e.config = submodule;
139 removed = hashmap_remove(&cache->for_path, &e, NULL);
140 free(removed);
141 }
142
143 static void cache_add(struct submodule_cache *cache,
144 struct submodule *submodule)
145 {
146 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
147 submodule->name);
148 struct submodule_entry *e = xmalloc(sizeof(*e));
149 hashmap_entry_init(e, hash);
150 e->config = submodule;
151 hashmap_add(&cache->for_name, e);
152 }
153
154 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
155 const struct object_id *gitmodules_oid, const char *path)
156 {
157 struct submodule_entry *entry;
158 unsigned int hash = hash_oid_string(gitmodules_oid, path);
159 struct submodule_entry key;
160 struct submodule key_config;
161
162 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
163 key_config.path = path;
164
165 hashmap_entry_init(&key, hash);
166 key.config = &key_config;
167
168 entry = hashmap_get(&cache->for_path, &key, NULL);
169 if (entry)
170 return entry->config;
171 return NULL;
172 }
173
174 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
175 const struct object_id *gitmodules_oid, const char *name)
176 {
177 struct submodule_entry *entry;
178 unsigned int hash = hash_oid_string(gitmodules_oid, name);
179 struct submodule_entry key;
180 struct submodule key_config;
181
182 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
183 key_config.name = name;
184
185 hashmap_entry_init(&key, hash);
186 key.config = &key_config;
187
188 entry = hashmap_get(&cache->for_name, &key, NULL);
189 if (entry)
190 return entry->config;
191 return NULL;
192 }
193
194 int check_submodule_name(const char *name)
195 {
196 /* Disallow empty names */
197 if (!*name)
198 return -1;
199
200 /*
201 * Look for '..' as a path component. Check both '/' and '\\' as
202 * separators rather than is_dir_sep(), because we want the name rules
203 * to be consistent across platforms.
204 */
205 goto in_component; /* always start inside component */
206 while (*name) {
207 char c = *name++;
208 if (c == '/' || c == '\\') {
209 in_component:
210 if (name[0] == '.' && name[1] == '.' &&
211 (!name[2] || name[2] == '/' || name[2] == '\\'))
212 return -1;
213 }
214 }
215
216 return 0;
217 }
218
219 static int name_and_item_from_var(const char *var, struct strbuf *name,
220 struct strbuf *item)
221 {
222 const char *subsection, *key;
223 int subsection_len, parse;
224 parse = parse_config_key(var, "submodule", &subsection,
225 &subsection_len, &key);
226 if (parse < 0 || !subsection)
227 return 0;
228
229 strbuf_add(name, subsection, subsection_len);
230 if (check_submodule_name(name->buf) < 0) {
231 warning(_("ignoring suspicious submodule name: %s"), name->buf);
232 strbuf_release(name);
233 return 0;
234 }
235
236 strbuf_addstr(item, key);
237
238 return 1;
239 }
240
241 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
242 const struct object_id *gitmodules_oid, const char *name)
243 {
244 struct submodule *submodule;
245 struct strbuf name_buf = STRBUF_INIT;
246
247 submodule = cache_lookup_name(cache, gitmodules_oid, name);
248 if (submodule)
249 return submodule;
250
251 submodule = xmalloc(sizeof(*submodule));
252
253 strbuf_addstr(&name_buf, name);
254 submodule->name = strbuf_detach(&name_buf, NULL);
255
256 submodule->path = NULL;
257 submodule->url = NULL;
258 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
259 submodule->update_strategy.command = NULL;
260 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
261 submodule->ignore = NULL;
262 submodule->branch = NULL;
263 submodule->recommend_shallow = -1;
264
265 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
266
267 cache_add(cache, submodule);
268
269 return submodule;
270 }
271
272 static int parse_fetch_recurse(const char *opt, const char *arg,
273 int die_on_error)
274 {
275 switch (git_parse_maybe_bool(arg)) {
276 case 1:
277 return RECURSE_SUBMODULES_ON;
278 case 0:
279 return RECURSE_SUBMODULES_OFF;
280 default:
281 if (!strcmp(arg, "on-demand"))
282 return RECURSE_SUBMODULES_ON_DEMAND;
283
284 if (die_on_error)
285 die("bad %s argument: %s", opt, arg);
286 else
287 return RECURSE_SUBMODULES_ERROR;
288 }
289 }
290
291 int parse_submodule_fetchjobs(const char *var, const char *value)
292 {
293 int fetchjobs = git_config_int(var, value);
294 if (fetchjobs < 0)
295 die(_("negative values not allowed for submodule.fetchjobs"));
296 return fetchjobs;
297 }
298
299 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
300 {
301 return parse_fetch_recurse(opt, arg, 1);
302 }
303
304 int option_fetch_parse_recurse_submodules(const struct option *opt,
305 const char *arg, int unset)
306 {
307 int *v;
308
309 if (!opt->value)
310 return -1;
311
312 v = opt->value;
313
314 if (unset) {
315 *v = RECURSE_SUBMODULES_OFF;
316 } else {
317 if (arg)
318 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
319 else
320 *v = RECURSE_SUBMODULES_ON;
321 }
322 return 0;
323 }
324
325 static int parse_update_recurse(const char *opt, const char *arg,
326 int die_on_error)
327 {
328 switch (git_parse_maybe_bool(arg)) {
329 case 1:
330 return RECURSE_SUBMODULES_ON;
331 case 0:
332 return RECURSE_SUBMODULES_OFF;
333 default:
334 if (die_on_error)
335 die("bad %s argument: %s", opt, arg);
336 return RECURSE_SUBMODULES_ERROR;
337 }
338 }
339
340 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
341 {
342 return parse_update_recurse(opt, arg, 1);
343 }
344
345 static int parse_push_recurse(const char *opt, const char *arg,
346 int die_on_error)
347 {
348 switch (git_parse_maybe_bool(arg)) {
349 case 1:
350 /* There's no simple "on" value when pushing */
351 if (die_on_error)
352 die("bad %s argument: %s", opt, arg);
353 else
354 return RECURSE_SUBMODULES_ERROR;
355 case 0:
356 return RECURSE_SUBMODULES_OFF;
357 default:
358 if (!strcmp(arg, "on-demand"))
359 return RECURSE_SUBMODULES_ON_DEMAND;
360 else if (!strcmp(arg, "check"))
361 return RECURSE_SUBMODULES_CHECK;
362 else if (!strcmp(arg, "only"))
363 return RECURSE_SUBMODULES_ONLY;
364 else if (die_on_error)
365 die("bad %s argument: %s", opt, arg);
366 else
367 return RECURSE_SUBMODULES_ERROR;
368 }
369 }
370
371 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
372 {
373 return parse_push_recurse(opt, arg, 1);
374 }
375
376 static void warn_multiple_config(const struct object_id *treeish_name,
377 const char *name, const char *option)
378 {
379 const char *commit_string = "WORKTREE";
380 if (treeish_name)
381 commit_string = oid_to_hex(treeish_name);
382 warning("%s:.gitmodules, multiple configurations found for "
383 "'submodule.%s.%s'. Skipping second one!",
384 commit_string, name, option);
385 }
386
387 static void warn_command_line_option(const char *var, const char *value)
388 {
389 warning(_("ignoring '%s' which may be interpreted as"
390 " a command-line option: %s"), var, value);
391 }
392
393 struct parse_config_parameter {
394 struct submodule_cache *cache;
395 const struct object_id *treeish_name;
396 const struct object_id *gitmodules_oid;
397 int overwrite;
398 };
399
400 static int parse_config(const char *var, const char *value, void *data)
401 {
402 struct parse_config_parameter *me = data;
403 struct submodule *submodule;
404 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
405 int ret = 0;
406
407 /* this also ensures that we only parse submodule entries */
408 if (!name_and_item_from_var(var, &name, &item))
409 return 0;
410
411 submodule = lookup_or_create_by_name(me->cache,
412 me->gitmodules_oid,
413 name.buf);
414
415 if (!strcmp(item.buf, "path")) {
416 if (!value)
417 ret = config_error_nonbool(var);
418 else if (looks_like_command_line_option(value))
419 warn_command_line_option(var, value);
420 else if (!me->overwrite && submodule->path)
421 warn_multiple_config(me->treeish_name, submodule->name,
422 "path");
423 else {
424 if (submodule->path)
425 cache_remove_path(me->cache, submodule);
426 free((void *) submodule->path);
427 submodule->path = xstrdup(value);
428 cache_put_path(me->cache, submodule);
429 }
430 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
431 /* when parsing worktree configurations we can die early */
432 int die_on_error = is_null_oid(me->gitmodules_oid);
433 if (!me->overwrite &&
434 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
435 warn_multiple_config(me->treeish_name, submodule->name,
436 "fetchrecursesubmodules");
437 else
438 submodule->fetch_recurse = parse_fetch_recurse(
439 var, value,
440 die_on_error);
441 } else if (!strcmp(item.buf, "ignore")) {
442 if (!value)
443 ret = config_error_nonbool(var);
444 else if (!me->overwrite && submodule->ignore)
445 warn_multiple_config(me->treeish_name, submodule->name,
446 "ignore");
447 else if (strcmp(value, "untracked") &&
448 strcmp(value, "dirty") &&
449 strcmp(value, "all") &&
450 strcmp(value, "none"))
451 warning("Invalid parameter '%s' for config option "
452 "'submodule.%s.ignore'", value, name.buf);
453 else {
454 free((void *) submodule->ignore);
455 submodule->ignore = xstrdup(value);
456 }
457 } else if (!strcmp(item.buf, "url")) {
458 if (!value) {
459 ret = config_error_nonbool(var);
460 } else if (looks_like_command_line_option(value)) {
461 warn_command_line_option(var, value);
462 } else if (!me->overwrite && submodule->url) {
463 warn_multiple_config(me->treeish_name, submodule->name,
464 "url");
465 } else {
466 free((void *) submodule->url);
467 submodule->url = xstrdup(value);
468 }
469 } else if (!strcmp(item.buf, "update")) {
470 if (!value)
471 ret = config_error_nonbool(var);
472 else if (!me->overwrite &&
473 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
474 warn_multiple_config(me->treeish_name, submodule->name,
475 "update");
476 else if (parse_submodule_update_strategy(value,
477 &submodule->update_strategy) < 0)
478 die(_("invalid value for %s"), var);
479 } else if (!strcmp(item.buf, "shallow")) {
480 if (!me->overwrite && submodule->recommend_shallow != -1)
481 warn_multiple_config(me->treeish_name, submodule->name,
482 "shallow");
483 else
484 submodule->recommend_shallow =
485 git_config_bool(var, value);
486 } else if (!strcmp(item.buf, "branch")) {
487 if (!me->overwrite && submodule->branch)
488 warn_multiple_config(me->treeish_name, submodule->name,
489 "branch");
490 else {
491 free((void *)submodule->branch);
492 submodule->branch = xstrdup(value);
493 }
494 }
495
496 strbuf_release(&name);
497 strbuf_release(&item);
498
499 return ret;
500 }
501
502 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
503 struct object_id *gitmodules_oid,
504 struct strbuf *rev)
505 {
506 int ret = 0;
507
508 if (is_null_oid(treeish_name)) {
509 oidclr(gitmodules_oid);
510 return 1;
511 }
512
513 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
514 if (get_oid(rev->buf, gitmodules_oid) >= 0)
515 ret = 1;
516
517 return ret;
518 }
519
520 /* This does a lookup of a submodule configuration by name or by path
521 * (key) with on-demand reading of the appropriate .gitmodules from
522 * revisions.
523 */
524 static const struct submodule *config_from(struct submodule_cache *cache,
525 const struct object_id *treeish_name, const char *key,
526 enum lookup_type lookup_type)
527 {
528 struct strbuf rev = STRBUF_INIT;
529 unsigned long config_size;
530 char *config = NULL;
531 struct object_id oid;
532 enum object_type type;
533 const struct submodule *submodule = NULL;
534 struct parse_config_parameter parameter;
535
536 /*
537 * If any parameter except the cache is a NULL pointer just
538 * return the first submodule. Can be used to check whether
539 * there are any submodules parsed.
540 */
541 if (!treeish_name || !key) {
542 struct hashmap_iter iter;
543 struct submodule_entry *entry;
544
545 entry = hashmap_iter_first(&cache->for_name, &iter);
546 if (!entry)
547 return NULL;
548 return entry->config;
549 }
550
551 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
552 goto out;
553
554 switch (lookup_type) {
555 case lookup_name:
556 submodule = cache_lookup_name(cache, &oid, key);
557 break;
558 case lookup_path:
559 submodule = cache_lookup_path(cache, &oid, key);
560 break;
561 }
562 if (submodule)
563 goto out;
564
565 config = read_object_file(&oid, &type, &config_size);
566 if (!config || type != OBJ_BLOB)
567 goto out;
568
569 /* fill the submodule config into the cache */
570 parameter.cache = cache;
571 parameter.treeish_name = treeish_name;
572 parameter.gitmodules_oid = &oid;
573 parameter.overwrite = 0;
574 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
575 config, config_size, &parameter, NULL);
576 strbuf_release(&rev);
577 free(config);
578
579 switch (lookup_type) {
580 case lookup_name:
581 return cache_lookup_name(cache, &oid, key);
582 case lookup_path:
583 return cache_lookup_path(cache, &oid, key);
584 default:
585 return NULL;
586 }
587
588 out:
589 strbuf_release(&rev);
590 free(config);
591 return submodule;
592 }
593
594 static void submodule_cache_check_init(struct repository *repo)
595 {
596 if (repo->submodule_cache && repo->submodule_cache->initialized)
597 return;
598
599 if (!repo->submodule_cache)
600 repo->submodule_cache = submodule_cache_alloc();
601
602 submodule_cache_init(repo->submodule_cache);
603 }
604
605 /*
606 * Note: This function is private for a reason, the '.gitmodules' file should
607 * not be used as as a mechanism to retrieve arbitrary configuration stored in
608 * the repository.
609 *
610 * Runs the provided config function on the '.gitmodules' file found in the
611 * working directory.
612 */
613 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
614 {
615 if (repo->worktree) {
616 char *file = repo_worktree_path(repo, GITMODULES_FILE);
617 git_config_from_file(fn, file, data);
618 free(file);
619 }
620 }
621
622 static int gitmodules_cb(const char *var, const char *value, void *data)
623 {
624 struct repository *repo = data;
625 struct parse_config_parameter parameter;
626
627 parameter.cache = repo->submodule_cache;
628 parameter.treeish_name = NULL;
629 parameter.gitmodules_oid = &null_oid;
630 parameter.overwrite = 1;
631
632 return parse_config(var, value, &parameter);
633 }
634
635 void repo_read_gitmodules(struct repository *repo)
636 {
637 submodule_cache_check_init(repo);
638
639 if (repo_read_index(repo) < 0)
640 return;
641
642 if (!is_gitmodules_unmerged(repo->index))
643 config_from_gitmodules(gitmodules_cb, repo, repo);
644
645 repo->submodule_cache->gitmodules_read = 1;
646 }
647
648 void gitmodules_config_oid(const struct object_id *commit_oid)
649 {
650 struct strbuf rev = STRBUF_INIT;
651 struct object_id oid;
652
653 submodule_cache_check_init(the_repository);
654
655 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
656 git_config_from_blob_oid(gitmodules_cb, rev.buf,
657 &oid, the_repository);
658 }
659 strbuf_release(&rev);
660
661 the_repository->submodule_cache->gitmodules_read = 1;
662 }
663
664 static void gitmodules_read_check(struct repository *repo)
665 {
666 submodule_cache_check_init(repo);
667
668 /* read the repo's .gitmodules file if it hasn't been already */
669 if (!repo->submodule_cache->gitmodules_read)
670 repo_read_gitmodules(repo);
671 }
672
673 const struct submodule *submodule_from_name(struct repository *r,
674 const struct object_id *treeish_name,
675 const char *name)
676 {
677 gitmodules_read_check(r);
678 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
679 }
680
681 const struct submodule *submodule_from_path(struct repository *r,
682 const struct object_id *treeish_name,
683 const char *path)
684 {
685 gitmodules_read_check(r);
686 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
687 }
688
689 void submodule_free(struct repository *r)
690 {
691 if (r->submodule_cache)
692 submodule_cache_clear(r->submodule_cache);
693 }
694
695 struct fetch_config {
696 int *max_children;
697 int *recurse_submodules;
698 };
699
700 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
701 {
702 struct fetch_config *config = cb;
703 if (!strcmp(var, "submodule.fetchjobs")) {
704 *(config->max_children) = parse_submodule_fetchjobs(var, value);
705 return 0;
706 } else if (!strcmp(var, "fetch.recursesubmodules")) {
707 *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
708 return 0;
709 }
710
711 return 0;
712 }
713
714 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
715 {
716 struct fetch_config config = {
717 .max_children = max_children,
718 .recurse_submodules = recurse_submodules
719 };
720 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
721 }
722
723 static int gitmodules_update_clone_config(const char *var, const char *value,
724 void *cb)
725 {
726 int *max_jobs = cb;
727 if (!strcmp(var, "submodule.fetchjobs"))
728 *max_jobs = parse_submodule_fetchjobs(var, value);
729 return 0;
730 }
731
732 void update_clone_config_from_gitmodules(int *max_jobs)
733 {
734 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
735 }