10000 refspec: Add func to distinguish negative refspecs · libgit2/libgit2@111fa3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 111fa3a

Browse files
committed
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.
1 parent 3251d1b commit 111fa3a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/git2/refspec.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ GIT_EXTERN(int) git_refspec_force(const git_refspec *refspec);
7878
*/
7979
GIT_EXTERN(git_direction) git_refspec_direction(const git_refspec *spec);
8080

81+
/**
82+
* Check if a refspec's source descriptor matches a negative reference
83+
*
84+
* @param refspec the refspec
85+
* @param refname the name of the reference to check
86+
* @return 1 if the refspec matches, 0 otherwise
87+
*/
88+
GIT_EXTERN(int) git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname);
89+
8190
/**
8291
* Check if a refspec's source descriptor matches a reference
8392
*

src/libgit2/refspec.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ int git_refspec_force(const git_refspec *refspec)
225225
return refspec->force;
226226
}
227227

228+
int git_refspec_src_matches_negative(const git_refspec *refspec, const char *refname)
229+
{
230+
if (refspec == NULL || refspec->src == NULL || !git_refspec_is_negative(refspec))
231+
return false;
232+
233+
return (wildmatch(refspec->src + 1, refname, 0) == 0);
234+
}
235+
228236
int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
229237
{
230238
if (refspec == NULL || refspec->src == NULL)
@@ -340,6 +348,14 @@ int git_refspec_is_wildcard(const git_refspec *spec)
340348
return (spec->src[strlen(spec->src) - 1] == '*');
341349
}
342350

351+
int git_refspec_is_negative(const git_refspec *spec)
352+
{
353+
GIT_ASSERT_ARG(spec);
354+
GIT_ASSERT_ARG(spec->src);
355+
356+
return (spec->src[0] == '^');
357+
}
358+
343359
git_direction git_refspec_direction(const git_refspec *spec)
344360
{
345361
GIT_ASSERT_ARG(spec);

src/libgit2/refspec.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ int git_refspec__serialize(git_str *out, const git_refspec *refspec);
4545
*/
4646
int git_refspec_is_wildcard(const git_refspec *spec);
4747

48+
/**
49+
* Determines if a refspec is a negative refspec.
50+
*
51+
* @param spec the refspec
52+
* @return 1 if the refspec is a negative, 0 otherwise
53+
*/
54+
int git_refspec_is_negative(const git_refspec *spec);
55+
4856
/**
4957
* DWIM `spec` with `refs` existing on the remote, append the dwim'ed
5058
* result in `out`.

0 commit comments

Comments
 (0)
0