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