summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-05-05 06:03:21 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-06-22 17:02:55 +0200
commitd769a3fdda81be4936a3760bd29f39935b32376a (patch)
treee8f3e2c2c3cc7a22ff816350f3d8e3c896b9d9c6
parentc6f489c964bc4df29bdacb1ee4afdcdb294f3815 (diff)
downloadlibgit2-d769a3fdda81be4936a3760bd29f39935b32376a.tar.gz
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.
-rw-r--r--src/submodule.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/submodule.c b/src/submodule.c
index c6effdef8..37d420525 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);