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