Elijah Newren | d812c3b | 2023-04-11 00:41:56 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 2 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 3 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 5 | #include "hex.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 6 | #include "path.h" |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 7 | #include "repository.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 9 | #include "submodule-config.h" |
| 10 | #include "submodule.h" |
| 11 | #include "strbuf.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 12 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 13 | #include "object-store-ll.h" |
Stefan Beller | 886dc15 | 2017-06-23 12:13:00 -0700 | [diff] [blame] | 14 | #include "parse-options.h" |
Elijah Newren | e3d2f20 | 2023-04-22 20:17:27 +0000 | [diff] [blame] | 15 | #include "thread-utils.h" |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 16 | #include "tree-walk.h" |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * submodule cache lookup structure |
| 20 | * There is one shared set of 'struct submodule' entries which can be |
Robert P. J. Day | 5aea9fe | 2018-02-13 19:09:31 -0500 | [diff] [blame] | 21 | * looked up by their sha1 blob id of the .gitmodules file and either |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 22 | * using path or name as key. |
| 23 | * for_path stores submodule entries with path as key |
| 24 | * for_name stores submodule entries with name as key |
| 25 | */ |
| 26 | struct submodule_cache { |
| 27 | struct hashmap for_path; |
| 28 | struct hashmap for_name; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 29 | unsigned initialized:1; |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 30 | unsigned gitmodules_read:1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | /* |
| 34 | * thin wrapper struct needed to insert 'struct submodule' entries to |
| 35 | * the hashmap |
| 36 | */ |
| 37 | struct submodule_entry { |
| 38 | struct hashmap_entry ent; |
| 39 | struct submodule *config; |
| 40 | }; |
| 41 | |
| 42 | enum lookup_type { |
| 43 | lookup_name, |
| 44 | lookup_path |
| 45 | }; |
| 46 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 47 | static int config_path_cmp(const void *cmp_data UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 48 | const struct hashmap_entry *eptr, |
| 49 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 50 | const void *keydata UNUSED) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 51 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 52 | const struct submodule_entry *a, *b; |
| 53 | |
| 54 | a = container_of(eptr, const struct submodule_entry, ent); |
| 55 | b = container_of(entry_or_key, const struct submodule_entry, ent); |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 56 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 57 | return strcmp(a->config->path, b->config->path) || |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 58 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 61 | static int config_name_cmp(const void *cmp_data UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 62 | const struct hashmap_entry *eptr, |
| 63 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 64 | const void *keydata UNUSED) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 65 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 66 | const struct submodule_entry *a, *b; |
| 67 | |
| 68 | a = container_of(eptr, const struct submodule_entry, ent); |
| 69 | b = container_of(entry_or_key, const struct submodule_entry, ent); |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 70 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 71 | return strcmp(a->config->name, b->config->name) || |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 72 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 75 | static struct submodule_cache *submodule_cache_alloc(void) |
| 76 | { |
| 77 | return xcalloc(1, sizeof(struct submodule_cache)); |
| 78 | } |
| 79 | |
| 80 | static void submodule_cache_init(struct submodule_cache *cache) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 81 | { |
Stefan Beller | 152cbdc | 2017-06-30 17:28:36 -0700 | [diff] [blame] | 82 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); |
| 83 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 84 | cache->initialized = 1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void free_one_config(struct submodule_entry *entry) |
| 88 | { |
| 89 | free((void *) entry->config->path); |
| 90 | free((void *) entry->config->name); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 91 | free((void *) entry->config->branch); |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 92 | free((void *) entry->config->update_strategy.command); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 93 | free(entry->config); |
| 94 | } |
| 95 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 96 | static void submodule_cache_clear(struct submodule_cache *cache) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 97 | { |
| 98 | struct hashmap_iter iter; |
| 99 | struct submodule_entry *entry; |
| 100 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 101 | if (!cache->initialized) |
| 102 | return; |
| 103 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 104 | /* |
| 105 | * We iterate over the name hash here to be symmetric with the |
| 106 | * allocation of struct submodule entries. Each is allocated by |
Robert P. J. Day | 5aea9fe | 2018-02-13 19:09:31 -0500 | [diff] [blame] | 107 | * their .gitmodules blob sha1 and submodule name. |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 108 | */ |
Eric Wong | 87571c3 | 2019-10-06 23:30:38 +0000 | [diff] [blame] | 109 | hashmap_for_each_entry(&cache->for_name, &iter, entry, |
Eric Wong | 23dee69 | 2019-10-06 23:30:41 +0000 | [diff] [blame] | 110 | ent /* member name */) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 111 | free_one_config(entry); |
| 112 | |
Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 +0000 | [diff] [blame] | 113 | hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent); |
| 114 | hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 115 | cache->initialized = 0; |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 116 | cache->gitmodules_read = 0; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void submodule_cache_free(struct submodule_cache *cache) |
| 120 | { |
| 121 | submodule_cache_clear(cache); |
| 122 | free(cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 123 | } |
| 124 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 125 | static unsigned int hash_oid_string(const struct object_id *oid, |
| 126 | const char *string) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 127 | { |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 128 | return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void cache_put_path(struct submodule_cache *cache, |
| 132 | struct submodule *submodule) |
| 133 | { |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 134 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
| 135 | submodule->path); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 136 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 137 | hashmap_entry_init(&e->ent, hash); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 138 | e->config = submodule; |
Eric Wong | 26b455f | 2019-10-06 23:30:32 +0000 | [diff] [blame] | 139 | hashmap_put(&cache->for_path, &e->ent); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void cache_remove_path(struct submodule_cache *cache, |
| 143 | struct submodule *submodule) |
| 144 | { |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 145 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
| 146 | submodule->path); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 147 | struct submodule_entry e; |
| 148 | struct submodule_entry *removed; |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 149 | hashmap_entry_init(&e.ent, hash); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 150 | e.config = submodule; |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 151 | removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 152 | free(removed); |
| 153 | } |
| 154 | |
| 155 | static void cache_add(struct submodule_cache *cache, |
| 156 | struct submodule *submodule) |
| 157 | { |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 158 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
| 159 | submodule->name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 160 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 161 | hashmap_entry_init(&e->ent, hash); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 162 | e->config = submodule; |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 163 | hashmap_add(&cache->for_name, &e->ent); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 167 | const struct object_id *gitmodules_oid, const char *path) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 168 | { |
| 169 | struct submodule_entry *entry; |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 170 | unsigned int hash = hash_oid_string(gitmodules_oid, path); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 171 | struct submodule_entry key; |
| 172 | struct submodule key_config; |
| 173 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 174 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 175 | key_config.path = path; |
| 176 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 177 | hashmap_entry_init(&key.ent, hash); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 178 | key.config = &key_config; |
| 179 | |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 180 | entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 181 | if (entry) |
| 182 | return entry->config; |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | static struct submodule *cache_lookup_name(struct submodule_cache *cache, |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 187 | const struct object_id *gitmodules_oid, const char *name) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 188 | { |
| 189 | struct submodule_entry *entry; |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 190 | unsigned int hash = hash_oid_string(gitmodules_oid, name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 191 | struct submodule_entry key; |
| 192 | struct submodule key_config; |
| 193 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 194 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 195 | key_config.name = name; |
| 196 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 197 | hashmap_entry_init(&key.ent, hash); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 198 | key.config = &key_config; |
| 199 | |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 200 | entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 201 | if (entry) |
| 202 | return entry->config; |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Jeff King | 0383bbb | 2018-04-30 03:25:25 -0400 | [diff] [blame] | 206 | int check_submodule_name(const char *name) |
| 207 | { |
| 208 | /* Disallow empty names */ |
| 209 | if (!*name) |
| 210 | return -1; |
| 211 | |
| 212 | /* |
Ævar Arnfjörð Bjarmason | 9fd512c | 2022-05-16 20:10:59 +0000 | [diff] [blame] | 213 | * Look for '..' as a path component. Check is_xplatform_dir_sep() as |
Jeff King | 0383bbb | 2018-04-30 03:25:25 -0400 | [diff] [blame] | 214 | * separators rather than is_dir_sep(), because we want the name rules |
| 215 | * to be consistent across platforms. |
| 216 | */ |
| 217 | goto in_component; /* always start inside component */ |
| 218 | while (*name) { |
| 219 | char c = *name++; |
Ævar Arnfjörð Bjarmason | 9fd512c | 2022-05-16 20:10:59 +0000 | [diff] [blame] | 220 | if (is_xplatform_dir_sep(c)) { |
Jeff King | 0383bbb | 2018-04-30 03:25:25 -0400 | [diff] [blame] | 221 | in_component: |
| 222 | if (name[0] == '.' && name[1] == '.' && |
Ævar Arnfjörð Bjarmason | 9fd512c | 2022-05-16 20:10:59 +0000 | [diff] [blame] | 223 | (!name[2] || is_xplatform_dir_sep(name[2]))) |
Jeff King | 0383bbb | 2018-04-30 03:25:25 -0400 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 231 | static int name_and_item_from_var(const char *var, struct strbuf *name, |
| 232 | struct strbuf *item) |
| 233 | { |
| 234 | const char *subsection, *key; |
Jeff King | f5914f4 | 2020-04-10 15:44:28 -0400 | [diff] [blame] | 235 | size_t subsection_len; |
| 236 | int parse; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 237 | parse = parse_config_key(var, "submodule", &subsection, |
| 238 | &subsection_len, &key); |
| 239 | if (parse < 0 || !subsection) |
| 240 | return 0; |
| 241 | |
| 242 | strbuf_add(name, subsection, subsection_len); |
Jeff King | 0383bbb | 2018-04-30 03:25:25 -0400 | [diff] [blame] | 243 | if (check_submodule_name(name->buf) < 0) { |
| 244 | warning(_("ignoring suspicious submodule name: %s"), name->buf); |
| 245 | strbuf_release(name); |
| 246 | return 0; |
| 247 | } |
| 248 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 249 | strbuf_addstr(item, key); |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 255 | const struct object_id *gitmodules_oid, const char *name) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 256 | { |
| 257 | struct submodule *submodule; |
| 258 | struct strbuf name_buf = STRBUF_INIT; |
| 259 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 260 | submodule = cache_lookup_name(cache, gitmodules_oid, name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 261 | if (submodule) |
| 262 | return submodule; |
| 263 | |
| 264 | submodule = xmalloc(sizeof(*submodule)); |
| 265 | |
| 266 | strbuf_addstr(&name_buf, name); |
| 267 | submodule->name = strbuf_detach(&name_buf, NULL); |
| 268 | |
| 269 | submodule->path = NULL; |
| 270 | submodule->url = NULL; |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 271 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; |
| 272 | submodule->update_strategy.command = NULL; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 273 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; |
| 274 | submodule->ignore = NULL; |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 275 | submodule->branch = NULL; |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 276 | submodule->recommend_shallow = -1; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 277 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 278 | oidcpy(&submodule->gitmodules_oid, gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 279 | |
| 280 | cache_add(cache, submodule); |
| 281 | |
| 282 | return submodule; |
| 283 | } |
| 284 | |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 285 | static int parse_fetch_recurse(const char *opt, const char *arg, |
| 286 | int die_on_error) |
| 287 | { |
Martin Ågren | 8957661 | 2017-08-07 20:20:49 +0200 | [diff] [blame] | 288 | switch (git_parse_maybe_bool(arg)) { |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 289 | case 1: |
| 290 | return RECURSE_SUBMODULES_ON; |
| 291 | case 0: |
| 292 | return RECURSE_SUBMODULES_OFF; |
| 293 | default: |
| 294 | if (!strcmp(arg, "on-demand")) |
| 295 | return RECURSE_SUBMODULES_ON_DEMAND; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 296 | /* |
| 297 | * Please update $__git_fetch_recurse_submodules in |
| 298 | * git-completion.bash when you add new options. |
| 299 | */ |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 300 | if (die_on_error) |
| 301 | die("bad %s argument: %s", opt, arg); |
| 302 | else |
| 303 | return RECURSE_SUBMODULES_ERROR; |
| 304 | } |
| 305 | } |
| 306 | |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 307 | int parse_submodule_fetchjobs(const char *var, const char *value, |
| 308 | const struct key_value_info *kvi) |
Brandon Williams | f20e7c1 | 2017-08-02 12:49:18 -0700 | [diff] [blame] | 309 | { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 310 | int fetchjobs = git_config_int(var, value, kvi); |
Brandon Williams | f20e7c1 | 2017-08-02 12:49:18 -0700 | [diff] [blame] | 311 | if (fetchjobs < 0) |
Jiang Xin | b4eda05 | 2022-06-17 18:03:09 +0800 | [diff] [blame] | 312 | die(_("negative values not allowed for submodule.fetchJobs")); |
Ævar Arnfjörð Bjarmason | 51243f9 | 2022-10-12 23:02:24 +0200 | [diff] [blame] | 313 | if (!fetchjobs) |
| 314 | fetchjobs = online_cpus(); |
Brandon Williams | f20e7c1 | 2017-08-02 12:49:18 -0700 | [diff] [blame] | 315 | return fetchjobs; |
| 316 | } |
| 317 | |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 318 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
| 319 | { |
| 320 | return parse_fetch_recurse(opt, arg, 1); |
| 321 | } |
| 322 | |
Stefan Beller | 886dc15 | 2017-06-23 12:13:00 -0700 | [diff] [blame] | 323 | int option_fetch_parse_recurse_submodules(const struct option *opt, |
| 324 | const char *arg, int unset) |
| 325 | { |
| 326 | int *v; |
| 327 | |
| 328 | if (!opt->value) |
| 329 | return -1; |
| 330 | |
| 331 | v = opt->value; |
| 332 | |
| 333 | if (unset) { |
| 334 | *v = RECURSE_SUBMODULES_OFF; |
| 335 | } else { |
| 336 | if (arg) |
| 337 | *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg); |
| 338 | else |
| 339 | *v = RECURSE_SUBMODULES_ON; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
Stefan Beller | d601fd0 | 2017-03-14 14:46:32 -0700 | [diff] [blame] | 344 | static int parse_update_recurse(const char *opt, const char *arg, |
| 345 | int die_on_error) |
| 346 | { |
Junio C Hamano | bdfcdef | 2017-08-22 10:29:03 -0700 | [diff] [blame] | 347 | switch (git_parse_maybe_bool(arg)) { |
Stefan Beller | d601fd0 | 2017-03-14 14:46:32 -0700 | [diff] [blame] | 348 | case 1: |
| 349 | return RECURSE_SUBMODULES_ON; |
| 350 | case 0: |
| 351 | return RECURSE_SUBMODULES_OFF; |
| 352 | default: |
| 353 | if (die_on_error) |
| 354 | die("bad %s argument: %s", opt, arg); |
| 355 | return RECURSE_SUBMODULES_ERROR; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) |
| 360 | { |
| 361 | return parse_update_recurse(opt, arg, 1); |
| 362 | } |
| 363 | |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 364 | static int parse_push_recurse(const char *opt, const char *arg, |
| 365 | int die_on_error) |
| 366 | { |
Martin Ågren | 8957661 | 2017-08-07 20:20:49 +0200 | [diff] [blame] | 367 | switch (git_parse_maybe_bool(arg)) { |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 368 | case 1: |
| 369 | /* There's no simple "on" value when pushing */ |
| 370 | if (die_on_error) |
| 371 | die("bad %s argument: %s", opt, arg); |
| 372 | else |
| 373 | return RECURSE_SUBMODULES_ERROR; |
| 374 | case 0: |
| 375 | return RECURSE_SUBMODULES_OFF; |
| 376 | default: |
| 377 | if (!strcmp(arg, "on-demand")) |
| 378 | return RECURSE_SUBMODULES_ON_DEMAND; |
| 379 | else if (!strcmp(arg, "check")) |
| 380 | return RECURSE_SUBMODULES_CHECK; |
Brandon Williams | 6c656c3 | 2016-12-19 10:25:32 -0800 | [diff] [blame] | 381 | else if (!strcmp(arg, "only")) |
| 382 | return RECURSE_SUBMODULES_ONLY; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 383 | /* |
| 384 | * Please update $__git_push_recurse_submodules in |
| 385 | * git-completion.bash when you add new modes. |
| 386 | */ |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 387 | else if (die_on_error) |
| 388 | die("bad %s argument: %s", opt, arg); |
| 389 | else |
| 390 | return RECURSE_SUBMODULES_ERROR; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) |
| 395 | { |
| 396 | return parse_push_recurse(opt, arg, 1); |
| 397 | } |
| 398 | |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 399 | static void warn_multiple_config(const struct object_id *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 400 | const char *name, const char *option) |
| 401 | { |
| 402 | const char *commit_string = "WORKTREE"; |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 403 | if (treeish_name) |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 404 | commit_string = oid_to_hex(treeish_name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 405 | warning("%s:.gitmodules, multiple configurations found for " |
| 406 | "'submodule.%s.%s'. Skipping second one!", |
| 407 | commit_string, name, option); |
| 408 | } |
| 409 | |
Jeff King | f6adec4 | 2018-09-24 04:36:30 -0400 | [diff] [blame] | 410 | static void warn_command_line_option(const char *var, const char *value) |
| 411 | { |
| 412 | warning(_("ignoring '%s' which may be interpreted as" |
| 413 | " a command-line option: %s"), var, value); |
| 414 | } |
| 415 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 416 | struct parse_config_parameter { |
| 417 | struct submodule_cache *cache; |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 418 | const struct object_id *treeish_name; |
| 419 | const struct object_id *gitmodules_oid; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 420 | int overwrite; |
| 421 | }; |
| 422 | |
Jonathan Nieder | e904deb | 2019-12-05 01:28:28 -0800 | [diff] [blame] | 423 | /* |
| 424 | * Parse a config item from .gitmodules. |
| 425 | * |
| 426 | * This does not handle submodule-related configuration from the main |
| 427 | * config store (.git/config, etc). Callers are responsible for |
| 428 | * checking for overrides in the main config store when appropriate. |
| 429 | */ |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 430 | static int parse_config(const char *var, const char *value, |
| 431 | const struct config_context *ctx UNUSED, void *data) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 432 | { |
| 433 | struct parse_config_parameter *me = data; |
| 434 | struct submodule *submodule; |
| 435 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; |
| 436 | int ret = 0; |
| 437 | |
| 438 | /* this also ensures that we only parse submodule entries */ |
| 439 | if (!name_and_item_from_var(var, &name, &item)) |
| 440 | return 0; |
| 441 | |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 442 | submodule = lookup_or_create_by_name(me->cache, |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 443 | me->gitmodules_oid, |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 444 | name.buf); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 445 | |
| 446 | if (!strcmp(item.buf, "path")) { |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 447 | if (!value) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 448 | ret = config_error_nonbool(var); |
Jeff King | 273c614 | 2018-09-24 04:39:55 -0400 | [diff] [blame] | 449 | else if (looks_like_command_line_option(value)) |
| 450 | warn_command_line_option(var, value); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 451 | else if (!me->overwrite && submodule->path) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 452 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 453 | "path"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 454 | else { |
| 455 | if (submodule->path) |
| 456 | cache_remove_path(me->cache, submodule); |
| 457 | free((void *) submodule->path); |
| 458 | submodule->path = xstrdup(value); |
| 459 | cache_put_path(me->cache, submodule); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 460 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 461 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 462 | /* when parsing worktree configurations we can die early */ |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 463 | int die_on_error = is_null_oid(me->gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 464 | if (!me->overwrite && |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 465 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 466 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 467 | "fetchrecursesubmodules"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 468 | else |
| 469 | submodule->fetch_recurse = parse_fetch_recurse( |
| 470 | var, value, |
Heiko Voigt | 027771f | 2015-08-17 17:22:00 -0700 | [diff] [blame] | 471 | die_on_error); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 472 | } else if (!strcmp(item.buf, "ignore")) { |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 473 | if (!value) |
| 474 | ret = config_error_nonbool(var); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 475 | else if (!me->overwrite && submodule->ignore) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 476 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 477 | "ignore"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 478 | else if (strcmp(value, "untracked") && |
| 479 | strcmp(value, "dirty") && |
| 480 | strcmp(value, "all") && |
| 481 | strcmp(value, "none")) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 482 | warning("Invalid parameter '%s' for config option " |
Stefan Beller | 5ea3048 | 2017-03-14 15:14:40 -0700 | [diff] [blame] | 483 | "'submodule.%s.ignore'", value, name.buf); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 484 | else { |
| 485 | free((void *) submodule->ignore); |
| 486 | submodule->ignore = xstrdup(value); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 487 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 488 | } else if (!strcmp(item.buf, "url")) { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 489 | if (!value) { |
| 490 | ret = config_error_nonbool(var); |
Jeff King | f6adec4 | 2018-09-24 04:36:30 -0400 | [diff] [blame] | 491 | } else if (looks_like_command_line_option(value)) { |
| 492 | warn_command_line_option(var, value); |
Stefan Beller | f73da11 | 2016-02-29 18:07:12 -0800 | [diff] [blame] | 493 | } else if (!me->overwrite && submodule->url) { |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 494 | warn_multiple_config(me->treeish_name, submodule->name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 495 | "url"); |
Stefan Beller | 147875f | 2015-10-12 10:58:58 -0700 | [diff] [blame] | 496 | } else { |
| 497 | free((void *) submodule->url); |
| 498 | submodule->url = xstrdup(value); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 499 | } |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 500 | } else if (!strcmp(item.buf, "update")) { |
| 501 | if (!value) |
| 502 | ret = config_error_nonbool(var); |
| 503 | else if (!me->overwrite && |
| 504 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 505 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 506 | "update"); |
| 507 | else if (parse_submodule_update_strategy(value, |
Jonathan Nieder | e904deb | 2019-12-05 01:28:28 -0800 | [diff] [blame] | 508 | &submodule->update_strategy) < 0 || |
| 509 | submodule->update_strategy.type == SM_UPDATE_COMMAND) |
Jean-Noël Avila | 1a8aea8 | 2022-01-31 22:07:47 +0000 | [diff] [blame] | 510 | die(_("invalid value for '%s'"), var); |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 511 | } else if (!strcmp(item.buf, "shallow")) { |
| 512 | if (!me->overwrite && submodule->recommend_shallow != -1) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 513 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 514 | "shallow"); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 515 | else |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 516 | submodule->recommend_shallow = |
| 517 | git_config_bool(var, value); |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 518 | } else if (!strcmp(item.buf, "branch")) { |
Jeff King | 34b1a0d | 2023-12-07 02:11:29 -0500 | [diff] [blame] | 519 | if (!value) |
| 520 | ret = config_error_nonbool(var); |
| 521 | else if (!me->overwrite && submodule->branch) |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 522 | warn_multiple_config(me->treeish_name, submodule->name, |
Stefan Beller | b5944f3 | 2016-07-28 17:44:07 -0700 | [diff] [blame] | 523 | "branch"); |
| 524 | else { |
| 525 | free((void *)submodule->branch); |
| 526 | submodule->branch = xstrdup(value); |
Stefan Beller | 37f52e9 | 2016-05-26 14:59:42 -0700 | [diff] [blame] | 527 | } |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 530 | strbuf_release(&name); |
| 531 | strbuf_release(&item); |
| 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 536 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, |
| 537 | struct object_id *gitmodules_oid, |
| 538 | struct strbuf *rev) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 539 | { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 540 | int ret = 0; |
| 541 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 542 | if (is_null_oid(treeish_name)) { |
| 543 | oidclr(gitmodules_oid); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 544 | return 1; |
| 545 | } |
| 546 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 547 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 548 | if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 549 | ret = 1; |
| 550 | |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* This does a lookup of a submodule configuration by name or by path |
| 555 | * (key) with on-demand reading of the appropriate .gitmodules from |
| 556 | * revisions. |
| 557 | */ |
| 558 | static const struct submodule *config_from(struct submodule_cache *cache, |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 559 | const struct object_id *treeish_name, const char *key, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 560 | enum lookup_type lookup_type) |
| 561 | { |
| 562 | struct strbuf rev = STRBUF_INIT; |
| 563 | unsigned long config_size; |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 564 | char *config = NULL; |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 565 | struct object_id oid; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 566 | enum object_type type; |
| 567 | const struct submodule *submodule = NULL; |
| 568 | struct parse_config_parameter parameter; |
| 569 | |
| 570 | /* |
| 571 | * If any parameter except the cache is a NULL pointer just |
| 572 | * return the first submodule. Can be used to check whether |
| 573 | * there are any submodules parsed. |
| 574 | */ |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 575 | if (!treeish_name || !key) { |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 576 | struct hashmap_iter iter; |
| 577 | struct submodule_entry *entry; |
| 578 | |
Eric Wong | 87571c3 | 2019-10-06 23:30:38 +0000 | [diff] [blame] | 579 | entry = hashmap_iter_first_entry(&cache->for_name, &iter, |
| 580 | struct submodule_entry, |
| 581 | ent /* member name */); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 582 | if (!entry) |
| 583 | return NULL; |
| 584 | return entry->config; |
| 585 | } |
| 586 | |
brian m. carlson | cd73de4 | 2017-07-13 23:49:20 +0000 | [diff] [blame] | 587 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 588 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 589 | |
| 590 | switch (lookup_type) { |
| 591 | case lookup_name: |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 592 | submodule = cache_lookup_name(cache, &oid, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 593 | break; |
| 594 | case lookup_path: |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 595 | submodule = cache_lookup_path(cache, &oid, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | if (submodule) |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 599 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 600 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 601 | config = repo_read_object_file(the_repository, &oid, &type, |
| 602 | &config_size); |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 603 | if (!config || type != OBJ_BLOB) |
| 604 | goto out; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 605 | |
| 606 | /* fill the submodule config into the cache */ |
| 607 | parameter.cache = cache; |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 608 | parameter.treeish_name = treeish_name; |
| 609 | parameter.gitmodules_oid = &oid; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 610 | parameter.overwrite = 0; |
Vasco Almeida | 1b8132d | 2016-07-28 13:14:03 +0000 | [diff] [blame] | 611 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, |
Glen Choo | 809d868 | 2023-06-28 19:26:24 +0000 | [diff] [blame] | 612 | config, config_size, ¶meter, CONFIG_SCOPE_UNKNOWN, NULL); |
Heiko Voigt | 514dea9 | 2016-07-28 14:49:11 +0200 | [diff] [blame] | 613 | strbuf_release(&rev); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 614 | free(config); |
| 615 | |
| 616 | switch (lookup_type) { |
| 617 | case lookup_name: |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 618 | return cache_lookup_name(cache, &oid, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 619 | case lookup_path: |
brian m. carlson | 34caab0 | 2018-05-02 00:25:42 +0000 | [diff] [blame] | 620 | return cache_lookup_path(cache, &oid, key); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 621 | default: |
| 622 | return NULL; |
| 623 | } |
Heiko Voigt | 0918e25 | 2016-07-28 14:49:47 +0200 | [diff] [blame] | 624 | |
| 625 | out: |
| 626 | strbuf_release(&rev); |
| 627 | free(config); |
| 628 | return submodule; |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 631 | static void submodule_cache_check_init(struct repository *repo) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 632 | { |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 633 | if (repo->submodule_cache && repo->submodule_cache->initialized) |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 634 | return; |
| 635 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 636 | if (!repo->submodule_cache) |
| 637 | repo->submodule_cache = submodule_cache_alloc(); |
| 638 | |
| 639 | submodule_cache_init(repo->submodule_cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Antonio Ospite | db64d11 | 2018-06-26 12:47:10 +0200 | [diff] [blame] | 642 | /* |
| 643 | * Note: This function is private for a reason, the '.gitmodules' file should |
ryenus | 571fb96 | 2019-12-15 15:12:24 +0000 | [diff] [blame] | 644 | * not be used as a mechanism to retrieve arbitrary configuration stored in |
Antonio Ospite | db64d11 | 2018-06-26 12:47:10 +0200 | [diff] [blame] | 645 | * the repository. |
| 646 | * |
| 647 | * Runs the provided config function on the '.gitmodules' file found in the |
| 648 | * working directory. |
| 649 | */ |
| 650 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) |
| 651 | { |
| 652 | if (repo->worktree) { |
Matthew Rogers | 9a83d08 | 2020-02-10 00:30:58 +0000 | [diff] [blame] | 653 | struct git_config_source config_source = { |
| 654 | 0, .scope = CONFIG_SCOPE_SUBMODULE |
| 655 | }; |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 656 | const struct config_options opts = { 0 }; |
| 657 | struct object_id oid; |
| 658 | char *file; |
Nguyễn Thái Ngọc Duy | d9b8b8f | 2019-04-16 16:33:38 +0700 | [diff] [blame] | 659 | char *oidstr = NULL; |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 660 | |
| 661 | file = repo_worktree_path(repo, GITMODULES_FILE); |
| 662 | if (file_exists(file)) { |
| 663 | config_source.file = file; |
Nguyễn Thái Ngọc Duy | d9b8b8f | 2019-04-16 16:33:38 +0700 | [diff] [blame] | 664 | } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 || |
| 665 | repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) { |
| 666 | config_source.blob = oidstr = xstrdup(oid_to_hex(&oid)); |
| 667 | if (repo != the_repository) |
Jonathan Tan | e3e8bf0 | 2021-08-16 14:09:57 -0700 | [diff] [blame] | 668 | add_submodule_odb_by_path(repo->objects->odb->path); |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 669 | } else { |
| 670 | goto out; |
| 671 | } |
| 672 | |
Victoria Dye | 9b6b06c | 2023-05-26 01:32:59 +0000 | [diff] [blame] | 673 | config_with_options(fn, data, &config_source, repo, &opts); |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 674 | |
| 675 | out: |
Nguyễn Thái Ngọc Duy | d9b8b8f | 2019-04-16 16:33:38 +0700 | [diff] [blame] | 676 | free(oidstr); |
Antonio Ospite | db64d11 | 2018-06-26 12:47:10 +0200 | [diff] [blame] | 677 | free(file); |
| 678 | } |
| 679 | } |
| 680 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 681 | static int gitmodules_cb(const char *var, const char *value, |
| 682 | const struct config_context *ctx, void *data) |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 683 | { |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 684 | struct repository *repo = data; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 685 | struct parse_config_parameter parameter; |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 686 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 687 | parameter.cache = repo->submodule_cache; |
Stefan Beller | 73c293b | 2016-11-22 12:14:37 -0800 | [diff] [blame] | 688 | parameter.treeish_name = NULL; |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 689 | parameter.gitmodules_oid = null_oid(); |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 690 | parameter.overwrite = 1; |
| 691 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 692 | return parse_config(var, value, ctx, ¶meter); |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 695 | void repo_read_gitmodules(struct repository *repo, int skip_if_read) |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 696 | { |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 697 | submodule_cache_check_init(repo); |
| 698 | |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 699 | if (repo->submodule_cache->gitmodules_read && skip_if_read) |
| 700 | return; |
| 701 | |
Antonio Ospite | db64d11 | 2018-06-26 12:47:10 +0200 | [diff] [blame] | 702 | if (repo_read_index(repo) < 0) |
| 703 | return; |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 704 | |
Antonio Ospite | db64d11 | 2018-06-26 12:47:10 +0200 | [diff] [blame] | 705 | if (!is_gitmodules_unmerged(repo->index)) |
| 706 | config_from_gitmodules(gitmodules_cb, repo, repo); |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 707 | |
| 708 | repo->submodule_cache->gitmodules_read = 1; |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void gitmodules_config_oid(const struct object_id *commit_oid) |
| 712 | { |
| 713 | struct strbuf rev = STRBUF_INIT; |
| 714 | struct object_id oid; |
| 715 | |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 716 | submodule_cache_check_init(the_repository); |
| 717 | |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 718 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { |
| 719 | git_config_from_blob_oid(gitmodules_cb, rev.buf, |
Glen Choo | 809d868 | 2023-06-28 19:26:24 +0000 | [diff] [blame] | 720 | the_repository, &oid, the_repository, |
| 721 | CONFIG_SCOPE_UNKNOWN); |
Brandon Williams | 1b796ac | 2017-08-03 11:19:57 -0700 | [diff] [blame] | 722 | } |
| 723 | strbuf_release(&rev); |
Brandon Williams | ff6f1f5 | 2017-08-03 11:19:58 -0700 | [diff] [blame] | 724 | |
| 725 | the_repository->submodule_cache->gitmodules_read = 1; |
| 726 | } |
| 727 | |
Stefan Beller | 3b8fb39 | 2018-03-28 15:35:29 -0700 | [diff] [blame] | 728 | const struct submodule *submodule_from_name(struct repository *r, |
| 729 | const struct object_id *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 730 | const char *name) |
| 731 | { |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 732 | repo_read_gitmodules(r, 1); |
Stefan Beller | 3b8fb39 | 2018-03-28 15:35:29 -0700 | [diff] [blame] | 733 | return config_from(r->submodule_cache, treeish_name, name, lookup_name); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Stefan Beller | 3b8fb39 | 2018-03-28 15:35:29 -0700 | [diff] [blame] | 736 | const struct submodule *submodule_from_path(struct repository *r, |
| 737 | const struct object_id *treeish_name, |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 738 | const char *path) |
| 739 | { |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 740 | repo_read_gitmodules(r, 1); |
Stefan Beller | 3b8fb39 | 2018-03-28 15:35:29 -0700 | [diff] [blame] | 741 | return config_from(r->submodule_cache, treeish_name, path, lookup_path); |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 744 | /** |
| 745 | * Used internally by submodules_of_tree(). Recurses into 'treeish_name' |
| 746 | * and appends submodule entries to 'out'. The submodule_cache expects |
| 747 | * a root-level treeish_name and paths, so keep track of these values |
| 748 | * with 'root_tree' and 'prefix'. |
| 749 | */ |
| 750 | static void traverse_tree_submodules(struct repository *r, |
| 751 | const struct object_id *root_tree, |
| 752 | char *prefix, |
| 753 | const struct object_id *treeish_name, |
| 754 | struct submodule_entry_list *out) |
| 755 | { |
| 756 | struct tree_desc tree; |
| 757 | struct submodule_tree_entry *st_entry; |
| 758 | struct name_entry *name_entry; |
| 759 | char *tree_path = NULL; |
| 760 | |
| 761 | name_entry = xmalloc(sizeof(*name_entry)); |
| 762 | |
| 763 | fill_tree_descriptor(r, &tree, treeish_name); |
| 764 | while (tree_entry(&tree, name_entry)) { |
| 765 | if (prefix) |
| 766 | tree_path = |
| 767 | mkpathdup("%s/%s", prefix, name_entry->path); |
| 768 | else |
| 769 | tree_path = xstrdup(name_entry->path); |
| 770 | |
| 771 | if (S_ISGITLINK(name_entry->mode) && |
| 772 | is_tree_submodule_active(r, root_tree, tree_path)) { |
Johannes Schindelin | f535592 | 2022-06-15 23:35:39 +0000 | [diff] [blame] | 773 | ALLOC_GROW(out->entries, out->entry_nr + 1, |
| 774 | out->entry_alloc); |
| 775 | st_entry = &out->entries[out->entry_nr++]; |
| 776 | |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 777 | st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry)); |
| 778 | *st_entry->name_entry = *name_entry; |
| 779 | st_entry->submodule = |
| 780 | submodule_from_path(r, root_tree, tree_path); |
| 781 | st_entry->repo = xmalloc(sizeof(*st_entry->repo)); |
| 782 | if (repo_submodule_init(st_entry->repo, r, tree_path, |
| 783 | root_tree)) |
| 784 | FREE_AND_NULL(st_entry->repo); |
| 785 | |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 786 | } else if (S_ISDIR(name_entry->mode)) |
| 787 | traverse_tree_submodules(r, root_tree, tree_path, |
| 788 | &name_entry->oid, out); |
| 789 | free(tree_path); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | void submodules_of_tree(struct repository *r, |
| 794 | const struct object_id *treeish_name, |
| 795 | struct submodule_entry_list *out) |
| 796 | { |
| 797 | CALLOC_ARRAY(out->entries, 0); |
| 798 | out->entry_nr = 0; |
| 799 | out->entry_alloc = 0; |
| 800 | |
| 801 | traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out); |
| 802 | } |
| 803 | |
Stefan Beller | f793b89 | 2018-03-28 15:35:28 -0700 | [diff] [blame] | 804 | void submodule_free(struct repository *r) |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 805 | { |
Stefan Beller | f793b89 | 2018-03-28 15:35:28 -0700 | [diff] [blame] | 806 | if (r->submodule_cache) |
| 807 | submodule_cache_clear(r->submodule_cache); |
Heiko Voigt | 959b545 | 2015-08-17 17:21:57 -0700 | [diff] [blame] | 808 | } |
Antonio Ospite | ad13637 | 2018-06-26 12:47:05 +0200 | [diff] [blame] | 809 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 810 | static int config_print_callback(const char *var, const char *value, |
| 811 | const struct config_context *ctx UNUSED, |
| 812 | void *cb_data) |
Antonio Ospite | bcbc780 | 2018-10-05 15:05:52 +0200 | [diff] [blame] | 813 | { |
| 814 | char *wanted_key = cb_data; |
| 815 | |
| 816 | if (!strcmp(wanted_key, var)) |
| 817 | printf("%s\n", value); |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | int print_config_from_gitmodules(struct repository *repo, const char *key) |
| 823 | { |
| 824 | int ret; |
| 825 | char *store_key; |
| 826 | |
| 827 | ret = git_config_parse_key(key, &store_key, NULL); |
| 828 | if (ret < 0) |
| 829 | return CONFIG_INVALID_KEY; |
| 830 | |
| 831 | config_from_gitmodules(config_print_callback, repo, store_key); |
| 832 | |
| 833 | free(store_key); |
| 834 | return 0; |
| 835 | } |
| 836 | |
Antonio Ospite | 45f5ef3 | 2018-10-05 15:05:53 +0200 | [diff] [blame] | 837 | int config_set_in_gitmodules_file_gently(const char *key, const char *value) |
| 838 | { |
| 839 | int ret; |
| 840 | |
| 841 | ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value); |
| 842 | if (ret < 0) |
| 843 | /* Maybe the user already did that, don't error out here */ |
| 844 | warning(_("Could not update .gitmodules entry %s"), key); |
| 845 | |
| 846 | return ret; |
| 847 | } |
| 848 | |
Antonio Ospite | 71a6953 | 2018-06-26 12:47:06 +0200 | [diff] [blame] | 849 | struct fetch_config { |
| 850 | int *max_children; |
| 851 | int *recurse_submodules; |
| 852 | }; |
| 853 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 854 | static int gitmodules_fetch_config(const char *var, const char *value, |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 855 | const struct config_context *ctx, |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 856 | void *cb) |
Antonio Ospite | 71a6953 | 2018-06-26 12:47:06 +0200 | [diff] [blame] | 857 | { |
| 858 | struct fetch_config *config = cb; |
| 859 | if (!strcmp(var, "submodule.fetchjobs")) { |
Jonathan Tan | e5b9421 | 2020-08-17 21:01:33 -0700 | [diff] [blame] | 860 | if (config->max_children) |
| 861 | *(config->max_children) = |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 862 | parse_submodule_fetchjobs(var, value, ctx->kvi); |
Antonio Ospite | 71a6953 | 2018-06-26 12:47:06 +0200 | [diff] [blame] | 863 | return 0; |
| 864 | } else if (!strcmp(var, "fetch.recursesubmodules")) { |
Jonathan Tan | e5b9421 | 2020-08-17 21:01:33 -0700 | [diff] [blame] | 865 | if (config->recurse_submodules) |
| 866 | *(config->recurse_submodules) = |
| 867 | parse_fetch_recurse_submodules_arg(var, value); |
Antonio Ospite | 71a6953 | 2018-06-26 12:47:06 +0200 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules) |
| 875 | { |
| 876 | struct fetch_config config = { |
| 877 | .max_children = max_children, |
| 878 | .recurse_submodules = recurse_submodules |
| 879 | }; |
Antonio Ospite | 9a0fb3e | 2018-06-26 12:47:09 +0200 | [diff] [blame] | 880 | config_from_gitmodules(gitmodules_fetch_config, the_repository, &config); |
Antonio Ospite | 71a6953 | 2018-06-26 12:47:06 +0200 | [diff] [blame] | 881 | } |
Antonio Ospite | 0574499 | 2018-06-26 12:47:07 +0200 | [diff] [blame] | 882 | |
| 883 | static int gitmodules_update_clone_config(const char *var, const char *value, |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 884 | const struct config_context *ctx, |
Antonio Ospite | 0574499 | 2018-06-26 12:47:07 +0200 | [diff] [blame] | 885 | void *cb) |
| 886 | { |
| 887 | int *max_jobs = cb; |
| 888 | if (!strcmp(var, "submodule.fetchjobs")) |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 889 | *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi); |
Antonio Ospite | 0574499 | 2018-06-26 12:47:07 +0200 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | void update_clone_config_from_gitmodules(int *max_jobs) |
| 894 | { |
Antonio Ospite | 9a0fb3e | 2018-06-26 12:47:09 +0200 | [diff] [blame] | 895 | config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs); |
Antonio Ospite | 0574499 | 2018-06-26 12:47:07 +0200 | [diff] [blame] | 896 | } |