Skip to content

Commit 91e699e

Browse files
committed
diff: add support for patchid.
Signed-off-by: Gregory Herrero <[email protected]>
1 parent d2b0ced commit 91e699e

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/diff.c

+17
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,22 @@ Diff_len(Diff *self)
565565
return (Py_ssize_t)git_diff_num_deltas(self->diff);
566566
}
567567

568+
PyDoc_STRVAR(Diff_patchid__doc__,
569+
"Corresponding patchid.");
570+
571+
PyObject *
572+
Diff_patchid__get__(Diff *self)
573+
{
574+
git_oid oid;
575+
int err;
576+
577+
err = git_diff_patchid(&oid, self->diff, NULL);
578+
if (err < 0)
579+
return Error_set(err);
580+
return git_oid_to_python(&oid);
581+
}
582+
583+
568584
PyDoc_STRVAR(Diff_deltas__doc__, "Iterate over the diff deltas.");
569585

570586
PyObject *
@@ -1024,6 +1040,7 @@ PyGetSetDef Diff_getsetters[] = {
10241040
GETTER(Diff, deltas),
10251041
GETTER(Diff, patch),
10261042
GETTER(Diff, stats),
1043+
GETTER(Diff, patchid),
10271044
{NULL}
10281045
};
10291046

src/diff.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
PyObject* Diff_changes(Diff *self);
3737
PyObject* Diff_patch(Diff *self);
38+
PyObject* Diff_patchid(Diff *self);
3839

3940
PyObject* wrap_diff(git_diff *diff, Repository *repo);
4041
PyObject* wrap_diff_delta(const git_diff_delta *delta);

test/test_diff.py

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
-c/d contents
6464
"""
6565

66+
PATCHID = 'f31412498a17e6c3fbc635f2c5f9aa3ef4c1a9b7'
67+
6668
PATCH_BINARY = """diff --git a/binary_file b/binary_file
6769
index 86e5c10..b835d73 100644
6870
Binary files a/binary_file and b/binary_file differ
@@ -279,6 +281,13 @@ def test_diff_ids(self):
279281
assert delta.old_file.id.hex == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
280282
assert delta.new_file.id.hex == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
281283

284+
def test_diff_patchid(self):
285+
commit_a = self.repo[COMMIT_SHA1_1]
286+
commit_b = self.repo[COMMIT_SHA1_2]
287+
diff = commit_a.tree.diff_to_tree(commit_b.tree)
288+
assert diff.patch == PATCH
289+
assert diff.patchid.hex == PATCHID
290+
282291
def test_hunk_content(self):
283292
commit_a = self.repo[COMMIT_SHA1_1]
284293
commit_b = self.repo[COMMIT_SHA1_2]

0 commit comments

Comments
 (0)