2 #include "repository.h"
4 #include "submodule-config.h"
7 #include "parse-options.h"
10 * submodule cache lookup structure
11 * There is one shared set of 'struct submodule' entries which can be
12 * looked up by their sha1 blob id of the .gitmodule file and either
13 * using path or name as key.
14 * for_path stores submodule entries with path as key
15 * for_name stores submodule entries with name as key
17 struct submodule_cache
{
18 struct hashmap for_path
;
19 struct hashmap for_name
;
20 unsigned initialized
:1;
21 unsigned gitmodules_read
:1;
25 * thin wrapper struct needed to insert 'struct submodule' entries to
28 struct submodule_entry
{
29 struct hashmap_entry ent
;
30 struct submodule
*config
;
38 static int config_path_cmp(const void *unused_cmp_data
,
39 const struct submodule_entry
*a
,
40 const struct submodule_entry
*b
,
41 const void *unused_keydata
)
43 return strcmp(a
->config
->path
, b
->config
->path
) ||
44 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
47 static int config_name_cmp(const void *unused_cmp_data
,
48 const struct submodule_entry
*a
,
49 const struct submodule_entry
*b
,
50 const void *unused_keydata
)
52 return strcmp(a
->config
->name
, b
->config
->name
) ||
53 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
56 static struct submodule_cache
*submodule_cache_alloc(void)
58 return xcalloc(1, sizeof(struct submodule_cache
));
61 static void submodule_cache_init(struct submodule_cache
*cache
)
63 hashmap_init(&cache
->for_path
, (hashmap_cmp_fn
) config_path_cmp
, NULL
, 0);
64 hashmap_init(&cache
->for_name
, (hashmap_cmp_fn
) config_name_cmp
, NULL
, 0);
65 cache
->initialized
= 1;
68 static void free_one_config(struct submodule_entry
*entry
)
70 free((void *) entry
->config
->path
);
71 free((void *) entry
->config
->name
);
72 free((void *) entry
->config
->branch
);
73 free((void *) entry
->config
->update_strategy
.command
);
77 static void submodule_cache_clear(struct submodule_cache
*cache
)
79 struct hashmap_iter iter
;
80 struct submodule_entry
*entry
;
82 if (!cache
->initialized
)
86 * We iterate over the name hash here to be symmetric with the
87 * allocation of struct submodule entries. Each is allocated by
88 * their .gitmodule blob sha1 and submodule name.
90 hashmap_iter_init(&cache
->for_name
, &iter
);
91 while ((entry
= hashmap_iter_next(&iter
)))
92 free_one_config(entry
);
94 hashmap_free(&cache
->for_path
, 1);
95 hashmap_free(&cache
->for_name
, 1);
96 cache
->initialized
= 0;
97 cache
->gitmodules_read
= 0;
100 void submodule_cache_free(struct submodule_cache
*cache
)
102 submodule_cache_clear(cache
);
106 static unsigned int hash_sha1_string(const unsigned char *sha1
,
109 return memhash(sha1
, 20) + strhash(string
);
112 static void cache_put_path(struct submodule_cache
*cache
,
113 struct submodule
*submodule
)
115 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
117 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
118 hashmap_entry_init(e
, hash
);
119 e
->config
= submodule
;
120 hashmap_put(&cache
->for_path
, e
);
123 static void cache_remove_path(struct submodule_cache
*cache
,
124 struct submodule
*submodule
)
126 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
128 struct submodule_entry e
;
129 struct submodule_entry
*removed
;
130 hashmap_entry_init(&e
, hash
);
131 e
.config
= submodule
;
132 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
136 static void cache_add(struct submodule_cache
*cache
,
137 struct submodule
*submodule
)
139 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
141 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
142 hashmap_entry_init(e
, hash
);
143 e
->config
= submodule
;
144 hashmap_add(&cache
->for_name
, e
);
147 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
148 const unsigned char *gitmodules_sha1
, const char *path
)
150 struct submodule_entry
*entry
;
151 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, path
);
152 struct submodule_entry key
;
153 struct submodule key_config
;
155 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
156 key_config
.path
= path
;
158 hashmap_entry_init(&key
, hash
);
159 key
.config
= &key_config
;
161 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
163 return entry
->config
;
167 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
168 const unsigned char *gitmodules_sha1
, const char *name
)
170 struct submodule_entry
*entry
;
171 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, name
);
172 struct submodule_entry key
;
173 struct submodule key_config
;
175 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
176 key_config
.name
= name
;
178 hashmap_entry_init(&key
, hash
);
179 key
.config
= &key_config
;
181 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
183 return entry
->config
;
187 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
190 const char *subsection
, *key
;
191 int subsection_len
, parse
;
192 parse
= parse_config_key(var
, "submodule", &subsection
,
193 &subsection_len
, &key
);
194 if (parse
< 0 || !subsection
)
197 strbuf_add(name
, subsection
, subsection_len
);
198 strbuf_addstr(item
, key
);
203 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
204 const unsigned char *gitmodules_sha1
, const char *name
)
206 struct submodule
*submodule
;
207 struct strbuf name_buf
= STRBUF_INIT
;
209 submodule
= cache_lookup_name(cache
, gitmodules_sha1
, name
);
213 submodule
= xmalloc(sizeof(*submodule
));
215 strbuf_addstr(&name_buf
, name
);
216 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
218 submodule
->path
= NULL
;
219 submodule
->url
= NULL
;
220 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
221 submodule
->update_strategy
.command
= NULL
;
222 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
223 submodule
->ignore
= NULL
;
224 submodule
->branch
= NULL
;
225 submodule
->recommend_shallow
= -1;
227 hashcpy(submodule
->gitmodules_sha1
, gitmodules_sha1
);
229 cache_add(cache
, submodule
);
234 static int parse_fetch_recurse(const char *opt
, const char *arg
,
237 switch (git_config_maybe_bool(opt
, arg
)) {
239 return RECURSE_SUBMODULES_ON
;
241 return RECURSE_SUBMODULES_OFF
;
243 if (!strcmp(arg
, "on-demand"))
244 return RECURSE_SUBMODULES_ON_DEMAND
;
247 die("bad %s argument: %s", opt
, arg
);
249 return RECURSE_SUBMODULES_ERROR
;
253 int parse_submodule_fetchjobs(const char *var
, const char *value
)
255 int fetchjobs
= git_config_int(var
, value
);
257 die(_("negative values not allowed for submodule.fetchjobs"));
261 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
263 return parse_fetch_recurse(opt
, arg
, 1);
266 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
267 const char *arg
, int unset
)
277 *v
= RECURSE_SUBMODULES_OFF
;
280 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
282 *v
= RECURSE_SUBMODULES_ON
;
287 static int parse_update_recurse(const char *opt
, const char *arg
,
290 switch (git_config_maybe_bool(opt
, arg
)) {
292 return RECURSE_SUBMODULES_ON
;
294 return RECURSE_SUBMODULES_OFF
;
297 die("bad %s argument: %s", opt
, arg
);
298 return RECURSE_SUBMODULES_ERROR
;
302 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
304 return parse_update_recurse(opt
, arg
, 1);
307 static int parse_push_recurse(const char *opt
, const char *arg
,
310 switch (git_config_maybe_bool(opt
, arg
)) {
312 /* There's no simple "on" value when pushing */
314 die("bad %s argument: %s", opt
, arg
);
316 return RECURSE_SUBMODULES_ERROR
;
318 return RECURSE_SUBMODULES_OFF
;
320 if (!strcmp(arg
, "on-demand"))
321 return RECURSE_SUBMODULES_ON_DEMAND
;
322 else if (!strcmp(arg
, "check"))
323 return RECURSE_SUBMODULES_CHECK
;
324 else if (!strcmp(arg
, "only"))
325 return RECURSE_SUBMODULES_ONLY
;
326 else if (die_on_error
)
327 die("bad %s argument: %s", opt
, arg
);
329 return RECURSE_SUBMODULES_ERROR
;
333 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
335 return parse_push_recurse(opt
, arg
, 1);
338 static void warn_multiple_config(const unsigned char *treeish_name
,
339 const char *name
, const char *option
)
341 const char *commit_string
= "WORKTREE";
343 commit_string
= sha1_to_hex(treeish_name
);
344 warning("%s:.gitmodules, multiple configurations found for "
345 "'submodule.%s.%s'. Skipping second one!",
346 commit_string
, name
, option
);
349 struct parse_config_parameter
{
350 struct submodule_cache
*cache
;
351 const unsigned char *treeish_name
;
352 const unsigned char *gitmodules_sha1
;
356 static int parse_config(const char *var
, const char *value
, void *data
)
358 struct parse_config_parameter
*me
= data
;
359 struct submodule
*submodule
;
360 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
363 /* this also ensures that we only parse submodule entries */
364 if (!name_and_item_from_var(var
, &name
, &item
))
367 submodule
= lookup_or_create_by_name(me
->cache
,
371 if (!strcmp(item
.buf
, "path")) {
373 ret
= config_error_nonbool(var
);
374 else if (!me
->overwrite
&& submodule
->path
)
375 warn_multiple_config(me
->treeish_name
, submodule
->name
,
379 cache_remove_path(me
->cache
, submodule
);
380 free((void *) submodule
->path
);
381 submodule
->path
= xstrdup(value
);
382 cache_put_path(me
->cache
, submodule
);
384 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
385 /* when parsing worktree configurations we can die early */
386 int die_on_error
= is_null_sha1(me
->gitmodules_sha1
);
387 if (!me
->overwrite
&&
388 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
389 warn_multiple_config(me
->treeish_name
, submodule
->name
,
390 "fetchrecursesubmodules");
392 submodule
->fetch_recurse
= parse_fetch_recurse(
395 } else if (!strcmp(item
.buf
, "ignore")) {
397 ret
= config_error_nonbool(var
);
398 else if (!me
->overwrite
&& submodule
->ignore
)
399 warn_multiple_config(me
->treeish_name
, submodule
->name
,
401 else if (strcmp(value
, "untracked") &&
402 strcmp(value
, "dirty") &&
403 strcmp(value
, "all") &&
404 strcmp(value
, "none"))
405 warning("Invalid parameter '%s' for config option "
406 "'submodule.%s.ignore'", value
, name
.buf
);
408 free((void *) submodule
->ignore
);
409 submodule
->ignore
= xstrdup(value
);
411 } else if (!strcmp(item
.buf
, "url")) {
413 ret
= config_error_nonbool(var
);
414 } else if (!me
->overwrite
&& submodule
->url
) {
415 warn_multiple_config(me
->treeish_name
, submodule
->name
,
418 free((void *) submodule
->url
);
419 submodule
->url
= xstrdup(value
);
421 } else if (!strcmp(item
.buf
, "update")) {
423 ret
= config_error_nonbool(var
);
424 else if (!me
->overwrite
&&
425 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
426 warn_multiple_config(me
->treeish_name
, submodule
->name
,
428 else if (parse_submodule_update_strategy(value
,
429 &submodule
->update_strategy
) < 0)
430 die(_("invalid value for %s"), var
);
431 } else if (!strcmp(item
.buf
, "shallow")) {
432 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
433 warn_multiple_config(me
->treeish_name
, submodule
->name
,
436 submodule
->recommend_shallow
=
437 git_config_bool(var
, value
);
438 } else if (!strcmp(item
.buf
, "branch")) {
439 if (!me
->overwrite
&& submodule
->branch
)
440 warn_multiple_config(me
->treeish_name
, submodule
->name
,
443 free((void *)submodule
->branch
);
444 submodule
->branch
= xstrdup(value
);
448 strbuf_release(&name
);
449 strbuf_release(&item
);
454 static int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
455 struct object_id
*gitmodules_oid
,
460 if (is_null_oid(treeish_name
)) {
461 oidclr(gitmodules_oid
);
465 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
466 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
472 /* This does a lookup of a submodule configuration by name or by path
473 * (key) with on-demand reading of the appropriate .gitmodules from
476 static const struct submodule
*config_from(struct submodule_cache
*cache
,
477 const struct object_id
*treeish_name
, const char *key
,
478 enum lookup_type lookup_type
)
480 struct strbuf rev
= STRBUF_INIT
;
481 unsigned long config_size
;
483 struct object_id oid
;
484 enum object_type type
;
485 const struct submodule
*submodule
= NULL
;
486 struct parse_config_parameter parameter
;
489 * If any parameter except the cache is a NULL pointer just
490 * return the first submodule. Can be used to check whether
491 * there are any submodules parsed.
493 if (!treeish_name
|| !key
) {
494 struct hashmap_iter iter
;
495 struct submodule_entry
*entry
;
497 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
500 return entry
->config
;
503 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
506 switch (lookup_type
) {
508 submodule
= cache_lookup_name(cache
, oid
.hash
, key
);
511 submodule
= cache_lookup_path(cache
, oid
.hash
, key
);
517 config
= read_sha1_file(oid
.hash
, &type
, &config_size
);
518 if (!config
|| type
!= OBJ_BLOB
)
521 /* fill the submodule config into the cache */
522 parameter
.cache
= cache
;
523 parameter
.treeish_name
= treeish_name
->hash
;
524 parameter
.gitmodules_sha1
= oid
.hash
;
525 parameter
.overwrite
= 0;
526 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
527 config
, config_size
, ¶meter
);
528 strbuf_release(&rev
);
531 switch (lookup_type
) {
533 return cache_lookup_name(cache
, oid
.hash
, key
);
535 return cache_lookup_path(cache
, oid
.hash
, key
);
541 strbuf_release(&rev
);
546 static void submodule_cache_check_init(struct repository
*repo
)
548 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
551 if (!repo
->submodule_cache
)
552 repo
->submodule_cache
= submodule_cache_alloc();
554 submodule_cache_init(repo
->submodule_cache
);
557 static int gitmodules_cb(const char *var
, const char *value
, void *data
)
559 struct repository
*repo
= data
;
560 struct parse_config_parameter parameter
;
562 parameter
.cache
= repo
->submodule_cache
;
563 parameter
.treeish_name
= NULL
;
564 parameter
.gitmodules_sha1
= null_sha1
;
565 parameter
.overwrite
= 1;
567 return parse_config(var
, value
, ¶meter
);
570 void repo_read_gitmodules(struct repository
*repo
)
572 submodule_cache_check_init(repo
);
574 if (repo
->worktree
) {
577 if (repo_read_index(repo
) < 0)
580 gitmodules
= repo_worktree_path(repo
, GITMODULES_FILE
);
582 if (!is_gitmodules_unmerged(repo
->index
))
583 git_config_from_file(gitmodules_cb
, gitmodules
, repo
);
588 repo
->submodule_cache
->gitmodules_read
= 1;
591 void gitmodules_config_oid(const struct object_id
*commit_oid
)
593 struct strbuf rev
= STRBUF_INIT
;
594 struct object_id oid
;
596 submodule_cache_check_init(the_repository
);
598 if (gitmodule_oid_from_commit(commit_oid
, &oid
, &rev
)) {
599 git_config_from_blob_oid(gitmodules_cb
, rev
.buf
,
600 &oid
, the_repository
);
602 strbuf_release(&rev
);
604 the_repository
->submodule_cache
->gitmodules_read
= 1;
607 static void gitmodules_read_check(struct repository
*repo
)
609 submodule_cache_check_init(repo
);
611 /* read the repo's .gitmodules file if it hasn't been already */
612 if (!repo
->submodule_cache
->gitmodules_read
)
613 repo_read_gitmodules(repo
);
616 const struct submodule
*submodule_from_name(const struct object_id
*treeish_name
,
619 gitmodules_read_check(the_repository
);
620 return config_from(the_repository
->submodule_cache
, treeish_name
, name
, lookup_name
);
623 const struct submodule
*submodule_from_path(const struct object_id
*treeish_name
,
626 gitmodules_read_check(the_repository
);
627 return config_from(the_repository
->submodule_cache
, treeish_name
, path
, lookup_path
);
630 const struct submodule
*submodule_from_cache(struct repository
*repo
,
631 const struct object_id
*treeish_name
,
634 gitmodules_read_check(repo
);
635 return config_from(repo
->submodule_cache
, treeish_name
,
639 void submodule_free(void)
641 if (the_repository
->submodule_cache
)
642 submodule_cache_clear(the_repository
->submodule_cache
);