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