Skip to content

Commit

Permalink
Fix crash in reference rename
Browse files Browse the repository at this point in the history
Fixes #1233
  • Loading branch information
jdavid committed Sep 22, 2023
1 parent 8f912e1 commit bd9f2a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,22 @@ Reference_rename(Reference *self, PyObject *py_name)
{
CHECK_REFERENCE(self);

/* Get the C name */
// Get the C name
char *c_name = pgit_encode_fsdefault(py_name);
if (c_name == NULL)
return NULL;

/* Rename */
// Rename
git_reference *new_reference;
int err = git_reference_rename(&new_reference, self->reference, c_name, 0, NULL);
free(c_name);

git_reference_free(self->reference);
if (err)
return Error_set(err);

// Upadate reference
git_reference_free(self->reference);
self->reference = new_reference;

Py_RETURN_NONE;
}

Expand Down
9 changes: 7 additions & 2 deletions test/test_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,17 @@ def test_refs_delete(testrepo):

def test_refs_rename(testrepo):
# We add a tag as a new reference that points to "origin/master"
reference = testrepo.references.create('refs/tags/version1',
LAST_COMMIT)
reference = testrepo.references.create('refs/tags/version1', LAST_COMMIT)
assert reference.name == 'refs/tags/version1'
reference.rename('refs/tags/version2')
assert reference.name == 'refs/tags/version2'

with pytest.raises(AlreadyExistsError):
reference.rename('refs/tags/version2')

with pytest.raises(InvalidSpecError):
reference.rename('b1')

#def test_reload(testrepo):
# name = 'refs/tags/version1'
# ref = testrepo.create_reference(name, "refs/heads/master", symbolic=True)
Expand Down

0 comments on commit bd9f2a5

Please sign in to comment.