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