[Python-modules-commits] [python-pygit2] 01/11: Import python-pygit2_0.26.3.orig.tar.gz

Ondrej Novy onovy at debian.org
Fri Jan 5 15:50:17 UTC 2018


This is an automated email from the git hooks/post-receive script.

onovy pushed a commit to branch master
in repository python-pygit2.

commit cad87463cef2aed297dd01f16840c21484f2730d
Author: Ondřej Nový <onovy at debian.org>
Date:   Fri Jan 5 16:09:11 2018 +0100

    Import python-pygit2_0.26.3.orig.tar.gz
---
 CHANGELOG.rst       |  26 ++++++++
 docs/conf.py        |   2 +-
 docs/diff.rst       |   6 +-
 docs/install.rst    |  10 +--
 pygit2/_build.py    |   2 +-
 src/blob.c          |   4 +-
 src/diff.c          |  89 ++++++++++++++++++++++++--
 src/patch.c         |  77 ++++++++++++-----------
 src/patch.h         |   2 +-
 src/pygit2.c        |   2 +
 src/types.h         |   9 +++
 test/test_blob.py   |  30 +++++++++
 test/test_diff.py   |  18 ++++++
 test/test_patch.py  | 175 ++++++++++++++++++++++++++++++++++++++++++++--------
 test/test_remote.py |   4 ++
 15 files changed, 375 insertions(+), 81 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 45a4896..2b549ad 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,3 +1,29 @@
+0.26.3 (2017-12-24)
+-------------------------
+
+- New ``Diff.deltas``
+  `#736 <https://github.com/libgit2/pygit2/issues/736>`_
+
+- Improvements to ``Patch.create_from``
+  `#753 <https://github.com/libgit2/pygit2/pull/753>`_
+  `#756 <https://github.com/libgit2/pygit2/pull/756>`_
+  `#759 <https://github.com/libgit2/pygit2/pull/759>`_
+
+- Fix build and tests in Windows, broken in the previous release
+  `#749 <https://github.com/libgit2/pygit2/pull/749>`_
+  `#751 <https://github.com/libgit2/pygit2/pull/751>`_
+
+- Review ``Patch.patch``
+  `#757 <https://github.com/libgit2/pygit2/issues/757>`_
+
+- Workaround bug `#4442 <https://github.com/libgit2/libgit2/issues/4442>`_
+  in libgit2, and improve unit tests
+  `#748 <https://github.com/libgit2/pygit2/issues/748>`_
+  `#754 <https://github.com/libgit2/pygit2/issues/754>`_
+  `#758 <https://github.com/libgit2/pygit2/pull/758>`_
+  `#761 <https://github.com/libgit2/pygit2/pull/761>`_
+
+
 0.26.2 (2017-12-01)
 -------------------------
 
diff --git a/docs/conf.py b/docs/conf.py
index 236ddfb..86e2fbf 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -52,7 +52,7 @@ copyright = u'2010-2015 The pygit2 contributors'
 # The short X.Y version.
 version = '0.26'
 # The full version, including alpha/beta/rc tags.
-release = '0.26.2'
+release = '0.26.3'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
diff --git a/docs/diff.rst b/docs/diff.rst
index b1d9fbe..1027323 100644
--- a/docs/diff.rst
+++ b/docs/diff.rst
@@ -38,7 +38,6 @@ Examples
 The Diff type
 ====================
 
-.. autoattribute:: pygit2.Diff.patch
 .. method:: Diff.__iter__()
 
    Returns an iterator over the deltas/patches in this diff.
@@ -47,8 +46,11 @@ The Diff type
 
    Returns the number of deltas/patches in this diff.
 
-.. automethod:: pygit2.Diff.merge
+.. autoattribute:: pygit2.Diff.deltas
 .. automethod:: pygit2.Diff.find_similar
+.. automethod:: pygit2.Diff.merge
+.. autoattribute:: pygit2.Diff.patch
+.. autoattribute:: pygit2.Diff.stats
 
 
 The Patch type
diff --git a/docs/install.rst b/docs/install.rst
index 178aa62..4678418 100644
--- a/docs/install.rst
+++ b/docs/install.rst
@@ -43,11 +43,11 @@ while the last number |lq| *.micro* |rq| auto-increments independently.
 
 As illustration see this table of compatible releases:
 
-+-----------+------------------------+----------------+------------------------+
-|**libgit2**| 0.26.0                 | 0.25.0, 0.25.1 | 0.24.0, 0.24.1, 0.24.2 |
-+-----------+------------------------+----------------+------------------------+
-|**pygit2** | 0.26.0, 0.26.1, 0.26.2 | 0.25.0, 0.25.1 | 0.24.0, 0.24.1, 0.24.2 |
-+-----------+------------------------+----------------+------------------------+
++-----------+--------------------------------+----------------+------------------------+
+|**libgit2**| 0.26.0                         | 0.25.0, 0.25.1 | 0.24.0, 0.24.1, 0.24.2 |
++-----------+--------------------------------+----------------+------------------------+
+|**pygit2** | 0.26.0, 0.26.1, 0.26.2, 0.26.3 | 0.25.0, 0.25.1 | 0.24.0, 0.24.1, 0.24.2 |
++-----------+--------------------------------+----------------+------------------------+
 
 .. warning::
 
diff --git a/pygit2/_build.py b/pygit2/_build.py
index 60148de..17c89bd 100644
--- a/pygit2/_build.py
+++ b/pygit2/_build.py
@@ -37,7 +37,7 @@ from os import getenv
 #
 # The version number of pygit2
 #
-__version__ = '0.26.2'
+__version__ = '0.26.3'
 
 
 #
diff --git a/src/blob.c b/src/blob.c
index d3e7284..6ec23f4 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -81,7 +81,7 @@ Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
     if (err < 0)
         return Error_set(err);
 
-    return wrap_patch(patch);
+    return wrap_patch(patch, self, py_blob);
 }
 
 
@@ -130,7 +130,7 @@ Blob_diff_to_buffer(Blob *self, PyObject *args, PyObject *kwds)
     if (err < 0)
         return Error_set(err);
 
-    return wrap_patch(patch);
+    return wrap_patch(patch, self, NULL);
 }
 
 static PyMethodDef Blob_methods[] = {
diff --git a/src/diff.c b/src/diff.c
index 9c2a11e..9d084f1 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -422,7 +422,7 @@ diff_get_patch_byindex(git_diff *diff, size_t idx)
     if (err < 0)
         return Error_set(err);
 
-    return (PyObject*) wrap_patch(patch);
+    return (PyObject*) wrap_patch(patch, NULL, NULL);
 }
 
 PyObject *
@@ -475,6 +475,68 @@ PyTypeObject DiffIterType = {
     (iternextfunc) DiffIter_iternext,          /* tp_iternext       */
 };
 
+PyObject *
+diff_get_delta_byindex(git_diff *diff, size_t idx)
+{
+    const git_diff_delta *delta = git_diff_get_delta(diff, idx);
+    if (delta == NULL) {
+        PyErr_SetObject(PyExc_IndexError, PyInt_FromSize_t(idx));
+        return NULL;
+    }
+
+    return (PyObject*) wrap_diff_delta(delta);
+}
+
+PyObject *
+DeltasIter_iternext(DeltasIter *self)
+{
+    if (self->i < self->n)
+        return diff_get_delta_byindex(self->diff->diff, self->i++);
+
+    PyErr_SetNone(PyExc_StopIteration);
+    return NULL;
+}
+
+void
+DeltasIter_dealloc(DeltasIter *self)
+{
+    Py_CLEAR(self->diff);
+    PyObject_Del(self);
+}
+
+PyDoc_STRVAR(DeltasIter__doc__, "Deltas iterator object.");
+
+PyTypeObject DeltasIterType = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_pygit2.DeltasIter",                      /* tp_name           */
+    sizeof(DeltasIter),                        /* tp_basicsize      */
+    0,                                         /* tp_itemsize       */
+    (destructor)DeltasIter_dealloc,            /* tp_dealloc        */
+    0,                                         /* tp_print          */
+    0,                                         /* tp_getattr        */
+    0,                                         /* tp_setattr        */
+    0,                                         /* tp_compare        */
+    0,                                         /* tp_repr           */
+    0,                                         /* tp_as_number      */
+    0,                                         /* tp_as_sequence    */
+    0,                                         /* tp_as_mapping     */
+    0,                                         /* tp_hash           */
+    0,                                         /* tp_call           */
+    0,                                         /* tp_str            */
+    0,                                         /* tp_getattro       */
+    0,                                         /* tp_setattro       */
+    0,                                         /* tp_as_buffer      */
+    Py_TPFLAGS_DEFAULT,                        /* tp_flags          */
+    DeltasIter__doc__,                         /* tp_doc            */
+    0,                                         /* tp_traverse       */
+    0,                                         /* tp_clear          */
+    0,                                         /* tp_richcompare    */
+    0,                                         /* tp_weaklistoffset */
+    PyObject_SelfIter,                         /* tp_iter           */
+    (iternextfunc) DeltasIter_iternext,        /* tp_iternext       */
+};
+
+
 Py_ssize_t
 Diff_len(Diff *self)
 {
@@ -482,6 +544,23 @@ Diff_len(Diff *self)
     return (Py_ssize_t)git_diff_num_deltas(self->diff);
 }
 
+PyDoc_STRVAR(Diff_deltas__doc__, "Iterate over the diff deltas.");
+
+PyObject *
+Diff_deltas__get__(Diff *self)
+{
+    DeltasIter *iter;
+
+    iter = PyObject_New(DeltasIter, &DeltasIterType);
+    if (iter != NULL) {
+        Py_INCREF(self);
+        iter->diff = self;
+        iter->i = 0;
+        iter->n = git_diff_num_deltas(self->diff);
+    }
+    return (PyObject*)iter;
+}
+
 PyDoc_STRVAR(Diff_patch__doc__,
     "Patch diff string. Can be None in some cases, such as empty commits.");
 
@@ -672,7 +751,7 @@ PyDoc_STRVAR(DiffStats__doc__, "DiffStats object.");
 PyTypeObject DiffStatsType = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "_pygit2.DiffStats",                       /* tp_name           */
-    sizeof(DiffStats),                          /* tp_basicsize      */
+    sizeof(DiffStats),                         /* tp_basicsize      */
     0,                                         /* tp_itemsize       */
     (destructor)DiffStats_dealloc,             /* tp_dealloc        */
     0,                                         /* tp_print          */
@@ -697,7 +776,7 @@ PyTypeObject DiffStatsType = {
     0,                                         /* tp_weaklistoffset */
     0,                                         /* tp_iter           */
     0,                                         /* tp_iternext       */
-    DiffStats_methods,                                         /* tp_methods        */
+    DiffStats_methods,                         /* tp_methods        */
     0,                                         /* tp_members        */
     DiffStats_getseters,                       /* tp_getset         */
     0,                                         /* tp_base           */
@@ -814,7 +893,7 @@ Diff_getitem(Diff *self, PyObject *value)
     return diff_get_patch_byindex(self->diff, i);
 }
 
-PyDoc_STRVAR(Diff_stats__doc__, "Accumulate diff statistics for all patches");
+PyDoc_STRVAR(Diff_stats__doc__, "Accumulate diff statistics for all patches.");
 
 PyObject *
 Diff_stats__get__(Diff *self)
@@ -831,6 +910,7 @@ Diff_dealloc(Diff *self)
 }
 
 PyGetSetDef Diff_getseters[] = {
+    GETTER(Diff, deltas),
     GETTER(Diff, patch),
     GETTER(Diff, stats),
     {NULL}
@@ -849,6 +929,7 @@ static PyMethodDef Diff_methods[] = {
     {NULL}
 };
 
+/* TODO Implement Diff.patches, deprecate Diff_iter and Diff_getitem */
 
 PyDoc_STRVAR(Diff__doc__, "Diff objects.");
 
diff --git a/src/patch.c b/src/patch.c
index 503c21c..c5e0ca4 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -40,14 +40,13 @@ PyTypeObject PatchType;
 
 
 PyObject *
-wrap_patch(git_patch *patch)
+wrap_patch(git_patch *patch, Blob *oldblob, Blob *newblob)
 {
     Patch *py_patch;
     PyObject *py_hunk;
     size_t i, hunk_amounts;
 
-    if (!patch)
-        Py_RETURN_NONE;
+    assert(patch);
 
     py_patch = PyObject_New(Patch, &PatchType);
     if (py_patch) {
@@ -60,6 +59,12 @@ wrap_patch(git_patch *patch)
             if (py_hunk)
                 PyList_SetItem((PyObject*) py_patch->hunks, i, py_hunk);
         }
+
+        Py_XINCREF(oldblob);
+        py_patch->oldblob = oldblob;
+
+        Py_XINCREF(newblob);
+        py_patch->newblob = newblob;
     }
 
     return (PyObject*) py_patch;
@@ -68,6 +73,8 @@ wrap_patch(git_patch *patch)
 static void
 Patch_dealloc(Patch *self)
 {
+    Py_CLEAR(self->oldblob);
+    Py_CLEAR(self->newblob);
     Py_CLEAR(self->hunks);
     git_patch_free(self->patch);
     PyObject_Del(self);
@@ -78,9 +85,7 @@ PyDoc_STRVAR(Patch_delta__doc__, "Get the delta associated with a patch.");
 PyObject *
 Patch_delta__get__(Patch *self)
 {
-    if (!self->patch)
-        Py_RETURN_NONE;
-
+    assert(self->patch);
     return wrap_diff_delta(git_patch_get_delta(self->patch));
 }
 
@@ -93,11 +98,8 @@ Patch_line_stats__get__(Patch *self)
     size_t context, additions, deletions;
     int err;
 
-    if (!self->patch)
-        Py_RETURN_NONE;
-
-    err = git_patch_line_stats(&context, &additions, &deletions,
-                               self->patch);
+    assert(self->patch);
+    err = git_patch_line_stats(&context, &additions, &deletions, self->patch);
     if (err < 0)
         return Error_set(err);
 
@@ -108,7 +110,7 @@ PyDoc_STRVAR(Patch_create_from__doc__,
     "Create a patch from blobs, buffers, or a blob and a buffer");
 
 static PyObject *
-Patch_create_from(PyObject *self, PyObject *args)
+Patch_create_from(PyObject *self, PyObject *args, PyObject *kwds)
 {
   /* A generic wrapper around
    * git_patch_from_blob_and_buffer
@@ -119,24 +121,30 @@ Patch_create_from(PyObject *self, PyObject *args)
   git_patch *patch;
   char *old_as_path = NULL, *new_as_path = NULL;
   PyObject *oldobj = NULL, *newobj = NULL;
+  Blob *oldblob = NULL, *newblob = NULL;
+  const char *oldbuf = NULL, *newbuf = NULL;
+  Py_ssize_t oldbuflen, newbuflen;
   int err;
 
-  if (!PyArg_ParseTuple(args, "OzOz|I", &oldobj, &old_as_path, &newobj,
-                        &new_as_path, &opts.flags))
+  char *keywords[] = {"old", "new", "old_as_path", "new_as_path",
+                      "flag", "context_lines", "interhunk_lines",
+                      NULL};
+
+  if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|zzIHH", keywords,
+                                   &oldobj, &newobj, &old_as_path, &new_as_path,
+                                   &opts.flags, &opts.context_lines,
+                                   &opts.interhunk_lines))
     return NULL;
 
   if (oldobj != Py_None && PyObject_TypeCheck(oldobj, &BlobType))
   {
     /* The old object exists and is a blob */
-    Blob *oldblob = NULL;
     if (!PyArg_Parse(oldobj, "O!", &BlobType, &oldblob))
       return NULL;
 
     if (newobj != Py_None && PyObject_TypeCheck(newobj, &BlobType))
     {
       /* The new object exists and is a blob */
-      Blob *newblob = NULL;
-
       if (!PyArg_Parse(newobj, "O!", &BlobType, &newblob))
         return NULL;
 
@@ -145,8 +153,6 @@ Patch_create_from(PyObject *self, PyObject *args)
     }
     else {
       /* The new object does not exist or is a buffer */
-      const char* newbuf = NULL;
-      Py_ssize_t newbuflen;
       if (!PyArg_Parse(newobj, "z#", &newbuf, &newbuflen))
         return NULL;
 
@@ -158,8 +164,6 @@ Patch_create_from(PyObject *self, PyObject *args)
   else
   {
     /* The old object does exist and is a buffer */
-    const char *oldbuf = NULL, *newbuf = NULL;
-    Py_ssize_t oldbuflen, newbuflen;
     if (!PyArg_Parse(oldobj, "z#", &oldbuf, &oldbuflen))
       return NULL;
 
@@ -169,12 +173,11 @@ Patch_create_from(PyObject *self, PyObject *args)
     err = git_patch_from_buffers(&patch, oldbuf, oldbuflen, old_as_path,
                                  newbuf, newbuflen, new_as_path, &opts);
   }
-  
+
   if (err < 0)
     return Error_set(err);
 
-  return wrap_patch(patch);
-    
+  return wrap_patch(patch, oldblob, newblob);
 }
 
 
@@ -184,27 +187,23 @@ PyDoc_STRVAR(Patch_patch__doc__,
 PyObject *
 Patch_patch__get__(Patch *self)
 {
-  if (!self->patch)
-      Py_RETURN_NONE;
-
-  git_buf buf = {NULL};
-  int err = GIT_ERROR;
-  PyObject *py_patch = NULL;
-
-  err = git_patch_to_buf(&buf, self->patch);
-  if (err < 0)
-    goto cleanup;
+    git_buf buf = {NULL};
+    int err;
+    PyObject *py_patch;
 
-  py_patch = to_unicode(buf.ptr, NULL, NULL);
-  git_buf_free(&buf);
+    assert(self->patch);
+    err = git_patch_to_buf(&buf, self->patch);
+    if (err < 0)
+        return Error_set(err);
 
-cleanup:
+    py_patch = to_unicode(buf.ptr, NULL, NULL);
     git_buf_free(&buf);
-    return (err < 0) ? Error_set(err) : py_patch;
+    return py_patch;
 }
 
 PyMethodDef Patch_methods[] = {
-    {"create_from", (PyCFunction) Patch_create_from, METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
+    {"create_from", (PyCFunction) Patch_create_from,
+      METH_KEYWORDS | METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
     {NULL}
 };
 
diff --git a/src/patch.h b/src/patch.h
index f6f8ba0..4ab4d1c 100644
--- a/src/patch.h
+++ b/src/patch.h
@@ -32,6 +32,6 @@
 #include <Python.h>
 #include <git2.h>
 
-PyObject* wrap_patch(git_patch *patch);
+PyObject* wrap_patch(git_patch *patch, Blob *oldblob, Blob *newblob);
 
 #endif
diff --git a/src/pygit2.c b/src/pygit2.c
index 50e9773..6ff7c4e 100644
--- a/src/pygit2.c
+++ b/src/pygit2.c
@@ -43,6 +43,7 @@ extern PyTypeObject OidType;
 extern PyTypeObject ObjectType;
 extern PyTypeObject CommitType;
 extern PyTypeObject DiffType;
+extern PyTypeObject DeltasIterType;
 extern PyTypeObject DiffIterType;
 extern PyTypeObject DiffDeltaType;
 extern PyTypeObject DiffFileType;
@@ -335,6 +336,7 @@ moduleinit(PyObject* m)
      * Diff
      */
     INIT_TYPE(DiffType, NULL, NULL)
+    INIT_TYPE(DeltasIterType, NULL, NULL)
     INIT_TYPE(DiffIterType, NULL, NULL)
     INIT_TYPE(DiffDeltaType, NULL, NULL)
     INIT_TYPE(DiffFileType, NULL, NULL)
diff --git a/src/types.h b/src/types.h
index 93f06e0..1dbf6b1 100644
--- a/src/types.h
+++ b/src/types.h
@@ -95,6 +95,8 @@ typedef struct {
     PyObject_HEAD
     git_patch *patch;
     PyObject* hunks;
+    Blob* oldblob;
+    Blob* newblob;
 } Patch;
 
 /* git_diff */
@@ -105,6 +107,13 @@ typedef struct {
     Diff *diff;
     size_t i;
     size_t n;
+} DeltasIter;
+
+typedef struct {
+    PyObject_HEAD
+    Diff *diff;
+    size_t i;
+    size_t n;
 } DiffIter;
 
 typedef struct {
diff --git a/test/test_blob.py b/test/test_blob.py
index c2095fd..a97746e 100644
--- a/test/test_blob.py
+++ b/test/test_blob.py
@@ -57,6 +57,17 @@ index a520c24..95d09f2 100644
 \ No newline at end of file
 """
 
+BLOB_PATCH_2 = """diff --git a/file b/file
+index a520c24..d675fa4 100644
+--- a/file
++++ b/file
+@@ -1,3 +1 @@
+-hello world
+-hola mundo
+-bonjour le monde
++foo bar
+"""
+
 BLOB_PATCH_DELETED = """diff --git a/file b/file
 deleted file mode 100644
 index a520c24..0000000
@@ -170,5 +181,24 @@ class BlobTest(utils.RepoTestCase):
         patch = blob.diff_to_buffer(None)
         self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
 
+    def test_diff_blob_create(self):
+        old = self.repo[self.repo.create_blob(BLOB_CONTENT)]
+        new = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]
+
+        patch = old.diff(new)
+        self.assertEqual(patch.patch, BLOB_PATCH_2)
+
+    def test_blob_from_repo(self):
+        blob = self.repo[BLOB_SHA]
+        patch_one = blob.diff_to_buffer(None)
+
+        blob = self.repo[BLOB_SHA]
+        patch_two = blob.diff_to_buffer(None)
+
+        self.assertEqual(
+            patch_one.patch,
+            patch_two.patch,
+        )
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/test/test_diff.py b/test/test_diff.py
index a59b530..260c93d 100644
--- a/test/test_diff.py
+++ b/test/test_diff.py
@@ -325,6 +325,24 @@ class DiffTest(utils.BareRepoTestCase):
                                  width=80)
         self.assertEqual(STATS_EXPECTED, formatted)
 
+    def test_deltas(self):
+        commit_a = self.repo[COMMIT_SHA1_1]
+        commit_b = self.repo[COMMIT_SHA1_2]
+        diff = commit_a.tree.diff_to_tree(commit_b.tree)
+        deltas = list(diff.deltas)
+        patches = list(diff)
+        self.assertEqual(len(deltas), len(patches))
+        for i, delta in enumerate(deltas):
+            patch_delta = patches[i].delta
+            self.assertEqual(delta.status, patch_delta.status)
+            self.assertEqual(delta.similarity, patch_delta.similarity)
+            self.assertEqual(delta.nfiles, patch_delta.nfiles)
+            self.assertEqual(delta.old_file.id, patch_delta.old_file.id)
+            self.assertEqual(delta.new_file.id, patch_delta.new_file.id)
+
+            # As explained in the libgit2 documentation, flags are not set
+            #self.assertEqual(delta.flags, patch_delta.flags)
+
 
 class BinaryDiffTest(utils.BinaryFileRepoTestCase):
     def test_binary_diff(self):
diff --git a/test/test_patch.py b/test/test_patch.py
index 1855556..6774f37 100644
--- a/test/test_patch.py
+++ b/test/test_patch.py
@@ -31,16 +31,27 @@ from __future__ import unicode_literals
 import pygit2
 from . import utils
 
-BLOB_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
-BLOB_OLD_CONTENT = """hello world
+BLOB_OLD_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
+BLOB_NEW_SHA = '3b18e512dba79e4c8300dd08aeb37f8e728b8dad'
+BLOB_OLD_CONTENT = b"""hello world
 hola mundo
 bonjour le monde
-""".encode()
+"""
 BLOB_NEW_CONTENT = b'foo bar\n'
 
 BLOB_OLD_PATH = 'a/file'
 BLOB_NEW_PATH = 'b/file'
 
+BLOB_PATCH2 = """diff --git a/a/file b/b/file
+index a520c24..3b18e51 100644
+--- a/a/file
++++ b/b/file
+@@ -1,3 +1 @@
+ hello world
+-hola mundo
+-bonjour le monde
+"""
+
 BLOB_PATCH = """diff --git a/a/file b/b/file
 index a520c24..d675fa4 100644
 --- a/a/file
@@ -78,33 +89,33 @@ class PatchTest(utils.RepoTestCase):
     def test_patch_create_from_buffers(self):
         patch = pygit2.Patch.create_from(
             BLOB_OLD_CONTENT,
-            BLOB_OLD_PATH,
             BLOB_NEW_CONTENT,
-            BLOB_NEW_PATH,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
         )
 
         self.assertEqual(patch.patch, BLOB_PATCH)
 
     def test_patch_create_from_blobs(self):
-        old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
-        new_blob = self.repo.create_blob(BLOB_NEW_CONTENT)
+        old_blob = self.repo[BLOB_OLD_SHA]
+        new_blob = self.repo[BLOB_NEW_SHA]
 
         patch = pygit2.Patch.create_from(
-            self.repo[old_blob],
-            BLOB_OLD_PATH,
-            self.repo[new_blob],
-            BLOB_NEW_PATH,
+            old_blob,
+            new_blob,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
         )
 
-        self.assertEqual(patch.patch, BLOB_PATCH)
+        self.assertEqual(patch.patch, BLOB_PATCH2)
 
     def test_patch_create_from_blob_buffer(self):
-        old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
+        old_blob = self.repo[BLOB_OLD_SHA]
         patch = pygit2.Patch.create_from(
-            self.repo[old_blob],
-            BLOB_OLD_PATH,
+            old_blob,
             BLOB_NEW_CONTENT,
-            BLOB_NEW_PATH,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
         )
 
         self.assertEqual(patch.patch, BLOB_PATCH)
@@ -112,21 +123,21 @@ class PatchTest(utils.RepoTestCase):
     def test_patch_create_from_blob_buffer_add(self):
         patch = pygit2.Patch.create_from(
             None,
-            BLOB_OLD_PATH,
             BLOB_NEW_CONTENT,
-            BLOB_NEW_PATH,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
         )
 
         self.assertEqual(patch.patch, BLOB_PATCH_ADDED)
 
     def test_patch_create_from_blob_buffer_delete(self):
-        old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
+        old_blob = self.repo[BLOB_OLD_SHA]
 
         patch = pygit2.Patch.create_from(
-            self.repo[old_blob],
-            BLOB_OLD_PATH,
+            old_blob,
             None,
-            BLOB_NEW_PATH,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
         )
 
         self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
@@ -135,16 +146,128 @@ class PatchTest(utils.RepoTestCase):
         with self.assertRaises(TypeError):
             pygit2.Patch.create_from(
                 self.repo,
-                BLOB_OLD_PATH,
                 BLOB_NEW_CONTENT,
-                BLOB_NEW_PATH,
             )
 
     def test_patch_create_from_bad_new_type_arg(self):
         with self.assertRaises(TypeError):
             pygit2.Patch.create_from(
                 None,
-                BLOB_OLD_PATH,
                 self.repo,
-                BLOB_NEW_PATH,
             )
+
+    def test_context_lines(self):
+        old_blob = self.repo[BLOB_OLD_SHA]
+        new_blob = self.repo[BLOB_NEW_SHA]
+
+        patch = pygit2.Patch.create_from(
+            old_blob,
+            new_blob,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        context_count = (
+            len([line for line in patch.patch.splitlines() if line.startswith(" ")])
+        )
+
+        self.assertNotEqual(context_count, 0)
+
+    def test_no_context_lines(self):
+        old_blob = self.repo[BLOB_OLD_SHA]
+        new_blob = self.repo[BLOB_NEW_SHA]
+
+        patch = pygit2.Patch.create_from(
+            old_blob,
+            new_blob,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+            context_lines=0,
+        )
+
+        context_count = (
+            len([line for line in patch.patch.splitlines() if line.startswith(" ")])
+        )
+
+        self.assertEqual(context_count, 0)
+
+
+    def test_patch_create_blob_blobs(self):
+        old_blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
+        new_blob = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]
+
+        patch = pygit2.Patch.create_from(
+            old_blob,
+            new_blob,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        self.assertEqual(patch.patch, BLOB_PATCH)
+
+    def test_patch_create_blob_buffer(self):
+        blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
+        patch = pygit2.Patch.create_from(
+            blob,
+            BLOB_NEW_CONTENT,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        self.assertEqual(patch.patch, BLOB_PATCH)
+
+    def test_patch_create_blob_delete(self):
+        blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
+        patch = pygit2.Patch.create_from(
+            blob,
+            None,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
+
+    def test_patch_create_blob_add(self):
+        blob = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]
+        patch = pygit2.Patch.create_from(
+            None,
+            blob,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        self.assertEqual(patch.patch, BLOB_PATCH_ADDED)
+
+    def test_patch_delete_blob(self):
+        blob = self.repo[BLOB_OLD_SHA]
+        patch = pygit2.Patch.create_from(
+            blob,
+            None,
+            old_as_path=BLOB_OLD_PATH,
+            new_as_path=BLOB_NEW_PATH,
+        )
+
+        # Make sure that even after deleting the blob the patch still has the
+        # necessary references to generate its patch
+        del blob
+        self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
+
+    def test_patch_multi_blob(self):
+        blob = self.repo[BLOB_OLD_SHA]
+        patch = pygit2.Patch.create_from(
+            blob,
+            None
+        )
+        patch_text = patch.patch
+
+        blob = self.repo[BLOB_OLD_SHA]
+        patch2 = pygit2.Patch.create_from(
+            blob,
+            None
+        )
+        patch_text2 = patch.patch
+
+        self.assertEqual(patch_text, patch_text2)
+        self.assertEqual(patch_text, patch.patch)
+        self.assertEqual(patch_text2, patch2.patch)
+        self.assertEqual(patch.patch, patch2.patch)
diff --git a/test/test_remote.py b/test/test_remote.py
index d048961..7edd09b 100644
--- a/test/test_remote.py
+++ b/test/test_remote.py
@@ -240,6 +240,10 @@ class PruneTestCase(utils.RepoTestCase):
         self.clone_repo = pygit2.Repository(cloned_repo_path)
         self.repo.branches.delete('i18n')
 
+    def tearDown(self):
+        self.clone_repo = None
+        super(PruneTestCase, self).tearDown()
+        
     def test_fetch_remote_default(self):
         self.clone_repo.remotes[0].fetch()
         self.assertIn('origin/i18n', self.clone_repo.branches)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-pygit2.git



More information about the Python-modules-commits mailing list