summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Clatworthy <ian.clatworthy@canonical.com>2009-11-23 17:06:23 +1000
committerIan Clatworthy <ian.clatworthy@canonical.com>2009-11-23 17:06:23 +1000
commit2cc9973fff3377f5f5a49733ce1e799410859eb7 (patch)
treeebee90f9368fe7c6a58e5bcd226e88f78f336db3
parent44e5328212670b63422fe674863a39504daca414 (diff)
parentd2c891bc43514e5ba04c310ba936eb9eeb5da439 (diff)
downloadpython-fastimport-2cc9973fff3377f5f5a49733ce1e799410859eb7.tar.gz
Default branch nick to mapped git ref name (Max Bowsher)
-rw-r--r--branch_mapper.py61
-rw-r--r--branch_updater.py6
-rw-r--r--bzr_commit_handler.py3
-rw-r--r--cache_manager.py6
-rw-r--r--tests/test_branch_mapper.py42
5 files changed, 48 insertions, 70 deletions
diff --git a/branch_mapper.py b/branch_mapper.py
index 3bfc39b..acc37c9 100644
--- a/branch_mapper.py
+++ b/branch_mapper.py
@@ -14,50 +14,45 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-"""An object that maps bzr branch names <-> git ref names."""
+"""An object that maps git ref names to bzr branch names. Note that it is not
+used to map git ref names to bzr tag names."""
+
+
+import re
class BranchMapper(object):
+ _GIT_TRUNK_RE = re.compile('(?:git-)*trunk')
- def git_to_bzr(self, ref_names):
- """Get the mapping from git reference names to Bazaar branch names.
-
- :return: a dictionary with git reference names as keys and
- the Bazaar branch names as values.
+ def git_to_bzr(self, ref_name):
+ """Map a git reference name to a Bazaar branch name.
"""
- bazaar_names = {}
- for ref_name in sorted(ref_names):
- parts = ref_name.split('/')
- if parts[0] == 'refs':
+ parts = ref_name.split('/')
+ if parts[0] == 'refs':
+ parts.pop(0)
+ category = parts.pop(0)
+ if category == 'heads':
+ git_name = '/'.join(parts)
+ bazaar_name = self._git_to_bzr_name(git_name)
+ else:
+ if category == 'remotes' and parts[0] == 'origin':
parts.pop(0)
- category = parts.pop(0)
- if category == 'heads':
- git_name = '/'.join(parts)
- bazaar_name = self._git_to_bzr_name(git_name)
- else:
- if category == 'remotes' and parts[0] == 'origin':
- parts.pop(0)
- git_name = '/'.join(parts)
- if category.endswith('s'):
- category = category[:-1]
- name_no_ext = self._git_to_bzr_name(git_name)
- bazaar_name = "%s.%s" % (name_no_ext, category)
- bazaar_names[ref_name] = bazaar_name
- return bazaar_names
+ git_name = '/'.join(parts)
+ if category.endswith('s'):
+ category = category[:-1]
+ name_no_ext = self._git_to_bzr_name(git_name)
+ bazaar_name = "%s.%s" % (name_no_ext, category)
+ return bazaar_name
def _git_to_bzr_name(self, git_name):
+ # Make a simple name more bzr-like, by mapping git 'master' to bzr 'trunk'.
+ # To avoid collision, map git 'trunk' to bzr 'git-trunk'. Likewise
+ # 'git-trunk' to 'git-git-trunk' and so on, such that the mapping is
+ # one-to-one in both directions.
if git_name == 'master':
bazaar_name = 'trunk'
- elif git_name.endswith('trunk'):
+ elif self._GIT_TRUNK_RE.match(git_name):
bazaar_name = 'git-%s' % (git_name,)
else:
bazaar_name = git_name
return bazaar_name
-
- def bzr_to_git(self, branch_names):
- """Get the mapping from Bazaar branch names to git reference names.
-
- :return: a dictionary with Bazaar branch names as keys and
- the git reference names as values.
- """
- raise NotImplementedError(self.bzr_to_git)
diff --git a/branch_updater.py b/branch_updater.py
index 004f279..c19cfef 100644
--- a/branch_updater.py
+++ b/branch_updater.py
@@ -21,7 +21,6 @@ from operator import itemgetter
from bzrlib import bzrdir, errors, osutils, transport
from bzrlib.trace import error, note
-import branch_mapper
import helpers
@@ -40,7 +39,6 @@ class BranchUpdater(object):
self.heads_by_ref = heads_by_ref
self.last_ref = last_ref
self.tags = tags
- self.name_mapper = branch_mapper.BranchMapper()
self._branch_format = \
helpers.best_format_for_objects_in_a_repository(repo)
@@ -84,7 +82,9 @@ class BranchUpdater(object):
# Convert the reference names into Bazaar speak. If we haven't
# already put the 'trunk' first, do it now.
- git_to_bzr_map = self.name_mapper.git_to_bzr(ref_names)
+ git_to_bzr_map = {}
+ for ref_name in ref_names:
+ git_to_bzr_map[ref_name] = self.cache_mgr.branch_mapper.git_to_bzr(ref_name)
if ref_names and self.branch is None:
trunk = self.select_trunk(ref_names)
git_bzr_items = [(trunk, git_to_bzr_map[trunk])]
diff --git a/bzr_commit_handler.py b/bzr_commit_handler.py
index 31dd578..0114b82 100644
--- a/bzr_commit_handler.py
+++ b/bzr_commit_handler.py
@@ -218,6 +218,9 @@ class GenericCommitHandler(processor.CommitHandler):
def build_revision(self):
rev_props = self._legal_revision_properties(self.command.properties)
+ if 'branch-nick' not in rev_props:
+ rev_props['branch-nick'] = self.cache_mgr.branch_mapper.git_to_bzr(
+ self.branch_ref)
self._save_author_info(rev_props)
committer = self.command.committer
who = self._format_name_email(committer[0], committer[1])
diff --git a/cache_manager.py b/cache_manager.py
index 3ecfddc..27c14e4 100644
--- a/cache_manager.py
+++ b/cache_manager.py
@@ -18,7 +18,7 @@
from bzrlib import lru_cache, trace
-from bzrlib.plugins.fastimport import helpers
+from bzrlib.plugins.fastimport import branch_mapper, helpers
class CacheManager(object):
@@ -66,6 +66,10 @@ class CacheManager(object):
# info not in file - possible when no blobs used
pass
+ # BranchMapper has no state (for now?), but we keep it around rather
+ # than reinstantiate on every usage
+ self.branch_mapper = branch_mapper.BranchMapper()
+
def dump_stats(self, note=trace.note):
"""Dump some statistics about what we cached."""
# TODO: add in inventory stastistics
diff --git a/tests/test_branch_mapper.py b/tests/test_branch_mapper.py
index 0a50eec..00450c9 100644
--- a/tests/test_branch_mapper.py
+++ b/tests/test_branch_mapper.py
@@ -27,62 +27,38 @@ class TestBranchMapper(tests.TestCase):
def test_git_to_bzr(self):
m = branch_mapper.BranchMapper()
- git_refs = [
- 'refs/heads/master',
- 'refs/heads/foo',
- 'refs/tags/master',
- 'refs/tags/foo',
- 'refs/remotes/origin/master',
- 'refs/remotes/origin/foo',
- ]
- git_to_bzr_map = m.git_to_bzr(git_refs)
- self.assertEqual(git_to_bzr_map, {
+ for git, bzr in {
'refs/heads/master': 'trunk',
'refs/heads/foo': 'foo',
'refs/tags/master': 'trunk.tag',
'refs/tags/foo': 'foo.tag',
'refs/remotes/origin/master': 'trunk.remote',
'refs/remotes/origin/foo': 'foo.remote',
- })
+ }.items():
+ self.assertEqual(m.git_to_bzr(git), bzr)
def test_git_to_bzr_with_slashes(self):
m = branch_mapper.BranchMapper()
- git_refs = [
- 'refs/heads/master/slave',
- 'refs/heads/foo/bar',
- 'refs/tags/master/slave',
- 'refs/tags/foo/bar',
- 'refs/remotes/origin/master/slave',
- 'refs/remotes/origin/foo/bar',
- ]
- git_to_bzr_map = m.git_to_bzr(git_refs)
- self.assertEqual(git_to_bzr_map, {
+ for git, bzr in {
'refs/heads/master/slave': 'master/slave',
'refs/heads/foo/bar': 'foo/bar',
'refs/tags/master/slave': 'master/slave.tag',
'refs/tags/foo/bar': 'foo/bar.tag',
'refs/remotes/origin/master/slave': 'master/slave.remote',
'refs/remotes/origin/foo/bar': 'foo/bar.remote',
- })
+ }.items():
+ self.assertEqual(m.git_to_bzr(git), bzr)
def test_git_to_bzr_for_trunk(self):
# As 'master' in git is mapped to trunk in bzr, we need to handle
# 'trunk' in git in a sensible way.
m = branch_mapper.BranchMapper()
- git_refs = [
- 'refs/heads/trunk',
- 'refs/tags/trunk',
- 'refs/remotes/origin/trunk',
- 'refs/heads/git-trunk',
- 'refs/tags/git-trunk',
- 'refs/remotes/origin/git-trunk',
- ]
- git_to_bzr_map = m.git_to_bzr(git_refs)
- self.assertEqual(git_to_bzr_map, {
+ for git, bzr in {
'refs/heads/trunk': 'git-trunk',
'refs/tags/trunk': 'git-trunk.tag',
'refs/remotes/origin/trunk': 'git-trunk.remote',
'refs/heads/git-trunk': 'git-git-trunk',
'refs/tags/git-trunk': 'git-git-trunk.tag',
'refs/remotes/origin/git-trunk':'git-git-trunk.remote',
- })
+ }.items():
+ self.assertEqual(m.git_to_bzr(git), bzr)