Merge lp:~jelmer/bzr/path2id-list into lp:bzr

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merged at revision: 6495
Proposed branch: lp:~jelmer/bzr/path2id-list
Merge into: lp:bzr
Diff against target: 164 lines (+76/-6)
6 files modified
bzrlib/inventory.py (+7/-4)
bzrlib/tests/per_tree/__init__.py (+1/-0)
bzrlib/tests/per_tree/test_ids.py (+51/-0)
bzrlib/transform.py (+4/-0)
bzrlib/workingtree_4.py (+10/-1)
doc/en/release-notes/bzr-2.6.txt (+3/-1)
To merge this branch: bzr merge lp:~jelmer/bzr/path2id-list
Reviewer Review Type Date Requested Status
Martin Packman (community) Approve
Review via email: mp+95956@code.launchpad.net

Commit message

Reintroduce support for passing lists to Tree.path2id().

Description of the change

Allow passing in a list of path elements to Tree.path2id.

This was supported in the past, because Inventory.path2id allowed it,
and is still quite useful.

Also, add some more tests for Tree.path2id and Tree.id2path. Previously
these weren't directly tested (though they usually just called out to
Inventory.{path2id,id2path}, which are).

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

There is some repetition here. Perhaps I should factor out:

12 + if relpath == []:
13 + relpath = [""]
14 + relpath = osutils.pathjoin(*relpath)

... into another helper method?

Revision history for this message
Martin Packman (gz) wrote :

Appears mostly harmless.

+ if isinstance(relpath, basestring):
+ names = osutils.splitpath(relpath)
+ else:

Think I'd prefer if this check was round the other way like the other branches, use `isinstance(relpath, list)` as the special case and assume string otherwise.

+ if path == []:
+ path = [""]
+ path = osutils.pathjoin(*path)

This makes me want to indulge and write `path and osutils.pathjoin(*path) or ""` but it's better not to.

The factoring out of a lot of this path stuff into a Path object is still something I'd like, then this methods could just use a from_string_or_list constructor or similar.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/inventory.py'
2--- bzrlib/inventory.py 2012-02-17 16:37:15 +0000
3+++ bzrlib/inventory.py 2012-03-05 17:33:21 +0000
4@@ -2111,13 +2111,16 @@
5 def path2id(self, relpath):
6 """See CommonInventory.path2id()."""
7 # TODO: perhaps support negative hits?
8+ if isinstance(relpath, basestring):
9+ names = osutils.splitpath(relpath)
10+ else:
11+ names = relpath
12+ if relpath == []:
13+ relpath = [""]
14+ relpath = osutils.pathjoin(*relpath)
15 result = self._path_to_fileid_cache.get(relpath, None)
16 if result is not None:
17 return result
18- if isinstance(relpath, basestring):
19- names = osutils.splitpath(relpath)
20- else:
21- names = relpath
22 current_id = self.root_id
23 if current_id is None:
24 return None
25
26=== modified file 'bzrlib/tests/per_tree/__init__.py'
27--- bzrlib/tests/per_tree/__init__.py 2011-06-14 01:26:41 +0000
28+++ bzrlib/tests/per_tree/__init__.py 2012-03-05 17:33:21 +0000
29@@ -382,6 +382,7 @@
30 'get_file_with_stat',
31 'get_root_id',
32 'get_symlink_target',
33+ 'ids',
34 'inv',
35 'iter_search_rules',
36 'is_executable',
37
38=== added file 'bzrlib/tests/per_tree/test_ids.py'
39--- bzrlib/tests/per_tree/test_ids.py 1970-01-01 00:00:00 +0000
40+++ bzrlib/tests/per_tree/test_ids.py 2012-03-05 17:33:21 +0000
41@@ -0,0 +1,51 @@
42+# Copyright (C) 2012 Canonical Ltd
43+#
44+# This program is free software; you can redistribute it and/or modify
45+# it under the terms of the GNU General Public License as published by
46+# the Free Software Foundation; either version 2 of the License, or
47+# (at your option) any later version.
48+#
49+# This program is distributed in the hope that it will be useful,
50+# but WITHOUT ANY WARRANTY; without even the implied warranty of
51+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52+# GNU General Public License for more details.
53+#
54+# You should have received a copy of the GNU General Public License
55+# along with this program; if not, write to the Free Software
56+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
57+
58+from bzrlib import (
59+ errors,
60+ )
61+from bzrlib.tests.per_tree import TestCaseWithTree
62+
63+class IdTests(TestCaseWithTree):
64+
65+ def setUp(self):
66+ super(IdTests, self).setUp()
67+ work_a = self.make_branch_and_tree('wta')
68+ self.build_tree(['wta/bla', 'wta/dir/', 'wta/dir/file'])
69+ work_a.add(['bla', 'dir', 'dir/file'], ['bla-id', 'dir-id', 'file-id'])
70+ work_a.commit('add files')
71+ self.tree_a = self.workingtree_to_test_tree(work_a)
72+
73+ def test_path2id(self):
74+ self.assertEquals('bla-id', self.tree_a.path2id('bla'))
75+ self.assertEquals('dir-id', self.tree_a.path2id('dir'))
76+ self.assertIs(None, self.tree_a.path2id('idontexist'))
77+
78+ def test_path2id_list(self):
79+ self.assertEquals('bla-id', self.tree_a.path2id(['bla']))
80+ self.assertEquals('dir-id', self.tree_a.path2id(['dir']))
81+ self.assertEquals('file-id', self.tree_a.path2id(['dir', 'file']))
82+ self.assertEquals(self.tree_a.get_root_id(),
83+ self.tree_a.path2id([]))
84+ self.assertIs(None, self.tree_a.path2id(['idontexist']))
85+ self.assertIs(None, self.tree_a.path2id(['dir', 'idontexist']))
86+
87+ def test_id2path(self):
88+ self.addCleanup(self.tree_a.lock_read().unlock)
89+ self.assertEquals('bla', self.tree_a.id2path('bla-id'))
90+ self.assertEquals('dir', self.tree_a.id2path('dir-id'))
91+ self.assertEquals('dir/file', self.tree_a.id2path('file-id'))
92+ self.assertRaises(errors.NoSuchId, self.tree_a.id2path, 'nonexistant')
93
94=== modified file 'bzrlib/transform.py'
95--- bzrlib/transform.py 2012-02-06 23:38:33 +0000
96+++ bzrlib/transform.py 2012-03-05 17:33:21 +0000
97@@ -2117,6 +2117,10 @@
98 return cur_parent
99
100 def path2id(self, path):
101+ if isinstance(path, list):
102+ if path == []:
103+ path = [""]
104+ path = osutils.pathjoin(*path)
105 return self._transform.final_file_id(self._path2trans_id(path))
106
107 def id2path(self, file_id):
108
109=== modified file 'bzrlib/workingtree_4.py'
110--- bzrlib/workingtree_4.py 2012-02-06 23:38:33 +0000
111+++ bzrlib/workingtree_4.py 2012-03-05 17:33:21 +0000
112@@ -893,6 +893,10 @@
113 @needs_read_lock
114 def path2id(self, path):
115 """Return the id for path in this tree."""
116+ if isinstance(path, list):
117+ if path == []:
118+ path = [""]
119+ path = osutils.pathjoin(*path)
120 path = path.strip('/')
121 entry = self._get_entry(path=path)
122 if entry == (None, None):
123@@ -1776,7 +1780,8 @@
124 if path is not None:
125 path = path.encode('utf8')
126 parent_index = self._get_parent_index()
127- return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
128+ return self._dirstate._get_entry(parent_index, fileid_utf8=file_id,
129+ path_utf8=path)
130
131 def _generate_inventory(self):
132 """Create and set self.inventory from the dirstate object.
133@@ -2028,6 +2033,10 @@
134 def path2id(self, path):
135 """Return the id for path in this tree."""
136 # lookup by path: faster than splitting and walking the ivnentory.
137+ if isinstance(path, list):
138+ if path == []:
139+ path = [""]
140+ path = osutils.pathjoin(*path)
141 entry = self._get_entry(path=path)
142 if entry == (None, None):
143 return None
144
145=== modified file 'doc/en/release-notes/bzr-2.6.txt'
146--- doc/en/release-notes/bzr-2.6.txt 2012-02-25 14:13:19 +0000
147+++ doc/en/release-notes/bzr-2.6.txt 2012-03-05 17:33:21 +0000
148@@ -77,6 +77,9 @@
149 .. Major internal changes, unlikely to be visible to users or plugin
150 developers, but interesting for bzr developers.
151
152+* ``Tree.path2id`` now once again accepts a list of path elements
153+ in addition to a path. (Jelmer Vernooij)
154+
155 Testing
156 *******
157
158@@ -84,6 +87,5 @@
159 suite. This can include new facilities for writing tests, fixes to
160 spurious test failures and changes to the way things should be tested.
161
162-
163 ..
164 vim: tw=74 ft=rst ff=unix