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