3 #include "submodule-config.h"
8 * submodule cache lookup structure
9 * There is one shared set of 'struct submodule' entries which can be
10 * looked up by their sha1 blob id of the .gitmodule file and either
11 * using path or name as key.
12 * for_path stores submodule entries with path as key
13 * for_name stores submodule entries with name as key
15 struct submodule_cache
{
16 struct hashmap for_path
;
17 struct hashmap for_name
;
21 * thin wrapper struct needed to insert 'struct submodule' entries to
24 struct submodule_entry
{
25 struct hashmap_entry ent
;
26 struct submodule
*config
;
34 static struct submodule_cache the_submodule_cache
;
35 static int is_cache_init
;
37 static int config_path_cmp(const void *unused_cmp_data
,
38 const struct submodule_entry
*a
,
39 const struct submodule_entry
*b
,
40 const void *unused_keydata
)
42 return strcmp(a
->config
->path
, b
->config
->path
) ||
43 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
46 static int config_name_cmp(const void *unused_cmp_data
,
47 const struct submodule_entry
*a
,
48 const struct submodule_entry
*b
,
49 const void *unused_keydata
)
51 return strcmp(a
->config
->name
, b
->config
->name
) ||
52 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
55 static void cache_init(struct submodule_cache
*cache
)
57 hashmap_init(&cache
->for_path
, (hashmap_cmp_fn
) config_path_cmp
, NULL
, 0);
58 hashmap_init(&cache
->for_name
, (hashmap_cmp_fn
) config_name_cmp
, NULL
, 0);
61 static void free_one_config(struct submodule_entry
*entry
)
63 free((void *) entry
->config
->path
);
64 free((void *) entry
->config
->name
);
65 free((void *) entry
->config
->branch
);
66 free((void *) entry
->config
->update_strategy
.command
);
70 static void cache_free(struct submodule_cache
*cache
)
72 struct hashmap_iter iter
;
73 struct submodule_entry
*entry
;
76 * We iterate over the name hash here to be symmetric with the
77 * allocation of struct submodule entries. Each is allocated by
78 * their .gitmodule blob sha1 and submodule name.
80 hashmap_iter_init(&cache
->for_name
, &iter
);
81 while ((entry
= hashmap_iter_next(&iter
)))
82 free_one_config(entry
);
84 hashmap_free(&cache
->for_path
, 1);
85 hashmap_free(&cache
->for_name
, 1);
88 static unsigned int hash_sha1_string(const unsigned char *sha1
,
91 return memhash(sha1
, 20) + strhash(string
);
94 static void cache_put_path(struct submodule_cache
*cache
,
95 struct submodule
*submodule
)
97 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
99 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
100 hashmap_entry_init(e
, hash
);
101 e
->config
= submodule
;
102 hashmap_put(&cache
->for_path
, e
);
105 static void cache_remove_path(struct submodule_cache
*cache
,
106 struct submodule
*submodule
)
108 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
110 struct submodule_entry e
;
111 struct submodule_entry
*removed
;
112 hashmap_entry_init(&e
, hash
);
113 e
.config
= submodule
;
114 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
118 static void cache_add(struct submodule_cache
*cache
,
119 struct submodule
*submodule
)
121 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
123 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
124 hashmap_entry_init(e
, hash
);
125 e
->config
= submodule
;
126 hashmap_add(&cache
->for_name
, e
);
129 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
130 const unsigned char *gitmodules_sha1
, const char *path
)
132 struct submodule_entry
*entry
;
133 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, path
);
134 struct submodule_entry key
;
135 struct submodule key_config
;
137 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
138 key_config
.path
= path
;
140 hashmap_entry_init(&key
, hash
);
141 key
.config
= &key_config
;
143 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
145 return entry
->config
;
149 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
150 const unsigned char *gitmodules_sha1
, const char *name
)
152 struct submodule_entry
*entry
;
153 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, name
);
154 struct submodule_entry key
;
155 struct submodule key_config
;
157 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
158 key_config
.name
= name
;
160 hashmap_entry_init(&key
, hash
);
161 key
.config
= &key_config
;
163 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
165 return entry
->config
;
169 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
172 const char *subsection
, *key
;
173 int subsection_len
, parse
;
174 parse
= parse_config_key(var
, "submodule", &subsection
,
175 &subsection_len
, &key
);
176 if (parse
< 0 || !subsection
)
179 strbuf_add(name
, subsection
, subsection_len
);
180 strbuf_addstr(item
, key
);
185 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
186 const unsigned char *gitmodules_sha1
, const char *name
)
188 struct submodule
*submodule
;
189 struct strbuf name_buf
= STRBUF_INIT
;
191 submodule
= cache_lookup_name(cache
, gitmodules_sha1
, name
);
195 submodule
= xmalloc(sizeof(*submodule
));
197 strbuf_addstr(&name_buf
, name
);
198 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
200 submodule
->path
= NULL
;
201 submodule
->url
= NULL
;
202 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
203 submodule
->update_strategy
.command
= NULL
;
204 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
205 submodule
->ignore
= NULL
;
206 submodule
->branch
= NULL
;
207 submodule
->recommend_shallow
= -1;
209 hashcpy(submodule
->gitmodules_sha1
, gitmodules_sha1
);
211 cache_add(cache
, submodule
);
216 static int parse_fetch_recurse(const char *opt
, const char *arg
,
219 switch (git_config_maybe_bool(opt
, arg
)) {
221 return RECURSE_SUBMODULES_ON
;
223 return RECURSE_SUBMODULES_OFF
;
225 if (!strcmp(arg
, "on-demand"))
226 return RECURSE_SUBMODULES_ON_DEMAND
;
229 die("bad %s argument: %s", opt
, arg
);
231 return RECURSE_SUBMODULES_ERROR
;
235 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
237 return parse_fetch_recurse(opt
, arg
, 1);
240 static int parse_update_recurse(const char *opt
, const char *arg
,
243 switch (git_config_maybe_bool(opt
, arg
)) {
245 return RECURSE_SUBMODULES_ON
;
247 return RECURSE_SUBMODULES_OFF
;
250 die("bad %s argument: %s", opt
, arg
);
251 return RECURSE_SUBMODULES_ERROR
;
255 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
257 return parse_update_recurse(opt
, arg
, 1);
260 static int parse_push_recurse(const char *opt
, const char *arg
,
263 switch (git_config_maybe_bool(opt
, arg
)) {
265 /* There's no simple "on" value when pushing */
267 die("bad %s argument: %s", opt
, arg
);
269 return RECURSE_SUBMODULES_ERROR
;
271 return RECURSE_SUBMODULES_OFF
;
273 if (!strcmp(arg
, "on-demand"))
274 return RECURSE_SUBMODULES_ON_DEMAND
;
275 else if (!strcmp(arg
, "check"))
276 return RECURSE_SUBMODULES_CHECK
;
277 else if (!strcmp(arg
, "only"))
278 return RECURSE_SUBMODULES_ONLY
;
279 else if (die_on_error
)
280 die("bad %s argument: %s", opt
, arg
);
282 return RECURSE_SUBMODULES_ERROR
;
286 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
288 return parse_push_recurse(opt
, arg
, 1);
291 static void warn_multiple_config(const unsigned char *treeish_name
,
292 const char *name
, const char *option
)
294 const char *commit_string
= "WORKTREE";
296 commit_string
= sha1_to_hex(treeish_name
);
297 warning("%s:.gitmodules, multiple configurations found for "
298 "'submodule.%s.%s'. Skipping second one!",
299 commit_string
, name
, option
);
302 struct parse_config_parameter
{
303 struct submodule_cache
*cache
;
304 const unsigned char *treeish_name
;
305 const unsigned char *gitmodules_sha1
;
309 static int parse_config(const char *var
, const char *value
, void *data
)
311 struct parse_config_parameter
*me
= data
;
312 struct submodule
*submodule
;
313 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
316 /* this also ensures that we only parse submodule entries */
317 if (!name_and_item_from_var(var
, &name
, &item
))
320 submodule
= lookup_or_create_by_name(me
->cache
,
324 if (!strcmp(item
.buf
, "path")) {
326 ret
= config_error_nonbool(var
);
327 else if (!me
->overwrite
&& submodule
->path
)
328 warn_multiple_config(me
->treeish_name
, submodule
->name
,
332 cache_remove_path(me
->cache
, submodule
);
333 free((void *) submodule
->path
);
334 submodule
->path
= xstrdup(value
);
335 cache_put_path(me
->cache
, submodule
);
337 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
338 /* when parsing worktree configurations we can die early */
339 int die_on_error
= is_null_sha1(me
->gitmodules_sha1
);
340 if (!me
->overwrite
&&
341 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
342 warn_multiple_config(me
->treeish_name
, submodule
->name
,
343 "fetchrecursesubmodules");
345 submodule
->fetch_recurse
= parse_fetch_recurse(
348 } else if (!strcmp(item
.buf
, "ignore")) {
350 ret
= config_error_nonbool(var
);
351 else if (!me
->overwrite
&& submodule
->ignore
)
352 warn_multiple_config(me
->treeish_name
, submodule
->name
,
354 else if (strcmp(value
, "untracked") &&
355 strcmp(value
, "dirty") &&
356 strcmp(value
, "all") &&
357 strcmp(value
, "none"))
358 warning("Invalid parameter '%s' for config option "
359 "'submodule.%s.ignore'", value
, name
.buf
);
361 free((void *) submodule
->ignore
);
362 submodule
->ignore
= xstrdup(value
);
364 } else if (!strcmp(item
.buf
, "url")) {
366 ret
= config_error_nonbool(var
);
367 } else if (!me
->overwrite
&& submodule
->url
) {
368 warn_multiple_config(me
->treeish_name
, submodule
->name
,
371 free((void *) submodule
->url
);
372 submodule
->url
= xstrdup(value
);
374 } else if (!strcmp(item
.buf
, "update")) {
376 ret
= config_error_nonbool(var
);
377 else if (!me
->overwrite
&&
378 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
379 warn_multiple_config(me
->treeish_name
, submodule
->name
,
381 else if (parse_submodule_update_strategy(value
,
382 &submodule
->update_strategy
) < 0)
383 die(_("invalid value for %s"), var
);
384 } else if (!strcmp(item
.buf
, "shallow")) {
385 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
386 warn_multiple_config(me
->treeish_name
, submodule
->name
,
389 submodule
->recommend_shallow
=
390 git_config_bool(var
, value
);
391 } else if (!strcmp(item
.buf
, "branch")) {
392 if (!me
->overwrite
&& submodule
->branch
)
393 warn_multiple_config(me
->treeish_name
, submodule
->name
,
396 free((void *)submodule
->branch
);
397 submodule
->branch
= xstrdup(value
);
401 strbuf_release(&name
);
402 strbuf_release(&item
);
407 int gitmodule_sha1_from_commit(const unsigned char *treeish_name
,
408 unsigned char *gitmodules_sha1
,
413 if (is_null_sha1(treeish_name
)) {
414 hashclr(gitmodules_sha1
);
418 strbuf_addf(rev
, "%s:.gitmodules", sha1_to_hex(treeish_name
));
419 if (get_sha1(rev
->buf
, gitmodules_sha1
) >= 0)
425 /* This does a lookup of a submodule configuration by name or by path
426 * (key) with on-demand reading of the appropriate .gitmodules from
429 static const struct submodule
*config_from(struct submodule_cache
*cache
,
430 const unsigned char *treeish_name
, const char *key
,
431 enum lookup_type lookup_type
)
433 struct strbuf rev
= STRBUF_INIT
;
434 unsigned long config_size
;
436 unsigned char sha1
[20];
437 enum object_type type
;
438 const struct submodule
*submodule
= NULL
;
439 struct parse_config_parameter parameter
;
442 * If any parameter except the cache is a NULL pointer just
443 * return the first submodule. Can be used to check whether
444 * there are any submodules parsed.
446 if (!treeish_name
|| !key
) {
447 struct hashmap_iter iter
;
448 struct submodule_entry
*entry
;
450 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
453 return entry
->config
;
456 if (!gitmodule_sha1_from_commit(treeish_name
, sha1
, &rev
))
459 switch (lookup_type
) {
461 submodule
= cache_lookup_name(cache
, sha1
, key
);
464 submodule
= cache_lookup_path(cache
, sha1
, key
);
470 config
= read_sha1_file(sha1
, &type
, &config_size
);
471 if (!config
|| type
!= OBJ_BLOB
)
474 /* fill the submodule config into the cache */
475 parameter
.cache
= cache
;
476 parameter
.treeish_name
= treeish_name
;
477 parameter
.gitmodules_sha1
= sha1
;
478 parameter
.overwrite
= 0;
479 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
480 config
, config_size
, ¶meter
);
481 strbuf_release(&rev
);
484 switch (lookup_type
) {
486 return cache_lookup_name(cache
, sha1
, key
);
488 return cache_lookup_path(cache
, sha1
, key
);
494 strbuf_release(&rev
);
499 static void ensure_cache_init(void)
504 cache_init(&the_submodule_cache
);
508 int parse_submodule_config_option(const char *var
, const char *value
)
510 struct parse_config_parameter parameter
;
511 parameter
.cache
= &the_submodule_cache
;
512 parameter
.treeish_name
= NULL
;
513 parameter
.gitmodules_sha1
= null_sha1
;
514 parameter
.overwrite
= 1;
517 return parse_config(var
, value
, ¶meter
);
520 const struct submodule
*submodule_from_name(const unsigned char *treeish_name
,
524 return config_from(&the_submodule_cache
, treeish_name
, name
, lookup_name
);
527 const struct submodule
*submodule_from_path(const unsigned char *treeish_name
,
531 return config_from(&the_submodule_cache
, treeish_name
, path
, lookup_path
);
534 void submodule_free(void)
536 cache_free(&the_submodule_cache
);