hgext/narrow/narrowmerge.py
author Yuya Nishihara <yuya@tcha.org>
Fri, 26 Jan 2018 19:48:39 +0900
changeset 36200 deb851914fd7
parent 36165 53fe5a1a92bd
child 36472 d0d5eef57fb0
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``.

# narrowmerge.py - extensions to mercurial merge 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.i18n import _
from mercurial import (
    copies,
    error,
    extensions,
    merge,
    util,
)

def setup():
    def _manifestmerge(orig, repo, wctx, p2, pa, branchmerge, *args, **kwargs):
        """Filter updates to only lay out files that match the narrow spec."""
        actions, diverge, renamedelete = orig(
            repo, wctx, p2, pa, branchmerge, *args, **kwargs)

        if not util.safehasattr(repo, 'narrowmatch'):
            return actions, diverge, renamedelete

        nooptypes = set(['k']) # TODO: handle with nonconflicttypes
        nonconflicttypes = set('a am c cm f g r e'.split())
        narrowmatch = repo.narrowmatch()
        # We mutate the items in the dict during iteration, so iterate
        # over a copy.
        for f, action in list(actions.items()):
            if narrowmatch(f):
                pass
            elif not branchmerge:
                del actions[f] # just updating, ignore changes outside clone
            elif action[0] in nooptypes:
                del actions[f] # merge does not affect file
            elif action[0] in nonconflicttypes:
                raise error.Abort(_('merge affects file \'%s\' outside narrow, '
                                    'which is not yet supported') % f,
                                  hint=_('merging in the other direction '
                                         'may work'))
            else:
                raise error.Abort(_('conflict in file \'%s\' is outside '
                                    'narrow clone') % f)

        return actions, diverge, renamedelete

    extensions.wrapfunction(merge, 'manifestmerge', _manifestmerge)

    def _checkcollision(orig, repo, wmf, actions):
        if util.safehasattr(repo, 'narrowmatch'):
            narrowmatch = repo.narrowmatch()
            wmf = wmf.matches(narrowmatch)
            if actions:
                narrowactions = {}
                for m, actionsfortype in actions.iteritems():
                    narrowactions[m] = []
                    for (f, args, msg) in actionsfortype:
                        if narrowmatch(f):
                            narrowactions[m].append((f, args, msg))
                actions = narrowactions
        return orig(repo, wmf, actions)

    extensions.wrapfunction(merge, '_checkcollision', _checkcollision)

    def _computenonoverlap(orig, repo, *args, **kwargs):
        u1, u2 = orig(repo, *args, **kwargs)
        if not util.safehasattr(repo, 'narrowmatch'):
            return u1, u2

        narrowmatch = repo.narrowmatch()
        u1 = [f for f in u1 if narrowmatch(f)]
        u2 = [f for f in u2 if narrowmatch(f)]
        return u1, u2
    extensions.wrapfunction(copies, '_computenonoverlap', _computenonoverlap)