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