3 #include "repository.h"
5 #include "submodule-config.h"
8 #include "object-store.h"
9 #include "parse-options.h"
12 * submodule cache lookup structure
13 * There is one shared set of 'struct submodule' entries which can be
14 * looked up by their sha1 blob id of the .gitmodules file and either
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
19 struct submodule_cache
{
20 struct hashmap for_path
;
21 struct hashmap for_name
;
22 unsigned initialized
:1;
23 unsigned gitmodules_read
:1;
27 * thin wrapper struct needed to insert 'struct submodule' entries to
30 struct submodule_entry
{
31 struct hashmap_entry ent
;
32 struct submodule
*config
;
40 static int config_path_cmp(const void *unused_cmp_data
,
42 const void *entry_or_key
,
43 const void *unused_keydata
)
45 const struct submodule_entry
*a
= entry
;
46 const struct submodule_entry
*b
= entry_or_key
;
48 return strcmp(a
->config
->path
, b
->config
->path
) ||
49 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
52 static int config_name_cmp(const void *unused_cmp_data
,
54 const void *entry_or_key
,
55 const void *unused_keydata
)
57 const struct submodule_entry
*a
= entry
;
58 const struct submodule_entry
*b
= entry_or_key
;
60 return strcmp(a
->config
->name
, b
->config
->name
) ||
61 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
64 static struct submodule_cache
*submodule_cache_alloc(void)
66 return xcalloc(1, sizeof(struct submodule_cache
));
69 static void submodule_cache_init(struct submodule_cache
*cache
)
71 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
72 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
73 cache
->initialized
= 1;
76 static void free_one_config(struct submodule_entry
*entry
)
78 free((void *) entry
->config
->path
);
79 free((void *) entry
->config
->name
);
80 free((void *) entry
->config
->branch
);
81 free((void *) entry
->config
->update_strategy
.command
);
85 static void submodule_cache_clear(struct submodule_cache
*cache
)
87 struct hashmap_iter iter
;
88 struct submodule_entry
*entry
;
90 if (!cache
->initialized
)
94 * We iterate over the name hash here to be symmetric with the
95 * allocation of struct submodule entries. Each is allocated by
96 * their .gitmodules blob sha1 and submodule name.
98 hashmap_iter_init(&cache
->for_name
, &iter
);
99 while ((entry
= hashmap_iter_next(&iter
)))
100 free_one_config(entry
);
102 hashmap_free(&cache
->for_path
, 1);
103 hashmap_free(&cache
->for_name
, 1);
104 cache
->initialized
= 0;
105 cache
->gitmodules_read
= 0;
108 void submodule_cache_free(struct submodule_cache
*cache
)
110 submodule_cache_clear(cache
);
114 static unsigned int hash_oid_string(const struct object_id
*oid
,
117 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
120 static void cache_put_path(struct submodule_cache
*cache
,
121 struct submodule
*submodule
)
123 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
125 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
126 hashmap_entry_init(&e
->ent
, hash
);
127 e
->config
= submodule
;
128 hashmap_put(&cache
->for_path
, &e
->ent
);
131 static void cache_remove_path(struct submodule_cache
*cache
,
132 struct submodule
*submodule
)
134 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
136 struct submodule_entry e
;
137 struct submodule_entry
*removed
;
138 hashmap_entry_init(&e
.ent
, hash
);
139 e
.config
= submodule
;
140 removed
= hashmap_remove(&cache
->for_path
, &e
.ent
, NULL
);
144 static void cache_add(struct submodule_cache
*cache
,
145 struct submodule
*submodule
)
147 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
149 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
150 hashmap_entry_init(&e
->ent
, hash
);
151 e
->config
= submodule
;
152 hashmap_add(&cache
->for_name
, &e
->ent
);
155 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
156 const struct object_id
*gitmodules_oid
, const char *path
)
158 struct submodule_entry
*entry
;
159 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
160 struct submodule_entry key
;
161 struct submodule key_config
;
163 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
164 key_config
.path
= path
;
166 hashmap_entry_init(&key
.ent
, hash
);
167 key
.config
= &key_config
;
169 entry
= hashmap_get_entry(&cache
->for_path
, &key
, NULL
,
170 struct submodule_entry
, ent
);
172 return entry
->config
;
176 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
177 const struct object_id
*gitmodules_oid
, const char *name
)
179 struct submodule_entry
*entry
;
180 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
181 struct submodule_entry key
;
182 struct submodule key_config
;
184 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
185 key_config
.name
= name
;
187 hashmap_entry_init(&key
.ent
, hash
);
188 key
.config
= &key_config
;
190 entry
= hashmap_get_entry(&cache
->for_name
, &key
, NULL
,
191 struct submodule_entry
, ent
);
193 return entry
->config
;
197 int check_submodule_name(const char *name
)
199 /* Disallow empty names */
204 * Look for '..' as a path component. Check both '/' and '\\' as
205 * separators rather than is_dir_sep(), because we want the name rules
206 * to be consistent across platforms.
208 goto in_component
; /* always start inside component */
211 if (c
== '/' || c
== '\\') {
213 if (name
[0] == '.' && name
[1] == '.' &&
214 (!name
[2] || name
[2] == '/' || name
[2] == '\\'))
222 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
225 const char *subsection
, *key
;
226 int subsection_len
, parse
;
227 parse
= parse_config_key(var
, "submodule", &subsection
,
228 &subsection_len
, &key
);
229 if (parse
< 0 || !subsection
)
232 strbuf_add(name
, subsection
, subsection_len
);
233 if (check_submodule_name(name
->buf
) < 0) {
234 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
235 strbuf_release(name
);
239 strbuf_addstr(item
, key
);
244 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
245 const struct object_id
*gitmodules_oid
, const char *name
)
247 struct submodule
*submodule
;
248 struct strbuf name_buf
= STRBUF_INIT
;
250 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
254 submodule
= xmalloc(sizeof(*submodule
));
256 strbuf_addstr(&name_buf
, name
);
257 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
259 submodule
->path
= NULL
;
260 submodule
->url
= NULL
;
261 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
262 submodule
->update_strategy
.command
= NULL
;
263 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
264 submodule
->ignore
= NULL
;
265 submodule
->branch
= NULL
;
266 submodule
->recommend_shallow
= -1;
268 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
270 cache_add(cache
, submodule
);
275 static int parse_fetch_recurse(const char *opt
, const char *arg
,
278 switch (git_parse_maybe_bool(arg
)) {
280 return RECURSE_SUBMODULES_ON
;
282 return RECURSE_SUBMODULES_OFF
;
284 if (!strcmp(arg
, "on-demand"))
285 return RECURSE_SUBMODULES_ON_DEMAND
;
287 * Please update $__git_fetch_recurse_submodules in
288 * git-completion.bash when you add new options.
291 die("bad %s argument: %s", opt
, arg
);
293 return RECURSE_SUBMODULES_ERROR
;
297 int parse_submodule_fetchjobs(const char *var
, const char *value
)
299 int fetchjobs
= git_config_int(var
, value
);
301 die(_("negative values not allowed for submodule.fetchjobs"));
305 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
307 return parse_fetch_recurse(opt
, arg
, 1);
310 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
311 const char *arg
, int unset
)
321 *v
= RECURSE_SUBMODULES_OFF
;
324 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
326 *v
= RECURSE_SUBMODULES_ON
;
331 static int parse_update_recurse(const char *opt
, const char *arg
,
334 switch (git_parse_maybe_bool(arg
)) {
336 return RECURSE_SUBMODULES_ON
;
338 return RECURSE_SUBMODULES_OFF
;
341 die("bad %s argument: %s", opt
, arg
);
342 return RECURSE_SUBMODULES_ERROR
;
346 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
348 return parse_update_recurse(opt
, arg
, 1);
351 static int parse_push_recurse(const char *opt
, const char *arg
,
354 switch (git_parse_maybe_bool(arg
)) {
356 /* There's no simple "on" value when pushing */
358 die("bad %s argument: %s", opt
, arg
);
360 return RECURSE_SUBMODULES_ERROR
;
362 return RECURSE_SUBMODULES_OFF
;
364 if (!strcmp(arg
, "on-demand"))
365 return RECURSE_SUBMODULES_ON_DEMAND
;
366 else if (!strcmp(arg
, "check"))
367 return RECURSE_SUBMODULES_CHECK
;
368 else if (!strcmp(arg
, "only"))
369 return RECURSE_SUBMODULES_ONLY
;
371 * Please update $__git_push_recurse_submodules in
372 * git-completion.bash when you add new modes.
374 else if (die_on_error
)
375 die("bad %s argument: %s", opt
, arg
);
377 return RECURSE_SUBMODULES_ERROR
;
381 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
383 return parse_push_recurse(opt
, arg
, 1);
386 static void warn_multiple_config(const struct object_id
*treeish_name
,
387 const char *name
, const char *option
)
389 const char *commit_string
= "WORKTREE";
391 commit_string
= oid_to_hex(treeish_name
);
392 warning("%s:.gitmodules, multiple configurations found for "
393 "'submodule.%s.%s'. Skipping second one!",
394 commit_string
, name
, option
);
397 static void warn_command_line_option(const char *var
, const char *value
)
399 warning(_("ignoring '%s' which may be interpreted as"
400 " a command-line option: %s"), var
, value
);
403 struct parse_config_parameter
{
404 struct submodule_cache
*cache
;
405 const struct object_id
*treeish_name
;
406 const struct object_id
*gitmodules_oid
;
410 static int parse_config(const char *var
, const char *value
, void *data
)
412 struct parse_config_parameter
*me
= data
;
413 struct submodule
*submodule
;
414 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
417 /* this also ensures that we only parse submodule entries */
418 if (!name_and_item_from_var(var
, &name
, &item
))
421 submodule
= lookup_or_create_by_name(me
->cache
,
425 if (!strcmp(item
.buf
, "path")) {
427 ret
= config_error_nonbool(var
);
428 else if (looks_like_command_line_option(value
))
429 warn_command_line_option(var
, value
);
430 else if (!me
->overwrite
&& submodule
->path
)
431 warn_multiple_config(me
->treeish_name
, submodule
->name
,
435 cache_remove_path(me
->cache
, submodule
);
436 free((void *) submodule
->path
);
437 submodule
->path
= xstrdup(value
);
438 cache_put_path(me
->cache
, submodule
);
440 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
441 /* when parsing worktree configurations we can die early */
442 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
443 if (!me
->overwrite
&&
444 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
445 warn_multiple_config(me
->treeish_name
, submodule
->name
,
446 "fetchrecursesubmodules");
448 submodule
->fetch_recurse
= parse_fetch_recurse(
451 } else if (!strcmp(item
.buf
, "ignore")) {
453 ret
= config_error_nonbool(var
);
454 else if (!me
->overwrite
&& submodule
->ignore
)
455 warn_multiple_config(me
->treeish_name
, submodule
->name
,
457 else if (strcmp(value
, "untracked") &&
458 strcmp(value
, "dirty") &&
459 strcmp(value
, "all") &&
460 strcmp(value
, "none"))
461 warning("Invalid parameter '%s' for config option "
462 "'submodule.%s.ignore'", value
, name
.buf
);
464 free((void *) submodule
->ignore
);
465 submodule
->ignore
= xstrdup(value
);
467 } else if (!strcmp(item
.buf
, "url")) {
469 ret
= config_error_nonbool(var
);
470 } else if (looks_like_command_line_option(value
)) {
471 warn_command_line_option(var
, value
);
472 } else if (!me
->overwrite
&& submodule
->url
) {
473 warn_multiple_config(me
->treeish_name
, submodule
->name
,
476 free((void *) submodule
->url
);
477 submodule
->url
= xstrdup(value
);
479 } else if (!strcmp(item
.buf
, "update")) {
481 ret
= config_error_nonbool(var
);
482 else if (!me
->overwrite
&&
483 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
484 warn_multiple_config(me
->treeish_name
, submodule
->name
,
486 else if (parse_submodule_update_strategy(value
,
487 &submodule
->update_strategy
) < 0)
488 die(_("invalid value for %s"), var
);
489 } else if (!strcmp(item
.buf
, "shallow")) {
490 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
491 warn_multiple_config(me
->treeish_name
, submodule
->name
,
494 submodule
->recommend_shallow
=
495 git_config_bool(var
, value
);
496 } else if (!strcmp(item
.buf
, "branch")) {
497 if (!me
->overwrite
&& submodule
->branch
)
498 warn_multiple_config(me
->treeish_name
, submodule
->name
,
501 free((void *)submodule
->branch
);
502 submodule
->branch
= xstrdup(value
);
506 strbuf_release(&name
);
507 strbuf_release(&item
);
512 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
513 struct object_id
*gitmodules_oid
,
518 if (is_null_oid(treeish_name
)) {
519 oidclr(gitmodules_oid
);
523 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
524 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
530 /* This does a lookup of a submodule configuration by name or by path
531 * (key) with on-demand reading of the appropriate .gitmodules from
534 static const struct submodule
*config_from(struct submodule_cache
*cache
,
535 const struct object_id
*treeish_name
, const char *key
,
536 enum lookup_type lookup_type
)
538 struct strbuf rev
= STRBUF_INIT
;
539 unsigned long config_size
;
541 struct object_id oid
;
542 enum object_type type
;
543 const struct submodule
*submodule
= NULL
;
544 struct parse_config_parameter parameter
;
547 * If any parameter except the cache is a NULL pointer just
548 * return the first submodule. Can be used to check whether
549 * there are any submodules parsed.
551 if (!treeish_name
|| !key
) {
552 struct hashmap_iter iter
;
553 struct submodule_entry
*entry
;
555 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
558 return entry
->config
;
561 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
564 switch (lookup_type
) {
566 submodule
= cache_lookup_name(cache
, &oid
, key
);
569 submodule
= cache_lookup_path(cache
, &oid
, key
);
575 config
= read_object_file(&oid
, &type
, &config_size
);
576 if (!config
|| type
!= OBJ_BLOB
)
579 /* fill the submodule config into the cache */
580 parameter
.cache
= cache
;
581 parameter
.treeish_name
= treeish_name
;
582 parameter
.gitmodules_oid
= &oid
;
583 parameter
.overwrite
= 0;
584 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
585 config
, config_size
, ¶meter
, NULL
);
586 strbuf_release(&rev
);
589 switch (lookup_type
) {
591 return cache_lookup_name(cache
, &oid
, key
);
593 return cache_lookup_path(cache
, &oid
, key
);
599 strbuf_release(&rev
);
604 static void submodule_cache_check_init(struct repository
*repo
)
606 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
609 if (!repo
->submodule_cache
)
610 repo
->submodule_cache
= submodule_cache_alloc();
612 submodule_cache_init(repo
->submodule_cache
);
616 * Note: This function is private for a reason, the '.gitmodules' file should
617 * not be used as as a mechanism to retrieve arbitrary configuration stored in
620 * Runs the provided config function on the '.gitmodules' file found in the
623 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
625 if (repo
->worktree
) {
626 struct git_config_source config_source
= { 0 };
627 const struct config_options opts
= { 0 };
628 struct object_id oid
;
632 file
= repo_worktree_path(repo
, GITMODULES_FILE
);
633 if (file_exists(file
)) {
634 config_source
.file
= file
;
635 } else if (repo_get_oid(repo
, GITMODULES_INDEX
, &oid
) >= 0 ||
636 repo_get_oid(repo
, GITMODULES_HEAD
, &oid
) >= 0) {
637 config_source
.blob
= oidstr
= xstrdup(oid_to_hex(&oid
));
638 if (repo
!= the_repository
)
639 add_to_alternates_memory(repo
->objects
->odb
->path
);
644 config_with_options(fn
, data
, &config_source
, &opts
);
652 static int gitmodules_cb(const char *var
, const char *value
, void *data
)
654 struct repository
*repo
= data
;
655 struct parse_config_parameter parameter
;
657 parameter
.cache
= repo
->submodule_cache
;
658 parameter
.treeish_name
= NULL
;
659 parameter
.gitmodules_oid
= &null_oid
;
660 parameter
.overwrite
= 1;
662 return parse_config(var
, value
, ¶meter
);
665 void repo_read_gitmodules(struct repository
*repo
)
667 submodule_cache_check_init(repo
);
669 if (repo_read_index(repo
) < 0)
672 if (!is_gitmodules_unmerged(repo
->index
))
673 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
675 repo
->submodule_cache
->gitmodules_read
= 1;
678 void gitmodules_config_oid(const struct object_id
*commit_oid
)
680 struct strbuf rev
= STRBUF_INIT
;
681 struct object_id oid
;
683 submodule_cache_check_init(the_repository
);
685 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
686 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
687 &oid
, the_repository
);
689 strbuf_release(&rev
);
691 the_repository
->submodule_cache
->gitmodules_read
= 1;
694 static void gitmodules_read_check(struct repository
*repo
)
696 submodule_cache_check_init(repo
);
698 /* read the repo's .gitmodules file if it hasn't been already */
699 if (!repo
->submodule_cache
->gitmodules_read
)
700 repo_read_gitmodules(repo
);
703 const struct submodule
*submodule_from_name(struct repository
*r
,
704 const struct object_id
*treeish_name
,
707 gitmodules_read_check(r
);
708 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
711 const struct submodule
*submodule_from_path(struct repository
*r
,
712 const struct object_id
*treeish_name
,
715 gitmodules_read_check(r
);
716 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
719 void submodule_free(struct repository
*r
)
721 if (r
->submodule_cache
)
722 submodule_cache_clear(r
->submodule_cache
);
725 static int config_print_callback(const char *var
, const char *value
, void *cb_data
)
727 char *wanted_key
= cb_data
;
729 if (!strcmp(wanted_key
, var
))
730 printf("%s\n", value
);
735 int print_config_from_gitmodules(struct repository
*repo
, const char *key
)
740 ret
= git_config_parse_key(key
, &store_key
, NULL
);
742 return CONFIG_INVALID_KEY
;
744 config_from_gitmodules(config_print_callback
, repo
, store_key
);
750 int config_set_in_gitmodules_file_gently(const char *key
, const char *value
)
754 ret
= git_config_set_in_file_gently(GITMODULES_FILE
, key
, value
);
756 /* Maybe the user already did that, don't error out here */
757 warning(_("Could not update .gitmodules entry %s"), key
);
762 struct fetch_config
{
764 int *recurse_submodules
;
767 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
769 struct fetch_config
*config
= cb
;
770 if (!strcmp(var
, "submodule.fetchjobs")) {
771 *(config
->max_children
) = parse_submodule_fetchjobs(var
, value
);
773 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
774 *(config
->recurse_submodules
) = parse_fetch_recurse_submodules_arg(var
, value
);
781 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
783 struct fetch_config config
= {
784 .max_children
= max_children
,
785 .recurse_submodules
= recurse_submodules
787 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
790 static int gitmodules_update_clone_config(const char *var
, const char *value
,
794 if (!strcmp(var
, "submodule.fetchjobs"))
795 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
799 void update_clone_config_from_gitmodules(int *max_jobs
)
801 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);