Merge branch 'jk/pretty-empty-format' into next
[git/jrn.git] / submodule-config.c
blob96623adacb89144c84c65eb4c44c80b07088c363
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
4 #include "strbuf.h"
6 /*
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
21 * the hashmap
23 struct submodule_entry {
24 struct hashmap_entry ent;
25 struct submodule *config;
28 static struct submodule_cache cache;
29 static int is_cache_init;
31 static int config_path_cmp(const struct submodule_entry *a,
32 const struct submodule_entry *b,
33 const void *unused)
35 return strcmp(a->config->path, b->config->path) ||
36 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
39 static int config_name_cmp(const struct submodule_entry *a,
40 const struct submodule_entry *b,
41 const void *unused)
43 return strcmp(a->config->name, b->config->name) ||
44 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
47 static void cache_init(struct submodule_cache *cache)
49 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
50 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
53 static void free_one_config(struct submodule_entry *entry)
55 free((void *) entry->config->path);
56 free((void *) entry->config->name);
57 free(entry->config);
60 static void cache_free(struct submodule_cache *cache)
62 struct hashmap_iter iter;
63 struct submodule_entry *entry;
66 * We iterate over the name hash here to be symmetric with the
67 * allocation of struct submodule entries. Each is allocated by
68 * their .gitmodule blob sha1 and submodule name.
70 hashmap_iter_init(&cache->for_name, &iter);
71 while ((entry = hashmap_iter_next(&iter)))
72 free_one_config(entry);
74 hashmap_free(&cache->for_path, 1);
75 hashmap_free(&cache->for_name, 1);
78 static unsigned int hash_sha1_string(const unsigned char *sha1,
79 const char *string)
81 return memhash(sha1, 20) + strhash(string);
84 static void cache_put_path(struct submodule_cache *cache,
85 struct submodule *submodule)
87 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
88 submodule->path);
89 struct submodule_entry *e = xmalloc(sizeof(*e));
90 hashmap_entry_init(e, hash);
91 e->config = submodule;
92 hashmap_put(&cache->for_path, e);
95 static void cache_remove_path(struct submodule_cache *cache,
96 struct submodule *submodule)
98 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
99 submodule->path);
100 struct submodule_entry e;
101 struct submodule_entry *removed;
102 hashmap_entry_init(&e, hash);
103 e.config = submodule;
104 removed = hashmap_remove(&cache->for_path, &e, NULL);
105 free(removed);
108 static void cache_add(struct submodule_cache *cache,
109 struct submodule *submodule)
111 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
112 submodule->name);
113 struct submodule_entry *e = xmalloc(sizeof(*e));
114 hashmap_entry_init(e, hash);
115 e->config = submodule;
116 hashmap_add(&cache->for_name, e);
119 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
120 const unsigned char *gitmodules_sha1, const char *path)
122 struct submodule_entry *entry;
123 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
124 struct submodule_entry key;
125 struct submodule key_config;
127 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
128 key_config.path = path;
130 hashmap_entry_init(&key, hash);
131 key.config = &key_config;
133 entry = hashmap_get(&cache->for_path, &key, NULL);
134 if (entry)
135 return entry->config;
136 return NULL;
139 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
140 const unsigned char *gitmodules_sha1, const char *name)
142 struct submodule_entry *entry;
143 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
144 struct submodule_entry key;
145 struct submodule key_config;
147 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
148 key_config.name = name;
150 hashmap_entry_init(&key, hash);
151 key.config = &key_config;
153 entry = hashmap_get(&cache->for_name, &key, NULL);
154 if (entry)
155 return entry->config;
156 return NULL;
159 static int name_and_item_from_var(const char *var, struct strbuf *name,
160 struct strbuf *item)
162 const char *subsection, *key;
163 int subsection_len, parse;
164 parse = parse_config_key(var, "submodule", &subsection,
165 &subsection_len, &key);
166 if (parse < 0 || !subsection)
167 return 0;
169 strbuf_add(name, subsection, subsection_len);
170 strbuf_addstr(item, key);
172 return 1;
175 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
176 const unsigned char *gitmodules_sha1, const char *name)
178 struct submodule *submodule;
179 struct strbuf name_buf = STRBUF_INIT;
181 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
182 if (submodule)
183 return submodule;
185 submodule = xmalloc(sizeof(*submodule));
187 strbuf_addstr(&name_buf, name);
188 submodule->name = strbuf_detach(&name_buf, NULL);
190 submodule->path = NULL;
191 submodule->url = NULL;
192 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
193 submodule->ignore = NULL;
195 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
197 cache_add(cache, submodule);
199 return submodule;
202 static int parse_fetch_recurse(const char *opt, const char *arg,
203 int die_on_error)
205 switch (git_config_maybe_bool(opt, arg)) {
206 case 1:
207 return RECURSE_SUBMODULES_ON;
208 case 0:
209 return RECURSE_SUBMODULES_OFF;
210 default:
211 if (!strcmp(arg, "on-demand"))
212 return RECURSE_SUBMODULES_ON_DEMAND;
214 if (die_on_error)
215 die("bad %s argument: %s", opt, arg);
216 else
217 return RECURSE_SUBMODULES_ERROR;
221 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
223 return parse_fetch_recurse(opt, arg, 1);
226 static void warn_multiple_config(const unsigned char *commit_sha1,
227 const char *name, const char *option)
229 const char *commit_string = "WORKTREE";
230 if (commit_sha1)
231 commit_string = sha1_to_hex(commit_sha1);
232 warning("%s:.gitmodules, multiple configurations found for "
233 "'submodule.%s.%s'. Skipping second one!",
234 commit_string, name, option);
237 struct parse_config_parameter {
238 struct submodule_cache *cache;
239 const unsigned char *commit_sha1;
240 const unsigned char *gitmodules_sha1;
241 int overwrite;
244 static int parse_config(const char *var, const char *value, void *data)
246 struct parse_config_parameter *me = data;
247 struct submodule *submodule;
248 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
249 int ret = 0;
251 /* this also ensures that we only parse submodule entries */
252 if (!name_and_item_from_var(var, &name, &item))
253 return 0;
255 submodule = lookup_or_create_by_name(me->cache, me->gitmodules_sha1,
256 name.buf);
258 if (!strcmp(item.buf, "path")) {
259 struct strbuf path = STRBUF_INIT;
260 if (!value) {
261 ret = config_error_nonbool(var);
262 goto release_return;
264 if (!me->overwrite && submodule->path != NULL) {
265 warn_multiple_config(me->commit_sha1, submodule->name,
266 "path");
267 goto release_return;
270 if (submodule->path)
271 cache_remove_path(me->cache, submodule);
272 free((void *) submodule->path);
273 strbuf_addstr(&path, value);
274 submodule->path = strbuf_detach(&path, NULL);
275 cache_put_path(me->cache, submodule);
276 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
277 /* when parsing worktree configurations we can die early */
278 int die_on_error = is_null_sha1(me->gitmodules_sha1);
279 if (!me->overwrite &&
280 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) {
281 warn_multiple_config(me->commit_sha1, submodule->name,
282 "fetchrecursesubmodules");
283 goto release_return;
286 submodule->fetch_recurse = parse_fetch_recurse(var, value,
287 die_on_error);
288 } else if (!strcmp(item.buf, "ignore")) {
289 struct strbuf ignore = STRBUF_INIT;
290 if (!me->overwrite && submodule->ignore != NULL) {
291 warn_multiple_config(me->commit_sha1, submodule->name,
292 "ignore");
293 goto release_return;
295 if (!value) {
296 ret = config_error_nonbool(var);
297 goto release_return;
299 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
300 strcmp(value, "all") && strcmp(value, "none")) {
301 warning("Invalid parameter '%s' for config option "
302 "'submodule.%s.ignore'", value, var);
303 goto release_return;
306 free((void *) submodule->ignore);
307 strbuf_addstr(&ignore, value);
308 submodule->ignore = strbuf_detach(&ignore, NULL);
309 } else if (!strcmp(item.buf, "url")) {
310 struct strbuf url = STRBUF_INIT;
311 if (!value) {
312 ret = config_error_nonbool(var);
313 goto release_return;
315 if (!me->overwrite && submodule->url != NULL) {
316 warn_multiple_config(me->commit_sha1, submodule->name,
317 "url");
318 goto release_return;
321 free((void *) submodule->url);
322 strbuf_addstr(&url, value);
323 submodule->url = strbuf_detach(&url, NULL);
326 release_return:
327 strbuf_release(&name);
328 strbuf_release(&item);
330 return ret;
333 static const struct submodule *config_from_path(struct submodule_cache *cache,
334 const unsigned char *commit_sha1, const char *path)
336 struct strbuf rev = STRBUF_INIT;
337 unsigned long config_size;
338 char *config;
339 unsigned char sha1[20];
340 enum object_type type;
341 const struct submodule *submodule = NULL;
342 struct parse_config_parameter parameter;
345 * If any parameter except the cache is a NULL pointer just
346 * return the first submodule. Can be used to check whether
347 * there are any submodules parsed.
349 if (!commit_sha1 || !path) {
350 struct hashmap_iter iter;
351 struct submodule_entry *entry;
353 hashmap_iter_init(&cache->for_name, &iter);
354 entry = hashmap_iter_next(&iter);
355 if (!entry)
356 return NULL;
357 return entry->config;
360 if (is_null_sha1(commit_sha1))
361 return cache_lookup_path(cache, null_sha1, path);
363 strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
364 if (get_sha1(rev.buf, sha1) < 0)
365 goto free_rev;
367 submodule = cache_lookup_path(cache, sha1, path);
368 if (submodule)
369 goto free_rev;
371 config = read_sha1_file(sha1, &type, &config_size);
372 if (!config)
373 goto free_rev;
375 if (type != OBJ_BLOB) {
376 free(config);
377 goto free_rev;
380 /* fill the submodule config into the cache */
381 parameter.cache = cache;
382 parameter.commit_sha1 = commit_sha1;
383 parameter.gitmodules_sha1 = sha1;
384 parameter.overwrite = 0;
385 git_config_from_buf(parse_config, rev.buf, config, config_size,
386 &parameter);
387 free(config);
389 submodule = cache_lookup_path(cache, sha1, path);
391 free_rev:
392 strbuf_release(&rev);
393 return submodule;
396 static void ensure_cache_init(void)
398 if (is_cache_init)
399 return;
401 cache_init(&cache);
402 is_cache_init = 1;
405 int parse_submodule_config_option(const char *var, const char *value)
407 struct parse_config_parameter parameter;
408 parameter.cache = &cache;
409 parameter.commit_sha1 = NULL;
410 parameter.gitmodules_sha1 = null_sha1;
411 parameter.overwrite = 1;
413 ensure_cache_init();
414 return parse_config(var, value, &parameter);
417 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
418 const char *name)
420 ensure_cache_init();
421 return cache_lookup_name(&cache, commit_sha1, name);
424 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
425 const char *path)
427 ensure_cache_init();
428 return config_from_path(&cache, commit_sha1, path);
431 void submodule_free(void)
433 cache_free(&cache);
434 is_cache_init = 0;