Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 2 | #include "repository.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 4 | #include "submodule-config.h" |
| 5 | #include "submodule.h" |
| 6 | #include "strbuf.h" |
Stefan Beller | 886dc15 | 2017-06-23 12:13:00 -0700 | [diff] [blame] | 7 | #include "parse-options.h" |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * submodule cache lookup structure |
| 11 | * There is one shared set of 'struct submodule' entries which can be |
| 12 | * looked up by their sha1 blob id of the .gitmodule file and either |
| 13 | * using path or name as key. |
| 14 | * for_path stores submodule entries with path as key |
| 15 | * for_name stores submodule entries with name as key |
| 16 | */ |
| 17 | struct submodule_cache { |
| 18 | struct hashmap for_path; |
| 19 | struct hashmap for_name; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 20 | unsigned initialized:1; |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 21 | unsigned gitmodules_read:1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | /* |
| 25 | * thin wrapper struct needed to insert 'struct submodule' entries to |
| 26 | * the hashmap |
| 27 | */ |
| 28 | struct submodule_entry { |
| 29 | struct hashmap_entry ent; |
| 30 | struct submodule *config; |
| 31 | }; |
| 32 | |
| 33 | enum lookup_type { |
| 34 | lookup_name, |
| 35 | lookup_path |
| 36 | }; |
| 37 | |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 38 | static int config_path_cmp(const void *unused_cmp_data, |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 39 | const void *entry, |
| 40 | const void *entry_or_key, |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 41 | const void *unused_keydata) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 42 | { |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 43 | const struct submodule_entry *a = entry; |
| 44 | const struct submodule_entry *b = entry_or_key; |
| 45 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 46 | return strcmp(a->config->path, b->config->path) || |
| 47 | hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1); |
| 48 | } |
| 49 | |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 50 | static int config_name_cmp(const void *unused_cmp_data, |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 51 | const void *entry, |
| 52 | const void *entry_or_key, |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 53 | const void *unused_keydata) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 54 | { |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 55 | const struct submodule_entry *a = entry; |
| 56 | const struct submodule_entry *b = entry_or_key; |
| 57 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 58 | return strcmp(a->config->name, b->config->name) || |
| 59 | hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1); |
| 60 | } |
| 61 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 62 | static struct submodule_cache *submodule_cache_alloc(void) |
| 63 | { |
| 64 | return xcalloc(1, sizeof(struct submodule_cache)); |
| 65 | } |
| 66 | |
| 67 | static void submodule_cache_init(struct submodule_cache *cache) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 68 | { |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 69 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); |
| 70 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 71 | cache->initialized = 1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void free_one_config(struct submodule_entry *entry) |
| 75 | { |
| 76 | free((void *) entry->config->path); |
| 77 | free((void *) entry->config->name); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 78 | free((void *) entry->config->branch); |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 79 | free((void *) entry->config->update_strategy.command); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 80 | free(entry->config); |
| 81 | } |
| 82 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 83 | static void submodule_cache_clear(struct submodule_cache *cache) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 84 | { |
| 85 | struct hashmap_iter iter; |
| 86 | struct submodule_entry *entry; |
| 87 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 88 | if (!cache->initialized) |
| 89 | return; |
| 90 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 91 | /* |
| 92 | * We iterate over the name hash here to be symmetric with the |
| 93 | * allocation of struct submodule entries. Each is allocated by |
| 94 | * their .gitmodule blob sha1 and submodule name. |
| 95 | */ |
| 96 | hashmap_iter_init(&cache->for_name, &iter); |
| 97 | while ((entry = hashmap_iter_next(&iter))) |
| 98 | free_one_config(entry); |
| 99 | |
| 100 | hashmap_free(&cache->for_path, 1); |
| 101 | hashmap_free(&cache->for_name, 1); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 102 | cache->initialized = 0; |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 103 | cache->gitmodules_read = 0; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void submodule_cache_free(struct submodule_cache *cache) |
| 107 | { |
| 108 | submodule_cache_clear(cache); |
| 109 | free(cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static unsigned int hash_sha1_string(const unsigned char *sha1, |
| 113 | const char *string) |
| 114 | { |
| 115 | return memhash(sha1, 20) + strhash(string); |
| 116 | } |
| 117 | |
| 118 | static void cache_put_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 = xmalloc(sizeof(*e)); |
| 124 | hashmap_entry_init(e, hash); |
| 125 | e->config = submodule; |
| 126 | hashmap_put(&cache->for_path, e); |
| 127 | } |
| 128 | |
| 129 | static void cache_remove_path(struct submodule_cache *cache, |
| 130 | struct submodule *submodule) |
| 131 | { |
| 132 | unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1, |
| 133 | submodule->path); |
| 134 | struct submodule_entry e; |
| 135 | struct submodule_entry *removed; |
| 136 | hashmap_entry_init(&e, hash); |
| 137 | e.config = submodule; |
| 138 | removed = hashmap_remove(&cache->for_path, &e, NULL); |
| 139 | free(removed); |
| 140 | } |
| 141 | |
| 142 | static void cache_add(struct submodule_cache *cache, |
| 143 | struct submodule *submodule) |
| 144 | { |
| 145 | unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1, |
| 146 | submodule->name); |
| 147 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
| 148 | hashmap_entry_init(e, hash); |
| 149 | e->config = submodule; |
| 150 | hashmap_add(&cache->for_name, e); |
| 151 | } |
| 152 | |
| 153 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, |
| 154 | const unsigned char *gitmodules_sha1, const char *path) |
| 155 | { |
| 156 | struct submodule_entry *entry; |
| 157 | unsigned int hash = hash_sha1_string(gitmodules_sha1, path); |
| 158 | struct submodule_entry key; |
| 159 | struct submodule key_config; |
| 160 | |
| 161 | hashcpy(key_config.gitmodules_sha1, gitmodules_sha1); |
| 162 | key_config.path = path; |
| 163 | |
| 164 | hashmap_entry_init(&key, hash); |
| 165 | key.config = &key_config; |
| 166 | |
| 167 | entry = hashmap_get(&cache->for_path, &key, NULL); |
| 168 | if (entry) |
| 169 | return entry->config; |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | static struct submodule *cache_lookup_name(struct submodule_cache *cache, |
| 174 | const unsigned char *gitmodules_sha1, const char *name) |
| 175 | { |
| 176 | struct submodule_entry *entry; |
| 177 | unsigned int hash = hash_sha1_string(gitmodules_sha1, name); |
| 178 | struct submodule_entry key; |
| 179 | struct submodule key_config; |
| 180 | |
| 181 | hashcpy(key_config.gitmodules_sha1, gitmodules_sha1); |
| 182 | key_config.name = name; |
| 183 | |
| 184 | hashmap_entry_init(&key, hash); |
| 185 | key.config = &key_config; |
| 186 | |
| 187 | entry = hashmap_get(&cache->for_name, &key, NULL); |
| 188 | if (entry) |
| 189 | return entry->config; |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | static int name_and_item_from_var(const char *var, struct strbuf *name, |
| 194 | struct strbuf *item) |
| 195 | { |
| 196 | const char *subsection, *key; |
| 197 | int subsection_len, parse; |
| 198 | parse = parse_config_key(var, "submodule", &subsection, |
| 199 | &subsection_len, &key); |
| 200 | if (parse < 0 || !subsection) |
| 201 | return 0; |
| 202 | |
| 203 | strbuf_add(name, subsection, subsection_len); |
| 204 | strbuf_addstr(item, key); |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, |
| 210 | const unsigned char *gitmodules_sha1, const char *name) |
| 211 | { |
| 212 | struct submodule *submodule; |
| 213 | struct strbuf name_buf = STRBUF_INIT; |
| 214 | |
| 215 | submodule = cache_lookup_name(cache, gitmodules_sha1, name); |
| 216 | if (submodule) |
| 217 | return submodule; |
| 218 | |
| 219 | submodule = xmalloc(sizeof(*submodule)); |
| 220 | |
| 221 | strbuf_addstr(&name_buf, name); |
| 222 | submodule->name = strbuf_detach(&name_buf, NULL); |
| 223 | |
| 224 | submodule->path = NULL; |
| 225 | submodule->url = NULL; |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 226 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; |
| 227 | submodule->update_strategy.command = NULL; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 228 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; |
| 229 | submodule->ignore = NULL; |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 230 | submodule->branch = NULL; |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 231 | submodule->recommend_shallow = -1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 232 | |
| 233 | hashcpy(submodule->gitmodules_sha1, gitmodules_sha1); |
| 234 | |
| 235 | cache_add(cache, submodule); |
| 236 | |
| 237 | return submodule; |
| 238 | } |
| 239 | |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 240 | static int parse_fetch_recurse(const char *opt, const char *arg, |
| 241 | int die_on_error) |
| 242 | { |
Martin Ã…gren | 8957661 | 2017-08-07 20:20:49 +0200 | [diff] [blame] | 243 | switch (git_parse_maybe_bool(arg)) { |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 244 | case 1: |
| 245 | return RECURSE_SUBMODULES_ON; |
| 246 | case 0: |
| 247 | return RECURSE_SUBMODULES_OFF; |
| 248 | default: |
| 249 | if (!strcmp(arg, "on-demand")) |
| 250 | return RECURSE_SUBMODULES_ON_DEMAND; |
| 251 | |
| 252 | if (die_on_error) |
| 253 | die("bad %s argument: %s", opt, arg); |
| 254 | else |
| 255 | return RECURSE_SUBMODULES_ERROR; |
| 256 | } |
| 257 | } |
| 258 | |
Brandon Williams | f20e7c1 | 2017-08-02 12:49:18 -0700 | [diff] [blame] | 259 | int parse_submodule_fetchjobs(const char *var, const char *value) |
| 260 | { |
| 261 | int fetchjobs = git_config_int(var, value); |
| 262 | if (fetchjobs < 0) |
| 263 | die(_("negative values not allowed for submodule.fetchjobs")); |
| 264 | return fetchjobs; |
| 265 | } |
| 266 | |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 267 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
| 268 | { |
| 269 | return parse_fetch_recurse(opt, arg, 1); |
| 270 | } |
| 271 | |
Stefan Beller | 886dc15 | 2017-06-23 12:13:00 -0700 | [diff] [blame] | 272 | int option_fetch_parse_recurse_submodules(const struct option *opt, |
| 273 | const char *arg, int unset) |
| 274 | { |
| 275 | int *v; |
| 276 | |
| 277 | if (!opt->value) |
| 278 | return -1; |
| 279 | |
| 280 | v = opt->value; |
| 281 | |
| 282 | if (unset) { |
| 283 | *v = RECURSE_SUBMODULES_OFF; |
| 284 | } else { |
| 285 | if (arg) |
| 286 | *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg); |
| 287 | else |
| 288 | *v = RECURSE_SUBMODULES_ON; |
| 289 | } |
| 290 | return 0; |
| 291 | } |
| 292 | |
Stefan Beller | d601fd0 | 2017-03-14 14:46:32 -0700 | [diff] [blame] | 293 | static int parse_update_recurse(const char *opt, const char *arg, |
| 294 | int die_on_error) |
| 295 | { |
Junio C Hamano | bdfcdef | 2017-08-22 10:29:03 -0700 | [diff] [blame] | 296 | switch (git_parse_maybe_bool(arg)) { |
Stefan Beller | d601fd0 | 2017-03-14 14:46:32 -0700 | [diff] [blame] | 297 | case 1: |
| 298 | return RECURSE_SUBMODULES_ON; |
| 299 | case 0: |
| 300 | return RECURSE_SUBMODULES_OFF; |
| 301 | default: |
| 302 | if (die_on_error) |
| 303 | die("bad %s argument: %s", opt, arg); |
| 304 | return RECURSE_SUBMODULES_ERROR; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) |
| 309 | { |
| 310 | return parse_update_recurse(opt, arg, 1); |
| 311 | } |
| 312 | |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 313 | static int parse_push_recurse(const char *opt, const char *arg, |
| 314 | int die_on_error) |
| 315 | { |
Martin Ã…gren | 8957661 | 2017-08-07 20:20:49 +0200 | [diff] [blame] | 316 | switch (git_parse_maybe_bool(arg)) { |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 317 | case 1: |
| 318 | /* There's no simple "on" value when pushing */ |
| 319 | if (die_on_error) |
| 320 | die("bad %s argument: %s", opt, arg); |
| 321 | else |
| 322 | return RECURSE_SUBMODULES_ERROR; |
| 323 | case 0: |
| 324 | return RECURSE_SUBMODULES_OFF; |
| 325 | default: |
| 326 | if (!strcmp(arg, "on-demand")) |
| 327 | return RECURSE_SUBMODULES_ON_DEMAND; |
| 328 | else if (!strcmp(arg, "check")) |
| 329 | return RECURSE_SUBMODULES_CHECK; |
Brandon Williams | 6c656c3 | 2016-12-19 10:25:32 -0800 | [diff] [blame] | 330 | else if (!strcmp(arg, "only")) |
| 331 | return RECURSE_SUBMODULES_ONLY; |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 332 | else if (die_on_error) |
| 333 | die("bad %s argument: %s", opt, arg); |
| 334 | else |
| 335 | return RECURSE_SUBMODULES_ERROR; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) |
| 340 | { |
| 341 | return parse_push_recurse(opt, arg, 1); |
| 342 | } |
| 343 | |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 344 | static void warn_multiple_config(const unsigned char *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 345 | const char *name, const char *option) |
| 346 | { |
| 347 | const char *commit_string = "WORKTREE"; |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 348 | if (treeish_name) |
| 349 | commit_string = sha1_to_hex(treeish_name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 350 | warning("%s:.gitmodules, multiple configurations found for " |
| 351 | "'submodule.%s.%s'. Skipping second one!", |
| 352 | commit_string, name, option); |
| 353 | } |
| 354 | |
| 355 | struct parse_config_parameter { |
| 356 | struct submodule_cache *cache; |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 357 | const unsigned char *treeish_name; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 358 | const unsigned char *gitmodules_sha1; |
| 359 | int overwrite; |
| 360 | }; |
| 361 | |
| 362 | static int parse_config(const char *var, const char *value, void *data) |
| 363 | { |
| 364 | struct parse_config_parameter *me = data; |
| 365 | struct submodule *submodule; |
| 366 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; |
| 367 | int ret = 0; |
| 368 | |
| 369 | /* this also ensures that we only parse submodule entries */ |
| 370 | if (!name_and_item_from_var(var, &name, &item)) |
| 371 | return 0; |
| 372 | |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 373 | submodule = lookup_or_create_by_name(me->cache, |
| 374 | me->gitmodules_sha1, |
| 375 | name.buf); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 376 | |
| 377 | if (!strcmp(item.buf, "path")) { |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 378 | if (!value) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 379 | ret = config_error_nonbool(var); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 380 | else if (!me->overwrite && submodule->path) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 381 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 382 | "path"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 383 | else { |
| 384 | if (submodule->path) |
| 385 | cache_remove_path(me->cache, submodule); |
| 386 | free((void *) submodule->path); |
| 387 | submodule->path = xstrdup(value); |
| 388 | cache_put_path(me->cache, submodule); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 389 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 390 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 391 | /* when parsing worktree configurations we can die early */ |
| 392 | int die_on_error = is_null_sha1(me->gitmodules_sha1); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 393 | if (!me->overwrite && |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 394 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 395 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 396 | "fetchrecursesubmodules"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 397 | else |
| 398 | submodule->fetch_recurse = parse_fetch_recurse( |
| 399 | var, value, |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 400 | die_on_error); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 401 | } else if (!strcmp(item.buf, "ignore")) { |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 402 | if (!value) |
| 403 | ret = config_error_nonbool(var); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 404 | else if (!me->overwrite && submodule->ignore) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 405 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 406 | "ignore"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 407 | else if (strcmp(value, "untracked") && |
| 408 | strcmp(value, "dirty") && |
| 409 | strcmp(value, "all") && |
| 410 | strcmp(value, "none")) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 411 | warning("Invalid parameter '%s' for config option " |
Stefan Beller | 5ea3048 | 2017-03-14 15:14:40 -0700 | [diff] [blame] | 412 | "'submodule.%s.ignore'", value, name.buf); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 413 | else { |
| 414 | free((void *) submodule->ignore); |
| 415 | submodule->ignore = xstrdup(value); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 416 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 417 | } else if (!strcmp(item.buf, "url")) { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 418 | if (!value) { |
| 419 | ret = config_error_nonbool(var); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 420 | } else if (!me->overwrite && submodule->url) { |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 421 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 422 | "url"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 423 | } else { |
| 424 | free((void *) submodule->url); |
| 425 | submodule->url = xstrdup(value); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 426 | } |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 427 | } else if (!strcmp(item.buf, "update")) { |
| 428 | if (!value) |
| 429 | ret = config_error_nonbool(var); |
| 430 | else if (!me->overwrite && |
| 431 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 432 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 433 | "update"); |
| 434 | else if (parse_submodule_update_strategy(value, |
| 435 | &submodule->update_strategy) < 0) |
| 436 | die(_("invalid value for %s"), var); |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 437 | } else if (!strcmp(item.buf, "shallow")) { |
| 438 | if (!me->overwrite && submodule->recommend_shallow != -1) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 439 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 440 | "shallow"); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 441 | else |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 442 | submodule->recommend_shallow = |
| 443 | git_config_bool(var, value); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 444 | } else if (!strcmp(item.buf, "branch")) { |
| 445 | if (!me->overwrite && submodule->branch) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 446 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 447 | "branch"); |
| 448 | else { |
| 449 | free((void *)submodule->branch); |
| 450 | submodule->branch = xstrdup(value); |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 451 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 454 | strbuf_release(&name); |
| 455 | strbuf_release(&item); |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 460 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, |
| 461 | struct object_id *gitmodules_oid, |
| 462 | struct strbuf *rev) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 463 | { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 464 | int ret = 0; |
| 465 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 466 | if (is_null_oid(treeish_name)) { |
| 467 | oidclr(gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 468 | return 1; |
| 469 | } |
| 470 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 471 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); |
| 472 | if (get_oid(rev->buf, gitmodules_oid) >= 0) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 473 | ret = 1; |
| 474 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | /* This does a lookup of a submodule configuration by name or by path |
| 479 | * (key) with on-demand reading of the appropriate .gitmodules from |
| 480 | * revisions. |
| 481 | */ |
| 482 | static const struct submodule *config_from(struct submodule_cache *cache, |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 483 | const struct object_id *treeish_name, const char *key, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 484 | enum lookup_type lookup_type) |
| 485 | { |
| 486 | struct strbuf rev = STRBUF_INIT; |
| 487 | unsigned long config_size; |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 488 | char *config = NULL; |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 489 | struct object_id oid; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 490 | enum object_type type; |
| 491 | const struct submodule *submodule = NULL; |
| 492 | struct parse_config_parameter parameter; |
| 493 | |
| 494 | /* |
| 495 | * If any parameter except the cache is a NULL pointer just |
| 496 | * return the first submodule. Can be used to check whether |
| 497 | * there are any submodules parsed. |
| 498 | */ |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 499 | if (!treeish_name || !key) { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 500 | struct hashmap_iter iter; |
| 501 | struct submodule_entry *entry; |
| 502 | |
Alexander Kuleshov | 01d98e8 | 2016-03-16 13:46:31 +0600 | [diff] [blame] | 503 | entry = hashmap_iter_first(&cache->for_name, &iter); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 504 | if (!entry) |
| 505 | return NULL; |
| 506 | return entry->config; |
| 507 | } |
| 508 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 509 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 510 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 511 | |
| 512 | switch (lookup_type) { |
| 513 | case lookup_name: |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 514 | submodule = cache_lookup_name(cache, oid.hash, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 515 | break; |
| 516 | case lookup_path: |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 517 | submodule = cache_lookup_path(cache, oid.hash, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | if (submodule) |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 521 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 522 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 523 | config = read_sha1_file(oid.hash, &type, &config_size); |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 524 | if (!config || type != OBJ_BLOB) |
| 525 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 526 | |
| 527 | /* fill the submodule config into the cache */ |
| 528 | parameter.cache = cache; |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 529 | parameter.treeish_name = treeish_name->hash; |
| 530 | parameter.gitmodules_sha1 = oid.hash; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 531 | parameter.overwrite = 0; |
Vasco Almeida | 1b8132d | 2016-07-28 13:14:03 +0000 | [diff] [blame] | 532 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 533 | config, config_size, ¶meter); |
Heiko Voigt | 514dea9 | 2016-07-28 14:49:11 +0200 | [diff] [blame] | 534 | strbuf_release(&rev); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 535 | free(config); |
| 536 | |
| 537 | switch (lookup_type) { |
| 538 | case lookup_name: |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 539 | return cache_lookup_name(cache, oid.hash, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 540 | case lookup_path: |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 541 | return cache_lookup_path(cache, oid.hash, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 542 | default: |
| 543 | return NULL; |
| 544 | } |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 545 | |
| 546 | out: |
| 547 | strbuf_release(&rev); |
| 548 | free(config); |
| 549 | return submodule; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 552 | static void submodule_cache_check_init(struct repository *repo) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 553 | { |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 554 | if (repo->submodule_cache && repo->submodule_cache->initialized) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 555 | return; |
| 556 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 557 | if (!repo->submodule_cache) |
| 558 | repo->submodule_cache = submodule_cache_alloc(); |
| 559 | |
| 560 | submodule_cache_init(repo->submodule_cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 563 | static int gitmodules_cb(const char *var, const char *value, void *data) |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 564 | { |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 565 | struct repository *repo = data; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 566 | struct parse_config_parameter parameter; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 567 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 568 | parameter.cache = repo->submodule_cache; |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 569 | parameter.treeish_name = NULL; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 570 | parameter.gitmodules_sha1 = null_sha1; |
| 571 | parameter.overwrite = 1; |
| 572 | |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 573 | return parse_config(var, value, ¶meter); |
| 574 | } |
| 575 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 576 | void repo_read_gitmodules(struct repository *repo) |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 577 | { |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 578 | submodule_cache_check_init(repo); |
| 579 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 580 | if (repo->worktree) { |
| 581 | char *gitmodules; |
| 582 | |
| 583 | if (repo_read_index(repo) < 0) |
| 584 | return; |
| 585 | |
| 586 | gitmodules = repo_worktree_path(repo, GITMODULES_FILE); |
| 587 | |
| 588 | if (!is_gitmodules_unmerged(repo->index)) |
| 589 | git_config_from_file(gitmodules_cb, gitmodules, repo); |
| 590 | |
| 591 | free(gitmodules); |
| 592 | } |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 593 | |
| 594 | repo->submodule_cache->gitmodules_read = 1; |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void gitmodules_config_oid(const struct object_id *commit_oid) |
| 598 | { |
| 599 | struct strbuf rev = STRBUF_INIT; |
| 600 | struct object_id oid; |
| 601 | |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 602 | submodule_cache_check_init(the_repository); |
| 603 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 604 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { |
| 605 | git_config_from_blob_oid(gitmodules_cb, rev.buf, |
| 606 | &oid, the_repository); |
| 607 | } |
| 608 | strbuf_release(&rev); |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 609 | |
| 610 | the_repository->submodule_cache->gitmodules_read = 1; |
| 611 | } |
| 612 | |
| 613 | static void gitmodules_read_check(struct repository *repo) |
| 614 | { |
| 615 | submodule_cache_check_init(repo); |
| 616 | |
| 617 | /* read the repo's .gitmodules file if it hasn't been already */ |
| 618 | if (!repo->submodule_cache->gitmodules_read) |
| 619 | repo_read_gitmodules(repo); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 620 | } |
| 621 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 622 | const struct submodule *submodule_from_name(const struct object_id *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 623 | const char *name) |
| 624 | { |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 625 | gitmodules_read_check(the_repository); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 626 | return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 627 | } |
| 628 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 629 | const struct submodule *submodule_from_path(const struct object_id *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 630 | const char *path) |
| 631 | { |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 632 | gitmodules_read_check(the_repository); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 633 | return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path); |
| 634 | } |
| 635 | |
| 636 | const struct submodule *submodule_from_cache(struct repository *repo, |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 637 | const struct object_id *treeish_name, |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 638 | const char *key) |
| 639 | { |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 640 | gitmodules_read_check(repo); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 641 | return config_from(repo->submodule_cache, treeish_name, |
| 642 | key, lookup_path); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void submodule_free(void) |
| 646 | { |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 647 | if (the_repository->submodule_cache) |
| 648 | submodule_cache_clear(the_repository->submodule_cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 649 | } |