refs: introduce git_remote_name_is_valid Provide a function that can check remote name validity but can also signal when an error occurs. Use the name "name_is_valid", which is more suggestive of checking a given name, rather than "is_valid_name", which suggests that the function checks the validity of the current remote's name.
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 70 71 72 73 74
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 51d9c72..fa0345e 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -915,6 +915,15 @@ GIT_EXTERN(int) git_remote_rename(
/**
* Ensure the remote name is well-formed.
*
+ * @param valid output pointer to set with validity of given remote name
+ * @param remote_name name to be checked.
+ * @return 0 on success or an error code
+ */
+int git_remote_name_is_valid(int *valid, const char *remote_name);
+
+/**
+ * Ensure the remote name is well-formed.
+ *
* @param remote_name name to be checked.
* @return 1 if the reference name is acceptable; 0 if it isn't
*/
diff --git a/src/remote.c b/src/remote.c
index 9c795ee..d471c4c 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -2092,24 +2092,42 @@ cleanup:
return error;
}
-int git_remote_is_valid_name(
- const char *remote_name)
+int git_remote_name_is_valid(int *valid, const char *remote_name)
{
git_buf buf = GIT_BUF_INIT;
- git_refspec refspec;
- int error = -1;
+ git_refspec refspec = {0};
+ int error;
+
+ GIT_ASSERT(valid);
+
+ *valid = 0;
if (!remote_name || *remote_name == '\0')
return 0;
- git_buf_printf(&buf, "refs/heads/test:refs/remotes/%s/test", remote_name);
+ if ((error = git_buf_printf(&buf, "refs/heads/test:refs/remotes/%s/test", remote_name)) < 0)
+ goto done;
+
error = git_refspec__parse(&refspec, git_buf_cstr(&buf), true);
+ if (!error)
+ *valid = 1;
+ else if (error == GIT_EINVALIDSPEC)
+ error = 0;
+
+done:
git_buf_dispose(&buf);
git_refspec__dispose(&refspec);
- git_error_clear();
- return error == 0;
+ return error;
+}
+
+int git_remote_is_valid_name(const char *remote_name)
+{
+ int valid = 0;
+
+ git_remote_name_is_valid(&valid, remote_name);
+ return valid;
}
git_refspec *git_remote__matching_refspec(git_remote *remote, const char *refname)