hgext/interhg.py
author Kaz Nishimura <kazssym@vx68k.org>
Thu, 17 Oct 2013 13:27:17 +0900
changeset 20528 1a9ebc83a74c
parent 16743 38caf405d010
permissions -rw-r--r--
win32: improve the performance of win32.unlink() over CIFS Emulating POSIX unlink() behavior with os.rename() and os.unlink() is often slow especially over CIFS from Windows clients due to its protocol overhead. This patch changes win32.unlink() to try first an exclusive open with the Win32 delete-on-close flag, and if a sharing violation is detected, to fall back to the original emulation. This patch also removes a test with os.path.isdir() since we expect opening a directory shall fail as os.unlink() would. Example measurements (repeated 3-times after 1-time calibration): (Without this patch: hg update from null to default) 127 files updated, 0 files merged, 0 files removed, 0 files unresolved time: real 19.871 secs (user 0.328+0.000 sys 1.794+0.000) time: real 19.622 secs (user 0.312+0.000 sys 2.044+0.000) time: real 19.138 secs (user 0.250+0.000 sys 1.872+0.000) (Without this patch: hg update from default to null) 0 files updated, 0 files merged, 127 files removed, 0 files unresolved time: real 35.158 secs (user 0.156+0.000 sys 2.512+0.000) time: real 35.272 secs (user 0.250+0.000 sys 2.512+0.000) time: real 36.569 secs (user 0.203+0.000 sys 2.387+0.000) (With this patch: hg update from null to default) 127 files updated, 0 files merged, 0 files removed, 0 files unresolved time: real 17.893 secs (user 0.328+0.000 sys 1.700+0.000) time: real 18.512 secs (user 0.265+0.000 sys 1.529+0.000) time: real 20.238 secs (user 0.312+0.000 sys 1.685+0.000) (With this patch: hg update from default to null) 0 files updated, 0 files merged, 127 files removed, 0 files unresolved time: real 12.312 secs (user 0.250+0.000 sys 0.811+0.000) time: real 12.471 secs (user 0.156+0.000 sys 0.889+0.000) time: real 9.727 secs (user 0.125+0.000 sys 0.858+0.000)

# interhg.py - interhg
#
# Copyright 2007 OHASHI Hideya <ohachige@gmail.com>
#
# Contributor(s):
#   Edward Lee <edward.lee@engineering.uiuc.edu>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''expand expressions into changelog and summaries

This extension allows the use of a special syntax in summaries, which
will be automatically expanded into links or any other arbitrary
expression, much like InterWiki does.

A few example patterns (link to bug tracking, etc.) that may be used
in your hgrc::

  [interhg]
  issues = s!issue(\\d+)!<a href="http://bts/issue\\1">issue\\1</a>!
  bugzilla = s!((?:bug|b=|(?=#?\\d{4,}))(?:\\s*#?)(\\d+))!<a..=\\2">\\1</a>!i
  boldify = s!(^|\\s)#(\\d+)\\b! <b>#\\2</b>!
'''

import re
from mercurial.hgweb import hgweb_mod
from mercurial import templatefilters, extensions
from mercurial.i18n import _

testedwith = 'internal'

interhg_table = []

def uisetup(ui):
    orig_escape = templatefilters.filters["escape"]

    def interhg_escape(x):
        escstr = orig_escape(x)
        for regexp, format in interhg_table:
            escstr = regexp.sub(format, escstr)
        return escstr

    templatefilters.filters["escape"] = interhg_escape

def interhg_refresh(orig, self, *args, **kwargs):
    interhg_table[:] = []
    for key, pattern in self.repo.ui.configitems('interhg'):
        # grab the delimiter from the character after the "s"
        unesc = pattern[1]
        delim = re.escape(unesc)

        # identify portions of the pattern, taking care to avoid escaped
        # delimiters. the replace format and flags are optional, but delimiters
        # are required.
        match = re.match(r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
                         % (delim, delim, delim), pattern)
        if not match:
            self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n")
                              % (key, pattern))
            continue

        # we need to unescape the delimiter for regexp and format
        delim_re = re.compile(r'(?<!\\)\\%s' % delim)
        regexp = delim_re.sub(unesc, match.group(1))
        format = delim_re.sub(unesc, match.group(2))

        # the pattern allows for 6 regexp flags, so set them if necessary
        flagin = match.group(3)
        flags = 0
        if flagin:
            for flag in flagin.upper():
                flags |= re.__dict__[flag]

        try:
            regexp = re.compile(regexp, flags)
            interhg_table.append((regexp, format))
        except re.error:
            self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n")
                              % (key, regexp))
    return orig(self, *args, **kwargs)

extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh)