Merge lp:~maxb/bzr-fastimport/branch-mapper-stuff into lp:~bzr/bzr-fastimport/fastimport.dev

Proposed by Max Bowsher
Status: Merged
Merged at revision: not available
Proposed branch: lp:~maxb/bzr-fastimport/branch-mapper-stuff
Merge into: lp:~bzr/bzr-fastimport/fastimport.dev
Diff against target: 227 lines (+48/-70)
5 files modified
branch_mapper.py (+28/-33)
branch_updater.py (+3/-3)
bzr_commit_handler.py (+3/-0)
cache_manager.py (+5/-1)
tests/test_branch_mapper.py (+9/-33)
To merge this branch: bzr merge lp:~maxb/bzr-fastimport/branch-mapper-stuff
Reviewer Review Type Date Requested Status
Ian Clatworthy Approve
Review via email: mp+14594@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Max Bowsher (maxb) wrote :

As per thread "bzr-fastimport: Set branch-nick from git ref-names" on the bazaar ML.

First, a number of minor refactorings to the branch_mapper:

* Avoid spurious 'git-' being prefixed on branches whose names happen to end with 'trunk',
  by tightening logic in BranchMapper._git_to_bzr_name. Also document.

* Remove unused and unimplemented BranchMapper.bzr_to_git, and update docstring.

* Make BranchMapper just map one name per call.
  Move building a dict to the one callsite which actually wants that.

* Store the BranchMapper in the CacheManager so it can be got from other places.

Culminating in:

* Default branch-nick to mapped git ref name.

One concern is that with this change, it would be impossible to round-trip a Bazaar revision with no branch-nick property. I think fast-import might need an option to suppress the auto-filling of branch nicks.

Revision history for this message
Ian Clatworthy (ian-clatworthy) wrote :

Looks good. I'll merge.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'branch_mapper.py'
2--- branch_mapper.py 2009-10-22 07:55:22 +0000
3+++ branch_mapper.py 2009-11-07 18:00:30 +0000
4@@ -14,50 +14,45 @@
5 # along with this program; if not, write to the Free Software
6 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7
8-"""An object that maps bzr branch names <-> git ref names."""
9+"""An object that maps git ref names to bzr branch names. Note that it is not
10+used to map git ref names to bzr tag names."""
11+
12+
13+import re
14
15
16 class BranchMapper(object):
17+ _GIT_TRUNK_RE = re.compile('(?:git-)*trunk')
18
19- def git_to_bzr(self, ref_names):
20- """Get the mapping from git reference names to Bazaar branch names.
21-
22- :return: a dictionary with git reference names as keys and
23- the Bazaar branch names as values.
24+ def git_to_bzr(self, ref_name):
25+ """Map a git reference name to a Bazaar branch name.
26 """
27- bazaar_names = {}
28- for ref_name in sorted(ref_names):
29- parts = ref_name.split('/')
30- if parts[0] == 'refs':
31+ parts = ref_name.split('/')
32+ if parts[0] == 'refs':
33+ parts.pop(0)
34+ category = parts.pop(0)
35+ if category == 'heads':
36+ git_name = '/'.join(parts)
37+ bazaar_name = self._git_to_bzr_name(git_name)
38+ else:
39+ if category == 'remotes' and parts[0] == 'origin':
40 parts.pop(0)
41- category = parts.pop(0)
42- if category == 'heads':
43- git_name = '/'.join(parts)
44- bazaar_name = self._git_to_bzr_name(git_name)
45- else:
46- if category == 'remotes' and parts[0] == 'origin':
47- parts.pop(0)
48- git_name = '/'.join(parts)
49- if category.endswith('s'):
50- category = category[:-1]
51- name_no_ext = self._git_to_bzr_name(git_name)
52- bazaar_name = "%s.%s" % (name_no_ext, category)
53- bazaar_names[ref_name] = bazaar_name
54- return bazaar_names
55+ git_name = '/'.join(parts)
56+ if category.endswith('s'):
57+ category = category[:-1]
58+ name_no_ext = self._git_to_bzr_name(git_name)
59+ bazaar_name = "%s.%s" % (name_no_ext, category)
60+ return bazaar_name
61
62 def _git_to_bzr_name(self, git_name):
63+ # Make a simple name more bzr-like, by mapping git 'master' to bzr 'trunk'.
64+ # To avoid collision, map git 'trunk' to bzr 'git-trunk'. Likewise
65+ # 'git-trunk' to 'git-git-trunk' and so on, such that the mapping is
66+ # one-to-one in both directions.
67 if git_name == 'master':
68 bazaar_name = 'trunk'
69- elif git_name.endswith('trunk'):
70+ elif self._GIT_TRUNK_RE.match(git_name):
71 bazaar_name = 'git-%s' % (git_name,)
72 else:
73 bazaar_name = git_name
74 return bazaar_name
75-
76- def bzr_to_git(self, branch_names):
77- """Get the mapping from Bazaar branch names to git reference names.
78-
79- :return: a dictionary with Bazaar branch names as keys and
80- the git reference names as values.
81- """
82- raise NotImplementedError(self.bzr_to_git)
83
84=== modified file 'branch_updater.py'
85--- branch_updater.py 2009-10-26 00:15:57 +0000
86+++ branch_updater.py 2009-11-07 18:00:30 +0000
87@@ -21,7 +21,6 @@
88 from bzrlib import bzrdir, errors, osutils, transport
89 from bzrlib.trace import error, note
90
91-import branch_mapper
92 import helpers
93
94
95@@ -40,7 +39,6 @@
96 self.heads_by_ref = heads_by_ref
97 self.last_ref = last_ref
98 self.tags = tags
99- self.name_mapper = branch_mapper.BranchMapper()
100 self._branch_format = \
101 helpers.best_format_for_objects_in_a_repository(repo)
102
103@@ -84,7 +82,9 @@
104
105 # Convert the reference names into Bazaar speak. If we haven't
106 # already put the 'trunk' first, do it now.
107- git_to_bzr_map = self.name_mapper.git_to_bzr(ref_names)
108+ git_to_bzr_map = {}
109+ for ref_name in ref_names:
110+ git_to_bzr_map[ref_name] = self.cache_mgr.branch_mapper.git_to_bzr(ref_name)
111 if ref_names and self.branch is None:
112 trunk = self.select_trunk(ref_names)
113 git_bzr_items = [(trunk, git_to_bzr_map[trunk])]
114
115=== modified file 'bzr_commit_handler.py'
116--- bzr_commit_handler.py 2009-10-12 02:12:22 +0000
117+++ bzr_commit_handler.py 2009-11-07 18:00:30 +0000
118@@ -218,6 +218,9 @@
119
120 def build_revision(self):
121 rev_props = self._legal_revision_properties(self.command.properties)
122+ if 'branch-nick' not in rev_props:
123+ rev_props['branch-nick'] = self.cache_mgr.branch_mapper.git_to_bzr(
124+ self.branch_ref)
125 self._save_author_info(rev_props)
126 committer = self.command.committer
127 who = self._format_name_email(committer[0], committer[1])
128
129=== modified file 'cache_manager.py'
130--- cache_manager.py 2009-08-24 10:13:02 +0000
131+++ cache_manager.py 2009-11-07 18:00:30 +0000
132@@ -18,7 +18,7 @@
133
134
135 from bzrlib import lru_cache, trace
136-from bzrlib.plugins.fastimport import helpers
137+from bzrlib.plugins.fastimport import branch_mapper, helpers
138
139 class CacheManager(object):
140
141@@ -66,6 +66,10 @@
142 # info not in file - possible when no blobs used
143 pass
144
145+ # BranchMapper has no state (for now?), but we keep it around rather
146+ # than reinstantiate on every usage
147+ self.branch_mapper = branch_mapper.BranchMapper()
148+
149 def dump_stats(self, note=trace.note):
150 """Dump some statistics about what we cached."""
151 # TODO: add in inventory stastistics
152
153=== modified file 'tests/test_branch_mapper.py'
154--- tests/test_branch_mapper.py 2009-10-22 07:55:22 +0000
155+++ tests/test_branch_mapper.py 2009-11-07 18:00:30 +0000
156@@ -27,62 +27,38 @@
157
158 def test_git_to_bzr(self):
159 m = branch_mapper.BranchMapper()
160- git_refs = [
161- 'refs/heads/master',
162- 'refs/heads/foo',
163- 'refs/tags/master',
164- 'refs/tags/foo',
165- 'refs/remotes/origin/master',
166- 'refs/remotes/origin/foo',
167- ]
168- git_to_bzr_map = m.git_to_bzr(git_refs)
169- self.assertEqual(git_to_bzr_map, {
170+ for git, bzr in {
171 'refs/heads/master': 'trunk',
172 'refs/heads/foo': 'foo',
173 'refs/tags/master': 'trunk.tag',
174 'refs/tags/foo': 'foo.tag',
175 'refs/remotes/origin/master': 'trunk.remote',
176 'refs/remotes/origin/foo': 'foo.remote',
177- })
178+ }.items():
179+ self.assertEqual(m.git_to_bzr(git), bzr)
180
181 def test_git_to_bzr_with_slashes(self):
182 m = branch_mapper.BranchMapper()
183- git_refs = [
184- 'refs/heads/master/slave',
185- 'refs/heads/foo/bar',
186- 'refs/tags/master/slave',
187- 'refs/tags/foo/bar',
188- 'refs/remotes/origin/master/slave',
189- 'refs/remotes/origin/foo/bar',
190- ]
191- git_to_bzr_map = m.git_to_bzr(git_refs)
192- self.assertEqual(git_to_bzr_map, {
193+ for git, bzr in {
194 'refs/heads/master/slave': 'master/slave',
195 'refs/heads/foo/bar': 'foo/bar',
196 'refs/tags/master/slave': 'master/slave.tag',
197 'refs/tags/foo/bar': 'foo/bar.tag',
198 'refs/remotes/origin/master/slave': 'master/slave.remote',
199 'refs/remotes/origin/foo/bar': 'foo/bar.remote',
200- })
201+ }.items():
202+ self.assertEqual(m.git_to_bzr(git), bzr)
203
204 def test_git_to_bzr_for_trunk(self):
205 # As 'master' in git is mapped to trunk in bzr, we need to handle
206 # 'trunk' in git in a sensible way.
207 m = branch_mapper.BranchMapper()
208- git_refs = [
209- 'refs/heads/trunk',
210- 'refs/tags/trunk',
211- 'refs/remotes/origin/trunk',
212- 'refs/heads/git-trunk',
213- 'refs/tags/git-trunk',
214- 'refs/remotes/origin/git-trunk',
215- ]
216- git_to_bzr_map = m.git_to_bzr(git_refs)
217- self.assertEqual(git_to_bzr_map, {
218+ for git, bzr in {
219 'refs/heads/trunk': 'git-trunk',
220 'refs/tags/trunk': 'git-trunk.tag',
221 'refs/remotes/origin/trunk': 'git-trunk.remote',
222 'refs/heads/git-trunk': 'git-git-trunk',
223 'refs/tags/git-trunk': 'git-git-trunk.tag',
224 'refs/remotes/origin/git-trunk':'git-git-trunk.remote',
225- })
226+ }.items():
227+ self.assertEqual(m.git_to_bzr(git), bzr)

Subscribers

People subscribed via source and target branches

to all changes: