summaryrefslogtreecommitdiff
path: root/lib/git/diff.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-11 22:50:44 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-11 22:50:44 +0200
commit3c0a65226f038c58fc6d6ed525f38fc00b3579b7 (patch)
treea5b715a490d9cbd8f45eabc1968374c96bdea1c0 /lib/git/diff.py
parent9c0c2fc4ee2d8a5d0a2de50ba882657989dedc51 (diff)
parentc68459a17ff59043d29c90020fffe651b2164e6a (diff)
downloadgitpython-3c0a65226f038c58fc6d6ed525f38fc00b3579b7.tar.gz
Merge branch 'hierarchyfix' into improvements
* hierarchyfix: Added remaining tests for new base classes and removed some methods whose existance was doubtful or unsafe Fixed remaining tests to deal with the changes commit: fixed failing commit tests as the mocked git command would always return the same thing which does not work anymore - re-implemented it in a more dynamic manner, but in the end tests will have to be revised anyway mode-only change for test system - this should be in a separate repository in fact so that changes are a little more self-contained and not depending on the actual source repository fixed issue in Ref.name implementation which would not handle components properly lazymixin system now supports per-attribute baking, it is up to the class whether it bakes more. This also leads to more efficient use of memory as values are only cached and set when required - the baking system does not require an own tracking variable anymore, and values are only to be cached once - then python will natively find the cache without involving any additional overhead. This works by using __getattr__ instead of __get_attribute__ which would always be called put Tree and Blob onto a new base class suitable to deal with IndexObjects blob tests fixed to deal with changes to the Blob type converted all spaces to tabs ( 4 spaces = 1 tab ) just to allow me and my editor to work with the files properly. Can convert it back for releaes Re-designed the tag testing - it does not use fixtures anymore but dyamically checks the existance of tags within the repository - it basically tests the interface and checks that expected return types are actually returned Intermediate commit: commit,tree and blob objects now derive from object - test is in place which still fails on purpose. Need to integrate tags which can be objects or just a special form of a ref Renamed lazy.py to base.py to have a file for base classes - lazy not yet changed to allow proper rename tracking
Diffstat (limited to 'lib/git/diff.py')
-rw-r--r--lib/git/diff.py146
1 files changed, 75 insertions, 71 deletions
diff --git a/lib/git/diff.py b/lib/git/diff.py
index 75450d70..943fb08a 100644
--- a/lib/git/diff.py
+++ b/lib/git/diff.py
@@ -8,30 +8,30 @@ import re
import blob
class Diff(object):
- """
- A Diff contains diff information between two commits.
-
- It contains two sides a and b of the diff, members are prefixed with
- "a" and "b" respectively to inidcate that.
-
- Diffs keep information about the changed blob objects, the file mode, renames,
- deletions and new files.
-
- There are a few cases where None has to be expected as member variable value:
-
- ``New File``::
-
- a_mode is None
- a_blob is None
-
- ``Deleted File``::
-
- b_mode is None
- b_blob is NOne
- """
-
- # precompiled regex
- re_header = re.compile(r"""
+ """
+ A Diff contains diff information between two commits.
+
+ It contains two sides a and b of the diff, members are prefixed with
+ "a" and "b" respectively to inidcate that.
+
+ Diffs keep information about the changed blob objects, the file mode, renames,
+ deletions and new files.
+
+ There are a few cases where None has to be expected as member variable value:
+
+ ``New File``::
+
+ a_mode is None
+ a_blob is None
+
+ ``Deleted File``::
+
+ b_mode is None
+ b_blob is NOne
+ """
+
+ # precompiled regex
+ re_header = re.compile(r"""
#^diff[ ]--git
[ ]a/(?P<a_path>\S+)[ ]b/(?P<b_path>\S+)\n
(?:^similarity[ ]index[ ](?P<similarity_index>\d+)%\n
@@ -44,58 +44,62 @@ class Diff(object):
(?:^index[ ](?P<a_blob_id>[0-9A-Fa-f]+)
\.\.(?P<b_blob_id>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
""", re.VERBOSE | re.MULTILINE)
- re_is_null_hexsha = re.compile( r'^0{40}$' )
+ re_is_null_hexsha = re.compile( r'^0{40}$' )
+ __slots__ = ("a_blob", "b_blob", "a_mode", "b_mode", "new_file", "deleted_file",
+ "rename_from", "rename_to", "renamed", "diff")
- def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
- b_mode, new_file, deleted_file, rename_from,
- rename_to, diff):
- self.repo = repo
+ def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
+ b_mode, new_file, deleted_file, rename_from,
+ rename_to, diff):
+ if not a_blob_id or self.re_is_null_hexsha.search(a_blob_id):
+ self.a_blob = None
+ else:
+ self.a_blob = blob.Blob(repo, id=a_blob_id, mode=a_mode, path=a_path)
+ if not b_blob_id or self.re_is_null_hexsha.search(b_blob_id):
+ self.b_blob = None
+ else:
+ self.b_blob = blob.Blob(repo, id=b_blob_id, mode=b_mode, path=b_path)
- if not a_blob_id or self.re_is_null_hexsha.search(a_blob_id):
- self.a_blob = None
- else:
- self.a_blob = blob.Blob(repo, id=a_blob_id, mode=a_mode, path=a_path)
- if not b_blob_id or self.re_is_null_hexsha.search(b_blob_id):
- self.b_blob = None
- else:
- self.b_blob = blob.Blob(repo, id=b_blob_id, mode=b_mode, path=b_path)
+ self.a_mode = a_mode
+ self.b_mode = b_mode
+ if self.a_mode:
+ self.a_mode = blob.Blob._mode_str_to_int( self.a_mode )
+ if self.b_mode:
+ self.b_mode = blob.Blob._mode_str_to_int( self.b_mode )
+ self.new_file = new_file
+ self.deleted_file = deleted_file
+ self.rename_from = rename_from
+ self.rename_to = rename_to
+ self.renamed = rename_from != rename_to
+ self.diff = diff
- self.a_mode = a_mode
- self.b_mode = b_mode
- self.new_file = new_file
- self.deleted_file = deleted_file
- self.rename_from = rename_from
- self.rename_to = rename_to
- self.renamed = rename_from != rename_to
- self.diff = diff
+ @classmethod
+ def list_from_string(cls, repo, text):
+ """
+ Create a new diff object from the given text
+ ``repo``
+ is the repository we are operating on - it is required
+
+ ``text``
+ result of 'git diff' between two commits or one commit and the index
+
+ Returns
+ git.Diff[]
+ """
+ diffs = []
- @classmethod
- def list_from_string(cls, repo, text):
- """
- Create a new diff object from the given text
- ``repo``
- is the repository we are operating on - it is required
-
- ``text``
- result of 'git diff' between two commits or one commit and the index
-
- Returns
- git.Diff[]
- """
- diffs = []
+ diff_header = cls.re_header.match
+ for diff in ('\n' + text).split('\ndiff --git')[1:]:
+ header = diff_header(diff)
- diff_header = cls.re_header.match
- for diff in ('\n' + text).split('\ndiff --git')[1:]:
- header = diff_header(diff)
+ a_path, b_path, similarity_index, rename_from, rename_to, \
+ old_mode, new_mode, new_file_mode, deleted_file_mode, \
+ a_blob_id, b_blob_id, b_mode = header.groups()
+ new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode)
- a_path, b_path, similarity_index, rename_from, rename_to, \
- old_mode, new_mode, new_file_mode, deleted_file_mode, \
- a_blob_id, b_blob_id, b_mode = header.groups()
- new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode)
+ diffs.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id,
+ old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode,
+ new_file, deleted_file, rename_from, rename_to, diff[header.end():]))
- diffs.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id,
- old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode,
- new_file, deleted_file, rename_from, rename_to, diff[header.end():]))
-
- return diffs
+ return diffs