none
      2
 ?÷     # common.py - common code for the convert extension
#
#  Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

import base64
import datetime
import os
import pickle
import re
import shlex
import subprocess

from mercurial.i18n import _
from mercurial.pycompat import open
from mercurial import (
    encoding,
    error,
    phases,
    pycompat,
    util,
)
from mercurial.utils import procutil

propertycache = util.propertycache


def _encodeornone(d):
    if d is None:
        return
    return d.encode('latin1')


class _shlexpy3proxy:
    def __init__(self, l):
        self._l = l

    def __iter__(self):
        return (_encodeornone(v) for v in self._l)

    def get_token(self):
        return _encodeornone(self._l.get_token())

    @property
    def infile(self):
        return self._l.infile or b'<unknown>'

    @property
    def lineno(self):
        return self._l.lineno


def shlexer(data=None, filepath=None, wordchars=None, whitespace=None):
    if data is None:
        data = open(filepath, b'r', encoding='latin1')
    else:
        if filepath is not None:
            raise error.ProgrammingError(
                b'shlexer only accepts data or filepath, not both'
            )
        data = data.decode('latin1')
    l = shlex.shlex(data, infile=filepath, posix=True)
    if whitespace is not None:
        l.whitespace_split = True
        l.whitespace += whitespace.decode('latin1')
    if wordchars is not None:
        l.wordchars += wordchars.decode('latin1')
    return _shlexpy3proxy(l)


def encodeargs(args):
    def encodearg(s):
        lines = base64.encodebytes(s)
        lines = [l.splitlines()[0] for l in pycompat.iterbytestr(lines)]
        return b''.join(lines)

    s = pickle.dumps(args)
    return encodearg(s)


def decodeargs(s):
    s = base64.decodebytes(s)
    return pickle.loads(s)


class MissingTool(Exception):
    pass


def checktool(exe, name=None, abort=True, debname=None):
    name = name or exe
    if not procutil.findexe(exe):
        if abort:
            exc = error.Abort
        else:
            exc = MissingTool
        raise exc(_(b'cannot find required "%s" tool') % name +
                  (debname and _(b' (try installing the %s package)') % debname or b''))


class NoRepo(Exception):
    pass


SKIPREV = b'SKIP'


class commit:
    def __init__(
        self,
        author,
        date,
        desc,
        parents,
        branch=None,
        rev=None,
        extra=None,
        sortkey=None,
        saverev=True,
        phase=phases.draft,
        optparents=None,
        ctx=None,
    ):
        self.author = author or b'unknown'
        self.date = date or b'0 0'
        self.desc = desc
        self.parents = parents  # will be converted and used as parents
        self.optparents = optparents or []  # will be used if already converted
        self.branch = branch
        self.rev = rev
        self.extra = extra or {}
        self.sortkey = sortkey
        self.saverev = saverev
        self.phase = phase
        self.ctx = ctx  # for hg to hg conversions


class converter_source:
    """Conversion source interface"""

    def __init__(self, ui, repotype, path=None, revs=None):
        """Initialize conversion source (or raise NoRepo("message")
        exception if path is not a valid repository)"""
        self.ui = ui
        self.path = path
        self.revs = revs
        self.repotype = repotype

        self.encoding = b'utf-8'

    def checkhexformat(self, revstr, mapname=b'splicemap'):
        """fails if revstr is not a 40 byte hex. mercurial and git both uses
        such format for their revision numbering
        """
        if not re.match(br'[0-9a-fA-F]{40,40}$', revstr):
            raise error.Abort(
                _(b'%s entry %s is not a valid revision identifier')
                % (mapname, revstr)
            )

    def before(self):
        pass

    def after(sel