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
,
41 const struct hashmap_entry
*eptr
,
42 const struct hashmap_entry
*entry_or_key
,
43 const void *unused_keydata
)
45 const struct submodule_entry
*a
, *b
;
47 a
= container_of(eptr
, const struct submodule_entry
, ent
);
48 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
50 return strcmp(a
->config
->path
, b
->config
->path
) ||
51 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
54 static int config_name_cmp(const void *unused_cmp_data
,
55 const struct hashmap_entry
*eptr
,
56 const struct hashmap_entry
*entry_or_key
,
57 const void *unused_keydata
)
59 const struct submodule_entry
*a
, *b
;
61 a
= container_of(eptr
, const struct submodule_entry
, ent
);
62 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
64 return strcmp(a
->config
->name
, b
->config
->name
) ||
65 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
68 static struct submodule_cache
*submodule_cache_alloc(void)
70 return xcalloc(1, sizeof(struct submodule_cache
));
73 static void submodule_cache_init(struct submodule_cache
*cache
)
75 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
76 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
77 cache
->initialized
= 1;
80 static void free_one_config(struct submodule_entry
*entry
)
82 free((void *) entry
->config
->path
);
83 free((void *) entry
->config
->name
);
84 free((void *) entry
->config
->branch
);
85 free((void *) entry
->config
->update_strategy
.command
);
89 static void submodule_cache_clear(struct submodule_cache
*cache
)
91 struct hashmap_iter iter
;
92 struct submodule_entry
*entry
;
94 if (!cache
->initialized
)
98 * We iterate over the name hash here to be symmetric with the
99 * allocation of struct submodule entries. Each is allocated by
100 * their .gitmodules blob sha1 and submodule name.
102 hashmap_for_each_entry(&cache
->for_name
, &iter
, entry
,
103 struct submodule_entry
, ent
/* member name */)
104 free_one_config(entry
);
106 hashmap_free_entries(&cache
->for_path
, struct submodule_entry
, ent
);
107 hashmap_free_entries(&cache
->for_name
, struct submodule_entry
, ent
);
108 cache
->initialized
= 0;
109 cache
->gitmodules_read
= 0;
112 void submodule_cache_free(struct submodule_cache
*cache
)
114 submodule_cache_clear(cache
);
118 static unsigned int hash_oid_string(const struct object_id
*oid
,
121 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
124 static void cache_put_path(struct submodule_cache
*cache
,
125 struct submodule
*submodule
)
127 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
129 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
130 hashmap_entry_init(&e
->ent
, hash
);
131 e
->config
= submodule
;
132 hashmap_put(&cache
->for_path
, &e
->ent
);
135 static void cache_remove_path(struct submodule_cache
*cache
,
136 struct submodule
*submodule
)
138 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
140 struct submodule_entry e
;
141 struct submodule_entry
*removed
;
142 hashmap_entry_init(&e
.ent
, hash
);
143 e
.config
= submodule
;
144 removed
= hashmap_remove_entry(&cache
->for_path
, &e
, NULL
,
145 struct submodule_entry
,
146 ent
/* member name */);
150 static void cache_add(struct submodule_cache
*cache
,
151 struct submodule
*submodule
)
153 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
155 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
156 hashmap_entry_init(&e
->ent
, hash
);
157 e
->config
= submodule
;
158 hashmap_add(&cache
->for_name
, &e
->ent
);
161 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
162 const struct object_id
*gitmodules_oid
, const char *path
)
164 struct submodule_entry
*entry
;
165 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
166 struct submodule_entry key
;
167 struct submodule key_config
;
169 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
170 key_config
.path
= path
;
172 hashmap_entry_init(&key
.ent
, hash
);
173 key
.config
= &key_config
;
175 entry
= hashmap_get_entry(&cache
->for_path
, &key
, NULL
,
176 struct submodule_entry
, ent
);
178 return entry
->config
;
182 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
183 const struct object_id
*gitmodules_oid
, const char *name
)
185 struct submodule_entry
*entry
;
186 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
187 struct submodule_entry key
;
188 struct submodule key_config
;
190 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
191 key_config
.name
= name
;
193 hashmap_entry_init(&key
.ent
, hash
);
194 key
.config
= &key_config
;
196 entry
= hashmap_get_entry(&cache
->for_name
, &key
, NULL
,
197 struct submodule_entry
, ent
);
199 return entry
->config
;
203 int check_submodule_name(const char *name
)
205 /* Disallow empty names */
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.
214 goto in_component
; /* always start inside component */
217 if (c
== '/' || c
== '\\') {
219 if (name
[0] == '.' && name
[1] == '.' &&
220 (!name
[2] || name
[2] == '/' || name
[2] == '\\'))
228 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
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
)
238 strbuf_add(name
, subsection
, subsection_len
);
239 if (check_submodule_name(name
->buf
) < 0) {
240 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
241 strbuf_release(name
);
245 strbuf_addstr(item
, key
);
250 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
251 const struct object_id
*gitmodules_oid
, const char *name
)
253 struct submodule
*submodule
;
254 struct strbuf name_buf
= STRBUF_INIT
;
256 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
260 submodule
= xmalloc(sizeof(*submodule
));
262 strbuf_addstr(&name_buf
, name
);
263 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
265 submodule
->path
= NULL
;
266 submodule
->url
= NULL
;
267 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
268 submodule
->update_strategy
.command
= NULL
;
269 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
270 submodule
->ignore
= NULL
;
271 submodule
->branch
= NULL
;
272 submodule
->recommend_shallow
= -1;
274 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
276 cache_add(cache
, submodule
);
281 static int parse_fetch_recurse(const char *opt
, const char *arg
,
284 switch (git_parse_maybe_bool(arg
)) {
286 return RECURSE_SUBMODULES_ON
;
288 return RECURSE_SUBMODULES_OFF
;
290 if (!strcmp(arg
, "on-demand"))
291 return RECURSE_SUBMODULES_ON_DEMAND
;
293 * Please update $__git_fetch_recurse_submodules in
294 * git-completion.bash when you add new options.
297 die("bad %s argument: %s", opt
, arg
);
299 return RECURSE_SUBMODULES_ERROR
;
303 int parse_submodule_fetchjobs(const char *var
, const char *value
)
305 int fetchjobs
= git_config_int(var
, value
);
307 die(_("negative values not allowed for submodule.fetchjobs"));
311 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
313 return parse_fetch_recurse(opt
, arg
, 1);
316 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
317 const char *arg
, int unset
)
327 *v
= RECURSE_SUBMODULES_OFF
;
330 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
332 *v
= RECURSE_SUBMODULES_ON
;
337 static int parse_update_recurse(const char *opt
, const char *arg
,
340 switch (git_parse_maybe_bool(arg
)) {
342 return RECURSE_SUBMODULES_ON
;
344 return RECURSE_SUBMODULES_OFF
;
347 die("bad %s argument: %s", opt
, arg
);
348 return RECURSE_SUBMODULES_ERROR
;
352 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
354 return parse_update_recurse(opt
, arg
, 1);
357 static int parse_push_recurse(const char *opt
, const char *arg
,
360 switch (git_parse_maybe_bool(arg
)) {
362 /* There's no simple "on" value when pushing */
364 die("bad %s argument: %s", opt
, arg
);
366 return RECURSE_SUBMODULES_ERROR
;
368 return RECURSE_SUBMODULES_OFF
;
370 if (!strcmp(arg
, "on-demand"))
371 return RECURSE_SUBMODULES_ON_DEMAND
;
372 else if (!strcmp(arg
, "check"))
373 return RECURSE_SUBMODULES_CHECK
;
374 else if (!strcmp(arg
, "only"))
375 return RECURSE_SUBMODULES_ONLY
;
377 * Please update $__git_push_recurse_submodules in
378 * git-completion.bash when you add new modes.
380 else if (die_on_error
)
381 die("bad %s argument: %s", opt
, arg
);
383 return RECURSE_SUBMODULES_ERROR
;
387 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
389 return parse_push_recurse(opt
, arg
, 1);
392 static void warn_multiple_config(const struct object_id
*treeish_name
,
393 const char *name
, const char *option
)
395 const char *commit_string
= "WORKTREE";
397 commit_string
= oid_to_hex(treeish_name
);
398 warning("%s:.gitmodules, multiple configurations found for "
399 "'submodule.%s.%s'. Skipping second one!",
400 commit_string
, name
, option
);
403 static void warn_command_line_option(const char *var
, const char *value
)
405 warning(_("ignoring '%s' which may be interpreted as"
406 " a command-line option: %s"), var
, value
);
409 struct parse_config_parameter
{
410 struct submodule_cache
*cache
;
411 const struct object_id
*treeish_name
;
412 const struct object_id
*gitmodules_oid
;
416 static int parse_config(const char *var
, const char *value
, void *data
)
418 struct parse_config_parameter
*me
= data
;
419 struct submodule
*submodule
;
420 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
423 /* this also ensures that we only parse submodule entries */
424 if (!name_and_item_from_var(var
, &name
, &item
))
427 submodule
= lookup_or_create_by_name(me
->cache
,
431 if (!strcmp(item
.buf
, "path")) {
433 ret
= config_error_nonbool(var
);
434 else if (looks_like_command_line_option(value
))
435 warn_command_line_option(var
, value
);
436 else if (!me
->overwrite
&& submodule
->path
)
437 warn_multiple_config(me
->treeish_name
, submodule
->name
,
441 cache_remove_path(me
->cache
, submodule
);
442 free((void *) submodule
->path
);
443 submodule
->path
= xstrdup(value
);
444 cache_put_path(me
->cache
, submodule
);
446 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
447 /* when parsing worktree configurations we can die early */
448 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
449 if (!me
->overwrite
&&
450 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
451 warn_multiple_config(me
->treeish_name
, submodule
->name
,
452 "fetchrecursesubmodules");
454 submodule
->fetch_recurse
= parse_fetch_recurse(
457 } else if (!strcmp(item
.buf
, "ignore")) {
459 ret
= config_error_nonbool(var
);
460 else if (!me
->overwrite
&& submodule
->ignore
)
461 warn_multiple_config(me
->treeish_name
, submodule
->name
,
463 else if (strcmp(value
, "untracked") &&
464 strcmp(value
, "dirty") &&
465 strcmp(value
, "all") &&
466 strcmp(value
, "none"))
467 warning("Invalid parameter '%s' for config option "
468 "'submodule.%s.ignore'", value
, name
.buf
);
470 free((void *) submodule
->ignore
);
471 submodule
->ignore
= xstrdup(value
);
473 } else if (!strcmp(item
.buf
, "url")) {
475 ret
= config_error_nonbool(var
);
476 } else if (looks_like_command_line_option(value
)) {
477 warn_command_line_option(var
, value
);
478 } else if (!me
->overwrite
&& submodule
->url
) {
479 warn_multiple_config(me
->treeish_name
, submodule
->name
,
482 free((void *) submodule
->url
);
483 submodule
->url
= xstrdup(value
);
485 } else if (!strcmp(item
.buf
, "update")) {
487 ret
= config_error_nonbool(var
);
488 else if (!me
->overwrite
&&
489 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
490 warn_multiple_config(me
->treeish_name
, submodule
->name
,
492 else if (parse_submodule_update_strategy(value
,
493 &submodule
->update_strategy
) < 0)
494 die(_("invalid value for %s"), var
);
495 } else if (!strcmp(item
.buf
, "shallow")) {
496 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
497 warn_multiple_config(me
->treeish_name
, submodule
->name
,
500 submodule
->recommend_shallow
=
501 git_config_bool(var
, value
);
502 } else if (!strcmp(item
.buf
, "branch")) {
503 if (!me
->overwrite
&& submodule
->branch
)
504 warn_multiple_config(me
->treeish_name
, submodule
->name
,
507 free((void *)submodule
->branch
);
508 submodule
->branch
= xstrdup(value
);
512 strbuf_release(&name
);
513 strbuf_release(&item
);
518 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
519 struct object_id
*gitmodules_oid
,
524 if (is_null_oid(treeish_name
)) {
525 oidclr(gitmodules_oid
);
529 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
530 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
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
540 static const struct submodule
*config_from(struct submodule_cache
*cache
,
541 const struct object_id
*treeish_name
, const char *key
,
542 enum lookup_type lookup_type
)
544 struct strbuf rev
= STRBUF_INIT
;
545 unsigned long config_size
;
547 struct object_id oid
;
548 enum object_type type
;
549 const struct submodule
*submodule
= NULL
;
550 struct parse_config_parameter parameter
;
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.
557 if (!treeish_name
|| !key
) {
558 struct hashmap_iter iter
;
559 struct submodule_entry
*entry
;
561 entry
= hashmap_iter_first_entry(&cache
->for_name
, &iter
,
562 struct submodule_entry
,
563 ent
/* member name */);
566 return entry
->config
;
569 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
572 switch (lookup_type
) {
574 submodule
= cache_lookup_name(cache
, &oid
, key
);
577 submodule
= cache_lookup_path(cache
, &oid
, key
);
583 config
= read_object_file(&oid
, &type
, &config_size
);
584 if (!config
|| type
!= OBJ_BLOB
)
587 /* fill the submodule config into the cache */
588 parameter
.cache
= cache
;
589 parameter
.treeish_name
= treeish_name
;
590 parameter
.gitmodules_oid
= &oid
;
591 parameter
.overwrite
= 0;
592 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
593 config
, config_size
, ¶meter
, NULL
);
594 strbuf_release(&rev
);
597 switch (lookup_type
) {
599 return cache_lookup_name(cache
, &oid
, key
);
601 return cache_lookup_path(cache
, &oid
, key
);
607 strbuf_release(&rev
);
612 static void submodule_cache_check_init(struct repository
*repo
)
614 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
617 if (!repo
->submodule_cache
)
618 repo
->submodule_cache
= submodule_cache_alloc();
620 submodule_cache_init(repo
->submodule_cache
);
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
628 * Runs the provided config function on the '.gitmodules' file found in the
631 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
633 if (repo
->worktree
) {
634 struct git_config_source config_source
= { 0 };
635 const struct config_options opts
= { 0 };
636 struct object_id oid
;
640 file
= repo_worktree_path(repo
, GITMODULES_FILE
);
641 if (file_exists(file
)) {
642 config_source
.file
= file
;
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
);
652 config_with_options(fn
, data
, &config_source
, &opts
);
660 static int gitmodules_cb(const char *var
, const char *value
, void *data
)
662 struct repository
*repo
= data
;
663 struct parse_config_parameter parameter
;
665 parameter
.cache
= repo
->submodule_cache
;
666 parameter
.treeish_name
= NULL
;
667 parameter
.gitmodules_oid
= &null_oid
;
668 parameter
.overwrite
= 1;
670 return parse_config(var
, value
, ¶meter
);
673 void repo_read_gitmodules(struct repository
*repo
)
675 submodule_cache_check_init(repo
);
677 if (repo_read_index(repo
) < 0)
680 if (!is_gitmodules_unmerged(repo
->index
))
681 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
683 repo
->submodule_cache
->gitmodules_read
= 1;
686 void gitmodules_config_oid(const struct object_id
*commit_oid
)
688 struct strbuf rev
= STRBUF_INIT
;
689 struct object_id oid
;
691 submodule_cache_check_init(the_repository
);
693 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
694 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
695 &oid
, the_repository
);
697 strbuf_release(&rev
);
699 the_repository
->submodule_cache
->gitmodules_read
= 1;
702 static void gitmodules_read_check(struct repository
*repo
)
704 submodule_cache_check_init(repo
);
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
);
711 const struct submodule
*submodule_from_name(struct repository
*r
,
712 const struct object_id
*treeish_name
,
715 gitmodules_read_check(r
);
716 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
719 const struct submodule
*submodule_from_path(struct repository
*r
,
720 const struct object_id
*treeish_name
,
723 gitmodules_read_check(r
);
724 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
727 void submodule_free(struct repository
*r
)
729 if (r
->submodule_cache
)
730 submodule_cache_clear(r
->submodule_cache
);
733 static int config_print_callback(const char *var
, const char *value
, void *cb_data
)
735 char *wanted_key
= cb_data
;
737 if (!strcmp(wanted_key
, var
))
738 printf("%s\n", value
);
743 int print_config_from_gitmodules(struct repository
*repo
, const char *key
)
748 ret
= git_config_parse_key(key
, &store_key
, NULL
);
750 return CONFIG_INVALID_KEY
;
752 config_from_gitmodules(config_print_callback
, repo
, store_key
);
758 int config_set_in_gitmodules_file_gently(const char *key
, const char *value
)
762 ret
= git_config_set_in_file_gently(GITMODULES_FILE
, key
, value
);
764 /* Maybe the user already did that, don't error out here */
765 warning(_("Could not update .gitmodules entry %s"), key
);
770 struct fetch_config
{
772 int *recurse_submodules
;
775 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
777 struct fetch_config
*config
= cb
;
778 if (!strcmp(var
, "submodule.fetchjobs")) {
779 *(config
->max_children
) = parse_submodule_fetchjobs(var
, value
);
781 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
782 *(config
->recurse_submodules
) = parse_fetch_recurse_submodules_arg(var
, value
);
789 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
791 struct fetch_config config
= {
792 .max_children
= max_children
,
793 .recurse_submodules
= recurse_submodules
795 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
798 static int gitmodules_update_clone_config(const char *var
, const char *value
,
802 if (!strcmp(var
, "submodule.fetchjobs"))
803 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
807 void update_clone_config_from_gitmodules(int *max_jobs
)
809 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);