submodule-config.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #include "cache.h"
  2. #include "dir.h"
  3. #include "repository.h"
  4. #include "config.h"
  5. #include "submodule-config.h"
  6. #include "submodule.h"
  7. #include "strbuf.h"
  8. #include "object-store.h"
  9. #include "parse-options.h"
  10. /*
  11. * submodule cache lookup structure
  12. * There is one shared set of 'struct submodule' entries which can be
  13. * looked up by their sha1 blob id of the .gitmodules file and either
  14. * using path or name as key.
  15. * for_path stores submodule entries with path as key
  16. * for_name stores submodule entries with name as key
  17. */
  18. struct submodule_cache {
  19. struct hashmap for_path;
  20. struct hashmap for_name;
  21. unsigned initialized:1;
  22. unsigned gitmodules_read:1;
  23. };
  24. /*
  25. * thin wrapper struct needed to insert 'struct submodule' entries to
  26. * the hashmap
  27. */
  28. struct submodule_entry {
  29. struct hashmap_entry ent;
  30. struct submodule *config;
  31. };
  32. enum lookup_type {
  33. lookup_name,
  34. lookup_path
  35. };
  36. static int config_path_cmp(const void *unused_cmp_data,
  37. const struct hashmap_entry *eptr,
  38. const struct hashmap_entry *entry_or_key,
  39. const void *unused_keydata)
  40. {
  41. const struct submodule_entry *a, *b;
  42. a = container_of(eptr, const struct submodule_entry, ent);
  43. b = container_of(entry_or_key, const struct submodule_entry, ent);
  44. return strcmp(a->config->path, b->config->path) ||
  45. !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
  46. }
  47. static int config_name_cmp(const void *unused_cmp_data,
  48. const struct hashmap_entry *eptr,
  49. const struct hashmap_entry *entry_or_key,
  50. const void *unused_keydata)
  51. {
  52. const struct submodule_entry *a, *b;
  53. a = container_of(eptr, const struct submodule_entry, ent);
  54. b = container_of(entry_or_key, const struct submodule_entry, ent);
  55. return strcmp(a->config->name, b->config->name) ||
  56. !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
  57. }
  58. static struct submodule_cache *submodule_cache_alloc(void)
  59. {
  60. return xcalloc(1, sizeof(struct submodule_cache));
  61. }
  62. static void submodule_cache_init(struct submodule_cache *cache)
  63. {
  64. hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
  65. hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
  66. cache->initialized = 1;
  67. }
  68. static void free_one_config(struct submodule_entry *entry)
  69. {
  70. free((void *) entry->config->path);
  71. free((void *) entry->config->name);
  72. free((void *) entry->config->branch);
  73. free((void *) entry->config->update_strategy.command);
  74. free(entry->config);
  75. }
  76. static void submodule_cache_clear(struct submodule_cache *cache)
  77. {
  78. struct hashmap_iter iter;
  79. struct submodule_entry *entry;
  80. if (!cache->initialized)
  81. return;
  82. /*
  83. * We iterate over the name hash here to be symmetric with the
  84. * allocation of struct submodule entries. Each is allocated by
  85. * their .gitmodules blob sha1 and submodule name.
  86. */
  87. hashmap_for_each_entry(&cache->for_name, &iter, entry,
  88. ent /* member name */)
  89. free_one_config(entry);
  90. hashmap_free_entries(&cache->for_path, struct submodule_entry, ent);
  91. hashmap_free_entries(&cache->for_name, struct submodule_entry, ent);
  92. cache->initialized = 0;
  93. cache->gitmodules_read = 0;
  94. }
  95. void submodule_cache_free(struct submodule_cache *cache)
  96. {
  97. submodule_cache_clear(cache);
  98. free(cache);
  99. }
  100. static unsigned int hash_oid_string(const struct object_id *oid,
  101. const char *string)
  102. {
  103. return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
  104. }
  105. static void cache_put_path(struct submodule_cache *cache,
  106. struct submodule *submodule)
  107. {
  108. unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
  109. submodule->path);
  110. struct submodule_entry *e = xmalloc(sizeof(*e));
  111. hashmap_entry_init(&e->ent, hash);
  112. e->config = submodule;
  113. hashmap_put(&cache->for_path, &e->ent);
  114. }
  115. static void cache_remove_path(struct submodule_cache *cache,
  116. struct submodule *submodule)
  117. {
  118. unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
  119. submodule->path);
  120. struct submodule_entry e;
  121. struct submodule_entry *removed;
  122. hashmap_entry_init(&e.ent, hash);
  123. e.config = submodule;
  124. removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
  125. free(removed);
  126. }
  127. static void cache_add(struct submodule_cache *cache,
  128. struct submodule *submodule)
  129. {
  130. unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
  131. submodule->name);
  132. struct submodule_entry *e = xmalloc(sizeof(*e));
  133. hashmap_entry_init(&e->ent, hash);
  134. e->config = submodule;
  135. hashmap_add(&cache->for_name, &e->ent);
  136. }
  137. static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
  138. const struct object_id *gitmodules_oid, const char *path)
  139. {
  140. struct submodule_entry *entry;
  141. unsigned int hash = hash_oid_string(gitmodules_oid, path);
  142. struct submodule_entry key;
  143. struct submodule key_config;
  144. oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
  145. key_config.path = path;
  146. hashmap_entry_init(&key.ent, hash);
  147. key.config = &key_config;
  148. entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
  149. if (entry)
  150. return entry->config;
  151. return NULL;
  152. }
  153. static struct submodule *cache_lookup_name(struct submodule_cache *cache,
  154. const struct object_id *gitmodules_oid, const char *name)
  155. {
  156. struct submodule_entry *entry;
  157. unsigned int hash = hash_oid_string(gitmodules_oid, name);
  158. struct submodule_entry key;
  159. struct submodule key_config;
  160. oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
  161. key_config.name = name;
  162. hashmap_entry_init(&key.ent, hash);
  163. key.config = &key_config;
  164. entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
  165. if (entry)
  166. return entry->config;
  167. return NULL;
  168. }
  169. int check_submodule_name(const char *name)
  170. {
  171. /* Disallow empty names */
  172. if (!*name)
  173. return -1;
  174. /*
  175. * Look for '..' as a path component. Check both '/' and '\\' as
  176. * separators rather than is_dir_sep(), because we want the name rules
  177. * to be consistent across platforms.
  178. */
  179. goto in_component; /* always start inside component */
  180. while (*name) {
  181. char c = *name++;
  182. if (c == '/' || c == '\\') {
  183. in_component:
  184. if (name[0] == '.' && name[1] == '.' &&
  185. (!name[2] || name[2] == '/' || name[2] == '\\'))
  186. return -1;
  187. }
  188. }
  189. return 0;
  190. }
  191. static int name_and_item_from_var(const char *var, struct strbuf *name,
  192. struct strbuf *item)
  193. {
  194. const char *subsection, *key;
  195. size_t subsection_len;
  196. int parse;
  197. parse = parse_config_key(var, "submodule", &subsection,
  198. &subsection_len, &key);
  199. if (parse < 0 || !subsection)
  200. return 0;
  201. strbuf_add(name, subsection, subsection_len);
  202. if (check_submodule_name(name->buf) < 0) {
  203. warning(_("ignoring suspicious submodule name: %s"), name->buf);
  204. strbuf_release(name);
  205. return 0;
  206. }
  207. strbuf_addstr(item, key);
  208. return 1;
  209. }
  210. static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
  211. const struct object_id *gitmodules_oid, const char *name)
  212. {
  213. struct submodule *submodule;
  214. struct strbuf name_buf = STRBUF_INIT;
  215. submodule = cache_lookup_name(cache, gitmodules_oid, name);
  216. if (submodule)
  217. return submodule;
  218. submodule = xmalloc(sizeof(*submodule));
  219. strbuf_addstr(&name_buf, name);
  220. submodule->name = strbuf_detach(&name_buf, NULL);
  221. submodule->path = NULL;
  222. submodule->url = NULL;
  223. submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
  224. submodule->update_strategy.command = NULL;
  225. submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
  226. submodule->ignore = NULL;
  227. submodule->branch = NULL;
  228. submodule->recommend_shallow = -1;
  229. oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
  230. cache_add(cache, submodule);
  231. return submodule;
  232. }
  233. static int parse_fetch_recurse(const char *opt, const char *arg,
  234. int die_on_error)
  235. {
  236. switch (git_parse_maybe_bool(arg)) {
  237. case 1:
  238. return RECURSE_SUBMODULES_ON;
  239. case 0:
  240. return RECURSE_SUBMODULES_OFF;
  241. default:
  242. if (!strcmp(arg, "on-demand"))
  243. return RECURSE_SUBMODULES_ON_DEMAND;
  244. /*
  245. * Please update $__git_fetch_recurse_submodules in
  246. * git-completion.bash when you add new options.
  247. */
  248. if (die_on_error)
  249. die("bad %s argument: %s", opt, arg);
  250. else
  251. return RECURSE_SUBMODULES_ERROR;
  252. }
  253. }
  254. int parse_submodule_fetchjobs(const char *var, const char *value)
  255. {
  256. int fetchjobs = git_config_int(var, value);
  257. if (fetchjobs < 0)
  258. die(_("negative values not allowed for submodule.fetchjobs"));
  259. return fetchjobs;
  260. }
  261. int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
  262. {
  263. return parse_fetch_recurse(opt, arg, 1);
  264. }
  265. int option_fetch_parse_recurse_submodules(const struct option *opt,
  266. const char *arg, int unset)
  267. {
  268. int *v;
  269. if (!opt->value)
  270. return -1;
  271. v = opt->value;
  272. if (unset) {
  273. *v = RECURSE_SUBMODULES_OFF;
  274. } else {
  275. if (arg)
  276. *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
  277. else
  278. *v = RECURSE_SUBMODULES_ON;
  279. }
  280. return 0;
  281. }
  282. static int parse_update_recurse(const char *opt, const char *arg,
  283. int die_on_error)
  284. {
  285. switch (git_parse_maybe_bool(arg)) {
  286. case 1:
  287. return RECURSE_SUBMODULES_ON;
  288. case 0:
  289. return RECURSE_SUBMODULES_OFF;
  290. default:
  291. if (die_on_error)
  292. die("bad %s argument: %s", opt, arg);
  293. return RECURSE_SUBMODULES_ERROR;
  294. }
  295. }
  296. int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
  297. {
  298. return parse_update_recurse(opt, arg, 1);
  299. }
  300. static int parse_push_recurse(const char *opt, const char *arg,
  301. int die_on_error)
  302. {
  303. switch (git_parse_maybe_bool(arg)) {
  304. case 1:
  305. /* There's no simple "on" value when pushing */
  306. if (die_on_error)
  307. die("bad %s argument: %s", opt, arg);
  308. else
  309. return RECURSE_SUBMODULES_ERROR;
  310. case 0:
  311. return RECURSE_SUBMODULES_OFF;
  312. default:
  313. if (!strcmp(arg, "on-demand"))
  314. return RECURSE_SUBMODULES_ON_DEMAND;
  315. else if (!strcmp(arg, "check"))
  316. return RECURSE_SUBMODULES_CHECK;
  317. else if (!strcmp(arg, "only"))
  318. return RECURSE_SUBMODULES_ONLY;
  319. /*
  320. * Please update $__git_push_recurse_submodules in
  321. * git-completion.bash when you add new modes.
  322. */
  323. else if (die_on_error)
  324. die("bad %s argument: %s", opt, arg);
  325. else
  326. return RECURSE_SUBMODULES_ERROR;
  327. }
  328. }
  329. int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
  330. {
  331. return parse_push_recurse(opt, arg, 1);
  332. }
  333. static void warn_multiple_config(const struct object_id *treeish_name,
  334. const char *name, const char *option)
  335. {
  336. const char *commit_string = "WORKTREE";
  337. if (treeish_name)
  338. commit_string = oid_to_hex(treeish_name);
  339. warning("%s:.gitmodules, multiple configurations found for "
  340. "'submodule.%s.%s'. Skipping second one!",
  341. commit_string, name, option);
  342. }
  343. static void warn_command_line_option(const char *var, const char *value)
  344. {
  345. warning(_("ignoring '%s' which may be interpreted as"
  346. " a command-line option: %s"), var, value);
  347. }
  348. struct parse_config_parameter {
  349. struct submodule_cache *cache;
  350. const struct object_id *treeish_name;
  351. const struct object_id *gitmodules_oid;
  352. int overwrite;
  353. };
  354. /*
  355. * Parse a config item from .gitmodules.
  356. *
  357. * This does not handle submodule-related configuration from the main
  358. * config store (.git/config, etc). Callers are responsible for
  359. * checking for overrides in the main config store when appropriate.
  360. */
  361. static int parse_config(const char *var, const char *value, void *data)
  362. {
  363. struct parse_config_parameter *me = data;
  364. struct submodule *submodule;
  365. struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
  366. int ret = 0;
  367. /* this also ensures that we only parse submodule entries */
  368. if (!name_and_item_from_var(var, &name, &item))
  369. return 0;
  370. submodule = lookup_or_create_by_name(me->cache,
  371. me->gitmodules_oid,
  372. name.buf);
  373. if (!strcmp(item.buf, "path")) {
  374. if (!value)
  375. ret = config_error_nonbool(var);
  376. else if (looks_like_command_line_option(value))
  377. warn_command_line_option(var, value);
  378. else if (!me->overwrite && submodule->path)
  379. warn_multiple_config(me->treeish_name, submodule->name,
  380. "path");
  381. else {
  382. if (submodule->path)
  383. cache_remove_path(me->cache, submodule);
  384. free((void *) submodule->path);
  385. submodule->path = xstrdup(value);
  386. cache_put_path(me->cache, submodule);
  387. }
  388. } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
  389. /* when parsing worktree configurations we can die early */
  390. int die_on_error = is_null_oid(me->gitmodules_oid);
  391. if (!me->overwrite &&
  392. submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
  393. warn_multiple_config(me->treeish_name, submodule->name,
  394. "fetchrecursesubmodules");
  395. else
  396. submodule->fetch_recurse = parse_fetch_recurse(
  397. var, value,
  398. die_on_error);
  399. } else if (!strcmp(item.buf, "ignore")) {
  400. if (!value)
  401. ret = config_error_nonbool(var);
  402. else if (!me->overwrite && submodule->ignore)
  403. warn_multiple_config(me->treeish_name, submodule->name,
  404. "ignore");
  405. else if (strcmp(value, "untracked") &&
  406. strcmp(value, "dirty") &&
  407. strcmp(value, "all") &&
  408. strcmp(value, "none"))
  409. warning("Invalid parameter '%s' for config option "
  410. "'submodule.%s.ignore'", value, name.buf);
  411. else {
  412. free((void *) submodule->ignore);
  413. submodule->ignore = xstrdup(value);
  414. }
  415. } else if (!strcmp(item.buf, "url")) {
  416. if (!value) {
  417. ret = config_error_nonbool(var);
  418. } else if (looks_like_command_line_option(value)) {
  419. warn_command_line_option(var, value);
  420. } else if (!me->overwrite && submodule->url) {
  421. warn_multiple_config(me->treeish_name, submodule->name,
  422. "url");
  423. } else {
  424. free((void *) submodule->url);
  425. submodule->url = xstrdup(value);
  426. }
  427. } else if (!strcmp(item.buf, "update")) {
  428. if (!value)
  429. ret = config_error_nonbool(var);
  430. else if (!me->overwrite &&
  431. submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
  432. warn_multiple_config(me->treeish_name, submodule->name,
  433. "update");
  434. else if (parse_submodule_update_strategy(value,
  435. &submodule->update_strategy) < 0 ||
  436. submodule->update_strategy.type == SM_UPDATE_COMMAND)
  437. die(_("invalid value for %s"), var);
  438. } else if (!strcmp(item.buf, "shallow")) {
  439. if (!me->overwrite && submodule->recommend_shallow != -1)
  440. warn_multiple_config(me->treeish_name, submodule->name,
  441. "shallow");
  442. else
  443. submodule->recommend_shallow =
  444. git_config_bool(var, value);
  445. } else if (!strcmp(item.buf, "branch")) {
  446. if (!me->overwrite && submodule->branch)
  447. warn_multiple_config(me->treeish_name, submodule->name,
  448. "branch");
  449. else {
  450. free((void *)submodule->branch);
  451. submodule->branch = xstrdup(value);
  452. }
  453. }
  454. strbuf_release(&name);
  455. strbuf_release(&item);
  456. return ret;
  457. }
  458. static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
  459. struct object_id *gitmodules_oid,
  460. struct strbuf *rev)
  461. {
  462. int ret = 0;
  463. if (is_null_oid(treeish_name)) {
  464. oidclr(gitmodules_oid);
  465. return 1;
  466. }
  467. strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
  468. if (get_oid(rev->buf, gitmodules_oid) >= 0)
  469. ret = 1;
  470. return ret;
  471. }
  472. /* This does a lookup of a submodule configuration by name or by path
  473. * (key) with on-demand reading of the appropriate .gitmodules from
  474. * revisions.
  475. */
  476. static const struct submodule *config_from(struct submodule_cache *cache,
  477. const struct object_id *treeish_name, const char *key,
  478. enum lookup_type lookup_type)
  479. {
  480. struct strbuf rev = STRBUF_INIT;
  481. unsigned long config_size;
  482. char *config = NULL;
  483. struct object_id oid;
  484. enum object_type type;
  485. const struct submodule *submodule = NULL;
  486. struct parse_config_parameter parameter;
  487. /*
  488. * If any parameter except the cache is a NULL pointer just
  489. * return the first submodule. Can be used to check whether
  490. * there are any submodules parsed.
  491. */
  492. if (!treeish_name || !key) {
  493. struct hashmap_iter iter;
  494. struct submodule_entry *entry;
  495. entry = hashmap_iter_first_entry(&cache->for_name, &iter,
  496. struct submodule_entry,
  497. ent /* member name */);
  498. if (!entry)
  499. return NULL;
  500. return entry->config;
  501. }
  502. if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
  503. goto out;
  504. switch (lookup_type) {
  505. case lookup_name:
  506. submodule = cache_lookup_name(cache, &oid, key);
  507. break;
  508. case lookup_path:
  509. submodule = cache_lookup_path(cache, &oid, key);
  510. break;
  511. }
  512. if (submodule)
  513. goto out;
  514. config = read_object_file(&oid, &type, &config_size);
  515. if (!config || type != OBJ_BLOB)
  516. goto out;
  517. /* fill the submodule config into the cache */
  518. parameter.cache = cache;
  519. parameter.treeish_name = treeish_name;
  520. parameter.gitmodules_oid = &oid;
  521. parameter.overwrite = 0;
  522. git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
  523. config, config_size, &parameter, NULL);
  524. strbuf_release(&rev);
  525. free(config);
  526. switch (lookup_type) {
  527. case lookup_name:
  528. return cache_lookup_name(cache, &oid, key);
  529. case lookup_path:
  530. return cache_lookup_path(cache, &oid, key);
  531. default:
  532. return NULL;
  533. }
  534. out:
  535. strbuf_release(&rev);
  536. free(config);
  537. return submodule;
  538. }
  539. static void submodule_cache_check_init(struct repository *repo)
  540. {
  541. if (repo->submodule_cache && repo->submodule_cache->initialized)
  542. return;
  543. if (!repo->submodule_cache)
  544. repo->submodule_cache = submodule_cache_alloc();
  545. submodule_cache_init(repo->submodule_cache);
  546. }
  547. /*
  548. * Note: This function is private for a reason, the '.gitmodules' file should
  549. * not be used as a mechanism to retrieve arbitrary configuration stored in
  550. * the repository.
  551. *
  552. * Runs the provided config function on the '.gitmodules' file found in the
  553. * working directory.
  554. */
  555. static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
  556. {
  557. if (repo->worktree) {
  558. struct git_config_source config_source = {
  559. 0, .scope = CONFIG_SCOPE_SUBMODULE
  560. };
  561. const struct config_options opts = { 0 };
  562. struct object_id oid;
  563. char *file;
  564. char *oidstr = NULL;
  565. file = repo_worktree_path(repo, GITMODULES_FILE);
  566. if (file_exists(file)) {
  567. config_source.file = file;
  568. } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
  569. repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
  570. config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
  571. if (repo != the_repository)
  572. add_to_alternates_memory(repo->objects->odb->path);
  573. } else {
  574. goto out;
  575. }
  576. config_with_options(fn, data, &config_source, &opts);
  577. out:
  578. free(oidstr);
  579. free(file);
  580. }
  581. }
  582. static int gitmodules_cb(const char *var, const char *value, void *data)
  583. {
  584. struct repository *repo = data;
  585. struct parse_config_parameter parameter;
  586. parameter.cache = repo->submodule_cache;
  587. parameter.treeish_name = NULL;
  588. parameter.gitmodules_oid = &null_oid;
  589. parameter.overwrite = 1;
  590. return parse_config(var, value, &parameter);
  591. }
  592. void repo_read_gitmodules(struct repository *repo, int skip_if_read)
  593. {
  594. submodule_cache_check_init(repo);
  595. if (repo->submodule_cache->gitmodules_read && skip_if_read)
  596. return;
  597. if (repo_read_index(repo) < 0)
  598. return;
  599. if (!is_gitmodules_unmerged(repo->index))
  600. config_from_gitmodules(gitmodules_cb, repo, repo);
  601. repo->submodule_cache->gitmodules_read = 1;
  602. }
  603. void gitmodules_config_oid(const struct object_id *commit_oid)
  604. {
  605. struct strbuf rev = STRBUF_INIT;
  606. struct object_id oid;
  607. submodule_cache_check_init(the_repository);
  608. if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
  609. git_config_from_blob_oid(gitmodules_cb, rev.buf,
  610. &oid, the_repository);
  611. }
  612. strbuf_release(&rev);
  613. the_repository->submodule_cache->gitmodules_read = 1;
  614. }
  615. const struct submodule *submodule_from_name(struct repository *r,
  616. const struct object_id *treeish_name,
  617. const char *name)
  618. {
  619. repo_read_gitmodules(r, 1);
  620. return config_from(r->submodule_cache, treeish_name, name, lookup_name);
  621. }
  622. const struct submodule *submodule_from_path(struct repository *r,
  623. const struct object_id *treeish_name,
  624. const char *path)
  625. {
  626. repo_read_gitmodules(r, 1);
  627. return config_from(r->submodule_cache, treeish_name, path, lookup_path);
  628. }
  629. void submodule_free(struct repository *r)
  630. {
  631. if (r->submodule_cache)
  632. submodule_cache_clear(r->submodule_cache);
  633. }
  634. static int config_print_callback(const char *var, const char *value, void *cb_data)
  635. {
  636. char *wanted_key = cb_data;
  637. if (!strcmp(wanted_key, var))
  638. printf("%s\n", value);
  639. return 0;
  640. }
  641. int print_config_from_gitmodules(struct repository *repo, const char *key)
  642. {
  643. int ret;
  644. char *store_key;
  645. ret = git_config_parse_key(key, &store_key, NULL);
  646. if (ret < 0)
  647. return CONFIG_INVALID_KEY;
  648. config_from_gitmodules(config_print_callback, repo, store_key);
  649. free(store_key);
  650. return 0;
  651. }
  652. int config_set_in_gitmodules_file_gently(const char *key, const char *value)
  653. {
  654. int ret;
  655. ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
  656. if (ret < 0)
  657. /* Maybe the user already did that, don't error out here */
  658. warning(_("Could not update .gitmodules entry %s"), key);
  659. return ret;
  660. }
  661. struct fetch_config {
  662. int *max_children;
  663. int *recurse_submodules;
  664. };
  665. static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
  666. {
  667. struct fetch_config *config = cb;
  668. if (!strcmp(var, "submodule.fetchjobs")) {
  669. if (config->max_children)
  670. *(config->max_children) =
  671. parse_submodule_fetchjobs(var, value);
  672. return 0;
  673. } else if (!strcmp(var, "fetch.recursesubmodules")) {
  674. if (config->recurse_submodules)
  675. *(config->recurse_submodules) =
  676. parse_fetch_recurse_submodules_arg(var, value);
  677. return 0;
  678. }
  679. return 0;
  680. }
  681. void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
  682. {
  683. struct fetch_config config = {
  684. .max_children = max_children,
  685. .recurse_submodules = recurse_submodules
  686. };
  687. config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
  688. }
  689. static int gitmodules_update_clone_config(const char *var, const char *value,
  690. void *cb)
  691. {
  692. int *max_jobs = cb;
  693. if (!strcmp(var, "submodule.fetchjobs"))
  694. *max_jobs = parse_submodule_fetchjobs(var, value);
  695. return 0;
  696. }
  697. void update_clone_config_from_gitmodules(int *max_jobs)
  698. {
  699. config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
  700. }