1
# Copyright (C) 2009 by Jelmer Vernooij <jelmer@samba.org>
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
"""Revision pseudonyms."""
19
from collections import defaultdict
29
def parse_git_svn_id(text):
30
"""Parse a git svn id string.
32
:param text: git svn id
33
:return: URL, revision number, uuid
35
(head, uuid) = text.rsplit(" ", 1)
36
(full_url, rev) = head.rsplit("@", 1)
37
return (full_url.encode("utf-8"), int(rev), uuid.encode("utf-8"))
40
class SubversionBranchUrlFinder(object):
43
self._roots = defaultdict(set)
45
def find_root(self, uuid, url):
46
for root in self._roots[uuid]:
47
if url.startswith(root):
50
from subvertpy.ra import RemoteAccess
54
root = c.get_repos_root()
55
self._roots[uuid].add(root)
58
def find_branch_path(self, uuid, url):
59
root = self.find_root(uuid, url)
62
assert url.startswith(root)
63
return url[len(root):].strip("/")
66
svn_branch_path_finder = SubversionBranchUrlFinder()
69
def _extract_converted_from_revid(rev):
70
if "converted-from" not in rev.properties:
73
for line in rev.properties.get("converted-from", "").splitlines():
74
(kind, serialized_foreign_revid) = line.split(" ", 1)
75
yield (kind, serialized_foreign_revid)
78
def _extract_cscvs(rev):
79
"""Older-style launchpad-cscvs import."""
80
if "cscvs-svn-branch-path" not in rev.properties:
84
rev.properties["cscvs-svn-repository-uuid"],
85
rev.properties["cscvs-svn-revision-number"],
86
urlutils.quote(rev.properties["cscvs-svn-branch-path"].strip("/"))))
89
def _extract_git_svn_id(rev):
90
if "git-svn-id" not in rev.properties:
92
(full_url, revnum, uuid) = parse_git_svn_id(rev.properties['git-svn-id'])
93
branch_path = svn_branch_path_finder.find_branch_path(uuid, full_url)
94
if branch_path is not None:
95
yield ("svn", "%s:%d:%s" % (uuid, revnum, urlutils.quote(branch_path)))
98
def _extract_foreign_revision(rev):
99
# Perhaps 'rev' is a foreign revision ?
100
if getattr(rev, "foreign_revid", None) is not None:
101
yield ("svn", rev.mapping.vcs.serialize_foreign_revid(rev.foreign_revid))
104
def _extract_foreign_revid(rev):
105
# Try parsing the revision id
107
foreign_revid, mapping = \
108
foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
109
except errors.InvalidRevisionId:
113
mapping.vcs.abbreviation,
114
mapping.vcs.serialize_foreign_revid(foreign_revid))
117
def _extract_debian_md5sum(rev):
118
if 'deb-md5' in rev.properties:
119
yield ("debian-md5sum", rev.properties["deb-md5"])
122
_foreign_revid_extractors = [
123
_extract_converted_from_revid,
126
_extract_foreign_revision,
127
_extract_foreign_revid,
128
_extract_debian_md5sum,
132
def extract_foreign_revids(rev):
133
"""Find ids of semi-equivalent revisions in foreign VCS'es.
135
:param: Bazaar revision object
136
:return: Set with semi-equivalent revisions.
139
for extractor in _foreign_revid_extractors:
140
ret.update(extractor(rev))
144
def find_pseudonyms(repository, revids):
145
"""Find revisions that are pseudonyms of each other.
147
:param repository: Repository object
148
:param revids: Sequence of revision ids to check
149
:return: Iterable over sets of pseudonyms
151
# Where have foreign revids ended up?
152
conversions = defaultdict(set)
153
# What are native revids conversions of?
154
conversion_of = defaultdict(set)
155
revs = repository.get_revisions(revids)
156
pb = ui.ui_factory.nested_progress_bar()
158
for i, rev in enumerate(revs):
159
pb.update("finding pseudonyms", i, len(revs))
160
for foreign_revid in extract_foreign_revids(rev):
161
conversion_of[rev.revision_id].add(foreign_revid)
162
conversions[foreign_revid].add(rev.revision_id)
166
for foreign_revid in conversions.keys():
168
check = set(conversions[foreign_revid])
172
for frevid in conversion_of[x]:
173
extra.update(conversions[frevid])
174
del conversions[frevid]
182
def pseudonyms_as_dict(l):
183
"""Convert an iterable over pseudonyms to a dictionary.
185
:param l: Iterable over sets of pseudonyms
186
:return: Dictionary with pseudonyms for each revid.
191
ret[pn] = pns - set([pn])
195
def generate_rebase_map_from_pseudonyms(pseudonym_dict, existing, desired):
196
"""Create a rebase map from pseudonyms and existing/desired ancestry.
198
:param pseudonym_dict: Dictionary with pseudonym as returned by
200
:param existing: Existing ancestry, might need to be rebased
201
:param desired: Desired ancestry
202
:return: rebase map, as dictionary
205
for revid in existing:
206
for pn in pseudonym_dict.get(revid, []):
208
rebase_map[revid] = pn