Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / submodule-config.c
blob07bdcdfc4239a78f99f90219f4d23a22f209b53e
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 cache;
34 static int is_cache_init;
35 static int parallel_jobs = -1;
37 static int config_path_cmp(const struct submodule_entry *a,
38 const struct submodule_entry *b,
39 const void *unused)
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 struct submodule_entry *a,
46 const struct submodule_entry *b,
47 const void *unused)
49 return strcmp(a->config->name, b->config->name) ||
50 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
53 static void cache_init(struct submodule_cache *cache)
55 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
56 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
59 static void free_one_config(struct submodule_entry *entry)
61 free((void *) entry->config->path);
62 free((void *) entry->config->name);
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 struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
166 const unsigned char *gitmodules_sha1, const char *name)
168 struct submodule *submodule;
169 struct strbuf name_buf = STRBUF_INIT;
171 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
172 if (submodule)
173 return submodule;
175 submodule = xmalloc(sizeof(*submodule));
177 strbuf_addstr(&name_buf, name);
178 submodule->name = strbuf_detach(&name_buf, NULL);
180 submodule->path = NULL;
181 submodule->url = NULL;
182 submodule->update = NULL;
183 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
184 submodule->ignore = NULL;
186 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
188 cache_add(cache, submodule);
190 return submodule;
193 static int parse_fetch_recurse(const char *opt, const char *arg,
194 int die_on_error)
196 switch (git_config_maybe_bool(opt, arg)) {
197 case 1:
198 return RECURSE_SUBMODULES_ON;
199 case 0:
200 return RECURSE_SUBMODULES_OFF;
201 default:
202 if (!strcmp(arg, "on-demand"))
203 return RECURSE_SUBMODULES_ON_DEMAND;
205 if (die_on_error)
206 die("bad %s argument: %s", opt, arg);
207 else
208 return RECURSE_SUBMODULES_ERROR;
212 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
214 return parse_fetch_recurse(opt, arg, 1);
217 static void warn_multiple_config(const unsigned char *commit_sha1,
218 const char *name, const char *option)
220 const char *commit_string = "WORKTREE";
221 if (commit_sha1)
222 commit_string = sha1_to_hex(commit_sha1);
223 warning("%s:.gitmodules, multiple configurations found for "
224 "'submodule.%s.%s'. Skipping second one!",
225 commit_string, name, option);
228 struct parse_config_parameter {
229 struct submodule_cache *cache;
230 const unsigned char *commit_sha1;
231 const unsigned char *gitmodules_sha1;
232 int overwrite;
235 static int parse_generic_submodule_config(const char *var,
236 const char *key,
237 const char *value)
239 if (!strcmp(key, "jobs")) {
240 parallel_jobs = strtol(value, NULL, 10);
242 return 0;
245 static int parse_specific_submodule_config(struct parse_config_parameter *me,
246 const char *name,
247 const char *key,
248 const char *value,
249 const char *var)
251 int ret = 0;
252 struct submodule *submodule = lookup_or_create_by_name(me->cache,
253 me->gitmodules_sha1,
254 name);
256 if (!strcmp(key, "path")) {
257 if (!value)
258 ret = config_error_nonbool(var);
259 else if (!me->overwrite && submodule->path != NULL)
260 warn_multiple_config(me->commit_sha1, submodule->name,
261 "path");
262 else {
263 if (submodule->path)
264 cache_remove_path(me->cache, submodule);
265 free((void *) submodule->path);
266 submodule->path = xstrdup(value);
267 cache_put_path(me->cache, submodule);
269 } else if (!strcmp(key, "fetchrecursesubmodules")) {
270 /* when parsing worktree configurations we can die early */
271 int die_on_error = is_null_sha1(me->gitmodules_sha1);
272 if (!me->overwrite &&
273 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
274 warn_multiple_config(me->commit_sha1, submodule->name,
275 "fetchrecursesubmodules");
276 else
277 submodule->fetch_recurse = parse_fetch_recurse(
278 var, value,
279 die_on_error);
280 } else if (!strcmp(key, "ignore")) {
281 if (!value)
282 ret = config_error_nonbool(var);
283 else if (!me->overwrite && submodule->ignore != NULL)
284 warn_multiple_config(me->commit_sha1, submodule->name,
285 "ignore");
286 else if (strcmp(value, "untracked") &&
287 strcmp(value, "dirty") &&
288 strcmp(value, "all") &&
289 strcmp(value, "none"))
290 warning("Invalid parameter '%s' for config option "
291 "'submodule.%s.ignore'", value, var);
292 else {
293 free((void *) submodule->ignore);
294 submodule->ignore = xstrdup(value);
296 } else if (!strcmp(key, "url")) {
297 if (!value) {
298 ret = config_error_nonbool(var);
299 } else if (!me->overwrite && submodule->url != NULL) {
300 warn_multiple_config(me->commit_sha1, submodule->name,
301 "url");
302 } else {
303 free((void *) submodule->url);
304 submodule->url = xstrdup(value);
306 } else if (!strcmp(key, "update")) {
307 if (!value)
308 ret = config_error_nonbool(var);
309 else if (!me->overwrite && submodule->update != NULL)
310 warn_multiple_config(me->commit_sha1, submodule->name,
311 "update");
312 else {
313 free((void *)submodule->update);
314 submodule->update = xstrdup(value);
318 return ret;
321 static int parse_config(const char *var, const char *value, void *data)
323 struct parse_config_parameter *me = data;
325 int subsection_len;
326 const char *subsection, *key;
327 char *name;
329 if (parse_config_key(var, "submodule", &subsection,
330 &subsection_len, &key) < 0)
331 return 0;
333 if (!subsection_len)
334 return parse_generic_submodule_config(var, key, value);
335 else {
336 int ret;
337 /* subsection is not null terminated */
338 name = xmemdupz(subsection, subsection_len);
339 ret = parse_specific_submodule_config(me, name, key, value, var);
340 free(name);
341 return ret;
345 static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
346 unsigned char *gitmodules_sha1)
348 struct strbuf rev = STRBUF_INIT;
349 int ret = 0;
351 if (is_null_sha1(commit_sha1)) {
352 hashcpy(gitmodules_sha1, null_sha1);
353 return 1;
356 strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
357 if (get_sha1(rev.buf, gitmodules_sha1) >= 0)
358 ret = 1;
360 strbuf_release(&rev);
361 return ret;
364 /* This does a lookup of a submodule configuration by name or by path
365 * (key) with on-demand reading of the appropriate .gitmodules from
366 * revisions.
368 static const struct submodule *config_from(struct submodule_cache *cache,
369 const unsigned char *commit_sha1, const char *key,
370 enum lookup_type lookup_type)
372 struct strbuf rev = STRBUF_INIT;
373 unsigned long config_size;
374 char *config;
375 unsigned char sha1[20];
376 enum object_type type;
377 const struct submodule *submodule = NULL;
378 struct parse_config_parameter parameter;
381 * If any parameter except the cache is a NULL pointer just
382 * return the first submodule. Can be used to check whether
383 * there are any submodules parsed.
385 if (!commit_sha1 || !key) {
386 struct hashmap_iter iter;
387 struct submodule_entry *entry;
389 hashmap_iter_init(&cache->for_name, &iter);
390 entry = hashmap_iter_next(&iter);
391 if (!entry)
392 return NULL;
393 return entry->config;
396 if (!gitmodule_sha1_from_commit(commit_sha1, sha1))
397 return NULL;
399 switch (lookup_type) {
400 case lookup_name:
401 submodule = cache_lookup_name(cache, sha1, key);
402 break;
403 case lookup_path:
404 submodule = cache_lookup_path(cache, sha1, key);
405 break;
407 if (submodule)
408 return submodule;
410 config = read_sha1_file(sha1, &type, &config_size);
411 if (!config)
412 return NULL;
414 if (type != OBJ_BLOB) {
415 free(config);
416 return NULL;
419 /* fill the submodule config into the cache */
420 parameter.cache = cache;
421 parameter.commit_sha1 = commit_sha1;
422 parameter.gitmodules_sha1 = sha1;
423 parameter.overwrite = 0;
424 git_config_from_buf(parse_config, rev.buf, config, config_size,
425 &parameter);
426 free(config);
428 switch (lookup_type) {
429 case lookup_name:
430 return cache_lookup_name(cache, sha1, key);
431 case lookup_path:
432 return cache_lookup_path(cache, sha1, key);
433 default:
434 return NULL;
438 static const struct submodule *config_from_path(struct submodule_cache *cache,
439 const unsigned char *commit_sha1, const char *path)
441 return config_from(cache, commit_sha1, path, lookup_path);
444 static const struct submodule *config_from_name(struct submodule_cache *cache,
445 const unsigned char *commit_sha1, const char *name)
447 return config_from(cache, commit_sha1, name, lookup_name);
450 static void ensure_cache_init(void)
452 if (is_cache_init)
453 return;
455 cache_init(&cache);
456 is_cache_init = 1;
459 int parse_submodule_config_option(const char *var, const char *value)
461 struct parse_config_parameter parameter;
462 parameter.cache = &cache;
463 parameter.commit_sha1 = NULL;
464 parameter.gitmodules_sha1 = null_sha1;
465 parameter.overwrite = 1;
467 ensure_cache_init();
468 return parse_config(var, value, &parameter);
471 const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
472 const char *name)
474 ensure_cache_init();
475 return config_from_name(&cache, commit_sha1, name);
478 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
479 const char *path)
481 ensure_cache_init();
482 return config_from_path(&cache, commit_sha1, path);
485 void submodule_free(void)
487 cache_free(&cache);
488 is_cache_init = 0;
491 int config_parallel_submodules(void)
493 return parallel_jobs;