Commit 06b9d915 by Carlos Martín Nieto

revwalk: allow pushing/hiding a reference by name

The code was already there, so factor it out and let users push an OID
by giving it a reference name. Only refs to commits are
supported. Annotated tags will throw an error.
parent 081d2291
......@@ -164,6 +164,28 @@ GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk *walk, const char *glob);
GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk);
/**
* Push the OID pointed to by a reference
*
* The reference must point to a commit.
*
* @param walk the walker being used for the traversal
* @param refname the referece to push
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_revwalk_push_ref(git_revwalk *walk, const char *refname);
/**
* Hide the OID pointed to by a reference
*
* The reference must point to a commit.
*
* @param walk the walker being used for the traversal
* @param refname the referece to hide
* @return GIT_SUCCESS or an error code
*/
GIT_EXTERN(int) git_revwalk_hide_ref(git_revwalk *walk, const char *refname);
/**
* Get the next commit from the revision walk.
*
* The initial call to this method is *not* blocking when
......
......@@ -310,31 +310,35 @@ int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
return push_commit(walk, oid, 1);
}
struct push_cb_data {
git_revwalk *walk;
const char *glob;
int hide;
};
static int push_glob_cb(const char *refname, void *data_)
static int push_ref(git_revwalk *walk, const char *refname, int hide)
{
struct push_cb_data *data = (struct push_cb_data *)data_;
if (!git__fnmatch(data->glob, refname, 0)) {
git_reference *ref, *resolved;
int error;
error = git_reference_lookup(&ref, data->walk->repo, refname);
error = git_reference_lookup(&ref, walk->repo, refname);
if (error < GIT_SUCCESS)
return error;
error = git_reference_resolve(&resolved, ref);
git_reference_free(ref);
if (error < GIT_SUCCESS)
return error;
error = push_commit(data->walk, git_reference_oid(resolved), data->hide);
error = push_commit(walk, git_reference_oid(resolved), hide);
git_reference_free(resolved);
return error;
}
}
struct push_cb_data {
git_revwalk *walk;
const char *glob;
int hide;
};
static int push_glob_cb(const char *refname, void *data_)
{
struct push_cb_data *data = (struct push_cb_data *)data_;
if (!git__fnmatch(data->glob, refname, 0))
return push_ref(data->walk, refname, data->hide);
return GIT_SUCCESS;
}
......@@ -394,37 +398,28 @@ int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
return push_glob(walk, glob, 1);
}
static int push_head(git_revwalk *walk, int hide)
{
git_reference *ref, *resolved;
int error;
error = git_reference_lookup(&ref, walk->repo, "HEAD");
if (error < GIT_SUCCESS) {
return error;
}
error = git_reference_resolve(&resolved, ref);
if (error < GIT_SUCCESS) {
return error;
}
git_reference_free(ref);
error = push_commit(walk, git_reference_oid(resolved), hide);
git_reference_free(resolved);
return error;
}
int git_revwalk_push_head(git_revwalk *walk)
{
assert(walk);
return push_head(walk, 0);
return push_ref(walk, GIT_HEAD_FILE, 0);
}
int git_revwalk_hide_head(git_revwalk *walk)
{
assert(walk);
return push_head(walk, 1);
return push_ref(walk, GIT_HEAD_FILE, 1);
}
int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
return push_ref(walk, refname, 0);
}
int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
return push_ref(walk, refname, 1);
}
static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit)
......
......@@ -148,14 +148,13 @@ void test_revwalk_basic__push_head(void)
cl_assert(i == 7);
}
void test_revwalk_basic__push_head_hide_glob(void)
void test_revwalk_basic__push_head_hide_ref(void)
{
int i = 0;
git_oid oid;
cl_git_pass(git_revwalk_push_head(_walk));
/* This is a hack, as we know this will only match the packed-test branch */
cl_git_pass(git_revwalk_hide_glob(_walk, "heads/packed-test*"));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
while (git_revwalk_next(&oid, _walk) == GIT_SUCCESS) {
i++;
......
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