Add git_submodule_resolve_url()
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 61 62 63 64 65 66 67 68 69
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index d724326..ac0abc0 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -272,6 +272,16 @@ GIT_EXTERN(const char *) git_submodule_path(git_submodule *submodule);
GIT_EXTERN(const char *) git_submodule_url(git_submodule *submodule);
/**
+ * Resolve a submodule url relative to the given repository.
+ *
+ * @param out buffer to store the absolute submodule url in
+ * @param repository Pointer to repository object
+ * @param url Relative url
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *url);
+
+/**
* Get the branch for the submodule.
*
* @param submodule Pointer to submodule object
diff --git a/src/submodule.c b/src/submodule.c
index 2388bd1..2fe7f1d 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -229,16 +229,7 @@ int git_submodule_add_setup(
}
/* resolve parameters */
-
- if (url[0] == '.' && (url[1] == '/' || (url[1] == '.' && url[2] == '/'))) {
- if (!(error = lookup_head_remote(&real_url, repo)))
- error = git_path_apply_relative(&real_url, url);
- } else if (strchr(url, ':') != NULL || url[0] == '/') {
- error = git_buf_sets(&real_url, url);
- } else {
- giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL");
- error = -1;
- }
+ error = git_submodule_resolve_url(&real_url, repo, url);
if (error)
goto cleanup;
@@ -533,6 +524,25 @@ const char *git_submodule_url(git_submodule *submodule)
return submodule->url;
}
+int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *url)
+{
+ assert(url);
+
+ int error = 0;
+
+ if (url[0] == '.' && (url[1] == '/' || (url[1] == '.' && url[2] == '/'))) {
+ if (!(error = lookup_head_remote(out, repo)))
+ error = git_path_apply_relative(out, url);
+ } else if (strchr(url, ':') != NULL || url[0] == '/') {
+ error = git_buf_sets(out, url);
+ } else {
+ giterr_set(GITERR_SUBMODULE, "Invalid format for submodule URL");
+ error = -1;
+ }
+
+ return error;
+}
+
const char *git_submodule_branch(git_submodule *submodule)
{
assert(submodule);