2 #include "repository.h"
4 #include "submodule-config.h"
9 * submodule cache lookup structure
10 * There is one shared set of 'struct submodule' entries which can be
11 * looked up by their sha1 blob id of the .gitmodule file and either
12 * using path or name as key.
13 * for_path stores submodule entries with path as key
14 * for_name stores submodule entries with name as key
16 struct submodule_cache
{
17 struct hashmap for_path
;
18 struct hashmap for_name
;
19 unsigned initialized
:1;
23 * thin wrapper struct needed to insert 'struct submodule' entries to
26 struct submodule_entry
{
27 struct hashmap_entry ent
;
28 struct submodule
*config
;
36 static int config_path_cmp(const void *unused_cmp_data
,
37 const struct submodule_entry
*a
,
38 const struct submodule_entry
*b
,
39 const void *unused_keydata
)
41 return strcmp(a
->config
->path
, b
->config
->path
) ||
42 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
45 static int config_name_cmp(const void *unused_cmp_data
,
46 const struct submodule_entry
*a
,
47 const struct submodule_entry
*b
,
48 const void *unused_keydata
)
50 return strcmp(a
->config
->name
, b
->config
->name
) ||
51 hashcmp(a
->config
->gitmodules_sha1
, b
->config
->gitmodules_sha1
);
54 static struct submodule_cache
*submodule_cache_alloc(void)
56 return xcalloc(1, sizeof(struct submodule_cache
));
59 static void submodule_cache_init(struct submodule_cache
*cache
)
61 hashmap_init(&cache
->for_path
, (hashmap_cmp_fn
) config_path_cmp
, NULL
, 0);
62 hashmap_init(&cache
->for_name
, (hashmap_cmp_fn
) config_name_cmp
, NULL
, 0);
63 cache
->initialized
= 1;
66 static void free_one_config(struct submodule_entry
*entry
)
68 free((void *) entry
->config
->path
);
69 free((void *) entry
->config
->name
);
70 free((void *) entry
->config
->branch
);
71 free((void *) entry
->config
->update_strategy
.command
);
75 static void submodule_cache_clear(struct submodule_cache
*cache
)
77 struct hashmap_iter iter
;
78 struct submodule_entry
*entry
;
80 if (!cache
->initialized
)
84 * We iterate over the name hash here to be symmetric with the
85 * allocation of struct submodule entries. Each is allocated by
86 * their .gitmodule blob sha1 and submodule name.
88 hashmap_iter_init(&cache
->for_name
, &iter
);
89 while ((entry
= hashmap_iter_next(&iter
)))
90 free_one_config(entry
);
92 hashmap_free(&cache
->for_path
, 1);
93 hashmap_free(&cache
->for_name
, 1);
94 cache
->initialized
= 0;
97 void submodule_cache_free(struct submodule_cache
*cache
)
99 submodule_cache_clear(cache
);
103 static unsigned int hash_sha1_string(const unsigned char *sha1
,
106 return memhash(sha1
, 20) + strhash(string
);
109 static void cache_put_path(struct submodule_cache
*cache
,
110 struct submodule
*submodule
)
112 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
114 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
115 hashmap_entry_init(e
, hash
);
116 e
->config
= submodule
;
117 hashmap_put(&cache
->for_path
, e
);
120 static void cache_remove_path(struct submodule_cache
*cache
,
121 struct submodule
*submodule
)
123 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
125 struct submodule_entry e
;
126 struct submodule_entry
*removed
;
127 hashmap_entry_init(&e
, hash
);
128 e
.config
= submodule
;
129 removed
= hashmap_remove(&cache
->for_path
, &e
, NULL
);
133 static void cache_add(struct submodule_cache
*cache
,
134 struct submodule
*submodule
)
136 unsigned int hash
= hash_sha1_string(submodule
->gitmodules_sha1
,
138 struct submodule_entry
*e
= xmalloc(sizeof(*e
));
139 hashmap_entry_init(e
, hash
);
140 e
->config
= submodule
;
141 hashmap_add(&cache
->for_name
, e
);
144 static const struct submodule
*cache_lookup_path(struct submodule_cache
*cache
,
145 const unsigned char *gitmodules_sha1
, const char *path
)
147 struct submodule_entry
*entry
;
148 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, path
);
149 struct submodule_entry key
;
150 struct submodule key_config
;
152 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
153 key_config
.path
= path
;
155 hashmap_entry_init(&key
, hash
);
156 key
.config
= &key_config
;
158 entry
= hashmap_get(&cache
->for_path
, &key
, NULL
);
160 return entry
->config
;
164 static struct submodule
*cache_lookup_name(struct submodule_cache
*cache
,
165 const unsigned char *gitmodules_sha1
, const char *name
)
167 struct submodule_entry
*entry
;
168 unsigned int hash
= hash_sha1_string(gitmodules_sha1
, name
);
169 struct submodule_entry key
;
170 struct submodule key_config
;
172 hashcpy(key_config
.gitmodules_sha1
, gitmodules_sha1
);
173 key_config
.name
= name
;
175 hashmap_entry_init(&key
, hash
);
176 key
.config
= &key_config
;
178 entry
= hashmap_get(&cache
->for_name
, &key
, NULL
);
180 return entry
->config
;
184 static int name_and_item_from_var(const char *var
, struct strbuf
*name
,
187 const char *subsection
, *key
;
188 int subsection_len
, parse
;
189 parse
= parse_config_key(var
, "submodule", &subsection
,
190 &subsection_len
, &key
);
191 if (parse
< 0 || !subsection
)
194 strbuf_add(name
, subsection
, subsection_len
);
195 strbuf_addstr(item
, key
);
200 static struct submodule
*lookup_or_create_by_name(struct submodule_cache
*cache
,
201 const unsigned char *gitmodules_sha1
, const char *name
)
203 struct submodule
*submodule
;
204 struct strbuf name_buf
= STRBUF_INIT
;
206 submodule
= cache_lookup_name(cache
, gitmodules_sha1
, name
);
210 submodule
= xmalloc(sizeof(*submodule
));
212 strbuf_addstr(&name_buf
, name
);
213 submodule
->name
= strbuf_detach(&name_buf
, NULL
);
215 submodule
->path
= NULL
;
216 submodule
->url
= NULL
;
217 submodule
->update_strategy
.type
= SM_UPDATE_UNSPECIFIED
;
218 submodule
->update_strategy
.command
= NULL
;
219 submodule
->fetch_recurse
= RECURSE_SUBMODULES_NONE
;
220 submodule
->ignore
= NULL
;
221 submodule
->branch
= NULL
;
222 submodule
->recommend_shallow
= -1;
224 hashcpy(submodule
->gitmodules_sha1
, gitmodules_sha1
);
226 cache_add(cache
, submodule
);
231 static int parse_fetch_recurse(const char *opt
, const char *arg
,
234 switch (git_config_maybe_bool(opt
, arg
)) {
236 return RECURSE_SUBMODULES_ON
;
238 return RECURSE_SUBMODULES_OFF
;
240 if (!strcmp(arg
, "on-demand"))
241 return RECURSE_SUBMODULES_ON_DEMAND
;
244 die("bad %s argument: %s", opt
, arg
);
246 return RECURSE_SUBMODULES_ERROR
;
250 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
252 return parse_fetch_recurse(opt
, arg
, 1);
255 static int parse_update_recurse(const char *opt
, const char *arg
,
258 switch (git_config_maybe_bool(opt
, arg
)) {
260 return RECURSE_SUBMODULES_ON
;
262 return RECURSE_SUBMODULES_OFF
;
265 die("bad %s argument: %s", opt
, arg
);
266 return RECURSE_SUBMODULES_ERROR
;
270 int parse_update_recurse_submodules_arg(const char *opt
, const char *arg
)
272 return parse_update_recurse(opt
, arg
, 1);
275 static int parse_push_recurse(const char *opt
, const char *arg
,
278 switch (git_config_maybe_bool(opt
, arg
)) {
280 /* There's no simple "on" value when pushing */
282 die("bad %s argument: %s", opt
, arg
);
284 return RECURSE_SUBMODULES_ERROR
;
286 return RECURSE_SUBMODULES_OFF
;
288 if (!strcmp(arg
, "on-demand"))
289 return RECURSE_SUBMODULES_ON_DEMAND
;
290 else if (!strcmp(arg
, "check"))
291 return RECURSE_SUBMODULES_CHECK
;
292 else if (!strcmp(arg
, "only"))
293 return RECURSE_SUBMODULES_ONLY
;
294 else if (die_on_error
)
295 die("bad %s argument: %s", opt
, arg
);
297 return RECURSE_SUBMODULES_ERROR
;
301 int parse_push_recurse_submodules_arg(const char *opt
, const char *arg
)
303 return parse_push_recurse(opt
, arg
, 1);
306 static void warn_multiple_config(const unsigned char *treeish_name
,
307 const char *name
, const char *option
)
309 const char *commit_string
= "WORKTREE";
311 commit_string
= sha1_to_hex(treeish_name
);
312 warning("%s:.gitmodules, multiple configurations found for "
313 "'submodule.%s.%s'. Skipping second one!",
314 commit_string
, name
, option
);
317 struct parse_config_parameter
{
318 struct submodule_cache
*cache
;
319 const unsigned char *treeish_name
;
320 const unsigned char *gitmodules_sha1
;
324 static int parse_config(const char *var
, const char *value
, void *data
)
326 struct parse_config_parameter
*me
= data
;
327 struct submodule
*submodule
;
328 struct strbuf name
= STRBUF_INIT
, item
= STRBUF_INIT
;
331 /* this also ensures that we only parse submodule entries */
332 if (!name_and_item_from_var(var
, &name
, &item
))
335 submodule
= lookup_or_create_by_name(me
->cache
,
339 if (!strcmp(item
.buf
, "path")) {
341 ret
= config_error_nonbool(var
);
342 else if (!me
->overwrite
&& submodule
->path
)
343 warn_multiple_config(me
->treeish_name
, submodule
->name
,
347 cache_remove_path(me
->cache
, submodule
);
348 free((void *) submodule
->path
);
349 submodule
->path
= xstrdup(value
);
350 cache_put_path(me
->cache
, submodule
);
352 } else if (!strcmp(item
.buf
, "fetchrecursesubmodules")) {
353 /* when parsing worktree configurations we can die early */
354 int die_on_error
= is_null_sha1(me
->gitmodules_sha1
);
355 if (!me
->overwrite
&&
356 submodule
->fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
357 warn_multiple_config(me
->treeish_name
, submodule
->name
,
358 "fetchrecursesubmodules");
360 submodule
->fetch_recurse
= parse_fetch_recurse(
363 } else if (!strcmp(item
.buf
, "ignore")) {
365 ret
= config_error_nonbool(var
);
366 else if (!me
->overwrite
&& submodule
->ignore
)
367 warn_multiple_config(me
->treeish_name
, submodule
->name
,
369 else if (strcmp(value
, "untracked") &&
370 strcmp(value
, "dirty") &&
371 strcmp(value
, "all") &&
372 strcmp(value
, "none"))
373 warning("Invalid parameter '%s' for config option "
374 "'submodule.%s.ignore'", value
, name
.buf
);
376 free((void *) submodule
->ignore
);
377 submodule
->ignore
= xstrdup(value
);
379 } else if (!strcmp(item
.buf
, "url")) {
381 ret
= config_error_nonbool(var
);
382 } else if (!me
->overwrite
&& submodule
->url
) {
383 warn_multiple_config(me
->treeish_name
, submodule
->name
,
386 free((void *) submodule
->url
);
387 submodule
->url
= xstrdup(value
);
389 } else if (!strcmp(item
.buf
, "update")) {
391 ret
= config_error_nonbool(var
);
392 else if (!me
->overwrite
&&
393 submodule
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
)
394 warn_multiple_config(me
->treeish_name
, submodule
->name
,
396 else if (parse_submodule_update_strategy(value
,
397 &submodule
->update_strategy
) < 0)
398 die(_("invalid value for %s"), var
);
399 } else if (!strcmp(item
.buf
, "shallow")) {
400 if (!me
->overwrite
&& submodule
->recommend_shallow
!= -1)
401 warn_multiple_config(me
->treeish_name
, submodule
->name
,
404 submodule
->recommend_shallow
=
405 git_config_bool(var
, value
);
406 } else if (!strcmp(item
.buf
, "branch")) {
407 if (!me
->overwrite
&& submodule
->branch
)
408 warn_multiple_config(me
->treeish_name
, submodule
->name
,
411 free((void *)submodule
->branch
);
412 submodule
->branch
= xstrdup(value
);
416 strbuf_release(&name
);
417 strbuf_release(&item
);
422 int gitmodule_sha1_from_commit(const unsigned char *treeish_name
,
423 unsigned char *gitmodules_sha1
,
428 if (is_null_sha1(treeish_name
)) {
429 hashclr(gitmodules_sha1
);
433 strbuf_addf(rev
, "%s:.gitmodules", sha1_to_hex(treeish_name
));
434 if (get_sha1(rev
->buf
, gitmodules_sha1
) >= 0)
440 /* This does a lookup of a submodule configuration by name or by path
441 * (key) with on-demand reading of the appropriate .gitmodules from
444 static const struct submodule
*config_from(struct submodule_cache
*cache
,
445 const unsigned char *treeish_name
, const char *key
,
446 enum lookup_type lookup_type
)
448 struct strbuf rev
= STRBUF_INIT
;
449 unsigned long config_size
;
451 unsigned char sha1
[20];
452 enum object_type type
;
453 const struct submodule
*submodule
= NULL
;
454 struct parse_config_parameter parameter
;
457 * If any parameter except the cache is a NULL pointer just
458 * return the first submodule. Can be used to check whether
459 * there are any submodules parsed.
461 if (!treeish_name
|| !key
) {
462 struct hashmap_iter iter
;
463 struct submodule_entry
*entry
;
465 entry
= hashmap_iter_first(&cache
->for_name
, &iter
);
468 return entry
->config
;
471 if (!gitmodule_sha1_from_commit(treeish_name
, sha1
, &rev
))
474 switch (lookup_type
) {
476 submodule
= cache_lookup_name(cache
, sha1
, key
);
479 submodule
= cache_lookup_path(cache
, sha1
, key
);
485 config
= read_sha1_file(sha1
, &type
, &config_size
);
486 if (!config
|| type
!= OBJ_BLOB
)
489 /* fill the submodule config into the cache */
490 parameter
.cache
= cache
;
491 parameter
.treeish_name
= treeish_name
;
492 parameter
.gitmodules_sha1
= sha1
;
493 parameter
.overwrite
= 0;
494 git_config_from_mem(parse_config
, CONFIG_ORIGIN_SUBMODULE_BLOB
, rev
.buf
,
495 config
, config_size
, ¶meter
);
496 strbuf_release(&rev
);
499 switch (lookup_type
) {
501 return cache_lookup_name(cache
, sha1
, key
);
503 return cache_lookup_path(cache
, sha1
, key
);
509 strbuf_release(&rev
);
514 static void submodule_cache_check_init(struct repository
*repo
)
516 if (repo
->submodule_cache
&& repo
->submodule_cache
->initialized
)
519 if (!repo
->submodule_cache
)
520 repo
->submodule_cache
= submodule_cache_alloc();
522 submodule_cache_init(repo
->submodule_cache
);
525 int submodule_config_option(struct repository
*repo
,
526 const char *var
, const char *value
)
528 struct parse_config_parameter parameter
;
530 submodule_cache_check_init(repo
);
532 parameter
.cache
= repo
->submodule_cache
;
533 parameter
.treeish_name
= NULL
;
534 parameter
.gitmodules_sha1
= null_sha1
;
535 parameter
.overwrite
= 1;
537 return parse_config(var
, value
, ¶meter
);
540 int parse_submodule_config_option(const char *var
, const char *value
)
542 return submodule_config_option(the_repository
, var
, value
);
545 const struct submodule
*submodule_from_name(const unsigned char *treeish_name
,
548 submodule_cache_check_init(the_repository
);
549 return config_from(the_repository
->submodule_cache
, treeish_name
, name
, lookup_name
);
552 const struct submodule
*submodule_from_path(const unsigned char *treeish_name
,
555 submodule_cache_check_init(the_repository
);
556 return config_from(the_repository
->submodule_cache
, treeish_name
, path
, lookup_path
);
559 const struct submodule
*submodule_from_cache(struct repository
*repo
,
560 const unsigned char *treeish_name
,
563 submodule_cache_check_init(repo
);
564 return config_from(repo
->submodule_cache
, treeish_name
,
568 void submodule_free(void)
570 if (the_repository
->submodule_cache
)
571 submodule_cache_clear(the_repository
->submodule_cache
);