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