]> git.ipfire.org Git - thirdparty/git.git/blame - submodule-config.c
refs: move submodule slash stripping code to get_submodule_ref_store
[thirdparty/git.git] / submodule-config.c
CommitLineData
959b5455 1#include "cache.h"
bf12fcdf 2#include "repository.h"
b2141fc1 3#include "config.h"
959b5455
HV
4#include "submodule-config.h"
5#include "submodule.h"
6#include "strbuf.h"
886dc154 7#include "parse-options.h"
959b5455
HV
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;
bf12fcdf 20 unsigned initialized:1;
959b5455
HV
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
7663cdc8 37static int config_path_cmp(const void *unused_cmp_data,
152cbdc6
SB
38 const void *entry,
39 const void *entry_or_key,
7663cdc8 40 const void *unused_keydata)
959b5455 41{
152cbdc6
SB
42 const struct submodule_entry *a = entry;
43 const struct submodule_entry *b = entry_or_key;
44
959b5455
HV
45 return strcmp(a->config->path, b->config->path) ||
46 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
47}
48
7663cdc8 49static int config_name_cmp(const void *unused_cmp_data,
152cbdc6
SB
50 const void *entry,
51 const void *entry_or_key,
7663cdc8 52 const void *unused_keydata)
959b5455 53{
152cbdc6
SB
54 const struct submodule_entry *a = entry;
55 const struct submodule_entry *b = entry_or_key;
56
959b5455
HV
57 return strcmp(a->config->name, b->config->name) ||
58 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
59}
60
bf12fcdf
BW
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)
959b5455 67{
152cbdc6
SB
68 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
69 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
bf12fcdf 70 cache->initialized = 1;
959b5455
HV
71}
72
73static void free_one_config(struct submodule_entry *entry)
74{
75 free((void *) entry->config->path);
76 free((void *) entry->config->name);
b5944f34 77 free((void *) entry->config->branch);
ea2fa5a3 78 free((void *) entry->config->update_strategy.command);
959b5455
HV
79 free(entry->config);
80}
81
bf12fcdf 82static void submodule_cache_clear(struct submodule_cache *cache)
959b5455
HV
83{
84 struct hashmap_iter iter;
85 struct submodule_entry *entry;
86
bf12fcdf
BW
87 if (!cache->initialized)
88 return;
89
959b5455
HV
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);
bf12fcdf
BW
101 cache->initialized = 0;
102}
103
104void submodule_cache_free(struct submodule_cache *cache)
105{
106 submodule_cache_clear(cache);
107 free(cache);
959b5455
HV
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;
ea2fa5a3
SB
224 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
225 submodule->update_strategy.command = NULL;
959b5455
HV
226 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
227 submodule->ignore = NULL;
b5944f34 228 submodule->branch = NULL;
37f52e93 229 submodule->recommend_shallow = -1;
959b5455
HV
230
231 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
232
233 cache_add(cache, submodule);
234
235 return submodule;
236}
237
027771fc
HV
238static int parse_fetch_recurse(const char *opt, const char *arg,
239 int die_on_error)
240{
89576613 241 switch (git_parse_maybe_bool(arg)) {
027771fc
HV
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
f20e7c1e
BW
257int parse_submodule_fetchjobs(const char *var, const char *value)
258{
259 int fetchjobs = git_config_int(var, value);
260 if (fetchjobs < 0)
261 die(_("negative values not allowed for submodule.fetchjobs"));
262 return fetchjobs;
263}
264
027771fc
HV
265int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
266{
267 return parse_fetch_recurse(opt, arg, 1);
268}
269
886dc154
SB
270int option_fetch_parse_recurse_submodules(const struct option *opt,
271 const char *arg, int unset)
272{
273 int *v;
274
275 if (!opt->value)
276 return -1;
277
278 v = opt->value;
279
280 if (unset) {
281 *v = RECURSE_SUBMODULES_OFF;
282 } else {
283 if (arg)
284 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
285 else
286 *v = RECURSE_SUBMODULES_ON;
287 }
288 return 0;
289}
290
d601fd09
SB
291static int parse_update_recurse(const char *opt, const char *arg,
292 int die_on_error)
293{
bdfcdefd 294 switch (git_parse_maybe_bool(arg)) {
d601fd09
SB
295 case 1:
296 return RECURSE_SUBMODULES_ON;
297 case 0:
298 return RECURSE_SUBMODULES_OFF;
299 default:
300 if (die_on_error)
301 die("bad %s argument: %s", opt, arg);
302 return RECURSE_SUBMODULES_ERROR;
303 }
304}
305
306int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
307{
308 return parse_update_recurse(opt, arg, 1);
309}
310
b33a15b0
MC
311static int parse_push_recurse(const char *opt, const char *arg,
312 int die_on_error)
313{
89576613 314 switch (git_parse_maybe_bool(arg)) {
b33a15b0
MC
315 case 1:
316 /* There's no simple "on" value when pushing */
317 if (die_on_error)
318 die("bad %s argument: %s", opt, arg);
319 else
320 return RECURSE_SUBMODULES_ERROR;
321 case 0:
322 return RECURSE_SUBMODULES_OFF;
323 default:
324 if (!strcmp(arg, "on-demand"))
325 return RECURSE_SUBMODULES_ON_DEMAND;
326 else if (!strcmp(arg, "check"))
327 return RECURSE_SUBMODULES_CHECK;
6c656c3f
BW
328 else if (!strcmp(arg, "only"))
329 return RECURSE_SUBMODULES_ONLY;
b33a15b0
MC
330 else if (die_on_error)
331 die("bad %s argument: %s", opt, arg);
332 else
333 return RECURSE_SUBMODULES_ERROR;
334 }
335}
336
337int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
338{
339 return parse_push_recurse(opt, arg, 1);
340}
341
73c293bb 342static void warn_multiple_config(const unsigned char *treeish_name,
959b5455
HV
343 const char *name, const char *option)
344{
345 const char *commit_string = "WORKTREE";
73c293bb
SB
346 if (treeish_name)
347 commit_string = sha1_to_hex(treeish_name);
959b5455
HV
348 warning("%s:.gitmodules, multiple configurations found for "
349 "'submodule.%s.%s'. Skipping second one!",
350 commit_string, name, option);
351}
352
353struct parse_config_parameter {
354 struct submodule_cache *cache;
73c293bb 355 const unsigned char *treeish_name;
959b5455
HV
356 const unsigned char *gitmodules_sha1;
357 int overwrite;
358};
359
360static int parse_config(const char *var, const char *value, void *data)
361{
362 struct parse_config_parameter *me = data;
363 struct submodule *submodule;
364 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
365 int ret = 0;
366
367 /* this also ensures that we only parse submodule entries */
368 if (!name_and_item_from_var(var, &name, &item))
369 return 0;
370
147875fd
SB
371 submodule = lookup_or_create_by_name(me->cache,
372 me->gitmodules_sha1,
373 name.buf);
959b5455
HV
374
375 if (!strcmp(item.buf, "path")) {
147875fd 376 if (!value)
959b5455 377 ret = config_error_nonbool(var);
f73da110 378 else if (!me->overwrite && submodule->path)
73c293bb 379 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 380 "path");
147875fd
SB
381 else {
382 if (submodule->path)
383 cache_remove_path(me->cache, submodule);
384 free((void *) submodule->path);
385 submodule->path = xstrdup(value);
386 cache_put_path(me->cache, submodule);
959b5455 387 }
959b5455 388 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc
HV
389 /* when parsing worktree configurations we can die early */
390 int die_on_error = is_null_sha1(me->gitmodules_sha1);
959b5455 391 if (!me->overwrite &&
147875fd 392 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
73c293bb 393 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 394 "fetchrecursesubmodules");
147875fd
SB
395 else
396 submodule->fetch_recurse = parse_fetch_recurse(
397 var, value,
027771fc 398 die_on_error);
959b5455 399 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
400 if (!value)
401 ret = config_error_nonbool(var);
f73da110 402 else if (!me->overwrite && submodule->ignore)
73c293bb 403 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 404 "ignore");
147875fd
SB
405 else if (strcmp(value, "untracked") &&
406 strcmp(value, "dirty") &&
407 strcmp(value, "all") &&
408 strcmp(value, "none"))
959b5455 409 warning("Invalid parameter '%s' for config option "
5ea30489 410 "'submodule.%s.ignore'", value, name.buf);
147875fd
SB
411 else {
412 free((void *) submodule->ignore);
413 submodule->ignore = xstrdup(value);
959b5455 414 }
959b5455 415 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
416 if (!value) {
417 ret = config_error_nonbool(var);
f73da110 418 } else if (!me->overwrite && submodule->url) {
73c293bb 419 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 420 "url");
147875fd
SB
421 } else {
422 free((void *) submodule->url);
423 submodule->url = xstrdup(value);
959b5455 424 }
ea2fa5a3
SB
425 } else if (!strcmp(item.buf, "update")) {
426 if (!value)
427 ret = config_error_nonbool(var);
428 else if (!me->overwrite &&
429 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
73c293bb 430 warn_multiple_config(me->treeish_name, submodule->name,
ea2fa5a3
SB
431 "update");
432 else if (parse_submodule_update_strategy(value,
433 &submodule->update_strategy) < 0)
434 die(_("invalid value for %s"), var);
37f52e93
SB
435 } else if (!strcmp(item.buf, "shallow")) {
436 if (!me->overwrite && submodule->recommend_shallow != -1)
73c293bb 437 warn_multiple_config(me->treeish_name, submodule->name,
37f52e93 438 "shallow");
b5944f34 439 else
37f52e93
SB
440 submodule->recommend_shallow =
441 git_config_bool(var, value);
b5944f34
SB
442 } else if (!strcmp(item.buf, "branch")) {
443 if (!me->overwrite && submodule->branch)
73c293bb 444 warn_multiple_config(me->treeish_name, submodule->name,
b5944f34
SB
445 "branch");
446 else {
447 free((void *)submodule->branch);
448 submodule->branch = xstrdup(value);
37f52e93 449 }
959b5455
HV
450 }
451
959b5455
HV
452 strbuf_release(&name);
453 strbuf_release(&item);
454
455 return ret;
456}
457
cd73de47 458int gitmodule_oid_from_commit(const struct object_id *treeish_name,
459 struct object_id *gitmodules_oid,
514dea90 460 struct strbuf *rev)
959b5455 461{
959b5455
HV
462 int ret = 0;
463
cd73de47 464 if (is_null_oid(treeish_name)) {
465 oidclr(gitmodules_oid);
959b5455
HV
466 return 1;
467 }
468
cd73de47 469 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
470 if (get_oid(rev->buf, gitmodules_oid) >= 0)
959b5455
HV
471 ret = 1;
472
959b5455
HV
473 return ret;
474}
475
476/* This does a lookup of a submodule configuration by name or by path
477 * (key) with on-demand reading of the appropriate .gitmodules from
478 * revisions.
479 */
480static const struct submodule *config_from(struct submodule_cache *cache,
cd73de47 481 const struct object_id *treeish_name, const char *key,
959b5455
HV
482 enum lookup_type lookup_type)
483{
484 struct strbuf rev = STRBUF_INIT;
485 unsigned long config_size;
0918e250 486 char *config = NULL;
cd73de47 487 struct object_id oid;
959b5455
HV
488 enum object_type type;
489 const struct submodule *submodule = NULL;
490 struct parse_config_parameter parameter;
491
492 /*
493 * If any parameter except the cache is a NULL pointer just
494 * return the first submodule. Can be used to check whether
495 * there are any submodules parsed.
496 */
73c293bb 497 if (!treeish_name || !key) {
959b5455
HV
498 struct hashmap_iter iter;
499 struct submodule_entry *entry;
500
01d98e8a 501 entry = hashmap_iter_first(&cache->for_name, &iter);
959b5455
HV
502 if (!entry)
503 return NULL;
504 return entry->config;
505 }
506
cd73de47 507 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
0918e250 508 goto out;
959b5455
HV
509
510 switch (lookup_type) {
511 case lookup_name:
cd73de47 512 submodule = cache_lookup_name(cache, oid.hash, key);
959b5455
HV
513 break;
514 case lookup_path:
cd73de47 515 submodule = cache_lookup_path(cache, oid.hash, key);
959b5455
HV
516 break;
517 }
518 if (submodule)
0918e250 519 goto out;
959b5455 520
cd73de47 521 config = read_sha1_file(oid.hash, &type, &config_size);
0918e250
HV
522 if (!config || type != OBJ_BLOB)
523 goto out;
959b5455
HV
524
525 /* fill the submodule config into the cache */
526 parameter.cache = cache;
cd73de47 527 parameter.treeish_name = treeish_name->hash;
528 parameter.gitmodules_sha1 = oid.hash;
959b5455 529 parameter.overwrite = 0;
1b8132d9 530 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
473166b9 531 config, config_size, &parameter);
514dea90 532 strbuf_release(&rev);
959b5455
HV
533 free(config);
534
535 switch (lookup_type) {
536 case lookup_name:
cd73de47 537 return cache_lookup_name(cache, oid.hash, key);
959b5455 538 case lookup_path:
cd73de47 539 return cache_lookup_path(cache, oid.hash, key);
959b5455
HV
540 default:
541 return NULL;
542 }
0918e250
HV
543
544out:
545 strbuf_release(&rev);
546 free(config);
547 return submodule;
959b5455
HV
548}
549
bf12fcdf 550static void submodule_cache_check_init(struct repository *repo)
959b5455 551{
bf12fcdf 552 if (repo->submodule_cache && repo->submodule_cache->initialized)
959b5455
HV
553 return;
554
bf12fcdf
BW
555 if (!repo->submodule_cache)
556 repo->submodule_cache = submodule_cache_alloc();
557
558 submodule_cache_init(repo->submodule_cache);
959b5455
HV
559}
560
bf12fcdf
BW
561int submodule_config_option(struct repository *repo,
562 const char *var, const char *value)
851e18c3
HV
563{
564 struct parse_config_parameter parameter;
bf12fcdf
BW
565
566 submodule_cache_check_init(repo);
567
568 parameter.cache = repo->submodule_cache;
73c293bb 569 parameter.treeish_name = NULL;
851e18c3
HV
570 parameter.gitmodules_sha1 = null_sha1;
571 parameter.overwrite = 1;
572
851e18c3
HV
573 return parse_config(var, value, &parameter);
574}
575
bf12fcdf
BW
576int parse_submodule_config_option(const char *var, const char *value)
577{
578 return submodule_config_option(the_repository, var, value);
579}
580
cd73de47 581const struct submodule *submodule_from_name(const struct object_id *treeish_name,
959b5455
HV
582 const char *name)
583{
bf12fcdf
BW
584 submodule_cache_check_init(the_repository);
585 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
959b5455
HV
586}
587
cd73de47 588const struct submodule *submodule_from_path(const struct object_id *treeish_name,
959b5455
HV
589 const char *path)
590{
bf12fcdf
BW
591 submodule_cache_check_init(the_repository);
592 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
593}
594
595const struct submodule *submodule_from_cache(struct repository *repo,
cd73de47 596 const struct object_id *treeish_name,
bf12fcdf
BW
597 const char *key)
598{
599 submodule_cache_check_init(repo);
600 return config_from(repo->submodule_cache, treeish_name,
601 key, lookup_path);
959b5455
HV
602}
603
604void submodule_free(void)
605{
bf12fcdf
BW
606 if (the_repository->submodule_cache)
607 submodule_cache_clear(the_repository->submodule_cache);
959b5455 608}