submodule: make static functions read submodules from commits

A future commit will teach "fetch --recurse-submodules" to fetch unpopulated submodules. To prepare for this, teach the necessary static functions how to read submodules from superproject commits using a "treeish_name" argument (instead of always reading from the index and filesystem) but do not actually change where submodules are read from. Submodules will be read from commits when we fetch unpopulated submodules. Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 7, 2022 at 16:14 UTC 7c2f8cc58cc3648a0e1e4c76be15a09114b4d9dd
1 file changed +19 -10
submodule.c
+19 -10
@@ -932,6 +932,7 @@ struct has_commit_data {
932 struct repository *repo;
933 int result;
934 const char *path;
935 + const struct object_id *super_oid;
936 };
937
938 static int check_has_commit(const struct object_id *oid, void *data)
@@ -940,7 +941,7 @@ static int check_has_commit(const struct object_id *oid, void *data)
941 struct repository subrepo;
942 enum object_type type;
943
943 - if (repo_submodule_init(&subrepo, cb->repo, cb->path, null_oid())) {
944 + if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
945 cb->result = 0;
946 goto cleanup;
947 }
@@ -968,9 +969,15 @@ cleanup:
969
970 static int submodule_has_commits(struct repository *r,
971 const char *path,
972 + const struct object_id *super_oid,
973 struct oid_array *commits)
974 {
973 - struct has_commit_data has_commit = { r, 1, path };
975 + struct has_commit_data has_commit = {
976 + .repo = r,
977 + .result = 1,
978 + .path = path,
979 + .super_oid = super_oid
980 + };
981
982 /*
983 * Perform a cheap, but incorrect check for the existence of 'commits'.
@@ -1017,7 +1024,7 @@ static int submodule_needs_pushing(struct repository *r,
1024 const char *path,
1025 struct oid_array *commits)
1026 {
1020 - if (!submodule_has_commits(r, path, commits))
1027 + if (!submodule_has_commits(r, path, null_oid(), commits))
1028 /*
1029 * NOTE: We do consider it safe to return "no" here. The
1030 * correct answer would be "We do not know" instead of
@@ -1277,7 +1284,7 @@ static void calculate_changed_submodule_paths(struct repository *r,
1284 if (!path)
1285 continue;
1286
1280 - if (submodule_has_commits(r, path, commits)) {
1287 + if (submodule_has_commits(r, path, null_oid(), commits)) {
1288 oid_array_clear(commits);
1289 *name->string = '\0';
1290 }
@@ -1402,12 +1409,13 @@ static const struct submodule *get_non_gitmodules_submodule(const char *path)
1409 }
1410
1411 static struct fetch_task *fetch_task_create(struct repository *r,
1405 - const char *path)
1412 + const char *path,
1413 + const struct object_id *treeish_name)
1414 {
1415 struct fetch_task *task = xmalloc(sizeof(*task));
1416 memset(task, 0, sizeof(*task));
1417
1410 - task->sub = submodule_from_path(r, null_oid(), path);
1418 + task->sub = submodule_from_path(r, treeish_name, path);
1419 if (!task->sub) {
1420 /*
1421 * No entry in .gitmodules? Technically not a submodule,
@@ -1439,11 +1447,12 @@ static void fetch_task_release(struct fetch_task *p)
1447 }
1448
1449 static struct repository *get_submodule_repo_for(struct repository *r,
1442 - const char *path)
1450 + const char *path,
1451 + const struct object_id *treeish_name)
1452 {
1453 struct repository *ret = xmalloc(sizeof(*ret));
1454
1446 - if (repo_submodule_init(ret, r, path, null_oid())) {
1455 + if (repo_submodule_init(ret, r, path, treeish_name)) {
1456 free(ret);
1457 return NULL;
1458 }
@@ -1464,7 +1473,7 @@ static int get_next_submodule(struct child_process *cp,
1473 if (!S_ISGITLINK(ce->ce_mode))
1474 continue;
1475
1467 - task = fetch_task_create(spf->r, ce->name);
1476 + task = fetch_task_create(spf->r, ce->name, null_oid());
1477 if (!task)
1478 continue;
1479
@@ -1487,7 +1496,7 @@ static int get_next_submodule(struct child_process *cp,
1496 continue;
1497 }
1498
1490 - task->repo = get_submodule_repo_for(spf->r, task->sub->path);
1499 + task->repo = get_submodule_repo_for(spf->r, task->sub->path, null_oid());
1500 if (task->repo) {
1501 struct strbuf submodule_prefix = STRBUF_INIT;
1502 child_process_init(cp);