hgext/narrow/narrowpatch.py
author Yuya Nishihara <yuya@tcha.org>
Fri, 26 Jan 2018 19:48:39 +0900
changeset 36200 deb851914fd7
parent 36079 a2a6e724d61a
child 36345 f85e32a5e5c8
permissions -rw-r--r--
dirstate: drop explicit files that shouldn't match (BC) (issue4679) Before, wctx.walk() could include files excluded by -X pattern, which disagrees with wctx.matches() and ctx.walk()/matches() behavior. This patch fixes the problem by testing stat results against the matcher if the matcher may contain false paths. I have no idea if the fix should be made before the workaround for case- insensitive filesystems, but that shouldn't matter since match.anypats() means 'not match.isexact()'. This patch also makes narrow and sparse extensions to not exclude explicit paths on walk() because they appear to depend on the buggy behavior. More detailed analysis about this issue by Martin von Zweigbergk: "I think it's just an unintended consequence of how the dirstate walk works, but I'm not sure. The exception for explicit files also bothered me when I was working on the matcher code a year or so ago. I actually added the exception to the matcher code because I thought it was always working like that (not just for dirstate) in a83a7d27911e (match: handle excludes using new differencematcher, 2017-05-16). It was only recently that Yuya realized that it used to be inconsistent and that I probably made it consistently bad because I didn't realize it was inconsistent to start with, see 821d8a5ab4ff (match: do not weirdly include explicit files excluded by -X option, 2018-01-16)." .. bc:: Working-directory commands now respect ``-X PATTERN`` no matter if PATTERN matches explicitly-specified FILEs. For example, ``hg add foo -X foo`` no longer add the file ``foo``.

# narrowpatch.py - extensions to mercurial patch module to support narrow clones
#
# Copyright 2017 Google, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

from mercurial import (
    extensions,
    patch,
    util,
)

def setup(repo):
    def _filepairs(orig, *args):
        """Only includes files within the narrow spec in the diff."""
        if util.safehasattr(repo, 'narrowmatch'):
            narrowmatch = repo.narrowmatch()
            for x in orig(*args):
                f1, f2, copyop = x
                if ((not f1 or narrowmatch(f1)) and
                    (not f2 or narrowmatch(f2))):
                    yield x
        else:
            for x in orig(*args):
                yield x

    def trydiff(orig, repo, revs, ctx1, ctx2, modified, added, removed,
                copy, getfilectx, *args, **kwargs):
        if util.safehasattr(repo, 'narrowmatch'):
            narrowmatch = repo.narrowmatch()
            modified = filter(narrowmatch, modified)
            added = filter(narrowmatch, added)
            removed = filter(narrowmatch, removed)
            copy = {k: v for k, v in copy.iteritems() if narrowmatch(k)}
        return orig(repo, revs, ctx1, ctx2, modified, added, removed, copy,
                    getfilectx, *args, **kwargs)

    extensions.wrapfunction(patch, '_filepairs', _filepairs)
    extensions.wrapfunction(patch, 'trydiff', trydiff)