Skip to content

Commit 19533ff

Browse files
committed
Fixed Diff class which used Commits instead of Blobs - as Blobs contain the path ( in the 'name' member variable ), the a|b_path members of Diff have been removed. Tests were adjusted and run
Diff docs have been updated to provide a little more information on specifics cases
1 parent 600bd69 commit 19533ff

File tree

3 files changed

+84
-40
lines changed

3 files changed

+84
-40
lines changed

CHANGES

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ Repo
4949
* Corrected ``commits_between`` always returning None instead of the reversed
5050
list.
5151

52+
53+
0.1.X
54+
=====
55+
( Future Release )
56+
General
57+
-------
58+
* See changes in Diff class as your client code needs adjustments to work with it
59+
60+
Diff
61+
----
62+
* Members a a_commit and b_commit renamed to a_blob and b_blob - they are populated
63+
with Blob objects if possible
64+
* Members a_path and b_path removed as this information is kept in the blobs
65+
5266

5367
0.1.5
5468
=====

lib/git/diff.py

+41-14
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,44 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

77
import re
8-
import commit
8+
import blob
99

1010
class Diff(object):
1111
"""
1212
A Diff contains diff information between two commits.
13+
14+
It contains two sides a and b of the diff, members are prefixed with
15+
"a" and "b" respectively to inidcate that.
16+
17+
Diffs keep information about the changed blob objects, the file mode, renames,
18+
deletions and new files.
19+
20+
There are a few cases where None has to be expected as member variable value:
21+
22+
``New File``::
23+
24+
a_mode is None
25+
a_blob is None
26+
27+
``Deleted File``::
28+
29+
b_mode is None
30+
b_blob is NOne
1331
"""
1432

15-
def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode,
33+
def __init__(self, repo, a_path, b_path, a_blob, b_blob, a_mode,
1634
b_mode, new_file, deleted_file, rename_from,
1735
rename_to, diff):
1836
self.repo = repo
19-
self.a_path = a_path
20-
self.b_path = b_path
2137

22-
if not a_commit or re.search(r'^0{40}$', a_commit):
23-
self.a_commit = None
38+
if not a_blob or re.search(r'^0{40}$', a_blob):
39+
self.a_blob = None
2440
else:
25-
self.a_commit = commit.Commit(repo, id=a_commit)
26-
if not b_commit or re.search(r'^0{40}$', b_commit):
27-
self.b_commit = None
41+
self.a_blob = blob.Blob(repo, id=a_blob, mode=a_mode, name=a_path)
42+
if not b_blob or re.search(r'^0{40}$', b_blob):
43+
self.b_blob = None
2844
else:
29-
self.b_commit = commit.Commit(repo, id=b_commit)
45+
self.b_blob = blob.Blob(repo, id=b_blob, mode=b_mode, name=b_path)
3046

3147
self.a_mode = a_mode
3248
self.b_mode = b_mode
@@ -39,6 +55,17 @@ def __init__(self, repo, a_path, b_path, a_commit, b_commit, a_mode,
3955

4056
@classmethod
4157
def list_from_string(cls, repo, text):
58+
"""
59+
Create a new diff object from the given text
60+
``repo``
61+
is the repository we are operating on - it is required
62+
63+
``text``
64+
result of 'git diff' between two commits or one commit and the index
65+
66+
Returns
67+
git.Diff[]
68+
"""
4269
diffs = []
4370

4471
diff_header = re.compile(r"""
@@ -51,19 +78,19 @@ def list_from_string(cls, repo, text):
5178
^new[ ]mode[ ](?P<new_mode>\d+)(?:\n|$))?
5279
(?:^new[ ]file[ ]mode[ ](?P<new_file_mode>.+)(?:\n|$))?
5380
(?:^deleted[ ]file[ ]mode[ ](?P<deleted_file_mode>.+)(?:\n|$))?
54-
(?:^index[ ](?P<a_commit>[0-9A-Fa-f]+)
55-
\.\.(?P<b_commit>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
81+
(?:^index[ ](?P<a_blob>[0-9A-Fa-f]+)
82+
\.\.(?P<b_blob>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
5683
""", re.VERBOSE | re.MULTILINE).match
5784

5885
for diff in ('\n' + text).split('\ndiff --git')[1:]:
5986
header = diff_header(diff)
6087

6188
a_path, b_path, similarity_index, rename_from, rename_to, \
6289
old_mode, new_mode, new_file_mode, deleted_file_mode, \
63-
a_commit, b_commit, b_mode = header.groups()
90+
a_blob, b_blob, b_mode = header.groups()
6491
new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode)
6592

66-
diffs.append(Diff(repo, a_path, b_path, a_commit, b_commit,
93+
diffs.append(Diff(repo, a_path, b_path, a_blob, b_blob,
6794
old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode,
6895
new_file, deleted_file, rename_from, rename_to, diff[header.end():]))
6996

test/git/test_commit.py

+29-26
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ def test_diff(self, git):
3737

3838
assert_equal(15, len(diffs))
3939

40-
assert_equal('.gitignore', diffs[0].a_path)
41-
assert_equal('.gitignore', diffs[0].b_path)
42-
assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs[0].a_commit.id)
43-
assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diffs[0].b_commit.id)
44-
assert_equal('100644', diffs[0].b_mode)
40+
assert_equal('.gitignore', diffs[0].a_blob.name)
41+
assert_equal('.gitignore', diffs[0].b_blob.name)
42+
assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs[0].a_blob.id)
43+
assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diffs[0].b_blob.id)
44+
assert_equal('100644', diffs[0].b_blob.mode)
4545
assert_equal(False, diffs[0].new_file)
4646
assert_equal(False, diffs[0].deleted_file)
4747
assert_equal("--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs[0].diff)
4848

49-
assert_equal('lib/grit/actor.rb', diffs[5].a_path)
50-
assert_equal(None, diffs[5].a_commit)
51-
assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_commit.id)
49+
assert_equal('lib/grit/actor.rb', diffs[5].b_blob.name)
50+
assert_equal(None, diffs[5].a_blob)
51+
assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_blob.id)
52+
assert_equal( None, diffs[5].a_mode )
5253
assert_equal(True, diffs[5].new_file)
5354

5455
assert_true(git.called)
@@ -88,7 +89,7 @@ def test_diff_with_files(self, git):
8889
diffs = Commit.diff(self.repo, '59ddc32', ['lib'])
8990

9091
assert_equal(1, len(diffs))
91-
assert_equal('lib/grit/diff.rb', diffs[0].a_path)
92+
assert_equal('lib/grit/diff.rb', diffs[0].a_blob.name)
9293

9394
assert_true(git.called)
9495
assert_equal(git.call_args, (('diff', '-M', '59ddc32', '--', 'lib'), {'full_index': True}))
@@ -100,7 +101,7 @@ def test_diff_with_two_commits_and_files(self, git):
100101
diffs = Commit.diff(self.repo, '59ddc32', '13d27d5', ['lib'])
101102

102103
assert_equal(1, len(diffs))
103-
assert_equal('lib/grit/commit.rb', diffs[0].a_path)
104+
assert_equal('lib/grit/commit.rb', diffs[0].a_blob.name)
104105

105106
assert_true(git.called)
106107
assert_equal(git.call_args, (('diff', '-M', '59ddc32', '13d27d5', '--', 'lib'), {'full_index': True}))
@@ -114,18 +115,18 @@ def test_diffs(self, git):
114115

115116
assert_equal(15, len(diffs))
116117

117-
assert_equal('.gitignore', diffs[0].a_path)
118-
assert_equal('.gitignore', diffs[0].b_path)
119-
assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs[0].a_commit.id)
120-
assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diffs[0].b_commit.id)
121-
assert_equal('100644', diffs[0].b_mode)
118+
assert_equal('.gitignore', diffs[0].a_blob.name)
119+
assert_equal('.gitignore', diffs[0].b_blob.name)
120+
assert_equal('4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs[0].a_blob.id)
121+
assert_equal('2dd02534615434d88c51307beb0f0092f21fd103', diffs[0].b_blob.id)
122+
assert_equal('100644', diffs[0].b_blob.mode)
122123
assert_equal(False, diffs[0].new_file)
123124
assert_equal(False, diffs[0].deleted_file)
124125
assert_equal("--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs[0].diff)
125126

126-
assert_equal('lib/grit/actor.rb', diffs[5].a_path)
127-
assert_equal(None, diffs[5].a_commit)
128-
assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_commit.id)
127+
assert_equal('lib/grit/actor.rb', diffs[5].b_blob.name)
128+
assert_equal(None, diffs[5].a_blob)
129+
assert_equal('f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_blob.id)
129130
assert_equal(True, diffs[5].new_file)
130131

131132
assert_true(git.called)
@@ -144,18 +145,17 @@ def test_diffs_on_initial_import(self, git):
144145

145146
assert_equal(10, len(diffs))
146147

147-
assert_equal('History.txt', diffs[0].a_path)
148-
assert_equal('History.txt', diffs[0].b_path)
149-
assert_equal(None, diffs[0].a_commit)
150-
assert_equal('100644', diffs[0].b_mode)
151-
assert_equal('81d2c27608b352814cbe979a6acd678d30219678', diffs[0].b_commit.id)
148+
assert_equal('History.txt', diffs[0].b_blob.name)
149+
assert_equal(None, diffs[0].a_blob)
150+
assert_equal('100644', diffs[0].b_blob.mode)
151+
assert_equal('81d2c27608b352814cbe979a6acd678d30219678', diffs[0].b_blob.id)
152152
assert_equal(True, diffs[0].new_file)
153153
assert_equal(False, diffs[0].deleted_file)
154154
assert_equal("--- /dev/null\n+++ b/History.txt\n@@ -0,0 +1,5 @@\n+== 1.0.0 / 2007-10-09\n+\n+* 1 major enhancement\n+ * Birthday!\n+", diffs[0].diff)
155155

156-
assert_equal('lib/grit.rb', diffs[5].a_path)
157-
assert_equal(None, diffs[5].a_commit)
158-
assert_equal('32cec87d1e78946a827ddf6a8776be4d81dcf1d1', diffs[5].b_commit.id)
156+
assert_equal('lib/grit.rb', diffs[5].b_blob.name)
157+
assert_equal(None, diffs[5].a_blob)
158+
assert_equal('32cec87d1e78946a827ddf6a8776be4d81dcf1d1', diffs[5].b_blob.id)
159159
assert_equal(True, diffs[5].new_file)
160160

161161
assert_true(git.called)
@@ -181,7 +181,10 @@ def test_diffs_with_mode_only_change(self, git):
181181
commit.__bake_it__()
182182
diffs = commit.diffs
183183

184+
# in case of mode-only changes, there is no blob
184185
assert_equal(23, len(diffs))
186+
assert_equal(None, diffs[0].a_blob)
187+
assert_equal(None, diffs[0].b_blob)
185188
assert_equal('100644', diffs[0].a_mode)
186189
assert_equal('100755', diffs[0].b_mode)
187190

0 commit comments

Comments
 (0)