2 #include "submodule-config.h"
7 * submodule cache lookup structure
8 * There is one shared set of 'struct submodule' entries which can be
9 * looked up by their sha1 blob id of the .gitmodule file and either
10 * using path or name as key.
11 * for_path stores submodule entries with path as key
12 * for_name stores submodule entries with name as key
14 struct submodule_cache {
15 struct hashmap for_path;
16 struct hashmap for_name;
20 * thin wrapper struct needed to insert 'struct submodule' entries to
23 struct submodule_entry {
24 struct hashmap_entry ent;
25 struct submodule *config;
33 static struct submodule_cache the_submodule_cache;
34 static int is_cache_init;
36 static int config_path_cmp(const struct submodule_entry *a,
37 const struct submodule_entry *b,
40 return strcmp(a->config->path, b->config->path) ||
41 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
44 static int config_name_cmp(const struct submodule_entry *a,
45 const struct submodule_entry *b,
48 return strcmp(a->config->name, b->config->name) ||
49 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
52 static void cache_init(struct submodule_cache *cache)
54 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
55 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
58 static void free_one_config(struct submodule_entry *entry)
60 free((void *) entry->config->path);
61 free((void *) entry->config->name);
62 free((void *) entry->config->update_strategy.command);
66 static void cache_free(struct submodule_cache *cache)
68 struct hashmap_iter iter;
69 struct submodule_entry *entry;
72 * We iterate over the name hash here to be symmetric with the
73 * allocation of struct submodule entries. Each is allocated by
74 * their .gitmodule blob sha1 and submodule name.
76 hashmap_iter_init(&cache->for_name, &iter);
77 while ((entry = hashmap_iter_next(&iter)))
78 free_one_config(entry);
80 hashmap_free(&cache->for_path, 1);
81 hashmap_free(&cache->for_name, 1);
84 static unsigned int hash_sha1_string(const unsigned char *sha1,
87 return memhash(sha1, 20) + strhash(string);
90 static void cache_put_path(struct submodule_cache *cache,
91 struct submodule *submodule)
93 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
95 struct submodule_entry *e = xmalloc(sizeof(*e));
96 hashmap_entry_init(e, hash);
97 e->config = submodule;
98 hashmap_put(&cache->for_path, e);
101 static void cache_remove_path(struct submodule_cache *cache,
102 struct submodule *submodule)
104 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
106 struct submodule_entry e;
107 struct submodule_entry *removed;
108 hashmap_entry_init(&e, hash);
109 e.config = submodule;
110 removed = hashmap_remove(&cache->for_path, &e, NULL);
114 static void cache_add(struct submodule_cache *cache,
115 struct submodule *submodule)
117 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
119 struct submodule_entry *e = xmalloc(sizeof(*e));
120 hashmap_entry_init(e, hash);
121 e->config = submodule;
122 hashmap_add(&cache->for_name, e);
125 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
126 const unsigned char *gitmodules_sha1, const char *path)
128 struct submodule_entry *entry;
129 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
130 struct submodule_entry key;
131 struct submodule key_config;
133 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
134 key_config.path = path;
136 hashmap_entry_init(&key, hash);
137 key.config = &key_config;
139 entry = hashmap_get(&cache->for_path, &key, NULL);
141 return entry->config;
145 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
146 const unsigned char *gitmodules_sha1, const char *name)
148 struct submodule_entry *entry;
149 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
150 struct submodule_entry key;
151 struct submodule key_config;
153 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
154 key_config.name = name;
156 hashmap_entry_init(&key, hash);
157 key.config = &key_config;
159 entry = hashmap_get(&cache->for_name, &key, NULL);
161 return entry->config;
165 static int name_and_item_from_var(const char *var, struct strbuf *name,
168 const char *subsection, *key;
169 int subsection_len, parse;
170 parse = parse_config_key(var, "submodule", &subsection,
171 &subsection_len, &key);
172 if (parse < 0 || !subsection)
175 strbuf_add(name, subsection, subsection_len);
176 strbuf_addstr(item, key);
181 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
182 const unsigned char *gitmodules_sha1, const char *name)
184 struct submodule *submodule;
185 struct strbuf name_buf = STRBUF_INIT;
187 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
191 submodule = xmalloc(sizeof(*submodule));
193 strbuf_addstr(&name_buf, name);
194 submodule->name = strbuf_detach(&name_buf, NULL);
196 submodule->path = NULL;
197 submodule->url = NULL;
198 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
199 submodule->update_strategy.command = NULL;
200 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
201 submodule->ignore = NULL;
202 submodule->recommend_shallow = -1;
204 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
206 cache_add(cache, submodule);
211 static int parse_fetch_recurse(const char *opt, const char *arg,
214 switch (git_config_maybe_bool(opt, arg)) {
216 return RECURSE_SUBMODULES_ON;
218 return RECURSE_SUBMODULES_OFF;
220 if (!strcmp(arg, "on-demand"))
221 return RECURSE_SUBMODULES_ON_DEMAND;
224 die("bad %s argument: %s", opt, arg);
226 return RECURSE_SUBMODULES_ERROR;
230 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
232 return parse_fetch_recurse(opt, arg, 1);
235 static int parse_push_recurse(const char *opt, const char *arg,
238 switch (git_config_maybe_bool(opt, arg)) {
240 /* There's no simple "on" value when pushing */
242 die("bad %s argument: %s", opt, arg);
244 return RECURSE_SUBMODULES_ERROR;
246 return RECURSE_SUBMODULES_OFF;
248 if (!strcmp(arg, "on-demand"))
249 return RECURSE_SUBMODULES_ON_DEMAND;
250 else if (!strcmp(arg, "check"))
251 return RECURSE_SUBMODULES_CHECK;
252 else if (die_on_error)
253 die("bad %s argument: %s", opt, arg);
255 return RECURSE_SUBMODULES_ERROR;
259 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
261 return parse_push_recurse(opt, arg, 1);
264 static void warn_multiple_config(const unsigned char *commit_sha1,
265 const char *name, const char *option)
267 const char *commit_string = "WORKTREE";
269 commit_string = sha1_to_hex(commit_sha1);
270 warning("%s:.gitmodules, multiple configurations found for "
271 "'submodule.%s.%s'. Skipping second one!",
272 commit_string, name, option);
275 struct parse_config_parameter {
276 struct submodule_cache *cache;
277 const unsigned char *commit_sha1;
278 const unsigned char *gitmodules_sha1;
282 static int parse_config(const char *var, const char *value, void *data)
284 struct parse_config_parameter *me = data;
285 struct submodule *submodule;
286 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
289 /* this also ensures that we only parse submodule entries */
290 if (!name_and_item_from_var(var, &name, &item))
293 submodule = lookup_or_create_by_name(me->cache,
297 if (!strcmp(item.buf, "path")) {
299 ret = config_error_nonbool(var);
300 else if (!me->overwrite && submodule->path)
301 warn_multiple_config(me->commit_sha1, submodule->name,
305 cache_remove_path(me->cache, submodule);
306 free((void *) submodule->path);
307 submodule->path = xstrdup(value);
308 cache_put_path(me->cache, submodule);
310 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
311 /* when parsing worktree configurations we can die early */
312 int die_on_error = is_null_sha1(me->gitmodules_sha1);
313 if (!me->overwrite &&
314 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
315 warn_multiple_config(me->commit_sha1, submodule->name,
316 "fetchrecursesubmodules");
318 submodule->fetch_recurse = parse_fetch_recurse(
321 } else if (!strcmp(item.buf, "ignore")) {
323 ret = config_error_nonbool(var);
324 else if (!me->overwrite && submodule->ignore)
325 warn_multiple_config(me->commit_sha1, submodule->name,
327 else if (strcmp(value, "untracked") &&
328 strcmp(value, "dirty") &&
329 strcmp(value, "all") &&
330 strcmp(value, "none"))
331 warning("Invalid parameter '%s' for config option "
332 "'submodule.%s.ignore'", value, var);
334 free((void *) submodule->ignore);
335 submodule->ignore = xstrdup(value);
337 } else if (!strcmp(item.buf, "url")) {
339 ret = config_error_nonbool(var);
340 } else if (!me->overwrite && submodule->url) {
341 warn_multiple_config(me->commit_sha1, submodule->name,
344 free((void *) submodule->url);
345 submodule->url = xstrdup(value);
347 } else if (!strcmp(item.buf, "update")) {
349 ret = config_error_nonbool(var);
350 else if (!me->overwrite &&
351 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
352 warn_multiple_config(me->commit_sha1, submodule->name,
354 else if (parse_submodule_update_strategy(value,
355 &submodule->update_strategy) < 0)
356 die(_("invalid value for %s"), var);
357 } else if (!strcmp(item.buf, "shallow")) {
358 if (!me->overwrite && submodule->recommend_shallow != -1)
359 warn_multiple_config(me->commit_sha1, submodule->name,
362 submodule->recommend_shallow =
363 git_config_bool(var, value);
367 strbuf_release(&name);
368 strbuf_release(&item);
373 static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
374 unsigned char *gitmodules_sha1,
379 if (is_null_sha1(commit_sha1)) {
380 hashclr(gitmodules_sha1);
384 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
385 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
391 /* This does a lookup of a submodule configuration by name or by path
392 * (key) with on-demand reading of the appropriate .gitmodules from
395 static const struct submodule *config_from(struct submodule_cache *cache,
396 const unsigned char *commit_sha1, const char *key,
397 enum lookup_type lookup_type)
399 struct strbuf rev = STRBUF_INIT;
400 unsigned long config_size;
402 unsigned char sha1[20];
403 enum object_type type;
404 const struct submodule *submodule = NULL;
405 struct parse_config_parameter parameter;
408 * If any parameter except the cache is a NULL pointer just
409 * return the first submodule. Can be used to check whether
410 * there are any submodules parsed.
412 if (!commit_sha1 || !key) {
413 struct hashmap_iter iter;
414 struct submodule_entry *entry;
416 entry = hashmap_iter_first(&cache->for_name, &iter);
419 return entry->config;
422 if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
425 switch (lookup_type) {
427 submodule = cache_lookup_name(cache, sha1, key);
430 submodule = cache_lookup_path(cache, sha1, key);
436 config = read_sha1_file(sha1, &type, &config_size);
437 if (!config || type != OBJ_BLOB)
440 /* fill the submodule config into the cache */
441 parameter.cache = cache;
442 parameter.commit_sha1 = commit_sha1;
443 parameter.gitmodules_sha1 = sha1;
444 parameter.overwrite = 0;
445 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
446 config, config_size, ¶meter);
447 strbuf_release(&rev);
450 switch (lookup_type) {
452 return cache_lookup_name(cache, sha1, key);
454 return cache_lookup_path(cache, sha1, key);
460 strbuf_release(&rev);
465 static const struct submodule *config_from_path(struct submodule_cache *cache,
466 const unsigned char *commit_sha1, const char *path)
468 return config_from(cache, commit_sha1, path, lookup_path);
471 static const struct submodule *config_from_name(struct submodule_cache *cache,
472 const unsigned char *commit_sha1, const char *name)
474 return config_from(cache, commit_sha1, name, lookup_name);
477 static void ensure_cache_init(void)
482 cache_init(&the_submodule_cache);
486 int parse_submodule_config_option(const char *var, const char *value)
488 struct parse_config_parameter parameter;
489 parameter.cache = &the_submodule_cache;
490 parameter.commit_sha1 = NULL;
491 parameter.gitmodules_sha1 = null_sha1;
492 parameter.overwrite = 1;
495 return parse_config(var, value, ¶meter);
498 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
502 return config_from_name(&the_submodule_cache, commit_sha1, name);
505 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
509 return config_from_path(&the_submodule_cache, commit_sha1, path);
512 void submodule_free(void)
514 cache_free(&the_submodule_cache);