Use bzr-foreign VcsMapping function names for converting between bzr and svn revids.
[jelmer/subvertpy.git] / mapping4.py
1 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
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 3 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, see <http://www.gnu.org/licenses/>.
15
16 from bzrlib import errors
17
18 from bzrlib.plugins.svn import mapping
19
20 supported_features = set()
21
22
23 class BzrSvnMappingv4(mapping.BzrSvnMapping):
24     """Mapping between Subversion and Bazaar, introduced in bzr-svn 0.5.
25
26     Tries to use revision properties when possible.
27
28     TODO: Add variable with required features.
29     """
30     revid_prefix = "svn-v4"
31     upgrade_suffix = "-svn4"
32     experimental = True
33
34     def __init__(self):
35         self.name = "v4"
36         self.revprops = mapping.BzrSvnMappingRevProps()
37         self.fileprops = mapping.BzrSvnMappingFileProps(self.name)
38
39     @staticmethod
40     def supports_roundtripping():
41         return True
42
43     @classmethod
44     def revision_id_bzr_to_foreign(cls, revid):
45         assert isinstance(revid, str)
46
47         if not revid.startswith(cls.revid_prefix):
48             raise errors.InvalidRevisionId(revid, "")
49
50         try:
51             (version, uuid, branch_path, srevnum) = revid.split(":")
52         except ValueError:
53             raise errors.InvalidRevisionId(revid, "")
54
55         branch_path = mapping.unescape_svn_path(branch_path)
56
57         return (uuid, branch_path, int(srevnum), cls())
58
59     def revision_id_foreign_to_bzr(self, (uuid, revnum, path)):
60         return "svn-v4:%s:%s:%d" % (uuid, path, revnum)
61
62     def generate_file_id(self, uuid, revnum, branch, inv_path):
63         return "%d@%s:%s/%s" % (revnum, uuid, branch, inv_path.encode("utf-8"))
64
65     def is_branch(self, branch_path):
66         return True
67
68     def is_tag(self, tag_path):
69         return True
70
71     def __eq__(self, other):
72         return type(self) == type(other)
73
74     def get_rhs_parents(self, branch_path, svn_revprops, fileprops):
75         if svn_revprops.has_key(mapping.SVN_REVPROP_BZR_MAPPING_VERSION):
76             return self.revprops.get_rhs_parents(branch_path, svn_revprops, fileprops)
77         else:
78             return self.fileprops.get_rhs_parents(branch_path, svn_revprops, fileprops)
79
80     def get_revision_id(self, branch_path, revprops, fileprops):
81         if revprops.has_key(mapping.SVN_REVPROP_BZR_MAPPING_VERSION):
82             return self.revprops.get_revision_id(branch_path, revprops, fileprops)
83         else:
84             return self.fileprops.get_revision_id(branch_path, revprops, fileprops)
85
86     def import_text_parents(self, svn_revprops, fileprops):
87         if svn_revprops.has_key(mapping.SVN_REVPROP_BZR_TEXT_PARENTS):
88             return self.revprops.import_text_parents(svn_revprops, fileprops)
89         else:
90             return self.fileprops.import_text_parents(svn_revprops, fileprops)
91
92     def import_fileid_map(self, svn_revprops, fileprops):
93         if svn_revprops.has_key(mapping.SVN_REVPROP_BZR_MAPPING_VERSION):
94             return self.revprops.import_fileid_map(svn_revprops, fileprops)
95         else:
96             return self.fileprops.import_fileid_map(svn_revprops, fileprops)
97
98     def export_revision(self, can_use_custom_revprops, branch_root, timestamp, timezone, committer, revprops, revision_id, 
99                         revno, merges, fileprops):
100         if can_use_custom_revprops:
101             (svn_revprops, fileprops) = self.revprops.export_revision(can_use_custom_revprops, branch_root, timestamp, timezone, committer, 
102                                           revprops, revision_id, revno, merges, fileprops)
103             svn_revprops[mapping.SVN_REVPROP_BZR_MAPPING_VERSION] = "4"
104             return (svn_revprops, fileprops)
105         else:
106             return self.fileprops.export_revision(can_use_custom_revprops, branch_root, timestamp, timezone, committer, 
107                                       revprops, revision_id, revno, merges, fileprops)
108
109     def export_fileid_map(self, can_use_custom_revprops, fileids, revprops, fileprops):
110         if can_use_custom_revprops:
111             self.revprops.export_fileid_map(can_use_custom_revprops, fileids, revprops, fileprops)
112         else:
113             self.fileprops.export_fileid_map(can_use_custom_revprops, fileids, revprops, fileprops)
114
115     def export_text_parents(self, can_use_custom_revprops, text_parents, revprops, fileprops):
116         if can_use_custom_revprops:
117             self.revprops.export_text_parents(can_use_custom_revprops, text_parents, revprops, fileprops)
118         else:
119             self.fileprops.export_text_parents(can_use_custom_revprops, text_parents, revprops, fileprops)
120
121     def import_revision(self, svn_revprops, fileprops, uuid, branch, revnum, rev):
122         if svn_revprops.has_key(mapping.SVN_REVPROP_BZR_REQUIRED_FEATURES):
123             features = set(svn_revprops[mapping.SVN_REVPROP_BZR_REQUIRED_FEATURES].split(","))
124             assert features.issubset(supported_features), "missing feature: %r" % features.difference(supported_features)
125         if svn_revprops.has_key(mapping.SVN_REVPROP_BZR_MAPPING_VERSION):
126             self.revprops.import_revision(svn_revprops, fileprops, uuid, branch, revnum, rev)
127         else:
128             self.fileprops.import_revision(svn_revprops, fileprops, uuid, branch, revnum, rev)
129
130