p4.py
369 lines
| 12.2 KiB
| text/x-python
|
PythonLexer
Frank Kingswood
|
r7823 | # Perforce source for convert extension. | ||
# | ||||
# Copyright 2009, Frank Kingswood <frank@kingswood-consulting.co.uk> | ||||
# | ||||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
timeless
|
r28371 | from __future__ import absolute_import | ||
Frank Kingswood
|
r7823 | |||
timeless
|
r28371 | import marshal | ||
import re | ||||
Yuya Nishihara
|
r29205 | from mercurial.i18n import _ | ||
timeless
|
r28371 | from mercurial import ( | ||
error, | ||||
util, | ||||
) | ||||
Frank Kingswood
|
r7823 | |||
timeless
|
r28371 | from . import common | ||
Frank Kingswood
|
r7823 | |||
def loaditer(f): | ||||
"Yield the dictionary objects generated by p4" | ||||
try: | ||||
while True: | ||||
d = marshal.load(f) | ||||
if not d: | ||||
break | ||||
yield d | ||||
except EOFError: | ||||
pass | ||||
Eugene Baranov
|
r25788 | def decodefilename(filename): | ||
"""Perforce escapes special characters @, #, *, or % | ||||
with %40, %23, %2A, or %25 respectively | ||||
Yuya Nishihara
|
r34133 | >>> decodefilename(b'portable-net45%252Bnetcore45%252Bwp8%252BMonoAndroid') | ||
Eugene Baranov
|
r25788 | 'portable-net45%2Bnetcore45%2Bwp8%2BMonoAndroid' | ||
Yuya Nishihara
|
r34133 | >>> decodefilename(b'//Depot/Directory/%2525/%2523/%23%40.%2A') | ||
Eugene Baranov
|
r25788 | '//Depot/Directory/%25/%23/#@.*' | ||
""" | ||||
replacements = [('%2A', '*'), ('%23', '#'), ('%40', '@'), ('%25', '%')] | ||||
for k, v in replacements: | ||||
filename = filename.replace(k, v) | ||||
return filename | ||||
timeless
|
r28371 | class p4_source(common.converter_source): | ||
Durham Goode
|
r25748 | def __init__(self, ui, path, revs=None): | ||
super(p4_source, self).__init__(ui, path, revs=revs) | ||||
Frank Kingswood
|
r7823 | |||
Frank Kingswood
|
r8829 | if "/" in path and not path.startswith('//'): | ||
timeless
|
r28371 | raise common.NoRepo(_('%s does not look like a P4 repository') % | ||
path) | ||||
Matt Mackall
|
r7973 | |||
timeless
|
r28371 | common.checktool('p4', abort=False) | ||
Frank Kingswood
|
r7823 | |||
David Soria Parra
|
r30601 | self.revmap = {} | ||
Boris Feld
|
r34504 | self.encoding = self.ui.config('convert', 'p4.encoding') | ||
Matt Mackall
|
r10282 | self.re_type = re.compile( | ||
"([a-z]+)?(text|binary|symlink|apple|resource|unicode|utf\d+)" | ||||
"(\+\w+)?$") | ||||
self.re_keywords = re.compile( | ||||
r"\$(Id|Header|Date|DateTime|Change|File|Revision|Author)" | ||||
r":[^$\n]*\$") | ||||
Frank Kingswood
|
r8829 | self.re_keywords_old = re.compile("\$(Id|Header):[^$\n]*\$") | ||
Frank Kingswood
|
r7823 | |||
Durham Goode
|
r25748 | if revs and len(revs) > 1: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("p4 source does not support specifying " | ||
Durham Goode
|
r25748 | "multiple revisions")) | ||
Frank Kingswood
|
r7823 | |||
David Soria Parra
|
r30601 | def setrevmap(self, revmap): | ||
"""Sets the parsed revmap dictionary. | ||||
Revmap stores mappings from a source revision to a target revision. | ||||
It is set in convertcmd.convert and provided by the user as a file | ||||
on the commandline. | ||||
Revisions in the map are considered beeing present in the | ||||
repository and ignored during _parse(). This allows for incremental | ||||
imports if a revmap is provided. | ||||
""" | ||||
self.revmap = revmap | ||||
Frank Kingswood
|
r7823 | def _parse_view(self, path): | ||
"Read changes affecting the path" | ||||
Frank Kingswood
|
r11347 | cmd = 'p4 -G changes -s submitted %s' % util.shellquote(path) | ||
Peter Ingebretson
|
r9474 | stdout = util.popen(cmd, mode='rb') | ||
David Soria Parra
|
r30629 | p4changes = {} | ||
Frank Kingswood
|
r7823 | for d in loaditer(stdout): | ||
c = d.get("change", None) | ||||
if c: | ||||
David Soria Parra
|
r30629 | p4changes[c] = True | ||
return p4changes | ||||
Frank Kingswood
|
r7823 | |||
def _parse(self, ui, path): | ||||
"Prepare list of P4 filenames and revisions to import" | ||||
David Soria Parra
|
r30631 | p4changes = {} | ||
changeset = {} | ||||
files_map = {} | ||||
copies_map = {} | ||||
localname = {} | ||||
depotname = {} | ||||
heads = [] | ||||
Frank Kingswood
|
r7823 | ui.status(_('reading p4 views\n')) | ||
# read client spec or view | ||||
if "/" in path: | ||||
David Soria Parra
|
r30631 | p4changes.update(self._parse_view(path)) | ||
Frank Kingswood
|
r7823 | if path.startswith("//") and path.endswith("/..."): | ||
views = {path[:-3]:""} | ||||
else: | ||||
views = {"//": ""} | ||||
else: | ||||
Frank Kingswood
|
r11347 | cmd = 'p4 -G client -o %s' % util.shellquote(path) | ||
Peter Ingebretson
|
r9474 | clientspec = marshal.load(util.popen(cmd, mode='rb')) | ||
Dirkjan Ochtman
|
r7869 | |||
Frank Kingswood
|
r7823 | views = {} | ||
for client in clientspec: | ||||
if client.startswith("View"): | ||||
sview, cview = clientspec[client].split() | ||||
David Soria Parra
|
r30631 | p4changes.update(self._parse_view(sview)) | ||
Frank Kingswood
|
r7823 | if sview.endswith("...") and cview.endswith("..."): | ||
sview = sview[:-3] | ||||
cview = cview[:-3] | ||||
cview = cview[2:] | ||||
cview = cview[cview.find("/") + 1:] | ||||
views[sview] = cview | ||||
# list of changes that affect our source files | ||||
David Soria Parra
|
r30631 | p4changes = p4changes.keys() | ||
p4changes.sort(key=int) | ||||
Frank Kingswood
|
r7823 | |||
# list with depot pathnames, longest first | ||||
vieworder = views.keys() | ||||
Martin Geisler
|
r9039 | vieworder.sort(key=len, reverse=True) | ||
Frank Kingswood
|
r7823 | |||
# handle revision limiting | ||||
Boris Feld
|
r34174 | startrev = self.ui.config('convert', 'p4.startrev') | ||
Frank Kingswood
|
r7823 | |||
# now read the full changelists to get the list of file revisions | ||||
ui.status(_('collecting p4 changelists\n')) | ||||
lastid = None | ||||
David Soria Parra
|
r30631 | for change in p4changes: | ||
David Soria Parra
|
r30597 | if startrev and int(change) < int(startrev): | ||
continue | ||||
if self.revs and int(change) > int(self.revs[0]): | ||||
continue | ||||
David Soria Parra
|
r30601 | if change in self.revmap: | ||
# Ignore already present revisions, but set the parent pointer. | ||||
lastid = change | ||||
continue | ||||
David Soria Parra
|
r30597 | |||
Frank Kingswood
|
r7823 | if lastid: | ||
parents = [lastid] | ||||
else: | ||||
parents = [] | ||||
Dirkjan Ochtman
|
r7869 | |||
David Soria Parra
|
r30603 | d = self._fetch_revision(change) | ||
c = self._construct_commit(d, parents) | ||||
David Soria Parra
|
r31590 | descarr = c.desc.splitlines(True) | ||
if len(descarr) > 0: | ||||
shortdesc = descarr[0].rstrip('\r\n') | ||||
else: | ||||
shortdesc = '**empty changelist description**' | ||||
David Soria Parra
|
r30603 | t = '%s %s' % (c.rev, repr(shortdesc)[1:-1]) | ||
ui.status(util.ellipsis(t, 80) + '\n') | ||||
Frank Kingswood
|
r7823 | |||
files = [] | ||||
Eugene Baranov
|
r25751 | copies = {} | ||
copiedfiles = [] | ||||
Frank Kingswood
|
r7823 | i = 0 | ||
while ("depotFile%d" % i) in d and ("rev%d" % i) in d: | ||||
oldname = d["depotFile%d" % i] | ||||
filename = None | ||||
for v in vieworder: | ||||
Eugene Baranov
|
r25776 | if oldname.lower().startswith(v.lower()): | ||
Eugene Baranov
|
r25788 | filename = decodefilename(views[v] + oldname[len(v):]) | ||
Frank Kingswood
|
r7823 | break | ||
if filename: | ||||
files.append((filename, d["rev%d" % i])) | ||||
David Soria Parra
|
r30631 | depotname[filename] = oldname | ||
Eugene Baranov
|
r25751 | if (d.get("action%d" % i) == "move/add"): | ||
copiedfiles.append(filename) | ||||
David Soria Parra
|
r30630 | localname[oldname] = filename | ||
Frank Kingswood
|
r7823 | i += 1 | ||
Eugene Baranov
|
r25751 | |||
# Collect information about copied files | ||||
for filename in copiedfiles: | ||||
David Soria Parra
|
r30631 | oldname = depotname[filename] | ||
Eugene Baranov
|
r25751 | |||
flcmd = 'p4 -G filelog %s' \ | ||||
% util.shellquote(oldname) | ||||
flstdout = util.popen(flcmd, mode='rb') | ||||
copiedfilename = None | ||||
for d in loaditer(flstdout): | ||||
copiedoldname = None | ||||
i = 0 | ||||
while ("change%d" % i) in d: | ||||
if (d["change%d" % i] == change and | ||||
d["action%d" % i] == "move/add"): | ||||
j = 0 | ||||
while ("file%d,%d" % (i, j)) in d: | ||||
if d["how%d,%d" % (i, j)] == "moved from": | ||||
copiedoldname = d["file%d,%d" % (i, j)] | ||||
break | ||||
j += 1 | ||||
i += 1 | ||||
David Soria Parra
|
r30630 | if copiedoldname and copiedoldname in localname: | ||
copiedfilename = localname[copiedoldname] | ||||
Eugene Baranov
|
r25751 | break | ||
if copiedfilename: | ||||
copies[filename] = copiedfilename | ||||
else: | ||||
ui.warn(_("cannot find source for copied file: %s@%s\n") | ||||
% (filename, change)) | ||||
David Soria Parra
|
r30631 | changeset[change] = c | ||
files_map[change] = files | ||||
copies_map[change] = copies | ||||
Frank Kingswood
|
r7823 | lastid = change | ||
Dirkjan Ochtman
|
r7869 | |||
David Soria Parra
|
r30631 | if lastid and len(changeset) > 0: | ||
heads = [lastid] | ||||
return { | ||||
'changeset': changeset, | ||||
'files': files_map, | ||||
'copies': copies_map, | ||||
'heads': heads, | ||||
'depotname': depotname, | ||||
} | ||||
David Soria Parra
|
r30632 | @util.propertycache | ||
def _parse_once(self): | ||||
return self._parse(self.ui, self.path) | ||||
@util.propertycache | ||||
def copies(self): | ||||
return self._parse_once['copies'] | ||||
@util.propertycache | ||||
def files(self): | ||||
return self._parse_once['files'] | ||||
@util.propertycache | ||||
def changeset(self): | ||||
return self._parse_once['changeset'] | ||||
@util.propertycache | ||||
def heads(self): | ||||
return self._parse_once['heads'] | ||||
@util.propertycache | ||||
def depotname(self): | ||||
return self._parse_once['depotname'] | ||||
Frank Kingswood
|
r7823 | |||
def getheads(self): | ||||
return self.heads | ||||
def getfile(self, name, rev): | ||||
Martin Geisler
|
r11348 | cmd = 'p4 -G print %s' \ | ||
% util.shellquote("%s#%s" % (self.depotname[name], rev)) | ||||
Frank Kingswood
|
r7823 | |||
Eugene Baranov
|
r25775 | lasterror = None | ||
while True: | ||||
stdout = util.popen(cmd, mode='rb') | ||||
mode = None | ||||
Eugene Baranov
|
r25882 | contents = [] | ||
Eugene Baranov
|
r25775 | keywords = None | ||
Frank Kingswood
|
r7823 | |||
Eugene Baranov
|
r25775 | for d in loaditer(stdout): | ||
code = d["code"] | ||||
data = d.get("data") | ||||
Frank Kingswood
|
r8829 | |||
Eugene Baranov
|
r25775 | if code == "error": | ||
# if this is the first time error happened | ||||
# re-attempt getting the file | ||||
if not lasterror: | ||||
lasterror = IOError(d["generic"], data) | ||||
# this will exit inner-most for-loop | ||||
break | ||||
else: | ||||
raise lasterror | ||||
Dirkjan Ochtman
|
r8843 | |||
Eugene Baranov
|
r25775 | elif code == "stat": | ||
action = d.get("action") | ||||
if action in ["purge", "delete", "move/delete"]: | ||||
return None, None | ||||
p4type = self.re_type.match(d["type"]) | ||||
if p4type: | ||||
mode = "" | ||||
flags = ((p4type.group(1) or "") | ||||
+ (p4type.group(3) or "")) | ||||
if "x" in flags: | ||||
mode = "x" | ||||
if p4type.group(2) == "symlink": | ||||
mode = "l" | ||||
if "ko" in flags: | ||||
keywords = self.re_keywords_old | ||||
elif "k" in flags: | ||||
keywords = self.re_keywords | ||||
Dirkjan Ochtman
|
r8843 | |||
Eugene Baranov
|
r25775 | elif code == "text" or code == "binary": | ||
Eugene Baranov
|
r25882 | contents.append(data) | ||
Eugene Baranov
|
r25775 | |||
lasterror = None | ||||
if not lasterror: | ||||
break | ||||
Frank Kingswood
|
r7823 | |||
if mode is None: | ||||
Mads Kiilerich
|
r22296 | return None, None | ||
Frank Kingswood
|
r7823 | |||
Eugene Baranov
|
r25882 | contents = ''.join(contents) | ||
Frank Kingswood
|
r8829 | if keywords: | ||
contents = keywords.sub("$\\1$", contents) | ||||
if mode == "l" and contents.endswith("\n"): | ||||
contents = contents[:-1] | ||||
Patrick Mezard
|
r11134 | return contents, mode | ||
Frank Kingswood
|
r7823 | |||
Mads Kiilerich
|
r22300 | def getchanges(self, rev, full): | ||
if full: | ||||
timeless@mozdev.org
|
r26779 | raise error.Abort(_("convert from p4 does not support --full")) | ||
Eugene Baranov
|
r25751 | return self.files[rev], self.copies[rev], set() | ||
Frank Kingswood
|
r7823 | |||
David Soria Parra
|
r30603 | def _construct_commit(self, obj, parents=None): | ||
""" | ||||
Constructs a common.commit object from an unmarshalled | ||||
`p4 describe` output | ||||
""" | ||||
desc = self.recode(obj.get("desc", "")) | ||||
date = (int(obj["time"]), 0) # timezone not set | ||||
if parents is None: | ||||
parents = [] | ||||
return common.commit(author=self.recode(obj["user"]), | ||||
date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'), | ||||
parents=parents, desc=desc, branch=None, rev=obj['change'], | ||||
extra={"p4": obj['change'], "convert_revision": obj['change']}) | ||||
def _fetch_revision(self, rev): | ||||
"""Return an output of `p4 describe` including author, commit date as | ||||
a dictionary.""" | ||||
cmd = "p4 -G describe -s %s" % rev | ||||
stdout = util.popen(cmd, mode='rb') | ||||
return marshal.load(stdout) | ||||
Frank Kingswood
|
r7823 | def getcommit(self, rev): | ||
David Soria Parra
|
r30604 | if rev in self.changeset: | ||
return self.changeset[rev] | ||||
elif rev in self.revmap: | ||||
d = self._fetch_revision(rev) | ||||
return self._construct_commit(d, parents=None) | ||||
raise error.Abort( | ||||
_("cannot find %s in the revmap or parsed changesets") % rev) | ||||
Frank Kingswood
|
r7823 | |||
def gettags(self): | ||||
David Soria Parra
|
r30599 | return {} | ||
Frank Kingswood
|
r7823 | |||
def getchangedfiles(self, rev, i): | ||||
Matt Mackall
|
r8209 | return sorted([x[0] for x in self.files[rev]]) | ||