submodule: store new submodule commits oid_array in a struct

This commit prepares for a future commit that will teach `git fetch --recurse-submodules` how to fetch submodules that are present in <gitdir>/modules, but are not populated. To do this, we need to store more information about the changed submodule so that we can read the submodule configuration from the superproject commit instead of the filesystem. Refactor the changed submodules string_list.util to hold a struct instead of an oid_array. This struct only holds the new_commits oid_array for now; more information will be added later. 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 6e1e0c9959f1df4b8c5aafb69d149374720b26dc
1 file changed +34 -18
submodule.c
+34 -18
@@ -806,6 +806,20 @@ static const char *default_name_or_path(const char *path_or_name)
806 return path_or_name;
807 }
808
809 +/*
810 + * Holds relevant information for a changed submodule. Used as the .util
811 + * member of the changed submodule string_list_item.
812 + */
813 +struct changed_submodule_data {
814 + /* The submodule commits that have changed in the rev walk. */
815 + struct oid_array new_commits;
816 +};
817 +
818 +static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
819 +{
820 + oid_array_clear(&cs_data->new_commits);
821 +}
822 +
823 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
824 struct diff_options *options,
825 void *data)
@@ -820,6 +834,7 @@ static void collect_changed_submodules_cb(struct diff_queue_struct *q,
834 const struct submodule *submodule;
835 const char *name;
836 struct string_list_item *item;
837 + struct changed_submodule_data *cs_data;
838
839 if (!S_ISGITLINK(p->two->mode))
840 continue;
@@ -848,9 +863,9 @@ static void collect_changed_submodules_cb(struct diff_queue_struct *q,
863
864 item = string_list_insert(changed, name);
865 if (!item->util)
851 - /* NEEDSWORK: should we have oid_array_init()? */
852 - item->util = xcalloc(1, sizeof(struct oid_array));
853 - oid_array_append(item->util, &p->two->oid);
866 + item->util = xcalloc(1, sizeof(struct changed_submodule_data));
867 + cs_data = item->util;
868 + oid_array_append(&cs_data->new_commits, &p->two->oid);
869 }
870 }
871
@@ -897,11 +912,12 @@ static void collect_changed_submodules(struct repository *r,
912 reset_revision_walk();
913 }
914
900 -static void free_submodules_oids(struct string_list *submodules)
915 +static void free_submodules_data(struct string_list *submodules)
916 {
917 struct string_list_item *item;
918 for_each_string_list_item(item, submodules)
904 - oid_array_clear((struct oid_array *) item->util);
919 + changed_submodule_data_clear(item->util);
920 +
921 string_list_clear(submodules, 1);
922 }
923
@@ -1074,7 +1090,7 @@ int find_unpushed_submodules(struct repository *r,
1090 collect_changed_submodules(r, &submodules, &argv);
1091
1092 for_each_string_list_item(name, &submodules) {
1077 - struct oid_array *commits = name->util;
1093 + struct changed_submodule_data *cs_data = name->util;
1094 const struct submodule *submodule;
1095 const char *path = NULL;
1096
@@ -1087,11 +1103,11 @@ int find_unpushed_submodules(struct repository *r,
1103 if (!path)
1104 continue;
1105
1090 - if (submodule_needs_pushing(r, path, commits))
1106 + if (submodule_needs_pushing(r, path, &cs_data->new_commits))
1107 string_list_insert(needs_pushing, path);
1108 }
1109
1094 - free_submodules_oids(&submodules);
1110 + free_submodules_data(&submodules);
1111 strvec_clear(&argv);
1112
1113 return needs_pushing->nr;
@@ -1261,7 +1277,7 @@ static void calculate_changed_submodule_paths(struct repository *r,
1277 collect_changed_submodules(r, changed_submodule_names, &argv);
1278
1279 for_each_string_list_item(name, changed_submodule_names) {
1264 - struct oid_array *commits = name->util;
1280 + struct changed_submodule_data *cs_data = name->util;
1281 const struct submodule *submodule;
1282 const char *path = NULL;
1283
@@ -1274,8 +1290,8 @@ static void calculate_changed_submodule_paths(struct repository *r,
1290 if (!path)
1291 continue;
1292
1277 - if (submodule_has_commits(r, path, null_oid(), commits)) {
1278 - oid_array_clear(commits);
1293 + if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1294 + changed_submodule_data_clear(cs_data);
1295 *name->string = '\0';
1296 }
1297 }
@@ -1312,7 +1328,7 @@ int submodule_touches_in_range(struct repository *r,
1328
1329 strvec_clear(&args);
1330
1315 - free_submodules_oids(&subs);
1331 + free_submodules_data(&subs);
1332 return ret;
1333 }
1334
@@ -1596,7 +1612,7 @@ static int fetch_finish(int retvalue, struct strbuf *err,
1612 struct fetch_task *task = task_cb;
1613
1614 struct string_list_item *it;
1599 - struct oid_array *commits;
1615 + struct changed_submodule_data *cs_data;
1616
1617 if (!task || !task->sub)
1618 BUG("callback cookie bogus");
@@ -1624,14 +1640,14 @@ static int fetch_finish(int retvalue, struct strbuf *err,
1640 /* Could be an unchanged submodule, not contained in the list */
1641 goto out;
1642
1627 - commits = it->util;
1628 - oid_array_filter(commits,
1643 + cs_data = it->util;
1644 + oid_array_filter(&cs_data->new_commits,
1645 commit_missing_in_sub,
1646 task->repo);
1647
1648 /* Are there commits we want, but do not exist? */
1633 - if (commits->nr) {
1634 - task->commits = commits;
1649 + if (cs_data->new_commits.nr) {
1650 + task->commits = &cs_data->new_commits;
1651 ALLOC_GROW(spf->oid_fetch_tasks,
1652 spf->oid_fetch_tasks_nr + 1,
1653 spf->oid_fetch_tasks_alloc);
@@ -1689,7 +1705,7 @@ int fetch_populated_submodules(struct repository *r,
1705
1706 strvec_clear(&spf.args);
1707 out:
1692 - free_submodules_oids(&spf.changed_submodule_names);
1708 + free_submodules_data(&spf.changed_submodule_names);
1709 return spf.result;
1710 }
1711