2 #include "repository.h"
4 #include "submodule-config.h"
7 #include "object-store.h"
8 #include "parse-options.h"
11 * submodule cache lookup structure
12 * There is one shared set of 'struct submodule' entries which can be
13 * looked up by their sha1 blob id of the .gitmodules file and either
14 * using path or name as key.
15 * for_path stores submodule entries with path as key
16 * for_name stores submodule entries with name as key
18 struct submodule_cache
{
19 struct hashmap for_path
;
20 struct hashmap for_name
;
21 unsigned initialized
:1;
22 unsigned gitmodules_read
:1;
26 * thin wrapper struct needed to insert 'struct submodule' entries to
29 struct submodule_entry
{
30 struct hashmap_entry ent
;
31 struct submodule
*config
;
39 static int config_path_cmp(const void *unused_cmp_data
,
41 const void *entry_or_key
,
42 const void *unused_keydata
)
44 const struct submodule_entry
*a
= entry
;
45 const struct submodule_entry
*b
= entry_or_key
;
47 return strcmp(a
->config
->path
, b
->config
->path
) ||
48 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
51 static int config_name_cmp(const void *unused_cmp_data
,
53 const void *entry_or_key
,
54 const void *unused_keydata
)
56 const struct submodule_entry
*a
= entry
;
57 const struct submodule_entry
*b
= entry_or_key
;
59 return strcmp(a
->config
->name
, b
->config
->name
) ||
60 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
63 static struct submodule_cache
*submodule_cache_alloc(void)
65 return xcalloc(1, sizeof(struct submodule_cache
));
68 static void submodule_cache_init(struct submodule_cache
*cache
)
70 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
71 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
72 cache
->initialized
= 1;
75 static void free_one_config(struct submodule_entry
*entry
)
77 free((void *) entry
->config
->path
);
78 free((void *) entry
->config
->name
);
79 free((void *) entry
->config
->branch
);
80 free((void *) entry
->config
->update_strategy
.command
);
84 static void submodule_cache_clear(struct submodule_cache
*cache
)
86 struct hashmap_iter iter
;
87 struct submodule_entry
*entry
;
89 if (!cache
->initialized
)
93 * We iterate over the name hash here to be symmetric with the
94 * allocation of struct submodule entries. Each is allocated by
95 * their .gitmodules blob sha1 and submodule name.
97 hashmap_iter_init(&cache
->for_name
, &iter
);
98 while ((entry
= hashmap_iter_next(&iter
)))
99 free_one_config(entry
);
101 hashmap_free(&cache
->for_path
, 1);
102 hashmap_free(&cache
->for_name
, 1);
103 cache
->initialized
= 0;
104 cache
->gitmodules_read
= 0;
107 void submodule_cache_free(struct submodule_cache
*cache
)
109 submodule_cache_clear(cache
);
113 static unsigned int hash_oid_string(const struct object_id
*oid
,
116 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
119 static void cache_put_path(struct submodule_cache
*cache
,
120 struct submodule
*submodule
)
122 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
124 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
125 hashmap_entry_init(e
, hash
);
126 e
->config
= submodule
;
127 hashmap_put(&cache
->for_path
, e
);
130 static void cache_remove_path(struct submodule_cache
*cache
,
131 struct submodule
*submodule
)
133 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
135 struct submodule_entry e
;
136 struct submodule_entry
*removed
;
137 hashmap_entry_init(&e
, hash
);
138 e
.config
= submodule
;
139 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
143 static void cache_add(struct submodule_cache
*cache
,
144 struct submodule
*submodule
)
146 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
148 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
149 hashmap_entry_init(e
, hash
);
150 e
->config
= submodule
;
151 hashmap_add(&cache
->for_name
, e
);
154 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
155 const struct object_id
*gitmodules_oid
, const char *path
)
157 struct submodule_entry
*entry
;
158 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
159 struct submodule_entry key
;
160 struct submodule key_config
;
162 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
163 key_config
.path
= path
;
165 hashmap_entry_init(&key
, hash
);
166 key
.config
= &key_config
;
168 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
170 return entry
->config
;
174 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
175 const struct object_id
*gitmodules_oid
, const char *name
)
177 struct submodule_entry
*entry
;
178 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
179 struct submodule_entry key
;
180 struct submodule key_config
;
182 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
183 key_config
.name
= name
;
185 hashmap_entry_init(&key
, hash
);
186 key
.config
= &key_config
;
188 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
190 return entry
->config
;
194 int check_submodule_name(const char *name
)
196 /* Disallow empty names */
201 * Look for '..' as a path component. Check both '/' and '\\' as
202 * separators rather than is_dir_sep(), because we want the name rules
203 * to be consistent across platforms.
205 goto in_component
; /* always start inside component */
208 if (c
== '/' || c
== '\\') {
210 if (name
[0] == '.' && name
[1] == '.' &&
211 (!name
[2] || name
[2] == '/' || name
[2] == '\\'))
219 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
222 const char *subsection
, *key
;
223 int subsection_len
, parse
;
224 parse
= parse_config_key(var
, "submodule", &subsection
,
225 &subsection_len
, &key
);
226 if (parse
< 0 || !subsection
)
229 strbuf_add(name
, subsection
, subsection_len
);
230 if (check_submodule_name(name
->buf
) < 0) {
231 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
232 strbuf_release(name
);
236 strbuf_addstr(item
, key
);
241 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
242 const struct object_id
*gitmodules_oid
, const char *name
)
244 struct submodule
*submodule
;
245 struct strbuf name_buf
= STRBUF_INIT
;
247 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
251 submodule
= xmalloc(sizeof(*submodule
));
253 strbuf_addstr(&name_buf
, name
);
254 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
256 submodule
->path
= NULL
;
257 submodule
->url
= NULL
;
258 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
259 submodule
->update_strategy
.command
= NULL
;
260 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
261 submodule
->ignore
= NULL
;
262 submodule
->branch
= NULL
;
263 submodule
->recommend_shallow
= -1;
265 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
267 cache_add(cache
, submodule
);
272 static int parse_fetch_recurse(const char *opt
, const char *arg
,
275 switch (git_parse_maybe_bool(arg
)) {
277 return RECURSE_SUBMODULES_ON
;
279 return RECURSE_SUBMODULES_OFF
;
281 if (!strcmp(arg
, "on-demand"))
282 return RECURSE_SUBMODULES_ON_DEMAND
;
285 die("bad %s argument: %s", opt
, arg
);
287 return RECURSE_SUBMODULES_ERROR
;
291 int parse_submodule_fetchjobs(const char *var
, const char *value
)
293 int fetchjobs
= git_config_int(var
, value
);
295 die(_("negative values not allowed for submodule.fetchjobs"));
299 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
301 return parse_fetch_recurse(opt
, arg
, 1);
304 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
305 const char *arg
, int unset
)
315 *v
= RECURSE_SUBMODULES_OFF
;
318 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
320 *v
= RECURSE_SUBMODULES_ON
;
325 static int parse_update_recurse(const char *opt
, const char *arg
,
328 switch (git_parse_maybe_bool(arg
)) {
330 return RECURSE_SUBMODULES_ON
;
332 return RECURSE_SUBMODULES_OFF
;
335 die("bad %s argument: %s", opt
, arg
);
336 return RECURSE_SUBMODULES_ERROR
;
340 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
342 return parse_update_recurse(opt
, arg
, 1);
345 static int parse_push_recurse(const char *opt
, const char *arg
,
348 switch (git_parse_maybe_bool(arg
)) {
350 /* There's no simple "on" value when pushing */
352 die("bad %s argument: %s", opt
, arg
);
354 return RECURSE_SUBMODULES_ERROR
;
356 return RECURSE_SUBMODULES_OFF
;
358 if (!strcmp(arg
, "on-demand"))
359 return RECURSE_SUBMODULES_ON_DEMAND
;
360 else if (!strcmp(arg
, "check"))
361 return RECURSE_SUBMODULES_CHECK
;
362 else if (!strcmp(arg
, "only"))
363 return RECURSE_SUBMODULES_ONLY
;
364 else if (die_on_error
)
365 die("bad %s argument: %s", opt
, arg
);
367 return RECURSE_SUBMODULES_ERROR
;
371 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
373 return parse_push_recurse(opt
, arg
, 1);
376 static void warn_multiple_config(const struct object_id
*treeish_name
,
377 const char *name
, const char *option
)
379 const char *commit_string
= "WORKTREE";
381 commit_string
= oid_to_hex(treeish_name
);
382 warning("%s:.gitmodules, multiple configurations found for "
383 "'submodule.%s.%s'. Skipping second one!",
384 commit_string
, name
, option
);
387 struct parse_config_parameter
{
388 struct submodule_cache
*cache
;
389 const struct object_id
*treeish_name
;
390 const struct object_id
*gitmodules_oid
;
394 static int parse_config(const char *var
, const char *value
, void *data
)
396 struct parse_config_parameter
*me
= data
;
397 struct submodule
*submodule
;
398 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
401 /* this also ensures that we only parse submodule entries */
402 if (!name_and_item_from_var(var
, &name
, &item
))
405 submodule
= lookup_or_create_by_name(me
->cache
,
409 if (!strcmp(item
.buf
, "path")) {
411 ret
= config_error_nonbool(var
);
412 else if (!me
->overwrite
&& submodule
->path
)
413 warn_multiple_config(me
->treeish_name
, submodule
->name
,
417 cache_remove_path(me
->cache
, submodule
);
418 free((void *) submodule
->path
);
419 submodule
->path
= xstrdup(value
);
420 cache_put_path(me
->cache
, submodule
);
422 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
423 /* when parsing worktree configurations we can die early */
424 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
425 if (!me
->overwrite
&&
426 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
427 warn_multiple_config(me
->treeish_name
, submodule
->name
,
428 "fetchrecursesubmodules");
430 submodule
->fetch_recurse
= parse_fetch_recurse(
433 } else if (!strcmp(item
.buf
, "ignore")) {
435 ret
= config_error_nonbool(var
);
436 else if (!me
->overwrite
&& submodule
->ignore
)
437 warn_multiple_config(me
->treeish_name
, submodule
->name
,
439 else if (strcmp(value
, "untracked") &&
440 strcmp(value
, "dirty") &&
441 strcmp(value
, "all") &&
442 strcmp(value
, "none"))
443 warning("Invalid parameter '%s' for config option "
444 "'submodule.%s.ignore'", value
, name
.buf
);
446 free((void *) submodule
->ignore
);
447 submodule
->ignore
= xstrdup(value
);
449 } else if (!strcmp(item
.buf
, "url")) {
451 ret
= config_error_nonbool(var
);
452 } else if (!me
->overwrite
&& submodule
->url
) {
453 warn_multiple_config(me
->treeish_name
, submodule
->name
,
456 free((void *) submodule
->url
);
457 submodule
->url
= xstrdup(value
);
459 } else if (!strcmp(item
.buf
, "update")) {
461 ret
= config_error_nonbool(var
);
462 else if (!me
->overwrite
&&
463 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
464 warn_multiple_config(me
->treeish_name
, submodule
->name
,
466 else if (parse_submodule_update_strategy(value
,
467 &submodule
->update_strategy
) < 0)
468 die(_("invalid value for %s"), var
);
469 } else if (!strcmp(item
.buf
, "shallow")) {
470 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
471 warn_multiple_config(me
->treeish_name
, submodule
->name
,
474 submodule
->recommend_shallow
=
475 git_config_bool(var
, value
);
476 } else if (!strcmp(item
.buf
, "branch")) {
477 if (!me
->overwrite
&& submodule
->branch
)
478 warn_multiple_config(me
->treeish_name
, submodule
->name
,
481 free((void *)submodule
->branch
);
482 submodule
->branch
= xstrdup(value
);
486 strbuf_release(&name
);
487 strbuf_release(&item
);
492 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
493 struct object_id
*gitmodules_oid
,
498 if (is_null_oid(treeish_name
)) {
499 oidclr(gitmodules_oid
);
503 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
504 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
510 /* This does a lookup of a submodule configuration by name or by path
511 * (key) with on-demand reading of the appropriate .gitmodules from
514 static const struct submodule
*config_from(struct submodule_cache
*cache
,
515 const struct object_id
*treeish_name
, const char *key
,
516 enum lookup_type lookup_type
)
518 struct strbuf rev
= STRBUF_INIT
;
519 unsigned long config_size
;
521 struct object_id oid
;
522 enum object_type type
;
523 const struct submodule
*submodule
= NULL
;
524 struct parse_config_parameter parameter
;
527 * If any parameter except the cache is a NULL pointer just
528 * return the first submodule. Can be used to check whether
529 * there are any submodules parsed.
531 if (!treeish_name
|| !key
) {
532 struct hashmap_iter iter
;
533 struct submodule_entry
*entry
;
535 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
538 return entry
->config
;
541 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
544 switch (lookup_type
) {
546 submodule
= cache_lookup_name(cache
, &oid
, key
);
549 submodule
= cache_lookup_path(cache
, &oid
, key
);
555 config
= read_object_file(&oid
, &type
, &config_size
);
556 if (!config
|| type
!= OBJ_BLOB
)
559 /* fill the submodule config into the cache */
560 parameter
.cache
= cache
;
561 parameter
.treeish_name
= treeish_name
;
562 parameter
.gitmodules_oid
= &oid
;
563 parameter
.overwrite
= 0;
564 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
565 config
, config_size
, ¶meter
, NULL
);
566 strbuf_release(&rev
);
569 switch (lookup_type
) {
571 return cache_lookup_name(cache
, &oid
, key
);
573 return cache_lookup_path(cache
, &oid
, key
);
579 strbuf_release(&rev
);
584 static void submodule_cache_check_init(struct repository
*repo
)
586 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
589 if (!repo
->submodule_cache
)
590 repo
->submodule_cache
= submodule_cache_alloc();
592 submodule_cache_init(repo
->submodule_cache
);
596 * Note: This function is private for a reason, the '.gitmodules' file should
597 * not be used as as a mechanism to retrieve arbitrary configuration stored in
600 * Runs the provided config function on the '.gitmodules' file found in the
603 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
605 if (repo
->worktree
) {
606 char *file
= repo_worktree_path(repo
, GITMODULES_FILE
);
607 git_config_from_file(fn
, file
, data
);
612 static int gitmodules_cb(const char *var
, const char *value
, void *data
)
614 struct repository
*repo
= data
;
615 struct parse_config_parameter parameter
;
617 parameter
.cache
= repo
->submodule_cache
;
618 parameter
.treeish_name
= NULL
;
619 parameter
.gitmodules_oid
= &null_oid
;
620 parameter
.overwrite
= 1;
622 return parse_config(var
, value
, ¶meter
);
625 void repo_read_gitmodules(struct repository
*repo
)
627 submodule_cache_check_init(repo
);
629 if (repo_read_index(repo
) < 0)
632 if (!is_gitmodules_unmerged(repo
->index
))
633 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
635 repo
->submodule_cache
->gitmodules_read
= 1;
638 void gitmodules_config_oid(const struct object_id
*commit_oid
)
640 struct strbuf rev
= STRBUF_INIT
;
641 struct object_id oid
;
643 submodule_cache_check_init(the_repository
);
645 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
646 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
647 &oid
, the_repository
);
649 strbuf_release(&rev
);
651 the_repository
->submodule_cache
->gitmodules_read
= 1;
654 static void gitmodules_read_check(struct repository
*repo
)
656 submodule_cache_check_init(repo
);
658 /* read the repo's .gitmodules file if it hasn't been already */
659 if (!repo
->submodule_cache
->gitmodules_read
)
660 repo_read_gitmodules(repo
);
663 const struct submodule
*submodule_from_name(struct repository
*r
,
664 const struct object_id
*treeish_name
,
667 gitmodules_read_check(r
);
668 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
671 const struct submodule
*submodule_from_path(struct repository
*r
,
672 const struct object_id
*treeish_name
,
675 gitmodules_read_check(r
);
676 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
679 void submodule_free(struct repository
*r
)
681 if (r
->submodule_cache
)
682 submodule_cache_clear(r
->submodule_cache
);
685 struct fetch_config
{
687 int *recurse_submodules
;
690 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
692 struct fetch_config
*config
= cb
;
693 if (!strcmp(var
, "submodule.fetchjobs")) {
694 *(config
->max_children
) = parse_submodule_fetchjobs(var
, value
);
696 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
697 *(config
->recurse_submodules
) = parse_fetch_recurse_submodules_arg(var
, value
);
704 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
706 struct fetch_config config
= {
707 .max_children
= max_children
,
708 .recurse_submodules
= recurse_submodules
710 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
713 static int gitmodules_update_clone_config(const char *var
, const char *value
,
717 if (!strcmp(var
, "submodule.fetchjobs"))
718 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
722 void update_clone_config_from_gitmodules(int *max_jobs
)
724 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);