~maxb/bzr-fastimport/tag-on-fi-commit-to-tag-ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (C) 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# 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."""


class BranchMapper(object):

    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.
        """
        bazaar_names = {}
        for ref_name in sorted(ref_names):
            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)
                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

    def _git_to_bzr_name(self, git_name):
        if git_name == 'master':
            bazaar_name = 'trunk'
        elif git_name.endswith('trunk'):
            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)