summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/git/objects/base.py17
-rw-r--r--lib/git/objects/tree.py10
-rw-r--r--test/git/test_tree.py14
3 files changed, 36 insertions, 5 deletions
diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py
index 6dd03ba4..b0989a43 100644
--- a/lib/git/objects/base.py
+++ b/lib/git/objects/base.py
@@ -196,3 +196,20 @@ class IndexObject(Object):
# END for each char
return mode
+ @property
+ def name(self):
+ """
+ Returns
+ Name portion of the path, effectively being the basename
+ """
+ return os.path.basename(self.path)
+
+ @property
+ def abspath(self):
+ """
+ Returns
+ Absolute path to this index object in the file system ( as opposed to the
+ .path field which is a path relative to the git repository )
+ """
+ return os.path.join(self.repo.git.git_dir, self.path)
+
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``
diff --git a/test/git/test_tree.py b/test/git/test_tree.py
index 64a7900a..7b66743f 100644
--- a/test/git/test_tree.py
+++ b/test/git/test_tree.py
@@ -4,6 +4,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
+import os
from test.testlib import *
from git import *
@@ -43,6 +44,19 @@ class TestTree(TestCase):
# trees and blobs
assert len(set(trees)|set(root.trees)) == len(trees)
assert len(set(b for b in root if isinstance(b, Blob)) | set(root.blobs)) == len( root.blobs )
+ subitem = trees[0][0]
+ assert "/" in subitem.path
+ assert subitem.name == os.path.basename(subitem.path)
+
+ # assure that at some point the traversed paths have a slash in them
+ found_slash = False
+ for item in root.traverse():
+ assert os.path.isabs(item.abspath)
+ if '/' in item.path:
+ found_slash = True
+ break
+ # END for each item
+ assert found_slash
def test_repr(self):
tree = Tree(self.repo, 'abc')