1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
9 #include "repository.h"
11 #include "submodule-config.h"
12 #include "submodule.h"
14 #include "object-name.h"
15 #include "object-store-ll.h"
16 #include "parse-options.h"
17 #include "thread-utils.h"
18 #include "tree-walk.h"
23 * submodule cache lookup structure
24 * There is one shared set of 'struct submodule' entries which can be
25 * looked up by their sha1 blob id of the .gitmodules file and either
26 * using path or name as key.
27 * for_path stores submodule entries with path as key
28 * for_name stores submodule entries with name as key
30 struct submodule_cache
{
31 struct hashmap for_path
;
32 struct hashmap for_name
;
33 unsigned initialized
:1;
34 unsigned gitmodules_read
:1;
38 * thin wrapper struct needed to insert 'struct submodule' entries to
41 struct submodule_entry
{
42 struct hashmap_entry ent
;
43 struct submodule
*config
;
51 static int config_path_cmp(const void *cmp_data UNUSED
,
52 const struct hashmap_entry
*eptr
,
53 const struct hashmap_entry
*entry_or_key
,
54 const void *keydata UNUSED
)
56 const struct submodule_entry
*a
, *b
;
58 a
= container_of(eptr
, const struct submodule_entry
, ent
);
59 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
61 return strcmp(a
->config
->path
, b
->config
->path
) ||
62 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
65 static int config_name_cmp(const void *cmp_data UNUSED
,
66 const struct hashmap_entry
*eptr
,
67 const struct hashmap_entry
*entry_or_key
,
68 const void *keydata UNUSED
)
70 const struct submodule_entry
*a
, *b
;
72 a
= container_of(eptr
, const struct submodule_entry
, ent
);
73 b
= container_of(entry_or_key
, const struct submodule_entry
, ent
);
75 return strcmp(a
->config
->name
, b
->config
->name
) ||
76 !oideq(&a
->config
->gitmodules_oid
, &b
->config
->gitmodules_oid
);
79 static struct submodule_cache
*submodule_cache_alloc(void)
81 return xcalloc(1, sizeof(struct submodule_cache
));
84 static void submodule_cache_init(struct submodule_cache
*cache
)
86 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
87 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
88 cache
->initialized
= 1;
91 static void free_one_config(struct submodule_entry
*entry
)
93 free((void *) entry
->config
->path
);
94 free((void *) entry
->config
->name
);
95 free((void *) entry
->config
->branch
);
96 free((void *) entry
->config
->url
);
97 free((void *) entry
->config
->ignore
);
98 free((void *) entry
->config
->update_strategy
.command
);
102 static void submodule_cache_clear(struct submodule_cache
*cache
)
104 struct hashmap_iter iter
;
105 struct submodule_entry
*entry
;
107 if (!cache
->initialized
)
111 * We iterate over the name hash here to be symmetric with the
112 * allocation of struct submodule entries. Each is allocated by
113 * their .gitmodules blob sha1 and submodule name.
115 hashmap_for_each_entry(&cache
->for_name
, &iter
, entry
,
116 ent
/* member name */)
117 free_one_config(entry
);
119 hashmap_clear_and_free(&cache
->for_path
, struct submodule_entry
, ent
);
120 hashmap_clear_and_free(&cache
->for_name
, struct submodule_entry
, ent
);
121 cache
->initialized
= 0;
122 cache
->gitmodules_read
= 0;
125 void submodule_cache_free(struct submodule_cache
*cache
)
127 submodule_cache_clear(cache
);
131 static unsigned int hash_oid_string(const struct object_id
*oid
,
134 return memhash(oid
->hash
, the_hash_algo
->rawsz
) + strhash(string
);
137 static void cache_put_path(struct submodule_cache
*cache
,
138 struct submodule
*submodule
)
140 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
142 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
143 hashmap_entry_init(&e
->ent
, hash
);
144 e
->config
= submodule
;
145 hashmap_put(&cache
->for_path
, &e
->ent
);
148 static void cache_remove_path(struct submodule_cache
*cache
,
149 struct submodule
*submodule
)
151 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
153 struct submodule_entry e
;
154 struct submodule_entry
*removed
;
155 hashmap_entry_init(&e
.ent
, hash
);
156 e
.config
= submodule
;
157 removed
= hashmap_remove_entry(&cache
->for_path
, &e
, ent
, NULL
);
161 static void cache_add(struct submodule_cache
*cache
,
162 struct submodule
*submodule
)
164 unsigned int hash
= hash_oid_string(&submodule
->gitmodules_oid
,
166 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
167 hashmap_entry_init(&e
->ent
, hash
);
168 e
->config
= submodule
;
169 hashmap_add(&cache
->for_name
, &e
->ent
);
172 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
173 const struct object_id
*gitmodules_oid
, const char *path
)
175 struct submodule_entry
*entry
;
176 unsigned int hash
= hash_oid_string(gitmodules_oid
, path
);
177 struct submodule_entry key
;
178 struct submodule key_config
;
180 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
181 key_config
.path
= path
;
183 hashmap_entry_init(&key
.ent
, hash
);
184 key
.config
= &key_config
;
186 entry
= hashmap_get_entry(&cache
->for_path
, &key
, ent
, NULL
);
188 return entry
->config
;
192 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
193 const struct object_id
*gitmodules_oid
, const char *name
)
195 struct submodule_entry
*entry
;
196 unsigned int hash
= hash_oid_string(gitmodules_oid
, name
);
197 struct submodule_entry key
;
198 struct submodule key_config
;
200 oidcpy(&key_config
.gitmodules_oid
, gitmodules_oid
);
201 key_config
.name
= name
;
203 hashmap_entry_init(&key
.ent
, hash
);
204 key
.config
= &key_config
;
206 entry
= hashmap_get_entry(&cache
->for_name
, &key
, ent
, NULL
);
208 return entry
->config
;
212 int check_submodule_name(const char *name
)
214 /* Disallow empty names */
219 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
220 * separators rather than is_dir_sep(), because we want the name rules
221 * to be consistent across platforms.
223 goto in_component
; /* always start inside component */
226 if (is_xplatform_dir_sep(c
)) {
228 if (name
[0] == '.' && name
[1] == '.' &&
229 (!name
[2] || is_xplatform_dir_sep(name
[2])))
237 static int starts_with_dot_slash(const char *const path
)
239 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
240 PATH_MATCH_XPLATFORM
);
243 static int starts_with_dot_dot_slash(const char *const path
)
245 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
246 PATH_MATCH_XPLATFORM
);
249 static int submodule_url_is_relative(const char *url
)
251 return starts_with_dot_slash(url
) || starts_with_dot_dot_slash(url
);
255 * Count directory components that a relative submodule URL should chop
256 * from the remote_url it is to be resolved against.
258 * In other words, this counts "../" components at the start of a
261 * Returns the number of directory components to chop and writes a
262 * pointer to the next character of url after all leading "./" and
263 * "../" components to out.
265 static int count_leading_dotdots(const char *url
, const char **out
)
269 if (starts_with_dot_dot_slash(url
)) {
271 url
+= strlen("../");
274 if (starts_with_dot_slash(url
)) {
283 * Check whether a transport is implemented by git-remote-curl.
285 * If it is, returns 1 and writes the URL that would be passed to
286 * git-remote-curl to the "out" parameter.
288 * Otherwise, returns 0 and leaves "out" untouched.
291 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
292 * https://example.com/repo.git -> 1, https://example.com/repo.git
293 * git://example.com/repo.git -> 0
295 * This is for use in checking for previously exploitable bugs that
296 * required a submodule URL to be passed to git-remote-curl.
298 static int url_to_curl_url(const char *url
, const char **out
)
301 * We don't need to check for case-aliases, "http.exe", and so
302 * on because in the default configuration, is_transport_allowed
303 * prevents URLs with those schemes from being cloned
306 if (skip_prefix(url
, "http::", out
) ||
307 skip_prefix(url
, "https::", out
) ||
308 skip_prefix(url
, "ftp::", out
) ||
309 skip_prefix(url
, "ftps::", out
))
311 if (starts_with(url
, "http://") ||
312 starts_with(url
, "https://") ||
313 starts_with(url
, "ftp://") ||
314 starts_with(url
, "ftps://")) {
321 int check_submodule_url(const char *url
)
323 const char *curl_url
;
325 if (looks_like_command_line_option(url
))
328 if (submodule_url_is_relative(url
) || starts_with(url
, "git://")) {
334 * This could be appended to an http URL and url-decoded;
335 * check for malicious characters.
337 decoded
= url_decode(url
);
338 has_nl
= !!strchr(decoded
, '\n');
345 * URLs which escape their root via "../" can overwrite
346 * the host field and previous components, resolving to
347 * URLs like https::example.com/submodule.git and
348 * https:///example.com/submodule.git that were
349 * susceptible to CVE-2020-11008.
351 if (count_leading_dotdots(url
, &next
) > 0 &&
352 (*next
== ':' || *next
== '/'))
356 else if (url_to_curl_url(url
, &curl_url
)) {
358 char *normalized
= url_normalize(curl_url
, NULL
);
360 char *decoded
= url_decode(normalized
);
361 if (strchr(decoded
, '\n'))
375 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
378 const char *subsection
, *key
;
379 size_t subsection_len
;
381 parse
= parse_config_key(var
, "submodule", &subsection
,
382 &subsection_len
, &key
);
383 if (parse
< 0 || !subsection
)
386 strbuf_add(name
, subsection
, subsection_len
);
387 if (check_submodule_name(name
->buf
) < 0) {
388 warning(_("ignoring suspicious submodule name: %s"), name
->buf
);
389 strbuf_release(name
);
393 strbuf_addstr(item
, key
);
398 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
399 const struct object_id
*gitmodules_oid
, const char *name
)
401 struct submodule
*submodule
;
402 struct strbuf name_buf
= STRBUF_INIT
;
404 submodule
= cache_lookup_name(cache
, gitmodules_oid
, name
);
408 submodule
= xmalloc(sizeof(*submodule
));
410 strbuf_addstr(&name_buf
, name
);
411 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
413 submodule
->path
= NULL
;
414 submodule
->url
= NULL
;
415 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
416 submodule
->update_strategy
.command
= NULL
;
417 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
418 submodule
->ignore
= NULL
;
419 submodule
->branch
= NULL
;
420 submodule
->recommend_shallow
= -1;
422 oidcpy(&submodule
->gitmodules_oid
, gitmodules_oid
);
424 cache_add(cache
, submodule
);
429 static int parse_fetch_recurse(const char *opt
, const char *arg
,
432 switch (git_parse_maybe_bool(arg
)) {
434 return RECURSE_SUBMODULES_ON
;
436 return RECURSE_SUBMODULES_OFF
;
438 if (!strcmp(arg
, "on-demand"))
439 return RECURSE_SUBMODULES_ON_DEMAND
;
441 * Please update $__git_fetch_recurse_submodules in
442 * git-completion.bash when you add new options.
445 die("bad %s argument: %s", opt
, arg
);
447 return RECURSE_SUBMODULES_ERROR
;
451 int parse_submodule_fetchjobs(const char *var
, const char *value
,
452 const struct key_value_info
*kvi
)
454 int fetchjobs
= git_config_int(var
, value
, kvi
);
456 die(_("negative values not allowed for submodule.fetchJobs"));
458 fetchjobs
= online_cpus();
462 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
464 return parse_fetch_recurse(opt
, arg
, 1);
467 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
468 const char *arg
, int unset
)
478 *v
= RECURSE_SUBMODULES_OFF
;
481 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
483 *v
= RECURSE_SUBMODULES_ON
;
488 static int parse_update_recurse(const char *opt
, const char *arg
,
491 switch (git_parse_maybe_bool(arg
)) {
493 return RECURSE_SUBMODULES_ON
;
495 return RECURSE_SUBMODULES_OFF
;
498 die("bad %s argument: %s", opt
, arg
);
499 return RECURSE_SUBMODULES_ERROR
;
503 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
505 return parse_update_recurse(opt
, arg
, 1);
508 static int parse_push_recurse(const char *opt
, const char *arg
,
511 switch (git_parse_maybe_bool(arg
)) {
513 /* There's no simple "on" value when pushing */
515 die("bad %s argument: %s", opt
, arg
);
517 return RECURSE_SUBMODULES_ERROR
;
519 return RECURSE_SUBMODULES_OFF
;
521 if (!strcmp(arg
, "on-demand"))
522 return RECURSE_SUBMODULES_ON_DEMAND
;
523 else if (!strcmp(arg
, "check"))
524 return RECURSE_SUBMODULES_CHECK
;
525 else if (!strcmp(arg
, "only"))
526 return RECURSE_SUBMODULES_ONLY
;
528 * Please update $__git_push_recurse_submodules in
529 * git-completion.bash when you add new modes.
531 else if (die_on_error
)
532 die("bad %s argument: %s", opt
, arg
);
534 return RECURSE_SUBMODULES_ERROR
;
538 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
540 return parse_push_recurse(opt
, arg
, 1);
543 static void warn_multiple_config(const struct object_id
*treeish_name
,
544 const char *name
, const char *option
)
546 const char *commit_string
= "WORKTREE";
548 commit_string
= oid_to_hex(treeish_name
);
549 warning("%s:.gitmodules, multiple configurations found for "
550 "'submodule.%s.%s'. Skipping second one!",
551 commit_string
, name
, option
);
554 static void warn_command_line_option(const char *var
, const char *value
)
556 warning(_("ignoring '%s' which may be interpreted as"
557 " a command-line option: %s"), var
, value
);
560 struct parse_config_parameter
{
561 struct submodule_cache
*cache
;
562 const struct object_id
*treeish_name
;
563 const struct object_id
*gitmodules_oid
;
568 * Parse a config item from .gitmodules.
570 * This does not handle submodule-related configuration from the main
571 * config store (.git/config, etc). Callers are responsible for
572 * checking for overrides in the main config store when appropriate.
574 static int parse_config(const char *var
, const char *value
,
575 const struct config_context
*ctx UNUSED
, void *data
)
577 struct parse_config_parameter
*me
= data
;
578 struct submodule
*submodule
;
579 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
582 /* this also ensures that we only parse submodule entries */
583 if (!name_and_item_from_var(var
, &name
, &item
))
586 submodule
= lookup_or_create_by_name(me
->cache
,
590 if (!strcmp(item
.buf
, "path")) {
592 ret
= config_error_nonbool(var
);
593 else if (looks_like_command_line_option(value
))
594 warn_command_line_option(var
, value
);
595 else if (!me
->overwrite
&& submodule
->path
)
596 warn_multiple_config(me
->treeish_name
, submodule
->name
,
600 cache_remove_path(me
->cache
, submodule
);
601 free((void *) submodule
->path
);
602 submodule
->path
= xstrdup(value
);
603 cache_put_path(me
->cache
, submodule
);
605 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
606 /* when parsing worktree configurations we can die early */
607 int die_on_error
= is_null_oid(me
->gitmodules_oid
);
608 if (!me
->overwrite
&&
609 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
610 warn_multiple_config(me
->treeish_name
, submodule
->name
,
611 "fetchrecursesubmodules");
613 submodule
->fetch_recurse
= parse_fetch_recurse(
616 } else if (!strcmp(item
.buf
, "ignore")) {
618 ret
= config_error_nonbool(var
);
619 else if (!me
->overwrite
&& submodule
->ignore
)
620 warn_multiple_config(me
->treeish_name
, submodule
->name
,
622 else if (strcmp(value
, "untracked") &&
623 strcmp(value
, "dirty") &&
624 strcmp(value
, "all") &&
625 strcmp(value
, "none"))
626 warning("Invalid parameter '%s' for config option "
627 "'submodule.%s.ignore'", value
, name
.buf
);
629 free((void *) submodule
->ignore
);
630 submodule
->ignore
= xstrdup(value
);
632 } else if (!strcmp(item
.buf
, "url")) {
634 ret
= config_error_nonbool(var
);
635 } else if (looks_like_command_line_option(value
)) {
636 warn_command_line_option(var
, value
);
637 } else if (!me
->overwrite
&& submodule
->url
) {
638 warn_multiple_config(me
->treeish_name
, submodule
->name
,
641 free((void *) submodule
->url
);
642 submodule
->url
= xstrdup(value
);
644 } else if (!strcmp(item
.buf
, "update")) {
646 ret
= config_error_nonbool(var
);
647 else if (!me
->overwrite
&&
648 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
649 warn_multiple_config(me
->treeish_name
, submodule
->name
,
651 else if (parse_submodule_update_strategy(value
,
652 &submodule
->update_strategy
) < 0 ||
653 submodule
->update_strategy
.type
== SM_UPDATE_COMMAND
)
654 die(_("invalid value for '%s'"), var
);
655 } else if (!strcmp(item
.buf
, "shallow")) {
656 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
657 warn_multiple_config(me
->treeish_name
, submodule
->name
,
660 submodule
->recommend_shallow
=
661 git_config_bool(var
, value
);
662 } else if (!strcmp(item
.buf
, "branch")) {
664 ret
= config_error_nonbool(var
);
665 else if (!me
->overwrite
&& submodule
->branch
)
666 warn_multiple_config(me
->treeish_name
, submodule
->name
,
669 free((void *)submodule
->branch
);
670 submodule
->branch
= xstrdup(value
);
674 strbuf_release(&name
);
675 strbuf_release(&item
);
680 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
681 struct object_id
*gitmodules_oid
,
686 if (is_null_oid(treeish_name
)) {
687 oidclr(gitmodules_oid
, the_repository
->hash_algo
);
691 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
692 if (repo_get_oid(the_repository
, rev
->buf
, gitmodules_oid
) >= 0)
698 /* This does a lookup of a submodule configuration by name or by path
699 * (key) with on-demand reading of the appropriate .gitmodules from
702 static const struct submodule
*config_from(struct submodule_cache
*cache
,
703 const struct object_id
*treeish_name
, const char *key
,
704 enum lookup_type lookup_type
)
706 struct strbuf rev
= STRBUF_INIT
;
707 unsigned long config_size
;
709 struct object_id oid
;
710 enum object_type type
;
711 const struct submodule
*submodule
= NULL
;
712 struct parse_config_parameter parameter
;
715 * If any parameter except the cache is a NULL pointer just
716 * return the first submodule. Can be used to check whether
717 * there are any submodules parsed.
719 if (!treeish_name
|| !key
) {
720 struct hashmap_iter iter
;
721 struct submodule_entry
*entry
;
723 entry
= hashmap_iter_first_entry(&cache
->for_name
, &iter
,
724 struct submodule_entry
,
725 ent
/* member name */);
728 return entry
->config
;
731 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
734 switch (lookup_type
) {
736 submodule
= cache_lookup_name(cache
, &oid
, key
);
739 submodule
= cache_lookup_path(cache
, &oid
, key
);
745 config
= repo_read_object_file(the_repository
, &oid
, &type
,
747 if (!config
|| type
!= OBJ_BLOB
)
750 /* fill the submodule config into the cache */
751 parameter
.cache
= cache
;
752 parameter
.treeish_name
= treeish_name
;
753 parameter
.gitmodules_oid
= &oid
;
754 parameter
.overwrite
= 0;
755 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
756 config
, config_size
, ¶meter
, CONFIG_SCOPE_UNKNOWN
, NULL
);
757 strbuf_release(&rev
);
760 switch (lookup_type
) {
762 return cache_lookup_name(cache
, &oid
, key
);
764 return cache_lookup_path(cache
, &oid
, key
);
770 strbuf_release(&rev
);
775 static void submodule_cache_check_init(struct repository
*repo
)
777 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
780 if (!repo
->submodule_cache
)
781 repo
->submodule_cache
= submodule_cache_alloc();
783 submodule_cache_init(repo
->submodule_cache
);
787 * Note: This function is private for a reason, the '.gitmodules' file should
788 * not be used as a mechanism to retrieve arbitrary configuration stored in
791 * Runs the provided config function on the '.gitmodules' file found in the
794 static void config_from_gitmodules(config_fn_t fn
, struct repository
*repo
, void *data
)
796 if (repo
->worktree
) {
797 struct git_config_source config_source
= {
798 0, .scope
= CONFIG_SCOPE_SUBMODULE
800 const struct config_options opts
= { 0 };
801 struct object_id oid
;
805 file
= repo_worktree_path(repo
, GITMODULES_FILE
);
806 if (file_exists(file
)) {
807 config_source
.file
= file
;
808 } else if (repo_get_oid(repo
, GITMODULES_INDEX
, &oid
) >= 0 ||
809 repo_get_oid(repo
, GITMODULES_HEAD
, &oid
) >= 0) {
810 config_source
.blob
= oidstr
= xstrdup(oid_to_hex(&oid
));
811 if (repo
!= the_repository
)
812 add_submodule_odb_by_path(repo
->objects
->odb
->path
);
817 config_with_options(fn
, data
, &config_source
, repo
, &opts
);
825 static int gitmodules_cb(const char *var
, const char *value
,
826 const struct config_context
*ctx
, void *data
)
828 struct repository
*repo
= data
;
829 struct parse_config_parameter parameter
;
831 parameter
.cache
= repo
->submodule_cache
;
832 parameter
.treeish_name
= NULL
;
833 parameter
.gitmodules_oid
= null_oid();
834 parameter
.overwrite
= 1;
836 return parse_config(var
, value
, ctx
, ¶meter
);
839 void repo_read_gitmodules(struct repository
*repo
, int skip_if_read
)
841 submodule_cache_check_init(repo
);
843 if (repo
->submodule_cache
->gitmodules_read
&& skip_if_read
)
846 if (repo_read_index(repo
) < 0)
849 if (!is_gitmodules_unmerged(repo
->index
))
850 config_from_gitmodules(gitmodules_cb
, repo
, repo
);
852 repo
->submodule_cache
->gitmodules_read
= 1;
855 void gitmodules_config_oid(const struct object_id
*commit_oid
)
857 struct strbuf rev
= STRBUF_INIT
;
858 struct object_id oid
;
860 submodule_cache_check_init(the_repository
);
862 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
863 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
864 the_repository
, &oid
, the_repository
,
865 CONFIG_SCOPE_UNKNOWN
);
867 strbuf_release(&rev
);
869 the_repository
->submodule_cache
->gitmodules_read
= 1;
872 const struct submodule
*submodule_from_name(struct repository
*r
,
873 const struct object_id
*treeish_name
,
876 repo_read_gitmodules(r
, 1);
877 return config_from(r
->submodule_cache
, treeish_name
, name
, lookup_name
);
880 const struct submodule
*submodule_from_path(struct repository
*r
,
881 const struct object_id
*treeish_name
,
884 repo_read_gitmodules(r
, 1);
885 return config_from(r
->submodule_cache
, treeish_name
, path
, lookup_path
);
889 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
890 * and appends submodule entries to 'out'. The submodule_cache expects
891 * a root-level treeish_name and paths, so keep track of these values
892 * with 'root_tree' and 'prefix'.
894 static void traverse_tree_submodules(struct repository
*r
,
895 const struct object_id
*root_tree
,
897 const struct object_id
*treeish_name
,
898 struct submodule_entry_list
*out
)
900 struct tree_desc tree
;
901 struct submodule_tree_entry
*st_entry
;
902 struct name_entry
*name_entry
;
903 char *tree_path
= NULL
;
905 name_entry
= xmalloc(sizeof(*name_entry
));
907 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
);
937 void submodules_of_tree(struct repository
*r
,
938 const struct object_id
*treeish_name
,
939 struct submodule_entry_list
*out
)
941 CALLOC_ARRAY(out
->entries
, 0);
943 out
->entry_alloc
= 0;
945 traverse_tree_submodules(r
, treeish_name
, NULL
, treeish_name
, out
);
948 void submodule_free(struct repository
*r
)
950 if (r
->submodule_cache
)
951 submodule_cache_clear(r
->submodule_cache
);
954 static int config_print_callback(const char *var
, const char *value
,
955 const struct config_context
*ctx UNUSED
,
958 char *wanted_key
= cb_data
;
960 if (!strcmp(wanted_key
, var
))
961 printf("%s\n", value
);
966 int print_config_from_gitmodules(struct repository
*repo
, const char *key
)
971 ret
= git_config_parse_key(key
, &store_key
, NULL
);
973 return CONFIG_INVALID_KEY
;
975 config_from_gitmodules(config_print_callback
, repo
, store_key
);
981 int config_set_in_gitmodules_file_gently(const char *key
, const char *value
)
985 ret
= git_config_set_in_file_gently(GITMODULES_FILE
, key
, NULL
, value
);
987 /* Maybe the user already did that, don't error out here */
988 warning(_("Could not update .gitmodules entry %s"), key
);
993 struct fetch_config
{
995 int *recurse_submodules
;
998 static int gitmodules_fetch_config(const char *var
, const char *value
,
999 const struct config_context
*ctx
,
1002 struct fetch_config
*config
= cb
;
1003 if (!strcmp(var
, "submodule.fetchjobs")) {
1004 if (config
->max_children
)
1005 *(config
->max_children
) =
1006 parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1008 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
1009 if (config
->recurse_submodules
)
1010 *(config
->recurse_submodules
) =
1011 parse_fetch_recurse_submodules_arg(var
, value
);
1018 void fetch_config_from_gitmodules(int *max_children
, int *recurse_submodules
)
1020 struct fetch_config config
= {
1021 .max_children
= max_children
,
1022 .recurse_submodules
= recurse_submodules
1024 config_from_gitmodules(gitmodules_fetch_config
, the_repository
, &config
);
1027 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1028 const struct config_context
*ctx
,
1032 if (!strcmp(var
, "submodule.fetchjobs"))
1033 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
1037 void update_clone_config_from_gitmodules(int *max_jobs
)
1039 config_from_gitmodules(gitmodules_update_clone_config
, the_repository
, &max_jobs
);