summaryrefslogtreecommitdiff
path: root/lib/git/objects/base.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-16 19:19:57 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-16 19:19:57 +0200
commitb372e26366348920eae32ee81a47b469b511a21f (patch)
tree9cee746944005c9c5d7adf284785a2fdb62dc2e4 /lib/git/objects/base.py
parentbb24f67e64b4ebe11c4d3ce7df021a6ad7ca98f2 (diff)
downloadgitpython-b372e26366348920eae32ee81a47b469b511a21f.tar.gz
added Diffable interface to objects.base, its used by Commit and Tree objects.
Diff class has been prepared to process raw input, but its not yet more than a frame
Diffstat (limited to 'lib/git/objects/base.py')
-rw-r--r--lib/git/objects/base.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py
index d780c7b3..1bb2e8f1 100644
--- a/lib/git/objects/base.py
+++ b/lib/git/objects/base.py
@@ -172,3 +172,74 @@ class IndexObject(Object):
return mode
+class Diffable(object):
+ """
+ Common interface for all object that can be diffed against another object of compatible type.
+
+ NOTE:
+ Subclasses require a repo member as it is the case for Object instances, for practical
+ reasons we do not derive from Object.
+ """
+ __slots__ = tuple()
+
+ # subclasses provide additional arguments to the git-diff comamnd by supplynig
+ # them in this tuple
+ _diff_args = tuple()
+
+ def diff(self, other=None, paths=None, create_patch=False, **kwargs):
+ """
+ Creates diffs between two items being trees, trees and index or an
+ index and the working tree.
+
+ ``other``
+ Is the item to compare us with.
+ If None, we will be compared to the working tree.
+
+ ``paths``
+ is a list of paths or a single path to limit the diff to.
+ It will only include at least one of the givne path or paths.
+
+ ``create_patch``
+ If True, the returned Diff contains a detailed patch that if applied
+ makes the self to other. Patches are somwhat costly as blobs have to be read
+ and diffed.
+
+ ``kwargs``
+ Additional arguments passed to git-diff, such as
+ R=True to swap both sides of the diff.
+
+ Returns
+ git.DiffIndex
+
+ Note
+ Rename detection will only work if create_patch is True
+ """
+ args = list(self._diff_args[:])
+ args.append( "--abbrev=40" ) # we need full shas
+ args.append( "--full-index" ) # get full index paths, not only filenames
+
+ if create_patch:
+ args.append("-p")
+ args.append("-M") # check for renames
+ else:
+ args.append("--raw")
+
+ paths = paths or []
+ if paths:
+ paths.insert(0, "--")
+
+ if other is not None:
+ args.insert(0, other)
+
+ args.insert(0,self)
+ args.extend(paths)
+
+ kwargs['as_process'] = True
+ proc = self.repo.git.diff(*args, **kwargs)
+
+ diff_method = diff.Diff._index_from_raw_format
+ if create_patch:
+ diff_method = diff.Diff._index_from_patch_format(self.repo, proc.stdout)
+ return diff_method(self.repo, proc.stdout)
+
+