Skip to content

Commit a3ab5c8

Browse files
committed
Fix test_diff_patch
Now Diff.parse_diff in Python 2 if given a unicode object it will decode using utf-8 instead of default.
1 parent 60f2e13 commit a3ab5c8

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/diff.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -998,24 +998,24 @@ PyDoc_STRVAR(Diff_parse_diff__doc__,
998998
"Parses a git unified diff into a diff object without a repository");
999999

10001000
static PyObject *
1001-
Diff_parse_diff(PyObject *self, PyObject *args)
1001+
Diff_parse_diff(PyObject *self, PyObject *py_str)
10021002
{
1003-
/* A wrapper around
1004-
* git_diff_from_buffer
1005-
*/
1006-
git_diff *diff;
1007-
const char *content = NULL;
1008-
Py_ssize_t content_len;
1009-
int err;
1010-
1011-
if (!PyArg_ParseTuple(args, "s#", &content, &content_len))
1012-
return NULL;
1003+
/* A wrapper around git_diff_from_buffer */
1004+
git_diff *diff;
1005+
const char *content = NULL;
1006+
int err;
1007+
PyObject *tvalue;
10131008

1014-
err = git_diff_from_buffer(&diff, content, content_len);
1015-
if (err < 0)
1016-
return Error_set(err);
1009+
content = py_str_borrow_c_str(&tvalue, py_str, NULL);
1010+
if (content == NULL)
1011+
return NULL;
1012+
1013+
err = git_diff_from_buffer(&diff, content, strlen(content));
1014+
Py_DECREF(tvalue);
1015+
if (err < 0)
1016+
return Error_set(err);
10171017

1018-
return wrap_diff(diff, NULL);
1018+
return wrap_diff(diff, NULL);
10191019
}
10201020

10211021
static void
@@ -1044,7 +1044,7 @@ static PyMethodDef Diff_methods[] = {
10441044
METHOD(Diff, find_similar, METH_VARARGS | METH_KEYWORDS),
10451045
METHOD(Diff, from_c, METH_STATIC | METH_VARARGS),
10461046
{"parse_diff", (PyCFunction) Diff_parse_diff,
1047-
METH_VARARGS | METH_STATIC, Diff_parse_diff__doc__},
1047+
METH_O | METH_STATIC, Diff_parse_diff__doc__},
10481048
{NULL}
10491049
};
10501050

src/repository.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,10 +1924,7 @@ Repository_apply(Repository *self, PyObject *py_diff)
19241924
git_apply_location_t location = GIT_APPLY_LOCATION_WORKDIR;
19251925
git_apply_options options = GIT_APPLY_OPTIONS_INIT;
19261926

1927-
err = git_apply(self->repo,
1928-
((Diff*)py_diff)->diff,
1929-
location,
1930-
&options);
1927+
err = git_apply(self->repo, ((Diff*)py_diff)->diff, location, &options);
19311928

19321929
if (err < 0)
19331930
return Error_set(err);

test/test_repository.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,19 +480,20 @@ def test_diff_patch(self):
480480
new_content = 'bye world\nadiós\nau revoir monde\n'
481481

482482
# create the patch
483-
with open(os.path.join(self.repo.workdir, 'hello.txt'), 'w') as f:
484-
f.write(new_content)
483+
with open(os.path.join(self.repo.workdir, 'hello.txt'), 'wb') as f:
484+
f.write(new_content.encode('utf-8'))
485485

486486
patch = self.repo.diff().patch
487487

488488
# rollback all changes
489489
self.repo.checkout('HEAD', strategy=pygit2.GIT_CHECKOUT_FORCE)
490490

491491
# apply the patch and compare
492-
self.repo.apply(pygit2.Diff.parse_diff(patch))
492+
diff = pygit2.Diff.parse_diff(patch)
493+
self.repo.apply(diff)
493494

494-
with open(os.path.join(self.repo.workdir, 'hello.txt'), 'r') as f:
495-
content = f.read()
495+
with open(os.path.join(self.repo.workdir, 'hello.txt'), 'rb') as f:
496+
content = f.read().decode('utf-8')
496497

497498
self.assertEqual(content, new_content)
498499

0 commit comments

Comments
 (0)