1 #include "git-compat-util.h"
3 #include "environment.h"
7 #include "repository.h"
9 #include "submodule-config.h"
10 #include "submodule.h"
12 #include "object-name.h"
13 #include "object-store-ll.h"
14 #include "parse-options.h"
15 #include "thread-utils.h"
16 #include "tree-walk.h"
21 * submodule cache lookup structure
22 * There is one shared set of 'struct submodule' entries which can be
23 * looked up by their sha1 blob id of the .gitmodules file and either
24 * using path or name as key.
25 * for_path stores submodule entries with path as key
26 * for_name stores submodule entries with name as key
28 struct submodule_cache
{
29 struct hashmap for_path
;
30 struct hashmap for_name
;
31 unsigned initialized
:1;
32 unsigned gitmodules_read
:1;
36 * thin wrapper struct needed to insert 'struct submodule' entries to
39 struct submodule_entry
{
40 struct hashmap_entry ent
;
41 struct submodule
*config
;
49 static int config_path_cmp(const void *cmp_data UNUSED
,
50 const struct hashmap_entry
*eptr
,
51 const struct hashmap_entry
*entry_or_key
,
52 const void *keydata UNUSED
)
54 const struct submodule_entry
*a
, *b
;
56 a
= container_of(eptr
, const struct submodule_entry
, ent
);
57 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
59 return strcmp(a
->config
->path
, b
->config
->path
) ||
60 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
63 static int config_name_cmp(const void *cmp_data UNUSED
,
64 const struct hashmap_entry
*eptr
,
65 const struct hashmap_entry
*entry_or_key
,
66 const void *keydata UNUSED
)
68 const struct submodule_entry
*a
, *b
;
70 a
= container_of(eptr
, const struct submodule_entry
, ent
);
71 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
73 return strcmp(a
->config
->name
, b
->config
->name
) ||
74 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
77 static struct submodule_cache
*submodule_cache_alloc(void)
79 return xcalloc(1, sizeof(struct submodule_cache
));
82 static void submodule_cache_init(struct submodule_cache
*cache
)
84 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
85 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
86 cache
->initialized
= 1;
89 static void free_one_config(struct submodule_entry
*entry
)
91 free((void *) entry
->config
->path
);
92 free((void *) entry
->config
->name
);
93 free((void *) entry
->config
->branch
);
94 free((void *) entry
->config
->update_strategy
.command
);
98 static void submodule_cache_clear(struct submodule_cache
*cache
)
100 struct hashmap_iter iter
;
101 struct submodule_entry
*entry
;
103 if (!cache
->initialized
)
107 * We iterate over the name hash here to be symmetric with the
108 * allocation of struct submodule entries. Each is allocated by
109 * their .gitmodules blob sha1 and submodule name.
111 hashmap_for_each_entry(&cache
->for_name
, &iter
, entry
,
112 ent
/* member name */)
113 free_one_config(entry
);
115 hashmap_clear_and_free(&cache
->for_path
, struct submodule_entry
, ent
);
116 hashmap_clear_and_free(&cache
->for_name
, struct submodule_entry
, ent
);
117 cache
->initialized
= 0;
118 cache
->gitmodules_read
= 0;
121 void submodule_cache_free(struct submodule_cache
*cache
)
123 submodule_cache_clear(cache
);
127 static unsigned int hash_oid_string(const struct object_id
*oid
,
130 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
133 static void cache_put_path(struct submodule_cache
*cache
,
134 struct submodule
*submodule
)
136 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
138 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
139 hashmap_entry_init(&e
->ent
, hash
);
140 e
->config
= submodule
;
141 hashmap_put(&cache
->for_path
, &e
->ent
);
144 static void cache_remove_path(struct submodule_cache
*cache
,
145 struct submodule
*submodule
)
147 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
149 struct submodule_entry e
;
150 struct submodule_entry
*removed
;
151 hashmap_entry_init(&e
.ent
, hash
);
152 e
.config
= submodule
;
153 removed
= hashmap_remove_entry(&cache
->for_path
, &e
, ent
, NULL
);
157 static void cache_add(struct submodule_cache
*cache
,
158 struct submodule
*submodule
)
160 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
162 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
163 hashmap_entry_init(&e
->ent
, hash
);
164 e
->config
= submodule
;
165 hashmap_add(&cache
->for_name
, &e
->ent
);
168 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
169 const struct object_id
*gitmodules_oid
, const char *path
)
171 struct submodule_entry
*entry
;
172 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
173 struct submodule_entry key
;
174 struct submodule key_config
;
176 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
177 key_config
.path
= path
;
179 hashmap_entry_init(&key
.ent
, hash
);
180 key
.config
= &key_config
;
182 entry
= hashmap_get_entry(&cache
->for_path
, &key
, ent
, NULL
);
184 return entry
->config
;
188 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
189 const struct object_id
*gitmodules_oid
, const char *name
)
191 struct submodule_entry
*entry
;
192 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
193 struct submodule_entry key
;
194 struct submodule key_config
;
196 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
197 key_config
.name
= name
;
199 hashmap_entry_init(&key
.ent
, hash
);
200 key
.config
= &key_config
;
202 entry
= hashmap_get_entry(&cache
->for_name
, &key
, ent
, NULL
);
204 return entry
->config
;
208 int check_submodule_name(const char *name
)
210 /* Disallow empty names */
215 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
216 * separators rather than is_dir_sep(), because we want the name rules
217 * to be consistent across platforms.
219 goto in_component
; /* always start inside component */
222 if (is_xplatform_dir_sep(c
)) {
224 if (name
[0] == '.' && name
[1] == '.' &&
225 (!name
[2] || is_xplatform_dir_sep(name
[2])))
233 static int starts_with_dot_slash(const char *const path
)
235 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
236 PATH_MATCH_XPLATFORM
);
239 static int starts_with_dot_dot_slash(const char *const path
)
241 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
242 PATH_MATCH_XPLATFORM
);
245 static int submodule_url_is_relative(const char *url
)
247 return starts_with_dot_slash(url
) || starts_with_dot_dot_slash(url
);
251 * Count directory components that a relative submodule URL should chop
252 * from the remote_url it is to be resolved against.
254 * In other words, this counts "../" components at the start of a
257 * Returns the number of directory components to chop and writes a
258 * pointer to the next character of url after all leading "./" and
259 * "../" components to out.
261 static int count_leading_dotdots(const char *url
, const char **out
)
265 if (starts_with_dot_dot_slash(url
)) {
267 url
+= strlen("../");
270 if (starts_with_dot_slash(url
)) {
279 * Check whether a transport is implemented by git-remote-curl.
281 * If it is, returns 1 and writes the URL that would be passed to
282 * git-remote-curl to the "out" parameter.
284 * Otherwise, returns 0 and leaves "out" untouched.
287 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
288 * https://example.com/repo.git -> 1, https://example.com/repo.git
289 * git://example.com/repo.git -> 0
291 * This is for use in checking for previously exploitable bugs that
292 * required a submodule URL to be passed to git-remote-curl.
294 static int url_to_curl_url(const char *url
, const char **out
)
297 * We don't need to check for case-aliases, "http.exe", and so
298 * on because in the default configuration, is_transport_allowed
299 * prevents URLs with those schemes from being cloned
302 if (skip_prefix(url
, "http::", out
) ||
303 skip_prefix(url
, "https::", out
) ||
304 skip_prefix(url
, "ftp::", out
) ||
305 skip_prefix(url
, "ftps::", out
))
307 if (starts_with(url
, "http://") ||
308 starts_with(url
, "https://") ||
309 starts_with(url
, "ftp://") ||
310 starts_with(url
, "ftps://")) {
317 int check_submodule_url(const char *url
)
319 const char *curl_url
;
321 if (looks_like_command_line_option(url
))
324 if (submodule_url_is_relative(url
) || starts_with(url
, "git://")) {
330 * This could be appended to an http URL and url-decoded;
331 * check for malicious characters.
333 decoded
= url_decode(url
);
334 has_nl
= !!strchr(decoded
, '\n');
341 * URLs which escape their root via "../" can overwrite
342 * the host field and previous components, resolving to
343 * URLs like https::example.com/submodule.git and
344 * https:///example.com/submodule.git that were
345 * susceptible to CVE-2020-11008.
347 if (count_leading_dotdots(url
, &next
) > 0 &&
348 (*next
== ':' || *next
== '/'))
352 else if (url_to_curl_url(url
, &curl_url
)) {
354 char *normalized
= url_normalize(curl_url
, NULL
);
356 char *decoded
= url_decode(normalized
);
357 if (strchr(decoded
, '\n'))
371 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
374 const char *subsection
, *key
;
375 size_t subsection_len
;
377 parse
= parse_config_key(var
, "submodule", &subsection
,
378 &subsection_len
, &key
);
379 if (parse
< 0 || !subsection
)
382 strbuf_add(name
, subsection
, subsection_len
);
383 if (check_submodule_name(name
->buf
) < 0) {
384 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
385 strbuf_release(name
);
389 strbuf_addstr(item
, key
);
394 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
395 const struct object_id
*gitmodules_oid
, const char *name
)
397 struct submodule
*submodule
;
398 struct strbuf name_buf
= STRBUF_INIT
;
400 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
404 submodule
= xmalloc(sizeof(*submodule
));
406 strbuf_addstr(&name_buf
, name
);
407 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
409 submodule
->path
= NULL
;
410 submodule
->url
= NULL
;
411 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
412 submodule
->update_strategy
.command
= NULL
;
413 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
414 submodule
->ignore
= NULL
;
415 submodule
->branch
= NULL
;
416 submodule
->recommend_shallow
= -1;
418 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
420 cache_add(cache
, submodule
);
425 static int parse_fetch_recurse(const char *opt
, const char *arg
,
428 switch (git_parse_maybe_bool(arg
)) {
430 return RECURSE_SUBMODULES_ON
;
432 return RECURSE_SUBMODULES_OFF
;
434 if (!strcmp(arg
, "on-demand"))
435 return RECURSE_SUBMODULES_ON_DEMAND
;
437 * Please update $__git_fetch_recurse_submodules in
438 * git-completion.bash when you add new options.
441 die("bad %s argument: %s", opt
, arg
);
443 return RECURSE_SUBMODULES_ERROR
;
447 int parse_submodule_fetchjobs(const char *var
, const char *value
,
448 const struct key_value_info
*kvi
)
450 int fetchjobs
= git_config_int(var
, value
, kvi
);
452 die(_("negative values not allowed for submodule.fetchJobs"));
454 fetchjobs
= online_cpus();
458 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
460 return parse_fetch_recurse(opt
, arg
, 1);
463 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
464 const char *arg
, int unset
)
474 *v
= RECURSE_SUBMODULES_OFF
;
477 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
479 *v
= RECURSE_SUBMODULES_ON
;
484 static int parse_update_recurse(const char *opt
, const char *arg
,
487 switch (git_parse_maybe_bool(arg
)) {
489 return RECURSE_SUBMODULES_ON
;
491 return RECURSE_SUBMODULES_OFF
;
494 die("bad %s argument: %s", opt
, arg
);
495 return RECURSE_SUBMODULES_ERROR
;
499 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
501 return parse_update_recurse(opt
, arg
, 1);
504 static int parse_push_recurse(const char *opt
, const char *arg
,
507 switch (git_parse_maybe_bool(arg
)) {
509 /* There's no simple "on" value when pushing */
511 die("bad %s argument: %s", opt
, arg
);
513 return RECURSE_SUBMODULES_ERROR
;
515 return RECURSE_SUBMODULES_OFF
;
517 if (!strcmp(arg
, "on-demand"))
518 return RECURSE_SUBMODULES_ON_DEMAND
;
519 else if (!strcmp(arg
, "check"))
520 return RECURSE_SUBMODULES_CHECK
;
521 else if (!strcmp(arg
, "only"))
522 return RECURSE_SUBMODULES_ONLY
;
524 * Please update $__git_push_recurse_submodules in
525 * git-completion.bash when you add new modes.
527 else if (die_on_error
)
528 die("bad %s argument: %s", opt
, arg
);
530 return RECURSE_SUBMODULES_ERROR
;
534 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
536 return parse_push_recurse(opt
, arg
, 1);
539 static void warn_multiple_config(const struct object_id
*treeish_name
,
540 const char *name
, const char *option
)
542 const char *commit_string
= "WORKTREE";
544 commit_string
= oid_to_hex(treeish_name
);
545 warning("%s:.gitmodules, multiple configurations found for "
546 "'submodule.%s.%s'. Skipping second one!",
547 commit_string
, name
, option
);
550 static void warn_command_line_option(const char *var
, const char *value
)
552 warning(_("ignoring '%s' which may be interpreted as"
553 " a command-line option: %s"), var
, value
);
556 struct parse_config_parameter
{
557 struct submodule_cache
*cache
;
558 const struct object_id
*treeish_name
;
559 const struct object_id
*gitmodules_oid
;
564 * Parse a config item from .gitmodules.
566 * This does not handle submodule-related configuration from the main
567 * config store (.git/config, etc). Callers are responsible for
568 * checking for overrides in the main config store when appropriate.
570 static int parse_config(const char *var
, const char *value
,
571 const struct config_context
*ctx UNUSED
, void *data
)
573 struct parse_config_parameter
*me
= data
;
574 struct submodule
*submodule
;
575 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
578 /* this also ensures that we only parse submodule entries */
579 if (!name_and_item_from_var(var
, &name
, &item
))
582 submodule
= lookup_or_create_by_name(me
->cache
,
586 if (!strcmp(item
.buf
, "path")) {
588 ret
= config_error_nonbool(var
);
589 else if (looks_like_command_line_option(value
))
590 warn_command_line_option(var
, value
);
591 else if (!me
->overwrite
&& submodule
->path
)
592 warn_multiple_config(me
->treeish_name
, submodule
->name
,
596 cache_remove_path(me
->cache
, submodule
);
597 free((void *) submodule
->path
);
598 submodule
->path
= xstrdup(value
);
599 cache_put_path(me
->cache
, submodule
);
601 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
602 /* when parsing worktree configurations we can die early */
603 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
604 if (!me
->overwrite
&&
605 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
606 warn_multiple_config(me
->treeish_name
, submodule
->name
,
607 "fetchrecursesubmodules");
609 submodule
->fetch_recurse
= parse_fetch_recurse(
612 } else if (!strcmp(item
.buf
, "ignore")) {
614 ret
= config_error_nonbool(var
);
615 else if (!me
->overwrite
&& submodule
->ignore
)
616 warn_multiple_config(me
->treeish_name
, submodule
->name
,
618 else if (strcmp(value
, "untracked") &&
619 strcmp(value
, "dirty") &&
620 strcmp(value
, "all") &&
621 strcmp(value
, "none"))
622 warning("Invalid parameter '%s' for config option "
623 "'submodule.%s.ignore'", value
, name
.buf
);
625 free((void *) submodule
->ignore
);
626 submodule
->ignore
= xstrdup(value
);
628 } else if (!strcmp(item
.buf
, "url")) {
630 ret
= config_error_nonbool(var
);
631 } else if (looks_like_command_line_option(value
)) {
632 warn_command_line_option(var
, value
);
633 } else if (!me
->overwrite
&& submodule
->url
) {
634 warn_multiple_config(me
->treeish_name
, submodule
->name
,
637 free((void *) submodule
->url
);
638 submodule
->url
= xstrdup(value
);
640 } else if (!strcmp(item
.buf
, "update")) {
642 ret
= config_error_nonbool(var
);
643 else if (!me
->overwrite
&&
644 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
645 warn_multiple_config(me
->treeish_name
, submodule
->name
,
647 else if (parse_submodule_update_strategy(value
,
648 &submodule
->update_strategy
) < 0 ||
649 submodule
->update_strategy
.type
== SM_UPDATE_COMMAND
)
650 die(_("invalid value for '%s'"), var
);
651 } else if (!strcmp(item
.buf
, "shallow")) {
652 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
653 warn_multiple_config(me
->treeish_name
, submodule
->name
,
656 submodule
->recommend_shallow
=
657 git_config_bool(var
, value
);
658 } else if (!strcmp(item
.buf
, "branch")) {
660 ret
= config_error_nonbool(var
);
661 else if (!me
->overwrite
&& submodule
->branch
)
662 warn_multiple_config(me
->treeish_name
, submodule
->name
,
665 free((void *)submodule
->branch
);
666 submodule
->branch
= xstrdup(value
);
670 strbuf_release(&name
);
671 strbuf_release(&item
);
676 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
677 struct object_id
*gitmodules_oid
,
682 if (is_null_oid(treeish_name
)) {
683 oidclr(gitmodules_oid
);
687 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
688 if (repo_get_oid(the_repository
, rev
->buf
, gitmodules_oid
) >= 0)
694 /* This does a lookup of a submodule configuration by name or by path
695 * (key) with on-demand reading of the appropriate .gitmodules from
698 static const struct submodule
*config_from(struct submodule_cache
*cache
,
699 const struct object_id
*treeish_name
, const char *key
,
700 enum lookup_type lookup_type
)
702 struct strbuf rev
= STRBUF_INIT
;
703 unsigned long config_size
;
705 struct object_id oid
;
706 enum object_type type
;
707 const struct submodule
*submodule
= NULL
;
708 struct parse_config_parameter parameter
;
711 * If any parameter except the cache is a NULL pointer just
712 * return the first submodule. Can be used to check whether
713 * there are any submodules parsed.
715 if (!treeish_name
|| !key
) {
716 struct hashmap_iter iter
;
717 struct submodule_entry
*entry
;
719 entry
= hashmap_iter_first_entry(&cache
->for_name
, &iter
,
720 struct submodule_entry
,
721 ent
/* member name */);
724 return entry
->config
;
727 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
730 switch (lookup_type
) {
732 submodule
= cache_lookup_name(cache
, &oid
, key
);
735 submodule
= cache_lookup_path(cache
, &oid
, key
);
741 config
= repo_read_object_file(the_repository
, &oid
, &type
,
743 if (!config
|| type
!= OBJ_BLOB
)
746 /* fill the submodule config into the cache */
747 parameter
.cache
= cache
;
748 parameter
.treeish_name
= treeish_name
;
749 parameter
.gitmodules_oid
= &oid
;
750 parameter
.overwrite
= 0;
751 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
752 config
, config_size
, ¶meter
, CONFIG_SCOPE_UNKNOWN
, NULL
);
753 strbuf_release(&rev
);
756 switch (lookup_type
) {
758 return cache_lookup_name(cache
, &oid
, key
);
760 return cache_lookup_path(cache
, &oid
, key
);
766 strbuf_release(&rev
);
771 static void submodule_cache_check_init(struct repository
*repo
)
773 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
776 if (!repo
->submodule_cache
)
777 repo
->submodule_cache
= submodule_cache_alloc();
779 submodule_cache_init(repo
->submodule_cache
);
783 * Note: This function is private for a reason, the '.gitmodules' file should
784 * not be used as a mechanism to retrieve arbitrary configuration stored in
787 * Runs the provided config function on the '.gitmodules' file found in the
790 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
792 if (repo
->worktree
) {
793 struct git_config_source config_source
= {
794 0, .scope
= CONFIG_SCOPE_SUBMODULE
796 const struct config_options opts
= { 0 };
797 struct object_id oid
;
801 file
= repo_worktree_path(repo
, GITMODULES_FILE
);
802 if (file_exists(file
)) {
803 config_source
.file
= file
;
804 } else if (repo_get_oid(repo
, GITMODULES_INDEX
, &oid
) >= 0 ||
805 repo_get_oid(repo
, GITMODULES_HEAD
, &oid
) >= 0) {
806 config_source
.blob
= oidstr
= xstrdup(oid_to_hex(&oid
));
807 if (repo
!= the_repository
)
808 add_submodule_odb_by_path(repo
->objects
->odb
->path
);
813 config_with_options(fn
, data
, &config_source
, repo
, &opts
);
821 static int gitmodules_cb(const char *var
, const char *value
,
822 const struct config_context
*ctx
, void *data
)
824 struct repository
*repo
= data
;
825 struct parse_config_parameter parameter
;
827 parameter
.cache
= repo
->submodule_cache
;
828 parameter
.treeish_name
= NULL
;
829 parameter
.gitmodules_oid
= null_oid();
830 parameter
.overwrite
= 1;
832 return parse_config(var
, value
, ctx
, ¶meter
);
835 void repo_read_gitmodules(struct repository
*repo
, int skip_if_read
)
837 submodule_cache_check_init(repo
);
839 if (repo
->submodule_cache
->gitmodules_read
&& skip_if_read
)
842 if (repo_read_index(repo
) < 0)
845 if (!is_gitmodules_unmerged(repo
->index
))
846 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
848 repo
->submodule_cache
->gitmodules_read
= 1;
851 void gitmodules_config_oid(const struct object_id
*commit_oid
)
853 struct strbuf rev
= STRBUF_INIT
;
854 struct object_id oid
;
856 submodule_cache_check_init(the_repository
);
858 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
859 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
860 the_repository
, &oid
, the_repository
,
861 CONFIG_SCOPE_UNKNOWN
);
863 strbuf_release(&rev
);
865 the_repository
->submodule_cache
->gitmodules_read
= 1;
868 const struct submodule
*submodule_from_name(struct repository
*r
,
869 const struct object_id
*treeish_name
,
872 repo_read_gitmodules(r
, 1);
873 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
876 const struct submodule
*submodule_from_path(struct repository
*r
,
877 const struct object_id
*treeish_name
,
880 repo_read_gitmodules(r
, 1);
881 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
885 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
886 * and appends submodule entries to 'out'. The submodule_cache expects
887 * a root-level treeish_name and paths, so keep track of these values
888 * with 'root_tree' and 'prefix'.
890 static void traverse_tree_submodules(struct repository
*r
,
891 const struct object_id
*root_tree
,
893 const struct object_id
*treeish_name
,
894 struct submodule_entry_list
*out
)
896 struct tree_desc tree
;
897 struct submodule_tree_entry
*st_entry
;
898 struct name_entry
*name_entry
;
899 char *tree_path
= NULL
;
901 name_entry
= xmalloc(sizeof(*name_entry
));
903 fill_tree_descriptor(r
, &tree
, treeish_name
);
904 while (tree_entry(&tree
, name_entry
)) {
907 mkpathdup("%s/%s", prefix
, name_entry
->path
);
909 tree_path
= xstrdup(name_entry
->path
);
911 if (S_ISGITLINK(name_entry
->mode
) &&
912 is_tree_submodule_active(r
, root_tree
, tree_path
)) {
913 ALLOC_GROW(out
->entries
, out
->entry_nr
+ 1,
915 st_entry
= &out
->entries
[out
->entry_nr
++];
917 st_entry
->name_entry
= xmalloc(sizeof(*st_entry
->name_entry
));
918 *st_entry
->name_entry
= *name_entry
;
919 st_entry
->submodule
=
920 submodule_from_path(r
, root_tree
, tree_path
);
921 st_entry
->repo
= xmalloc(sizeof(*st_entry
->repo
));
922 if (repo_submodule_init(st_entry
->repo
, r
, tree_path
,
924 FREE_AND_NULL(st_entry
->repo
);
926 } else if (S_ISDIR(name_entry
->mode
))
927 traverse_tree_submodules(r
, root_tree
, tree_path
,
928 &name_entry
->oid
, out
);
933 void submodules_of_tree(struct repository
*r
,
934 const struct object_id
*treeish_name
,
935 struct submodule_entry_list
*out
)
937 CALLOC_ARRAY(out
->entries
, 0);
939 out
->entry_alloc
= 0;
941 traverse_tree_submodules(r
, treeish_name
, NULL
, treeish_name
, out
);
944 void submodule_free(struct repository
*r
)
946 if (r
->submodule_cache
)
947 submodule_cache_clear(r
->submodule_cache
);
950 static int config_print_callback(const char *var
, const char *value
,
951 const struct config_context
*ctx UNUSED
,
954 char *wanted_key
= cb_data
;
956 if (!strcmp(wanted_key
, var
))
957 printf("%s\n", value
);
962 int print_config_from_gitmodules(struct repository
*repo
, const char *key
)
967 ret
= git_config_parse_key(key
, &store_key
, NULL
);
969 return CONFIG_INVALID_KEY
;
971 config_from_gitmodules(config_print_callback
, repo
, store_key
);
977 int config_set_in_gitmodules_file_gently(const char *key
, const char *value
)
981 ret
= git_config_set_in_file_gently(GITMODULES_FILE
, key
, value
);
983 /* Maybe the user already did that, don't error out here */
984 warning(_("Could not update .gitmodules entry %s"), key
);
989 struct fetch_config
{
991 int *recurse_submodules
;
994 static int gitmodules_fetch_config(const char *var
, const char *value
,
995 const struct config_context
*ctx
,
998 struct fetch_config
*config
= cb
;
999 if (!strcmp(var
, "submodule.fetchjobs")) {
1000 if (config
->max_children
)
1001 *(config
->max_children
) =
1002 parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1004 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
1005 if (config
->recurse_submodules
)
1006 *(config
->recurse_submodules
) =
1007 parse_fetch_recurse_submodules_arg(var
, value
);
1014 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
1016 struct fetch_config config
= {
1017 .max_children
= max_children
,
1018 .recurse_submodules
= recurse_submodules
1020 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
1023 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1024 const struct config_context
*ctx
,
1028 if (!strcmp(var
, "submodule.fetchjobs"))
1029 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1033 void update_clone_config_from_gitmodules(int *max_jobs
)
1035 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);