Remove compatibility hacks causing issues with newer GitPython

Remove some monkey patching used to allow git-upstream to work with
GitPython releases older than 0.3.2, as the project requirements now
depends on 1.0.1 or newer.

GitPython release from 2.0.9 onwards removes the '__slots__'
definition. This allowed for the git_dir attribute existence to be
checked to determine whether to patch the Commit class imported from
the GitPython project to add this attribute for older versions of the
API.

Change-Id: I3d83785b16cd0650942c7112b5cafffc21dddba7
Closes-Bug: #1634053
This commit is contained in:
Darragh Bailey 2016-10-17 11:26:44 +01:00
parent 691ae783e0
commit 5c0a4a7e4d
4 changed files with 6 additions and 96 deletions

View File

@ -15,17 +15,14 @@
# limitations under the License.
#
import git
from git import base
from git_upstream.lib import note
try:
from git.objects.commit import Commit
from git.repo import Repo
except ImportError:
from git import Commit
from git import Repo
try:
from git import BadName
@ -33,102 +30,14 @@ except ImportError:
BadName = object
class GitUpstreamCompatRepo(Repo):
@property
def git_dir(self):
return self.path
if not hasattr(Repo, 'git_dir'):
Repo = GitUpstreamCompatRepo
# Monkey patch old python-git library to use GitUpstreamCompatRepo instead
# of Repo
git.Repo = GitUpstreamCompatRepo
class GitUpstreamCompatCommit(Commit):
@classmethod
def list_from_string(cls, repo, text):
"""
Parse out commit information into a list of Commit objects
This fixes the superclass behaviour in earlier versions of python-git
where blank lines in commit messages weren't retained.
:param repo: is the Repo
:param text: is the text output from the git command (raw format)
Returns
Commit[]
"""
lines = [l for l in text.strip().splitlines()]
commits = []
while lines:
id = lines.pop(0).split()[1]
tree = lines.pop(0).split()[1]
parents = []
while lines and lines[0].startswith('parent'):
parents.append(lines.pop(0).split()[-1])
author, authored_date = cls.actor(lines.pop(0))
committer, committed_date = cls.actor(lines.pop(0))
messages = []
lines.pop(0)
while lines and lines[0].startswith(' '):
messages.append(lines.pop(0).strip())
message = '\n'.join(messages)
commits.append(GitUpstreamCompatCommit(
repo, id=id, parents=parents,
tree=tree, author=author,
authored_date=authored_date,
committer=committer,
committed_date=committed_date,
message=message))
while lines:
if not lines[0].strip():
lines.pop(0)
else:
break
return commits
@property
def hexsha(self):
return self.id
@property
def short(self):
return self.repo.git.rev_parse(self.hexsha, short=True)
@classmethod
def iter_items(cls, repo, ref, path='', **kwargs):
"""
Wrap call to find_all method in older versions of python-git
"""
results = cls.find_all(repo, ref, path='', **kwargs)
if type(results) == list:
# Callers are expecting a generator
for item in results:
yield item
elif hasattr(results, '__iter__') and not hasattr(results, '__len__'):
yield results
else:
raise RuntimeError("Unexpected type returned from 'find_all'")
if not hasattr(Commit, 'iter_items'):
Commit = GitUpstreamCompatCommit
# monkey patch class so that Repo will use the patched class
git.commit.Commit = GitUpstreamCompatCommit
else:
Commit.short = GitUpstreamCompatCommit.short
base.Object.short = GitUpstreamCompatCommit.short
base.Object.add_note = note.add_note
base.Object.append_note = note.append_note
base.Object.note = note.note_message

View File

@ -18,8 +18,9 @@
import os
import sys
from git.repo import Repo
from git_upstream.errors import GitUpstreamError
from git_upstream.lib.pygitcompat import Repo
try:
from git.exc import InvalidGitRepositoryError

View File

@ -41,7 +41,7 @@
pre-script: |
import os
from git_upstream.lib.pygitcompat import Repo
from git.repo import Repo
repo = Repo(os.path.curdir)
@ -58,7 +58,7 @@
post-script: |
import os
from git_upstream.lib.pygitcompat import Repo
from git.repo import Repo
repo = Repo(os.path.curdir)

View File

@ -53,7 +53,7 @@
pre-script: |
import os
from git_upstream.lib.pygitcompat import Repo
from git.repo import Repo
repo = Repo(os.path.curdir)