2009-04-26 03:47:44 +04:00
|
|
|
# common.py - common code for the convert extension
|
|
|
|
#
|
|
|
|
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2016-03-02 19:26:35 +03:00
|
|
|
from __future__ import absolute_import
|
2009-04-26 03:47:44 +04:00
|
|
|
|
2016-03-02 19:26:35 +03:00
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
2016-05-14 08:03:12 +03:00
|
|
|
from mercurial.i18n import _
|
2016-03-02 19:26:35 +03:00
|
|
|
from mercurial import (
|
2017-08-23 03:03:07 +03:00
|
|
|
encoding,
|
2016-03-02 19:26:35 +03:00
|
|
|
error,
|
|
|
|
phases,
|
|
|
|
util,
|
|
|
|
)
|
2007-08-06 23:49:26 +04:00
|
|
|
|
2016-06-04 12:08:00 +03:00
|
|
|
pickle = util.pickle
|
2011-12-02 21:36:32 +04:00
|
|
|
propertycache = util.propertycache
|
|
|
|
|
2007-08-06 23:49:26 +04:00
|
|
|
def encodeargs(args):
|
|
|
|
def encodearg(s):
|
|
|
|
lines = base64.encodestring(s)
|
|
|
|
lines = [l.splitlines()[0] for l in lines]
|
|
|
|
return ''.join(lines)
|
2007-08-07 12:28:43 +04:00
|
|
|
|
2007-08-06 23:49:26 +04:00
|
|
|
s = pickle.dumps(args)
|
|
|
|
return encodearg(s)
|
|
|
|
|
|
|
|
def decodeargs(s):
|
|
|
|
s = base64.decodestring(s)
|
|
|
|
return pickle.loads(s)
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2010-01-25 09:05:27 +03:00
|
|
|
class MissingTool(Exception):
|
|
|
|
pass
|
2008-03-21 01:32:43 +03:00
|
|
|
|
|
|
|
def checktool(exe, name=None, abort=True):
|
2007-10-31 00:14:15 +03:00
|
|
|
name = name or exe
|
2011-05-08 22:35:46 +04:00
|
|
|
if not util.findexe(exe):
|
2015-03-14 00:00:06 +03:00
|
|
|
if abort:
|
2015-10-08 22:55:45 +03:00
|
|
|
exc = error.Abort
|
2015-03-14 00:00:06 +03:00
|
|
|
else:
|
|
|
|
exc = MissingTool
|
2008-03-21 01:32:43 +03:00
|
|
|
raise exc(_('cannot find required "%s" tool') % name)
|
2007-10-31 00:14:15 +03:00
|
|
|
|
2010-01-25 09:05:27 +03:00
|
|
|
class NoRepo(Exception):
|
|
|
|
pass
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2007-10-06 22:30:15 +04:00
|
|
|
SKIPREV = 'SKIP'
|
2007-10-05 06:21:37 +04:00
|
|
|
|
2007-06-11 07:08:47 +04:00
|
|
|
class commit(object):
|
2007-10-11 02:30:00 +04:00
|
|
|
def __init__(self, author, date, desc, parents, branch=None, rev=None,
|
2016-12-22 20:26:47 +03:00
|
|
|
extra=None, sortkey=None, saverev=True, phase=phases.draft,
|
2016-04-13 01:16:21 +03:00
|
|
|
optparents=None):
|
2008-02-04 02:03:46 +03:00
|
|
|
self.author = author or 'unknown'
|
|
|
|
self.date = date or '0 0'
|
2007-07-28 01:30:20 +04:00
|
|
|
self.desc = desc
|
2016-04-13 01:16:21 +03:00
|
|
|
self.parents = parents # will be converted and used as parents
|
|
|
|
self.optparents = optparents or [] # will be used if already converted
|
2007-07-27 00:34:36 +04:00
|
|
|
self.branch = branch
|
|
|
|
self.rev = rev
|
2016-12-22 20:26:47 +03:00
|
|
|
self.extra = extra or {}
|
2009-06-01 19:12:39 +04:00
|
|
|
self.sortkey = sortkey
|
2015-06-14 20:04:00 +03:00
|
|
|
self.saverev = saverev
|
convert: apply the appropriate phases to the destination (issue4165)
If the conversion doesn't change the hash, and the cset is public in the source,
it should be public in the destination. (This can happen if file remapping is
done that doesn't affect the initial commits.) This also propagates the secret
phase from the source, regardless of the hash, because presumably the content is
what is secret. Otherwise, the destination commit stays in the draft phase.
Maybe any draft cset with an unchanged hash should be changed to public, because
it has effectively been shared, but convert pretty strongly implies throwing
away (or at least readonly archiving) the source repo.
The change in the rollback output is because the name of the outer transaction
is now 'convert', which seems more accurate. Unfortunately, the memctx won't
indicate the hash prior to committing, so the proper phase can't be applied with
the commit.
The repo is already write locked in mercurial_sink.before().
2015-06-14 20:08:11 +03:00
|
|
|
self.phase = phase
|
2007-06-11 07:08:47 +04:00
|
|
|
|
|
|
|
class converter_source(object):
|
|
|
|
"""Conversion source interface"""
|
|
|
|
|
2015-07-08 20:27:43 +03:00
|
|
|
def __init__(self, ui, path=None, revs=None):
|
2007-06-11 07:08:47 +04:00
|
|
|
"""Initialize conversion source (or raise NoRepo("message")
|
|
|
|
exception if path is not a valid repository)"""
|
2007-07-05 23:08:48 +04:00
|
|
|
self.ui = ui
|
|
|
|
self.path = path
|
2015-07-08 20:27:43 +03:00
|
|
|
self.revs = revs
|
2007-07-05 23:08:48 +04:00
|
|
|
|
|
|
|
self.encoding = 'utf-8'
|
2007-07-05 23:24:26 +04:00
|
|
|
|
2014-01-21 21:34:55 +04:00
|
|
|
def checkhexformat(self, revstr, mapname='splicemap'):
|
2013-04-25 22:50:26 +04:00
|
|
|
""" fails if revstr is not a 40 byte hex. mercurial and git both uses
|
|
|
|
such format for their revision numbering
|
|
|
|
"""
|
2013-04-26 05:01:00 +04:00
|
|
|
if not re.match(r'[0-9a-fA-F]{40,40}$', revstr):
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('%s entry %s is not a valid revision'
|
2014-01-21 21:34:55 +04:00
|
|
|
' identifier') % (mapname, revstr))
|
2013-04-25 22:50:26 +04:00
|
|
|
|
2007-10-03 00:46:17 +04:00
|
|
|
def before(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after(self):
|
|
|
|
pass
|
|
|
|
|
2015-08-15 23:44:55 +03:00
|
|
|
def targetfilebelongstosource(self, targetfilename):
|
|
|
|
"""Returns true if the given targetfile belongs to the source repo. This
|
|
|
|
is useful when only a subdirectory of the target belongs to the source
|
|
|
|
repo."""
|
|
|
|
# For normal full repo converts, this is always True.
|
|
|
|
return True
|
|
|
|
|
2007-11-08 04:06:02 +03:00
|
|
|
def setrevmap(self, revmap):
|
|
|
|
"""set the map of already-converted revisions"""
|
2007-07-05 23:41:58 +04:00
|
|
|
pass
|
2007-06-11 07:08:47 +04:00
|
|
|
|
|
|
|
def getheads(self):
|
|
|
|
"""Return a list of this repository's heads"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
|
|
|
def getfile(self, name, rev):
|
2010-05-09 23:52:34 +04:00
|
|
|
"""Return a pair (data, mode) where data is the file content
|
|
|
|
as a string and mode one of '', 'x' or 'l'. rev is the
|
2014-08-27 00:03:32 +04:00
|
|
|
identifier returned by a previous call to getchanges().
|
|
|
|
Data is None if file is missing/deleted in rev.
|
2008-09-29 11:30:36 +04:00
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
convert: introduce --full for converting all files
Convert will normally only process files that were changed in a source
revision, apply the filemap, and record it has a change in the target
repository. (If it ends up not really changing anything, nothing changes.)
That means that _if_ the filemap is changed before continuing an incremental
convert, the change will only kick in when the files it affects are modified in
a source revision and thus processed.
With --full, convert will make a full conversion every time and process
all files in the source repo and remove target repo files that shouldn't be
there. Filemap changes will thus kick in on the first converted revision, no
matter what is changed.
This flag should in most cases not make any difference but will make convert
significantly slower.
Other names has been considered for this feature, such as "resync", "sync",
"checkunmodified", "all" or "allfiles", but I found that they were less obvious
and required more explanation than "full" and were harder to describe
consistently.
2014-08-27 00:03:32 +04:00
|
|
|
def getchanges(self, version, full):
|
2015-03-19 19:40:19 +03:00
|
|
|
"""Returns a tuple of (files, copies, cleanp2).
|
2008-09-29 11:30:36 +04:00
|
|
|
|
|
|
|
files is a sorted list of (filename, id) tuples for all files
|
2009-05-16 22:15:58 +04:00
|
|
|
changed between version and its first parent returned by
|
convert: introduce --full for converting all files
Convert will normally only process files that were changed in a source
revision, apply the filemap, and record it has a change in the target
repository. (If it ends up not really changing anything, nothing changes.)
That means that _if_ the filemap is changed before continuing an incremental
convert, the change will only kick in when the files it affects are modified in
a source revision and thus processed.
With --full, convert will make a full conversion every time and process
all files in the source repo and remove target repo files that shouldn't be
there. Filemap changes will thus kick in on the first converted revision, no
matter what is changed.
This flag should in most cases not make any difference but will make convert
significantly slower.
Other names has been considered for this feature, such as "resync", "sync",
"checkunmodified", "all" or "allfiles", but I found that they were less obvious
and required more explanation than "full" and were harder to describe
consistently.
2014-08-27 00:03:32 +04:00
|
|
|
getcommit(). If full, all files in that revision is returned.
|
|
|
|
id is the source revision id of the file.
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2007-08-05 23:03:27 +04:00
|
|
|
copies is a dictionary of dest: source
|
2015-03-19 19:40:19 +03:00
|
|
|
|
|
|
|
cleanp2 is the set of files filenames that are clean against p2.
|
|
|
|
(Files that are clean against p1 are already not in files (unless
|
|
|
|
full). This makes it possible to handle p2 clean files similarly.)
|
2007-08-05 23:03:27 +04:00
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
|
|
|
def getcommit(self, version):
|
|
|
|
"""Return the commit object for version"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2014-05-26 19:53:12 +04:00
|
|
|
def numcommits(self):
|
|
|
|
"""Return the number of commits in this source.
|
|
|
|
|
|
|
|
If unknown, return None.
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
2007-06-11 07:08:47 +04:00
|
|
|
def gettags(self):
|
2009-06-22 01:17:05 +04:00
|
|
|
"""Return the tags as a dictionary of name: revision
|
|
|
|
|
|
|
|
Tag names must be UTF-8 strings.
|
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2007-07-01 23:58:08 +04:00
|
|
|
def recode(self, s, encoding=None):
|
|
|
|
if not encoding:
|
2007-07-05 23:08:48 +04:00
|
|
|
encoding = self.encoding or 'utf-8'
|
2007-07-21 12:30:51 +04:00
|
|
|
|
2007-09-07 18:14:51 +04:00
|
|
|
if isinstance(s, unicode):
|
|
|
|
return s.encode("utf-8")
|
2007-07-01 23:58:08 +04:00
|
|
|
try:
|
|
|
|
return s.decode(encoding).encode("utf-8")
|
2012-05-12 18:02:45 +04:00
|
|
|
except UnicodeError:
|
2007-07-01 23:58:08 +04:00
|
|
|
try:
|
|
|
|
return s.decode("latin-1").encode("utf-8")
|
2012-05-12 18:02:45 +04:00
|
|
|
except UnicodeError:
|
2007-07-01 23:58:08 +04:00
|
|
|
return s.decode(encoding, "replace").encode("utf-8")
|
|
|
|
|
2007-10-05 06:21:37 +04:00
|
|
|
def getchangedfiles(self, rev, i):
|
|
|
|
"""Return the files changed by rev compared to parent[i].
|
2007-12-29 21:49:48 +03:00
|
|
|
|
2007-10-05 06:21:37 +04:00
|
|
|
i is an index selecting one of the parents of rev. The return
|
|
|
|
value should be the list of files that are different in rev and
|
|
|
|
this parent.
|
|
|
|
|
|
|
|
If rev has no parents, i is None.
|
2007-12-29 21:49:48 +03:00
|
|
|
|
2007-10-05 06:21:37 +04:00
|
|
|
This function is only needed to support --filemap
|
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-10-05 06:21:37 +04:00
|
|
|
|
2007-11-27 04:23:20 +03:00
|
|
|
def converted(self, rev, sinkrev):
|
|
|
|
'''Notify the source that a revision has been converted.'''
|
|
|
|
pass
|
|
|
|
|
2009-06-01 19:12:39 +04:00
|
|
|
def hasnativeorder(self):
|
|
|
|
"""Return true if this source has a meaningful, native revision
|
|
|
|
order. For instance, Mercurial revisions are store sequentially
|
|
|
|
while there is no such global ordering with Darcs.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2013-03-23 21:06:52 +04:00
|
|
|
def hasnativeclose(self):
|
|
|
|
"""Return true if this source has ability to close branch.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2009-06-01 19:12:42 +04:00
|
|
|
def lookuprev(self, rev):
|
|
|
|
"""If rev is a meaningful revision reference in source, return
|
|
|
|
the referenced identifier in the same format used by getcommit().
|
|
|
|
return None otherwise.
|
|
|
|
"""
|
|
|
|
return None
|
2007-11-27 04:23:20 +03:00
|
|
|
|
2010-07-07 02:06:59 +04:00
|
|
|
def getbookmarks(self):
|
|
|
|
"""Return the bookmarks as a dictionary of name: revision
|
|
|
|
|
|
|
|
Bookmark names are to be UTF-8 strings.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
2014-01-21 21:34:55 +04:00
|
|
|
def checkrevformat(self, revstr, mapname='splicemap'):
|
2013-04-25 22:50:26 +04:00
|
|
|
"""revstr is a string that describes a revision in the given
|
|
|
|
source control system. Return true if revstr has correct
|
|
|
|
format.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
2007-06-11 07:08:47 +04:00
|
|
|
class converter_sink(object):
|
|
|
|
"""Conversion sink (target) interface"""
|
|
|
|
|
|
|
|
def __init__(self, ui, path):
|
|
|
|
"""Initialize conversion sink (or raise NoRepo("message")
|
2007-10-11 02:42:00 +04:00
|
|
|
exception if path is not a valid repository)
|
|
|
|
|
|
|
|
created is a list of paths to remove if a fatal error occurs
|
|
|
|
later"""
|
2007-10-11 02:30:00 +04:00
|
|
|
self.ui = ui
|
2007-10-11 02:42:00 +04:00
|
|
|
self.path = path
|
|
|
|
self.created = []
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2007-07-27 00:34:36 +04:00
|
|
|
def revmapfile(self):
|
2007-06-11 07:08:47 +04:00
|
|
|
"""Path to a file that will contain lines
|
|
|
|
source_rev_id sink_rev_id
|
|
|
|
mapping equivalent revision identifiers for each system."""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
2007-06-15 01:25:55 +04:00
|
|
|
def authorfile(self):
|
|
|
|
"""Path to a file that will contain lines
|
|
|
|
srcauthor=dstauthor
|
|
|
|
mapping equivalent authors identifiers for each system."""
|
2007-06-15 03:12:08 +04:00
|
|
|
return None
|
2007-06-15 01:25:55 +04:00
|
|
|
|
2015-03-19 19:40:19 +03:00
|
|
|
def putcommit(self, files, copies, parents, commit, source, revmap, full,
|
|
|
|
cleanp2):
|
2007-06-11 07:08:47 +04:00
|
|
|
"""Create a revision with all changed files listed in 'files'
|
2009-06-01 19:12:42 +04:00
|
|
|
and having listed parents. 'commit' is a commit object
|
|
|
|
containing at a minimum the author, date, and message for this
|
|
|
|
changeset. 'files' is a list of (path, version) tuples,
|
|
|
|
'copies' is a dictionary mapping destinations to sources,
|
|
|
|
'source' is the source repository, and 'revmap' is a mapfile
|
2010-05-09 23:52:34 +04:00
|
|
|
of source revisions to converted revisions. Only getfile() and
|
convert: introduce --full for converting all files
Convert will normally only process files that were changed in a source
revision, apply the filemap, and record it has a change in the target
repository. (If it ends up not really changing anything, nothing changes.)
That means that _if_ the filemap is changed before continuing an incremental
convert, the change will only kick in when the files it affects are modified in
a source revision and thus processed.
With --full, convert will make a full conversion every time and process
all files in the source repo and remove target repo files that shouldn't be
there. Filemap changes will thus kick in on the first converted revision, no
matter what is changed.
This flag should in most cases not make any difference but will make convert
significantly slower.
Other names has been considered for this feature, such as "resync", "sync",
"checkunmodified", "all" or "allfiles", but I found that they were less obvious
and required more explanation than "full" and were harder to describe
consistently.
2014-08-27 00:03:32 +04:00
|
|
|
lookuprev() should be called on 'source'. 'full' means that 'files'
|
|
|
|
is complete and all other files should be removed.
|
2015-03-19 19:40:19 +03:00
|
|
|
'cleanp2' is a set of the filenames that are unchanged from p2
|
|
|
|
(only in the common merge case where there two parents).
|
2008-06-19 02:14:24 +04:00
|
|
|
|
|
|
|
Note that the sink repository is not told to update itself to
|
|
|
|
a particular revision (or even what that revision would be)
|
|
|
|
before it receives the file data.
|
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-06-11 07:08:47 +04:00
|
|
|
|
|
|
|
def puttags(self, tags):
|
|
|
|
"""Put tags into sink.
|
2009-06-22 01:17:05 +04:00
|
|
|
|
|
|
|
tags: {tagname: sink_rev_id, ...} where tagname is an UTF-8 string.
|
2009-09-06 17:29:14 +04:00
|
|
|
Return a pair (tag_revision, tag_parent_revision), or (None, None)
|
|
|
|
if nothing was changed.
|
2009-06-22 01:17:05 +04:00
|
|
|
"""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2007-08-06 23:49:26 +04:00
|
|
|
|
2008-01-26 21:55:04 +03:00
|
|
|
def setbranch(self, branch, pbranches):
|
2008-06-19 02:14:24 +04:00
|
|
|
"""Set the current branch name. Called before the first putcommit
|
2007-08-16 00:21:23 +04:00
|
|
|
on the branch.
|
|
|
|
branch: branch name for subsequent commits
|
2008-01-26 21:55:04 +03:00
|
|
|
pbranches: (converted parent revision, parent branch) tuples"""
|
2007-08-16 00:21:23 +04:00
|
|
|
pass
|
convert: add a mode where mercurial_sink skips empty revisions.
The getchanges function of some converter_source classes can return
some false positives. I.e. they sometimes claim that a file "foo"
was changed in some revision, even though its contents are still the
same.
convert_svn is particularly bad, but I think this can also happen with
convert_cvs and, at least in theory, with mercurial_source.
For regular conversions this is not really a problem - as long as
getfile returns the right contents, we'll get a converted revision
with the right contents. But when we use --filemap, this could lead
to superfluous revisions being converted.
Instead of fixing every converter_source, I decided to change
mercurial_sink to work around this problem.
When --filemap is used, we're interested only in revisions that touch
some specific files. If a revision doesn't change any of these files,
then we're not interested in it (at least for revisions with a single
parent; merges are special).
For mercurial_sink, we abuse this property and rollback a commit if
the manifest text hasn't changed. This avoids duplicating the logic
from localrepo.filecommit to detect unchanged files.
2007-10-05 06:21:37 +04:00
|
|
|
|
|
|
|
def setfilemapmode(self, active):
|
|
|
|
"""Tell the destination that we're using a filemap
|
|
|
|
|
|
|
|
Some converter_sources (svn in particular) can claim that a file
|
|
|
|
was changed in a revision, even if there was no change. This method
|
|
|
|
tells the destination that we're using a filemap and that it should
|
|
|
|
filter empty revisions.
|
|
|
|
"""
|
|
|
|
pass
|
2007-11-08 04:06:02 +03:00
|
|
|
|
2007-11-08 04:40:39 +03:00
|
|
|
def before(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after(self):
|
|
|
|
pass
|
|
|
|
|
2010-07-07 02:06:59 +04:00
|
|
|
def putbookmarks(self, bookmarks):
|
|
|
|
"""Put bookmarks into sink.
|
|
|
|
|
|
|
|
bookmarks: {bookmarkname: sink_rev_id, ...}
|
|
|
|
where bookmarkname is an UTF-8 string.
|
|
|
|
"""
|
|
|
|
pass
|
2007-11-08 04:40:39 +03:00
|
|
|
|
2014-05-20 00:12:30 +04:00
|
|
|
def hascommitfrommap(self, rev):
|
|
|
|
"""Return False if a rev mentioned in a filemap is known to not be
|
|
|
|
present."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-05-20 00:11:14 +04:00
|
|
|
def hascommitforsplicemap(self, rev):
|
|
|
|
"""This method is for the special needs for splicemap handling and not
|
|
|
|
for general use. Returns True if the sink contains rev, aborts on some
|
|
|
|
special cases."""
|
2012-05-12 18:00:58 +04:00
|
|
|
raise NotImplementedError
|
2012-02-11 01:34:13 +04:00
|
|
|
|
2007-11-08 04:40:39 +03:00
|
|
|
class commandline(object):
|
|
|
|
def __init__(self, ui, command):
|
|
|
|
self.ui = ui
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
def prerun(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def postrun(self):
|
|
|
|
pass
|
|
|
|
|
2012-08-03 23:37:33 +04:00
|
|
|
def _cmdline(self, cmd, *args, **kwargs):
|
2007-11-08 04:40:39 +03:00
|
|
|
cmdline = [self.command, cmd] + list(args)
|
|
|
|
for k, v in kwargs.iteritems():
|
|
|
|
if len(k) == 1:
|
|
|
|
cmdline.append('-' + k)
|
|
|
|
else:
|
|
|
|
cmdline.append('--' + k.replace('_', '-'))
|
|
|
|
try:
|
|
|
|
if len(k) == 1:
|
|
|
|
cmdline.append('' + v)
|
|
|
|
else:
|
|
|
|
cmdline[-1] += '=' + v
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
cmdline = [util.shellquote(arg) for arg in cmdline]
|
2009-01-08 16:26:30 +03:00
|
|
|
if not self.ui.debugflag:
|
2012-08-04 09:14:40 +04:00
|
|
|
cmdline += ['2>', os.devnull]
|
2007-11-10 19:09:56 +03:00
|
|
|
cmdline = ' '.join(cmdline)
|
2008-01-11 06:07:43 +03:00
|
|
|
return cmdline
|
2007-11-08 04:40:39 +03:00
|
|
|
|
2008-01-11 06:07:43 +03:00
|
|
|
def _run(self, cmd, *args, **kwargs):
|
2012-08-03 23:37:33 +04:00
|
|
|
def popen(cmdline):
|
|
|
|
p = subprocess.Popen(cmdline, shell=True, bufsize=-1,
|
|
|
|
close_fds=util.closefds,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
return p
|
|
|
|
return self._dorun(popen, cmd, *args, **kwargs)
|
2011-03-24 23:54:09 +03:00
|
|
|
|
|
|
|
def _run2(self, cmd, *args, **kwargs):
|
2012-08-03 23:37:33 +04:00
|
|
|
return self._dorun(util.popen2, cmd, *args, **kwargs)
|
2011-03-24 23:54:09 +03:00
|
|
|
|
2016-03-23 03:05:11 +03:00
|
|
|
def _run3(self, cmd, *args, **kwargs):
|
|
|
|
return self._dorun(util.popen3, cmd, *args, **kwargs)
|
|
|
|
|
2012-08-03 23:37:33 +04:00
|
|
|
def _dorun(self, openfunc, cmd, *args, **kwargs):
|
|
|
|
cmdline = self._cmdline(cmd, *args, **kwargs)
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('running: %s\n' % (cmdline,))
|
2007-11-08 04:40:39 +03:00
|
|
|
self.prerun()
|
|
|
|
try:
|
2011-03-24 23:54:09 +03:00
|
|
|
return openfunc(cmdline)
|
2007-11-08 04:40:39 +03:00
|
|
|
finally:
|
|
|
|
self.postrun()
|
|
|
|
|
|
|
|
def run(self, cmd, *args, **kwargs):
|
2012-08-03 23:37:33 +04:00
|
|
|
p = self._run(cmd, *args, **kwargs)
|
|
|
|
output = p.communicate()[0]
|
2007-11-08 04:40:39 +03:00
|
|
|
self.ui.debug(output)
|
2012-08-03 23:37:33 +04:00
|
|
|
return output, p.returncode
|
2007-11-08 04:40:39 +03:00
|
|
|
|
2008-02-05 11:30:08 +03:00
|
|
|
def runlines(self, cmd, *args, **kwargs):
|
2012-08-03 23:37:33 +04:00
|
|
|
p = self._run(cmd, *args, **kwargs)
|
|
|
|
output = p.stdout.readlines()
|
|
|
|
p.wait()
|
2008-02-08 15:56:29 +03:00
|
|
|
self.ui.debug(''.join(output))
|
2012-08-03 23:37:33 +04:00
|
|
|
return output, p.returncode
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2007-11-08 04:40:39 +03:00
|
|
|
def checkexit(self, status, output=''):
|
|
|
|
if status:
|
|
|
|
if output:
|
|
|
|
self.ui.warn(_('%s error:\n') % self.command)
|
|
|
|
self.ui.warn(output)
|
2011-05-06 17:31:09 +04:00
|
|
|
msg = util.explainexit(status)[0]
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort('%s %s' % (self.command, msg))
|
2007-11-08 04:40:39 +03:00
|
|
|
|
|
|
|
def run0(self, cmd, *args, **kwargs):
|
|
|
|
output, status = self.run(cmd, *args, **kwargs)
|
|
|
|
self.checkexit(status, output)
|
|
|
|
return output
|
|
|
|
|
2008-02-05 11:30:08 +03:00
|
|
|
def runlines0(self, cmd, *args, **kwargs):
|
|
|
|
output, status = self.runlines(cmd, *args, **kwargs)
|
2008-02-08 15:56:29 +03:00
|
|
|
self.checkexit(status, ''.join(output))
|
2008-02-05 11:30:08 +03:00
|
|
|
return output
|
|
|
|
|
2011-12-02 21:36:32 +04:00
|
|
|
@propertycache
|
|
|
|
def argmax(self):
|
2008-01-11 06:07:43 +03:00
|
|
|
# POSIX requires at least 4096 bytes for ARG_MAX
|
2011-12-02 21:36:32 +04:00
|
|
|
argmax = 4096
|
2008-01-11 06:07:43 +03:00
|
|
|
try:
|
2011-12-02 21:36:32 +04:00
|
|
|
argmax = os.sysconf("SC_ARG_MAX")
|
2012-05-12 18:02:45 +04:00
|
|
|
except (AttributeError, ValueError):
|
2008-01-11 06:07:43 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
# Windows shells impose their own limits on command line length,
|
|
|
|
# down to 2047 bytes for cmd.exe under Windows NT/2k and 2500 bytes
|
|
|
|
# for older 4nt.exe. See http://support.microsoft.com/kb/830473 for
|
|
|
|
# details about cmd.exe limitations.
|
|
|
|
|
|
|
|
# Since ARG_MAX is for command line _and_ environment, lower our limit
|
|
|
|
# (and make happy Windows shells while doing this).
|
2012-01-08 21:15:54 +04:00
|
|
|
return argmax // 2 - 1
|
2008-01-11 06:07:43 +03:00
|
|
|
|
2012-08-03 23:05:01 +04:00
|
|
|
def _limit_arglist(self, arglist, cmd, *args, **kwargs):
|
|
|
|
cmdlen = len(self._cmdline(cmd, *args, **kwargs))
|
2011-12-02 21:36:32 +04:00
|
|
|
limit = self.argmax - cmdlen
|
2008-01-11 06:07:43 +03:00
|
|
|
bytes = 0
|
|
|
|
fl = []
|
|
|
|
for fn in arglist:
|
|
|
|
b = len(fn) + 3
|
|
|
|
if bytes + b < limit or len(fl) == 0:
|
|
|
|
fl.append(fn)
|
|
|
|
bytes += b
|
|
|
|
else:
|
|
|
|
yield fl
|
|
|
|
fl = [fn]
|
|
|
|
bytes = b
|
|
|
|
if fl:
|
|
|
|
yield fl
|
|
|
|
|
|
|
|
def xargs(self, arglist, cmd, *args, **kwargs):
|
2012-08-03 23:05:01 +04:00
|
|
|
for l in self._limit_arglist(arglist, cmd, *args, **kwargs):
|
2008-01-11 06:07:43 +03:00
|
|
|
self.run0(cmd, *(list(args) + l), **kwargs)
|
2007-11-08 04:06:02 +03:00
|
|
|
|
|
|
|
class mapfile(dict):
|
|
|
|
def __init__(self, ui, path):
|
|
|
|
super(mapfile, self).__init__()
|
|
|
|
self.ui = ui
|
|
|
|
self.path = path
|
|
|
|
self.fp = None
|
|
|
|
self.order = []
|
|
|
|
self._read()
|
|
|
|
|
|
|
|
def _read(self):
|
2009-02-11 23:47:57 +03:00
|
|
|
if not self.path:
|
2008-02-02 00:11:03 +03:00
|
|
|
return
|
2007-11-08 04:06:02 +03:00
|
|
|
try:
|
|
|
|
fp = open(self.path, 'r')
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as err:
|
2007-11-08 04:06:02 +03:00
|
|
|
if err.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
return
|
2016-11-15 02:17:15 +03:00
|
|
|
for i, line in enumerate(util.iterfile(fp)):
|
2012-02-28 13:06:35 +04:00
|
|
|
line = line.splitlines()[0].rstrip()
|
|
|
|
if not line:
|
|
|
|
# Ignore blank lines
|
|
|
|
continue
|
2009-04-11 23:15:27 +04:00
|
|
|
try:
|
2012-02-28 13:06:35 +04:00
|
|
|
key, value = line.rsplit(' ', 1)
|
2009-04-11 23:15:27 +04:00
|
|
|
except ValueError:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(
|
2010-01-25 09:05:27 +03:00
|
|
|
_('syntax error in %s(%d): key/value pair expected')
|
|
|
|
% (self.path, i + 1))
|
2007-11-08 04:06:02 +03:00
|
|
|
if key not in self:
|
|
|
|
self.order.append(key)
|
|
|
|
super(mapfile, self).__setitem__(key, value)
|
|
|
|
fp.close()
|
2007-12-29 21:49:48 +03:00
|
|
|
|
2007-11-08 04:06:02 +03:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if self.fp is None:
|
|
|
|
try:
|
|
|
|
self.fp = open(self.path, 'a')
|
2015-06-24 08:20:08 +03:00
|
|
|
except IOError as err:
|
2017-08-23 03:03:07 +03:00
|
|
|
raise error.Abort(
|
|
|
|
_('could not open map file %r: %s') %
|
|
|
|
(self.path, encoding.strtolocal(err.strerror)))
|
2007-11-08 04:06:02 +03:00
|
|
|
self.fp.write('%s %s\n' % (key, value))
|
|
|
|
self.fp.flush()
|
|
|
|
super(mapfile, self).__setitem__(key, value)
|
|
|
|
|
|
|
|
def close(self):
|
2007-11-08 04:40:39 +03:00
|
|
|
if self.fp:
|
|
|
|
self.fp.close()
|
|
|
|
self.fp = None
|
2012-02-11 01:25:49 +04:00
|
|
|
|
2012-11-19 02:26:50 +04:00
|
|
|
def makedatetimestamp(t):
|
|
|
|
"""Like util.makedate() but for time t instead of current time"""
|
|
|
|
delta = (datetime.datetime.utcfromtimestamp(t) -
|
|
|
|
datetime.datetime.fromtimestamp(t))
|
|
|
|
tz = delta.days * 86400 + delta.seconds
|
|
|
|
return t, tz
|