Skip to content

Ignore all lines of subsequent hunks until last one is found #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,14 @@ def blame_incremental(self, rev, file, **kwargs):
committed_date=int(props[b'committer-time']))
commits[hexsha] = c
else:
# Discard the next line (it's a filename end tag)
line = next(stream)
tag, value = line.split(b' ', 1)
assert tag == b'filename', 'Unexpected git blame output'
orig_filename = value
# Discard all lines until we find "filename" which is
# guaranteed to be the last line
while True:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this loops break-condition is not clearly defined just now.
Do you think 'end-of-stream' could be another one?

What do you think of something like this:

for line in stream:
	tag, value = line.split(b' ', 1)
	if tag == b'filename':
		orig_filename = value
		break
		
# Finally handle the case of the stream being exhausted too early

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment in 77b20be to be more explicit about an unexpected EOF condition. With a for loop we'd need to raise that exception ourselves in an else block so I think the while loop works better here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see now! I didn't know that next throws - iterators do that, right. Have been doing Rust for too long ;).

line = next(stream) # will fail if we reach the EOF unexpectedly
tag, value = line.split(b' ', 1)
if tag == b'filename':
orig_filename = value
break

yield BlameEntry(commits[hexsha],
range(lineno, lineno + num_lines),
Expand Down
33 changes: 33 additions & 0 deletions git/test/fixtures/blame_incremental_2.11.1_plus
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
82b8902e033430000481eb355733cd7065342037 2 2 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! I am ready to merge!
One last question though: should there or should there not be empty lines in the fixture?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there should be empty lines in the fixture.

If it helps I generated the fixture with git blame --incremental -p 82b8902e033430000481eb355733cd7065342037 -- AUTHORS > git/test/fixtures/blame_incremental_2.11.1_plus (which I believe matches how blame_incremental was generated).

author Sebastian Thiel
author-mail <[email protected]>
author-time 1270634931
author-tz +0200
committer Sebastian Thiel
committer-mail <[email protected]>
committer-time 1270634931
committer-tz +0200
summary Used this release for a first beta of the 0.2 branch of development
previous 501bf602abea7d21c3dbb409b435976e92033145 AUTHORS
filename AUTHORS
82b8902e033430000481eb355733cd7065342037 14 14 1
previous 501bf602abea7d21c3dbb409b435976e92033145 AUTHORS
filename AUTHORS
c76852d0bff115720af3f27acdb084c59361e5f6 1 1 1
author Michael Trier
author-mail <[email protected]>
author-time 1232829627
author-tz -0500
committer Michael Trier
committer-mail <[email protected]>
committer-time 1232829627
committer-tz -0500
summary Lots of spring cleaning and added in Sphinx documentation.
previous bcd57e349c08bd7f076f8d6d2f39b702015358c1 AUTHORS
filename AUTHORS
c76852d0bff115720af3f27acdb084c59361e5f6 2 3 11
previous bcd57e349c08bd7f076f8d6d2f39b702015358c1 AUTHORS
filename AUTHORS
c76852d0bff115720af3f27acdb084c59361e5f6 13 15 2
previous bcd57e349c08bd7f076f8d6d2f39b702015358c1 AUTHORS
filename AUTHORS
38 changes: 20 additions & 18 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,26 @@ def test_blame_real(self):

@patch.object(Git, '_call_process')
def test_blame_incremental(self, git):
git.return_value = fixture('blame_incremental')
blame_output = self.rorepo.blame_incremental('9debf6b0aafb6f7781ea9d1383c86939a1aacde3', 'AUTHORS')
blame_output = list(blame_output)
self.assertEqual(len(blame_output), 5)

# Check all outputted line numbers
ranges = flatten([entry.linenos for entry in blame_output])
self.assertEqual(ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(3, 14), range(15, 17)]))

commits = [entry.commit.hexsha[:7] for entry in blame_output]
self.assertEqual(commits, ['82b8902', '82b8902', 'c76852d', 'c76852d', 'c76852d'])

# Original filenames
self.assertSequenceEqual([entry.orig_path for entry in blame_output], [u'AUTHORS'] * len(blame_output))

# Original line numbers
orig_ranges = flatten([entry.orig_linenos for entry in blame_output])
self.assertEqual(orig_ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(2, 13), range(13, 15)])) # noqa E501
# loop over two fixtures, create a test fixture for 2.11.1+ syntax
for git_fixture in ('blame_incremental', 'blame_incremental_2.11.1_plus'):
git.return_value = fixture(git_fixture)
blame_output = self.rorepo.blame_incremental('9debf6b0aafb6f7781ea9d1383c86939a1aacde3', 'AUTHORS')
blame_output = list(blame_output)
self.assertEqual(len(blame_output), 5)

# Check all outputted line numbers
ranges = flatten([entry.linenos for entry in blame_output])
self.assertEqual(ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(3, 14), range(15, 17)]))

commits = [entry.commit.hexsha[:7] for entry in blame_output]
self.assertEqual(commits, ['82b8902', '82b8902', 'c76852d', 'c76852d', 'c76852d'])

# Original filenames
self.assertSequenceEqual([entry.orig_path for entry in blame_output], [u'AUTHORS'] * len(blame_output))

# Original line numbers
orig_ranges = flatten([entry.orig_linenos for entry in blame_output])
self.assertEqual(orig_ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(2, 13), range(13, 15)])) # noqa E501

@patch.object(Git, '_call_process')
def test_blame_complex_revision(self, git):
Expand Down