Exemplo n.º 1
0
Arquivo: git.py Projeto: tf198/pycart
def unified_diff(repo, old, new):
    import StringIO
    s = StringIO.StringIO()

    write_object_diff(s, repo, old, new)

    return s.getvalue()
Exemplo n.º 2
0
    def get_file_diff(self, path):
        cache_key = '%s:file_diff:%s:%s' % (self.repo.repo.pk, self.id, path)

        file_diff = cache.get(cache_key)

        if not file_diff:
            files = self.get_tree_changes(True)
            diff = cStringIO.StringIO()
            try:
                write_object_diff(diff, self.repo.git_repo,
                    files[path].old, files[path].new)
                diff = diff.getvalue()
            except KeyError:
                diff = u''

            try:
                diff = diff.decode('utf-8')
            except UnicodeDecodeError:
                pass

            file_diff = {
                'file': path,
                'diff': diff,
                'line_numbers': self._get_diff_line_numbers(diff)
            }
            cache.set(cache_key, file_diff, 2592000)

        return file_diff
Exemplo n.º 3
0
 def test_object_diff_bin_blob_force(self):
     f = BytesIO()
     # Prepare two slightly different PNG headers
     b1 = Blob.from_string(
         b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
         b"\x00\x00\x00\x0d\x49\x48\x44\x52"
         b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
         b"\x08\x04\x00\x00\x00\x05\x04\x8b")
     b2 = Blob.from_string(
         b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
         b"\x00\x00\x00\x0d\x49\x48\x44\x52"
         b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
         b"\x08\x03\x00\x00\x00\x98\xd3\xb3")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(
         f, store, (b'foo.png', 0o644, b1.id),
         (b'bar.png', 0o644, b2.id), diff_binary=True)
     self.assertEqual([
         b'diff --git a/foo.png b/bar.png',
         b'index f73e47d..06364b7 644',
         b'--- a/foo.png',
         b'+++ b/bar.png',
         b'@@ -1,4 +1,4 @@',
         b' \x89PNG',
         b' \x1a',
         b' \x00\x00\x00',
         b'-IHDR\x00\x00\x01\xd5\x00\x00\x00'
         b'\x9f\x08\x04\x00\x00\x00\x05\x04\x8b',
         b'\\ No newline at end of file',
         b'+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f'
         b'\x08\x03\x00\x00\x00\x98\xd3\xb3',
         b'\\ No newline at end of file'
         ], f.getvalue().splitlines())
Exemplo n.º 4
0
 def test_object_diff_bin_blob_force(self):
     f = StringIO()
     # Prepare two slightly different PNG headers
     b1 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
     b2 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, ('foo.png', 0644, b1.id),
                                 ('bar.png', 0644, b2.id), diff_binary=True)
     self.assertEqual([
         'diff --git a/foo.png b/bar.png',
         'index f73e47d..06364b7 644',
         '--- a/foo.png',
         '+++ b/bar.png',
         '@@ -1,4 +1,4 @@',
         ' \x89PNG',
         ' \x1a',
         ' \x00\x00\x00',
         '-IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b',
         '\\ No newline at end of file',
         '+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3',
         '\\ No newline at end of file'
         ], f.getvalue().splitlines())
 def test_object_diff_kind_change(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(
         f,
         store,
         (b"bar.txt", 0o644, b1.id),
         (
             b"bar.txt",
             0o160000,
             b"06d0bdd9e2e20377b3180e4986b14c8549b393e4",
         ),
     )
     self.assertEqual(
         [
             b"diff --git a/bar.txt b/bar.txt",
             b"old file mode 644",
             b"new file mode 160000",
             b"index a116b51..06d0bdd 160000",
             b"--- a/bar.txt",
             b"+++ b/bar.txt",
             b"@@ -1,2 +1 @@",
             b"-new",
             b"-same",
             b"+Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4",
         ],
         f.getvalue().splitlines(),
     )
Exemplo n.º 6
0
 def test_object_diff_bin_blob_force(self):
     f = BytesIO()
     # Prepare two slightly different PNG headers
     b1 = Blob.from_string(
         b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b"
     )
     b2 = Blob.from_string(
         b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3"
     )
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, (b"foo.png", 0o644, b1.id), (b"bar.png", 0o644, b2.id), diff_binary=True)
     self.assertEqual(
         [
             b"diff --git a/foo.png b/bar.png",
             b"index f73e47d..06364b7 644",
             b"--- a/foo.png",
             b"+++ b/bar.png",
             b"@@ -1,4 +1,4 @@",
             b" \x89PNG",
             b" \x1a",
             b" \x00\x00\x00",
             b"-IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b",
             b"\\ No newline at end of file",
             b"+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3",
             b"\\ No newline at end of file",
         ],
         f.getvalue().splitlines(),
     )
Exemplo n.º 7
0
 def test_object_diff_add_blob(self):
     f = BytesIO()
     store = MemoryObjectStore()
     b2 = Blob.from_string(b"new\nsame\n")
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                       (b"bar.txt", 0o644, b2.id))
     self.assertEqual([
         b'diff --git /dev/null b/bar.txt', b'new mode 644',
         b'index 0000000..a116b51 644', b'--- /dev/null', b'+++ b/bar.txt',
         b'@@ -1,0 +1,2 @@', b'+new', b'+same'
     ],
                      f.getvalue().splitlines())
Exemplo n.º 8
0
 def test_object_diff_remove_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, (b"bar.txt", 0o644, b1.id),
                       (None, None, None))
     self.assertEqual([
         b'diff --git a/bar.txt /dev/null', b'deleted mode 644',
         b'index a116b51..0000000', b'--- a/bar.txt', b'+++ /dev/null',
         b'@@ -1,2 +1,0 @@', b'-new', b'-same'
     ],
                      f.getvalue().splitlines())
Exemplo n.º 9
0
 def test_object_diff_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"old\nsame\n")
     b2 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, (b"foo.txt", 0o644, b1.id),
                       (b"bar.txt", 0o644, b2.id))
     self.assertEqual([
         b"diff --git a/foo.txt b/bar.txt", b"index 3b0f961..a116b51 644",
         b"--- a/foo.txt", b"+++ b/bar.txt", b"@@ -1,2 +1,2 @@", b"-old",
         b"+new", b" same"
     ],
                      f.getvalue().splitlines())
Exemplo n.º 10
0
 def test_object_diff_remove_bin_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(
         '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
         '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b')
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, ('foo.png', 0o644, b1.id),
                                 (None, None, None))
     self.assertEqual([
         'diff --git a/foo.png /dev/null',
         'deleted mode 644',
         'index f73e47d..0000000',
         'Binary files a/foo.png and /dev/null differ'
         ], f.getvalue().splitlines())
Exemplo n.º 11
0
 def test_object_diff_add_bin_blob(self):
     f = BytesIO()
     b2 = Blob.from_string(
         '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
         '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3')
     store = MemoryObjectStore()
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                                 ('bar.png', 0o644, b2.id))
     self.assertEqual([
         'diff --git /dev/null b/bar.png',
         'new mode 644',
         'index 0000000..06364b7 644',
         'Binary files /dev/null and b/bar.png differ'
         ], f.getvalue().splitlines())
Exemplo n.º 12
0
 def test_object_diff_remove_bin_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(
         '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
         '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b')
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, ('foo.png', 0o644, b1.id),
                       (None, None, None))
     self.assertEqual([
         'diff --git a/foo.png /dev/null', 'deleted mode 644',
         'index f73e47d..0000000',
         'Binary files a/foo.png and /dev/null differ'
     ],
                      f.getvalue().splitlines())
Exemplo n.º 13
0
 def test_object_diff_add_bin_blob(self):
     f = BytesIO()
     b2 = Blob.from_string(
         '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
         '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3')
     store = MemoryObjectStore()
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                       ('bar.png', 0o644, b2.id))
     self.assertEqual([
         'diff --git /dev/null b/bar.png', 'new mode 644',
         'index 0000000..06364b7 644',
         'Binary files /dev/null and b/bar.png differ'
     ],
                      f.getvalue().splitlines())
Exemplo n.º 14
0
 def test_object_diff_add_blob(self):
     f = BytesIO()
     store = MemoryObjectStore()
     b2 = Blob.from_string(b"new\nsame\n")
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                                 ("bar.txt", 0o644, b2.id))
     self.assertEqual([
         b'diff --git /dev/null b/bar.txt',
         b'new mode 644',
         b'index 0000000..a116b51 644',
         b'--- /dev/null',
         b'+++ b/bar.txt',
         b'@@ -1,0 +1,2 @@',
         b'+new',
         b'+same'
         ], f.getvalue().splitlines())
Exemplo n.º 15
0
 def test_object_diff_remove_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, ("bar.txt", 0o644, b1.id),
                                 (None, None, None))
     self.assertEqual([
         b'diff --git a/bar.txt /dev/null',
         b'deleted mode 644',
         b'index a116b51..0000000',
         b'--- a/bar.txt',
         b'+++ /dev/null',
         b'@@ -1,2 +1,0 @@',
         b'-new',
         b'-same'
         ], f.getvalue().splitlines())
Exemplo n.º 16
0
 def test_object_diff_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"old\nsame\n")
     b2 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, ("foo.txt", 0o644, b1.id),
                                 ("bar.txt", 0o644, b2.id))
     self.assertEqual([
         b"diff --git a/foo.txt b/bar.txt",
         b"index 3b0f961..a116b51 644",
         b"--- a/foo.txt",
         b"+++ b/bar.txt",
         b"@@ -1,2 +1,2 @@",
         b"-old",
         b"+new",
         b" same"
         ], f.getvalue().splitlines())
Exemplo n.º 17
0
 def test_object_diff_bin_blob(self):
     f = BytesIO()
     # Prepare two slightly different PNG headers
     b1 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
     b2 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, ('foo.png', 0o644, b1.id),
                                 ('bar.png', 0o644, b2.id))
     self.assertEqual([
         'diff --git a/foo.png b/bar.png',
         'index f73e47d..06364b7 644',
         'Binary files a/foo.png and b/bar.png differ'
         ], f.getvalue().splitlines())
Exemplo n.º 18
0
 def test_object_diff_bin_blob(self):
     f = BytesIO()
     # Prepare two slightly different PNG headers
     b1 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
     b2 = Blob.from_string(
         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
         "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
     store = MemoryObjectStore()
     store.add_objects([(b1, None), (b2, None)])
     write_object_diff(f, store, ('foo.png', 0o644, b1.id),
                       ('bar.png', 0o644, b2.id))
     self.assertEqual([
         'diff --git a/foo.png b/bar.png', 'index f73e47d..06364b7 644',
         'Binary files a/foo.png and b/bar.png differ'
     ],
                      f.getvalue().splitlines())
 def test_object_diff_add_bin_blob(self):
     f = BytesIO()
     b2 = Blob.from_string(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
                           b"\x00\x00\x00\x0d\x49\x48\x44\x52"
                           b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
                           b"\x08\x03\x00\x00\x00\x98\xd3\xb3")
     store = MemoryObjectStore()
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                       (b"bar.png", 0o644, b2.id))
     self.assertEqual(
         [
             b"diff --git a/bar.png b/bar.png",
             b"new file mode 644",
             b"index 0000000..06364b7",
             b"Binary files /dev/null and b/bar.png differ",
         ],
         f.getvalue().splitlines(),
     )
 def test_object_diff_remove_bin_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
                           b"\x00\x00\x00\x0d\x49\x48\x44\x52"
                           b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
                           b"\x08\x04\x00\x00\x00\x05\x04\x8b")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, (b"foo.png", 0o644, b1.id),
                       (None, None, None))
     self.assertEqual(
         [
             b"diff --git a/foo.png b/foo.png",
             b"deleted file mode 644",
             b"index f73e47d..0000000",
             b"Binary files a/foo.png and /dev/null differ",
         ],
         f.getvalue().splitlines(),
     )
Exemplo n.º 21
0
 def test_object_diff_kind_change(self):
     f = BytesIO()
     b1 = Blob.from_string("new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, ("bar.txt", 0o644, b1.id),
         ("bar.txt", 0o160000, "06d0bdd9e2e20377b3180e4986b14c8549b393e4"))
     self.assertEqual([
         'diff --git a/bar.txt b/bar.txt',
         'old mode 644',
         'new mode 160000',
         'index a116b51..06d0bdd 160000',
         '--- a/bar.txt',
         '+++ b/bar.txt',
         '@@ -1,2 +1,1 @@',
         '-new',
         '-same',
         '+Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
         ], f.getvalue().splitlines())
Exemplo n.º 22
0
 def test_object_diff_remove_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, (b"bar.txt", 0o644, b1.id), (None, None, None))
     self.assertEqual(
         [
             b"diff --git a/bar.txt /dev/null",
             b"deleted mode 644",
             b"index a116b51..0000000",
             b"--- a/bar.txt",
             b"+++ /dev/null",
             b"@@ -1,2 +1,0 @@",
             b"-new",
             b"-same",
         ],
         f.getvalue().splitlines(),
     )
Exemplo n.º 23
0
 def test_object_diff_kind_change(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, ("bar.txt", 0o644, b1.id),
         ("bar.txt", 0o160000, "06d0bdd9e2e20377b3180e4986b14c8549b393e4"))
     self.assertEqual([
         b'diff --git a/bar.txt b/bar.txt',
         b'old mode 644',
         b'new mode 160000',
         b'index a116b51..06d0bdd 160000',
         b'--- a/bar.txt',
         b'+++ b/bar.txt',
         b'@@ -1,2 +1,1 @@',
         b'-new',
         b'-same',
         b'+Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
         ], f.getvalue().splitlines())
Exemplo n.º 24
0
 def test_object_diff_add_blob(self):
     f = BytesIO()
     store = MemoryObjectStore()
     b2 = Blob.from_string(b"new\nsame\n")
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None), (b"bar.txt", 0o644, b2.id))
     self.assertEqual(
         [
             b"diff --git /dev/null b/bar.txt",
             b"new mode 644",
             b"index 0000000..a116b51 644",
             b"--- /dev/null",
             b"+++ b/bar.txt",
             b"@@ -1,0 +1,2 @@",
             b"+new",
             b"+same",
         ],
         f.getvalue().splitlines(),
     )
 def test_object_diff_add_blob(self):
     f = BytesIO()
     store = MemoryObjectStore()
     b2 = Blob.from_string(b"new\nsame\n")
     store.add_object(b2)
     write_object_diff(f, store, (None, None, None),
                       (b"bar.txt", 0o644, b2.id))
     self.assertEqual(
         [
             b"diff --git a/bar.txt b/bar.txt",
             b"new file mode 644",
             b"index 0000000..a116b51",
             b"--- /dev/null",
             b"+++ b/bar.txt",
             b"@@ -0,0 +1,2 @@",
             b"+new",
             b"+same",
         ],
         f.getvalue().splitlines(),
     )
 def test_object_diff_remove_blob(self):
     f = BytesIO()
     b1 = Blob.from_string(b"new\nsame\n")
     store = MemoryObjectStore()
     store.add_object(b1)
     write_object_diff(f, store, (b"bar.txt", 0o644, b1.id),
                       (None, None, None))
     self.assertEqual(
         [
             b"diff --git a/bar.txt b/bar.txt",
             b"deleted file mode 644",
             b"index a116b51..0000000",
             b"--- a/bar.txt",
             b"+++ /dev/null",
             b"@@ -1,2 +0,0 @@",
             b"-new",
             b"-same",
         ],
         f.getvalue().splitlines(),
     )
Exemplo n.º 27
0
    def changed_files(self):
        cache_key = '%s:changed_files:%s' % (self.repo.repo.pk, self.id)

        files = cache.get(cache_key)

        if not files:
            files = []
            for change in self.get_tree_changes():
                diff = cStringIO.StringIO()
                write_object_diff(diff, self.repo.git_repo,
                    change.old, change.new)
                diff = diff.getvalue()
                lines_added, lines_removed = self._get_diff_line_numbers(
                    diff, count=True)
                files.append({
                    'file': change.new.path or change.old.path,
                    'lines_added': lines_added,
                    'lines_removed': lines_removed,
                    'change_type': change.type,
                })
            cache.set(cache_key, files, 2592000)

        return files
Exemplo n.º 28
0
Arquivo: repo.py Projeto: tek/proteome
def file_diff(f, store, old_tree, new_tree, path):
    changes = store.tree_changes(old_tree, new_tree)
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
        if newpath.decode() == path:
            write_object_diff(f, store, (oldpath, oldmode, oldsha),
                                        (newpath, newmode, newsha))
Exemplo n.º 29
0
def object_diff(*args, **kwargs):
    """A more convenient wrapper around Dulwich's patching
    """
    fd = StringIO()
    patch.write_object_diff(fd, *args, **kwargs)
    return fd.getvalue()
Exemplo n.º 30
0
def object_diff(*args, **kwargs):
    """A more convenient wrapper around Dulwich's patching
    """
    fd = StringIO()
    patch.write_object_diff(fd, *args, **kwargs)
    return fd.getvalue()