diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-12-30 16:17:28 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-12-30 16:17:28 +0100 |
commit | d86e77edd500f09e3e19017c974b525643fbd539 (patch) | |
tree | e2c4bbdb4aff635b9480109d7dcef17a63c050b3 | |
parent | 1b3feddd1269a0b0976f4eef2093cb0dbf258f99 (diff) | |
download | gitpython-d86e77edd500f09e3e19017c974b525643fbd539.tar.gz |
tree: implemented recursive paths in __div__ and __getitem__ method, allowing the keys to contain slashes; adjusted test to check for this
-rw-r--r-- | doc/tutorial.rst | 2 | ||||
-rw-r--r-- | lib/git/objects/tree.py | 33 | ||||
-rw-r--r-- | test/git/test_tree.py | 5 |
3 files changed, 33 insertions, 7 deletions
diff --git a/doc/tutorial.rst b/doc/tutorial.rst index 695e9812..86012335 100644 --- a/doc/tutorial.rst +++ b/doc/tutorial.rst @@ -239,6 +239,7 @@ query entries by name. 'dir/file' >>> blob.abspath '/Users/mtrier/Development/git-python/dir/file' + >>>tree['dir/file'].sha == blob.sha There is a convenience method that allows you to get a named sub-object from a tree with a syntax similar to how paths are written in an unix @@ -246,6 +247,7 @@ system. >>> tree/"lib" <git.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30"> + >>> tree/"dir/file" == blob.sha You can also get a tree directly from the repository if you know its name. diff --git a/lib/git/objects/tree.py b/lib/git/objects/tree.py index c80450fc..0b0ac0b1 100644 --- a/lib/git/objects/tree.py +++ b/lib/git/objects/tree.py @@ -158,7 +158,32 @@ class Tree(base.IndexObject, diff.Diffable, utils.Traversable): Raise KeyError if given file or tree does not exist in tree """ - return self[file] + msg = "Blob or Tree named %r not found" + if '/' in file: + tree = self + item = self + tokens = file.split('/') + for i,token in enumerate(tokens): + item = tree[token] + if item.type == 'tree': + tree = item + else: + # safety assertion - blobs are at the end of the path + if i != len(tokens)-1: + raise KeyError(msg % file) + return item + # END handle item type + # END for each token of split path + if item == self: + raise KeyError(msg % file) + return item + else: + for obj in self._cache: + if obj.name == file: + return obj + # END for each obj + raise KeyError( msg % file ) + # END handle long paths def __repr__(self): @@ -205,11 +230,7 @@ class Tree(base.IndexObject, diff.Diffable, utils.Traversable): if isinstance(item, basestring): # compatability - for obj in self._cache: - if obj.name == item: - return obj - # END for each obj - raise KeyError( "Blob or Tree named %s not found" % item ) + return self.__div__(item) # END index is basestring raise TypeError( "Invalid index type: %r" % item ) diff --git a/test/git/test_tree.py b/test/git/test_tree.py index 0bb9647f..54a3352b 100644 --- a/test/git/test_tree.py +++ b/test/git/test_tree.py @@ -54,7 +54,10 @@ class TestTree(TestCase): assert os.path.isabs(item.abspath) if '/' in item.path: found_slash = True - break + # END check for slash + + # slashes in paths are supported as well + assert root[item.path] == item == root/item.path # END for each item assert found_slash |