f6d06707aca6e419c1b7d5f8bc9a3045d92b1c60
[jelmer/python-fastimport.git] / branch_mapper.py
1 # Copyright (C) 2009 Canonical Ltd
2 #
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.
7 #
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.
12 #
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
16
17 """An object that maps bzr branch names <-> git ref names."""
18
19
20 class BranchMapper(object):
21
22     def git_to_bzr(self, ref_names):
23         """Get the mapping from git reference names to Bazaar branch names.
24         
25         :return: a dictionary with git reference names as keys and
26           the Bazaar branch names as values.
27         """
28         bazaar_names = {}
29         for ref_name in sorted(ref_names):
30             parts = ref_name.split('/')
31             if parts[0] == 'refs':
32                 parts.pop(0)
33             category = parts.pop(0)
34             if category == 'heads':
35                 bazaar_name = self._git_to_bzr_name(parts[-1])
36             else:
37                 if category.endswith('s'):
38                     category = category[:-1]
39                 name_no_ext = self._git_to_bzr_name(parts[-1])
40                 bazaar_name = "%s.%s" % (name_no_ext, category)
41             bazaar_names[ref_name] = bazaar_name
42         return bazaar_names
43
44     def _git_to_bzr_name(self, git_name):
45         if git_name == 'master':
46             bazaar_name = 'trunk'
47         elif git_name.endswith('trunk'):
48             bazaar_name = 'git-%s' % (git_name,)
49         else:
50             bazaar_name = git_name
51         return bazaar_name
52
53     def bzr_to_git(self, branch_names):
54         """Get the mapping from Bazaar branch names to git reference names.
55         
56         :return: a dictionary with Bazaar branch names as keys and
57           the git reference names as values.
58         """
59         raise NotImplementedError(self.bzr_to_git)