Fix bzr-fastimport when used with newer versions of python-fastimport.(Jelmer Vernooij)
[bzr-fastimport.git] / branch_mapper.py
blob047fb3f8ee05656ca0a8abb307b1a1e1760f3e4d
1 # Copyright (C) 2009 Canonical Ltd
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """An object that maps git ref names to bzr branch names. Note that it is not
17 used to map git ref names to bzr tag names."""
20 import re
23 class BranchMapper(object):
24 _GIT_TRUNK_RE = re.compile('(?:git-)*trunk')
26 def git_to_bzr(self, ref_name):
27 """Map a git reference name to a Bazaar branch name.
28 """
29 parts = ref_name.split('/')
30 if parts[0] == 'refs':
31 parts.pop(0)
32 category = parts.pop(0)
33 if category == 'heads':
34 git_name = '/'.join(parts)
35 bazaar_name = self._git_to_bzr_name(git_name)
36 else:
37 if category == 'remotes' and parts[0] == 'origin':
38 parts.pop(0)
39 git_name = '/'.join(parts)
40 if category.endswith('s'):
41 category = category[:-1]
42 name_no_ext = self._git_to_bzr_name(git_name)
43 bazaar_name = "%s.%s" % (name_no_ext, category)
44 return bazaar_name
46 def _git_to_bzr_name(self, git_name):
47 # Make a simple name more bzr-like, by mapping git 'master' to bzr 'trunk'.
48 # To avoid collision, map git 'trunk' to bzr 'git-trunk'. Likewise
49 # 'git-trunk' to 'git-git-trunk' and so on, such that the mapping is
50 # one-to-one in both directions.
51 if git_name == 'master':
52 bazaar_name = 'trunk'
53 elif self._GIT_TRUNK_RE.match(git_name):
54 bazaar_name = 'git-%s' % (git_name,)
55 else:
56 bazaar_name = git_name
57 return bazaar_name