]> git.ipfire.org Git - thirdparty/git.git/blame - submodule-config.c
submodule-config: passing name reference for .gitmodule blobs
[thirdparty/git.git] / submodule-config.c
CommitLineData
959b5455
HV
1#include "cache.h"
2#include "submodule-config.h"
3#include "submodule.h"
4#include "strbuf.h"
5
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
13 */
14struct submodule_cache {
15 struct hashmap for_path;
16 struct hashmap for_name;
17};
18
19/*
20 * thin wrapper struct needed to insert 'struct submodule' entries to
21 * the hashmap
22 */
23struct submodule_entry {
24 struct hashmap_entry ent;
25 struct submodule *config;
26};
27
28enum lookup_type {
29 lookup_name,
30 lookup_path
31};
32
99dab168 33static struct submodule_cache the_submodule_cache;
959b5455
HV
34static int is_cache_init;
35
36static int config_path_cmp(const struct submodule_entry *a,
37 const struct submodule_entry *b,
38 const void *unused)
39{
40 return strcmp(a->config->path, b->config->path) ||
41 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
42}
43
44static int config_name_cmp(const struct submodule_entry *a,
45 const struct submodule_entry *b,
46 const void *unused)
47{
48 return strcmp(a->config->name, b->config->name) ||
49 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
50}
51
52static void cache_init(struct submodule_cache *cache)
53{
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);
56}
57
58static void free_one_config(struct submodule_entry *entry)
59{
60 free((void *) entry->config->path);
61 free((void *) entry->config->name);
62 free(entry->config);
63}
64
65static void cache_free(struct submodule_cache *cache)
66{
67 struct hashmap_iter iter;
68 struct submodule_entry *entry;
69
70 /*
71 * We iterate over the name hash here to be symmetric with the
72 * allocation of struct submodule entries. Each is allocated by
73 * their .gitmodule blob sha1 and submodule name.
74 */
75 hashmap_iter_init(&cache->for_name, &iter);
76 while ((entry = hashmap_iter_next(&iter)))
77 free_one_config(entry);
78
79 hashmap_free(&cache->for_path, 1);
80 hashmap_free(&cache->for_name, 1);
81}
82
83static unsigned int hash_sha1_string(const unsigned char *sha1,
84 const char *string)
85{
86 return memhash(sha1, 20) + strhash(string);
87}
88
89static void cache_put_path(struct submodule_cache *cache,
90 struct submodule *submodule)
91{
92 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
93 submodule->path);
94 struct submodule_entry *e = xmalloc(sizeof(*e));
95 hashmap_entry_init(e, hash);
96 e->config = submodule;
97 hashmap_put(&cache->for_path, e);
98}
99
100static void cache_remove_path(struct submodule_cache *cache,
101 struct submodule *submodule)
102{
103 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
104 submodule->path);
105 struct submodule_entry e;
106 struct submodule_entry *removed;
107 hashmap_entry_init(&e, hash);
108 e.config = submodule;
109 removed = hashmap_remove(&cache->for_path, &e, NULL);
110 free(removed);
111}
112
113static void cache_add(struct submodule_cache *cache,
114 struct submodule *submodule)
115{
116 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
117 submodule->name);
118 struct submodule_entry *e = xmalloc(sizeof(*e));
119 hashmap_entry_init(e, hash);
120 e->config = submodule;
121 hashmap_add(&cache->for_name, e);
122}
123
124static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
125 const unsigned char *gitmodules_sha1, const char *path)
126{
127 struct submodule_entry *entry;
128 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
129 struct submodule_entry key;
130 struct submodule key_config;
131
132 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
133 key_config.path = path;
134
135 hashmap_entry_init(&key, hash);
136 key.config = &key_config;
137
138 entry = hashmap_get(&cache->for_path, &key, NULL);
139 if (entry)
140 return entry->config;
141 return NULL;
142}
143
144static struct submodule *cache_lookup_name(struct submodule_cache *cache,
145 const unsigned char *gitmodules_sha1, const char *name)
146{
147 struct submodule_entry *entry;
148 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
149 struct submodule_entry key;
150 struct submodule key_config;
151
152 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
153 key_config.name = name;
154
155 hashmap_entry_init(&key, hash);
156 key.config = &key_config;
157
158 entry = hashmap_get(&cache->for_name, &key, NULL);
159 if (entry)
160 return entry->config;
161 return NULL;
162}
163
164static int name_and_item_from_var(const char *var, struct strbuf *name,
165 struct strbuf *item)
166{
167 const char *subsection, *key;
168 int subsection_len, parse;
169 parse = parse_config_key(var, "submodule", &subsection,
170 &subsection_len, &key);
171 if (parse < 0 || !subsection)
172 return 0;
173
174 strbuf_add(name, subsection, subsection_len);
175 strbuf_addstr(item, key);
176
177 return 1;
178}
179
180static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
181 const unsigned char *gitmodules_sha1, const char *name)
182{
183 struct submodule *submodule;
184 struct strbuf name_buf = STRBUF_INIT;
185
186 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
187 if (submodule)
188 return submodule;
189
190 submodule = xmalloc(sizeof(*submodule));
191
192 strbuf_addstr(&name_buf, name);
193 submodule->name = strbuf_detach(&name_buf, NULL);
194
195 submodule->path = NULL;
196 submodule->url = NULL;
197 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
198 submodule->ignore = NULL;
199
200 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
201
202 cache_add(cache, submodule);
203
204 return submodule;
205}
206
027771fc
HV
207static int parse_fetch_recurse(const char *opt, const char *arg,
208 int die_on_error)
209{
210 switch (git_config_maybe_bool(opt, arg)) {
211 case 1:
212 return RECURSE_SUBMODULES_ON;
213 case 0:
214 return RECURSE_SUBMODULES_OFF;
215 default:
216 if (!strcmp(arg, "on-demand"))
217 return RECURSE_SUBMODULES_ON_DEMAND;
218
219 if (die_on_error)
220 die("bad %s argument: %s", opt, arg);
221 else
222 return RECURSE_SUBMODULES_ERROR;
223 }
224}
225
226int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
227{
228 return parse_fetch_recurse(opt, arg, 1);
229}
230
b33a15b0
MC
231static int parse_push_recurse(const char *opt, const char *arg,
232 int die_on_error)
233{
234 switch (git_config_maybe_bool(opt, arg)) {
235 case 1:
236 /* There's no simple "on" value when pushing */
237 if (die_on_error)
238 die("bad %s argument: %s", opt, arg);
239 else
240 return RECURSE_SUBMODULES_ERROR;
241 case 0:
242 return RECURSE_SUBMODULES_OFF;
243 default:
244 if (!strcmp(arg, "on-demand"))
245 return RECURSE_SUBMODULES_ON_DEMAND;
246 else if (!strcmp(arg, "check"))
247 return RECURSE_SUBMODULES_CHECK;
248 else if (die_on_error)
249 die("bad %s argument: %s", opt, arg);
250 else
251 return RECURSE_SUBMODULES_ERROR;
252 }
253}
254
255int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
256{
257 return parse_push_recurse(opt, arg, 1);
258}
259
959b5455
HV
260static void warn_multiple_config(const unsigned char *commit_sha1,
261 const char *name, const char *option)
262{
263 const char *commit_string = "WORKTREE";
264 if (commit_sha1)
265 commit_string = sha1_to_hex(commit_sha1);
266 warning("%s:.gitmodules, multiple configurations found for "
267 "'submodule.%s.%s'. Skipping second one!",
268 commit_string, name, option);
269}
270
271struct parse_config_parameter {
272 struct submodule_cache *cache;
273 const unsigned char *commit_sha1;
274 const unsigned char *gitmodules_sha1;
275 int overwrite;
276};
277
278static int parse_config(const char *var, const char *value, void *data)
279{
280 struct parse_config_parameter *me = data;
281 struct submodule *submodule;
282 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
283 int ret = 0;
284
285 /* this also ensures that we only parse submodule entries */
286 if (!name_and_item_from_var(var, &name, &item))
287 return 0;
288
147875fd
SB
289 submodule = lookup_or_create_by_name(me->cache,
290 me->gitmodules_sha1,
291 name.buf);
959b5455
HV
292
293 if (!strcmp(item.buf, "path")) {
147875fd 294 if (!value)
959b5455 295 ret = config_error_nonbool(var);
147875fd 296 else if (!me->overwrite && submodule->path != NULL)
959b5455
HV
297 warn_multiple_config(me->commit_sha1, submodule->name,
298 "path");
147875fd
SB
299 else {
300 if (submodule->path)
301 cache_remove_path(me->cache, submodule);
302 free((void *) submodule->path);
303 submodule->path = xstrdup(value);
304 cache_put_path(me->cache, submodule);
959b5455 305 }
959b5455 306 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc
HV
307 /* when parsing worktree configurations we can die early */
308 int die_on_error = is_null_sha1(me->gitmodules_sha1);
959b5455 309 if (!me->overwrite &&
147875fd 310 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
959b5455
HV
311 warn_multiple_config(me->commit_sha1, submodule->name,
312 "fetchrecursesubmodules");
147875fd
SB
313 else
314 submodule->fetch_recurse = parse_fetch_recurse(
315 var, value,
027771fc 316 die_on_error);
959b5455 317 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
318 if (!value)
319 ret = config_error_nonbool(var);
320 else if (!me->overwrite && submodule->ignore != NULL)
959b5455
HV
321 warn_multiple_config(me->commit_sha1, submodule->name,
322 "ignore");
147875fd
SB
323 else if (strcmp(value, "untracked") &&
324 strcmp(value, "dirty") &&
325 strcmp(value, "all") &&
326 strcmp(value, "none"))
959b5455
HV
327 warning("Invalid parameter '%s' for config option "
328 "'submodule.%s.ignore'", value, var);
147875fd
SB
329 else {
330 free((void *) submodule->ignore);
331 submodule->ignore = xstrdup(value);
959b5455 332 }
959b5455 333 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
334 if (!value) {
335 ret = config_error_nonbool(var);
147875fd 336 } else if (!me->overwrite && submodule->url != NULL) {
959b5455
HV
337 warn_multiple_config(me->commit_sha1, submodule->name,
338 "url");
147875fd
SB
339 } else {
340 free((void *) submodule->url);
341 submodule->url = xstrdup(value);
959b5455 342 }
959b5455
HV
343 }
344
959b5455
HV
345 strbuf_release(&name);
346 strbuf_release(&item);
347
348 return ret;
349}
350
351static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
514dea90
HV
352 unsigned char *gitmodules_sha1,
353 struct strbuf *rev)
959b5455 354{
959b5455
HV
355 int ret = 0;
356
357 if (is_null_sha1(commit_sha1)) {
358 hashcpy(gitmodules_sha1, null_sha1);
359 return 1;
360 }
361
514dea90
HV
362 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
363 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
959b5455
HV
364 ret = 1;
365
959b5455
HV
366 return ret;
367}
368
369/* This does a lookup of a submodule configuration by name or by path
370 * (key) with on-demand reading of the appropriate .gitmodules from
371 * revisions.
372 */
373static const struct submodule *config_from(struct submodule_cache *cache,
374 const unsigned char *commit_sha1, const char *key,
375 enum lookup_type lookup_type)
376{
514dea90 377 struct strbuf rev = STRBUF_INIT;
959b5455
HV
378 unsigned long config_size;
379 char *config;
380 unsigned char sha1[20];
381 enum object_type type;
382 const struct submodule *submodule = NULL;
383 struct parse_config_parameter parameter;
384
385 /*
386 * If any parameter except the cache is a NULL pointer just
387 * return the first submodule. Can be used to check whether
388 * there are any submodules parsed.
389 */
390 if (!commit_sha1 || !key) {
391 struct hashmap_iter iter;
392 struct submodule_entry *entry;
393
01d98e8a 394 entry = hashmap_iter_first(&cache->for_name, &iter);
959b5455
HV
395 if (!entry)
396 return NULL;
397 return entry->config;
398 }
399
514dea90
HV
400 if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
401 strbuf_release(&rev);
959b5455 402 return NULL;
514dea90 403 }
959b5455
HV
404
405 switch (lookup_type) {
406 case lookup_name:
407 submodule = cache_lookup_name(cache, sha1, key);
408 break;
409 case lookup_path:
410 submodule = cache_lookup_path(cache, sha1, key);
411 break;
412 }
514dea90
HV
413 if (submodule) {
414 strbuf_release(&rev);
959b5455 415 return submodule;
514dea90 416 }
959b5455
HV
417
418 config = read_sha1_file(sha1, &type, &config_size);
514dea90
HV
419 if (!config) {
420 strbuf_release(&rev);
959b5455 421 return NULL;
514dea90 422 }
959b5455
HV
423
424 if (type != OBJ_BLOB) {
514dea90 425 strbuf_release(&rev);
959b5455
HV
426 free(config);
427 return NULL;
428 }
429
430 /* fill the submodule config into the cache */
431 parameter.cache = cache;
432 parameter.commit_sha1 = commit_sha1;
433 parameter.gitmodules_sha1 = sha1;
434 parameter.overwrite = 0;
514dea90 435 git_config_from_mem(parse_config, "submodule-blob", rev.buf,
473166b9 436 config, config_size, &parameter);
514dea90 437 strbuf_release(&rev);
959b5455
HV
438 free(config);
439
440 switch (lookup_type) {
441 case lookup_name:
442 return cache_lookup_name(cache, sha1, key);
443 case lookup_path:
444 return cache_lookup_path(cache, sha1, key);
445 default:
446 return NULL;
447 }
448}
449
450static const struct submodule *config_from_path(struct submodule_cache *cache,
451 const unsigned char *commit_sha1, const char *path)
452{
453 return config_from(cache, commit_sha1, path, lookup_path);
454}
455
456static const struct submodule *config_from_name(struct submodule_cache *cache,
457 const unsigned char *commit_sha1, const char *name)
458{
459 return config_from(cache, commit_sha1, name, lookup_name);
460}
461
462static void ensure_cache_init(void)
463{
464 if (is_cache_init)
465 return;
466
99dab168 467 cache_init(&the_submodule_cache);
959b5455
HV
468 is_cache_init = 1;
469}
470
851e18c3
HV
471int parse_submodule_config_option(const char *var, const char *value)
472{
473 struct parse_config_parameter parameter;
99dab168 474 parameter.cache = &the_submodule_cache;
851e18c3
HV
475 parameter.commit_sha1 = NULL;
476 parameter.gitmodules_sha1 = null_sha1;
477 parameter.overwrite = 1;
478
479 ensure_cache_init();
480 return parse_config(var, value, &parameter);
481}
482
959b5455
HV
483const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
484 const char *name)
485{
486 ensure_cache_init();
99dab168 487 return config_from_name(&the_submodule_cache, commit_sha1, name);
959b5455
HV
488}
489
490const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
491 const char *path)
492{
493 ensure_cache_init();
99dab168 494 return config_from_path(&the_submodule_cache, commit_sha1, path);
959b5455
HV
495}
496
497void submodule_free(void)
498{
99dab168 499 cache_free(&the_submodule_cache);
959b5455
HV
500 is_cache_init = 0;
501}