Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement repository.reset, with tests #292

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ moduleinit(PyObject* m)
ADD_CONSTANT_INT(m, GIT_SORT_TIME)
ADD_CONSTANT_INT(m, GIT_SORT_REVERSE)

/*
* Reset
*/
ADD_CONSTANT_INT(m, GIT_RESET_SOFT)
ADD_CONSTANT_INT(m, GIT_RESET_MIXED)
ADD_CONSTANT_INT(m, GIT_RESET_HARD)

/*
* References
*/
Expand Down
35 changes: 35 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,40 @@ Repository_lookup_note(Repository *self, PyObject* args)
return (PyObject*) wrap_note(self, &annotated_id, ref);
}


PyDoc_STRVAR(Repository_reset__doc__,
"reset(oid, reset_type)\n"
"\n"
"Resets current head to the provided oid");

PyObject *
Repository_reset(Repository *self, PyObject* args)
{
PyObject *py_oid;
git_oid oid;
git_object *target = NULL;
int err, reset_type;
size_t len;

if (!PyArg_ParseTuple(args, "Oi",
&py_oid,
&reset_type
))
return NULL;

len = py_oid_to_git_oid(py_oid, &oid);
if (len == 0)
return NULL;

err = git_object_lookup_prefix(&target, self->repo, &oid, len,
GIT_OBJ_COMMIT);
err = err < 0 ? err : git_reset(self->repo, target, reset_type);
git_object_free(target);
if (err < 0)
return Error_set_oid(err, &oid, len);
Py_RETURN_NONE;
}

PyMethodDef Repository_methods[] = {
METHOD(Repository, create_blob, METH_VARARGS),
METHOD(Repository, create_blob_fromworkdir, METH_VARARGS),
Expand Down Expand Up @@ -1459,6 +1493,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, lookup_branch, METH_VARARGS),
METHOD(Repository, listall_branches, METH_VARARGS),
METHOD(Repository, create_branch, METH_VARARGS),
METHOD(Repository, reset, METH_VARARGS),
{NULL}
};

Expand Down
63 changes: 63 additions & 0 deletions test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,69 @@ def test_merge_base(self):
self.assertEqual(commit.hex,
'acecd5ea2924a4b900e7e149496e1f4b57976e51')

def test_reset_hard(self):
ref = "5ebeeebb320790caf276b9fc8b24546d63316533"
with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
self.assertTrue("hola mundo\n" in lines)
self.assertTrue("bonjour le monde\n" in lines)

self.repo.reset(
ref,
pygit2.GIT_RESET_HARD)
self.assertEqual(self.repo.head.target.hex, ref)

with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
#Hard reset will reset the working copy too
self.assertFalse("hola mundo\n" in lines)
self.assertFalse("bonjour le monde\n" in lines)

def test_reset_soft(self):
ref = "5ebeeebb320790caf276b9fc8b24546d63316533"
with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
self.assertTrue("hola mundo\n" in lines)
self.assertTrue("bonjour le monde\n" in lines)

self.repo.reset(
ref,
pygit2.GIT_RESET_SOFT)
self.assertEqual(self.repo.head.target.hex, ref)
with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
#Soft reset will not reset the working copy
self.assertTrue("hola mundo\n" in lines)
self.assertTrue("bonjour le monde\n" in lines)

#soft reset will keep changes in the index
diff = self.repo.diff(cached=True)
self.assertRaises(KeyError, lambda: diff[0])

def test_reset_mixed(self):
ref = "5ebeeebb320790caf276b9fc8b24546d63316533"
with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
self.assertTrue("hola mundo\n" in lines)
self.assertTrue("bonjour le monde\n" in lines)

self.repo.reset(
ref,
pygit2.GIT_RESET_MIXED)

self.assertEqual(self.repo.head.target.hex, ref)

with open(os.path.join(self.repo.workdir, "hello.txt")) as f:
lines = f.readlines()
#mixed reset will not reset the working copy
self.assertTrue("hola mundo\n" in lines)
self.assertTrue("bonjour le monde\n" in lines)

#mixed reset will set the index to match working copy
diff = self.repo.diff(cached=True)
self.assertTrue("hola mundo\n" in diff.patch)
self.assertTrue("bonjour le monde\n" in diff.patch)


class NewRepositoryTest(utils.NoRepoTestCase):

Expand Down