diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-10-13 21:26:19 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-10-13 21:26:19 +0200 |
commit | 6acec357c7609fdd2cb0f5fdb1d2756726c7fe98 (patch) | |
tree | c7cb258ba85691082b7f7e95fa7f187e1f37405a /lib/git | |
parent | f4fa1cb3c3e84cad8b74edb28531d2e27508be26 (diff) | |
download | gitpython-6acec357c7609fdd2cb0f5fdb1d2756726c7fe98.tar.gz |
renamed find_all to list_all, changed commit to use iterable interface in preparation for command changes
Diffstat (limited to 'lib/git')
-rw-r--r-- | lib/git/objects/commit.py | 71 | ||||
-rw-r--r-- | lib/git/refs.py | 16 | ||||
-rw-r--r-- | lib/git/repo.py | 17 | ||||
-rw-r--r-- | lib/git/utils.py | 4 |
4 files changed, 58 insertions, 50 deletions
diff --git a/lib/git/objects/commit.py b/lib/git/objects/commit.py index f1f878d7..c289b825 100644 --- a/lib/git/objects/commit.py +++ b/lib/git/objects/commit.py @@ -6,14 +6,14 @@ import re import time - +from git.utils import Iterable from git.actor import Actor -from tree import Tree import git.diff as diff import git.stats as stats +from tree import Tree import base -class Commit(base.Object): +class Commit(base.Object, Iterable): """ Wraps a git Commit object. @@ -81,7 +81,7 @@ class Commit(base.Object): We set all values at once. """ if attr in self.__slots__: - temp = Commit.find_all(self.repo, self.id, max_count=1)[0] + temp = Commit.list_items(self.repo, self.id, max_count=1)[0] self.parents = temp.parents self.tree = temp.tree self.author = temp.author @@ -120,7 +120,7 @@ class Commit(base.Object): return len(repo.git.rev_list(ref, '--', path).strip().splitlines()) @classmethod - def find_all(cls, repo, ref, path='', **kwargs): + def iter_items(cls, repo, ref, path='', **kwargs): """ Find all commits matching the given criteria. @@ -128,7 +128,7 @@ class Commit(base.Object): is the Repo ``ref`` - is the ref from which to begin (SHA1 or name) + is the ref from which to begin (SHA1, Head or name) ``path`` is an optinal path, if set only Commits that include the path @@ -146,49 +146,56 @@ class Commit(base.Object): options.update(kwargs) output = repo.git.rev_list(ref, '--', path, **options) - return cls._list_from_string(repo, output) + return cls._iter_from_stream(repo, iter(output.splitlines(False))) @classmethod - def _list_from_string(cls, repo, text): + def _iter_from_stream(cls, repo, stream): """ Parse out commit information into a list of Commit objects ``repo`` is the Repo - ``text`` - is the text output from the git-rev-list command (raw format) + ``stream`` + output stream from the git-rev-list command (raw format) Returns - git.Commit[] + iterator returning Commit objects """ - lines =text.splitlines(False) - commits = [] - - while lines: - id = lines.pop(0).split()[1] - tree = lines.pop(0).split()[1] + for line in stream: + id = line.split()[1] + assert line.split()[0] == "commit" + tree = stream.next().split()[1] parents = [] - while lines and lines[0].startswith('parent'): - parents.append(lines.pop(0).split()[-1]) - # END while there are parent lines - author, authored_date = cls._actor(lines.pop(0)) - committer, committed_date = cls._actor(lines.pop(0)) + next_line = None + for parent_line in stream: + if not parent_line.startswith('parent'): + next_line = parent_line + break + # END abort reading parents + parents.append(parent_line.split()[-1]) + # END for each parent line + + author, authored_date = cls._actor(next_line) + committer, committed_date = cls._actor(stream.next()) - # free line - lines.pop(0) + # empty line + stream.next() message_lines = [] - while lines and not lines[0].startswith('commit'): - message_lines.append(lines.pop(0).strip()) + next_line = None + for msg_line in stream: + if not msg_line.startswith(' '): + break + # END abort message reading + message_lines.append(msg_line.strip()) # END while there are message lines - message = '\n'.join(message_lines[:-1]) # last line is empty - - commits.append(Commit(repo, id=id, parents=parents, tree=tree, author=author, authored_date=authored_date, - committer=committer, committed_date=committed_date, message=message)) - # END while lines - return commits + message = '\n'.join(message_lines) + + yield Commit(repo, id=id, parents=parents, tree=tree, author=author, authored_date=authored_date, + committer=committer, committed_date=committed_date, message=message) + # END for each line in stream @classmethod def diff(cls, repo, a, b=None, paths=None): diff --git a/lib/git/refs.py b/lib/git/refs.py index bdfff6f4..32bc7784 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -73,7 +73,7 @@ class Ref(LazyMixin): return '/'.join(tokens[2:]) @classmethod - def find_all(cls, repo, common_path = "refs", **kwargs): + def list_items(cls, repo, common_path = "refs", **kwargs): """ Find all refs in the repository @@ -158,14 +158,14 @@ class Head(Ref): return self.object @classmethod - def find_all(cls, repo, common_path = "refs/heads", **kwargs): + def list_items(cls, repo, common_path = "refs/heads", **kwargs): """ Returns git.Head[] - For more documentation, please refer to git.base.Ref.find_all + For more documentation, please refer to git.base.Ref.list_items """ - return super(Head,cls).find_all(repo, common_path, **kwargs) + return super(Head,cls).list_items(repo, common_path, **kwargs) def __repr__(self): return '<git.Head "%s">' % self.name @@ -181,7 +181,7 @@ class TagRef(Ref): This tag object will always point to a commit object, but may carray additional information in a tag object:: - tagref = TagRef.find_all(repo)[0] + tagref = TagRef.list_items(repo)[0] print tagref.commit.message if tagref.tag is not None: print tagref.tag.message @@ -215,14 +215,14 @@ class TagRef(Ref): return None @classmethod - def find_all(cls, repo, common_path = "refs/tags", **kwargs): + def list_items(cls, repo, common_path = "refs/tags", **kwargs): """ Returns git.Tag[] - For more documentation, please refer to git.base.Ref.find_all + For more documentation, please refer to git.base.Ref.list_items """ - return super(TagRef,cls).find_all(repo, common_path, **kwargs) + return super(TagRef,cls).list_items(repo, common_path, **kwargs) # provide an alias diff --git a/lib/git/repo.py b/lib/git/repo.py index c1387870..0dd776f6 100644 --- a/lib/git/repo.py +++ b/lib/git/repo.py @@ -102,7 +102,7 @@ class Repo(object): Returns ``git.Head[]`` """ - return Head.find_all(self) + return Head.list_items(self) # alias heads branches = heads @@ -115,7 +115,7 @@ class Repo(object): Returns ``git.Tag[]`` """ - return Tag.find_all(self) + return Tag.list_items(self) def blame(self, commit, file): """ @@ -221,7 +221,7 @@ class Repo(object): options = {'max_count': max_count, 'skip': skip} - return Commit.find_all(self, start, path, **options) + return Commit.list_items(self, start, path, **options) def commits_between(self, frm, to): """ @@ -237,7 +237,7 @@ class Repo(object): Returns ``git.Commit[]`` """ - return reversed(Commit.find_all(self, "%s..%s" % (frm, to))) + return reversed(Commit.list_items(self, "%s..%s" % (frm, to))) def commits_since(self, start='master', path='', since='1970-01-01'): """ @@ -259,7 +259,7 @@ class Repo(object): """ options = {'since': since} - return Commit.find_all(self, start, path, **options) + return Commit.list_items(self, start, path, **options) def commit_count(self, start='master', path=''): """ @@ -296,7 +296,7 @@ class Repo(object): id = self.active_branch options = {'max_count': 1} - commits = Commit.find_all(self, id, path, **options) + commits = Commit.list_items(self, id, path, **options) if not commits: raise ValueError, "Invalid identifier %s, or given path '%s' too restrictive" % ( id, path ) @@ -313,7 +313,7 @@ class Repo(object): other_repo_refs = other_repo.git.rev_list(other_ref, '--').strip().splitlines() diff_refs = list(set(other_repo_refs) - set(repo_refs)) - return map(lambda ref: Commit.find_all(other_repo, ref, max_count=1)[0], diff_refs) + return map(lambda ref: Commit.list_items(other_repo, ref, max_count=1)[0], diff_refs) def tree(self, treeish=None): """ @@ -364,7 +364,8 @@ class Repo(object): if path: arg.append(path) commits = self.git.log(*arg, **options) - return Commit._list_from_string(self, commits) + print commits.splitlines(False) + return list(Commit._iter_from_stream(self, iter(commits.splitlines()))) def diff(self, a, b, *paths): """ diff --git a/lib/git/utils.py b/lib/git/utils.py index 96ec15b9..f84c247d 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -75,13 +75,13 @@ class Iterable(object): Returns: list(Item,...) list of item instances """ - return list(cls.iter_items, repo, *args, **kwargs) + return list(cls.iter_items(repo, *args, **kwargs)) @classmethod def iter_items(cls, repo, *args, **kwargs): """ - For more information about the arguments, see find_all + For more information about the arguments, see list_items Return: iterator yielding Items """ |