]> git.ipfire.org Git - thirdparty/git.git/blame - submodule-config.c
hashmap_get{,_from_hash} return "struct hashmap_entry *"
[thirdparty/git.git] / submodule-config.c
CommitLineData
959b5455 1#include "cache.h"
76e9bdc4 2#include "dir.h"
bf12fcdf 3#include "repository.h"
b2141fc1 4#include "config.h"
959b5455
HV
5#include "submodule-config.h"
6#include "submodule.h"
7#include "strbuf.h"
cbd53a21 8#include "object-store.h"
886dc154 9#include "parse-options.h"
959b5455
HV
10
11/*
12 * submodule cache lookup structure
13 * There is one shared set of 'struct submodule' entries which can be
5aea9fe6 14 * looked up by their sha1 blob id of the .gitmodules file and either
959b5455
HV
15 * using path or name as key.
16 * for_path stores submodule entries with path as key
17 * for_name stores submodule entries with name as key
18 */
19struct submodule_cache {
20 struct hashmap for_path;
21 struct hashmap for_name;
bf12fcdf 22 unsigned initialized:1;
ff6f1f56 23 unsigned gitmodules_read:1;
959b5455
HV
24};
25
26/*
27 * thin wrapper struct needed to insert 'struct submodule' entries to
28 * the hashmap
29 */
30struct submodule_entry {
31 struct hashmap_entry ent;
32 struct submodule *config;
33};
34
35enum lookup_type {
36 lookup_name,
37 lookup_path
38};
39
7663cdc8 40static int config_path_cmp(const void *unused_cmp_data,
152cbdc6
SB
41 const void *entry,
42 const void *entry_or_key,
7663cdc8 43 const void *unused_keydata)
959b5455 44{
152cbdc6
SB
45 const struct submodule_entry *a = entry;
46 const struct submodule_entry *b = entry_or_key;
47
959b5455 48 return strcmp(a->config->path, b->config->path) ||
9001dc2a 49 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
50}
51
7663cdc8 52static int config_name_cmp(const void *unused_cmp_data,
152cbdc6
SB
53 const void *entry,
54 const void *entry_or_key,
7663cdc8 55 const void *unused_keydata)
959b5455 56{
152cbdc6
SB
57 const struct submodule_entry *a = entry;
58 const struct submodule_entry *b = entry_or_key;
59
959b5455 60 return strcmp(a->config->name, b->config->name) ||
9001dc2a 61 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
62}
63
bf12fcdf
BW
64static struct submodule_cache *submodule_cache_alloc(void)
65{
66 return xcalloc(1, sizeof(struct submodule_cache));
67}
68
69static void submodule_cache_init(struct submodule_cache *cache)
959b5455 70{
152cbdc6
SB
71 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
72 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
bf12fcdf 73 cache->initialized = 1;
959b5455
HV
74}
75
76static void free_one_config(struct submodule_entry *entry)
77{
78 free((void *) entry->config->path);
79 free((void *) entry->config->name);
b5944f34 80 free((void *) entry->config->branch);
ea2fa5a3 81 free((void *) entry->config->update_strategy.command);
959b5455
HV
82 free(entry->config);
83}
84
bf12fcdf 85static void submodule_cache_clear(struct submodule_cache *cache)
959b5455
HV
86{
87 struct hashmap_iter iter;
88 struct submodule_entry *entry;
89
bf12fcdf
BW
90 if (!cache->initialized)
91 return;
92
959b5455
HV
93 /*
94 * We iterate over the name hash here to be symmetric with the
95 * allocation of struct submodule entries. Each is allocated by
5aea9fe6 96 * their .gitmodules blob sha1 and submodule name.
959b5455
HV
97 */
98 hashmap_iter_init(&cache->for_name, &iter);
99 while ((entry = hashmap_iter_next(&iter)))
100 free_one_config(entry);
101
102 hashmap_free(&cache->for_path, 1);
103 hashmap_free(&cache->for_name, 1);
bf12fcdf 104 cache->initialized = 0;
ff6f1f56 105 cache->gitmodules_read = 0;
bf12fcdf
BW
106}
107
108void submodule_cache_free(struct submodule_cache *cache)
109{
110 submodule_cache_clear(cache);
111 free(cache);
959b5455
HV
112}
113
34caab02 114static unsigned int hash_oid_string(const struct object_id *oid,
115 const char *string)
959b5455 116{
34caab02 117 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
959b5455
HV
118}
119
120static void cache_put_path(struct submodule_cache *cache,
121 struct submodule *submodule)
122{
34caab02 123 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
124 submodule->path);
959b5455 125 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 126 hashmap_entry_init(&e->ent, hash);
959b5455 127 e->config = submodule;
26b455f2 128 hashmap_put(&cache->for_path, &e->ent);
959b5455
HV
129}
130
131static void cache_remove_path(struct submodule_cache *cache,
132 struct submodule *submodule)
133{
34caab02 134 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
135 submodule->path);
959b5455
HV
136 struct submodule_entry e;
137 struct submodule_entry *removed;
d22245a2 138 hashmap_entry_init(&e.ent, hash);
959b5455 139 e.config = submodule;
28ee7941 140 removed = hashmap_remove(&cache->for_path, &e.ent, NULL);
959b5455
HV
141 free(removed);
142}
143
144static void cache_add(struct submodule_cache *cache,
145 struct submodule *submodule)
146{
34caab02 147 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
148 submodule->name);
959b5455 149 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 150 hashmap_entry_init(&e->ent, hash);
959b5455 151 e->config = submodule;
b94e5c1d 152 hashmap_add(&cache->for_name, &e->ent);
959b5455
HV
153}
154
155static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
34caab02 156 const struct object_id *gitmodules_oid, const char *path)
959b5455
HV
157{
158 struct submodule_entry *entry;
34caab02 159 unsigned int hash = hash_oid_string(gitmodules_oid, path);
959b5455
HV
160 struct submodule_entry key;
161 struct submodule key_config;
162
34caab02 163 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
164 key_config.path = path;
165
d22245a2 166 hashmap_entry_init(&key.ent, hash);
959b5455
HV
167 key.config = &key_config;
168
f23a4651
EW
169 entry = hashmap_get_entry(&cache->for_path, &key, NULL,
170 struct submodule_entry, ent);
959b5455
HV
171 if (entry)
172 return entry->config;
173 return NULL;
174}
175
176static struct submodule *cache_lookup_name(struct submodule_cache *cache,
34caab02 177 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
178{
179 struct submodule_entry *entry;
34caab02 180 unsigned int hash = hash_oid_string(gitmodules_oid, name);
959b5455
HV
181 struct submodule_entry key;
182 struct submodule key_config;
183
34caab02 184 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
185 key_config.name = name;
186
d22245a2 187 hashmap_entry_init(&key.ent, hash);
959b5455
HV
188 key.config = &key_config;
189
f23a4651
EW
190 entry = hashmap_get_entry(&cache->for_name, &key, NULL,
191 struct submodule_entry, ent);
959b5455
HV
192 if (entry)
193 return entry->config;
194 return NULL;
195}
196
0383bbb9
JK
197int check_submodule_name(const char *name)
198{
199 /* Disallow empty names */
200 if (!*name)
201 return -1;
202
203 /*
204 * Look for '..' as a path component. Check both '/' and '\\' as
205 * separators rather than is_dir_sep(), because we want the name rules
206 * to be consistent across platforms.
207 */
208 goto in_component; /* always start inside component */
209 while (*name) {
210 char c = *name++;
211 if (c == '/' || c == '\\') {
212in_component:
213 if (name[0] == '.' && name[1] == '.' &&
214 (!name[2] || name[2] == '/' || name[2] == '\\'))
215 return -1;
216 }
217 }
218
219 return 0;
220}
221
959b5455
HV
222static int name_and_item_from_var(const char *var, struct strbuf *name,
223 struct strbuf *item)
224{
225 const char *subsection, *key;
226 int subsection_len, parse;
227 parse = parse_config_key(var, "submodule", &subsection,
228 &subsection_len, &key);
229 if (parse < 0 || !subsection)
230 return 0;
231
232 strbuf_add(name, subsection, subsection_len);
0383bbb9
JK
233 if (check_submodule_name(name->buf) < 0) {
234 warning(_("ignoring suspicious submodule name: %s"), name->buf);
235 strbuf_release(name);
236 return 0;
237 }
238
959b5455
HV
239 strbuf_addstr(item, key);
240
241 return 1;
242}
243
244static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
34caab02 245 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
246{
247 struct submodule *submodule;
248 struct strbuf name_buf = STRBUF_INIT;
249
34caab02 250 submodule = cache_lookup_name(cache, gitmodules_oid, name);
959b5455
HV
251 if (submodule)
252 return submodule;
253
254 submodule = xmalloc(sizeof(*submodule));
255
256 strbuf_addstr(&name_buf, name);
257 submodule->name = strbuf_detach(&name_buf, NULL);
258
259 submodule->path = NULL;
260 submodule->url = NULL;
ea2fa5a3
SB
261 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
262 submodule->update_strategy.command = NULL;
959b5455
HV
263 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
264 submodule->ignore = NULL;
b5944f34 265 submodule->branch = NULL;
37f52e93 266 submodule->recommend_shallow = -1;
959b5455 267
34caab02 268 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
959b5455
HV
269
270 cache_add(cache, submodule);
271
272 return submodule;
273}
274
027771fc
HV
275static int parse_fetch_recurse(const char *opt, const char *arg,
276 int die_on_error)
277{
89576613 278 switch (git_parse_maybe_bool(arg)) {
027771fc
HV
279 case 1:
280 return RECURSE_SUBMODULES_ON;
281 case 0:
282 return RECURSE_SUBMODULES_OFF;
283 default:
284 if (!strcmp(arg, "on-demand"))
285 return RECURSE_SUBMODULES_ON_DEMAND;
5a59a230
NTND
286 /*
287 * Please update $__git_fetch_recurse_submodules in
288 * git-completion.bash when you add new options.
289 */
027771fc
HV
290 if (die_on_error)
291 die("bad %s argument: %s", opt, arg);
292 else
293 return RECURSE_SUBMODULES_ERROR;
294 }
295}
296
f20e7c1e
BW
297int parse_submodule_fetchjobs(const char *var, const char *value)
298{
299 int fetchjobs = git_config_int(var, value);
300 if (fetchjobs < 0)
301 die(_("negative values not allowed for submodule.fetchjobs"));
302 return fetchjobs;
303}
304
027771fc
HV
305int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
306{
307 return parse_fetch_recurse(opt, arg, 1);
308}
309
886dc154
SB
310int option_fetch_parse_recurse_submodules(const struct option *opt,
311 const char *arg, int unset)
312{
313 int *v;
314
315 if (!opt->value)
316 return -1;
317
318 v = opt->value;
319
320 if (unset) {
321 *v = RECURSE_SUBMODULES_OFF;
322 } else {
323 if (arg)
324 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
325 else
326 *v = RECURSE_SUBMODULES_ON;
327 }
328 return 0;
329}
330
d601fd09
SB
331static int parse_update_recurse(const char *opt, const char *arg,
332 int die_on_error)
333{
bdfcdefd 334 switch (git_parse_maybe_bool(arg)) {
d601fd09
SB
335 case 1:
336 return RECURSE_SUBMODULES_ON;
337 case 0:
338 return RECURSE_SUBMODULES_OFF;
339 default:
340 if (die_on_error)
341 die("bad %s argument: %s", opt, arg);
342 return RECURSE_SUBMODULES_ERROR;
343 }
344}
345
346int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
347{
348 return parse_update_recurse(opt, arg, 1);
349}
350
b33a15b0
MC
351static int parse_push_recurse(const char *opt, const char *arg,
352 int die_on_error)
353{
89576613 354 switch (git_parse_maybe_bool(arg)) {
b33a15b0
MC
355 case 1:
356 /* There's no simple "on" value when pushing */
357 if (die_on_error)
358 die("bad %s argument: %s", opt, arg);
359 else
360 return RECURSE_SUBMODULES_ERROR;
361 case 0:
362 return RECURSE_SUBMODULES_OFF;
363 default:
364 if (!strcmp(arg, "on-demand"))
365 return RECURSE_SUBMODULES_ON_DEMAND;
366 else if (!strcmp(arg, "check"))
367 return RECURSE_SUBMODULES_CHECK;
6c656c3f
BW
368 else if (!strcmp(arg, "only"))
369 return RECURSE_SUBMODULES_ONLY;
5a59a230
NTND
370 /*
371 * Please update $__git_push_recurse_submodules in
372 * git-completion.bash when you add new modes.
373 */
b33a15b0
MC
374 else if (die_on_error)
375 die("bad %s argument: %s", opt, arg);
376 else
377 return RECURSE_SUBMODULES_ERROR;
378 }
379}
380
381int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
382{
383 return parse_push_recurse(opt, arg, 1);
384}
385
34caab02 386static void warn_multiple_config(const struct object_id *treeish_name,
959b5455
HV
387 const char *name, const char *option)
388{
389 const char *commit_string = "WORKTREE";
73c293bb 390 if (treeish_name)
34caab02 391 commit_string = oid_to_hex(treeish_name);
959b5455
HV
392 warning("%s:.gitmodules, multiple configurations found for "
393 "'submodule.%s.%s'. Skipping second one!",
394 commit_string, name, option);
395}
396
f6adec4e
JK
397static void warn_command_line_option(const char *var, const char *value)
398{
399 warning(_("ignoring '%s' which may be interpreted as"
400 " a command-line option: %s"), var, value);
401}
402
959b5455
HV
403struct parse_config_parameter {
404 struct submodule_cache *cache;
34caab02 405 const struct object_id *treeish_name;
406 const struct object_id *gitmodules_oid;
959b5455
HV
407 int overwrite;
408};
409
410static int parse_config(const char *var, const char *value, void *data)
411{
412 struct parse_config_parameter *me = data;
413 struct submodule *submodule;
414 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
415 int ret = 0;
416
417 /* this also ensures that we only parse submodule entries */
418 if (!name_and_item_from_var(var, &name, &item))
419 return 0;
420
147875fd 421 submodule = lookup_or_create_by_name(me->cache,
34caab02 422 me->gitmodules_oid,
147875fd 423 name.buf);
959b5455
HV
424
425 if (!strcmp(item.buf, "path")) {
147875fd 426 if (!value)
959b5455 427 ret = config_error_nonbool(var);
273c6149
JK
428 else if (looks_like_command_line_option(value))
429 warn_command_line_option(var, value);
f73da110 430 else if (!me->overwrite && submodule->path)
73c293bb 431 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 432 "path");
147875fd
SB
433 else {
434 if (submodule->path)
435 cache_remove_path(me->cache, submodule);
436 free((void *) submodule->path);
437 submodule->path = xstrdup(value);
438 cache_put_path(me->cache, submodule);
959b5455 439 }
959b5455 440 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc 441 /* when parsing worktree configurations we can die early */
34caab02 442 int die_on_error = is_null_oid(me->gitmodules_oid);
959b5455 443 if (!me->overwrite &&
147875fd 444 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
73c293bb 445 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 446 "fetchrecursesubmodules");
147875fd
SB
447 else
448 submodule->fetch_recurse = parse_fetch_recurse(
449 var, value,
027771fc 450 die_on_error);
959b5455 451 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
452 if (!value)
453 ret = config_error_nonbool(var);
f73da110 454 else if (!me->overwrite && submodule->ignore)
73c293bb 455 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 456 "ignore");
147875fd
SB
457 else if (strcmp(value, "untracked") &&
458 strcmp(value, "dirty") &&
459 strcmp(value, "all") &&
460 strcmp(value, "none"))
959b5455 461 warning("Invalid parameter '%s' for config option "
5ea30489 462 "'submodule.%s.ignore'", value, name.buf);
147875fd
SB
463 else {
464 free((void *) submodule->ignore);
465 submodule->ignore = xstrdup(value);
959b5455 466 }
959b5455 467 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
468 if (!value) {
469 ret = config_error_nonbool(var);
f6adec4e
JK
470 } else if (looks_like_command_line_option(value)) {
471 warn_command_line_option(var, value);
f73da110 472 } else if (!me->overwrite && submodule->url) {
73c293bb 473 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 474 "url");
147875fd
SB
475 } else {
476 free((void *) submodule->url);
477 submodule->url = xstrdup(value);
959b5455 478 }
ea2fa5a3
SB
479 } else if (!strcmp(item.buf, "update")) {
480 if (!value)
481 ret = config_error_nonbool(var);
482 else if (!me->overwrite &&
483 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
73c293bb 484 warn_multiple_config(me->treeish_name, submodule->name,
ea2fa5a3
SB
485 "update");
486 else if (parse_submodule_update_strategy(value,
487 &submodule->update_strategy) < 0)
488 die(_("invalid value for %s"), var);
37f52e93
SB
489 } else if (!strcmp(item.buf, "shallow")) {
490 if (!me->overwrite && submodule->recommend_shallow != -1)
73c293bb 491 warn_multiple_config(me->treeish_name, submodule->name,
37f52e93 492 "shallow");
b5944f34 493 else
37f52e93
SB
494 submodule->recommend_shallow =
495 git_config_bool(var, value);
b5944f34
SB
496 } else if (!strcmp(item.buf, "branch")) {
497 if (!me->overwrite && submodule->branch)
73c293bb 498 warn_multiple_config(me->treeish_name, submodule->name,
b5944f34
SB
499 "branch");
500 else {
501 free((void *)submodule->branch);
502 submodule->branch = xstrdup(value);
37f52e93 503 }
959b5455
HV
504 }
505
959b5455
HV
506 strbuf_release(&name);
507 strbuf_release(&item);
508
509 return ret;
510}
511
1b796ace
BW
512static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
513 struct object_id *gitmodules_oid,
514 struct strbuf *rev)
959b5455 515{
959b5455
HV
516 int ret = 0;
517
cd73de47 518 if (is_null_oid(treeish_name)) {
519 oidclr(gitmodules_oid);
959b5455
HV
520 return 1;
521 }
522
cd73de47 523 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
524 if (get_oid(rev->buf, gitmodules_oid) >= 0)
959b5455
HV
525 ret = 1;
526
959b5455
HV
527 return ret;
528}
529
530/* This does a lookup of a submodule configuration by name or by path
531 * (key) with on-demand reading of the appropriate .gitmodules from
532 * revisions.
533 */
534static const struct submodule *config_from(struct submodule_cache *cache,
cd73de47 535 const struct object_id *treeish_name, const char *key,
959b5455
HV
536 enum lookup_type lookup_type)
537{
538 struct strbuf rev = STRBUF_INIT;
539 unsigned long config_size;
0918e250 540 char *config = NULL;
cd73de47 541 struct object_id oid;
959b5455
HV
542 enum object_type type;
543 const struct submodule *submodule = NULL;
544 struct parse_config_parameter parameter;
545
546 /*
547 * If any parameter except the cache is a NULL pointer just
548 * return the first submodule. Can be used to check whether
549 * there are any submodules parsed.
550 */
73c293bb 551 if (!treeish_name || !key) {
959b5455
HV
552 struct hashmap_iter iter;
553 struct submodule_entry *entry;
554
01d98e8a 555 entry = hashmap_iter_first(&cache->for_name, &iter);
959b5455
HV
556 if (!entry)
557 return NULL;
558 return entry->config;
559 }
560
cd73de47 561 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
0918e250 562 goto out;
959b5455
HV
563
564 switch (lookup_type) {
565 case lookup_name:
34caab02 566 submodule = cache_lookup_name(cache, &oid, key);
959b5455
HV
567 break;
568 case lookup_path:
34caab02 569 submodule = cache_lookup_path(cache, &oid, key);
959b5455
HV
570 break;
571 }
572 if (submodule)
0918e250 573 goto out;
959b5455 574
b4f5aca4 575 config = read_object_file(&oid, &type, &config_size);
0918e250
HV
576 if (!config || type != OBJ_BLOB)
577 goto out;
959b5455
HV
578
579 /* fill the submodule config into the cache */
580 parameter.cache = cache;
34caab02 581 parameter.treeish_name = treeish_name;
582 parameter.gitmodules_oid = &oid;
959b5455 583 parameter.overwrite = 0;
1b8132d9 584 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
4574f1aa 585 config, config_size, &parameter, NULL);
514dea90 586 strbuf_release(&rev);
959b5455
HV
587 free(config);
588
589 switch (lookup_type) {
590 case lookup_name:
34caab02 591 return cache_lookup_name(cache, &oid, key);
959b5455 592 case lookup_path:
34caab02 593 return cache_lookup_path(cache, &oid, key);
959b5455
HV
594 default:
595 return NULL;
596 }
0918e250
HV
597
598out:
599 strbuf_release(&rev);
600 free(config);
601 return submodule;
959b5455
HV
602}
603
bf12fcdf 604static void submodule_cache_check_init(struct repository *repo)
959b5455 605{
bf12fcdf 606 if (repo->submodule_cache && repo->submodule_cache->initialized)
959b5455
HV
607 return;
608
bf12fcdf
BW
609 if (!repo->submodule_cache)
610 repo->submodule_cache = submodule_cache_alloc();
611
612 submodule_cache_init(repo->submodule_cache);
959b5455
HV
613}
614
db64d112
AO
615/*
616 * Note: This function is private for a reason, the '.gitmodules' file should
617 * not be used as as a mechanism to retrieve arbitrary configuration stored in
618 * the repository.
619 *
620 * Runs the provided config function on the '.gitmodules' file found in the
621 * working directory.
622 */
623static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
624{
625 if (repo->worktree) {
76e9bdc4
AO
626 struct git_config_source config_source = { 0 };
627 const struct config_options opts = { 0 };
628 struct object_id oid;
629 char *file;
d9b8b8f8 630 char *oidstr = NULL;
76e9bdc4
AO
631
632 file = repo_worktree_path(repo, GITMODULES_FILE);
633 if (file_exists(file)) {
634 config_source.file = file;
d9b8b8f8
NTND
635 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
636 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
637 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
638 if (repo != the_repository)
639 add_to_alternates_memory(repo->objects->odb->path);
76e9bdc4
AO
640 } else {
641 goto out;
642 }
643
644 config_with_options(fn, data, &config_source, &opts);
645
646out:
d9b8b8f8 647 free(oidstr);
db64d112
AO
648 free(file);
649 }
650}
651
1b796ace 652static int gitmodules_cb(const char *var, const char *value, void *data)
851e18c3 653{
1b796ace 654 struct repository *repo = data;
851e18c3 655 struct parse_config_parameter parameter;
bf12fcdf 656
bf12fcdf 657 parameter.cache = repo->submodule_cache;
73c293bb 658 parameter.treeish_name = NULL;
34caab02 659 parameter.gitmodules_oid = &null_oid;
851e18c3
HV
660 parameter.overwrite = 1;
661
851e18c3
HV
662 return parse_config(var, value, &parameter);
663}
664
1b796ace 665void repo_read_gitmodules(struct repository *repo)
bf12fcdf 666{
ff6f1f56
BW
667 submodule_cache_check_init(repo);
668
db64d112
AO
669 if (repo_read_index(repo) < 0)
670 return;
1b796ace 671
db64d112
AO
672 if (!is_gitmodules_unmerged(repo->index))
673 config_from_gitmodules(gitmodules_cb, repo, repo);
ff6f1f56
BW
674
675 repo->submodule_cache->gitmodules_read = 1;
1b796ace
BW
676}
677
678void gitmodules_config_oid(const struct object_id *commit_oid)
679{
680 struct strbuf rev = STRBUF_INIT;
681 struct object_id oid;
682
ff6f1f56
BW
683 submodule_cache_check_init(the_repository);
684
1b796ace
BW
685 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
686 git_config_from_blob_oid(gitmodules_cb, rev.buf,
687 &oid, the_repository);
688 }
689 strbuf_release(&rev);
ff6f1f56
BW
690
691 the_repository->submodule_cache->gitmodules_read = 1;
692}
693
694static void gitmodules_read_check(struct repository *repo)
695{
696 submodule_cache_check_init(repo);
697
698 /* read the repo's .gitmodules file if it hasn't been already */
699 if (!repo->submodule_cache->gitmodules_read)
700 repo_read_gitmodules(repo);
bf12fcdf
BW
701}
702
3b8fb393
SB
703const struct submodule *submodule_from_name(struct repository *r,
704 const struct object_id *treeish_name,
959b5455
HV
705 const char *name)
706{
3b8fb393
SB
707 gitmodules_read_check(r);
708 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
959b5455
HV
709}
710
3b8fb393
SB
711const struct submodule *submodule_from_path(struct repository *r,
712 const struct object_id *treeish_name,
959b5455
HV
713 const char *path)
714{
3b8fb393
SB
715 gitmodules_read_check(r);
716 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
bf12fcdf
BW
717}
718
f793b895 719void submodule_free(struct repository *r)
bf12fcdf 720{
f793b895
SB
721 if (r->submodule_cache)
722 submodule_cache_clear(r->submodule_cache);
959b5455 723}
ad136370 724
bcbc780d
AO
725static int config_print_callback(const char *var, const char *value, void *cb_data)
726{
727 char *wanted_key = cb_data;
728
729 if (!strcmp(wanted_key, var))
730 printf("%s\n", value);
731
732 return 0;
733}
734
735int print_config_from_gitmodules(struct repository *repo, const char *key)
736{
737 int ret;
738 char *store_key;
739
740 ret = git_config_parse_key(key, &store_key, NULL);
741 if (ret < 0)
742 return CONFIG_INVALID_KEY;
743
744 config_from_gitmodules(config_print_callback, repo, store_key);
745
746 free(store_key);
747 return 0;
748}
749
45f5ef3d
AO
750int config_set_in_gitmodules_file_gently(const char *key, const char *value)
751{
752 int ret;
753
754 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
755 if (ret < 0)
756 /* Maybe the user already did that, don't error out here */
757 warning(_("Could not update .gitmodules entry %s"), key);
758
759 return ret;
760}
761
71a6953d
AO
762struct fetch_config {
763 int *max_children;
764 int *recurse_submodules;
765};
766
767static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
768{
769 struct fetch_config *config = cb;
770 if (!strcmp(var, "submodule.fetchjobs")) {
771 *(config->max_children) = parse_submodule_fetchjobs(var, value);
772 return 0;
773 } else if (!strcmp(var, "fetch.recursesubmodules")) {
774 *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
775 return 0;
776 }
777
778 return 0;
779}
780
781void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
782{
783 struct fetch_config config = {
784 .max_children = max_children,
785 .recurse_submodules = recurse_submodules
786 };
9a0fb3e7 787 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
71a6953d 788}
05744997
AO
789
790static int gitmodules_update_clone_config(const char *var, const char *value,
791 void *cb)
792{
793 int *max_jobs = cb;
794 if (!strcmp(var, "submodule.fetchjobs"))
795 *max_jobs = parse_submodule_fetchjobs(var, value);
796 return 0;
797}
798
799void update_clone_config_from_gitmodules(int *max_jobs)
800{
9a0fb3e7 801 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
05744997 802}