Commit d70979cf by Edward Thomson

refs: error checking in internal name validation

Move `git_reference__is_valid_name` to `git_reference__name_is_valid`,
which returns errors and sets an out boolean parameter.
parent 52294c41
...@@ -1287,19 +1287,32 @@ cleanup: ...@@ -1287,19 +1287,32 @@ cleanup:
return error; return error;
} }
int git_reference__is_valid_name(const char *refname, unsigned int flags) int git_reference__name_is_valid(
int *valid,
const char *refname,
unsigned int flags)
{ {
if (git_reference__normalize_name(NULL, refname, flags) < 0) { int error;
git_error_clear();
return false;
}
return true; *valid = 0;
error = git_reference__normalize_name(NULL, refname, flags);
if (!error)
*valid = 1;
else if (error == GIT_EINVALIDSPEC)
error = 0;
return error;
} }
int git_reference_is_valid_name(const char *refname) int git_reference_is_valid_name(const char *refname)
{ {
return git_reference__is_valid_name(refname, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL); int valid = 0;
git_reference__name_is_valid(&valid, refname, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL);
return valid;
} }
const char *git_reference__shorthand(const char *name) const char *git_reference__shorthand(const char *name)
......
...@@ -85,7 +85,7 @@ git_reference *git_reference__realloc(git_reference **ptr_to_ref, const char *na ...@@ -85,7 +85,7 @@ git_reference *git_reference__realloc(git_reference **ptr_to_ref, const char *na
int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags); int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags);
int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *sig, const char *log_message); int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *sig, const char *log_message);
int git_reference__is_valid_name(const char *refname, unsigned int flags); int git_reference__name_is_valid(int *valid, const char *name, unsigned int flags);
int git_reference__is_branch(const char *ref_name); int git_reference__is_branch(const char *ref_name);
int git_reference__is_remote(const char *ref_name); int git_reference__is_remote(const char *ref_name);
int git_reference__is_tag(const char *ref_name); int git_reference__is_tag(const char *ref_name);
......
...@@ -21,7 +21,8 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -21,7 +21,8 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
size_t llen; size_t llen;
int is_glob = 0; int is_glob = 0;
const char *lhs, *rhs; const char *lhs, *rhs;
int flags; int valid = 0;
unsigned int flags;
assert(refspec && input); assert(refspec && input);
...@@ -81,8 +82,11 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -81,8 +82,11 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
*/ */
if (!*refspec->src) if (!*refspec->src)
; /* empty is ok */ ; /* empty is ok */
else if (!git_reference__is_valid_name(refspec->src, flags)) else if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
goto on_error;
else if (!valid)
goto invalid; goto invalid;
/* /*
* RHS * RHS
* - missing is ok, and is same as empty. * - missing is ok, and is same as empty.
...@@ -93,7 +97,9 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -93,7 +97,9 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
; /* ok */ ; /* ok */
else if (!*refspec->dst) else if (!*refspec->dst)
; /* ok */ ; /* ok */
else if (!git_reference__is_valid_name(refspec->dst, flags)) else if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0)
goto on_error;
else if (!valid)
goto invalid; goto invalid;
} else { } else {
/* /*
...@@ -106,12 +112,15 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -106,12 +112,15 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
if (!*refspec->src) if (!*refspec->src)
; /* empty is ok */ ; /* empty is ok */
else if (is_glob) { else if (is_glob) {
if (!git_reference__is_valid_name(refspec->src, flags)) if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
goto on_error;
else if (!valid)
goto invalid; goto invalid;
} }
else { else {
; /* anything goes, for now */ ; /* anything goes, for now */
} }
/* /*
* RHS * RHS
* - missing is allowed, but LHS then must be a * - missing is allowed, but LHS then must be a
...@@ -120,12 +129,16 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -120,12 +129,16 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
* - otherwise it must be a valid looking ref. * - otherwise it must be a valid looking ref.
*/ */
if (!refspec->dst) { if (!refspec->dst) {
if (!git_reference__is_valid_name(refspec->src, flags)) if (git_reference__name_is_valid(&valid, refspec->src, flags) < 0)
goto on_error;
else if (!valid)
goto invalid; goto invalid;
} else if (!*refspec->dst) { } else if (!*refspec->dst) {
goto invalid; goto invalid;
} else { } else {
if (!git_reference__is_valid_name(refspec->dst, flags)) if (git_reference__name_is_valid(&valid, refspec->dst, flags) < 0)
goto on_error;
else if (!valid)
goto invalid; goto invalid;
} }
...@@ -141,10 +154,12 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch) ...@@ -141,10 +154,12 @@ int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
return 0; return 0;
invalid: invalid:
git_error_set( git_error_set(
GIT_ERROR_INVALID, GIT_ERROR_INVALID,
"'%s' is not a valid refspec.", input); "'%s' is not a valid refspec.", input);
on_error:
git_refspec__dispose(refspec); git_refspec__dispose(refspec);
return -1; return -1;
} }
......
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