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