THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

807 lines
21KB

  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. int subsection_len, parse;
  196. parse = parse_config_key(var, "submodule", &subsection,
  197. &subsection_len, &key);
  198. if (parse < 0 || !subsection)
  199. return 0;
  200. strbuf_add(name, subsection, subsection_len);
  201. if (check_submodule_name(name->buf) < 0) {
  202. warning(_("ignoring suspicious submodule name: %s"), name->buf);
  203. strbuf_release(name);
  204. return 0;
  205. }
  206. strbuf_addstr(item, key);
  207. return 1;
  208. }
  209. static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
  210. const struct object_id *gitmodules_oid, const char *name)
  211. {
  212. struct submodule *submodule;
  213. struct strbuf name_buf = STRBUF_INIT;
  214. submodule = cache_lookup_name(cache, gitmodules_oid, name);
  215. if (submodule)
  216. return submodule;
  217. submodule = xmalloc(sizeof(*submodule));
  218. strbuf_addstr(&name_buf, name);
  219. submodule->name = strbuf_detach(&name_buf, NULL);
  220. submodule->path = NULL;
  221. submodule->url = NULL;
  222. submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
  223. submodule->update_strategy.command = NULL;
  224. submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
  225. submodule->ignore = NULL;
  226. submodule->branch = NULL;
  227. submodule->recommend_shallow = -1;
  228. oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
  229. cache_add(cache, submodule);
  230. return submodule;
  231. }
  232. static int parse_fetch_recurse(const char *opt, const char *arg,
  233. int die_on_error)
  234. {
  235. switch (git_parse_maybe_bool(arg)) {
  236. case 1:
  237. return RECURSE_SUBMODULES_ON;
  238. case 0:
  239. return RECURSE_SUBMODULES_OFF;
  240. default:
  241. if (!strcmp(arg, "on-demand"))
  242. return RECURSE_SUBMODULES_ON_DEMAND;
  243. /*
  244. * Please update $__git_fetch_recurse_submodules in
  245. * git-completion.bash when you add new options.
  246. */
  247. if (die_on_error)
  248. die("bad %s argument: %s", opt, arg);
  249. else
  250. return RECURSE_SUBMODULES_ERROR;
  251. }
  252. }
  253. int parse_submodule_fetchjobs(const char *var, const char *value)
  254. {
  255. int fetchjobs = git_config_int(var, value);
  256. if (fetchjobs < 0)
  257. die(_("negative values not allowed for submodule.fetchjobs"));
  258. return fetchjobs;
  259. }
  260. int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
  261. {
  262. return parse_fetch_recurse(opt, arg, 1);
  263. }
  264. int option_fetch_parse_recurse_submodules(const struct option *opt,
  265. const char *arg, int unset)
  266. {
  267. int *v;
  268. if (!opt->value)
  269. return -1;
  270. v = opt->value;
  271. if (unset) {
  272. *v = RECURSE_SUBMODULES_OFF;
  273. } else {
  274. if (arg)
  275. *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
  276. else
  277. *v = RECURSE_SUBMODULES_ON;
  278. }
  279. return 0;
  280. }
  281. static int parse_update_recurse(const char *opt, const char *arg,
  282. int die_on_error)
  283. {
  284. switch (git_parse_maybe_bool(arg)) {
  285. case 1:
  286. return RECURSE_SUBMODULES_ON;
  287. case 0:
  288. return RECURSE_SUBMODULES_OFF;
  289. default:
  290. if (die_on_error)
  291. die("bad %s argument: %s", opt, arg);
  292. return RECURSE_SUBMODULES_ERROR;
  293. }
  294. }
  295. int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
  296. {
  297. return parse_update_recurse(opt, arg, 1);
  298. }
  299. static int parse_push_recurse(const char *opt, const char *arg,
  300. int die_on_error)
  301. {
  302. switch (git_parse_maybe_bool(arg)) {
  303. case 1:
  304. /* There's no simple "on" value when pushing */
  305. if (die_on_error)
  306. die("bad %s argument: %s", opt, arg);
  307. else
  308. return RECURSE_SUBMODULES_ERROR;
  309. case 0:
  310. return RECURSE_SUBMODULES_OFF;
  311. default:
  312. if (!strcmp(arg, "on-demand"))
  313. return RECURSE_SUBMODULES_ON_DEMAND;
  314. else if (!strcmp(arg, "check"))
  315. return RECURSE_SUBMODULES_CHECK;
  316. else if (!strcmp(arg, "only"))
  317. return RECURSE_SUBMODULES_ONLY;
  318. /*
  319. * Please update $__git_push_recurse_submodules in
  320. * git-completion.bash when you add new modes.
  321. */
  322. else if (die_on_error)
  323. die("bad %s argument: %s", opt, arg);
  324. else
  325. return RECURSE_SUBMODULES_ERROR;
  326. }
  327. }
  328. int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
  329. {
  330. return parse_push_recurse(opt, arg, 1);
  331. }
  332. static void warn_multiple_config(const struct object_id *treeish_name,
  333. const char *name, const char *option)
  334. {
  335. const char *commit_string = "WORKTREE";
  336. if (treeish_name)
  337. commit_string = oid_to_hex(treeish_name);
  338. warning("%s:.gitmodules, multiple configurations found for "
  339. "'submodule.%s.%s'. Skipping second one!",
  340. commit_string, name, option);
  341. }
  342. static void warn_command_line_option(const char *var, const char *value)
  343. {
  344. warning(_("ignoring '%s' which may be interpreted as"
  345. " a command-line option: %s"), var, value);
  346. }
  347. struct parse_config_parameter {
  348. struct submodule_cache *cache;
  349. const struct object_id *treeish_name;
  350. const struct object_id *gitmodules_oid;
  351. int overwrite;
  352. };
  353. static int parse_config(const char *var, const char *value, void *data)
  354. {
  355. struct parse_config_parameter *me = data;
  356. struct submodule *submodule;
  357. struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
  358. int ret = 0;
  359. /* this also ensures that we only parse submodule entries */
  360. if (!name_and_item_from_var(var, &name, &item))
  361. return 0;
  362. submodule = lookup_or_create_by_name(me->cache,
  363. me->gitmodules_oid,
  364. name.buf);
  365. if (!strcmp(item.buf, "path")) {
  366. if (!value)
  367. ret = config_error_nonbool(var);
  368. else if (looks_like_command_line_option(value))
  369. warn_command_line_option(var, value);
  370. else if (!me->overwrite && submodule->path)
  371. warn_multiple_config(me->treeish_name, submodule->name,
  372. "path");
  373. else {
  374. if (submodule->path)
  375. cache_remove_path(me->cache, submodule);
  376. free((void *) submodule->path);
  377. submodule->path = xstrdup(value);
  378. cache_put_path(me->cache, submodule);
  379. }
  380. } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
  381. /* when parsing worktree configurations we can die early */
  382. int die_on_error = is_null_oid(me->gitmodules_oid);
  383. if (!me->overwrite &&
  384. submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
  385. warn_multiple_config(me->treeish_name, submodule->name,
  386. "fetchrecursesubmodules");
  387. else
  388. submodule->fetch_recurse = parse_fetch_recurse(
  389. var, value,
  390. die_on_error);
  391. } else if (!strcmp(item.buf, "ignore")) {
  392. if (!value)
  393. ret = config_error_nonbool(var);
  394. else if (!me->overwrite && submodule->ignore)
  395. warn_multiple_config(me->treeish_name, submodule->name,
  396. "ignore");
  397. else if (strcmp(value, "untracked") &&
  398. strcmp(value, "dirty") &&
  399. strcmp(value, "all") &&
  400. strcmp(value, "none"))
  401. warning("Invalid parameter '%s' for config option "
  402. "'submodule.%s.ignore'", value, name.buf);
  403. else {
  404. free((void *) submodule->ignore);
  405. submodule->ignore = xstrdup(value);
  406. }
  407. } else if (!strcmp(item.buf, "url")) {
  408. if (!value) {
  409. ret = config_error_nonbool(var);
  410. } else if (looks_like_command_line_option(value)) {
  411. warn_command_line_option(var, value);
  412. } else if (!me->overwrite && submodule->url) {
  413. warn_multiple_config(me->treeish_name, submodule->name,
  414. "url");
  415. } else {
  416. free((void *) submodule->url);
  417. submodule->url = xstrdup(value);
  418. }
  419. } else if (!strcmp(item.buf, "update")) {
  420. if (!value)
  421. ret = config_error_nonbool(var);
  422. else if (!me->overwrite &&
  423. submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
  424. warn_multiple_config(me->treeish_name, submodule->name,
  425. "update");
  426. else if (parse_submodule_update_strategy(value,
  427. &submodule->update_strategy) < 0)
  428. die(_("invalid value for %s"), var);
  429. } else if (!strcmp(item.buf, "shallow")) {
  430. if (!me->overwrite && submodule->recommend_shallow != -1)
  431. warn_multiple_config(me->treeish_name, submodule->name,
  432. "shallow");
  433. else
  434. submodule->recommend_shallow =
  435. git_config_bool(var, value);
  436. } else if (!strcmp(item.buf, "branch")) {
  437. if (!me->overwrite && submodule->branch)
  438. warn_multiple_config(me->treeish_name, submodule->name,
  439. "branch");
  440. else {
  441. free((void *)submodule->branch);
  442. submodule->branch = xstrdup(value);
  443. }
  444. }
  445. strbuf_release(&name);
  446. strbuf_release(&item);
  447. return ret;
  448. }
  449. static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
  450. struct object_id *gitmodules_oid,
  451. struct strbuf *rev)
  452. {
  453. int ret = 0;
  454. if (is_null_oid(treeish_name)) {
  455. oidclr(gitmodules_oid);
  456. return 1;
  457. }
  458. strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
  459. if (get_oid(rev->buf, gitmodules_oid) >= 0)
  460. ret = 1;
  461. return ret;
  462. }
  463. /* This does a lookup of a submodule configuration by name or by path
  464. * (key) with on-demand reading of the appropriate .gitmodules from
  465. * revisions.
  466. */
  467. static const struct submodule *config_from(struct submodule_cache *cache,
  468. const struct object_id *treeish_name, const char *key,
  469. enum lookup_type lookup_type)
  470. {
  471. struct strbuf rev = STRBUF_INIT;
  472. unsigned long config_size;
  473. char *config = NULL;
  474. struct object_id oid;
  475. enum object_type type;
  476. const struct submodule *submodule = NULL;
  477. struct parse_config_parameter parameter;
  478. /*
  479. * If any parameter except the cache is a NULL pointer just
  480. * return the first submodule. Can be used to check whether
  481. * there are any submodules parsed.
  482. */
  483. if (!treeish_name || !key) {
  484. struct hashmap_iter iter;
  485. struct submodule_entry *entry;
  486. entry = hashmap_iter_first_entry(&cache->for_name, &iter,
  487. struct submodule_entry,
  488. ent /* member name */);
  489. if (!entry)
  490. return NULL;
  491. return entry->config;
  492. }
  493. if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
  494. goto out;
  495. switch (lookup_type) {
  496. case lookup_name:
  497. submodule = cache_lookup_name(cache, &oid, key);
  498. break;
  499. case lookup_path:
  500. submodule = cache_lookup_path(cache, &oid, key);
  501. break;
  502. }
  503. if (submodule)
  504. goto out;
  505. config = read_object_file(&oid, &type, &config_size);
  506. if (!config || type != OBJ_BLOB)
  507. goto out;
  508. /* fill the submodule config into the cache */
  509. parameter.cache = cache;
  510. parameter.treeish_name = treeish_name;
  511. parameter.gitmodules_oid = &oid;
  512. parameter.overwrite = 0;
  513. git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
  514. config, config_size, &parameter, NULL);
  515. strbuf_release(&rev);
  516. free(config);
  517. switch (lookup_type) {
  518. case lookup_name:
  519. return cache_lookup_name(cache, &oid, key);
  520. case lookup_path:
  521. return cache_lookup_path(cache, &oid, key);
  522. default:
  523. return NULL;
  524. }
  525. out:
  526. strbuf_release(&rev);
  527. free(config);
  528. return submodule;
  529. }
  530. static void submodule_cache_check_init(struct repository *repo)
  531. {
  532. if (repo->submodule_cache && repo->submodule_cache->initialized)
  533. return;
  534. if (!repo->submodule_cache)
  535. repo->submodule_cache = submodule_cache_alloc();
  536. submodule_cache_init(repo->submodule_cache);
  537. }
  538. /*
  539. * Note: This function is private for a reason, the '.gitmodules' file should
  540. * not be used as as a mechanism to retrieve arbitrary configuration stored in
  541. * the repository.
  542. *
  543. * Runs the provided config function on the '.gitmodules' file found in the
  544. * working directory.
  545. */
  546. static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
  547. {
  548. if (repo->worktree) {
  549. struct git_config_source config_source = { 0 };
  550. const struct config_options opts = { 0 };
  551. struct object_id oid;
  552. char *file;
  553. char *oidstr = NULL;
  554. file = repo_worktree_path(repo, GITMODULES_FILE);
  555. if (file_exists(file)) {
  556. config_source.file = file;
  557. } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
  558. repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
  559. config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
  560. if (repo != the_repository)
  561. add_to_alternates_memory(repo->objects->odb->path);
  562. } else {
  563. goto out;
  564. }
  565. config_with_options(fn, data, &config_source, &opts);
  566. out:
  567. free(oidstr);
  568. free(file);
  569. }
  570. }
  571. static int gitmodules_cb(const char *var, const char *value, void *data)
  572. {
  573. struct repository *repo = data;
  574. struct parse_config_parameter parameter;
  575. parameter.cache = repo->submodule_cache;
  576. parameter.treeish_name = NULL;
  577. parameter.gitmodules_oid = &null_oid;
  578. parameter.overwrite = 1;
  579. return parse_config(var, value, &parameter);
  580. }
  581. void repo_read_gitmodules(struct repository *repo)
  582. {
  583. submodule_cache_check_init(repo);
  584. if (repo_read_index(repo) < 0)
  585. return;
  586. if (!is_gitmodules_unmerged(repo->index))
  587. config_from_gitmodules(gitmodules_cb, repo, repo);
  588. repo->submodule_cache->gitmodules_read = 1;
  589. }
  590. void gitmodules_config_oid(const struct object_id *commit_oid)
  591. {
  592. struct strbuf rev = STRBUF_INIT;
  593. struct object_id oid;
  594. submodule_cache_check_init(the_repository);
  595. if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
  596. git_config_from_blob_oid(gitmodules_cb, rev.buf,
  597. &oid, the_repository);
  598. }
  599. strbuf_release(&rev);
  600. the_repository->submodule_cache->gitmodules_read = 1;
  601. }
  602. static void gitmodules_read_check(struct repository *repo)
  603. {
  604. submodule_cache_check_init(repo);
  605. /* read the repo's .gitmodules file if it hasn't been already */
  606. if (!repo->submodule_cache->gitmodules_read)
  607. repo_read_gitmodules(repo);
  608. }
  609. const struct submodule *submodule_from_name(struct repository *r,
  610. const struct object_id *treeish_name,
  611. const char *name)
  612. {
  613. gitmodules_read_check(r);
  614. return config_from(r->submodule_cache, treeish_name, name, lookup_name);
  615. }
  616. const struct submodule *submodule_from_path(struct repository *r,
  617. const struct object_id *treeish_name,
  618. const char *path)
  619. {
  620. gitmodules_read_check(r);
  621. return config_from(r->submodule_cache, treeish_name, path, lookup_path);
  622. }
  623. void submodule_free(struct repository *r)
  624. {
  625. if (r->submodule_cache)
  626. submodule_cache_clear(r->submodule_cache);
  627. }
  628. static int config_print_callback(const char *var, const char *value, void *cb_data)
  629. {
  630. char *wanted_key = cb_data;
  631. if (!strcmp(wanted_key, var))
  632. printf("%s\n", value);
  633. return 0;
  634. }
  635. int print_config_from_gitmodules(struct repository *repo, const char *key)
  636. {
  637. int ret;
  638. char *store_key;
  639. ret = git_config_parse_key(key, &store_key, NULL);
  640. if (ret < 0)
  641. return CONFIG_INVALID_KEY;
  642. config_from_gitmodules(config_print_callback, repo, store_key);
  643. free(store_key);
  644. return 0;
  645. }
  646. int config_set_in_gitmodules_file_gently(const char *key, const char *value)
  647. {
  648. int ret;
  649. ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
  650. if (ret < 0)
  651. /* Maybe the user already did that, don't error out here */
  652. warning(_("Could not update .gitmodules entry %s"), key);
  653. return ret;
  654. }
  655. struct fetch_config {
  656. int *max_children;
  657. int *recurse_submodules;
  658. };
  659. static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
  660. {
  661. struct fetch_config *config = cb;
  662. if (!strcmp(var, "submodule.fetchjobs")) {
  663. *(config->max_children) = parse_submodule_fetchjobs(var, value);
  664. return 0;
  665. } else if (!strcmp(var, "fetch.recursesubmodules")) {
  666. *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
  667. return 0;
  668. }
  669. return 0;
  670. }
  671. void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
  672. {
  673. struct fetch_config config = {
  674. .max_children = max_children,
  675. .recurse_submodules = recurse_submodules
  676. };
  677. config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
  678. }
  679. static int gitmodules_update_clone_config(const char *var, const char *value,
  680. void *cb)
  681. {
  682. int *max_jobs = cb;
  683. if (!strcmp(var, "submodule.fetchjobs"))
  684. *max_jobs = parse_submodule_fetchjobs(var, value);
  685. return 0;
  686. }
  687. void update_clone_config_from_gitmodules(int *max_jobs)
  688. {
  689. config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
  690. }