refspec: Add func to distinguish negative refspecs

Negative refspecs were added in Git v2.29.0 and are denoted by prefixing
a refspec with a caret. This adds a way to distinguish if a refspec is
negative and match negative refspecs.
This commit is contained in:
Ryan Pham
2024-11-28 13:35:11 +09:00
parent 3251d1bb62
commit 3d9f4061ca
3 changed files with 33 additions and 0 deletions

View File

@@ -78,6 +78,15 @@ GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec);
*/
GIT_EXTERN(git_direction) git_refspec_direction(const git_refspec *spec);
/**
* Check if a refspec's source descriptor matches a negative reference
*
* @param refspec the refspec
* @param refname the name of the reference to check
* @return 1 if the refspec matches, 0 otherwise
*/
GIT_EXTERN(int) git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname);
/**
* Check if a refspec's source descriptor matches a reference
*

View File

@@ -225,6 +225,14 @@ int git_refspec_force(const git_refspec *refspec)
return refspec->force;
}
int git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname)
{
if (refspec == NULL || refspec->src == NULL || !git_refspec_is_negative(refspec))
return false;
return (wildmatch(refspec->src + 1, refname, 0) == 0);
}
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
{
if (refspec == NULL || refspec->src == NULL)
@@ -340,6 +348,14 @@ int git_refspec_is_wildcard(const git_refspec *spec)
return (spec->src[strlen(spec->src) - 1] == '*');
}
int git_refspec_is_negative(const git_refspec *spec)
{
GIT_ASSERT_ARG(spec);
GIT_ASSERT_ARG(spec->src);
return (spec->src[0] == '^' && spec->dst == NULL);
}
git_direction git_refspec_direction(const git_refspec *spec)
{
GIT_ASSERT_ARG(spec);

View File

@@ -45,6 +45,14 @@ int git_refspec__serialize(git_str *out, const git_refspec *refspec);
*/
int git_refspec_is_wildcard(const git_refspec *spec);
/**
* Determines if a refspec is a negative refspec.
*
* @param spec the refspec
* @return 1 if the refspec is a negative, 0 otherwise
*/
int git_refspec_is_negative(const git_refspec *spec);
/**
* DWIM `spec` with `refs` existing on the remote, append the dwim'ed
* result in `out`.