debian: git-daemon-sysvinit: Depend lsb-base (>= 3.0-6)
[git/debian/andersk.git] / submodule-config.c
blob93dd36424c3b2923950d5ade2c152f18b5444f4b
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 enum lookup_type {
29 lookup_name,
30 lookup_path
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,
38 const void *unused)
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,
46 const void *unused)
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);
63 free(entry->config);
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,
85 const char *string)
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,
94 submodule->path);
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,
105 submodule->path);
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);
111 free(removed);
114 static void cache_add(struct submodule_cache *cache,
115 struct submodule *submodule)
117 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
118 submodule->name);
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);
140 if (entry)
141 return entry->config;
142 return NULL;
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);
160 if (entry)
161 return entry->config;
162 return NULL;
165 static int name_and_item_from_var(const char *var, struct strbuf *name,
166 struct strbuf *item)
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)
173 return 0;
175 strbuf_add(name, subsection, subsection_len);
176 strbuf_addstr(item, key);
178 return 1;
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);
188 if (submodule)
189 return submodule;
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;
203 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
205 cache_add(cache, submodule);
207 return submodule;
210 static int parse_fetch_recurse(const char *opt, const char *arg,
211 int die_on_error)
213 switch (git_config_maybe_bool(opt, arg)) {
214 case 1:
215 return RECURSE_SUBMODULES_ON;
216 case 0:
217 return RECURSE_SUBMODULES_OFF;
218 default:
219 if (!strcmp(arg, "on-demand"))
220 return RECURSE_SUBMODULES_ON_DEMAND;
222 if (die_on_error)
223 die("bad %s argument: %s", opt, arg);
224 else
225 return RECURSE_SUBMODULES_ERROR;
229 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
231 return parse_fetch_recurse(opt, arg, 1);
234 static int parse_push_recurse(const char *opt, const char *arg,
235 int die_on_error)
237 switch (git_config_maybe_bool(opt, arg)) {
238 case 1:
239 /* There's no simple "on" value when pushing */
240 if (die_on_error)
241 die("bad %s argument: %s", opt, arg);
242 else
243 return RECURSE_SUBMODULES_ERROR;
244 case 0:
245 return RECURSE_SUBMODULES_OFF;
246 default:
247 if (!strcmp(arg, "on-demand"))
248 return RECURSE_SUBMODULES_ON_DEMAND;
249 else if (!strcmp(arg, "check"))
250 return RECURSE_SUBMODULES_CHECK;
251 else if (die_on_error)
252 die("bad %s argument: %s", opt, arg);
253 else
254 return RECURSE_SUBMODULES_ERROR;
258 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
260 return parse_push_recurse(opt, arg, 1);
263 static void warn_multiple_config(const unsigned char *commit_sha1,
264 const char *name, const char *option)
266 const char *commit_string = "WORKTREE";
267 if (commit_sha1)
268 commit_string = sha1_to_hex(commit_sha1);
269 warning("%s:.gitmodules, multiple configurations found for "
270 "'submodule.%s.%s'. Skipping second one!",
271 commit_string, name, option);
274 struct parse_config_parameter {
275 struct submodule_cache *cache;
276 const unsigned char *commit_sha1;
277 const unsigned char *gitmodules_sha1;
278 int overwrite;
281 static int parse_config(const char *var, const char *value, void *data)
283 struct parse_config_parameter *me = data;
284 struct submodule *submodule;
285 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
286 int ret = 0;
288 /* this also ensures that we only parse submodule entries */
289 if (!name_and_item_from_var(var, &name, &item))
290 return 0;
292 submodule = lookup_or_create_by_name(me->cache,
293 me->gitmodules_sha1,
294 name.buf);
296 if (!strcmp(item.buf, "path")) {
297 if (!value)
298 ret = config_error_nonbool(var);
299 else if (!me->overwrite && submodule->path)
300 warn_multiple_config(me->commit_sha1, submodule->name,
301 "path");
302 else {
303 if (submodule->path)
304 cache_remove_path(me->cache, submodule);
305 free((void *) submodule->path);
306 submodule->path = xstrdup(value);
307 cache_put_path(me->cache, submodule);
309 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
310 /* when parsing worktree configurations we can die early */
311 int die_on_error = is_null_sha1(me->gitmodules_sha1);
312 if (!me->overwrite &&
313 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
314 warn_multiple_config(me->commit_sha1, submodule->name,
315 "fetchrecursesubmodules");
316 else
317 submodule->fetch_recurse = parse_fetch_recurse(
318 var, value,
319 die_on_error);
320 } else if (!strcmp(item.buf, "ignore")) {
321 if (!value)
322 ret = config_error_nonbool(var);
323 else if (!me->overwrite && submodule->ignore)
324 warn_multiple_config(me->commit_sha1, submodule->name,
325 "ignore");
326 else if (strcmp(value, "untracked") &&
327 strcmp(value, "dirty") &&
328 strcmp(value, "all") &&
329 strcmp(value, "none"))
330 warning("Invalid parameter '%s' for config option "
331 "'submodule.%s.ignore'", value, var);
332 else {
333 free((void *) submodule->ignore);
334 submodule->ignore = xstrdup(value);
336 } else if (!strcmp(item.buf, "url")) {
337 if (!value) {
338 ret = config_error_nonbool(var);
339 } else if (!me->overwrite && submodule->url) {
340 warn_multiple_config(me->commit_sha1, submodule->name,
341 "url");
342 } else {
343 free((void *) submodule->url);
344 submodule->url = xstrdup(value);
346 } else if (!strcmp(item.buf, "update")) {
347 if (!value)
348 ret = config_error_nonbool(var);
349 else if (!me->overwrite &&
350 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
351 warn_multiple_config(me->commit_sha1, submodule->name,
352 "update");
353 else if (parse_submodule_update_strategy(value,
354 &submodule->update_strategy) < 0)
355 die(_("invalid value for %s"), var);
358 strbuf_release(&name);
359 strbuf_release(&item);
361 return ret;
364 static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
365 unsigned char *gitmodules_sha1,
366 struct strbuf *rev)
368 int ret = 0;
370 if (is_null_sha1(commit_sha1)) {
371 hashcpy(gitmodules_sha1, null_sha1);
372 return 1;
375 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
376 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
377 ret = 1;
379 return ret;
382 /* This does a lookup of a submodule configuration by name or by path
383 * (key) with on-demand reading of the appropriate .gitmodules from
384 * revisions.
386 static const struct submodule *config_from(struct submodule_cache *cache,
387 const unsigned char *commit_sha1, const char *key,
388 enum lookup_type lookup_type)
390 struct strbuf rev = STRBUF_INIT;
391 unsigned long config_size;
392 char *config = NULL;
393 unsigned char sha1[20];
394 enum object_type type;
395 const struct submodule *submodule = NULL;
396 struct parse_config_parameter parameter;
399 * If any parameter except the cache is a NULL pointer just
400 * return the first submodule. Can be used to check whether
401 * there are any submodules parsed.
403 if (!commit_sha1 || !key) {
404 struct hashmap_iter iter;
405 struct submodule_entry *entry;
407 entry = hashmap_iter_first(&cache->for_name, &iter);
408 if (!entry)
409 return NULL;
410 return entry->config;
413 if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
414 goto out;
416 switch (lookup_type) {
417 case lookup_name:
418 submodule = cache_lookup_name(cache, sha1, key);
419 break;
420 case lookup_path:
421 submodule = cache_lookup_path(cache, sha1, key);
422 break;
424 if (submodule)
425 goto out;
427 config = read_sha1_file(sha1, &type, &config_size);
428 if (!config || type != OBJ_BLOB)
429 goto out;
431 /* fill the submodule config into the cache */
432 parameter.cache = cache;
433 parameter.commit_sha1 = commit_sha1;
434 parameter.gitmodules_sha1 = sha1;
435 parameter.overwrite = 0;
436 git_config_from_mem(parse_config, "submodule-blob", rev.buf,
437 config, config_size, &parameter);
438 strbuf_release(&rev);
439 free(config);
441 switch (lookup_type) {
442 case lookup_name:
443 return cache_lookup_name(cache, sha1, key);
444 case lookup_path:
445 return cache_lookup_path(cache, sha1, key);
446 default:
447 return NULL;
450 out:
451 strbuf_release(&rev);
452 free(config);
453 return submodule;
456 static const struct submodule *config_from_path(struct submodule_cache *cache,
457 const unsigned char *commit_sha1, const char *path)
459 return config_from(cache, commit_sha1, path, lookup_path);
462 static const struct submodule *config_from_name(struct submodule_cache *cache,
463 const unsigned char *commit_sha1, const char *name)
465 return config_from(cache, commit_sha1, name, lookup_name);
468 static void ensure_cache_init(void)
470 if (is_cache_init)
471 return;
473 cache_init(&the_submodule_cache);
474 is_cache_init = 1;
477 int parse_submodule_config_option(const char *var, const char *value)
479 struct parse_config_parameter parameter;
480 parameter.cache = &the_submodule_cache;
481 parameter.commit_sha1 = NULL;
482 parameter.gitmodules_sha1 = null_sha1;
483 parameter.overwrite = 1;
485 ensure_cache_init();
486 return parse_config(var, value, &parameter);
489 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
490 const char *name)
492 ensure_cache_init();
493 return config_from_name(&the_submodule_cache, commit_sha1, name);
496 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
497 const char *path)
499 ensure_cache_init();
500 return config_from_path(&the_submodule_cache, commit_sha1, path);
503 void submodule_free(void)
505 cache_free(&the_submodule_cache);
506 is_cache_init = 0;