Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd86e77e

Browse files
committed
tree: implemented recursive paths in __div__ and __getitem__ method, allowing the keys to contain slashes; adjusted test to check for this
1 parent1b3fedd commitd86e77e

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

‎doc/tutorial.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,15 @@ query entries by name.
239239
'dir/file'
240240
>>>blob.abspath
241241
'/Users/mtrier/Development/git-python/dir/file'
242+
>>>tree['dir/file'].sha == blob.sha
242243

243244
There is a convenience method that allows you to get a named sub-object
244245
from a tree with a syntax similar to how paths are written in an unix
245246
system.
246247

247248
>>>tree/"lib"
248249
<git.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30">
250+
>>>tree/"dir/file"== blob.sha
249251

250252
You can also get a tree directly from the repository if you know its name.
251253

‎lib/git/objects/tree.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,32 @@ def __div__(self, file):
158158
Raise
159159
KeyError if given file or tree does not exist in tree
160160
"""
161-
returnself[file]
161+
msg="Blob or Tree named %r not found"
162+
if'/'infile:
163+
tree=self
164+
item=self
165+
tokens=file.split('/')
166+
fori,tokeninenumerate(tokens):
167+
item=tree[token]
168+
ifitem.type=='tree':
169+
tree=item
170+
else:
171+
# safety assertion - blobs are at the end of the path
172+
ifi!=len(tokens)-1:
173+
raiseKeyError(msg%file)
174+
returnitem
175+
# END handle item type
176+
# END for each token of split path
177+
ifitem==self:
178+
raiseKeyError(msg%file)
179+
returnitem
180+
else:
181+
forobjinself._cache:
182+
ifobj.name==file:
183+
returnobj
184+
# END for each obj
185+
raiseKeyError(msg%file )
186+
# END handle long paths
162187

163188

164189
def__repr__(self):
@@ -205,11 +230,7 @@ def __getitem__(self,item):
205230

206231
ifisinstance(item,basestring):
207232
# compatability
208-
forobjinself._cache:
209-
ifobj.name==item:
210-
returnobj
211-
# END for each obj
212-
raiseKeyError("Blob or Tree named %s not found"%item )
233+
returnself.__div__(item)
213234
# END index is basestring
214235

215236
raiseTypeError("Invalid index type: %r"%item )

‎test/git/test_tree.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def test_traverse(self):
5454
assertos.path.isabs(item.abspath)
5555
if'/'initem.path:
5656
found_slash=True
57-
break
57+
# END check for slash
58+
59+
# slashes in paths are supported as well
60+
assertroot[item.path]==item==root/item.path
5861
# END for each item
5962
assertfound_slash
6063

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp