2 #include "repository.h"
4 #include "submodule-config.h"
7 #include "parse-options.h"
10 * submodule cache lookup structure
11 * There is one shared set of 'struct submodule' entries which can be
12 * looked up by their sha1 blob id of the .gitmodules file and either
13 * using path or name as key.
14 * for_path stores submodule entries with path as key
15 * for_name stores submodule entries with name as key
17 struct submodule_cache
{
18 struct hashmap for_path
;
19 struct hashmap for_name
;
20 unsigned initialized
:1;
21 unsigned gitmodules_read
:1;
25 * thin wrapper struct needed to insert 'struct submodule' entries to
28 struct submodule_entry
{
29 struct hashmap_entry ent
;
30 struct submodule
*config
;
38 static int config_path_cmp(const void *unused_cmp_data
,
40 const void *entry_or_key
,
41 const void *unused_keydata
)
43 const struct submodule_entry
*a
= entry
;
44 const struct submodule_entry
*b
= entry_or_key
;
46 return strcmp(a
->config
->path
, b
->config
->path
) ||
47 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
50 static int config_name_cmp(const void *unused_cmp_data
,
52 const void *entry_or_key
,
53 const void *unused_keydata
)
55 const struct submodule_entry
*a
= entry
;
56 const struct submodule_entry
*b
= entry_or_key
;
58 return strcmp(a
->config
->name
, b
->config
->name
) ||
59 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
62 static struct submodule_cache
*submodule_cache_alloc(void)
64 return xcalloc(1, sizeof(struct submodule_cache
));
67 static void submodule_cache_init(struct submodule_cache
*cache
)
69 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
70 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
71 cache
->initialized
= 1;
74 static void free_one_config(struct submodule_entry
*entry
)
76 free((void *) entry
->config
->path
);
77 free((void *) entry
->config
->name
);
78 free((void *) entry
->config
->branch
);
79 free((void *) entry
->config
->update_strategy
.command
);
83 static void submodule_cache_clear(struct submodule_cache
*cache
)
85 struct hashmap_iter iter
;
86 struct submodule_entry
*entry
;
88 if (!cache
->initialized
)
92 * We iterate over the name hash here to be symmetric with the
93 * allocation of struct submodule entries. Each is allocated by
94 * their .gitmodules blob sha1 and submodule name.
96 hashmap_iter_init(&cache
->for_name
, &iter
);
97 while ((entry
= hashmap_iter_next(&iter
)))
98 free_one_config(entry
);
100 hashmap_free(&cache
->for_path
, 1);
101 hashmap_free(&cache
->for_name
, 1);
102 cache
->initialized
= 0;
103 cache
->gitmodules_read
= 0;
106 void submodule_cache_free(struct submodule_cache
*cache
)
108 submodule_cache_clear(cache
);
112 static unsigned int hash_sha1_string(const unsigned char *sha1
,
115 return memhash(sha1
, 20) + strhash(string
);
118 static void cache_put_path(struct submodule_cache
*cache
,
119 struct submodule
*submodule
)
121 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
123 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
124 hashmap_entry_init(e
, hash
);
125 e
->config
= submodule
;
126 hashmap_put(&cache
->for_path
, e
);
129 static void cache_remove_path(struct submodule_cache
*cache
,
130 struct submodule
*submodule
)
132 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
134 struct submodule_entry e
;
135 struct submodule_entry
*removed
;
136 hashmap_entry_init(&e
, hash
);
137 e
.config
= submodule
;
138 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
142 static void cache_add(struct submodule_cache
*cache
,
143 struct submodule
*submodule
)
145 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
147 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
148 hashmap_entry_init(e
, hash
);
149 e
->config
= submodule
;
150 hashmap_add(&cache
->for_name
, e
);
153 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
154 const unsigned char *gitmodules_sha1
, const char *path
)
156 struct submodule_entry
*entry
;
157 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, path
);
158 struct submodule_entry key
;
159 struct submodule key_config
;
161 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
162 key_config
.path
= path
;
164 hashmap_entry_init(&key
, hash
);
165 key
.config
= &key_config
;
167 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
169 return entry
->config
;
173 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
174 const unsigned char *gitmodules_sha1
, const char *name
)
176 struct submodule_entry
*entry
;
177 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, name
);
178 struct submodule_entry key
;
179 struct submodule key_config
;
181 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
182 key_config
.name
= name
;
184 hashmap_entry_init(&key
, hash
);
185 key
.config
= &key_config
;
187 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
189 return entry
->config
;
193 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
196 const char *subsection
, *key
;
197 int subsection_len
, parse
;
198 parse
= parse_config_key(var
, "submodule", &subsection
,
199 &subsection_len
, &key
);
200 if (parse
< 0 || !subsection
)
203 strbuf_add(name
, subsection
, subsection_len
);
204 strbuf_addstr(item
, key
);
209 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
210 const unsigned char *gitmodules_sha1
, const char *name
)
212 struct submodule
*submodule
;
213 struct strbuf name_buf
= STRBUF_INIT
;
215 submodule
= cache_lookup_name(cache
, gitmodules_sha1
, name
);
219 submodule
= xmalloc(sizeof(*submodule
));
221 strbuf_addstr(&name_buf
, name
);
222 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
224 submodule
->path
= NULL
;
225 submodule
->url
= NULL
;
226 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
227 submodule
->update_strategy
.command
= NULL
;
228 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
229 submodule
->ignore
= NULL
;
230 submodule
->branch
= NULL
;
231 submodule
->recommend_shallow
= -1;
233 hashcpy(submodule
->gitmodules_sha1
, gitmodules_sha1
);
235 cache_add(cache
, submodule
);
240 static int parse_fetch_recurse(const char *opt
, const char *arg
,
243 switch (git_parse_maybe_bool(arg
)) {
245 return RECURSE_SUBMODULES_ON
;
247 return RECURSE_SUBMODULES_OFF
;
249 if (!strcmp(arg
, "on-demand"))
250 return RECURSE_SUBMODULES_ON_DEMAND
;
253 die("bad %s argument: %s", opt
, arg
);
255 return RECURSE_SUBMODULES_ERROR
;
259 int parse_submodule_fetchjobs(const char *var
, const char *value
)
261 int fetchjobs
= git_config_int(var
, value
);
263 die(_("negative values not allowed for submodule.fetchjobs"));
267 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
269 return parse_fetch_recurse(opt
, arg
, 1);
272 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
273 const char *arg
, int unset
)
283 *v
= RECURSE_SUBMODULES_OFF
;
286 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
288 *v
= RECURSE_SUBMODULES_ON
;
293 static int parse_update_recurse(const char *opt
, const char *arg
,
296 switch (git_parse_maybe_bool(arg
)) {
298 return RECURSE_SUBMODULES_ON
;
300 return RECURSE_SUBMODULES_OFF
;
303 die("bad %s argument: %s", opt
, arg
);
304 return RECURSE_SUBMODULES_ERROR
;
308 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
310 return parse_update_recurse(opt
, arg
, 1);
313 static int parse_push_recurse(const char *opt
, const char *arg
,
316 switch (git_parse_maybe_bool(arg
)) {
318 /* There's no simple "on" value when pushing */
320 die("bad %s argument: %s", opt
, arg
);
322 return RECURSE_SUBMODULES_ERROR
;
324 return RECURSE_SUBMODULES_OFF
;
326 if (!strcmp(arg
, "on-demand"))
327 return RECURSE_SUBMODULES_ON_DEMAND
;
328 else if (!strcmp(arg
, "check"))
329 return RECURSE_SUBMODULES_CHECK
;
330 else if (!strcmp(arg
, "only"))
331 return RECURSE_SUBMODULES_ONLY
;
332 else if (die_on_error
)
333 die("bad %s argument: %s", opt
, arg
);
335 return RECURSE_SUBMODULES_ERROR
;
339 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
341 return parse_push_recurse(opt
, arg
, 1);
344 static void warn_multiple_config(const unsigned char *treeish_name
,
345 const char *name
, const char *option
)
347 const char *commit_string
= "WORKTREE";
349 commit_string
= sha1_to_hex(treeish_name
);
350 warning("%s:.gitmodules, multiple configurations found for "
351 "'submodule.%s.%s'. Skipping second one!",
352 commit_string
, name
, option
);
355 struct parse_config_parameter
{
356 struct submodule_cache
*cache
;
357 const unsigned char *treeish_name
;
358 const unsigned char *gitmodules_sha1
;
362 static int parse_config(const char *var
, const char *value
, void *data
)
364 struct parse_config_parameter
*me
= data
;
365 struct submodule
*submodule
;
366 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
369 /* this also ensures that we only parse submodule entries */
370 if (!name_and_item_from_var(var
, &name
, &item
))
373 submodule
= lookup_or_create_by_name(me
->cache
,
377 if (!strcmp(item
.buf
, "path")) {
379 ret
= config_error_nonbool(var
);
380 else if (!me
->overwrite
&& submodule
->path
)
381 warn_multiple_config(me
->treeish_name
, submodule
->name
,
385 cache_remove_path(me
->cache
, submodule
);
386 free((void *) submodule
->path
);
387 submodule
->path
= xstrdup(value
);
388 cache_put_path(me
->cache
, submodule
);
390 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
391 /* when parsing worktree configurations we can die early */
392 int die_on_error
= is_null_sha1(me
->gitmodules_sha1
);
393 if (!me
->overwrite
&&
394 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
395 warn_multiple_config(me
->treeish_name
, submodule
->name
,
396 "fetchrecursesubmodules");
398 submodule
->fetch_recurse
= parse_fetch_recurse(
401 } else if (!strcmp(item
.buf
, "ignore")) {
403 ret
= config_error_nonbool(var
);
404 else if (!me
->overwrite
&& submodule
->ignore
)
405 warn_multiple_config(me
->treeish_name
, submodule
->name
,
407 else if (strcmp(value
, "untracked") &&
408 strcmp(value
, "dirty") &&
409 strcmp(value
, "all") &&
410 strcmp(value
, "none"))
411 warning("Invalid parameter '%s' for config option "
412 "'submodule.%s.ignore'", value
, name
.buf
);
414 free((void *) submodule
->ignore
);
415 submodule
->ignore
= xstrdup(value
);
417 } else if (!strcmp(item
.buf
, "url")) {
419 ret
= config_error_nonbool(var
);
420 } else if (!me
->overwrite
&& submodule
->url
) {
421 warn_multiple_config(me
->treeish_name
, submodule
->name
,
424 free((void *) submodule
->url
);
425 submodule
->url
= xstrdup(value
);
427 } else if (!strcmp(item
.buf
, "update")) {
429 ret
= config_error_nonbool(var
);
430 else if (!me
->overwrite
&&
431 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
432 warn_multiple_config(me
->treeish_name
, submodule
->name
,
434 else if (parse_submodule_update_strategy(value
,
435 &submodule
->update_strategy
) < 0)
436 die(_("invalid value for %s"), var
);
437 } else if (!strcmp(item
.buf
, "shallow")) {
438 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
439 warn_multiple_config(me
->treeish_name
, submodule
->name
,
442 submodule
->recommend_shallow
=
443 git_config_bool(var
, value
);
444 } else if (!strcmp(item
.buf
, "branch")) {
445 if (!me
->overwrite
&& submodule
->branch
)
446 warn_multiple_config(me
->treeish_name
, submodule
->name
,
449 free((void *)submodule
->branch
);
450 submodule
->branch
= xstrdup(value
);
454 strbuf_release(&name
);
455 strbuf_release(&item
);
460 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
461 struct object_id
*gitmodules_oid
,
466 if (is_null_oid(treeish_name
)) {
467 oidclr(gitmodules_oid
);
471 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
472 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
478 /* This does a lookup of a submodule configuration by name or by path
479 * (key) with on-demand reading of the appropriate .gitmodules from
482 static const struct submodule
*config_from(struct submodule_cache
*cache
,
483 const struct object_id
*treeish_name
, const char *key
,
484 enum lookup_type lookup_type
)
486 struct strbuf rev
= STRBUF_INIT
;
487 unsigned long config_size
;
489 struct object_id oid
;
490 enum object_type type
;
491 const struct submodule
*submodule
= NULL
;
492 struct parse_config_parameter parameter
;
495 * If any parameter except the cache is a NULL pointer just
496 * return the first submodule. Can be used to check whether
497 * there are any submodules parsed.
499 if (!treeish_name
|| !key
) {
500 struct hashmap_iter iter
;
501 struct submodule_entry
*entry
;
503 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
506 return entry
->config
;
509 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
512 switch (lookup_type
) {
514 submodule
= cache_lookup_name(cache
, oid
.hash
, key
);
517 submodule
= cache_lookup_path(cache
, oid
.hash
, key
);
523 config
= read_object_file(&oid
, &type
, &config_size
);
524 if (!config
|| type
!= OBJ_BLOB
)
527 /* fill the submodule config into the cache */
528 parameter
.cache
= cache
;
529 parameter
.treeish_name
= treeish_name
->hash
;
530 parameter
.gitmodules_sha1
= oid
.hash
;
531 parameter
.overwrite
= 0;
532 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
533 config
, config_size
, ¶meter
);
534 strbuf_release(&rev
);
537 switch (lookup_type
) {
539 return cache_lookup_name(cache
, oid
.hash
, key
);
541 return cache_lookup_path(cache
, oid
.hash
, key
);
547 strbuf_release(&rev
);
552 static void submodule_cache_check_init(struct repository
*repo
)
554 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
557 if (!repo
->submodule_cache
)
558 repo
->submodule_cache
= submodule_cache_alloc();
560 submodule_cache_init(repo
->submodule_cache
);
563 static int gitmodules_cb(const char *var
, const char *value
, void *data
)
565 struct repository
*repo
= data
;
566 struct parse_config_parameter parameter
;
568 parameter
.cache
= repo
->submodule_cache
;
569 parameter
.treeish_name
= NULL
;
570 parameter
.gitmodules_sha1
= null_sha1
;
571 parameter
.overwrite
= 1;
573 return parse_config(var
, value
, ¶meter
);
576 void repo_read_gitmodules(struct repository
*repo
)
578 submodule_cache_check_init(repo
);
580 if (repo
->worktree
) {
583 if (repo_read_index(repo
) < 0)
586 gitmodules
= repo_worktree_path(repo
, GITMODULES_FILE
);
588 if (!is_gitmodules_unmerged(repo
->index
))
589 git_config_from_file(gitmodules_cb
, gitmodules
, repo
);
594 repo
->submodule_cache
->gitmodules_read
= 1;
597 void gitmodules_config_oid(const struct object_id
*commit_oid
)
599 struct strbuf rev
= STRBUF_INIT
;
600 struct object_id oid
;
602 submodule_cache_check_init(the_repository
);
604 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
605 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
606 &oid
, the_repository
);
608 strbuf_release(&rev
);
610 the_repository
->submodule_cache
->gitmodules_read
= 1;
613 static void gitmodules_read_check(struct repository
*repo
)
615 submodule_cache_check_init(repo
);
617 /* read the repo's .gitmodules file if it hasn't been already */
618 if (!repo
->submodule_cache
->gitmodules_read
)
619 repo_read_gitmodules(repo
);
622 const struct submodule
*submodule_from_name(struct repository
*r
,
623 const struct object_id
*treeish_name
,
626 gitmodules_read_check(r
);
627 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
630 const struct submodule
*submodule_from_path(struct repository
*r
,
631 const struct object_id
*treeish_name
,
634 gitmodules_read_check(r
);
635 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
638 void submodule_free(struct repository
*r
)
640 if (r
->submodule_cache
)
641 submodule_cache_clear(r
->submodule_cache
);