From b372e26366348920eae32ee81a47b469b511a21f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 16 Oct 2009 19:19:57 +0200 Subject: added Diffable interface to objects.base, its used by Commit and Tree objects. Diff class has been prepared to process raw input, but its not yet more than a frame --- lib/git/objects/tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index abfa9622..4d3e9ebd 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -15,7 +15,7 @@ def sha_to_hex(sha): assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha return hexsha -class Tree(base.IndexObject): +class Tree(base.IndexObject, base.Diffable): """ Tress represent a ordered list of Blobs and other Trees. Hence it can be accessed like a list. @@ -169,6 +169,7 @@ class Tree(base.IndexObject): def traverse(self, max_depth=-1, predicate = lambda i: True): """ Returns + Iterator to traverse the tree recursively up to the given level. The iterator returns Blob and Tree objects -- cgit v1.2.1 From aed099a73025422f0550f5dd5c3e4651049494b2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 18 Oct 2009 12:54:16 +0200 Subject: resolved cyclic inclusion issue by moving the Diffable interface into the diff module, which probably is the right thing to do anyway --- lib/git/objects/tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index 4d3e9ebd..c35c075e 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -8,6 +8,7 @@ import os import blob import base import binascii +import git.diff as diff def sha_to_hex(sha): """Takes a string and returns the hex of the sha within""" @@ -15,7 +16,7 @@ def sha_to_hex(sha): assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha return hexsha -class Tree(base.IndexObject, base.Diffable): +class Tree(base.IndexObject, diff.Diffable): """ Tress represent a ordered list of Blobs and other Trees. Hence it can be accessed like a list. -- cgit v1.2.1 From 2e68d907022c84392597e05afc22d9fe06bf0927 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 21 Oct 2009 18:45:41 +0200 Subject: tree.traverse: Added prune functionality - previously the predciate did both, pruning and preventing to return items --- lib/git/objects/tree.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index c35c075e..92aae881 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -152,22 +152,21 @@ class Tree(base.IndexObject, diff.Diffable): return '' % self.id @classmethod - def _iter_recursive(cls, repo, tree, cur_depth, max_depth, predicate ): + def _iter_recursive(cls, repo, tree, cur_depth, max_depth, predicate, prune ): for obj in tree: # adjust path to be complete obj.path = os.path.join(tree.path, obj.path) - if not predicate(obj): - continue - yield obj - if obj.type == "tree" and ( max_depth < 0 or cur_depth+1 <= max_depth ): - for recursive_obj in cls._iter_recursive( repo, obj, cur_depth+1, max_depth, predicate ): + if predicate(obj): + yield obj + if obj.type == "tree" and ( max_depth < 0 or cur_depth+1 <= max_depth ) and not prune(obj): + for recursive_obj in cls._iter_recursive( repo, obj, cur_depth+1, max_depth, predicate, prune ): yield recursive_obj # END for each recursive object # END if we may enter recursion # END for each object - def traverse(self, max_depth=-1, predicate = lambda i: True): + def traverse(self, max_depth=-1, predicate = lambda i: True, prune = lambda t: False): """ Returns @@ -183,8 +182,13 @@ class Tree(base.IndexObject, diff.Diffable): ``predicate`` If predicate(item) returns True, item will be returned by iterator + + ``prune`` + + If prune(tree) returns True, the traversal will not continue into the + given tree object. """ - return self._iter_recursive( self.repo, self, 0, max_depth, predicate ) + return self._iter_recursive( self.repo, self, 0, max_depth, predicate, prune ) @property def trees(self): -- cgit v1.2.1 From 291d2f85bb861ec23b80854b974f3b7a8ded2921 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 26 Oct 2009 21:23:55 +0100 Subject: When parsing trees, we now store the originan type bits as well, previously we dropped it --- lib/git/objects/tree.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index 92aae881..371c0dd3 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -38,9 +38,9 @@ class Tree(base.IndexObject, diff.Diffable): __slots__ = "_cache" # using ascii codes for comparison - ascii_commit_id = (0x31 << 4) + 0x36 - ascii_blob_id = (0x31 << 4) + 0x30 - ascii_tree_id = (0x34 << 4) + 0x30 + commit_id = 016 + blob_id = 010 + tree_id = 040 def __init__(self, repo, id, mode=0, path=None): @@ -88,8 +88,8 @@ class Tree(base.IndexObject, diff.Diffable): mode = 0 mode_boundary = i + 6 - # keep it ascii - we compare against the respective values - type_id = (ord(data[i])<<4) + ord(data[i+1]) + # read type + type_id = ((ord(data[i])-ord_zero)<<3) + (ord(data[i+1])-ord_zero) i += 2 while data[i] != ' ': @@ -115,12 +115,13 @@ class Tree(base.IndexObject, diff.Diffable): sha = data[i:i+20] i = i + 20 + mode |= type_id<<12 hexsha = sha_to_hex(sha) - if type_id == self.ascii_blob_id: + if type_id == self.blob_id: yield blob.Blob(self.repo, hexsha, mode, name) - elif type_id == self.ascii_tree_id: + elif type_id == self.tree_id: yield Tree(self.repo, hexsha, mode, name) - elif type_id == self.ascii_commit_id: + elif type_id == self.commit_id: # todo yield None else: -- cgit v1.2.1 From 3cb5ba18ab1a875ef6b62c65342de476be47871b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 3 Nov 2009 16:35:33 +0100 Subject: object: renamed id attribute to sha as it in fact is always being rewritten as sha, even if the passed in id was a ref. This is done to assure objects are uniquely identified and will compare correctly --- lib/git/objects/tree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index 371c0dd3..413efdb8 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -43,8 +43,8 @@ class Tree(base.IndexObject, diff.Diffable): tree_id = 040 - def __init__(self, repo, id, mode=0, path=None): - super(Tree, self).__init__(repo, id, mode, path) + def __init__(self, repo, sha, mode=0, path=None): + super(Tree, self).__init__(repo, sha, mode, path) def _set_cache_(self, attr): if attr == "_cache": @@ -150,7 +150,7 @@ class Tree(base.IndexObject, diff.Diffable): def __repr__(self): - return '' % self.id + return '' % self.sha @classmethod def _iter_recursive(cls, repo, tree, cur_depth, max_depth, predicate, prune ): -- cgit v1.2.1 From f9bbdc87a7263f479344fcf67c4b9fd6005bb6cd Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 4 Nov 2009 11:28:45 +0100 Subject: tree: parsing would fail when symlinks where encountered. This has been fixed --- lib/git/objects/tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index 413efdb8..fb292677 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -40,6 +40,7 @@ class Tree(base.IndexObject, diff.Diffable): # using ascii codes for comparison commit_id = 016 blob_id = 010 + symlink_id = 012 tree_id = 040 @@ -117,7 +118,7 @@ class Tree(base.IndexObject, diff.Diffable): mode |= type_id<<12 hexsha = sha_to_hex(sha) - if type_id == self.blob_id: + if type_id == self.blob_id or type_id == self.symlink_id: yield blob.Blob(self.repo, hexsha, mode, name) elif type_id == self.tree_id: yield Tree(self.repo, hexsha, mode, name) -- cgit v1.2.1 From c4cde8df886112ee32b0a09fcac90c28c85ded7f Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 4 Nov 2009 12:46:37 +0100 Subject: IndexObject: assured that .path fields are relative to the repository ( previously it would just be a name ) added abspath property and name property to provide easy access to most common paths of an index object --- lib/git/objects/tree.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index fb292677..bcb805af 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -110,6 +110,7 @@ class Tree(base.IndexObject, diff.Diffable): i += 1 # END while not reached NULL name = data[ns:i] + path = os.path.join(self.path, name) # byte is NULL, get next 20 i += 1 @@ -119,9 +120,9 @@ class Tree(base.IndexObject, diff.Diffable): mode |= type_id<<12 hexsha = sha_to_hex(sha) if type_id == self.blob_id or type_id == self.symlink_id: - yield blob.Blob(self.repo, hexsha, mode, name) + yield blob.Blob(self.repo, hexsha, mode, path) elif type_id == self.tree_id: - yield Tree(self.repo, hexsha, mode, name) + yield Tree(self.repo, hexsha, mode, path) elif type_id == self.commit_id: # todo yield None @@ -157,8 +158,6 @@ class Tree(base.IndexObject, diff.Diffable): def _iter_recursive(cls, repo, tree, cur_depth, max_depth, predicate, prune ): for obj in tree: - # adjust path to be complete - obj.path = os.path.join(tree.path, obj.path) if predicate(obj): yield obj if obj.type == "tree" and ( max_depth < 0 or cur_depth+1 <= max_depth ) and not prune(obj): @@ -173,7 +172,8 @@ class Tree(base.IndexObject, diff.Diffable): Returns Iterator to traverse the tree recursively up to the given level. - The iterator returns Blob and Tree objects + The iterator returns Blob and Tree objects with paths relative to their + repository. ``max_depth`` -- cgit v1.2.1 From f41d42ee7e264ce2fc32cea555e5f666fa1b1fe9 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 4 Nov 2009 13:17:37 +0100 Subject: Improved cmd error handling in case an invalid revision is specified for an object repo.tree: improved to be less restricting --- lib/git/objects/tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/git/objects/tree.py') diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index bcb805af..27bd84d0 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -226,7 +226,7 @@ class Tree(base.IndexObject, diff.Diffable): if isinstance(item, basestring): # compatability for obj in self._cache: - if obj.path == item: + if obj.name == item: return obj # END for each obj raise KeyError( "Blob or Tree named %s not found" % item ) -- cgit v1.2.1