1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
6 #include "environment.h"
10 #include "repository.h"
12 #include "submodule-config.h"
13 #include "submodule.h"
15 #include "object-name.h"
16 #include "object-store.h"
17 #include "parse-options.h"
18 #include "thread-utils.h"
19 #include "tree-walk.h"
24 * submodule cache lookup structure
25 * There is one shared set of 'struct submodule' entries which can be
26 * looked up by their sha1 blob id of the .gitmodules file and either
27 * using path or name as key.
28 * for_path stores submodule entries with path as key
29 * for_name stores submodule entries with name as key
31 struct submodule_cache
{
32 struct hashmap for_path
;
33 struct hashmap for_name
;
34 unsigned initialized
:1;
35 unsigned gitmodules_read
:1;
39 * thin wrapper struct needed to insert 'struct submodule' entries to
42 struct submodule_entry
{
43 struct hashmap_entry ent
;
44 struct submodule
*config
;
52 static int config_path_cmp(const void *cmp_data UNUSED
,
53 const struct hashmap_entry
*eptr
,
54 const struct hashmap_entry
*entry_or_key
,
55 const void *keydata UNUSED
)
57 const struct submodule_entry
*a
, *b
;
59 a
= container_of(eptr
, const struct submodule_entry
, ent
);
60 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
62 return strcmp(a
->config
->path
, b
->config
->path
) ||
63 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
66 static int config_name_cmp(const void *cmp_data UNUSED
,
67 const struct hashmap_entry
*eptr
,
68 const struct hashmap_entry
*entry_or_key
,
69 const void *keydata UNUSED
)
71 const struct submodule_entry
*a
, *b
;
73 a
= container_of(eptr
, const struct submodule_entry
, ent
);
74 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
76 return strcmp(a
->config
->name
, b
->config
->name
) ||
77 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
80 static struct submodule_cache
*submodule_cache_alloc(void)
82 return xcalloc(1, sizeof(struct submodule_cache
));
85 static void submodule_cache_init(struct submodule_cache
*cache
)
87 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
88 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
89 cache
->initialized
= 1;
92 static void free_one_config(struct submodule_entry
*entry
)
94 free((void *) entry
->config
->path
);
95 free((void *) entry
->config
->name
);
96 free((void *) entry
->config
->branch
);
97 free((void *) entry
->config
->url
);
98 free((void *) entry
->config
->ignore
);
99 submodule_update_strategy_release(&entry
->config
->update_strategy
);
103 static void submodule_cache_clear(struct submodule_cache
*cache
)
105 struct hashmap_iter iter
;
106 struct submodule_entry
*entry
;
108 if (!cache
->initialized
)
112 * We iterate over the name hash here to be symmetric with the
113 * allocation of struct submodule entries. Each is allocated by
114 * their .gitmodules blob sha1 and submodule name.
116 hashmap_for_each_entry(&cache
->for_name
, &iter
, entry
,
117 ent
/* member name */)
118 free_one_config(entry
);
120 hashmap_clear_and_free(&cache
->for_path
, struct submodule_entry
, ent
);
121 hashmap_clear_and_free(&cache
->for_name
, struct submodule_entry
, ent
);
122 cache
->initialized
= 0;
123 cache
->gitmodules_read
= 0;
126 void submodule_cache_free(struct submodule_cache
*cache
)
128 submodule_cache_clear(cache
);
132 static unsigned int hash_oid_string(const struct object_id
*oid
,
135 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
138 static void cache_put_path(struct submodule_cache
*cache
,
139 struct submodule
*submodule
)
141 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
143 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
144 hashmap_entry_init(&e
->ent
, hash
);
145 e
->config
= submodule
;
146 hashmap_put(&cache
->for_path
, &e
->ent
);
149 static void cache_remove_path(struct submodule_cache
*cache
,
150 struct submodule
*submodule
)
152 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
154 struct submodule_entry e
;
155 struct submodule_entry
*removed
;
156 hashmap_entry_init(&e
.ent
, hash
);
157 e
.config
= submodule
;
158 removed
= hashmap_remove_entry(&cache
->for_path
, &e
, ent
, NULL
);
162 static void cache_add(struct submodule_cache
*cache
,
163 struct submodule
*submodule
)
165 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
167 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
168 hashmap_entry_init(&e
->ent
, hash
);
169 e
->config
= submodule
;
170 hashmap_add(&cache
->for_name
, &e
->ent
);
173 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
174 const struct object_id
*gitmodules_oid
, const char *path
)
176 struct submodule_entry
*entry
;
177 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
178 struct submodule_entry key
;
179 struct submodule key_config
;
181 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
182 key_config
.path
= path
;
184 hashmap_entry_init(&key
.ent
, hash
);
185 key
.config
= &key_config
;
187 entry
= hashmap_get_entry(&cache
->for_path
, &key
, ent
, NULL
);
189 return entry
->config
;
193 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
194 const struct object_id
*gitmodules_oid
, const char *name
)
196 struct submodule_entry
*entry
;
197 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
198 struct submodule_entry key
;
199 struct submodule key_config
;
201 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
202 key_config
.name
= name
;
204 hashmap_entry_init(&key
.ent
, hash
);
205 key
.config
= &key_config
;
207 entry
= hashmap_get_entry(&cache
->for_name
, &key
, ent
, NULL
);
209 return entry
->config
;
213 int check_submodule_name(const char *name
)
215 /* Disallow empty names */
220 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
221 * separators rather than is_dir_sep(), because we want the name rules
222 * to be consistent across platforms.
224 goto in_component
; /* always start inside component */
227 if (is_xplatform_dir_sep(c
)) {
229 if (name
[0] == '.' && name
[1] == '.' &&
230 (!name
[2] || is_xplatform_dir_sep(name
[2])))
238 static int starts_with_dot_slash(const char *const path
)
240 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
241 PATH_MATCH_XPLATFORM
);
244 static int starts_with_dot_dot_slash(const char *const path
)
246 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
247 PATH_MATCH_XPLATFORM
);
250 static int submodule_url_is_relative(const char *url
)
252 return starts_with_dot_slash(url
) || starts_with_dot_dot_slash(url
);
256 * Count directory components that a relative submodule URL should chop
257 * from the remote_url it is to be resolved against.
259 * In other words, this counts "../" components at the start of a
262 * Returns the number of directory components to chop and writes a
263 * pointer to the next character of url after all leading "./" and
264 * "../" components to out.
266 static int count_leading_dotdots(const char *url
, const char **out
)
270 if (starts_with_dot_dot_slash(url
)) {
272 url
+= strlen("../");
275 if (starts_with_dot_slash(url
)) {
284 * Check whether a transport is implemented by git-remote-curl.
286 * If it is, returns 1 and writes the URL that would be passed to
287 * git-remote-curl to the "out" parameter.
289 * Otherwise, returns 0 and leaves "out" untouched.
292 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
293 * https://example.com/repo.git -> 1, https://example.com/repo.git
294 * git://example.com/repo.git -> 0
296 * This is for use in checking for previously exploitable bugs that
297 * required a submodule URL to be passed to git-remote-curl.
299 static int url_to_curl_url(const char *url
, const char **out
)
302 * We don't need to check for case-aliases, "http.exe", and so
303 * on because in the default configuration, is_transport_allowed
304 * prevents URLs with those schemes from being cloned
307 if (skip_prefix(url
, "http::", out
) ||
308 skip_prefix(url
, "https::", out
) ||
309 skip_prefix(url
, "ftp::", out
) ||
310 skip_prefix(url
, "ftps::", out
))
312 if (starts_with(url
, "http://") ||
313 starts_with(url
, "https://") ||
314 starts_with(url
, "ftp://") ||
315 starts_with(url
, "ftps://")) {
322 int check_submodule_url(const char *url
)
324 const char *curl_url
;
326 if (looks_like_command_line_option(url
))
329 if (submodule_url_is_relative(url
) || starts_with(url
, "git://")) {
335 * This could be appended to an http URL and url-decoded;
336 * check for malicious characters.
338 decoded
= url_decode(url
);
339 has_nl
= !!strchr(decoded
, '\n');
346 * URLs which escape their root via "../" can overwrite
347 * the host field and previous components, resolving to
348 * URLs like https::example.com/submodule.git and
349 * https:///example.com/submodule.git that were
350 * susceptible to CVE-2020-11008.
352 if (count_leading_dotdots(url
, &next
) > 0 &&
353 (*next
== ':' || *next
== '/'))
357 else if (url_to_curl_url(url
, &curl_url
)) {
359 char *normalized
= url_normalize(curl_url
, NULL
);
361 char *decoded
= url_decode(normalized
);
362 if (strchr(decoded
, '\n'))
376 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
379 const char *subsection
, *key
;
380 size_t subsection_len
;
382 parse
= parse_config_key(var
, "submodule", &subsection
,
383 &subsection_len
, &key
);
384 if (parse
< 0 || !subsection
)
387 strbuf_add(name
, subsection
, subsection_len
);
388 if (check_submodule_name(name
->buf
) < 0) {
389 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
390 strbuf_release(name
);
394 strbuf_addstr(item
, key
);
399 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
400 const struct object_id
*gitmodules_oid
, const char *name
)
402 struct submodule
*submodule
;
403 struct strbuf name_buf
= STRBUF_INIT
;
405 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
409 submodule
= xmalloc(sizeof(*submodule
));
411 strbuf_addstr(&name_buf
, name
);
412 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
414 submodule
->path
= NULL
;
415 submodule
->url
= NULL
;
416 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
417 submodule
->update_strategy
.command
= NULL
;
418 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
419 submodule
->ignore
= NULL
;
420 submodule
->branch
= NULL
;
421 submodule
->recommend_shallow
= -1;
423 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
425 cache_add(cache
, submodule
);
430 static int parse_fetch_recurse(const char *opt
, const char *arg
,
433 switch (git_parse_maybe_bool(arg
)) {
435 return RECURSE_SUBMODULES_ON
;
437 return RECURSE_SUBMODULES_OFF
;
439 if (!strcmp(arg
, "on-demand"))
440 return RECURSE_SUBMODULES_ON_DEMAND
;
442 * Please update $__git_fetch_recurse_submodules in
443 * git-completion.bash when you add new options.
446 die("bad %s argument: %s", opt
, arg
);
448 return RECURSE_SUBMODULES_ERROR
;
452 int parse_submodule_fetchjobs(const char *var
, const char *value
,
453 const struct key_value_info
*kvi
)
455 int fetchjobs
= git_config_int(var
, value
, kvi
);
457 die(_("negative values not allowed for submodule.fetchJobs"));
459 fetchjobs
= online_cpus();
463 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
465 return parse_fetch_recurse(opt
, arg
, 1);
468 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
469 const char *arg
, int unset
)
479 *v
= RECURSE_SUBMODULES_OFF
;
482 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
484 *v
= RECURSE_SUBMODULES_ON
;
489 static int parse_update_recurse(const char *opt
, const char *arg
,
492 switch (git_parse_maybe_bool(arg
)) {
494 return RECURSE_SUBMODULES_ON
;
496 return RECURSE_SUBMODULES_OFF
;
499 die("bad %s argument: %s", opt
, arg
);
500 return RECURSE_SUBMODULES_ERROR
;
504 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
506 return parse_update_recurse(opt
, arg
, 1);
509 static int parse_push_recurse(const char *opt
, const char *arg
,
512 switch (git_parse_maybe_bool(arg
)) {
514 /* There's no simple "on" value when pushing */
516 die("bad %s argument: %s", opt
, arg
);
518 return RECURSE_SUBMODULES_ERROR
;
520 return RECURSE_SUBMODULES_OFF
;
522 if (!strcmp(arg
, "on-demand"))
523 return RECURSE_SUBMODULES_ON_DEMAND
;
524 else if (!strcmp(arg
, "check"))
525 return RECURSE_SUBMODULES_CHECK
;
526 else if (!strcmp(arg
, "only"))
527 return RECURSE_SUBMODULES_ONLY
;
529 * Please update $__git_push_recurse_submodules in
530 * git-completion.bash when you add new modes.
532 else if (die_on_error
)
533 die("bad %s argument: %s", opt
, arg
);
535 return RECURSE_SUBMODULES_ERROR
;
539 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
541 return parse_push_recurse(opt
, arg
, 1);
544 static void warn_multiple_config(const struct object_id
*treeish_name
,
545 const char *name
, const char *option
)
547 const char *commit_string
= "WORKTREE";
549 commit_string
= oid_to_hex(treeish_name
);
550 warning("%s:.gitmodules, multiple configurations found for "
551 "'submodule.%s.%s'. Skipping second one!",
552 commit_string
, name
, option
);
555 static void warn_command_line_option(const char *var
, const char *value
)
557 warning(_("ignoring '%s' which may be interpreted as"
558 " a command-line option: %s"), var
, value
);
561 struct parse_config_parameter
{
562 struct submodule_cache
*cache
;
563 const struct object_id
*treeish_name
;
564 const struct object_id
*gitmodules_oid
;
569 * Parse a config item from .gitmodules.
571 * This does not handle submodule-related configuration from the main
572 * config store (.git/config, etc). Callers are responsible for
573 * checking for overrides in the main config store when appropriate.
575 static int parse_config(const char *var
, const char *value
,
576 const struct config_context
*ctx UNUSED
, void *data
)
578 struct parse_config_parameter
*me
= data
;
579 struct submodule
*submodule
;
580 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
583 /* this also ensures that we only parse submodule entries */
584 if (!name_and_item_from_var(var
, &name
, &item
))
587 submodule
= lookup_or_create_by_name(me
->cache
,
591 if (!strcmp(item
.buf
, "path")) {
593 ret
= config_error_nonbool(var
);
594 else if (looks_like_command_line_option(value
))
595 warn_command_line_option(var
, value
);
596 else if (!me
->overwrite
&& submodule
->path
)
597 warn_multiple_config(me
->treeish_name
, submodule
->name
,
601 cache_remove_path(me
->cache
, submodule
);
602 free((void *) submodule
->path
);
603 submodule
->path
= xstrdup(value
);
604 cache_put_path(me
->cache
, submodule
);
606 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
607 /* when parsing worktree configurations we can die early */
608 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
609 if (!me
->overwrite
&&
610 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
611 warn_multiple_config(me
->treeish_name
, submodule
->name
,
612 "fetchrecursesubmodules");
614 submodule
->fetch_recurse
= parse_fetch_recurse(
617 } else if (!strcmp(item
.buf
, "ignore")) {
619 ret
= config_error_nonbool(var
);
620 else if (!me
->overwrite
&& submodule
->ignore
)
621 warn_multiple_config(me
->treeish_name
, submodule
->name
,
623 else if (strcmp(value
, "untracked") &&
624 strcmp(value
, "dirty") &&
625 strcmp(value
, "all") &&
626 strcmp(value
, "none"))
627 warning("Invalid parameter '%s' for config option "
628 "'submodule.%s.ignore'", value
, name
.buf
);
630 free((void *) submodule
->ignore
);
631 submodule
->ignore
= xstrdup(value
);
633 } else if (!strcmp(item
.buf
, "url")) {
635 ret
= config_error_nonbool(var
);
636 } else if (looks_like_command_line_option(value
)) {
637 warn_command_line_option(var
, value
);
638 } else if (!me
->overwrite
&& submodule
->url
) {
639 warn_multiple_config(me
->treeish_name
, submodule
->name
,
642 free((void *) submodule
->url
);
643 submodule
->url
= xstrdup(value
);
645 } else if (!strcmp(item
.buf
, "update")) {
647 ret
= config_error_nonbool(var
);
648 else if (!me
->overwrite
&&
649 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
650 warn_multiple_config(me
->treeish_name
, submodule
->name
,
652 else if (parse_submodule_update_strategy(value
,
653 &submodule
->update_strategy
) < 0 ||
654 submodule
->update_strategy
.type
== SM_UPDATE_COMMAND
)
655 die(_("invalid value for '%s'"), var
);
656 } else if (!strcmp(item
.buf
, "shallow")) {
657 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
658 warn_multiple_config(me
->treeish_name
, submodule
->name
,
661 submodule
->recommend_shallow
=
662 git_config_bool(var
, value
);
663 } else if (!strcmp(item
.buf
, "branch")) {
665 ret
= config_error_nonbool(var
);
666 else if (!me
->overwrite
&& submodule
->branch
)
667 warn_multiple_config(me
->treeish_name
, submodule
->name
,
670 free((void *)submodule
->branch
);
671 submodule
->branch
= xstrdup(value
);
675 strbuf_release(&name
);
676 strbuf_release(&item
);
681 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
682 struct object_id
*gitmodules_oid
,
687 if (is_null_oid(treeish_name
)) {
688 oidclr(gitmodules_oid
, the_repository
->hash_algo
);
692 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
693 if (repo_get_oid(the_repository
, rev
->buf
, gitmodules_oid
) >= 0)
699 /* This does a lookup of a submodule configuration by name or by path
700 * (key) with on-demand reading of the appropriate .gitmodules from
703 static const struct submodule
*config_from(struct submodule_cache
*cache
,
704 const struct object_id
*treeish_name
, const char *key
,
705 enum lookup_type lookup_type
)
707 struct strbuf rev
= STRBUF_INIT
;
708 unsigned long config_size
;
710 struct object_id oid
;
711 enum object_type type
;
712 const struct submodule
*submodule
= NULL
;
713 struct parse_config_parameter parameter
;
716 * If any parameter except the cache is a NULL pointer just
717 * return the first submodule. Can be used to check whether
718 * there are any submodules parsed.
720 if (!treeish_name
|| !key
) {
721 struct hashmap_iter iter
;
722 struct submodule_entry
*entry
;
724 entry
= hashmap_iter_first_entry(&cache
->for_name
, &iter
,
725 struct submodule_entry
,
726 ent
/* member name */);
729 return entry
->config
;
732 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
735 switch (lookup_type
) {
737 submodule
= cache_lookup_name(cache
, &oid
, key
);
740 submodule
= cache_lookup_path(cache
, &oid
, key
);
746 config
= repo_read_object_file(the_repository
, &oid
, &type
,
748 if (!config
|| type
!= OBJ_BLOB
)
751 /* fill the submodule config into the cache */
752 parameter
.cache
= cache
;
753 parameter
.treeish_name
= treeish_name
;
754 parameter
.gitmodules_oid
= &oid
;
755 parameter
.overwrite
= 0;
756 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
757 config
, config_size
, ¶meter
, CONFIG_SCOPE_UNKNOWN
, NULL
);
758 strbuf_release(&rev
);
761 switch (lookup_type
) {
763 return cache_lookup_name(cache
, &oid
, key
);
765 return cache_lookup_path(cache
, &oid
, key
);
771 strbuf_release(&rev
);
776 static void submodule_cache_check_init(struct repository
*repo
)
778 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
781 if (!repo
->submodule_cache
)
782 repo
->submodule_cache
= submodule_cache_alloc();
784 submodule_cache_init(repo
->submodule_cache
);
788 * Note: This function is private for a reason, the '.gitmodules' file should
789 * not be used as a mechanism to retrieve arbitrary configuration stored in
792 * Runs the provided config function on the '.gitmodules' file found in the
795 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
797 if (repo
->worktree
) {
798 struct git_config_source config_source
= {
799 0, .scope
= CONFIG_SCOPE_SUBMODULE
801 const struct config_options opts
= { 0 };
802 struct object_id oid
;
806 file
= repo_worktree_path(repo
, GITMODULES_FILE
);
807 if (file_exists(file
)) {
808 config_source
.file
= file
;
809 } else if (repo_get_oid(repo
, GITMODULES_INDEX
, &oid
) >= 0 ||
810 repo_get_oid(repo
, GITMODULES_HEAD
, &oid
) >= 0) {
811 config_source
.blob
= oidstr
= xstrdup(oid_to_hex(&oid
));
812 if (repo
!= the_repository
)
813 add_submodule_odb_by_path(repo
->objects
->odb
->path
);
818 config_with_options(fn
, data
, &config_source
, repo
, &opts
);
826 static int gitmodules_cb(const char *var
, const char *value
,
827 const struct config_context
*ctx
, void *data
)
829 struct repository
*repo
= data
;
830 struct parse_config_parameter parameter
;
832 parameter
.cache
= repo
->submodule_cache
;
833 parameter
.treeish_name
= NULL
;
834 parameter
.gitmodules_oid
= null_oid(the_hash_algo
);
835 parameter
.overwrite
= 1;
837 return parse_config(var
, value
, ctx
, ¶meter
);
840 void repo_read_gitmodules(struct repository
*repo
, int skip_if_read
)
842 submodule_cache_check_init(repo
);
844 if (repo
->submodule_cache
->gitmodules_read
&& skip_if_read
)
847 if (repo_read_index(repo
) < 0)
850 if (!is_gitmodules_unmerged(repo
->index
))
851 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
853 repo
->submodule_cache
->gitmodules_read
= 1;
856 void gitmodules_config_oid(const struct object_id
*commit_oid
)
858 struct strbuf rev
= STRBUF_INIT
;
859 struct object_id oid
;
861 submodule_cache_check_init(the_repository
);
863 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
864 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
865 the_repository
, &oid
, the_repository
,
866 CONFIG_SCOPE_UNKNOWN
);
868 strbuf_release(&rev
);
870 the_repository
->submodule_cache
->gitmodules_read
= 1;
873 const struct submodule
*submodule_from_name(struct repository
*r
,
874 const struct object_id
*treeish_name
,
877 repo_read_gitmodules(r
, 1);
878 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
881 const struct submodule
*submodule_from_path(struct repository
*r
,
882 const struct object_id
*treeish_name
,
885 repo_read_gitmodules(r
, 1);
886 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
890 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
891 * and appends submodule entries to 'out'. The submodule_cache expects
892 * a root-level treeish_name and paths, so keep track of these values
893 * with 'root_tree' and 'prefix'.
895 static void traverse_tree_submodules(struct repository
*r
,
896 const struct object_id
*root_tree
,
898 const struct object_id
*treeish_name
,
899 struct submodule_entry_list
*out
)
901 struct tree_desc tree
;
902 struct submodule_tree_entry
*st_entry
;
903 struct name_entry name_entry
;
904 char *tree_path
= NULL
;
907 tree_buf
= fill_tree_descriptor(r
, &tree
, treeish_name
);
908 while (tree_entry(&tree
, &name_entry
)) {
911 mkpathdup("%s/%s", prefix
, name_entry
.path
);
913 tree_path
= xstrdup(name_entry
.path
);
915 if (S_ISGITLINK(name_entry
.mode
) &&
916 is_tree_submodule_active(r
, root_tree
, tree_path
)) {
917 ALLOC_GROW(out
->entries
, out
->entry_nr
+ 1,
919 st_entry
= &out
->entries
[out
->entry_nr
++];
921 st_entry
->name_entry
= xmalloc(sizeof(*st_entry
->name_entry
));
922 *st_entry
->name_entry
= name_entry
;
923 st_entry
->submodule
=
924 submodule_from_path(r
, root_tree
, tree_path
);
925 st_entry
->repo
= xmalloc(sizeof(*st_entry
->repo
));
926 if (repo_submodule_init(st_entry
->repo
, r
, tree_path
,
928 FREE_AND_NULL(st_entry
->repo
);
930 } else if (S_ISDIR(name_entry
.mode
))
931 traverse_tree_submodules(r
, root_tree
, tree_path
,
932 &name_entry
.oid
, out
);
939 void submodules_of_tree(struct repository
*r
,
940 const struct object_id
*treeish_name
,
941 struct submodule_entry_list
*out
)
943 CALLOC_ARRAY(out
->entries
, 0);
945 out
->entry_alloc
= 0;
947 traverse_tree_submodules(r
, treeish_name
, NULL
, treeish_name
, out
);
950 void submodule_entry_list_release(struct submodule_entry_list
*list
)
952 for (size_t i
= 0; i
< list
->entry_nr
; i
++) {
953 free(list
->entries
[i
].name_entry
);
954 repo_clear(list
->entries
[i
].repo
);
955 free(list
->entries
[i
].repo
);
960 void submodule_free(struct repository
*r
)
962 if (r
->submodule_cache
)
963 submodule_cache_clear(r
->submodule_cache
);
966 static int config_print_callback(const char *var
, const char *value
,
967 const struct config_context
*ctx UNUSED
,
970 char *wanted_key
= cb_data
;
972 if (!strcmp(wanted_key
, var
))
973 printf("%s\n", value
);
978 int print_config_from_gitmodules(struct repository
*repo
, const char *key
)
983 ret
= git_config_parse_key(key
, &store_key
, NULL
);
985 return CONFIG_INVALID_KEY
;
987 config_from_gitmodules(config_print_callback
, repo
, store_key
);
993 int config_set_in_gitmodules_file_gently(const char *key
, const char *value
)
997 ret
= git_config_set_in_file_gently(GITMODULES_FILE
, key
, NULL
, value
);
999 /* Maybe the user already did that, don't error out here */
1000 warning(_("Could not update .gitmodules entry %s"), key
);
1005 struct fetch_config
{
1007 int *recurse_submodules
;
1010 static int gitmodules_fetch_config(const char *var
, const char *value
,
1011 const struct config_context
*ctx
,
1014 struct fetch_config
*config
= cb
;
1015 if (!strcmp(var
, "submodule.fetchjobs")) {
1016 if (config
->max_children
)
1017 *(config
->max_children
) =
1018 parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1020 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
1021 if (config
->recurse_submodules
)
1022 *(config
->recurse_submodules
) =
1023 parse_fetch_recurse_submodules_arg(var
, value
);
1030 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
1032 struct fetch_config config
= {
1033 .max_children
= max_children
,
1034 .recurse_submodules
= recurse_submodules
1036 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
1039 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1040 const struct config_context
*ctx
,
1044 if (!strcmp(var
, "submodule.fetchjobs"))
1045 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1049 void update_clone_config_from_gitmodules(int *max_jobs
)
1051 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);