Skip to content

Commit

Permalink
finding git content directly by hash
Browse files Browse the repository at this point in the history
The GitWorkingCopy.content method was iterating over all commits of
the master branch before to find the commit with the specified hash.
When the commit was not on the master branch, that would fail. Instead,
it is not iterated over a branch anymore, but the commit is directly
accessed through GitPython.
  • Loading branch information
timtroendle committed Jan 23, 2016
1 parent 29ac837 commit 0758a78
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions sumatra/versioncontrol/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,21 @@ def diff(self):
def content(self, digest, file=None):
"""Get the file content from repository."""
repo = git.Repo(self.path)
for item in repo.iter_commits('master'):
if item.hexsha == digest:
curtree = item.tree
if file is None:
return curtree.blobs[0].data_stream.read() # Get the latest added file content.
dirname,filename = os.path.split(file)
if dirname != '':
for dname in dirname.split(os.path.sep):
for subtree in curtree.trees:
if subtree.name == dname:
curtree = subtree
break
for blob in curtree.blobs:
if blob.name == filename:
expected_encoding = sys.getfilesystemencoding()
file_content = blob.data_stream.read().decode(expected_encoding)
return file_content
curtree = repo.commit(digest).tree
if file is None:
return curtree.blobs[0].data_stream.read() # Get the latest added file content.
dirname, filename = os.path.split(file)
if dirname != '':
for dname in dirname.split(os.path.sep):
for subtree in curtree.trees:
if subtree.name == dname:
curtree = subtree
break
for blob in curtree.blobs:
if blob.name == filename:
expected_encoding = sys.getfilesystemencoding()
file_content = blob.data_stream.read().decode(expected_encoding)
return file_content
return 'File content not found.'

def contains(self, path):
Expand Down

0 comments on commit 0758a78

Please sign in to comment.