def recover(self, repo):
        '''commit working directory using journal metadata'''
        node, user, date, message, parents = self.readlog()
        merge = len(parents) == 2

        if not user or not date or not message or not parents[0]:
            raise util.Abort(_('transplant log file is corrupt'))

        extra = {'transplant_source': node}
        wlock = repo.wlock()
        try:
            p1, p2 = repo.dirstate.parents()
            if p1 != parents[0]:
                raise util.Abort(
                    _('working dir not at transplant parent %s') %
                                 revlog.hex(parents[0]))
            if merge:
                repo.dirstate.setparents(p1, parents[1])
            n = repo.commit(message, user, date, extra=extra)
            if not n:
                raise util.Abort(_('commit failed'))
            if not merge:
                self.transplants.set(n, node)
            self.unlog()

            return n, node
        finally:
            wlock.release()
    def __init__(self, ui, path, rev=None):
        converter_source.__init__(self, ui, path, rev=rev)
        commandline.__init__(self, ui, 'darcs')

        # check for _darcs, ElementTree so that we can easily skip
        # test-convert-darcs if ElementTree is not around
        if not os.path.exists(os.path.join(path, '_darcs')):
            raise NoRepo(_("%s does not look like a darcs repository") % path)

        checktool('darcs')
        version = self.run0('--version').splitlines()[0].strip()
        if version < '2.1':
            raise util.Abort(
                _('darcs version 2.1 or newer needed (found %r)') % version)

        if ElementTree is None:
            raise util.Abort(_("Python ElementTree module is not available"))

        self.path = os.path.realpath(path)

        self.lastrev = None
        self.changes = {}
        self.parents = {}
        self.tags = {}

        # Check darcs repository format
        format = self.format()
        if format:
            if format in ('darcs-1.0', 'hashed'):
                raise NoRepo(
                    _("%s repository format is unsupported, "
                      "please upgrade") % format)
        else:
            self.ui.warn(_('failed to detect repository format!'))
Example #3
0
def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    if hooktype not in ['pretxnchangegroup', 'pretxncommit']:
        raise util.Abort(
            _('config error - hook type "%s" cannot stop '
              'incoming changesets nor commits') % hooktype)
    if (hooktype == 'pretxnchangegroup'
            and source not in ui.config('acl', 'sources', 'serve').split()):
        ui.debug('acl: changes have source "%s" - skipping\n' % source)
        return

    user = None
    if source == 'serve' and 'url' in kwargs:
        url = kwargs['url'].split(':')
        if url[0] == 'remote' and url[1].startswith('http'):
            user = urllib.unquote(url[3])

    if user is None:
        user = getpass.getuser()

    cfg = ui.config('acl', 'config')
    if cfg:
        ui.readconfig(cfg,
                      sections=[
                          'acl.groups', 'acl.allow.branches',
                          'acl.deny.branches', 'acl.allow', 'acl.deny'
                      ])

    allowbranches = buildmatch(ui, None, user, 'acl.allow.branches')
    denybranches = buildmatch(ui, None, user, 'acl.deny.branches')
    allow = buildmatch(ui, repo, user, 'acl.allow')
    deny = buildmatch(ui, repo, user, 'acl.deny')

    for rev in xrange(repo[node], len(repo)):
        ctx = repo[rev]
        branch = ctx.branch()
        if denybranches and denybranches(branch):
            raise util.Abort(
                _('acl: user "%s" denied on branch "%s"'
                  ' (changeset "%s")') % (user, branch, ctx))
        if allowbranches and not allowbranches(branch):
            raise util.Abort(
                _('acl: user "%s" not allowed on branch "%s"'
                  ' (changeset "%s")') % (user, branch, ctx))
        ui.debug('acl: branch access granted: "%s" on branch "%s"\n' %
                 (ctx, branch))

        for f in ctx.files():
            if deny and deny(f):
                ui.debug('acl: user %s denied on %s\n' % (user, f))
                raise util.Abort(
                    _('acl: access denied for changeset %s') % ctx)
            if allow and not allow(f):
                ui.debug('acl: user %s not allowed on %s\n' % (user, f))
                raise util.Abort(
                    _('acl: access denied for changeset %s') % ctx)
        ui.debug('acl: allowing changeset %s\n' % ctx)
def _status(ui, repo, kwt, *pats, **opts):
    '''Bails out if [keyword] configuration is not active.
    Returns status of working directory.'''
    if kwt:
        return repo.status(match=cmdutil.match(repo, pats, opts),
                           clean=True,
                           unknown=opts.get('unknown') or opts.get('all'))
    if ui.configitems('keyword'):
        raise util.Abort(_('[keyword] patterns cannot match'))
    raise util.Abort(_('no [keyword] patterns configured'))
Example #5
0
    def getfile(self, name, rev):
        def chunkedread(fp, count):
            # file-objects returned by socked.makefile() do not handle
            # large read() requests very well.
            chunksize = 65536
            output = StringIO()
            while count > 0:
                data = fp.read(min(count, chunksize))
                if not data:
                    raise util.Abort(
                        _("%d bytes missing from remote file") % count)
                count -= len(data)
                output.write(data)
            return output.getvalue()

        self._parse()
        if rev.endswith("(DEAD)"):
            raise IOError

        args = ("-N -P -kk -r %s --" % rev).split()
        args.append(self.cvsrepo + '/' + name)
        for x in args:
            self.writep.write("Argument %s\n" % x)
        self.writep.write("Directory .\n%s\nco\n" % self.realroot)
        self.writep.flush()

        data = ""
        mode = None
        while 1:
            line = self.readp.readline()
            if line.startswith("Created ") or line.startswith("Updated "):
                self.readp.readline()  # path
                self.readp.readline()  # entries
                mode = self.readp.readline()[:-1]
                count = int(self.readp.readline()[:-1])
                data = chunkedread(self.readp, count)
            elif line.startswith(" "):
                data += line[1:]
            elif line.startswith("M "):
                pass
            elif line.startswith("Mbinary "):
                count = int(self.readp.readline()[:-1])
                data = chunkedread(self.readp, count)
            else:
                if line == "ok\n":
                    if mode is None:
                        raise util.Abort(_('malformed response from CVS'))
                    return (data, "x" in mode and "x" or "")
                elif line.startswith("E "):
                    self.ui.warn(_("cvs server: %s\n") % line[2:])
                elif line.startswith("Remove"):
                    self.readp.readline()
                else:
                    raise util.Abort(_("unknown CVS response: %s") % line)
def _kwfwrite(ui, repo, expand, *pats, **opts):
    '''Selects files and passes them to kwtemplater.overwrite.'''
    wctx = repo[None]
    if len(wctx.parents()) > 1:
        raise util.Abort(_('outstanding uncommitted merge'))
    kwt = kwtools['templater']
    wlock = repo.wlock()
    try:
        status = _status(ui, repo, kwt, *pats, **opts)
        modified, added, removed, deleted, unknown, ignored, clean = status
        if modified or added or removed or deleted:
            raise util.Abort(_('outstanding uncommitted changes'))
        kwt.overwrite(wctx, clean, True, expand)
    finally:
        wlock.release()
def defineparents(repo, rev, target, state, targetancestors):
    'Return the new parent relationship of the revision that will be rebased'
    parents = repo[rev].parents()
    p1 = p2 = nullrev

    P1n = parents[0].rev()
    if P1n in targetancestors:
        p1 = target
    elif P1n in state:
        if state[P1n] == nullmerge:
            p1 = target
        else:
            p1 = state[P1n]
    else:  # P1n external
        p1 = target
        p2 = P1n

    if len(parents) == 2 and parents[1].rev() not in targetancestors:
        P2n = parents[1].rev()
        # interesting second parent
        if P2n in state:
            if p1 == target:  # P1n in targetancestors or external
                p1 = state[P2n]
            else:
                p2 = state[P2n]
        else:  # P2n external
            if p2 != nullrev:  # P1n external too => rev is a merged revision
                raise util.Abort(
                    _('cannot use revision %d as base, result '
                      'would have 3 parents') % rev)
            p2 = P2n
    repo.ui.debug(" future parents are %d and %d\n" %
                  (repo[p1].rev(), repo[p2].rev()))
    return p1, p2
 def __init__(self, ui, path, rev=None):
     converter_source.__init__(self, ui, path, rev)
     self.ignoreerrors = ui.configbool('convert', 'hg.ignoreerrors', False)
     self.ignored = set()
     self.saverev = ui.configbool('convert', 'hg.saverev', False)
     try:
         self.repo = hg.repository(self.ui, path)
         # try to provoke an exception if this isn't really a hg
         # repo, but some other bogus compatible-looking url
         if not self.repo.local():
             raise error.RepoError()
     except error.RepoError:
         ui.traceback()
         raise NoRepo(_("%s is not a local Mercurial repository") % path)
     self.lastrev = None
     self.lastctx = None
     self._changescache = None
     self.convertfp = None
     # Restrict converted revisions to startrev descendants
     startnode = ui.config('convert', 'hg.startrev')
     if startnode is not None:
         try:
             startnode = self.repo.lookup(startnode)
         except error.RepoError:
             raise util.Abort(
                 _('%s is not a valid start revision') % startnode)
         startrev = self.repo.changelog.rev(startnode)
         children = {startnode: 1}
         for rev in self.repo.changelog.descendants(startrev):
             children[self.repo.changelog.node(rev)] = 1
         self.keep = children.__contains__
     else:
         self.keep = util.always
 def get_longdesc_id(self):
     '''get identity of longdesc field'''
     self.run('select id from fielddefs where name = "longdesc"')
     ids = self.cursor.fetchall()
     if len(ids) != 1:
         raise util.Abort(_('unknown database schema'))
     return ids[0][0]
    def notify(self, ids, committer):
        '''tell bugzilla to send mail.'''

        self.ui.status(_('telling bugzilla to send mail:\n'))
        (user, userid) = self.get_bugzilla_user(committer)
        for id in ids:
            self.ui.status(_('  bug %s\n') % id)
            cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify)
            bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla')
            try:
                # Backwards-compatible with old notify string, which
                # took one string. This will throw with a new format
                # string.
                cmd = cmdfmt % id
            except TypeError:
                cmd = cmdfmt % {'bzdir': bzdir, 'id': id, 'user': user}
            self.ui.note(_('running notify command %s\n') % cmd)
            fp = util.popen('(%s) 2>&1' % cmd)
            out = fp.read()
            ret = fp.close()
            if ret:
                self.ui.warn(out)
                raise util.Abort(_('bugzilla notify command %s') %
                                 util.explain_exit(ret)[0])
        self.ui.status(_('done\n'))
Example #11
0
 def _getlog(self,
             paths,
             start,
             end,
             limit=0,
             discover_changed_paths=True,
             strict_node_history=False):
     # Normalize path names, svn >= 1.5 only wants paths relative to
     # supplied URL
     relpaths = []
     for p in paths:
         if not p.startswith('/'):
             p = self.module + '/' + p
         relpaths.append(p.strip('/'))
     args = [
         self.baseurl, relpaths, start, end, limit, discover_changed_paths,
         strict_node_history
     ]
     arg = encodeargs(args)
     hgexe = util.hgexecutable()
     cmd = '%s debugsvnlog' % util.shellquote(hgexe)
     stdin, stdout = util.popen2(cmd)
     stdin.write(arg)
     try:
         stdin.close()
     except IOError:
         raise util.Abort(
             _('Mercurial failed to run itself, check'
               ' hg executable is in PATH'))
     return logstream(stdout)
Example #12
0
 def __setitem__(self, key, value):
     if self.fp is None:
         try:
             self.fp = open(self.path, 'a')
         except IOError, err:
             raise util.Abort(_('could not open map file %r: %s') %
                              (self.path, err.strerror))
Example #13
0
 def checkexit(self, status, output=''):
     if status:
         if output:
             self.ui.warn(_('%s error:\n') % self.command)
             self.ui.warn(output)
         msg = util.explain_exit(status)[0]
         raise util.Abort('%s %s' % (self.command, msg))
    def __init__(self, ui, path, rev=None):
        super(gnuarch_source, self).__init__(ui, path, rev=rev)

        if not os.path.exists(os.path.join(path, '{arch}')):
            raise NoRepo(_("%s does not look like a GNU Arch repository")
                         % path)

        # Could use checktool, but we want to check for baz or tla.
        self.execmd = None
        if util.find_exe('baz'):
            self.execmd = 'baz'
        else:
            if util.find_exe('tla'):
                self.execmd = 'tla'
            else:
                raise util.Abort(_('cannot find a GNU Arch tool'))

        commandline.__init__(self, ui, self.execmd)

        self.path = os.path.realpath(path)
        self.tmppath = None

        self.treeversion = None
        self.lastrev = None
        self.changes = {}
        self.parents = {}
        self.tags = {}
        self.catlogparser = Parser()
        self.encoding = encoding.encoding
        self.archives = []
Example #15
0
def pull(oldpull, ui, repo, source="default", **opts):
    # translate bookmark args to rev args for actual pull
    if opts.get('bookmark'):
        # this is an unpleasant hack as pull will do this internally
        source, branches = hg.parseurl(ui.expandpath(source),
                                       opts.get('branch'))
        other = hg.repository(hg.remoteui(repo, opts), source)
        rb = other.listkeys('bookmarks')

        for b in opts['bookmark']:
            if b not in rb:
                raise util.Abort(_('remote bookmark %s not found!') % b)
            opts.setdefault('rev', []).append(b)

    result = oldpull(ui, repo, source, **opts)

    # update specified bookmarks
    if opts.get('bookmark'):
        for b in opts['bookmark']:
            # explicit pull overrides local bookmark if any
            ui.status(_("importing bookmark %s\n") % b)
            repo._bookmarks[b] = repo[rb[b]].node()
        write(repo)

    return result
 def catfile(self, rev, type):
     if rev == hex(nullid):
         raise IOError()
     data, ret = self.gitread("git cat-file %s %s" % (type, rev))
     if ret:
         raise util.Abort(_('cannot read %r object at %s') % (type, rev))
     return data
 def checkopts(opts, revs):
     if opts.get('continue'):
         if opts.get('branch') or opts.get('all') or opts.get('merge'):
             raise util.Abort(_('--continue is incompatible with '
                                'branch, all or merge'))
         return
     if not (opts.get('source') or revs or
             opts.get('merge') or opts.get('branch')):
         raise util.Abort(_('no source URL, branch tag or revision '
                            'list provided'))
     if opts.get('all'):
         if not opts.get('branch'):
             raise util.Abort(_('--all requires a branch revision'))
         if revs:
             raise util.Abort(_('--all is incompatible with a '
                                'revision list'))
 def getchanges(self, version):
     self.modecache = {}
     fh = self.gitopen("git diff-tree -z --root -m -r %s" % version)
     changes = []
     seen = set()
     entry = None
     for l in fh.read().split('\x00'):
         if not entry:
             if not l.startswith(':'):
                 continue
             entry = l
             continue
         f = l
         if f not in seen:
             seen.add(f)
             entry = entry.split()
             h = entry[3]
             p = (entry[1] == "100755")
             s = (entry[1] == "120000")
             self.modecache[(f, h)] = (p and "x") or (s and "l") or ""
             changes.append((f, h))
         entry = None
     if fh.close():
         raise util.Abort(_('cannot read changes in %s') % version)
     return (changes, {})
def create_server(ui, app):

    if ui.config('web', 'certificate'):
        if sys.version_info >= (2, 6):
            handler = _httprequesthandlerssl
        else:
            handler = _httprequesthandleropenssl
    else:
        handler = _httprequesthandler

    if ui.configbool('web', 'ipv6'):
        cls = IPv6HTTPServer
    else:
        cls = MercurialHTTPServer

    # ugly hack due to python issue5853 (for threaded use)
    import mimetypes
    mimetypes.init()

    address = ui.config('web', 'address', '')
    port = util.getport(ui.config('web', 'port', 8000))
    try:
        return cls(ui, app, (address, port), handler)
    except socket.error, inst:
        raise util.Abort(
            _("cannot start server at '%s:%d': %s") %
            (address, port, inst.args[1]))
Example #20
0
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):
        if not self.path:
            return
        try:
            fp = open(self.path, 'r')
        except IOError, err:
            if err.errno != errno.ENOENT:
                raise
            return
        for i, line in enumerate(fp):
            try:
                key, value = line.splitlines()[0].rsplit(' ', 1)
            except ValueError:
                raise util.Abort(
                    _('syntax error in %s(%d): key/value pair expected')
                    % (self.path, i + 1))
            if key not in self:
                self.order.append(key)
            super(mapfile, self).__setitem__(key, value)
        fp.close()
    def send(self, ctx, count, data):
        '''send message.'''

        p = email.Parser.Parser()
        try:
            msg = p.parsestr(data)
        except email.Errors.MessageParseError, inst:
            raise util.Abort(inst)
Example #22
0
def extsetup(ui):
    try:
        extensions.find('win32text')
        raise util.Abort(
            _("the eol extension is incompatible with the "
              "win32text extension"))
    except KeyError:
        pass
Example #23
0
def _explain_watch_limit(ui, dirstate, rootabs):
    path = '/proc/sys/fs/inotify/max_user_watches'
    try:
        limit = int(file(path).read())
    except IOError, err:
        if err.errno != errno.ENOENT:
            raise
        raise util.Abort(_('this system does not seem to ' 'support inotify'))
 def getfile(self, name, rev):
     if rev != self.lastrev:
         raise util.Abort(_('internal calling inconsistency'))
     path = os.path.join(self.tmppath, name)
     data = open(path, 'rb').read()
     mode = os.lstat(path).st_mode
     mode = (mode & 0111) and 'x' or ''
     return data, mode
Example #25
0
 def __init__(self, ui, path=None):
     self.ui = ui
     self.include = {}
     self.exclude = {}
     self.rename = {}
     if path:
         if self.parse(path):
             raise util.Abort(_('errors in filemap'))
Example #26
0
    def _connect(self):
        root = self.cvsroot
        conntype = None
        user, host = None, None
        cmd = ['cvs', 'server']

        self.ui.status(_("connecting to %s\n") % root)

        if root.startswith(":pserver:"):
            root = root[9:]
            m = re.match(r'(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?(.*)',
                         root)
            if m:
                conntype = "pserver"
                user, passw, serv, port, root = m.groups()
                if not user:
                    user = "******"
                if not port:
                    port = 2401
                else:
                    port = int(port)
                format0 = ":pserver:%s@%s:%s" % (user, serv, root)
                format1 = ":pserver:%s@%s:%d%s" % (user, serv, port, root)

                if not passw:
                    passw = "A"
                    cvspass = os.path.expanduser("~/.cvspass")
                    try:
                        pf = open(cvspass)
                        for line in pf.read().splitlines():
                            part1, part2 = line.split(' ', 1)
                            if part1 == '/1':
                                # /1 :pserver:[email protected]:2401/cvsroot/foo Ah<Z
                                part1, part2 = part2.split(' ', 1)
                                format = format1
                            else:
                                # :pserver:[email protected]:/cvsroot/foo Ah<Z
                                format = format0
                            if part1 == format:
                                passw = part2
                                break
                        pf.close()
                    except IOError, inst:
                        if inst.errno != errno.ENOENT:
                            if not getattr(inst, 'filename', None):
                                inst.filename = cvspass
                            raise

                sck = socket.socket()
                sck.connect((serv, port))
                sck.send("\n".join([
                    "BEGIN AUTH REQUEST", root, user, passw,
                    "END AUTH REQUEST", ""
                ]))
                if sck.recv(128) != "I LOVE YOU\n":
                    raise util.Abort(_("CVS pserver authentication failed"))

                self.writep = self.readp = sck.makefile('r+')
Example #27
0
    def _parse(self):
        if self.changeset is not None:
            return
        self.changeset = {}

        maxrev = 0
        if self.rev:
            # TODO: handle tags
            try:
                # patchset number?
                maxrev = int(self.rev)
            except ValueError:
                raise util.Abort(
                    _('revision %s is not a patchset number') % self.rev)

        d = os.getcwd()
        try:
            os.chdir(self.path)
            id = None

            cache = 'update'
            if not self.ui.configbool('convert', 'cvsps.cache', True):
                cache = None
            db = cvsps.createlog(self.ui, cache=cache)
            db = cvsps.createchangeset(
                self.ui,
                db,
                fuzz=int(self.ui.config('convert', 'cvsps.fuzz', 60)),
                mergeto=self.ui.config('convert', 'cvsps.mergeto', None),
                mergefrom=self.ui.config('convert', 'cvsps.mergefrom', None))

            for cs in db:
                if maxrev and cs.id > maxrev:
                    break
                id = str(cs.id)
                cs.author = self.recode(cs.author)
                self.lastbranch[cs.branch] = id
                cs.comment = self.recode(cs.comment)
                date = util.datestr(cs.date)
                self.tags.update(dict.fromkeys(cs.tags, id))

                files = {}
                for f in cs.entries:
                    files[f.file] = "%s%s" % ('.'.join(
                        [str(x) for x in f.revision]), ['', '(DEAD)'][f.dead])

                # add current commit to set
                c = commit(author=cs.author,
                           date=date,
                           parents=[str(p.id) for p in cs.parents],
                           desc=cs.comment,
                           branch=cs.branch or '')
                self.changeset[id] = c
                self.files[id] = files

            self.heads = self.lastbranch.values()
        finally:
            os.chdir(d)
Example #28
0
def convert(ui, src, dest=None, revmapfile=None, **opts):
    global orig_encoding
    orig_encoding = encoding.encoding
    encoding.encoding = 'UTF-8'

    # support --authors as an alias for --authormap
    if not opts.get('authormap'):
        opts['authormap'] = opts.get('authors')

    if not dest:
        dest = hg.defaultdest(src) + "-hg"
        ui.status(_("assuming destination %s\n") % dest)

    destc = convertsink(ui, dest, opts.get('dest_type'))

    try:
        srcc, defaultsort = convertsource(ui, src, opts.get('source_type'),
                                          opts.get('rev'))
    except Exception:
        for path in destc.created:
            shutil.rmtree(path, True)
        raise

    sortmodes = ('branchsort', 'datesort', 'sourcesort')
    sortmode = [m for m in sortmodes if opts.get(m)]
    if len(sortmode) > 1:
        raise util.Abort(_('more than one sort mode specified'))
    sortmode = sortmode and sortmode[0] or defaultsort
    if sortmode == 'sourcesort' and not srcc.hasnativeorder():
        raise util.Abort(
            _('--sourcesort is not supported by this data source'))

    fmap = opts.get('filemap')
    if fmap:
        srcc = filemap.filemap_source(ui, srcc, fmap)
        destc.setfilemapmode(True)

    if not revmapfile:
        try:
            revmapfile = destc.revmapfile()
        except:
            revmapfile = os.path.join(destc, "map")

    c = converter(ui, srcc, destc, revmapfile, opts)
    c.convert(sortmode)
Example #29
0
def hook(ui, repo, node, hooktype, **kwargs):
    """verify that files have expected EOLs"""
    files = set()
    for rev in xrange(repo[node].rev(), len(repo)):
        files.update(repo[rev].files())
    tip = repo['tip']
    for f in files:
        if f not in tip:
            continue
        for pattern, target in ui.configitems('encode'):
            if match.match(repo.root, '', [pattern])(f):
                data = tip[f].data()
                if target == "to-lf" and "\r\n" in data:
                    raise util.Abort(
                        _("%s should not have CRLF line endings") % f)
                elif target == "to-crlf" and singlelf.search(data):
                    raise util.Abort(
                        _("%s should not have LF line endings") % f)
Example #30
0
def convertsink(ui, path, type):
    if type and type not in [s[0] for s in sink_converters]:
        raise util.Abort(_('%s: invalid destination repository type') % type)
    for name, sink in sink_converters:
        try:
            if not type or name == type:
                return sink(ui, path)
        except NoRepo, inst:
            ui.note(_("convert: %s\n") % inst)