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
33static struct submodule_cache cache;
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((void *) entry->config->update_strategy.command);
63 free(entry->config);
64}
65
66static void cache_free(struct submodule_cache *cache)
67{
68 struct hashmap_iter iter;
69 struct submodule_entry *entry;
70
71 /*
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.
75 */
76 hashmap_iter_init(&cache->for_name, &iter);
77 while ((entry = hashmap_iter_next(&iter)))
78 free_one_config(entry);
79
80 hashmap_free(&cache->for_path, 1);
81 hashmap_free(&cache->for_name, 1);
82}
83
84static unsigned int hash_sha1_string(const unsigned char *sha1,
85 const char *string)
86{
87 return memhash(sha1, 20) + strhash(string);
88}
89
90static void cache_put_path(struct submodule_cache *cache,
91 struct submodule *submodule)
92{
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);
99}
100
101static void cache_remove_path(struct submodule_cache *cache,
102 struct submodule *submodule)
103{
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);
112}
113
114static void cache_add(struct submodule_cache *cache,
115 struct submodule *submodule)
116{
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);
123}
124
125static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
126 const unsigned char *gitmodules_sha1, const char *path)
127{
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;
132
133 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
134 key_config.path = path;
135
136 hashmap_entry_init(&key, hash);
137 key.config = &key_config;
138
139 entry = hashmap_get(&cache->for_path, &key, NULL);
140 if (entry)
141 return entry->config;
142 return NULL;
143}
144
145static struct submodule *cache_lookup_name(struct submodule_cache *cache,
146 const unsigned char *gitmodules_sha1, const char *name)
147{
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;
152
153 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
154 key_config.name = name;
155
156 hashmap_entry_init(&key, hash);
157 key.config = &key_config;
158
159 entry = hashmap_get(&cache->for_name, &key, NULL);
160 if (entry)
161 return entry->config;
162 return NULL;
163}
164
165static int name_and_item_from_var(const char *var, struct strbuf *name,
166 struct strbuf *item)
167{
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;
174
175 strbuf_add(name, subsection, subsection_len);
176 strbuf_addstr(item, key);
177
178 return 1;
179}
180
181static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
182 const unsigned char *gitmodules_sha1, const char *name)
183{
184 struct submodule *submodule;
185 struct strbuf name_buf = STRBUF_INIT;
186
187 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
188 if (submodule)
189 return submodule;
190
191 submodule = xmalloc(sizeof(*submodule));
192
193 strbuf_addstr(&name_buf, name);
194 submodule->name = strbuf_detach(&name_buf, NULL);
195
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
203 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
204
205 cache_add(cache, submodule);
206
207 return submodule;
208}
209
210static int parse_fetch_recurse(const char *opt, const char *arg,
211 int die_on_error)
212{
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;
221
222 if (die_on_error)
223 die("bad %s argument: %s", opt, arg);
224 else
225 return RECURSE_SUBMODULES_ERROR;
226 }
227}
228
229int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
230{
231 return parse_fetch_recurse(opt, arg, 1);
232}
233
234static void warn_multiple_config(const unsigned char *commit_sha1,
235 const char *name, const char *option)
236{
237 const char *commit_string = "WORKTREE";
238 if (commit_sha1)
239 commit_string = sha1_to_hex(commit_sha1);
240 warning("%s:.gitmodules, multiple configurations found for "
241 "'submodule.%s.%s'. Skipping second one!",
242 commit_string, name, option);
243}
244
245struct parse_config_parameter {
246 struct submodule_cache *cache;
247 const unsigned char *commit_sha1;
248 const unsigned char *gitmodules_sha1;
249 int overwrite;
250};
251
252static int parse_config(const char *var, const char *value, void *data)
253{
254 struct parse_config_parameter *me = data;
255 struct submodule *submodule;
256 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
257 int ret = 0;
258
259 /* this also ensures that we only parse submodule entries */
260 if (!name_and_item_from_var(var, &name, &item))
261 return 0;
262
263 submodule = lookup_or_create_by_name(me->cache,
264 me->gitmodules_sha1,
265 name.buf);
266
267 if (!strcmp(item.buf, "path")) {
268 if (!value)
269 ret = config_error_nonbool(var);
270 else if (!me->overwrite && submodule->path)
271 warn_multiple_config(me->commit_sha1, submodule->name,
272 "path");
273 else {
274 if (submodule->path)
275 cache_remove_path(me->cache, submodule);
276 free((void *) submodule->path);
277 submodule->path = xstrdup(value);
278 cache_put_path(me->cache, submodule);
279 }
280 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
281 /* when parsing worktree configurations we can die early */
282 int die_on_error = is_null_sha1(me->gitmodules_sha1);
283 if (!me->overwrite &&
284 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
285 warn_multiple_config(me->commit_sha1, submodule->name,
286 "fetchrecursesubmodules");
287 else
288 submodule->fetch_recurse = parse_fetch_recurse(
289 var, value,
290 die_on_error);
291 } else if (!strcmp(item.buf, "ignore")) {
292 if (!value)
293 ret = config_error_nonbool(var);
294 else if (!me->overwrite && submodule->ignore)
295 warn_multiple_config(me->commit_sha1, submodule->name,
296 "ignore");
297 else if (strcmp(value, "untracked") &&
298 strcmp(value, "dirty") &&
299 strcmp(value, "all") &&
300 strcmp(value, "none"))
301 warning("Invalid parameter '%s' for config option "
302 "'submodule.%s.ignore'", value, var);
303 else {
304 free((void *) submodule->ignore);
305 submodule->ignore = xstrdup(value);
306 }
307 } else if (!strcmp(item.buf, "url")) {
308 if (!value) {
309 ret = config_error_nonbool(var);
310 } else if (!me->overwrite && submodule->url) {
311 warn_multiple_config(me->commit_sha1, submodule->name,
312 "url");
313 } else {
314 free((void *) submodule->url);
315 submodule->url = xstrdup(value);
316 }
317 } else if (!strcmp(item.buf, "update")) {
318 if (!value)
319 ret = config_error_nonbool(var);
320 else if (!me->overwrite &&
321 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
322 warn_multiple_config(me->commit_sha1, submodule->name,
323 "update");
324 else if (parse_submodule_update_strategy(value,
325 &submodule->update_strategy) < 0)
326 die(_("invalid value for %s"), var);
327 }
328
329 strbuf_release(&name);
330 strbuf_release(&item);
331
332 return ret;
333}
334
335static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
336 unsigned char *gitmodules_sha1)
337{
338 struct strbuf rev = STRBUF_INIT;
339 int ret = 0;
340
341 if (is_null_sha1(commit_sha1)) {
342 hashcpy(gitmodules_sha1, null_sha1);
343 return 1;
344 }
345
346 strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
347 if (get_sha1(rev.buf, gitmodules_sha1) >= 0)
348 ret = 1;
349
350 strbuf_release(&rev);
351 return ret;
352}
353
354/* This does a lookup of a submodule configuration by name or by path
355 * (key) with on-demand reading of the appropriate .gitmodules from
356 * revisions.
357 */
358static const struct submodule *config_from(struct submodule_cache *cache,
359 const unsigned char *commit_sha1, const char *key,
360 enum lookup_type lookup_type)
361{
362 struct strbuf rev = STRBUF_INIT;
363 unsigned long config_size;
364 char *config;
365 unsigned char sha1[20];
366 enum object_type type;
367 const struct submodule *submodule = NULL;
368 struct parse_config_parameter parameter;
369
370 /*
371 * If any parameter except the cache is a NULL pointer just
372 * return the first submodule. Can be used to check whether
373 * there are any submodules parsed.
374 */
375 if (!commit_sha1 || !key) {
376 struct hashmap_iter iter;
377 struct submodule_entry *entry;
378
379 hashmap_iter_init(&cache->for_name, &iter);
380 entry = hashmap_iter_next(&iter);
381 if (!entry)
382 return NULL;
383 return entry->config;
384 }
385
386 if (!gitmodule_sha1_from_commit(commit_sha1, sha1))
387 return NULL;
388
389 switch (lookup_type) {
390 case lookup_name:
391 submodule = cache_lookup_name(cache, sha1, key);
392 break;
393 case lookup_path:
394 submodule = cache_lookup_path(cache, sha1, key);
395 break;
396 }
397 if (submodule)
398 return submodule;
399
400 config = read_sha1_file(sha1, &type, &config_size);
401 if (!config)
402 return NULL;
403
404 if (type != OBJ_BLOB) {
405 free(config);
406 return NULL;
407 }
408
409 /* fill the submodule config into the cache */
410 parameter.cache = cache;
411 parameter.commit_sha1 = commit_sha1;
412 parameter.gitmodules_sha1 = sha1;
413 parameter.overwrite = 0;
414 git_config_from_buf(parse_config, rev.buf, config, config_size,
415 ¶meter);
416 free(config);
417
418 switch (lookup_type) {
419 case lookup_name:
420 return cache_lookup_name(cache, sha1, key);
421 case lookup_path:
422 return cache_lookup_path(cache, sha1, key);
423 default:
424 return NULL;
425 }
426}
427
428static const struct submodule *config_from_path(struct submodule_cache *cache,
429 const unsigned char *commit_sha1, const char *path)
430{
431 return config_from(cache, commit_sha1, path, lookup_path);
432}
433
434static const struct submodule *config_from_name(struct submodule_cache *cache,
435 const unsigned char *commit_sha1, const char *name)
436{
437 return config_from(cache, commit_sha1, name, lookup_name);
438}
439
440static void ensure_cache_init(void)
441{
442 if (is_cache_init)
443 return;
444
445 cache_init(&cache);
446 is_cache_init = 1;
447}
448
449int parse_submodule_config_option(const char *var, const char *value)
450{
451 struct parse_config_parameter parameter;
452 parameter.cache = &cache;
453 parameter.commit_sha1 = NULL;
454 parameter.gitmodules_sha1 = null_sha1;
455 parameter.overwrite = 1;
456
457 ensure_cache_init();
458 return parse_config(var, value, ¶meter);
459}
460
461const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
462 const char *name)
463{
464 ensure_cache_init();
465 return config_from_name(&cache, commit_sha1, name);
466}
467
468const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
469 const char *path)
470{
471 ensure_cache_init();
472 return config_from_path(&cache, commit_sha1, path);
473}
474
475void submodule_free(void)
476{
477 cache_free(&cache);
478 is_cache_init = 0;
479}