Skip to content

Commit

Permalink
Update with master
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarsj committed Dec 2, 2013
1 parent e65ab19 commit 9f4f978
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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
37 changes: 37 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,42 @@ PyObject* Repository_blame(Repository *self, PyObject *args, PyObject *kwds)
return wrap_blame(blame, self);
}

PyDoc_STRVAR(Repository_reset__doc__,
"reset(oid, reset_type)\n"
"\n"
"Resets current head to the provided oid.\n"
"reset_type:\n"
"GIT_RESET_SOFT: resets head to point to oid, but does not modfy working copy, and leaves the changes in the index.\n"
"GIT_RESET_MIXED: resets head to point to oid, but does not modfy working copy. It empties the index too.\n"
"GIT_RESET_HARD: resets head to point to oid, and resets too the working copy and the content of the index.\n");

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_ANY);
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),
Expand Down Expand Up @@ -1534,6 +1570,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, listall_branches, METH_VARARGS),
METHOD(Repository, create_branch, METH_VARARGS),
METHOD(Repository, blame, METH_VARARGS | METH_KEYWORDS),
METHOD(Repository, reset, METH_VARARGS),
{NULL}
};

Expand Down
66 changes: 63 additions & 3 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 Expand Up @@ -274,7 +337,6 @@ def test_keyword_arg_true(self):
self.assertTrue(repo.is_bare)



class DiscoverRepositoryTest(utils.NoRepoTestCase):

def test_discover_repo(self):
Expand All @@ -284,7 +346,6 @@ def test_discover_repo(self):
self.assertEqual(repo.path, discover_repository(subdir))



class EmptyRepositoryTest(utils.EmptyRepoTestCase):

def test_is_empty(self):
Expand All @@ -298,7 +359,6 @@ def test_head(self):
self.assertFalse(self.repo.head_is_detached)



class CloneRepositoryTest(utils.NoRepoTestCase):

def test_clone_repository(self):
Expand Down

0 comments on commit 9f4f978

Please sign in to comment.