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