Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
GitPlugin: avoid usage of StringIO wrappers throughout PyGIT.py where…
Browse files Browse the repository at this point in the history
… avoidable
  • Loading branch information
hvr committed Mar 23, 2009
1 parent 3666f7f commit a14e33d
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions tracext/git/PyGIT.py
Expand Up @@ -20,6 +20,7 @@
from threading import Lock from threading import Lock
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import cStringIO import cStringIO
import string


__all__ = ["git_version", "GitError", "GitErrorSha", "Storage", "StorageFactory"] __all__ = ["git_version", "GitError", "GitErrorSha", "Storage", "StorageFactory"]


Expand Down Expand Up @@ -56,7 +57,7 @@ def __execute(self, git_cmd, *cmd_args):
stdout_data, stderr_data = p.communicate() stdout_data, stderr_data = p.communicate()
#TODO, do something with p.returncode, e.g. raise exception #TODO, do something with p.returncode, e.g. raise exception


return cStringIO.StringIO(stdout_data) return stdout_data


def __getattr__(self, name): def __getattr__(self, name):
return partial(self.__execute, name.replace('_','-')) return partial(self.__execute, name.replace('_','-'))
Expand Down Expand Up @@ -149,8 +150,7 @@ def git_version(git_bin="git"):
GIT_VERSION_MIN_REQUIRED = (1,5,6) GIT_VERSION_MIN_REQUIRED = (1,5,6)
try: try:
g = GitCore(git_bin=git_bin) g = GitCore(git_bin=git_bin)
output = g.version() [v] = g.version().splitlines()
[v] = output.readlines()
_,_,version = v.strip().split() _,_,version = v.strip().split()
# 'version' has usually at least 3 numeric version components, e.g.:: # 'version' has usually at least 3 numeric version components, e.g.::
# 1.5.4.2 # 1.5.4.2
Expand Down Expand Up @@ -239,7 +239,7 @@ def get_rev_cache(self):
new_tags = set([]) new_tags = set([])
youngest = None youngest = None
oldest = None oldest = None
for revs in self.repo.rev_parse("--tags"): for revs in self.repo.rev_parse("--tags").splitlines():
new_tags.add(revs.strip()) new_tags.add(revs.strip())


# helper for reusing strings # helper for reusing strings
Expand All @@ -249,7 +249,7 @@ def __rev_reuse(rev):
return __rev_seen.setdefault(rev, rev) return __rev_seen.setdefault(rev, rev)


rev = ord_rev = 0 rev = ord_rev = 0
for revs in self.repo.rev_list("--parents", "--all"): for revs in self.repo.rev_list("--parents", "--all").splitlines():
revs = revs.strip().split() revs = revs.strip().split()


revs = map(__rev_reuse, revs) revs = map(__rev_reuse, revs)
Expand Down Expand Up @@ -371,7 +371,7 @@ def hist_prev_revision(self, sha):
def get_commit_encoding(self): def get_commit_encoding(self):
if self.commit_encoding is None: if self.commit_encoding is None:
self.commit_encoding = \ self.commit_encoding = \
self.repo.repo_config("--get", "i18n.commitEncoding").read().strip() or 'utf-8' self.repo.repo_config("--get", "i18n.commitEncoding").strip() or 'utf-8'


return self.commit_encoding return self.commit_encoding


Expand All @@ -392,15 +392,15 @@ def verifyrev(self, rev):
return fullrev return fullrev


# fall back to external git calls # fall back to external git calls
rc = self.repo.rev_parse("--verify", rev).read().strip() rc = self.repo.rev_parse("--verify", rev).strip()
if not rc: if not rc:
return None return None


if db.has_key(rc): if db.has_key(rc):
return rc return rc


if rc in tag_db: if rc in tag_db:
sha=self.repo.cat_file("tag", rc).read().split(None, 2)[:2] sha=self.repo.cat_file("tag", rc).split(None, 2)[:2]
if sha[0] != 'object': if sha[0] != 'object':
self.logger.debug("unexpected result from 'git-cat-file tag %s'" % rc) self.logger.debug("unexpected result from 'git-cat-file tag %s'" % rc)
return None return None
Expand All @@ -411,7 +411,7 @@ def verifyrev(self, rev):
def shortrev(self, rev, min_len=7): def shortrev(self, rev, min_len=7):
"try to shorten sha id" "try to shorten sha id"
#try to emulate the following: #try to emulate the following:
#return self.repo.rev_parse("--short", str(rev)).read().strip() #return self.repo.rev_parse("--short", str(rev)).strip()
rev = str(rev) rev = str(rev)


if min_len < self.__SREV_MIN: if min_len < self.__SREV_MIN:
Expand Down Expand Up @@ -465,7 +465,7 @@ def fullrev(self, srev):
def get_branches(self): def get_branches(self):
"returns list of (local) branches, with active (= HEAD) one being the first item" "returns list of (local) branches, with active (= HEAD) one being the first item"
result=[] result=[]
for e in self.repo.branch("-v", "--no-abbrev"): for e in self.repo.branch("-v", "--no-abbrev").splitlines():
(bname,bsha)=e[1:].strip().split()[:2] (bname,bsha)=e[1:].strip().split()[:2]
if e.startswith('*'): if e.startswith('*'):
result.insert(0,(bname,bsha)) result.insert(0,(bname,bsha))
Expand All @@ -474,14 +474,14 @@ def get_branches(self):
return result return result


def get_tags(self): def get_tags(self):
return [e.strip() for e in self.repo.tag("-l")] return [e.strip() for e in self.repo.tag("-l").splitlines()]


def ls_tree(self, rev, path=""): def ls_tree(self, rev, path=""):
rev = str(rev) # paranoia rev = str(rev) # paranoia
if path.startswith('/'): if path.startswith('/'):
path = path[1:] path = path[1:]


tree = self.repo.ls_tree("-z", "-l", rev, "--", path).read().split('\0') tree = self.repo.ls_tree("-z", "-l", rev, "--", path).split('\0')


def split_ls_tree_line(l): def split_ls_tree_line(l):
"split according to '<mode> <type> <sha> <size>\t<fname>'" "split according to '<mode> <type> <sha> <size>\t<fname>'"
Expand Down Expand Up @@ -515,7 +515,7 @@ def read_commit(self, commit_id):
return result[0], dict(result[1]) return result[0], dict(result[1])


# cache miss # cache miss
raw = self.repo.cat_file("commit", commit_id).read() raw = self.repo.cat_file("commit", commit_id)
raw = unicode(raw, self.get_commit_encoding(), 'replace') raw = unicode(raw, self.get_commit_encoding(), 'replace')
lines = raw.splitlines() lines = raw.splitlines()


Expand All @@ -536,13 +536,13 @@ def read_commit(self, commit_id):
return result[0], dict(result[1]) return result[0], dict(result[1])


def get_file(self, sha): def get_file(self, sha):
return self.repo.cat_file("blob", str(sha)) return cStringIO.StringIO(self.repo.cat_file("blob", str(sha)))


def get_obj_size(self, sha): def get_obj_size(self, sha):
sha = str(sha) sha = str(sha)


try: try:
obj_size = int(self.repo.cat_file("-s", sha).read().strip()) obj_size = int(self.repo.cat_file("-s", sha).strip())
except ValueError: except ValueError:
raise GitErrorSha("object '%s' not found" % sha) raise GitErrorSha("object '%s' not found" % sha)


Expand Down Expand Up @@ -588,26 +588,26 @@ def all_revs(self):
return self.get_commits().iterkeys() return self.get_commits().iterkeys()


def sync(self): def sync(self):
rev = self.repo.rev_list("--max-count=1", "--all").read().strip() rev = self.repo.rev_list("--max-count=1", "--all").strip()
return self.__rev_cache_sync(rev) return self.__rev_cache_sync(rev)


def last_change(self, sha, path): def last_change(self, sha, path):
return self.repo.rev_list("--max-count=1", return self.repo.rev_list("--max-count=1",
sha, "--", path).read().strip() or None sha, "--", path).strip() or None


def history(self, sha, path, limit=None): def history(self, sha, path, limit=None):
if limit is None: if limit is None:
limit = -1 limit = -1
for rev in self.repo.rev_list("--max-count=%d" % limit,
str(sha), "--", path): tmp = self.repo.rev_list("--max-count=%d" % limit, str(sha), "--", path)
yield rev.strip() return [ rev.strip() for rev in tmp.splitlines() ]


def history_timerange(self, start, stop): def history_timerange(self, start, stop):
for rev in self.repo.rev_list("--reverse", return [ rev.strip() for rev in \
"--max-age=%d" % start, self.repo.rev_list("--reverse",
"--min-age=%d" % stop, "--max-age=%d" % start,
"--all"): "--min-age=%d" % stop,
yield rev.strip() "--all").splitlines() ]


def rev_is_anchestor_of(self, rev1, rev2): def rev_is_anchestor_of(self, rev1, rev2):
"""return True if rev2 is successor of rev1""" """return True if rev2 is successor of rev1"""
Expand All @@ -618,7 +618,7 @@ def rev_is_anchestor_of(self, rev1, rev2):
def blame(self, commit_sha, path): def blame(self, commit_sha, path):
in_metadata = False in_metadata = False


for line in self.repo.blame("-p", "--", path, str(commit_sha)): for line in self.repo.blame("-p", "--", path, str(commit_sha)).splitlines():
assert line assert line
if in_metadata: if in_metadata:
in_metadata = not line.startswith('\t') in_metadata = not line.startswith('\t')
Expand Down Expand Up @@ -649,7 +649,7 @@ def diff_tree(self, tree1, tree2, path="", find_renames=False):
str(tree2), str(tree2),
"--", path]) "--", path])


lines = self.repo.diff_tree(*diff_tree_args).read().split('\0') lines = self.repo.diff_tree(*diff_tree_args).split('\0')


assert lines[-1] == "" assert lines[-1] == ""
del lines[-1] del lines[-1]
Expand Down

0 comments on commit a14e33d

Please sign in to comment.