Merge trivial patch from Gabriel Filion to cope with API changes for
[bzr-fastimport.git] / branch_mapper.py
blobacc37c9711943d118d8be36279e63e7ce103b8b0
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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """An object that maps git ref names to bzr branch names. Note that it is not
18 used to map git ref names to bzr tag names."""
21 import re
24 class BranchMapper(object):
25 _GIT_TRUNK_RE = re.compile('(?:git-)*trunk')
27 def git_to_bzr(self, ref_name):
28 """Map a git reference name to a Bazaar branch name.
29 """
30 parts = ref_name.split('/')
31 if parts[0] == 'refs':
32 parts.pop(0)
33 category = parts.pop(0)
34 if category == 'heads':
35 git_name = '/'.join(parts)
36 bazaar_name = self._git_to_bzr_name(git_name)
37 else:
38 if category == 'remotes' and parts[0] == 'origin':
39 parts.pop(0)
40 git_name = '/'.join(parts)
41 if category.endswith('s'):
42 category = category[:-1]
43 name_no_ext = self._git_to_bzr_name(git_name)
44 bazaar_name = "%s.%s" % (name_no_ext, category)
45 return bazaar_name
47 def _git_to_bzr_name(self, git_name):
48 # Make a simple name more bzr-like, by mapping git 'master' to bzr 'trunk'.
49 # To avoid collision, map git 'trunk' to bzr 'git-trunk'. Likewise
50 # 'git-trunk' to 'git-git-trunk' and so on, such that the mapping is
51 # one-to-one in both directions.
52 if git_name == 'master':
53 bazaar_name = 'trunk'
54 elif self._GIT_TRUNK_RE.match(git_name):
55 bazaar_name = 'git-%s' % (git_name,)
56 else:
57 bazaar_name = git_name
58 return bazaar_name