diff options
Diffstat (limited to 'lib/git')
-rw-r--r-- | lib/git/__init__.py | 2 | ||||
-rw-r--r-- | lib/git/diff.py | 72 | ||||
-rw-r--r-- | lib/git/objects/base.py | 75 | ||||
-rw-r--r-- | lib/git/objects/commit.py | 2 | ||||
-rw-r--r-- | lib/git/objects/tree.py | 3 |
5 files changed, 76 insertions, 78 deletions
diff --git a/lib/git/__init__.py b/lib/git/__init__.py index 6f482128..e2adac62 100644 --- a/lib/git/__init__.py +++ b/lib/git/__init__.py @@ -12,7 +12,7 @@ __version__ = 'git' from git.objects import * from git.refs import * from git.actor import Actor -from git.diff import Diff +from git.diff import * from git.errors import InvalidGitRepositoryError, NoSuchPathError, GitCommandError from git.cmd import Git from git.repo import Repo diff --git a/lib/git/diff.py b/lib/git/diff.py index e16d5b07..760897ec 100644 --- a/lib/git/diff.py +++ b/lib/git/diff.py @@ -7,6 +7,78 @@ import re import objects.blob as blob + +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._index_from_raw_format + if create_patch: + diff_method = Diff._index_from_patch_format + return diff_method(self.repo, proc.stdout) + + class Diff(object): """ A Diff contains diff information between two Trees. diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py index b347b5f1..ab1da7b0 100644 --- a/lib/git/objects/base.py +++ b/lib/git/objects/base.py @@ -170,79 +170,4 @@ class IndexObject(Object): mode += int(char) << iteration*3 # END for each char 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 - """ - # import it in a retared fashion to avoid dependency cycle - from git.diff import Diff - - 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._index_from_raw_format - if create_patch: - diff_method = Diff._index_from_patch_format - return diff_method(self.repo, proc.stdout) - - diff --git a/lib/git/objects/commit.py b/lib/git/objects/commit.py index 521130c5..181cbb52 100644 --- a/lib/git/objects/commit.py +++ b/lib/git/objects/commit.py @@ -11,7 +11,7 @@ from tree import Tree import base import utils -class Commit(base.Object, Iterable, base.Diffable): +class Commit(base.Object, Iterable, diff.Diffable): """ Wraps a git Commit object. diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index 4d3e9ebd..c35c075e 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -8,6 +8,7 @@ import os import blob import base import binascii +import git.diff as diff def sha_to_hex(sha): """Takes a string and returns the hex of the sha within""" @@ -15,7 +16,7 @@ def sha_to_hex(sha): assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha return hexsha -class Tree(base.IndexObject, base.Diffable): +class Tree(base.IndexObject, diff.Diffable): """ Tress represent a ordered list of Blobs and other Trees. Hence it can be accessed like a list. |