Skip to content

Commit

Permalink
support setting a detatched HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663 committed Jun 23, 2014
1 parent a063867 commit b190169
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,31 @@ Repository_head__get__(Repository *self)
}

int
Repository_head__set__(Repository *self, PyObject *py_refname)
Repository_head__set__(Repository *self, PyObject *py_val)
{
int err;
const char *refname;
PyObject *trefname;
if (PyObject_TypeCheck(py_val, &OidType)) {
git_oid oid;
py_oid_to_git_oid(py_val, &oid);
err = git_repository_set_head_detached(self->repo, &oid, NULL, NULL);
if (err < 0) {
Error_set(err);
return -1;
}
} else {
const char *refname;
PyObject *trefname;

refname = py_str_borrow_c_str(&trefname, py_refname, NULL);
if (refname == NULL)
return -1;
refname = py_str_borrow_c_str(&trefname, py_val, NULL);
if (refname == NULL)
return -1;

err = git_repository_set_head(self->repo, refname, NULL, NULL);
Py_DECREF(trefname);
if (err < 0) {
Error_set_str(err, refname);
return -1;
err = git_repository_set_head(self->repo, refname, NULL, NULL);
Py_DECREF(trefname);
if (err < 0) {
Error_set_str(err, refname);
return -1;
}
}

return 0;
Expand Down
9 changes: 9 additions & 0 deletions test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def test_head(self):
self.assertFalse(self.repo.head_is_unborn)
self.assertFalse(self.repo.head_is_detached)

def test_set_head(self):
# Test setting a detatched HEAD.
self.repo.head = Oid(hex=PARENT_SHA)
self.assertEqual(self.repo.head.target.hex, PARENT_SHA)
# And test setting a normal HEAD.
self.repo.head = "refs/heads/master"
self.assertEqual(self.repo.head.name, "refs/heads/master")
self.assertEqual(self.repo.head.target.hex, HEAD_SHA)

def test_read(self):
self.assertRaises(TypeError, self.repo.read, 123)
self.assertRaisesWithArg(KeyError, '1' * 40, self.repo.read, '1' * 40)
Expand Down

0 comments on commit b190169

Please sign in to comment.