submodule: bring back finding by path During the removal of the cache, we also removed the ability to use `_lookup()` to search by path rather than name. Bring this logic back.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
diff --git a/src/submodule.c b/src/submodule.c
index c6effde..37d4205 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -124,6 +124,26 @@ static void submodule_set_lookup_error(int error, const char *name)
"Submodule '%s' has not been added yet", name);
}
+typedef struct {
+ const char *path;
+ char *name;
+} fbp_data;
+
+static int find_by_path(const git_config_entry *entry, void *payload)
+{
+ fbp_data *data = payload;
+
+ if (!strcmp(entry->value, data->path)) {
+ const char *fdot, *ldot;
+ fdot = strchr(entry->name, '.');
+ ldot = strrchr(entry->name, '.');
+ data->name = git__strndup(fdot + 1, ldot - fdot - 1);
+ GITERR_CHECK_ALLOC(data->name);
+ }
+
+ return 0;
+}
+
int git_submodule_lookup(
git_submodule **out, /* NULL if user only wants to test existence */
git_repository *repo,
@@ -147,6 +167,28 @@ int git_submodule_lookup(
return error;
}
+ /* Didn't find it via the name, maybe it's the path */
+ if (!sm->url) {
+ const char *pattern = "submodule\\..*\\.path";
+ fbp_data data = { name, NULL };
+
+ if ((error = git_config_file_foreach_match(mods, pattern, find_by_path, &data)) < 0)
+ return error;
+
+ if (data.name) {
+ git__free(sm->name);
+ sm->name = data.name;
+ sm->path = git__strdup(name);
+ GITERR_CHECK_ALLOC(sm->path);
+
+ /* Try to load again with the right name */
+ if ((error = git_submodule_reload(sm, false)) < 0) {
+ git_submodule_free(sm);
+ return error;
+ }
+ }
+ }
+
/* If we didn't find the url, consider it missing */
if (!sm->url) {
git_submodule_free(sm);