Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/git2/refspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@
*/
GIT_BEGIN_DECL

/**
* Parse a given refspec string
*
* @param refspec a pointer to hold the refspec handle
* @param input the refspec string
* @param is_fetch is this a refspec for a fetch
* @return 0 if the refspec string could be parsed, -1 otherwise
*/
GIT_EXTERN(int) git_refspec_parse(git_refspec **refspec, const char *input, int is_fetch);

/**
* Free a refspec object which has been created by git_refspec_parse
*
* @param refspec the refspec object
*/
GIT_EXTERN(void) git_refspec_free(git_refspec *refspec);

/**
* Get the source specifier
*
Expand Down
25 changes: 25 additions & 0 deletions src/refspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ void git_refspec__free(git_refspec *refspec)
memset(refspec, 0x0, sizeof(git_refspec));
}

int git_refspec_parse(git_refspec **out_refspec, const char *input, int is_fetch)
{
git_refspec *refspec;
assert(out_refspec && input);

*out_refspec = NULL;

refspec = git__malloc(sizeof(git_refspec));
GITERR_CHECK_ALLOC(refspec);

if (git_refspec__parse(refspec, input, !!is_fetch) != 0) {
git__free(refspec);
return -1;
}

*out_refspec = refspec;
return 0;
}

void git_refspec_free(git_refspec *refspec)
{
git_refspec__free(refspec);
git__free(refspec);
}

const char *git_refspec_src(const git_refspec *refspec)
{
return refspec == NULL ? NULL : refspec->src;
Expand Down
12 changes: 12 additions & 0 deletions tests/network/refspecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,15 @@ void test_network_refspecs__matching(void)

git_refspec__free(&spec);
}

void test_network_refspecs__parse_free(void)
{
git_refspec *spec = NULL;

cl_git_fail(git_refspec_parse(&spec, "", 0));
cl_git_fail(git_refspec_parse(&spec, ":::", 0));
cl_git_pass(git_refspec_parse(&spec, "HEAD:", 1));

cl_assert(spec != NULL);
git_refspec_free(spec);
}