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 __future__ import absolute_import
21
from collections import defaultdict
31
def parse_git_svn_id(text):
32
"""Parse a git svn id string.
34
:param text: git svn id
35
:return: URL, revision number, uuid
37
(head, uuid) = text.rsplit(" ", 1)
38
(full_url, rev) = head.rsplit("@", 1)
39
return (full_url.encode("utf-8"), int(rev), uuid.encode("utf-8"))
42
class SubversionBranchUrlFinder(object):
45
self._roots = defaultdict(set)
47
def find_root(self, uuid, url):
48
for root in self._roots[uuid]:
49
if url.startswith(root):
52
from subvertpy.ra import RemoteAccess
56
root = c.get_repos_root()
57
self._roots[uuid].add(root)
60
def find_branch_path(self, uuid, url):
61
root = self.find_root(uuid, url)
64
assert url.startswith(root)
65
return url[len(root):].strip("/")
68
svn_branch_path_finder = SubversionBranchUrlFinder()
71
def _extract_converted_from_revid(rev):
72
if "converted-from" not in rev.properties:
75
for line in rev.properties.get("converted-from", "").splitlines():
76
(kind, serialized_foreign_revid) = line.split(" ", 1)
77
yield (kind, serialized_foreign_revid)
80
def _extract_cscvs(rev):
81
"""Older-style launchpad-cscvs import."""
82
if "cscvs-svn-branch-path" not in rev.properties:
86
rev.properties["cscvs-svn-repository-uuid"],
87
rev.properties["cscvs-svn-revision-number"],
88
urlutils.quote(rev.properties["cscvs-svn-branch-path"].strip("/"))))
91
def _extract_git_svn_id(rev):
92
if "git-svn-id" not in rev.properties:
94
(full_url, revnum, uuid) = parse_git_svn_id(rev.properties['git-svn-id'])
95
branch_path = svn_branch_path_finder.find_branch_path(uuid, full_url)
96
if branch_path is not None:
97
yield ("svn", "%s:%d:%s" % (uuid, revnum, urlutils.quote(branch_path)))
100
def _extract_foreign_revision(rev):
101
# Perhaps 'rev' is a foreign revision ?
102
if getattr(rev, "foreign_revid", None) is not None:
103
yield ("svn", rev.mapping.vcs.serialize_foreign_revid(rev.foreign_revid))
106
def _extract_foreign_revid(rev):
107
# Try parsing the revision id
109
foreign_revid, mapping = \
110
foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id)
111
except errors.InvalidRevisionId:
115
mapping.vcs.abbreviation,
116
mapping.vcs.serialize_foreign_revid(foreign_revid))
119
def _extract_debian_md5sum(rev):
120
if 'deb-md5' in rev.properties:
121
yield ("debian-md5sum", rev.properties["deb-md5"])
124
_foreign_revid_extractors = [
125
_extract_converted_from_revid,
128
_extract_foreign_revision,
129
_extract_foreign_revid,
130
_extract_debian_md5sum,
134
def extract_foreign_revids(rev):
135
"""Find ids of semi-equivalent revisions in foreign VCS'es.
137
:param: Bazaar revision object
138
:return: Set with semi-equivalent revisions.
141
for extractor in _foreign_revid_extractors:
142
ret.update(extractor(rev))
146
def find_pseudonyms(repository, revids):
147
"""Find revisions that are pseudonyms of each other.
149
:param repository: Repository object
150
:param revids: Sequence of revision ids to check
151
:return: Iterable over sets of pseudonyms
153
# Where have foreign revids ended up?
154
conversions = defaultdict(set)
155
# What are native revids conversions of?
156
conversion_of = defaultdict(set)
157
revs = repository.get_revisions(revids)
158
pb = ui.ui_factory.nested_progress_bar()
160
for i, rev in enumerate(revs):
161
pb.update("finding pseudonyms", i, len(revs))
162
for foreign_revid in extract_foreign_revids(rev):
163
conversion_of[rev.revision_id].add(foreign_revid)
164
conversions[foreign_revid].add(rev.revision_id)
168
for foreign_revid in conversions.keys():
170
check = set(conversions[foreign_revid])
174
for frevid in conversion_of[x]:
175
extra.update(conversions[frevid])
176
del conversions[frevid]
184
def pseudonyms_as_dict(l):
185
"""Convert an iterable over pseudonyms to a dictionary.
187
:param l: Iterable over sets of pseudonyms
188
:return: Dictionary with pseudonyms for each revid.
193
ret[pn] = pns - set([pn])
197
def generate_rebase_map_from_pseudonyms(pseudonym_dict, existing, desired):
198
"""Create a rebase map from pseudonyms and existing/desired ancestry.
200
:param pseudonym_dict: Dictionary with pseudonym as returned by
202
:param existing: Existing ancestry, might need to be rebased
203
:param desired: Desired ancestry
204
:return: rebase map, as dictionary
207
for revid in existing:
208
for pn in pseudonym_dict.get(revid, []):
210
rebase_map[revid] = pn