Commit 023ebb9a by Edward Thomson

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.
parent 68e35588
...@@ -915,6 +915,15 @@ GIT_EXTERN(int) git_remote_rename( ...@@ -915,6 +915,15 @@ GIT_EXTERN(int) git_remote_rename(
/** /**
* Ensure the remote name is well-formed. * 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. * @param remote_name name to be checked.
* @return 1 if the reference name is acceptable; 0 if it isn't * @return 1 if the reference name is acceptable; 0 if it isn't
*/ */
......
...@@ -2092,24 +2092,42 @@ cleanup: ...@@ -2092,24 +2092,42 @@ cleanup:
return error; return error;
} }
int git_remote_is_valid_name( int git_remote_name_is_valid(int *valid, const char *remote_name)
const char *remote_name)
{ {
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
git_refspec refspec; git_refspec refspec = {0};
int error = -1; int error;
GIT_ASSERT(valid);
*valid = 0;
if (!remote_name || *remote_name == '\0') if (!remote_name || *remote_name == '\0')
return 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); 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_buf_dispose(&buf);
git_refspec__dispose(&refspec); git_refspec__dispose(&refspec);
git_error_clear(); return error;
return error == 0; }
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) git_refspec *git_remote__matching_refspec(git_remote *remote, const char *refname)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment