]> git.ipfire.org Git - thirdparty/git.git/blame - submodule-config.c
split-index: accept that a base index can be empty
[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"
961b130d 10#include "tree-walk.h"
959b5455
HV
11
12/*
13 * submodule cache lookup structure
14 * There is one shared set of 'struct submodule' entries which can be
5aea9fe6 15 * looked up by their sha1 blob id of the .gitmodules file and either
959b5455
HV
16 * using path or name as key.
17 * for_path stores submodule entries with path as key
18 * for_name stores submodule entries with name as key
19 */
20struct submodule_cache {
21 struct hashmap for_path;
22 struct hashmap for_name;
bf12fcdf 23 unsigned initialized:1;
ff6f1f56 24 unsigned gitmodules_read:1;
959b5455
HV
25};
26
27/*
28 * thin wrapper struct needed to insert 'struct submodule' entries to
29 * the hashmap
30 */
31struct submodule_entry {
32 struct hashmap_entry ent;
33 struct submodule *config;
34};
35
36enum lookup_type {
37 lookup_name,
38 lookup_path
39};
40
5cf88fd8 41static int config_path_cmp(const void *cmp_data UNUSED,
939af16e
EW
42 const struct hashmap_entry *eptr,
43 const struct hashmap_entry *entry_or_key,
5cf88fd8 44 const void *keydata UNUSED)
959b5455 45{
939af16e
EW
46 const struct submodule_entry *a, *b;
47
48 a = container_of(eptr, const struct submodule_entry, ent);
49 b = container_of(entry_or_key, const struct submodule_entry, ent);
152cbdc6 50
959b5455 51 return strcmp(a->config->path, b->config->path) ||
9001dc2a 52 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
53}
54
5cf88fd8 55static int config_name_cmp(const void *cmp_data UNUSED,
939af16e
EW
56 const struct hashmap_entry *eptr,
57 const struct hashmap_entry *entry_or_key,
5cf88fd8 58 const void *keydata UNUSED)
959b5455 59{
939af16e
EW
60 const struct submodule_entry *a, *b;
61
62 a = container_of(eptr, const struct submodule_entry, ent);
63 b = container_of(entry_or_key, const struct submodule_entry, ent);
152cbdc6 64
959b5455 65 return strcmp(a->config->name, b->config->name) ||
9001dc2a 66 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
67}
68
bf12fcdf
BW
69static struct submodule_cache *submodule_cache_alloc(void)
70{
71 return xcalloc(1, sizeof(struct submodule_cache));
72}
73
74static void submodule_cache_init(struct submodule_cache *cache)
959b5455 75{
152cbdc6
SB
76 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
77 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
bf12fcdf 78 cache->initialized = 1;
959b5455
HV
79}
80
81static void free_one_config(struct submodule_entry *entry)
82{
83 free((void *) entry->config->path);
84 free((void *) entry->config->name);
b5944f34 85 free((void *) entry->config->branch);
ea2fa5a3 86 free((void *) entry->config->update_strategy.command);
959b5455
HV
87 free(entry->config);
88}
89
bf12fcdf 90static void submodule_cache_clear(struct submodule_cache *cache)
959b5455
HV
91{
92 struct hashmap_iter iter;
93 struct submodule_entry *entry;
94
bf12fcdf
BW
95 if (!cache->initialized)
96 return;
97
959b5455
HV
98 /*
99 * We iterate over the name hash here to be symmetric with the
100 * allocation of struct submodule entries. Each is allocated by
5aea9fe6 101 * their .gitmodules blob sha1 and submodule name.
959b5455 102 */
87571c3f 103 hashmap_for_each_entry(&cache->for_name, &iter, entry,
23dee69f 104 ent /* member name */)
959b5455
HV
105 free_one_config(entry);
106
6da1a258
EN
107 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
108 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
bf12fcdf 109 cache->initialized = 0;
ff6f1f56 110 cache->gitmodules_read = 0;
bf12fcdf
BW
111}
112
113void submodule_cache_free(struct submodule_cache *cache)
114{
115 submodule_cache_clear(cache);
116 free(cache);
959b5455
HV
117}
118
34caab02 119static unsigned int hash_oid_string(const struct object_id *oid,
120 const char *string)
959b5455 121{
34caab02 122 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
959b5455
HV
123}
124
125static void cache_put_path(struct submodule_cache *cache,
126 struct submodule *submodule)
127{
34caab02 128 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
129 submodule->path);
959b5455 130 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 131 hashmap_entry_init(&e->ent, hash);
959b5455 132 e->config = submodule;
26b455f2 133 hashmap_put(&cache->for_path, &e->ent);
959b5455
HV
134}
135
136static void cache_remove_path(struct submodule_cache *cache,
137 struct submodule *submodule)
138{
34caab02 139 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
140 submodule->path);
959b5455
HV
141 struct submodule_entry e;
142 struct submodule_entry *removed;
d22245a2 143 hashmap_entry_init(&e.ent, hash);
959b5455 144 e.config = submodule;
404ab78e 145 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
959b5455
HV
146 free(removed);
147}
148
149static void cache_add(struct submodule_cache *cache,
150 struct submodule *submodule)
151{
34caab02 152 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
153 submodule->name);
959b5455 154 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 155 hashmap_entry_init(&e->ent, hash);
959b5455 156 e->config = submodule;
b94e5c1d 157 hashmap_add(&cache->for_name, &e->ent);
959b5455
HV
158}
159
160static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
34caab02 161 const struct object_id *gitmodules_oid, const char *path)
959b5455
HV
162{
163 struct submodule_entry *entry;
34caab02 164 unsigned int hash = hash_oid_string(gitmodules_oid, path);
959b5455
HV
165 struct submodule_entry key;
166 struct submodule key_config;
167
34caab02 168 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
169 key_config.path = path;
170
d22245a2 171 hashmap_entry_init(&key.ent, hash);
959b5455
HV
172 key.config = &key_config;
173
404ab78e 174 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
959b5455
HV
175 if (entry)
176 return entry->config;
177 return NULL;
178}
179
180static struct submodule *cache_lookup_name(struct submodule_cache *cache,
34caab02 181 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
182{
183 struct submodule_entry *entry;
34caab02 184 unsigned int hash = hash_oid_string(gitmodules_oid, name);
959b5455
HV
185 struct submodule_entry key;
186 struct submodule key_config;
187
34caab02 188 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
189 key_config.name = name;
190
d22245a2 191 hashmap_entry_init(&key.ent, hash);
959b5455
HV
192 key.config = &key_config;
193
404ab78e 194 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
959b5455
HV
195 if (entry)
196 return entry->config;
197 return NULL;
198}
199
0383bbb9
JK
200int check_submodule_name(const char *name)
201{
202 /* Disallow empty names */
203 if (!*name)
204 return -1;
205
206 /*
9fd512c8 207 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
0383bbb9
JK
208 * separators rather than is_dir_sep(), because we want the name rules
209 * to be consistent across platforms.
210 */
211 goto in_component; /* always start inside component */
212 while (*name) {
213 char c = *name++;
9fd512c8 214 if (is_xplatform_dir_sep(c)) {
0383bbb9
JK
215in_component:
216 if (name[0] == '.' && name[1] == '.' &&
9fd512c8 217 (!name[2] || is_xplatform_dir_sep(name[2])))
0383bbb9
JK
218 return -1;
219 }
220 }
221
222 return 0;
223}
224
959b5455
HV
225static int name_and_item_from_var(const char *var, struct strbuf *name,
226 struct strbuf *item)
227{
228 const char *subsection, *key;
f5914f4b
JK
229 size_t subsection_len;
230 int parse;
959b5455
HV
231 parse = parse_config_key(var, "submodule", &subsection,
232 &subsection_len, &key);
233 if (parse < 0 || !subsection)
234 return 0;
235
236 strbuf_add(name, subsection, subsection_len);
0383bbb9
JK
237 if (check_submodule_name(name->buf) < 0) {
238 warning(_("ignoring suspicious submodule name: %s"), name->buf);
239 strbuf_release(name);
240 return 0;
241 }
242
959b5455
HV
243 strbuf_addstr(item, key);
244
245 return 1;
246}
247
248static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
34caab02 249 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
250{
251 struct submodule *submodule;
252 struct strbuf name_buf = STRBUF_INIT;
253
34caab02 254 submodule = cache_lookup_name(cache, gitmodules_oid, name);
959b5455
HV
255 if (submodule)
256 return submodule;
257
258 submodule = xmalloc(sizeof(*submodule));
259
260 strbuf_addstr(&name_buf, name);
261 submodule->name = strbuf_detach(&name_buf, NULL);
262
263 submodule->path = NULL;
264 submodule->url = NULL;
ea2fa5a3
SB
265 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
266 submodule->update_strategy.command = NULL;
959b5455
HV
267 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
268 submodule->ignore = NULL;
b5944f34 269 submodule->branch = NULL;
37f52e93 270 submodule->recommend_shallow = -1;
959b5455 271
34caab02 272 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
959b5455
HV
273
274 cache_add(cache, submodule);
275
276 return submodule;
277}
278
027771fc
HV
279static int parse_fetch_recurse(const char *opt, const char *arg,
280 int die_on_error)
281{
89576613 282 switch (git_parse_maybe_bool(arg)) {
027771fc
HV
283 case 1:
284 return RECURSE_SUBMODULES_ON;
285 case 0:
286 return RECURSE_SUBMODULES_OFF;
287 default:
288 if (!strcmp(arg, "on-demand"))
289 return RECURSE_SUBMODULES_ON_DEMAND;
5a59a230
NTND
290 /*
291 * Please update $__git_fetch_recurse_submodules in
292 * git-completion.bash when you add new options.
293 */
027771fc
HV
294 if (die_on_error)
295 die("bad %s argument: %s", opt, arg);
296 else
297 return RECURSE_SUBMODULES_ERROR;
298 }
299}
300
f20e7c1e
BW
301int parse_submodule_fetchjobs(const char *var, const char *value)
302{
303 int fetchjobs = git_config_int(var, value);
304 if (fetchjobs < 0)
b4eda05d 305 die(_("negative values not allowed for submodule.fetchJobs"));
51243f9f
ÆAB
306 if (!fetchjobs)
307 fetchjobs = online_cpus();
f20e7c1e
BW
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
e904deb8
JN
416/*
417 * Parse a config item from .gitmodules.
418 *
419 * This does not handle submodule-related configuration from the main
420 * config store (.git/config, etc). Callers are responsible for
421 * checking for overrides in the main config store when appropriate.
422 */
959b5455
HV
423static int parse_config(const char *var, const char *value, void *data)
424{
425 struct parse_config_parameter *me = data;
426 struct submodule *submodule;
427 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
428 int ret = 0;
429
430 /* this also ensures that we only parse submodule entries */
431 if (!name_and_item_from_var(var, &name, &item))
432 return 0;
433
147875fd 434 submodule = lookup_or_create_by_name(me->cache,
34caab02 435 me->gitmodules_oid,
147875fd 436 name.buf);
959b5455
HV
437
438 if (!strcmp(item.buf, "path")) {
147875fd 439 if (!value)
959b5455 440 ret = config_error_nonbool(var);
273c6149
JK
441 else if (looks_like_command_line_option(value))
442 warn_command_line_option(var, value);
f73da110 443 else if (!me->overwrite && submodule->path)
73c293bb 444 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 445 "path");
147875fd
SB
446 else {
447 if (submodule->path)
448 cache_remove_path(me->cache, submodule);
449 free((void *) submodule->path);
450 submodule->path = xstrdup(value);
451 cache_put_path(me->cache, submodule);
959b5455 452 }
959b5455 453 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc 454 /* when parsing worktree configurations we can die early */
34caab02 455 int die_on_error = is_null_oid(me->gitmodules_oid);
959b5455 456 if (!me->overwrite &&
147875fd 457 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
73c293bb 458 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 459 "fetchrecursesubmodules");
147875fd
SB
460 else
461 submodule->fetch_recurse = parse_fetch_recurse(
462 var, value,
027771fc 463 die_on_error);
959b5455 464 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
465 if (!value)
466 ret = config_error_nonbool(var);
f73da110 467 else if (!me->overwrite && submodule->ignore)
73c293bb 468 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 469 "ignore");
147875fd
SB
470 else if (strcmp(value, "untracked") &&
471 strcmp(value, "dirty") &&
472 strcmp(value, "all") &&
473 strcmp(value, "none"))
959b5455 474 warning("Invalid parameter '%s' for config option "
5ea30489 475 "'submodule.%s.ignore'", value, name.buf);
147875fd
SB
476 else {
477 free((void *) submodule->ignore);
478 submodule->ignore = xstrdup(value);
959b5455 479 }
959b5455 480 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
481 if (!value) {
482 ret = config_error_nonbool(var);
f6adec4e
JK
483 } else if (looks_like_command_line_option(value)) {
484 warn_command_line_option(var, value);
f73da110 485 } else if (!me->overwrite && submodule->url) {
73c293bb 486 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 487 "url");
147875fd
SB
488 } else {
489 free((void *) submodule->url);
490 submodule->url = xstrdup(value);
959b5455 491 }
ea2fa5a3
SB
492 } else if (!strcmp(item.buf, "update")) {
493 if (!value)
494 ret = config_error_nonbool(var);
495 else if (!me->overwrite &&
496 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
73c293bb 497 warn_multiple_config(me->treeish_name, submodule->name,
ea2fa5a3
SB
498 "update");
499 else if (parse_submodule_update_strategy(value,
e904deb8
JN
500 &submodule->update_strategy) < 0 ||
501 submodule->update_strategy.type == SM_UPDATE_COMMAND)
1a8aea85 502 die(_("invalid value for '%s'"), var);
37f52e93
SB
503 } else if (!strcmp(item.buf, "shallow")) {
504 if (!me->overwrite && submodule->recommend_shallow != -1)
73c293bb 505 warn_multiple_config(me->treeish_name, submodule->name,
37f52e93 506 "shallow");
b5944f34 507 else
37f52e93
SB
508 submodule->recommend_shallow =
509 git_config_bool(var, value);
b5944f34
SB
510 } else if (!strcmp(item.buf, "branch")) {
511 if (!me->overwrite && submodule->branch)
73c293bb 512 warn_multiple_config(me->treeish_name, submodule->name,
b5944f34
SB
513 "branch");
514 else {
515 free((void *)submodule->branch);
516 submodule->branch = xstrdup(value);
37f52e93 517 }
959b5455
HV
518 }
519
959b5455
HV
520 strbuf_release(&name);
521 strbuf_release(&item);
522
523 return ret;
524}
525
1b796ace
BW
526static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
527 struct object_id *gitmodules_oid,
528 struct strbuf *rev)
959b5455 529{
959b5455
HV
530 int ret = 0;
531
cd73de47 532 if (is_null_oid(treeish_name)) {
533 oidclr(gitmodules_oid);
959b5455
HV
534 return 1;
535 }
536
cd73de47 537 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
538 if (get_oid(rev->buf, gitmodules_oid) >= 0)
959b5455
HV
539 ret = 1;
540
959b5455
HV
541 return ret;
542}
543
544/* This does a lookup of a submodule configuration by name or by path
545 * (key) with on-demand reading of the appropriate .gitmodules from
546 * revisions.
547 */
548static const struct submodule *config_from(struct submodule_cache *cache,
cd73de47 549 const struct object_id *treeish_name, const char *key,
959b5455
HV
550 enum lookup_type lookup_type)
551{
552 struct strbuf rev = STRBUF_INIT;
553 unsigned long config_size;
0918e250 554 char *config = NULL;
cd73de47 555 struct object_id oid;
959b5455
HV
556 enum object_type type;
557 const struct submodule *submodule = NULL;
558 struct parse_config_parameter parameter;
559
560 /*
561 * If any parameter except the cache is a NULL pointer just
562 * return the first submodule. Can be used to check whether
563 * there are any submodules parsed.
564 */
73c293bb 565 if (!treeish_name || !key) {
959b5455
HV
566 struct hashmap_iter iter;
567 struct submodule_entry *entry;
568
87571c3f
EW
569 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
570 struct submodule_entry,
571 ent /* member name */);
959b5455
HV
572 if (!entry)
573 return NULL;
574 return entry->config;
575 }
576
cd73de47 577 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
0918e250 578 goto out;
959b5455
HV
579
580 switch (lookup_type) {
581 case lookup_name:
34caab02 582 submodule = cache_lookup_name(cache, &oid, key);
959b5455
HV
583 break;
584 case lookup_path:
34caab02 585 submodule = cache_lookup_path(cache, &oid, key);
959b5455
HV
586 break;
587 }
588 if (submodule)
0918e250 589 goto out;
959b5455 590
b4f5aca4 591 config = read_object_file(&oid, &type, &config_size);
0918e250
HV
592 if (!config || type != OBJ_BLOB)
593 goto out;
959b5455
HV
594
595 /* fill the submodule config into the cache */
596 parameter.cache = cache;
34caab02 597 parameter.treeish_name = treeish_name;
598 parameter.gitmodules_oid = &oid;
959b5455 599 parameter.overwrite = 0;
1b8132d9 600 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
4574f1aa 601 config, config_size, &parameter, NULL);
514dea90 602 strbuf_release(&rev);
959b5455
HV
603 free(config);
604
605 switch (lookup_type) {
606 case lookup_name:
34caab02 607 return cache_lookup_name(cache, &oid, key);
959b5455 608 case lookup_path:
34caab02 609 return cache_lookup_path(cache, &oid, key);
959b5455
HV
610 default:
611 return NULL;
612 }
0918e250
HV
613
614out:
615 strbuf_release(&rev);
616 free(config);
617 return submodule;
959b5455
HV
618}
619
bf12fcdf 620static void submodule_cache_check_init(struct repository *repo)
959b5455 621{
bf12fcdf 622 if (repo->submodule_cache && repo->submodule_cache->initialized)
959b5455
HV
623 return;
624
bf12fcdf
BW
625 if (!repo->submodule_cache)
626 repo->submodule_cache = submodule_cache_alloc();
627
628 submodule_cache_init(repo->submodule_cache);
959b5455
HV
629}
630
db64d112
AO
631/*
632 * Note: This function is private for a reason, the '.gitmodules' file should
571fb965 633 * not be used as a mechanism to retrieve arbitrary configuration stored in
db64d112
AO
634 * the repository.
635 *
636 * Runs the provided config function on the '.gitmodules' file found in the
637 * working directory.
638 */
639static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
640{
641 if (repo->worktree) {
9a83d088
MR
642 struct git_config_source config_source = {
643 0, .scope = CONFIG_SCOPE_SUBMODULE
644 };
76e9bdc4
AO
645 const struct config_options opts = { 0 };
646 struct object_id oid;
647 char *file;
d9b8b8f8 648 char *oidstr = NULL;
76e9bdc4
AO
649
650 file = repo_worktree_path(repo, GITMODULES_FILE);
651 if (file_exists(file)) {
652 config_source.file = file;
d9b8b8f8
NTND
653 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
654 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
e3e8bf04 655 config_source.repo = repo;
d9b8b8f8
NTND
656 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
657 if (repo != the_repository)
e3e8bf04 658 add_submodule_odb_by_path(repo->objects->odb->path);
76e9bdc4
AO
659 } else {
660 goto out;
661 }
662
663 config_with_options(fn, data, &config_source, &opts);
664
665out:
d9b8b8f8 666 free(oidstr);
db64d112
AO
667 free(file);
668 }
669}
670
1b796ace 671static int gitmodules_cb(const char *var, const char *value, void *data)
851e18c3 672{
1b796ace 673 struct repository *repo = data;
851e18c3 674 struct parse_config_parameter parameter;
bf12fcdf 675
bf12fcdf 676 parameter.cache = repo->submodule_cache;
73c293bb 677 parameter.treeish_name = NULL;
14228447 678 parameter.gitmodules_oid = null_oid();
851e18c3
HV
679 parameter.overwrite = 1;
680
851e18c3
HV
681 return parse_config(var, value, &parameter);
682}
683
d7992421 684void repo_read_gitmodules(struct repository *repo, int skip_if_read)
bf12fcdf 685{
ff6f1f56
BW
686 submodule_cache_check_init(repo);
687
d7992421
MT
688 if (repo->submodule_cache->gitmodules_read && skip_if_read)
689 return;
690
db64d112
AO
691 if (repo_read_index(repo) < 0)
692 return;
1b796ace 693
db64d112
AO
694 if (!is_gitmodules_unmerged(repo->index))
695 config_from_gitmodules(gitmodules_cb, repo, repo);
ff6f1f56
BW
696
697 repo->submodule_cache->gitmodules_read = 1;
1b796ace
BW
698}
699
700void gitmodules_config_oid(const struct object_id *commit_oid)
701{
702 struct strbuf rev = STRBUF_INIT;
703 struct object_id oid;
704
ff6f1f56
BW
705 submodule_cache_check_init(the_repository);
706
1b796ace
BW
707 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
708 git_config_from_blob_oid(gitmodules_cb, rev.buf,
e3e8bf04 709 the_repository, &oid, the_repository);
1b796ace
BW
710 }
711 strbuf_release(&rev);
ff6f1f56
BW
712
713 the_repository->submodule_cache->gitmodules_read = 1;
714}
715
3b8fb393
SB
716const struct submodule *submodule_from_name(struct repository *r,
717 const struct object_id *treeish_name,
959b5455
HV
718 const char *name)
719{
d7992421 720 repo_read_gitmodules(r, 1);
3b8fb393 721 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
959b5455
HV
722}
723
3b8fb393
SB
724const struct submodule *submodule_from_path(struct repository *r,
725 const struct object_id *treeish_name,
959b5455
HV
726 const char *path)
727{
d7992421 728 repo_read_gitmodules(r, 1);
3b8fb393 729 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
bf12fcdf
BW
730}
731
961b130d
GC
732/**
733 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
734 * and appends submodule entries to 'out'. The submodule_cache expects
735 * a root-level treeish_name and paths, so keep track of these values
736 * with 'root_tree' and 'prefix'.
737 */
738static void traverse_tree_submodules(struct repository *r,
739 const struct object_id *root_tree,
740 char *prefix,
741 const struct object_id *treeish_name,
742 struct submodule_entry_list *out)
743{
744 struct tree_desc tree;
745 struct submodule_tree_entry *st_entry;
746 struct name_entry *name_entry;
747 char *tree_path = NULL;
748
749 name_entry = xmalloc(sizeof(*name_entry));
750
751 fill_tree_descriptor(r, &tree, treeish_name);
752 while (tree_entry(&tree, name_entry)) {
753 if (prefix)
754 tree_path =
755 mkpathdup("%s/%s", prefix, name_entry->path);
756 else
757 tree_path = xstrdup(name_entry->path);
758
759 if (S_ISGITLINK(name_entry->mode) &&
760 is_tree_submodule_active(r, root_tree, tree_path)) {
f5355922
JS
761 ALLOC_GROW(out->entries, out->entry_nr + 1,
762 out->entry_alloc);
763 st_entry = &out->entries[out->entry_nr++];
764
961b130d
GC
765 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
766 *st_entry->name_entry = *name_entry;
767 st_entry->submodule =
768 submodule_from_path(r, root_tree, tree_path);
769 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
770 if (repo_submodule_init(st_entry->repo, r, tree_path,
771 root_tree))
772 FREE_AND_NULL(st_entry->repo);
773
961b130d
GC
774 } else if (S_ISDIR(name_entry->mode))
775 traverse_tree_submodules(r, root_tree, tree_path,
776 &name_entry->oid, out);
777 free(tree_path);
778 }
779}
780
781void submodules_of_tree(struct repository *r,
782 const struct object_id *treeish_name,
783 struct submodule_entry_list *out)
784{
785 CALLOC_ARRAY(out->entries, 0);
786 out->entry_nr = 0;
787 out->entry_alloc = 0;
788
789 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
790}
791
f793b895 792void submodule_free(struct repository *r)
bf12fcdf 793{
f793b895
SB
794 if (r->submodule_cache)
795 submodule_cache_clear(r->submodule_cache);
959b5455 796}
ad136370 797
bcbc780d
AO
798static int config_print_callback(const char *var, const char *value, void *cb_data)
799{
800 char *wanted_key = cb_data;
801
802 if (!strcmp(wanted_key, var))
803 printf("%s\n", value);
804
805 return 0;
806}
807
808int print_config_from_gitmodules(struct repository *repo, const char *key)
809{
810 int ret;
811 char *store_key;
812
813 ret = git_config_parse_key(key, &store_key, NULL);
814 if (ret < 0)
815 return CONFIG_INVALID_KEY;
816
817 config_from_gitmodules(config_print_callback, repo, store_key);
818
819 free(store_key);
820 return 0;
821}
822
45f5ef3d
AO
823int config_set_in_gitmodules_file_gently(const char *key, const char *value)
824{
825 int ret;
826
827 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
828 if (ret < 0)
829 /* Maybe the user already did that, don't error out here */
830 warning(_("Could not update .gitmodules entry %s"), key);
831
832 return ret;
833}
834
71a6953d
AO
835struct fetch_config {
836 int *max_children;
837 int *recurse_submodules;
838};
839
840static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
841{
842 struct fetch_config *config = cb;
843 if (!strcmp(var, "submodule.fetchjobs")) {
e5b94213
JT
844 if (config->max_children)
845 *(config->max_children) =
846 parse_submodule_fetchjobs(var, value);
71a6953d
AO
847 return 0;
848 } else if (!strcmp(var, "fetch.recursesubmodules")) {
e5b94213
JT
849 if (config->recurse_submodules)
850 *(config->recurse_submodules) =
851 parse_fetch_recurse_submodules_arg(var, value);
71a6953d
AO
852 return 0;
853 }
854
855 return 0;
856}
857
858void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
859{
860 struct fetch_config config = {
861 .max_children = max_children,
862 .recurse_submodules = recurse_submodules
863 };
9a0fb3e7 864 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
71a6953d 865}
05744997
AO
866
867static int gitmodules_update_clone_config(const char *var, const char *value,
868 void *cb)
869{
870 int *max_jobs = cb;
871 if (!strcmp(var, "submodule.fetchjobs"))
872 *max_jobs = parse_submodule_fetchjobs(var, value);
873 return 0;
874}
875
876void update_clone_config_from_gitmodules(int *max_jobs)
877{
9a0fb3e7 878 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
05744997 879}