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;
24 * thin wrapper struct needed to insert 'struct submodule' entries to
27 struct submodule_entry
{
28 struct hashmap_entry ent
;
29 struct submodule
*config
;
37 static int config_path_cmp(const void *unused_cmp_data
,
39 const void *entry_or_key
,
40 const void *unused_keydata
)
42 const struct submodule_entry
*a
= entry
;
43 const struct submodule_entry
*b
= entry_or_key
;
45 return strcmp(a
->config
->path
, b
->config
->path
) ||
46 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
49 static int config_name_cmp(const void *unused_cmp_data
,
51 const void *entry_or_key
,
52 const void *unused_keydata
)
54 const struct submodule_entry
*a
= entry
;
55 const struct submodule_entry
*b
= entry_or_key
;
57 return strcmp(a
->config
->name
, b
->config
->name
) ||
58 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
61 static struct submodule_cache
*submodule_cache_alloc(void)
63 return xcalloc(1, sizeof(struct submodule_cache
));
66 static void submodule_cache_init(struct submodule_cache
*cache
)
68 hashmap_init(&cache
->for_path
, config_path_cmp
, NULL
, 0);
69 hashmap_init(&cache
->for_name
, config_name_cmp
, NULL
, 0);
70 cache
->initialized
= 1;
73 static void free_one_config(struct submodule_entry
*entry
)
75 free((void *) entry
->config
->path
);
76 free((void *) entry
->config
->name
);
77 free((void *) entry
->config
->branch
);
78 free((void *) entry
->config
->update_strategy
.command
);
82 static void submodule_cache_clear(struct submodule_cache
*cache
)
84 struct hashmap_iter iter
;
85 struct submodule_entry
*entry
;
87 if (!cache
->initialized
)
91 * We iterate over the name hash here to be symmetric with the
92 * allocation of struct submodule entries. Each is allocated by
93 * their .gitmodule blob sha1 and submodule name.
95 hashmap_iter_init(&cache
->for_name
, &iter
);
96 while ((entry
= hashmap_iter_next(&iter
)))
97 free_one_config(entry
);
99 hashmap_free(&cache
->for_path
, 1);
100 hashmap_free(&cache
->for_name
, 1);
101 cache
->initialized
= 0;
104 void submodule_cache_free(struct submodule_cache
*cache
)
106 submodule_cache_clear(cache
);
110 static unsigned int hash_sha1_string(const unsigned char *sha1
,
113 return memhash(sha1
, 20) + strhash(string
);
116 static void cache_put_path(struct submodule_cache
*cache
,
117 struct submodule
*submodule
)
119 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
121 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
122 hashmap_entry_init(e
, hash
);
123 e
->config
= submodule
;
124 hashmap_put(&cache
->for_path
, e
);
127 static void cache_remove_path(struct submodule_cache
*cache
,
128 struct submodule
*submodule
)
130 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
132 struct submodule_entry e
;
133 struct submodule_entry
*removed
;
134 hashmap_entry_init(&e
, hash
);
135 e
.config
= submodule
;
136 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
140 static void cache_add(struct submodule_cache
*cache
,
141 struct submodule
*submodule
)
143 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
145 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
146 hashmap_entry_init(e
, hash
);
147 e
->config
= submodule
;
148 hashmap_add(&cache
->for_name
, e
);
151 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
152 const unsigned char *gitmodules_sha1
, const char *path
)
154 struct submodule_entry
*entry
;
155 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, path
);
156 struct submodule_entry key
;
157 struct submodule key_config
;
159 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
160 key_config
.path
= path
;
162 hashmap_entry_init(&key
, hash
);
163 key
.config
= &key_config
;
165 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
167 return entry
->config
;
171 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
172 const unsigned char *gitmodules_sha1
, const char *name
)
174 struct submodule_entry
*entry
;
175 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, name
);
176 struct submodule_entry key
;
177 struct submodule key_config
;
179 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
180 key_config
.name
= name
;
182 hashmap_entry_init(&key
, hash
);
183 key
.config
= &key_config
;
185 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
187 return entry
->config
;
191 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
194 const char *subsection
, *key
;
195 int subsection_len
, parse
;
196 parse
= parse_config_key(var
, "submodule", &subsection
,
197 &subsection_len
, &key
);
198 if (parse
< 0 || !subsection
)
201 strbuf_add(name
, subsection
, subsection_len
);
202 strbuf_addstr(item
, key
);
207 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
208 const unsigned char *gitmodules_sha1
, const char *name
)
210 struct submodule
*submodule
;
211 struct strbuf name_buf
= STRBUF_INIT
;
213 submodule
= cache_lookup_name(cache
, gitmodules_sha1
, name
);
217 submodule
= xmalloc(sizeof(*submodule
));
219 strbuf_addstr(&name_buf
, name
);
220 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
222 submodule
->path
= NULL
;
223 submodule
->url
= NULL
;
224 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
225 submodule
->update_strategy
.command
= NULL
;
226 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
227 submodule
->ignore
= NULL
;
228 submodule
->branch
= NULL
;
229 submodule
->recommend_shallow
= -1;
231 hashcpy(submodule
->gitmodules_sha1
, gitmodules_sha1
);
233 cache_add(cache
, submodule
);
238 static int parse_fetch_recurse(const char *opt
, const char *arg
,
241 switch (git_parse_maybe_bool(arg
)) {
243 return RECURSE_SUBMODULES_ON
;
245 return RECURSE_SUBMODULES_OFF
;
247 if (!strcmp(arg
, "on-demand"))
248 return RECURSE_SUBMODULES_ON_DEMAND
;
251 die("bad %s argument: %s", opt
, arg
);
253 return RECURSE_SUBMODULES_ERROR
;
257 int parse_submodule_fetchjobs(const char *var
, const char *value
)
259 int fetchjobs
= git_config_int(var
, value
);
261 die(_("negative values not allowed for submodule.fetchjobs"));
265 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
267 return parse_fetch_recurse(opt
, arg
, 1);
270 int option_fetch_parse_recurse_submodules(const struct option
*opt
,
271 const char *arg
, int unset
)
281 *v
= RECURSE_SUBMODULES_OFF
;
284 *v
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
286 *v
= RECURSE_SUBMODULES_ON
;
291 static int parse_update_recurse(const char *opt
, const char *arg
,
294 switch (git_parse_maybe_bool(arg
)) {
296 return RECURSE_SUBMODULES_ON
;
298 return RECURSE_SUBMODULES_OFF
;
301 die("bad %s argument: %s", opt
, arg
);
302 return RECURSE_SUBMODULES_ERROR
;
306 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
308 return parse_update_recurse(opt
, arg
, 1);
311 static int parse_push_recurse(const char *opt
, const char *arg
,
314 switch (git_parse_maybe_bool(arg
)) {
316 /* There's no simple "on" value when pushing */
318 die("bad %s argument: %s", opt
, arg
);
320 return RECURSE_SUBMODULES_ERROR
;
322 return RECURSE_SUBMODULES_OFF
;
324 if (!strcmp(arg
, "on-demand"))
325 return RECURSE_SUBMODULES_ON_DEMAND
;
326 else if (!strcmp(arg
, "check"))
327 return RECURSE_SUBMODULES_CHECK
;
328 else if (!strcmp(arg
, "only"))
329 return RECURSE_SUBMODULES_ONLY
;
330 else if (die_on_error
)
331 die("bad %s argument: %s", opt
, arg
);
333 return RECURSE_SUBMODULES_ERROR
;
337 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
339 return parse_push_recurse(opt
, arg
, 1);
342 static void warn_multiple_config(const unsigned char *treeish_name
,
343 const char *name
, const char *option
)
345 const char *commit_string
= "WORKTREE";
347 commit_string
= sha1_to_hex(treeish_name
);
348 warning("%s:.gitmodules, multiple configurations found for "
349 "'submodule.%s.%s'. Skipping second one!",
350 commit_string
, name
, option
);
353 struct parse_config_parameter
{
354 struct submodule_cache
*cache
;
355 const unsigned char *treeish_name
;
356 const unsigned char *gitmodules_sha1
;
360 static int parse_config(const char *var
, const char *value
, void *data
)
362 struct parse_config_parameter
*me
= data
;
363 struct submodule
*submodule
;
364 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
367 /* this also ensures that we only parse submodule entries */
368 if (!name_and_item_from_var(var
, &name
, &item
))
371 submodule
= lookup_or_create_by_name(me
->cache
,
375 if (!strcmp(item
.buf
, "path")) {
377 ret
= config_error_nonbool(var
);
378 else if (!me
->overwrite
&& submodule
->path
)
379 warn_multiple_config(me
->treeish_name
, submodule
->name
,
383 cache_remove_path(me
->cache
, submodule
);
384 free((void *) submodule
->path
);
385 submodule
->path
= xstrdup(value
);
386 cache_put_path(me
->cache
, submodule
);
388 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
389 /* when parsing worktree configurations we can die early */
390 int die_on_error
= is_null_sha1(me
->gitmodules_sha1
);
391 if (!me
->overwrite
&&
392 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
393 warn_multiple_config(me
->treeish_name
, submodule
->name
,
394 "fetchrecursesubmodules");
396 submodule
->fetch_recurse
= parse_fetch_recurse(
399 } else if (!strcmp(item
.buf
, "ignore")) {
401 ret
= config_error_nonbool(var
);
402 else if (!me
->overwrite
&& submodule
->ignore
)
403 warn_multiple_config(me
->treeish_name
, submodule
->name
,
405 else if (strcmp(value
, "untracked") &&
406 strcmp(value
, "dirty") &&
407 strcmp(value
, "all") &&
408 strcmp(value
, "none"))
409 warning("Invalid parameter '%s' for config option "
410 "'submodule.%s.ignore'", value
, name
.buf
);
412 free((void *) submodule
->ignore
);
413 submodule
->ignore
= xstrdup(value
);
415 } else if (!strcmp(item
.buf
, "url")) {
417 ret
= config_error_nonbool(var
);
418 } else if (!me
->overwrite
&& submodule
->url
) {
419 warn_multiple_config(me
->treeish_name
, submodule
->name
,
422 free((void *) submodule
->url
);
423 submodule
->url
= xstrdup(value
);
425 } else if (!strcmp(item
.buf
, "update")) {
427 ret
= config_error_nonbool(var
);
428 else if (!me
->overwrite
&&
429 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
430 warn_multiple_config(me
->treeish_name
, submodule
->name
,
432 else if (parse_submodule_update_strategy(value
,
433 &submodule
->update_strategy
) < 0)
434 die(_("invalid value for %s"), var
);
435 } else if (!strcmp(item
.buf
, "shallow")) {
436 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
437 warn_multiple_config(me
->treeish_name
, submodule
->name
,
440 submodule
->recommend_shallow
=
441 git_config_bool(var
, value
);
442 } else if (!strcmp(item
.buf
, "branch")) {
443 if (!me
->overwrite
&& submodule
->branch
)
444 warn_multiple_config(me
->treeish_name
, submodule
->name
,
447 free((void *)submodule
->branch
);
448 submodule
->branch
= xstrdup(value
);
452 strbuf_release(&name
);
453 strbuf_release(&item
);
458 int gitmodule_oid_from_commit(const struct object_id
*treeish_name
,
459 struct object_id
*gitmodules_oid
,
464 if (is_null_oid(treeish_name
)) {
465 oidclr(gitmodules_oid
);
469 strbuf_addf(rev
, "%s:.gitmodules", oid_to_hex(treeish_name
));
470 if (get_oid(rev
->buf
, gitmodules_oid
) >= 0)
476 /* This does a lookup of a submodule configuration by name or by path
477 * (key) with on-demand reading of the appropriate .gitmodules from
480 static const struct submodule
*config_from(struct submodule_cache
*cache
,
481 const struct object_id
*treeish_name
, const char *key
,
482 enum lookup_type lookup_type
)
484 struct strbuf rev
= STRBUF_INIT
;
485 unsigned long config_size
;
487 struct object_id oid
;
488 enum object_type type
;
489 const struct submodule
*submodule
= NULL
;
490 struct parse_config_parameter parameter
;
493 * If any parameter except the cache is a NULL pointer just
494 * return the first submodule. Can be used to check whether
495 * there are any submodules parsed.
497 if (!treeish_name
|| !key
) {
498 struct hashmap_iter iter
;
499 struct submodule_entry
*entry
;
501 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
504 return entry
->config
;
507 if (!gitmodule_oid_from_commit(treeish_name
, &oid
, &rev
))
510 switch (lookup_type
) {
512 submodule
= cache_lookup_name(cache
, oid
.hash
, key
);
515 submodule
= cache_lookup_path(cache
, oid
.hash
, key
);
521 config
= read_sha1_file(oid
.hash
, &type
, &config_size
);
522 if (!config
|| type
!= OBJ_BLOB
)
525 /* fill the submodule config into the cache */
526 parameter
.cache
= cache
;
527 parameter
.treeish_name
= treeish_name
->hash
;
528 parameter
.gitmodules_sha1
= oid
.hash
;
529 parameter
.overwrite
= 0;
530 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
531 config
, config_size
, ¶meter
);
532 strbuf_release(&rev
);
535 switch (lookup_type
) {
537 return cache_lookup_name(cache
, oid
.hash
, key
);
539 return cache_lookup_path(cache
, oid
.hash
, key
);
545 strbuf_release(&rev
);
550 static void submodule_cache_check_init(struct repository
*repo
)
552 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
555 if (!repo
->submodule_cache
)
556 repo
->submodule_cache
= submodule_cache_alloc();
558 submodule_cache_init(repo
->submodule_cache
);
561 int submodule_config_option(struct repository
*repo
,
562 const char *var
, const char *value
)
564 struct parse_config_parameter parameter
;
566 submodule_cache_check_init(repo
);
568 parameter
.cache
= repo
->submodule_cache
;
569 parameter
.treeish_name
= NULL
;
570 parameter
.gitmodules_sha1
= null_sha1
;
571 parameter
.overwrite
= 1;
573 return parse_config(var
, value
, ¶meter
);
576 int parse_submodule_config_option(const char *var
, const char *value
)
578 return submodule_config_option(the_repository
, var
, value
);
581 const struct submodule
*submodule_from_name(const struct object_id
*treeish_name
,
584 submodule_cache_check_init(the_repository
);
585 return config_from(the_repository
->submodule_cache
, treeish_name
, name
, lookup_name
);
588 const struct submodule
*submodule_from_path(const struct object_id
*treeish_name
,
591 submodule_cache_check_init(the_repository
);
592 return config_from(the_repository
->submodule_cache
, treeish_name
, path
, lookup_path
);
595 const struct submodule
*submodule_from_cache(struct repository
*repo
,
596 const struct object_id
*treeish_name
,
599 submodule_cache_check_init(repo
);
600 return config_from(repo
->submodule_cache
, treeish_name
,
604 void submodule_free(void)
606 if (the_repository
->submodule_cache
)
607 submodule_cache_clear(the_repository
->submodule_cache
);