merge.py
2086 lines
| 80.8 KiB
| text/x-python
|
PythonLexer
/ mercurial / merge.py
|
r2775 | # merge.py - directory-level update/merge handling for Mercurial | ||
# | ||||
|
r4635 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> | ||
|
r2775 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2775 | |||
|
r25959 | from __future__ import absolute_import | ||
import errno | ||||
|
r29341 | import hashlib | ||
|
r25959 | import shutil | ||
|
r20590 | import struct | ||
|
r25959 | from .i18n import _ | ||
from .node import ( | ||||
|
r30361 | addednodeid, | ||
|
r25959 | bin, | ||
hex, | ||||
|
r30362 | modifiednodeid, | ||
|
r27031 | nullhex, | ||
|
r25959 | nullid, | ||
nullrev, | ||||
) | ||||
from . import ( | ||||
copies, | ||||
|
r26587 | error, | ||
|
r25959 | filemerge, | ||
|
r31257 | match as matchmod, | ||
r33147 | obsutil, | |||
|
r30519 | pycompat, | ||
|
r27656 | scmutil, | ||
|
r36026 | subrepoutil, | ||
|
r25959 | util, | ||
worker, | ||||
) | ||||
|
r6512 | |||
|
r20590 | _pack = struct.pack | ||
_unpack = struct.unpack | ||||
|
r20593 | def _droponode(data): | ||
# used for compatibility for v1 | ||||
|
r23380 | bits = data.split('\0') | ||
|
r20593 | bits = bits[:-2] + bits[-1:] | ||
|
r23380 | return '\0'.join(bits) | ||
|
r20593 | |||
|
r6512 | class mergestate(object): | ||
|
r20590 | '''track 3-way merge state of individual files | ||
|
r27022 | The merge state is stored on disk when needed. Two files are used: one with | ||
an old format (version 1), and one with a new format (version 2). Version 2 | ||||
stores a superset of the data in version 1, including new kinds of records | ||||
in the future. For more about the new format, see the documentation for | ||||
`_readrecordsv2`. | ||||
|
r20590 | |||
|
r27022 | Each record can contain arbitrary content, and has an associated type. This | ||
`type` should be a letter. If `type` is uppercase, the record is mandatory: | ||||
versions of Mercurial that don't support it should abort. If `type` is | ||||
lowercase, the record can be safely ignored. | ||||
|
r20590 | |||
|
r27022 | Currently known records: | ||
|
r20590 | |||
L: the node of the "local" part of the merge (hexified version) | ||||
|
r20591 | O: the node of the "other" part of the merge (hexified version) | ||
|
r20590 | F: a file to be merged entry | ||
|
r27031 | C: a change/delete or delete/change conflict | ||
|
r26650 | D: a file that the external merge driver will merge internally | ||
(experimental) | ||||
|
r34546 | P: a path conflict (file vs directory) | ||
|
r26649 | m: the external merge driver defined for this merge plus its run state | ||
(experimental) | ||||
|
r30332 | f: a (filename, dictionary) tuple of optional values for a given file | ||
|
r27027 | X: unsupported mandatory record type (used in tests) | ||
x: unsupported advisory record type (used in tests) | ||||
|
r28634 | l: the labels for the parts of the merge. | ||
|
r26649 | |||
Merge driver run states (experimental): | ||||
u: driver-resolved files unmarked -- needs to be run next time we're about | ||||
to resolve or commit | ||||
m: driver-resolved files marked -- only needs to be run before commit | ||||
s: success/skipped -- does not need to be run any more | ||||
|
r27022 | |||
|
r34546 | Merge record states (stored in self._state, indexed by filename): | ||
u: unresolved conflict | ||||
r: resolved conflict | ||||
pu: unresolved path conflict (file conflicts with directory) | ||||
pr: resolved path conflict | ||||
d: driver-resolved conflict | ||||
The resolve command transitions between 'u' and 'r' for conflicts and | ||||
'pu' and 'pr' for path conflicts. | ||||
|
r20590 | ''' | ||
|
r23380 | statepathv1 = 'merge/state' | ||
statepathv2 = 'merge/state2' | ||||
|
r20651 | |||
|
r26987 | @staticmethod | ||
|
r28634 | def clean(repo, node=None, other=None, labels=None): | ||
|
r26987 | """Initialize a brand new merge state, removing any existing state on | ||
disk.""" | ||||
ms = mergestate(repo) | ||||
|
r28634 | ms.reset(node, other, labels) | ||
|
r26987 | return ms | ||
|
r26991 | @staticmethod | ||
def read(repo): | ||||
"""Initialize the merge state, reading it from disk.""" | ||||
ms = mergestate(repo) | ||||
|
r27005 | ms._read() | ||
|
r26991 | return ms | ||
|
r6512 | def __init__(self, repo): | ||
|
r27005 | """Initialize the merge state. | ||
Do not use this directly! Instead call read() or clean().""" | ||||
|
r6512 | self._repo = repo | ||
|
r12369 | self._dirty = False | ||
|
r28634 | self._labels = None | ||
|
r20651 | |||
|
r28634 | def reset(self, node=None, other=None, labels=None): | ||
|
r6512 | self._state = {} | ||
|
r28009 | self._stateextras = {} | ||
|
r21261 | self._local = None | ||
self._other = None | ||||
|
r28634 | self._labels = labels | ||
|
r27130 | for var in ('localctx', 'otherctx'): | ||
if var in vars(self): | ||||
delattr(self, var) | ||||
|
r7848 | if node: | ||
self._local = node | ||||
|
r20591 | self._other = other | ||
|
r26768 | self._readmergedriver = None | ||
|
r26769 | if self.mergedriver: | ||
self._mdstate = 's' | ||||
else: | ||||
self._mdstate = 'u' | ||||
|
r31323 | shutil.rmtree(self._repo.vfs.join('merge'), True) | ||
|
r27074 | self._results = {} | ||
|
r12369 | self._dirty = False | ||
|
r20651 | |||
|
r6518 | def _read(self): | ||
|
r20652 | """Analyse each record content to restore a serialized state from disk | ||
This function process "record" entry produced by the de-serialization | ||||
of on disk file. | ||||
""" | ||||
|
r6518 | self._state = {} | ||
|
r28009 | self._stateextras = {} | ||
|
r21261 | self._local = None | ||
self._other = None | ||||
|
r27130 | for var in ('localctx', 'otherctx'): | ||
if var in vars(self): | ||||
delattr(self, var) | ||||
|
r26768 | self._readmergedriver = None | ||
|
r26769 | self._mdstate = 's' | ||
|
r26986 | unsupported = set() | ||
|
r20589 | records = self._readrecords() | ||
for rtype, record in records: | ||||
if rtype == 'L': | ||||
self._local = bin(record) | ||||
|
r20591 | elif rtype == 'O': | ||
self._other = bin(record) | ||||
|
r26649 | elif rtype == 'm': | ||
bits = record.split('\0', 1) | ||||
mdstate = bits[1] | ||||
if len(mdstate) != 1 or mdstate not in 'ums': | ||||
# the merge driver should be idempotent, so just rerun it | ||||
mdstate = 'u' | ||||
|
r26768 | self._readmergedriver = bits[0] | ||
|
r26649 | self._mdstate = mdstate | ||
|
r34546 | elif rtype in 'FDCP': | ||
|
r23380 | bits = record.split('\0') | ||
|
r20589 | self._state[bits[0]] = bits[1:] | ||
|
r28009 | elif rtype == 'f': | ||
filename, rawextras = record.split('\0', 1) | ||||
extraparts = rawextras.split('\0') | ||||
extras = {} | ||||
i = 0 | ||||
while i < len(extraparts): | ||||
extras[extraparts[i]] = extraparts[i + 1] | ||||
i += 2 | ||||
self._stateextras[filename] = extras | ||||
|
r28634 | elif rtype == 'l': | ||
labels = record.split('\0', 2) | ||||
self._labels = [l for l in labels if len(l) > 0] | ||||
|
r20589 | elif not rtype.islower(): | ||
|
r26986 | unsupported.add(rtype) | ||
|
r27074 | self._results = {} | ||
|
r20589 | self._dirty = False | ||
|
r20651 | |||
|
r26986 | if unsupported: | ||
raise error.UnsupportedMergeRecords(unsupported) | ||||
|
r20589 | def _readrecords(self): | ||
|
r20652 | """Read merge state from disk and return a list of record (TYPE, data) | ||
|
r21024 | We read data from both v1 and v2 files and decide which one to use. | ||
|
r20652 | |||
|
r21024 | V1 has been used by version prior to 2.9.1 and contains less data than | ||
v2. We read both versions and check if no data in v2 contradicts | ||||
|
r20652 | v1. If there is not contradiction we can safely assume that both v1 | ||
and v2 were written at the same time and use the extract data in v2. If | ||||
there is contradiction we ignore v2 content as we assume an old version | ||||
|
r21024 | of Mercurial has overwritten the mergestate file and left an old v2 | ||
|
r20652 | file around. | ||
returns list of record [(TYPE, data), ...]""" | ||||
|
r20590 | v1records = self._readrecordsv1() | ||
v2records = self._readrecordsv2() | ||||
|
r26500 | if self._v1v2match(v1records, v2records): | ||
return v2records | ||||
else: | ||||
# v1 file is newer than v2 file, use it | ||||
# we have to infer the "other" changeset of the merge | ||||
# we cannot do better than that with v1 of the format | ||||
mctx = self._repo[None].parents()[-1] | ||||
v1records.append(('O', mctx.hex())) | ||||
# add place holder "other" file node information | ||||
# nobody is using it yet so we do no need to fetch the data | ||||
# if mctx was wrong `mctx[bits[-2]]` may fails. | ||||
for idx, r in enumerate(v1records): | ||||
if r[0] == 'F': | ||||
bits = r[1].split('\0') | ||||
bits.insert(-2, '') | ||||
v1records[idx] = (r[0], '\0'.join(bits)) | ||||
return v1records | ||||
def _v1v2match(self, v1records, v2records): | ||||
|
r20593 | oldv2 = set() # old format version of v2 record | ||
for rec in v2records: | ||||
if rec[0] == 'L': | ||||
oldv2.add(rec) | ||||
elif rec[0] == 'F': | ||||
# drop the onode data (not contained in v1) | ||||
oldv2.add(('F', _droponode(rec[1]))) | ||||
for rec in v1records: | ||||
if rec not in oldv2: | ||||
|
r26500 | return False | ||
|
r20590 | else: | ||
|
r26500 | return True | ||
|
r20651 | |||
|
r20590 | def _readrecordsv1(self): | ||
|
r20652 | """read on disk merge state for version 1 file | ||
returns list of record [(TYPE, data), ...] | ||||
Note: the "F" data from this file are one entry short | ||||
(no "other file node" entry) | ||||
""" | ||||
|
r20589 | records = [] | ||
|
r6518 | try: | ||
|
r23877 | f = self._repo.vfs(self.statepathv1) | ||
|
r6530 | for i, l in enumerate(f): | ||
if i == 0: | ||||
|
r20589 | records.append(('L', l[:-1])) | ||
|
r6530 | else: | ||
|
r20589 | records.append(('F', l[:-1])) | ||
|
r13400 | f.close() | ||
|
r25660 | except IOError as err: | ||
|
r6518 | if err.errno != errno.ENOENT: | ||
raise | ||||
|
r20589 | return records | ||
|
r20651 | |||
|
r20590 | def _readrecordsv2(self): | ||
|
r20652 | """read on disk merge state for version 2 file | ||
|
r27022 | This format is a list of arbitrary records of the form: | ||
[type][length][content] | ||||
`type` is a single character, `length` is a 4 byte integer, and | ||||
`content` is an arbitrary byte sequence of length `length`. | ||||
|
r27027 | Mercurial versions prior to 3.7 have a bug where if there are | ||
unsupported mandatory merge records, attempting to clear out the merge | ||||
state with hg update --clean or similar aborts. The 't' record type | ||||
works around that by writing out what those versions treat as an | ||||
advisory record, but later versions interpret as special: the first | ||||
character is the 'real' record type and everything onwards is the data. | ||||
|
r27022 | Returns list of records [(TYPE, data), ...].""" | ||
|
r20590 | records = [] | ||
try: | ||||
|
r23877 | f = self._repo.vfs(self.statepathv2) | ||
|
r20590 | data = f.read() | ||
off = 0 | ||||
end = len(data) | ||||
while off < end: | ||||
|
r36554 | rtype = data[off:off + 1] | ||
|
r20590 | off += 1 | ||
|
r20607 | length = _unpack('>I', data[off:(off + 4)])[0] | ||
|
r20590 | off += 4 | ||
|
r20607 | record = data[off:(off + length)] | ||
off += length | ||||
|
r27027 | if rtype == 't': | ||
|
r36554 | rtype, record = record[0:1], record[1:] | ||
|
r20590 | records.append((rtype, record)) | ||
f.close() | ||||
|
r25660 | except IOError as err: | ||
|
r20590 | if err.errno != errno.ENOENT: | ||
raise | ||||
return records | ||||
|
r20651 | |||
|
r26649 | @util.propertycache | ||
def mergedriver(self): | ||||
|
r26768 | # protect against the following: | ||
# - A configures a malicious merge driver in their hgrc, then | ||||
# pauses the merge | ||||
# - A edits their hgrc to remove references to the merge driver | ||||
# - A gives a copy of their entire repo, including .hg, to B | ||||
# - B inspects .hgrc and finds it to be clean | ||||
# - B then continues the merge and the malicious merge driver | ||||
# gets invoked | ||||
configmergedriver = self._repo.ui.config('experimental', 'mergedriver') | ||||
if (self._readmergedriver is not None | ||||
and self._readmergedriver != configmergedriver): | ||||
raise error.ConfigError( | ||||
_("merge driver changed since merge started"), | ||||
hint=_("revert merge driver change or abort merge")) | ||||
return configmergedriver | ||||
|
r26765 | @util.propertycache | ||
|
r27130 | def localctx(self): | ||
if self._local is None: | ||||
|
r31646 | msg = "localctx accessed but self._local isn't set" | ||
raise error.ProgrammingError(msg) | ||||
|
r27130 | return self._repo[self._local] | ||
@util.propertycache | ||||
|
r26765 | def otherctx(self): | ||
|
r27129 | if self._other is None: | ||
|
r31646 | msg = "otherctx accessed but self._other isn't set" | ||
raise error.ProgrammingError(msg) | ||||
|
r26765 | return self._repo[self._other] | ||
|
r26649 | |||
|
r21264 | def active(self): | ||
"""Whether mergestate is active. | ||||
Returns True if there appears to be mergestate. This is a rough proxy | ||||
for "is a merge in progress." | ||||
""" | ||||
# Check local variables before looking at filesystem for performance | ||||
# reasons. | ||||
return bool(self._local) or bool(self._state) or \ | ||||
|
r23877 | self._repo.vfs.exists(self.statepathv1) or \ | ||
self._repo.vfs.exists(self.statepathv2) | ||||
|
r21264 | |||
|
r12369 | def commit(self): | ||
|
r20652 | """Write current state on disk (if necessary)""" | ||
|
r12369 | if self._dirty: | ||
|
r27006 | records = self._makerecords() | ||
|
r20589 | self._writerecords(records) | ||
|
r12369 | self._dirty = False | ||
|
r20651 | |||
|
r27006 | def _makerecords(self): | ||
records = [] | ||||
records.append(('L', hex(self._local))) | ||||
records.append(('O', hex(self._other))) | ||||
if self.mergedriver: | ||||
records.append(('m', '\0'.join([ | ||||
self.mergedriver, self._mdstate]))) | ||||
|
r34561 | # Write out state items. In all cases, the value of the state map entry | ||
# is written as the contents of the record. The record type depends on | ||||
# the type of state that is stored, and capital-letter records are used | ||||
# to prevent older versions of Mercurial that do not support the feature | ||||
# from loading them. | ||||
for filename, v in self._state.iteritems(): | ||||
|
r27006 | if v[0] == 'd': | ||
|
r34561 | # Driver-resolved merge. These are stored in 'D' records. | ||
records.append(('D', '\0'.join([filename] + v))) | ||||
|
r34546 | elif v[0] in ('pu', 'pr'): | ||
|
r34561 | # Path conflicts. These are stored in 'P' records. The current | ||
# resolution state ('pu' or 'pr') is stored within the record. | ||||
records.append(('P', '\0'.join([filename] + v))) | ||||
|
r27031 | elif v[1] == nullhex or v[6] == nullhex: | ||
|
r34561 | # Change/Delete or Delete/Change conflicts. These are stored in | ||
# 'C' records. v[1] is the local file, and is nullhex when the | ||||
# file is deleted locally ('dc'). v[6] is the remote file, and | ||||
# is nullhex when the file is deleted remotely ('cd'). | ||||
records.append(('C', '\0'.join([filename] + v))) | ||||
|
r27006 | else: | ||
|
r34561 | # Normal files. These are stored in 'F' records. | ||
records.append(('F', '\0'.join([filename] + v))) | ||||
|
r28009 | for filename, extras in sorted(self._stateextras.iteritems()): | ||
rawextras = '\0'.join('%s\0%s' % (k, v) for k, v in | ||||
extras.iteritems()) | ||||
records.append(('f', '%s\0%s' % (filename, rawextras))) | ||||
|
r28634 | if self._labels is not None: | ||
labels = '\0'.join(self._labels) | ||||
records.append(('l', labels)) | ||||
|
r27006 | return records | ||
|
r20589 | def _writerecords(self, records): | ||
|
r20652 | """Write current state on disk (both v1 and v2)""" | ||
|
r20590 | self._writerecordsv1(records) | ||
self._writerecordsv2(records) | ||||
|
r20651 | |||
|
r20590 | def _writerecordsv1(self, records): | ||
|
r20652 | """Write current state on disk in a version 1 file""" | ||
|
r36495 | f = self._repo.vfs(self.statepathv1, 'wb') | ||
|
r20589 | irecords = iter(records) | ||
|
r29216 | lrecords = next(irecords) | ||
|
r20589 | assert lrecords[0] == 'L' | ||
|
r23380 | f.write(hex(self._local) + '\n') | ||
|
r20589 | for rtype, data in irecords: | ||
|
r23380 | if rtype == 'F': | ||
f.write('%s\n' % _droponode(data)) | ||||
|
r20589 | f.close() | ||
|
r20651 | |||
|
r20590 | def _writerecordsv2(self, records): | ||
|
r27027 | """Write current state on disk in a version 2 file | ||
See the docstring for _readrecordsv2 for why we use 't'.""" | ||||
# these are the records that all version 2 clients can read | ||||
whitelist = 'LOF' | ||||
|
r36495 | f = self._repo.vfs(self.statepathv2, 'wb') | ||
|
r20590 | for key, data in records: | ||
assert len(key) == 1 | ||||
|
r27027 | if key not in whitelist: | ||
key, data = 't', '%s%s' % (key, data) | ||||
|
r23380 | format = '>sI%is' % len(data) | ||
|
r20590 | f.write(_pack(format, key, len(data), data)) | ||
f.close() | ||||
|
r20651 | |||
|
r18338 | def add(self, fcl, fco, fca, fd): | ||
|
r20652 | """add a new (potentially?) conflicting file the merge state | ||
fcl: file context for local, | ||||
fco: file context for remote, | ||||
fca: file context for ancestors, | ||||
fd: file path of the resulting merge. | ||||
note: also write the local version to the `.hg/merge` directory. | ||||
""" | ||||
|
r27049 | if fcl.isabsent(): | ||
hash = nullhex | ||||
else: | ||||
|
r33095 | hash = hex(hashlib.sha1(fcl.path()).digest()) | ||
|
r27049 | self._repo.vfs.write('merge/' + hash, fcl.data()) | ||
|
r20593 | self._state[fd] = ['u', hash, fcl.path(), | ||
fca.path(), hex(fca.filenode()), | ||||
fco.path(), hex(fco.filenode()), | ||||
fcl.flags()] | ||||
|
r34487 | self._stateextras[fd] = {'ancestorlinknode': hex(fca.node())} | ||
|
r12369 | self._dirty = True | ||
|
r20651 | |||
|
r34546 | def addpath(self, path, frename, forigin): | ||
"""add a new conflicting path to the merge state | ||||
path: the path that conflicts | ||||
frename: the filename the conflicting file was renamed to | ||||
forigin: origin of the file ('l' or 'r' for local/remote) | ||||
""" | ||||
self._state[path] = ['pu', frename, forigin] | ||||
self._dirty = True | ||||
|
r6512 | def __contains__(self, dfile): | ||
return dfile in self._state | ||||
|
r20651 | |||
|
r6512 | def __getitem__(self, dfile): | ||
|
r6518 | return self._state[dfile][0] | ||
|
r20651 | |||
|
r6518 | def __iter__(self): | ||
|
r21268 | return iter(sorted(self._state)) | ||
|
r20651 | |||
|
r19285 | def files(self): | ||
return self._state.keys() | ||||
|
r20651 | |||
|
r6512 | def mark(self, dfile, state): | ||
|
r6518 | self._state[dfile][0] = state | ||
|
r12369 | self._dirty = True | ||
|
r20651 | |||
|
r26766 | def mdstate(self): | ||
return self._mdstate | ||||
|
r21266 | def unresolved(self): | ||
"""Obtain the paths of unresolved files.""" | ||||
|
r33310 | for f, entry in self._state.iteritems(): | ||
|
r34546 | if entry[0] in ('u', 'pu'): | ||
|
r21266 | yield f | ||
|
r26740 | def driverresolved(self): | ||
"""Obtain the paths of driver-resolved files.""" | ||||
for f, entry in self._state.items(): | ||||
if entry[0] == 'd': | ||||
yield f | ||||
|
r28009 | def extras(self, filename): | ||
return self._stateextras.setdefault(filename, {}) | ||||
|
r28634 | def _resolve(self, preresolve, dfile, wctx): | ||
|
r20652 | """rerun merge process for file path `dfile`""" | ||
|
r26651 | if self[dfile] in 'rd': | ||
|
r26616 | return True, 0 | ||
|
r20593 | stateentry = self._state[dfile] | ||
state, hash, lfile, afile, anode, ofile, onode, flags = stateentry | ||||
|
r20594 | octx = self._repo[self._other] | ||
|
r28011 | extras = self.extras(dfile) | ||
anccommitnode = extras.get('ancestorlinknode') | ||||
if anccommitnode: | ||||
actx = self._repo[anccommitnode] | ||||
else: | ||||
actx = None | ||||
|
r27048 | fcd = self._filectxorabsent(hash, wctx, dfile) | ||
fco = self._filectxorabsent(onode, octx, ofile) | ||||
# TODO: move this to filectxorabsent | ||||
|
r28011 | fca = self._repo.filectx(afile, fileid=anode, changeid=actx) | ||
|
r18338 | # "premerge" x flags | ||
flo = fco.flags() | ||||
fla = fca.flags() | ||||
if 'x' in flags + flo + fla and 'l' not in flags + flo + fla: | ||||
|
r30161 | if fca.node() == nullid and flags != flo: | ||
|
r26617 | if preresolve: | ||
self._repo.ui.warn( | ||||
|
r30162 | _('warning: cannot merge flags for %s ' | ||
'without common ancestor - keeping local flags\n') | ||||
% afile) | ||||
|
r18338 | elif flags == fla: | ||
flags = flo | ||||
|
r26617 | if preresolve: | ||
# restore local | ||||
|
r27048 | if hash != nullhex: | ||
f = self._repo.vfs('merge/' + hash) | ||||
|
r33083 | wctx[dfile].write(f.read(), flags) | ||
|
r27048 | f.close() | ||
else: | ||||
|
r33082 | wctx[dfile].remove(ignoremissing=True) | ||
|
r34124 | complete, r, deleted = filemerge.premerge(self._repo, wctx, | ||
self._local, lfile, fcd, | ||||
fco, fca, | ||||
|
r28634 | labels=self._labels) | ||
|
r26617 | else: | ||
|
r34124 | complete, r, deleted = filemerge.filemerge(self._repo, wctx, | ||
self._local, lfile, fcd, | ||||
fco, fca, | ||||
|
r28634 | labels=self._labels) | ||
|
r13536 | if r is None: | ||
# no real conflict | ||||
del self._state[dfile] | ||||
|
r28009 | self._stateextras.pop(dfile, None) | ||
|
r20792 | self._dirty = True | ||
|
r13536 | elif not r: | ||
|
r6512 | self.mark(dfile, 'r') | ||
|
r27035 | |||
if complete: | ||||
|
r27075 | action = None | ||
|
r27035 | if deleted: | ||
|
r27122 | if fcd.isabsent(): | ||
# dc: local picked. Need to drop if present, which may | ||||
# happen on re-resolves. | ||||
action = 'f' | ||||
else: | ||||
|
r27035 | # cd: remote picked (or otherwise deleted) | ||
action = 'r' | ||||
else: | ||||
if fcd.isabsent(): # dc: remote picked | ||||
action = 'g' | ||||
elif fco.isabsent(): # cd: local picked | ||||
|
r27131 | if dfile in self.localctx: | ||
action = 'am' | ||||
else: | ||||
action = 'a' | ||||
|
r27035 | # else: regular merges (no action necessary) | ||
|
r27074 | self._results[dfile] = r, action | ||
|
r27035 | |||
|
r26616 | return complete, r | ||
|
r2775 | |||
|
r27048 | def _filectxorabsent(self, hexnode, ctx, f): | ||
if hexnode == nullhex: | ||||
return filemerge.absentfilectx(ctx, f) | ||||
else: | ||||
return ctx[f] | ||||
|
r28634 | def preresolve(self, dfile, wctx): | ||
|
r26870 | """run premerge process for dfile | ||
Returns whether the merge is complete, and the exit code.""" | ||||
|
r28634 | return self._resolve(True, dfile, wctx) | ||
|
r26617 | |||
|
r28634 | def resolve(self, dfile, wctx): | ||
|
r26870 | """run merge process (assuming premerge was run) for dfile | ||
Returns the exit code of the merge.""" | ||||
|
r28634 | return self._resolve(False, dfile, wctx)[1] | ||
|
r26615 | |||
|
r27076 | def counts(self): | ||
"""return counts for updated, merged and removed files in this | ||||
session""" | ||||
updated, merged, removed = 0, 0, 0 | ||||
for r, action in self._results.itervalues(): | ||||
if r is None: | ||||
updated += 1 | ||||
elif r == 0: | ||||
if action == 'r': | ||||
removed += 1 | ||||
else: | ||||
merged += 1 | ||||
return updated, merged, removed | ||||
|
r27077 | def unresolvedcount(self): | ||
"""get unresolved count for this merge (persistent)""" | ||||
|
r33311 | return len(list(self.unresolved())) | ||
|
r27077 | |||
|
r27079 | def actions(self): | ||
"""return lists of actions to perform on the dirstate""" | ||||
|
r27131 | actions = {'r': [], 'f': [], 'a': [], 'am': [], 'g': []} | ||
|
r27079 | for f, (r, action) in self._results.iteritems(): | ||
if action is not None: | ||||
actions[action].append((f, None, "merge result")) | ||||
return actions | ||||
|
r27088 | def recordactions(self): | ||
"""record remove/add/get actions in the dirstate""" | ||||
branchmerge = self._repo.dirstate.p2() != nullid | ||||
recordupdates(self._repo, self.actions(), branchmerge) | ||||
|
r27090 | def queueremove(self, f): | ||
"""queues a file to be removed from the dirstate | ||||
Meant for use by custom merge drivers.""" | ||||
self._results[f] = 0, 'r' | ||||
def queueadd(self, f): | ||||
"""queues a file to be added to the dirstate | ||||
Meant for use by custom merge drivers.""" | ||||
self._results[f] = 0, 'a' | ||||
def queueget(self, f): | ||||
"""queues a file to be marked modified in the dirstate | ||||
Meant for use by custom merge drivers.""" | ||||
self._results[f] = 0, 'g' | ||||
|
r27740 | def _getcheckunknownconfig(repo, section, name): | ||
|
r34523 | config = repo.ui.config(section, name) | ||
|
r27740 | valid = ['abort', 'ignore', 'warn'] | ||
if config not in valid: | ||||
validstr = ', '.join(["'" + v + "'" for v in valid]) | ||||
raise error.ConfigError(_("%s.%s not valid " | ||||
"('%s' is none of %s)") | ||||
% (section, name, config, validstr)) | ||||
return config | ||||
|
r23653 | def _checkunknownfile(repo, wctx, mctx, f, f2=None): | ||
|
r35289 | if wctx.isinmemory(): | ||
# Nothing to do in IMM because nothing in the "working copy" can be an | ||||
# unknown file. | ||||
# | ||||
# Note that we should bail out here, not in ``_checkunknownfiles()``, | ||||
# because that function does other useful work. | ||||
return False | ||||
|
r23653 | if f2 is None: | ||
f2 = f | ||||
|
r28088 | return (repo.wvfs.audit.check(f) | ||
and repo.wvfs.isfileorlink(f) | ||||
|
r16284 | and repo.dirstate.normalize(f) not in repo.dirstate | ||
|
r23653 | and mctx[f2].cmp(wctx[f])) | ||
|
r16093 | |||
|
r35181 | class _unknowndirschecker(object): | ||
|
r34551 | """ | ||
Look for any unknown files or directories that may have a path conflict | ||||
with a file. If any path prefix of the file exists as a file or link, | ||||
then it conflicts. If the file itself is a directory that contains any | ||||
file that is not tracked, then it conflicts. | ||||
Returns the shortest path at which a conflict occurs, or None if there is | ||||
no conflict. | ||||
""" | ||||
|
r35181 | def __init__(self): | ||
# A set of paths known to be good. This prevents repeated checking of | ||||
# dirs. It will be updated with any new dirs that are checked and found | ||||
# to be safe. | ||||
self._unknowndircache = set() | ||||
|
r34551 | |||
|
r35181 | # A set of paths that are known to be absent. This prevents repeated | ||
# checking of subdirectories that are known not to exist. It will be | ||||
# updated with any new dirs that are checked and found to be absent. | ||||
self._missingdircache = set() | ||||
|
r34551 | |||
|
r35289 | def __call__(self, repo, wctx, f): | ||
if wctx.isinmemory(): | ||||
# Nothing to do in IMM for the same reason as ``_checkunknownfile``. | ||||
return False | ||||
|
r35181 | # Check for path prefixes that exist as unknown files. | ||
for p in reversed(list(util.finddirs(f))): | ||||
if p in self._missingdircache: | ||||
return | ||||
if p in self._unknowndircache: | ||||
continue | ||||
if repo.wvfs.audit.check(p): | ||||
if (repo.wvfs.isfileorlink(p) | ||||
and repo.dirstate.normalize(p) not in repo.dirstate): | ||||
return p | ||||
if not repo.wvfs.lexists(p): | ||||
self._missingdircache.add(p) | ||||
return | ||||
self._unknowndircache.add(p) | ||||
# Check if the file conflicts with a directory containing unknown files. | ||||
if repo.wvfs.audit.check(f) and repo.wvfs.isdir(f): | ||||
# Does the directory contain any files that are not in the dirstate? | ||||
for p, dirs, files in repo.wvfs.walk(f): | ||||
for fn in files: | ||||
|
r37104 | relf = util.pconvert(repo.wvfs.reljoin(p, fn)) | ||
relf = repo.dirstate.normalize(relf) | ||||
|
r35181 | if relf not in repo.dirstate: | ||
return f | ||||
return None | ||||
|
r34551 | |||
|
r28020 | def _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce): | ||
|
r23655 | """ | ||
Considers any actions that care about the presence of conflicting unknown | ||||
files. For some actions, the result is to abort; for others, it is to | ||||
choose a different action. | ||||
""" | ||||
|
r34552 | fileconflicts = set() | ||
|
r34553 | pathconflicts = set() | ||
|
r28018 | warnconflicts = set() | ||
abortconflicts = set() | ||||
unknownconfig = _getcheckunknownconfig(repo, 'merge', 'checkunknown') | ||||
ignoredconfig = _getcheckunknownconfig(repo, 'merge', 'checkignored') | ||||
|
r34942 | pathconfig = repo.ui.configbool('experimental', 'merge.checkpathconflicts') | ||
|
r23655 | if not force: | ||
|
r27741 | def collectconflicts(conflicts, config): | ||
if config == 'abort': | ||||
abortconflicts.update(conflicts) | ||||
elif config == 'warn': | ||||
warnconflicts.update(conflicts) | ||||
|
r35181 | checkunknowndirs = _unknowndirschecker() | ||
|
r23655 | for f, (m, args, msg) in actions.iteritems(): | ||
if m in ('c', 'dc'): | ||||
if _checkunknownfile(repo, wctx, mctx, f): | ||||
|
r34552 | fileconflicts.add(f) | ||
|
r34942 | elif pathconfig and f not in wctx: | ||
|
r35289 | path = checkunknowndirs(repo, wctx, f) | ||
|
r34553 | if path is not None: | ||
pathconflicts.add(path) | ||||
|
r23655 | elif m == 'dg': | ||
if _checkunknownfile(repo, wctx, mctx, f, args[0]): | ||||
|
r34552 | fileconflicts.add(f) | ||
|
r23655 | |||
|
r34553 | allconflicts = fileconflicts | pathconflicts | ||
|
r34552 | ignoredconflicts = set([c for c in allconflicts | ||
|
r27742 | if repo.dirstate._ignore(c)]) | ||
|
r34552 | unknownconflicts = allconflicts - ignoredconflicts | ||
|
r27742 | collectconflicts(ignoredconflicts, ignoredconfig) | ||
collectconflicts(unknownconflicts, unknownconfig) | ||||
|
r28022 | else: | ||
for f, (m, args, msg) in actions.iteritems(): | ||||
if m == 'cm': | ||||
fl2, anc = args | ||||
different = _checkunknownfile(repo, wctx, mctx, f) | ||||
if repo.dirstate._ignore(f): | ||||
config = ignoredconfig | ||||
else: | ||||
config = unknownconfig | ||||
# The behavior when force is True is described by this table: | ||||
# config different mergeforce | action backup | ||||
# * n * | get n | ||||
# * y y | merge - | ||||
# abort y n | merge - (1) | ||||
# warn y n | warn + get y | ||||
# ignore y n | get y | ||||
# | ||||
# (1) this is probably the wrong behavior here -- we should | ||||
# probably abort, but some actions like rebases currently | ||||
# don't like an abort happening in the middle of | ||||
# merge.update. | ||||
if not different: | ||||
actions[f] = ('g', (fl2, False), "remote created") | ||||
elif mergeforce or config == 'abort': | ||||
actions[f] = ('m', (f, f, None, False, anc), | ||||
"remote differs from untracked local") | ||||
elif config == 'abort': | ||||
abortconflicts.add(f) | ||||
else: | ||||
if config == 'warn': | ||||
warnconflicts.add(f) | ||||
actions[f] = ('g', (fl2, True), "remote created") | ||||
|
r27741 | |||
|
r28018 | for f in sorted(abortconflicts): | ||
|
r34554 | warn = repo.ui.warn | ||
if f in pathconflicts: | ||||
if repo.wvfs.isfileorlink(f): | ||||
warn(_("%s: untracked file conflicts with directory\n") % f) | ||||
else: | ||||
warn(_("%s: untracked directory conflicts with file\n") % f) | ||||
else: | ||||
warn(_("%s: untracked file differs\n") % f) | ||||
|
r28018 | if abortconflicts: | ||
raise error.Abort(_("untracked files in working directory " | ||||
"differ from files in requested revision")) | ||||
for f in sorted(warnconflicts): | ||||
|
r34554 | if repo.wvfs.isfileorlink(f): | ||
repo.ui.warn(_("%s: replacing untracked file\n") % f) | ||||
else: | ||||
repo.ui.warn(_("%s: replacing untracked files in directory\n") % f) | ||||
|
r23655 | |||
for f, (m, args, msg) in actions.iteritems(): | ||||
if m == 'c': | ||||
|
r34553 | backup = (f in fileconflicts or f in pathconflicts or | ||
any(p in pathconflicts for p in util.finddirs(f))) | ||||
|
r27655 | flags, = args | ||
|
r27657 | actions[f] = ('g', (flags, backup), msg) | ||
|
r23655 | |||
|
r6269 | def _forgetremoved(wctx, mctx, branchmerge): | ||
|
r3107 | """ | ||
Forget removed files | ||||
If we're jumping between revisions (as opposed to merging), and if | ||||
neither the working directory nor the target rev has the file, | ||||
then we need to remove it from the dirstate, to prevent the | ||||
dirstate from listing the file when it is no longer in the | ||||
manifest. | ||||
|
r6242 | |||
If we're merging, and the other revision has removed a file | ||||
that is not present in the working directory, we need to mark it | ||||
as removed. | ||||
|
r3107 | """ | ||
|
r23640 | actions = {} | ||
m = 'f' | ||||
|
r21545 | if branchmerge: | ||
|
r23640 | m = 'r' | ||
|
r6242 | for f in wctx.deleted(): | ||
|
r6272 | if f not in mctx: | ||
|
r23640 | actions[f] = m, None, "forget deleted" | ||
|
r6242 | |||
if not branchmerge: | ||||
for f in wctx.removed(): | ||||
|
r6272 | if f not in mctx: | ||
|
r23640 | actions[f] = 'f', None, "forget removed" | ||
|
r3107 | |||
|
r23640 | return actions | ||
|
r3107 | |||
|
r20640 | def _checkcollision(repo, wmf, actions): | ||
|
r19105 | # build provisional merged manifest up | ||
pmmf = set(wmf) | ||||
|
r21545 | if actions: | ||
# k, dr, e and rd are no-op | ||||
|
r27131 | for m in 'a', 'am', 'f', 'g', 'cd', 'dc': | ||
|
r21545 | for f, args, msg in actions[m]: | ||
pmmf.add(f) | ||||
for f, args, msg in actions['r']: | ||||
pmmf.discard(f) | ||||
for f, args, msg in actions['dm']: | ||||
f2, flags = args | ||||
pmmf.discard(f2) | ||||
pmmf.add(f) | ||||
for f, args, msg in actions['dg']: | ||||
pmmf.add(f) | ||||
for f, args, msg in actions['m']: | ||||
f1, f2, fa, move, anc = args | ||||
if move: | ||||
pmmf.discard(f1) | ||||
pmmf.add(f) | ||||
|
r19105 | |||
# check case-folding collision in provisional merged manifest | ||||
foldmap = {} | ||||
|
r33807 | for f in pmmf: | ||
|
r19105 | fold = util.normcase(f) | ||
if fold in foldmap: | ||||
|
r26587 | raise error.Abort(_("case-folding collision between %s and %s") | ||
|
r19105 | % (f, foldmap[fold])) | ||
foldmap[fold] = f | ||||
|
r26661 | # check case-folding of directories | ||
foldprefix = unfoldprefix = lastfull = '' | ||||
for fold, f in sorted(foldmap.items()): | ||||
if fold.startswith(foldprefix) and not f.startswith(unfoldprefix): | ||||
# the folded prefix matches but actual casing is different | ||||
raise error.Abort(_("case-folding collision between " | ||||
"%s and directory of %s") % (lastfull, f)) | ||||
foldprefix = fold + '/' | ||||
unfoldprefix = f + '/' | ||||
lastfull = f | ||||
|
r26785 | def driverpreprocess(repo, ms, wctx, labels=None): | ||
"""run the preprocess step of the merge driver, if any | ||||
This is currently not implemented -- it's an extension point.""" | ||||
return True | ||||
def driverconclude(repo, ms, wctx, labels=None): | ||||
"""run the conclude step of the merge driver, if any | ||||
This is currently not implemented -- it's an extension point.""" | ||||
return True | ||||
|
r34556 | def _filesindirs(repo, manifest, dirs): | ||
""" | ||||
Generator that yields pairs of all the files in the manifest that are found | ||||
inside the directories listed in dirs, and which directory they are found | ||||
in. | ||||
""" | ||||
for f in manifest: | ||||
for p in util.finddirs(f): | ||||
if p in dirs: | ||||
yield f, p | ||||
break | ||||
def checkpathconflicts(repo, wctx, mctx, actions): | ||||
""" | ||||
Check if any actions introduce path conflicts in the repository, updating | ||||
actions to record or handle the path conflict accordingly. | ||||
""" | ||||
mf = wctx.manifest() | ||||
# The set of local files that conflict with a remote directory. | ||||
localconflicts = set() | ||||
# The set of directories that conflict with a remote file, and so may cause | ||||
# conflicts if they still contain any files after the merge. | ||||
remoteconflicts = set() | ||||
# The set of directories that appear as both a file and a directory in the | ||||
# remote manifest. These indicate an invalid remote manifest, which | ||||
# can't be updated to cleanly. | ||||
invalidconflicts = set() | ||||
|
r35182 | # The set of directories that contain files that are being created. | ||
createdfiledirs = set() | ||||
|
r34556 | # The set of files deleted by all the actions. | ||
deletedfiles = set() | ||||
for f, (m, args, msg) in actions.items(): | ||||
if m in ('c', 'dc', 'm', 'cm'): | ||||
# This action may create a new local file. | ||||
|
r35182 | createdfiledirs.update(util.finddirs(f)) | ||
|
r34556 | if mf.hasdir(f): | ||
# The file aliases a local directory. This might be ok if all | ||||
# the files in the local directory are being deleted. This | ||||
# will be checked once we know what all the deleted files are. | ||||
remoteconflicts.add(f) | ||||
# Track the names of all deleted files. | ||||
if m == 'r': | ||||
deletedfiles.add(f) | ||||
if m == 'm': | ||||
f1, f2, fa, move, anc = args | ||||
if move: | ||||
deletedfiles.add(f1) | ||||
if m == 'dm': | ||||
f2, flags = args | ||||
deletedfiles.add(f2) | ||||
|
r35182 | # Check all directories that contain created files for path conflicts. | ||
for p in createdfiledirs: | ||||
if p in mf: | ||||
if p in mctx: | ||||
# A file is in a directory which aliases both a local | ||||
# and a remote file. This is an internal inconsistency | ||||
# within the remote manifest. | ||||
invalidconflicts.add(p) | ||||
else: | ||||
# A file is in a directory which aliases a local file. | ||||
# We will need to rename the local file. | ||||
localconflicts.add(p) | ||||
if p in actions and actions[p][0] in ('c', 'dc', 'm', 'cm'): | ||||
# The file is in a directory which aliases a remote file. | ||||
# This is an internal inconsistency within the remote | ||||
# manifest. | ||||
invalidconflicts.add(p) | ||||
|
r34556 | # Rename all local conflicting files that have not been deleted. | ||
for p in localconflicts: | ||||
if p not in deletedfiles: | ||||
|
r36059 | ctxname = bytes(wctx).rstrip('+') | ||
|
r34556 | pnew = util.safename(p, ctxname, wctx, set(actions.keys())) | ||
actions[pnew] = ('pr', (p,), "local path conflict") | ||||
actions[p] = ('p', (pnew, 'l'), "path conflict") | ||||
if remoteconflicts: | ||||
# Check if all files in the conflicting directories have been removed. | ||||
|
r36059 | ctxname = bytes(mctx).rstrip('+') | ||
|
r34556 | for f, p in _filesindirs(repo, mf, remoteconflicts): | ||
if f not in deletedfiles: | ||||
m, args, msg = actions[p] | ||||
pnew = util.safename(p, ctxname, wctx, set(actions.keys())) | ||||
if m in ('dc', 'm'): | ||||
# Action was merge, just update target. | ||||
actions[pnew] = (m, args, msg) | ||||
else: | ||||
# Action was create, change to renamed get action. | ||||
fl = args[0] | ||||
actions[pnew] = ('dg', (p, fl), "remote path conflict") | ||||
actions[p] = ('p', (pnew, 'r'), "path conflict") | ||||
remoteconflicts.remove(p) | ||||
break | ||||
if invalidconflicts: | ||||
for p in invalidconflicts: | ||||
repo.ui.warn(_("%s: is both a file and a directory\n") % p) | ||||
raise error.Abort(_("destination manifest contains path conflicts")) | ||||
|
r27346 | def manifestmerge(repo, wctx, p2, pa, branchmerge, force, matcher, | ||
|
r32151 | acceptremote, followcopies, forcefulldiff=False): | ||
|
r3105 | """ | ||
|
r30096 | Merge wctx and p2 with ancestor pa and generate merge action list | ||
|
r3315 | |||
|
r18605 | branchmerge and force are as passed in to update | ||
|
r27346 | matcher = matcher to filter file lists | ||
|
r18778 | acceptremote = accept the incoming changes without prompting | ||
|
r3105 | """ | ||
|
r27346 | if matcher is not None and matcher.always(): | ||
matcher = None | ||||
|
r3105 | |||
|
r30581 | copy, movewithdir, diverge, renamedelete, dirmove = {}, {}, {}, {}, {} | ||
|
r8753 | |||
|
r18651 | # manifests fetched in order are going to be faster, so prime the caches | ||
[x.manifest() for x in | ||||
|
r32655 | sorted(wctx.parents() + [p2, pa], key=scmutil.intrev)] | ||
|
r18651 | |||
if followcopies: | ||||
|
r18611 | ret = copies.mergecopies(repo, wctx, p2, pa) | ||
|
r30581 | copy, movewithdir, diverge, renamedelete, dirmove = ret | ||
|
r8753 | |||
|
r32641 | boolbm = pycompat.bytestr(bool(branchmerge)) | ||
boolf = pycompat.bytestr(bool(force)) | ||||
boolm = pycompat.bytestr(bool(matcher)) | ||||
|
r8753 | repo.ui.note(_("resolving manifests\n")) | ||
|
r18605 | repo.ui.debug(" branchmerge: %s, force: %s, partial: %s\n" | ||
|
r32641 | % (boolbm, boolf, boolm)) | ||
|
r18611 | repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, wctx, p2)) | ||
|
r8753 | |||
|
r18611 | m1, m2, ma = wctx.manifest(), p2.manifest(), pa.manifest() | ||
|
r8753 | copied = set(copy.values()) | ||
|
r18134 | copied.update(movewithdir.values()) | ||
|
r3295 | |||
|
r11470 | if '.hgsubstate' in m1: | ||
|
r9783 | # check whether sub state is modified | ||
|
r28226 | if any(wctx.sub(s).dirty() for s in wctx.substate): | ||
|
r30362 | m1['.hgsubstate'] = modifiednodeid | ||
|
r9783 | |||
|
r32151 | # Don't use m2-vs-ma optimization if: | ||
# - ma is the same as m1 or m2, which we're just going to diff again later | ||||
# - The caller specifically asks for a full diff, which is useful during bid | ||||
# merge. | ||||
|
r32498 | if (pa not in ([wctx, p2] + wctx.parents()) and not forcefulldiff): | ||
|
r32151 | # Identify which files are relevant to the merge, so we can limit the | ||
# total m1-vs-m2 diff to just those files. This has significant | ||||
# performance benefits in large repositories. | ||||
relevantfiles = set(ma.diff(m2).keys()) | ||||
# For copied and moved files, we need to add the source file too. | ||||
for copykey, copyvalue in copy.iteritems(): | ||||
if copyvalue in relevantfiles: | ||||
relevantfiles.add(copykey) | ||||
|
r32863 | for movedirkey in movewithdir: | ||
|
r32151 | relevantfiles.add(movedirkey) | ||
|
r32498 | filesmatcher = scmutil.matchfiles(repo, relevantfiles) | ||
matcher = matchmod.intersectmatchers(matcher, filesmatcher) | ||||
|
r32151 | |||
|
r31257 | diff = m1.diff(m2, match=matcher) | ||
if matcher is None: | ||||
matcher = matchmod.always('', '') | ||||
|
r18822 | |||
|
r23637 | actions = {} | ||
|
r22966 | for f, ((n1, fl1), (n2, fl2)) in diff.iteritems(): | ||
|
r23473 | if n1 and n2: # file exists on both local and remote side | ||
|
r23396 | if f not in ma: | ||
|
r23397 | fa = copy.get(f, None) | ||
if fa is not None: | ||||
|
r23637 | actions[f] = ('m', (f, f, fa, False, pa.node()), | ||
"both renamed from " + fa) | ||||
|
r18338 | else: | ||
|
r23637 | actions[f] = ('m', (f, f, None, False, pa.node()), | ||
"both created") | ||||
|
r16094 | else: | ||
|
r23396 | a = ma[f] | ||
fla = ma.flags(f) | ||||
|
r23395 | nol = 'l' not in fl1 + fl2 + fla | ||
if n2 == a and fl2 == fla: | ||||
|
r34487 | actions[f] = ('k', (), "remote unchanged") | ||
|
r23395 | elif n1 == a and fl1 == fla: # local unchanged - use remote | ||
if n1 == n2: # optimization: keep local content | ||||
|
r23637 | actions[f] = ('e', (fl2,), "update permissions") | ||
|
r23395 | else: | ||
|
r27655 | actions[f] = ('g', (fl2, False), "remote is newer") | ||
|
r23395 | elif nol and n2 == a: # remote only changed 'x' | ||
|
r23637 | actions[f] = ('e', (fl2,), "update permissions") | ||
|
r23395 | elif nol and n1 == a: # local only changed 'x' | ||
|
r27655 | actions[f] = ('g', (fl1, False), "remote is newer") | ||
|
r23395 | else: # both changed something | ||
|
r23637 | actions[f] = ('m', (f, f, f, False, pa.node()), | ||
"versions differ") | ||||
|
r23473 | elif n1: # file exists only on local side | ||
|
r23474 | if f in copied: | ||
pass # we'll deal with it on m2 side | ||||
elif f in movewithdir: # directory rename, move local | ||||
|
r23473 | f2 = movewithdir[f] | ||
|
r31515 | if f2 in m2: | ||
|
r23637 | actions[f2] = ('m', (f, f2, None, True, pa.node()), | ||
"remote directory rename, both created") | ||||
|
r23475 | else: | ||
|
r23637 | actions[f2] = ('dm', (f, fl1), | ||
"remote directory rename - move from " + f) | ||||
|
r23473 | elif f in copy: | ||
f2 = copy[f] | ||||
|
r23637 | actions[f] = ('m', (f, f2, f2, False, pa.node()), | ||
"local copied/moved from " + f2) | ||||
|
r23473 | elif f in ma: # clean, a different, no remote | ||
if n1 != ma[f]: | ||||
if acceptremote: | ||||
|
r23637 | actions[f] = ('r', None, "remote delete") | ||
|
r23473 | else: | ||
|
r26962 | actions[f] = ('cd', (f, None, f, False, pa.node()), | ||
"prompt changed/deleted") | ||||
|
r30361 | elif n1 == addednodeid: | ||
|
r23473 | # This extra 'a' is added by working copy manifest to mark | ||
# the file as locally added. We should forget it instead of | ||||
# deleting it. | ||||
|
r23637 | actions[f] = ('f', None, "remote deleted") | ||
|
r20639 | else: | ||
|
r23637 | actions[f] = ('r', None, "other deleted") | ||
|
r23473 | elif n2: # file exists only on remote side | ||
|
r23474 | if f in copied: | ||
pass # we'll deal with it on m1 side | ||||
elif f in movewithdir: | ||||
|
r23473 | f2 = movewithdir[f] | ||
|
r31515 | if f2 in m1: | ||
|
r23637 | actions[f2] = ('m', (f2, f, None, False, pa.node()), | ||
"local directory rename, both created") | ||||
|
r23476 | else: | ||
|
r23637 | actions[f2] = ('dg', (f, fl2), | ||
"local directory rename - get from " + f) | ||||
|
r23473 | elif f in copy: | ||
f2 = copy[f] | ||||
|
r31515 | if f2 in m2: | ||
|
r23637 | actions[f] = ('m', (f2, f, f2, False, pa.node()), | ||
"remote copied from " + f2) | ||||
|
r23473 | else: | ||
|
r23637 | actions[f] = ('m', (f2, f, f2, True, pa.node()), | ||
"remote moved from " + f2) | ||||
|
r23473 | elif f not in ma: | ||
# local unknown, remote created: the logic is described by the | ||||
# following table: | ||||
# | ||||
# force branchmerge different | action | ||||
|
r23651 | # n * * | create | ||
|
r23650 | # y n * | create | ||
# y y n | create | ||||
|
r23473 | # y y y | merge | ||
# | ||||
# Checking whether the files are different is expensive, so we | ||||
# don't do that when we can avoid it. | ||||
|
r23649 | if not force: | ||
|
r23651 | actions[f] = ('c', (fl2,), "remote created") | ||
|
r23649 | elif not branchmerge: | ||
|
r23650 | actions[f] = ('c', (fl2,), "remote created") | ||
|
r23473 | else: | ||
|
r23654 | actions[f] = ('cm', (fl2, pa.node()), | ||
"remote created, get or merge") | ||||
|
r23473 | elif n2 != ma[f]: | ||
|
r30581 | df = None | ||
for d in dirmove: | ||||
if f.startswith(d): | ||||
# new file added in a directory that was moved | ||||
df = dirmove[d] + f[len(d):] | ||||
break | ||||
|
r31515 | if df is not None and df in m1: | ||
|
r30581 | actions[df] = ('m', (df, f, f, False, pa.node()), | ||
"local directory rename - respect move from " + f) | ||||
elif acceptremote: | ||||
|
r23651 | actions[f] = ('c', (fl2,), "remote recreating") | ||
|
r18606 | else: | ||
|
r26962 | actions[f] = ('dc', (None, f, f, False, pa.node()), | ||
"prompt deleted/changed") | ||||
|
r23651 | |||
|
r34942 | if repo.ui.configbool('experimental', 'merge.checkpathconflicts'): | ||
# If we are merging, look for path conflicts. | ||||
checkpathconflicts(repo, wctx, p2, actions) | ||||
|
r34556 | |||
|
r23526 | return actions, diverge, renamedelete | ||
|
r3105 | |||
|
r23531 | def _resolvetrivial(repo, wctx, mctx, ancestor, actions): | ||
"""Resolves false conflicts where the nodeid changed but the content | ||||
remained the same.""" | ||||
|
r36334 | # We force a copy of actions.items() because we're going to mutate | ||
# actions as we resolve trivial conflicts. | ||||
for f, (m, args, msg) in list(actions.items()): | ||||
|
r23639 | if m == 'cd' and f in ancestor and not wctx[f].cmp(ancestor[f]): | ||
|
r23531 | # local did change but ended up with same content | ||
|
r23639 | actions[f] = 'r', None, "prompt same" | ||
elif m == 'dc' and f in ancestor and not mctx[f].cmp(ancestor[f]): | ||||
|
r23531 | # remote did change but ended up with same content | ||
|
r23639 | del actions[f] # don't get = keep local deleted | ||
|
r23531 | |||
|
r27345 | def calculateupdates(repo, wctx, mctx, ancestors, branchmerge, force, | ||
|
r28020 | acceptremote, followcopies, matcher=None, | ||
mergeforce=False): | ||||
|
r33323 | """Calculate the actions needed to merge mctx into wctx using ancestors""" | ||
# Avoid cycle. | ||||
from . import sparse | ||||
|
r23385 | if len(ancestors) == 1: # default | ||
|
r23526 | actions, diverge, renamedelete = manifestmerge( | ||
|
r27346 | repo, wctx, mctx, ancestors[0], branchmerge, force, matcher, | ||
|
r23526 | acceptremote, followcopies) | ||
|
r28020 | _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce) | ||
|
r23385 | |||
else: # only when merge.preferancestor=* - the default | ||||
repo.ui.note( | ||||
_("note: merging %s and %s using bids from ancestors %s\n") % | ||||
|
r34351 | (wctx, mctx, _(' and ').join(pycompat.bytestr(anc) | ||
for anc in ancestors))) | ||||
|
r23385 | |||
# Call for bids | ||||
fbids = {} # mapping filename to bids (action method to list af actions) | ||||
|
r23526 | diverge, renamedelete = None, None | ||
|
r23385 | for ancestor in ancestors: | ||
repo.ui.note(_('\ncalculating bids for ancestor %s\n') % ancestor) | ||||
|
r23526 | actions, diverge1, renamedelete1 = manifestmerge( | ||
|
r27346 | repo, wctx, mctx, ancestor, branchmerge, force, matcher, | ||
|
r32151 | acceptremote, followcopies, forcefulldiff=True) | ||
|
r28020 | _checkunknownfiles(repo, wctx, mctx, force, actions, mergeforce) | ||
|
r26318 | |||
# Track the shortest set of warning on the theory that bid | ||||
# merge will correctly incorporate more information | ||||
if diverge is None or len(diverge1) < len(diverge): | ||||
|
r23526 | diverge = diverge1 | ||
|
r26318 | if renamedelete is None or len(renamedelete) < len(renamedelete1): | ||
|
r23526 | renamedelete = renamedelete1 | ||
|
r26318 | |||
|
r23638 | for f, a in sorted(actions.iteritems()): | ||
m, args, msg = a | ||||
repo.ui.debug(' %s: %s -> %s\n' % (f, msg, m)) | ||||
if f in fbids: | ||||
d = fbids[f] | ||||
if m in d: | ||||
d[m].append(a) | ||||
|
r23385 | else: | ||
|
r23638 | d[m] = [a] | ||
else: | ||||
fbids[f] = {m: [a]} | ||||
|
r23385 | |||
# Pick the best bid for each file | ||||
repo.ui.note(_('\nauction for merging merge bids\n')) | ||||
|
r23638 | actions = {} | ||
|
r30856 | dms = [] # filenames that have dm actions | ||
|
r23385 | for f, bids in sorted(fbids.items()): | ||
# bids is a mapping from action method to list af actions | ||||
# Consensus? | ||||
if len(bids) == 1: # all bids are the same kind of method | ||||
|
r34350 | m, l = list(bids.items())[0] | ||
|
r25151 | if all(a == l[0] for a in l[1:]): # len(bids) is > 1 | ||
|
r29242 | repo.ui.note(_(" %s: consensus for %s\n") % (f, m)) | ||
|
r23638 | actions[f] = l[0] | ||
|
r30856 | if m == 'dm': | ||
dms.append(f) | ||||
|
r23385 | continue | ||
# If keep is an option, just do it. | ||||
if 'k' in bids: | ||||
|
r29242 | repo.ui.note(_(" %s: picking 'keep' action\n") % f) | ||
|
r23638 | actions[f] = bids['k'][0] | ||
|
r23385 | continue | ||
# If there are gets and they all agree [how could they not?], do it. | ||||
if 'g' in bids: | ||||
ga0 = bids['g'][0] | ||||
|
r25151 | if all(a == ga0 for a in bids['g'][1:]): | ||
|
r29242 | repo.ui.note(_(" %s: picking 'get' action\n") % f) | ||
|
r23638 | actions[f] = ga0 | ||
|
r23385 | continue | ||
# TODO: Consider other simple actions such as mode changes | ||||
# Handle inefficient democrazy. | ||||
repo.ui.note(_(' %s: multiple bids for merge action:\n') % f) | ||||
for m, l in sorted(bids.items()): | ||||
for _f, args, msg in l: | ||||
repo.ui.note(' %s -> %s\n' % (msg, m)) | ||||
# Pick random action. TODO: Instead, prompt user when resolving | ||||
|
r34350 | m, l = list(bids.items())[0] | ||
|
r23385 | repo.ui.warn(_(' %s: ambiguous merge - picked %s action\n') % | ||
(f, m)) | ||||
|
r23638 | actions[f] = l[0] | ||
|
r30856 | if m == 'dm': | ||
dms.append(f) | ||||
|
r23385 | continue | ||
|
r30856 | # Work around 'dm' that can cause multiple actions for the same file | ||
for f in dms: | ||||
dm, (f0, flags), msg = actions[f] | ||||
assert dm == 'dm', dm | ||||
|
r30859 | if f0 in actions and actions[f0][0] == 'r': | ||
|
r30856 | # We have one bid for removing a file and another for moving it. | ||
# These two could be merged as first move and then delete ... | ||||
# but instead drop moving and just delete. | ||||
del actions[f] | ||||
|
r23385 | repo.ui.note(_('end of auction\n\n')) | ||
|
r23639 | _resolvetrivial(repo, wctx, mctx, ancestors[0], actions) | ||
|
r23640 | if wctx.rev() is None: | ||
fractions = _forgetremoved(wctx, mctx, branchmerge) | ||||
actions.update(fractions) | ||||
|
r33323 | prunedactions = sparse.filterupdatesactions(repo, wctx, mctx, branchmerge, | ||
actions) | ||||
return prunedactions, diverge, renamedelete | ||||
|
r23385 | |||
|
r34144 | def _getcwd(): | ||
try: | ||||
return pycompat.getcwd() | ||||
except OSError as err: | ||||
if err.errno == errno.ENOENT: | ||||
return None | ||||
raise | ||||
|
r33081 | def batchremove(repo, wctx, actions): | ||
|
r21392 | """apply removes to the working directory | ||
|
r18630 | |||
yields tuples for progress updates | ||||
""" | ||||
|
r18640 | verbose = repo.ui.verbose | ||
|
r34144 | cwd = _getcwd() | ||
|
r18633 | i = 0 | ||
|
r21545 | for f, args, msg in actions: | ||
|
r21392 | repo.ui.debug(" %s: %s -> r\n" % (f, msg)) | ||
|
r21551 | if verbose: | ||
repo.ui.note(_("removing %s\n") % f) | ||||
|
r33086 | wctx[f].audit() | ||
|
r21551 | try: | ||
|
r33082 | wctx[f].remove(ignoremissing=True) | ||
|
r25660 | except OSError as inst: | ||
|
r21551 | repo.ui.warn(_("update failed to remove %s: %s!\n") % | ||
(f, inst.strerror)) | ||||
|
r21392 | if i == 100: | ||
yield i, f | ||||
i = 0 | ||||
i += 1 | ||||
if i > 0: | ||||
yield i, f | ||||
|
r34144 | |||
if cwd and not _getcwd(): | ||||
# cwd was removed in the course of removing files; print a helpful | ||||
# warning. | ||||
repo.ui.warn(_("current directory was removed\n" | ||||
"(consider changing to repo root: %s)\n") % repo.root) | ||||
|
r21392 | |||
|
r33081 | def batchget(repo, mctx, wctx, actions): | ||
|
r21392 | """apply gets to the working directory | ||
mctx is the context to get from | ||||
yields tuples for progress updates | ||||
""" | ||||
verbose = repo.ui.verbose | ||||
fctx = mctx.filectx | ||||
|
r27656 | ui = repo.ui | ||
|
r21392 | i = 0 | ||
|
r28200 | with repo.wvfs.backgroundclosing(ui, expectedcount=len(actions)): | ||
|
r28199 | for f, (flags, backup), msg in actions: | ||
repo.ui.debug(" %s: %s -> g\n" % (f, msg)) | ||||
if verbose: | ||||
repo.ui.note(_("getting %s\n") % f) | ||||
|
r27656 | |||
|
r28199 | if backup: | ||
|
r34550 | # If a file or directory exists with the same name, back that | ||
# up. Otherwise, look to see if there is a file that conflicts | ||||
# with a directory this file is in, and if so, back that up. | ||||
|
r28199 | absf = repo.wjoin(f) | ||
|
r34550 | if not repo.wvfs.lexists(f): | ||
for p in util.finddirs(f): | ||||
if repo.wvfs.isfileorlink(p): | ||||
absf = repo.wjoin(p) | ||||
break | ||||
|
r28199 | orig = scmutil.origpath(ui, repo, absf) | ||
|
r34550 | if repo.wvfs.lexists(absf): | ||
util.rename(absf, orig) | ||||
|
r34039 | wctx[f].clearunknown() | ||
|
r35744 | atomictemp = ui.configbool("experimental", "update.atomic-file") | ||
wctx[f].write(fctx(f).data(), flags, backgroundclose=True, | ||||
atomictemp=atomictemp) | ||||
|
r28199 | if i == 100: | ||
yield i, f | ||||
i = 0 | ||||
i += 1 | ||||
|
r18633 | if i > 0: | ||
|
r18630 | yield i, f | ||
|
r36159 | def _prefetchfiles(repo, ctx, actions): | ||
"""Invoke ``scmutil.fileprefetchhooks()`` for the files relevant to the dict | ||||
of merge actions. ``ctx`` is the context being merged in.""" | ||||
# Skipping 'a', 'am', 'f', 'r', 'dm', 'e', 'k', 'p' and 'pr', because they | ||||
# don't touch the context to be merged in. 'cd' is skipped, because | ||||
# changed/deleted never resolves to something from the remote side. | ||||
oplist = [actions[a] for a in 'g dc dg m'.split()] | ||||
prefetch = scmutil.fileprefetchhooks | ||||
prefetch(repo, ctx, [f for sublist in oplist for f, args, msg in sublist]) | ||||
|
r34126 | |||
|
r21524 | def applyupdates(repo, actions, wctx, mctx, overwrite, labels=None): | ||
|
r11454 | """apply the merge action list to the working directory | ||
wctx is the working copy context | ||||
mctx is the context to be merged into the working copy | ||||
|
r13162 | |||
Return a tuple of counts (updated, merged, removed, unresolved) that | ||||
describes how many files were affected by the update. | ||||
|
r11454 | """ | ||
|
r3315 | |||
|
r36159 | _prefetchfiles(repo, mctx, actions) | ||
|
r27078 | updated, merged, removed = 0, 0, 0 | ||
|
r28634 | ms = mergestate.clean(repo, wctx.p1().node(), mctx.node(), labels) | ||
|
r6512 | moves = [] | ||
|
r21545 | for m, l in actions.items(): | ||
l.sort() | ||||
|
r6512 | |||
|
r27137 | # 'cd' and 'dc' actions are treated like other merge conflicts | ||
mergeactions = sorted(actions['cd']) | ||||
mergeactions.extend(sorted(actions['dc'])) | ||||
mergeactions.extend(actions['m']) | ||||
for f, args, msg in mergeactions: | ||||
|
r21551 | f1, f2, fa, move, anc = args | ||
if f == '.hgsubstate': # merged internally | ||||
continue | ||||
|
r27091 | if f1 is None: | ||
fcl = filemerge.absentfilectx(wctx, fa) | ||||
else: | ||||
repo.ui.debug(" preserving %s for resolve of %s\n" % (f1, f)) | ||||
fcl = wctx[f1] | ||||
if f2 is None: | ||||
fco = filemerge.absentfilectx(mctx, fa) | ||||
else: | ||||
fco = mctx[f2] | ||||
|
r21551 | actx = repo[anc] | ||
if fa in actx: | ||||
fca = actx[fa] | ||||
else: | ||||
|
r27091 | # TODO: move to absentfilectx | ||
|
r21551 | fca = repo.filectx(f1, fileid=nullrev) | ||
ms.add(fcl, fco, fca, f) | ||||
if f1 != f and move: | ||||
moves.append(f1) | ||||
|
r6512 | |||
|
r21390 | _updating = _('updating') | ||
_files = _('files') | ||||
progress = repo.ui.progress | ||||
|
r14398 | |||
|
r6512 | # remove renamed files after safely stored | ||
for f in moves: | ||||
|
r33283 | if wctx[f].lexists(): | ||
|
r9467 | repo.ui.debug("removing %s\n" % f) | ||
|
r33086 | wctx[f].audit() | ||
|
r33082 | wctx[f].remove() | ||
|
r5042 | |||
|
r23526 | numupdates = sum(len(l) for m, l in actions.items() if m != 'k') | ||
|
r34548 | z = 0 | ||
|
r18630 | |||
|
r21545 | if [a for a in actions['r'] if a[0] == '.hgsubstate']: | ||
|
r36026 | subrepoutil.submerge(repo, wctx, mctx, wctx, overwrite, labels) | ||
|
r18632 | |||
|
r34548 | # record path conflicts | ||
for f, args, msg in actions['p']: | ||||
f1, fo = args | ||||
s = repo.ui.status | ||||
s(_("%s: path conflict - a file or link has the same name as a " | ||||
"directory\n") % f) | ||||
if fo == 'l': | ||||
s(_("the local file has been renamed to %s\n") % f1) | ||||
else: | ||||
s(_("the remote file has been renamed to %s\n") % f1) | ||||
s(_("resolve manually then use 'hg resolve --mark %s'\n") % f) | ||||
ms.addpath(f, f1, fo) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
|
r34787 | # When merging in-memory, we can't support worker processes, so set the | ||
# per-item cost at 0 in that case. | ||||
cost = 0 if wctx.isinmemory() else 0.001 | ||||
|
r34549 | # remove in parallel (must come before resolving path conflicts and getting) | ||
|
r34787 | prog = worker.worker(repo.ui, cost, batchremove, (repo, wctx), | ||
|
r33081 | actions['r']) | ||
|
r19095 | for i, item in prog: | ||
z += i | ||||
|
r21390 | progress(_updating, z, item=item, total=numupdates, unit=_files) | ||
|
r21545 | removed = len(actions['r']) | ||
|
r21390 | |||
|
r34549 | # resolve path conflicts (must come before getting) | ||
for f, args, msg in actions['pr']: | ||||
repo.ui.debug(" %s: %s -> pr\n" % (f, msg)) | ||||
f0, = args | ||||
if wctx[f0].lexists(): | ||||
repo.ui.note(_("moving %s to %s\n") % (f0, f)) | ||||
wctx[f].audit() | ||||
wctx[f].write(wctx.filectx(f0).data(), wctx.filectx(f0).flags()) | ||||
wctx[f0].remove() | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
|
r21390 | # get in parallel | ||
|
r34787 | prog = worker.worker(repo.ui, cost, batchget, (repo, mctx, wctx), | ||
|
r33081 | actions['g']) | ||
|
r18639 | for i, item in prog: | ||
|
r18633 | z += i | ||
|
r21390 | progress(_updating, z, item=item, total=numupdates, unit=_files) | ||
|
r21545 | updated = len(actions['g']) | ||
|
r18630 | |||
|
r21545 | if [a for a in actions['g'] if a[0] == '.hgsubstate']: | ||
|
r36026 | subrepoutil.submerge(repo, wctx, mctx, wctx, overwrite, labels) | ||
|
r18632 | |||
|
r21551 | # forget (manifest only, just log it) (must come first) | ||
for f, args, msg in actions['f']: | ||||
repo.ui.debug(" %s: %s -> f\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
|
r21391 | |||
|
r21551 | # re-add (manifest only, just log it) | ||
for f, args, msg in actions['a']: | ||||
repo.ui.debug(" %s: %s -> a\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
|
r21391 | |||
|
r27131 | # re-add/mark as modified (manifest only, just log it) | ||
for f, args, msg in actions['am']: | ||||
repo.ui.debug(" %s: %s -> am\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
|
r21551 | # keep (noop, just log it) | ||
for f, args, msg in actions['k']: | ||||
repo.ui.debug(" %s: %s -> k\n" % (f, msg)) | ||||
# no progress | ||||
|
r21391 | |||
|
r21551 | # directory rename, move local | ||
for f, args, msg in actions['dm']: | ||||
repo.ui.debug(" %s: %s -> dm\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
f0, flags = args | ||||
repo.ui.note(_("moving %s to %s\n") % (f0, f)) | ||||
|
r33086 | wctx[f].audit() | ||
|
r33083 | wctx[f].write(wctx.filectx(f0).data(), flags) | ||
|
r33082 | wctx[f0].remove() | ||
|
r21551 | updated += 1 | ||
|
r21391 | |||
|
r21551 | # local directory rename, get | ||
for f, args, msg in actions['dg']: | ||||
repo.ui.debug(" %s: %s -> dg\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
f0, flags = args | ||||
repo.ui.note(_("getting %s to %s\n") % (f0, f)) | ||||
|
r33083 | wctx[f].write(mctx.filectx(f0).data(), flags) | ||
|
r21551 | updated += 1 | ||
|
r21391 | |||
|
r21551 | # exec | ||
for f, args, msg in actions['e']: | ||||
repo.ui.debug(" %s: %s -> e\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
flags, = args | ||||
|
r33086 | wctx[f].audit() | ||
|
r33084 | wctx[f].setflags('l' in flags, 'x' in flags) | ||
|
r21551 | updated += 1 | ||
|
r21391 | |||
|
r26786 | # the ordering is important here -- ms.mergedriver will raise if the merge | ||
# driver has changed, and we want to be able to bypass it when overwrite is | ||||
# True | ||||
usemergedriver = not overwrite and mergeactions and ms.mergedriver | ||||
if usemergedriver: | ||||
|
r35516 | if wctx.isinmemory(): | ||
raise error.InMemoryMergeConflictsError("in-memory merge does not " | ||||
"support mergedriver") | ||||
|
r26786 | ms.commit() | ||
proceed = driverpreprocess(repo, ms, wctx, labels=labels) | ||||
# the driver might leave some files unresolved | ||||
unresolvedf = set(ms.unresolved()) | ||||
if not proceed: | ||||
# XXX setting unresolved to at least 1 is a hack to make sure we | ||||
# error out | ||||
return updated, merged, removed, max(len(unresolvedf), 1) | ||||
newactions = [] | ||||
for f, args, msg in mergeactions: | ||||
if f in unresolvedf: | ||||
newactions.append((f, args, msg)) | ||||
mergeactions = newactions | ||||
|
r34681 | try: | ||
# premerge | ||||
tocomplete = [] | ||||
for f, args, msg in mergeactions: | ||||
repo.ui.debug(" %s: %s -> m (premerge)\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
if f == '.hgsubstate': # subrepo states need updating | ||||
|
r36026 | subrepoutil.submerge(repo, wctx, mctx, wctx.ancestor(mctx), | ||
overwrite, labels) | ||||
|
r34681 | continue | ||
wctx[f].audit() | ||||
complete, r = ms.preresolve(f, wctx) | ||||
if not complete: | ||||
numupdates += 1 | ||||
tocomplete.append((f, args, msg)) | ||||
|
r26618 | |||
|
r34681 | # merge | ||
for f, args, msg in tocomplete: | ||||
repo.ui.debug(" %s: %s -> m (merge)\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
ms.resolve(f, wctx) | ||||
|
r26292 | |||
|
r34681 | finally: | ||
ms.commit() | ||||
|
r26787 | |||
|
r27078 | unresolved = ms.unresolvedcount() | ||
|
r26787 | if usemergedriver and not unresolved and ms.mdstate() != 's': | ||
if not driverconclude(repo, ms, wctx, labels=labels): | ||||
# XXX setting unresolved to at least 1 is a hack to make sure we | ||||
# error out | ||||
|
r26975 | unresolved = max(unresolved, 1) | ||
|
r26787 | |||
ms.commit() | ||||
|
r27078 | msupdated, msmerged, msremoved = ms.counts() | ||
updated += msupdated | ||||
merged += msmerged | ||||
removed += msremoved | ||||
|
r27080 | |||
extraactions = ms.actions() | ||||
|
r29831 | if extraactions: | ||
mfiles = set(a[0] for a in actions['m']) | ||||
for k, acts in extraactions.iteritems(): | ||||
actions[k].extend(acts) | ||||
# Remove these files from actions['m'] as well. This is important | ||||
# because in recordupdates, files in actions['m'] are processed | ||||
# after files in other actions, and the merge driver might add | ||||
# files to those actions via extraactions above. This can lead to a | ||||
# file being recorded twice, with poor results. This is especially | ||||
# problematic for actions['r'] (currently only possible with the | ||||
# merge driver in the initial merge process; interrupted merges | ||||
# don't go through this flow). | ||||
# | ||||
# The real fix here is to have indexes by both file and action so | ||||
# that when the action for a file is changed it is automatically | ||||
# reflected in the other action lists. But that involves a more | ||||
# complex data structure, so this will do for now. | ||||
# | ||||
# We don't need to do the same operation for 'dc' and 'cd' because | ||||
# those lists aren't consulted again. | ||||
mfiles.difference_update(a[0] for a in acts) | ||||
actions['m'] = [a for a in actions['m'] if a[0] in mfiles] | ||||
|
r27080 | |||
|
r18640 | progress(_updating, None, total=numupdates, unit=_files) | ||
|
r3111 | |||
return updated, merged, removed, unresolved | ||||
|
r18330 | def recordupdates(repo, actions, branchmerge): | ||
|
r3315 | "record merge actions to the dirstate" | ||
|
r21551 | # remove (must come first) | ||
|
r27087 | for f, args, msg in actions.get('r', []): | ||
|
r21551 | if branchmerge: | ||
repo.dirstate.remove(f) | ||||
else: | ||||
|
r21389 | repo.dirstate.drop(f) | ||
|
r21391 | |||
|
r21551 | # forget (must come first) | ||
|
r27087 | for f, args, msg in actions.get('f', []): | ||
|
r21551 | repo.dirstate.drop(f) | ||
|
r21391 | |||
|
r34549 | # resolve path conflicts | ||
for f, args, msg in actions.get('pr', []): | ||||
f0, = args | ||||
origf0 = repo.dirstate.copied(f0) or f0 | ||||
repo.dirstate.add(f) | ||||
repo.dirstate.copy(origf0, f) | ||||
if f0 == origf0: | ||||
repo.dirstate.remove(f0) | ||||
else: | ||||
repo.dirstate.drop(f0) | ||||
|
r21551 | # re-add | ||
|
r27087 | for f, args, msg in actions.get('a', []): | ||
|
r27132 | repo.dirstate.add(f) | ||
|
r21391 | |||
|
r27131 | # re-add/mark as modified | ||
for f, args, msg in actions.get('am', []): | ||||
if branchmerge: | ||||
repo.dirstate.normallookup(f) | ||||
else: | ||||
|
r21551 | repo.dirstate.add(f) | ||
|
r21391 | |||
|
r21551 | # exec change | ||
|
r27087 | for f, args, msg in actions.get('e', []): | ||
|
r21551 | repo.dirstate.normallookup(f) | ||
|
r21391 | |||
|
r21551 | # keep | ||
|
r27087 | for f, args, msg in actions.get('k', []): | ||
|
r21551 | pass | ||
# get | ||||
|
r27087 | for f, args, msg in actions.get('g', []): | ||
|
r21551 | if branchmerge: | ||
repo.dirstate.otherparent(f) | ||||
else: | ||||
repo.dirstate.normal(f) | ||||
|
r21391 | |||
|
r21551 | # merge | ||
|
r27087 | for f, args, msg in actions.get('m', []): | ||
|
r21551 | f1, f2, fa, move, anc = args | ||
if branchmerge: | ||||
# We've done a branch merge, mark this file as merged | ||||
# so that we properly record the merger later | ||||
repo.dirstate.merge(f) | ||||
if f1 != f2: # copy/rename | ||||
|
r3308 | if move: | ||
|
r21551 | repo.dirstate.remove(f1) | ||
if f1 != f: | ||||
repo.dirstate.copy(f1, f) | ||||
else: | ||||
repo.dirstate.copy(f2, f) | ||||
else: | ||||
# We've update-merged a locally modified file, so | ||||
# we set the dirstate to emulate a normal checkout | ||||
# of that file some time in the past. Thus our | ||||
# merge will appear as a normal local file | ||||
# modification. | ||||
if f2 == f: # file not locally copied/moved | ||||
repo.dirstate.normallookup(f) | ||||
if move: | ||||
repo.dirstate.drop(f1) | ||||
|
r21391 | |||
|
r21551 | # directory rename, move local | ||
|
r27087 | for f, args, msg in actions.get('dm', []): | ||
|
r21551 | f0, flag = args | ||
if branchmerge: | ||||
repo.dirstate.add(f) | ||||
repo.dirstate.remove(f0) | ||||
repo.dirstate.copy(f0, f) | ||||
else: | ||||
repo.dirstate.normal(f) | ||||
repo.dirstate.drop(f0) | ||||
|
r21391 | |||
|
r21551 | # directory rename, get | ||
|
r27087 | for f, args, msg in actions.get('dg', []): | ||
|
r21551 | f0, flag = args | ||
if branchmerge: | ||||
repo.dirstate.add(f) | ||||
repo.dirstate.copy(f0, f) | ||||
else: | ||||
repo.dirstate.normal(f) | ||||
|
r3111 | |||
|
r27344 | def update(repo, node, branchmerge, force, ancestor=None, | ||
|
r31166 | mergeancestor=False, labels=None, matcher=None, mergeforce=False, | ||
|
r34303 | updatecheck=None, wc=None): | ||
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
|
r30902 | node = the node to update to | ||
|
r3315 | branchmerge = whether to merge between branches | ||
force = whether to force branch merging or file overwriting | ||||
|
r27344 | matcher = a matcher to filter file lists (dirstate not updated) | ||
|
r18778 | mergeancestor = whether it is merging with an ancestor. If true, | ||
we should accept the incoming changes for any prompts that occur. | ||||
If false, merging with an ancestor (fast-forward) is only allowed | ||||
between different named branches. This flag is used by rebase extension | ||||
as a temporary fix and should be avoided in general. | ||||
|
r28019 | labels = labels to use for base, local and other | ||
|
r28020 | mergeforce = whether the merge was run with 'merge --force' (deprecated): if | ||
this is True, then 'force' should be True as well. | ||||
|
r9716 | |||
|
r34920 | The table below shows all the behaviors of the update command given the | ||
-c/--check and -C/--clean or no options, whether the working directory is | ||||
dirty, whether a revision is specified, and the relationship of the parent | ||||
rev to the target rev (linear or not). Match from top first. The -n | ||||
option doesn't exist on the command line, but represents the | ||||
|
r31168 | experimental.updatecheck=noconflict option. | ||
|
r9716 | |||
|
r12279 | This logic is tested by test-update-branches.t. | ||
|
r9716 | |||
|
r31168 | -c -C -n -m dirty rev linear | result | ||
y y * * * * * | (1) | ||||
y * y * * * * | (1) | ||||
y * * y * * * | (1) | ||||
* y y * * * * | (1) | ||||
* y * y * * * | (1) | ||||
* * y y * * * | (1) | ||||
* * * * * n n | x | ||||
* * * * n * * | ok | ||||
n n n n y * y | merge | ||||
n n n n y y n | (2) | ||||
n n n y y * * | merge | ||||
n n y n y * * | merge if no conflict | ||||
n y n n y * * | discard | ||||
y n n n y * * | (3) | ||||
|
r9716 | |||
x = can't happen | ||||
* = don't-care | ||||
|
r31161 | 1 = incompatible options (checked in commands.py) | ||
2 = abort: uncommitted changes (commit or update --clean to discard changes) | ||||
3 = abort: uncommitted changes (checked in commands.py) | ||||
|
r13162 | |||
|
r34303 | The merge is performed inside ``wc``, a workingctx-like objects. It defaults | ||
to repo[None] if None is passed. | ||||
|
r13162 | Return the same tuple as applyupdates(). | ||
|
r3315 | """ | ||
|
r33321 | # Avoid cycle. | ||
from . import sparse | ||||
|
r2815 | |||
|
r31166 | # This function used to find the default destination if node was None, but | ||
|
r30902 | # that's now in destutil.py. | ||
assert node is not None | ||||
|
r31166 | if not branchmerge and not force: | ||
# TODO: remove the default once all callers that pass branchmerge=False | ||||
# and force=False pass a value for updatecheck. We may want to allow | ||||
# updatecheck='abort' to better suppport some of these callers. | ||||
if updatecheck is None: | ||||
updatecheck = 'linear' | ||||
|
r31168 | assert updatecheck in ('none', 'linear', 'noconflict') | ||
|
r27344 | # If we're doing a partial update, we need to skip updating | ||
# the dirstate, so make a note of any partial-ness to the | ||||
# update here. | ||||
if matcher is None or matcher.always(): | ||||
partial = False | ||||
else: | ||||
partial = True | ||||
|
r27852 | with repo.wlock(): | ||
|
r34303 | if wc is None: | ||
wc = repo[None] | ||||
|
r20279 | pl = wc.parents() | ||
p1 = pl[0] | ||||
|
r21081 | pas = [None] | ||
|
r23405 | if ancestor is not None: | ||
|
r21081 | pas = [repo[ancestor]] | ||
|
r20279 | |||
|
r4915 | overwrite = force and not branchmerge | ||
|
r20279 | |||
p2 = repo[node] | ||||
|
r21081 | if pas[0] is None: | ||
|
r34481 | if repo.ui.configlist('merge', 'preferancestor') == ['*']: | ||
|
r21128 | cahs = repo.changelog.commonancestorsheads(p1.node(), p2.node()) | ||
pas = [repo[anc] for anc in (sorted(cahs) or [nullid])] | ||||
else: | ||||
|
r22179 | pas = [p1.ancestor(p2, warn=branchmerge)] | ||
|
r13874 | |||
|
r36195 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), bytes(p1), bytes(p2) | ||
|
r3314 | |||
|
r4915 | ### check phase | ||
|
r27316 | if not overwrite: | ||
if len(pl) > 1: | ||||
raise error.Abort(_("outstanding uncommitted merge")) | ||||
ms = mergestate.read(repo) | ||||
if list(ms.unresolved()): | ||||
raise error.Abort(_("outstanding merge conflicts")) | ||||
|
r6375 | if branchmerge: | ||
|
r21081 | if pas == [p2]: | ||
|
r26587 | raise error.Abort(_("merging with a working directory ancestor" | ||
|
r11417 | " has no effect")) | ||
|
r21081 | elif pas == [p1]: | ||
|
r31379 | if not mergeancestor and wc.branch() == p2.branch(): | ||
|
r26587 | raise error.Abort(_("nothing to merge"), | ||
|
r15619 | hint=_("use 'hg update' " | ||
"or check 'hg heads'")) | ||||
|
r6375 | if not force and (wc.files() or wc.deleted()): | ||
|
r26587 | raise error.Abort(_("uncommitted changes"), | ||
|
r15619 | hint=_("use 'hg status' to list changes")) | ||
|
r35285 | if not wc.isinmemory(): | ||
for s in sorted(wc.substate): | ||||
wc.sub(s).bailifchanged() | ||||
|
r13437 | |||
|
r6375 | elif not overwrite: | ||
|
r19929 | if p1 == p2: # no-op update | ||
# call the hooks and exit early | ||||
repo.hook('preupdate', throw=True, parent1=xp2, parent2='') | ||||
repo.hook('update', parent1=xp2, parent2='', error=0) | ||||
return 0, 0, 0, 0 | ||||
|
r31166 | if (updatecheck == 'linear' and | ||
pas not in ([p1], [p2])): # nonlinear | ||||
|
r18985 | dirty = wc.dirty(missing=True) | ||
|
r30902 | if dirty: | ||
|
r18985 | # Branching is a bit strange to ensure we do the minimal | ||
r33147 | # amount of call to obsutil.foreground. | |||
foreground = obsutil.foreground(repo, [p1.node()]) | ||||
|
r18985 | # note: the <node> variable contains a random identifier | ||
if repo[node].node() in foreground: | ||||
|
r30901 | pass # allow updating to successors | ||
|
r30902 | else: | ||
|
r19799 | msg = _("uncommitted changes") | ||
|
r30902 | hint = _("commit or update --clean to discard changes") | ||
|
r30961 | raise error.UpdateAbort(msg, hint=hint) | ||
|
r18985 | else: | ||
# Allow jumping branches if clean and specific rev given | ||||
|
r30901 | pass | ||
if overwrite: | ||||
pas = [wc] | ||||
elif not branchmerge: | ||||
pas = [p1] | ||||
|
r2814 | |||
|
r25843 | # deprecated config: merge.followcopies | ||
|
r33499 | followcopies = repo.ui.configbool('merge', 'followcopies') | ||
|
r21080 | if overwrite: | ||
|
r30200 | followcopies = False | ||
elif not pas[0]: | ||||
followcopies = False | ||||
if not branchmerge and not wc.dirty(missing=True): | ||||
followcopies = False | ||||
|
r21080 | |||
|
r4915 | ### calculate phase | ||
|
r23641 | actionbyfile, diverge, renamedelete = calculateupdates( | ||
|
r27345 | repo, wc, p2, pas, branchmerge, force, mergeancestor, | ||
|
r28020 | followcopies, matcher=matcher, mergeforce=mergeforce) | ||
|
r27951 | |||
|
r31168 | if updatecheck == 'noconflict': | ||
for f, (m, args, msg) in actionbyfile.iteritems(): | ||||
|
r34549 | if m not in ('g', 'k', 'e', 'r', 'pr'): | ||
|
r31175 | msg = _("conflicting changes") | ||
hint = _("commit or update --clean to discard changes") | ||||
|
r31168 | raise error.Abort(msg, hint=hint) | ||
|
r27951 | # Prompt and create actions. Most of this is in the resolve phase | ||
# already, but we can't handle .hgsubstate in filemerge or | ||||
|
r36026 | # subrepoutil.submerge yet so we have to keep prompting for it. | ||
|
r27951 | if '.hgsubstate' in actionbyfile: | ||
f = '.hgsubstate' | ||||
m, args, msg = actionbyfile[f] | ||||
|
r29774 | prompts = filemerge.partextras(labels) | ||
prompts['f'] = f | ||||
|
r27951 | if m == 'cd': | ||
if repo.ui.promptchoice( | ||||
|
r29775 | _("local%(l)s changed %(f)s which other%(o)s deleted\n" | ||
|
r27951 | "use (c)hanged version or (d)elete?" | ||
|
r29774 | "$$ &Changed $$ &Delete") % prompts, 0): | ||
|
r27951 | actionbyfile[f] = ('r', None, "prompt delete") | ||
elif f in p1: | ||||
actionbyfile[f] = ('am', None, "prompt keep") | ||||
else: | ||||
actionbyfile[f] = ('a', None, "prompt keep") | ||||
elif m == 'dc': | ||||
f1, f2, fa, move, anc = args | ||||
flags = p2[f2].flags() | ||||
if repo.ui.promptchoice( | ||||
|
r29775 | _("other%(o)s changed %(f)s which local%(l)s deleted\n" | ||
|
r27951 | "use (c)hanged version or leave (d)eleted?" | ||
|
r29774 | "$$ &Changed $$ &Deleted") % prompts, 0) == 0: | ||
|
r27951 | actionbyfile[f] = ('g', (flags, False), "prompt recreating") | ||
else: | ||||
del actionbyfile[f] | ||||
|
r23641 | # Convert to dictionary-of-lists format | ||
|
r34548 | actions = dict((m, []) | ||
|
r34549 | for m in 'a am f g cd dc r dm dg m e k p pr'.split()) | ||
|
r23641 | for f, (m, args, msg) in actionbyfile.iteritems(): | ||
if m not in actions: | ||||
actions[m] = [] | ||||
actions[m].append((f, args, msg)) | ||||
|
r2775 | |||
|
r29889 | if not util.fscasesensitive(repo.path): | ||
|
r23544 | # check collision between files only in p2 for clean update | ||
if (not branchmerge and | ||||
(force or not wc.dirty(missing=True, branch=False))): | ||||
_checkcollision(repo, p2.manifest(), None) | ||||
else: | ||||
_checkcollision(repo, wc.manifest(), actions) | ||||
|
r23525 | # divergent renames | ||
|
r23526 | for f, fl in sorted(diverge.iteritems()): | ||
|
r23525 | repo.ui.warn(_("note: possible conflict - %s was renamed " | ||
"multiple times to:\n") % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
# rename and delete | ||||
|
r23526 | for f, fl in sorted(renamedelete.iteritems()): | ||
|
r23525 | repo.ui.warn(_("note: possible conflict - %s was deleted " | ||
"and renamed to:\n") % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
|
r26957 | ### apply phase | ||
if not branchmerge: # just jump to the new rev | ||||
fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||||
|
r35285 | if not partial and not wc.isinmemory(): | ||
|
r26957 | repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||
# note that we're in the middle of an update | ||||
repo.vfs.write('updatestate', p2.hex()) | ||||
|
r34886 | # Advertise fsmonitor when its presence could be useful. | ||
# | ||||
# We only advertise when performing an update from an empty working | ||||
# directory. This typically only occurs during initial clone. | ||||
# | ||||
# We give users a mechanism to disable the warning in case it is | ||||
# annoying. | ||||
# | ||||
# We only allow on Linux and MacOS because that's where fsmonitor is | ||||
# considered stable. | ||||
fsmonitorwarning = repo.ui.configbool('fsmonitor', 'warn_when_unused') | ||||
fsmonitorthreshold = repo.ui.configint('fsmonitor', | ||||
'warn_update_file_count') | ||||
try: | ||||
|
r36027 | # avoid cycle: extensions -> cmdutil -> merge | ||
from . import extensions | ||||
|
r34886 | extensions.find('fsmonitor') | ||
fsmonitorenabled = repo.ui.config('fsmonitor', 'mode') != 'off' | ||||
# We intentionally don't look at whether fsmonitor has disabled | ||||
# itself because a) fsmonitor may have already printed a warning | ||||
# b) we only care about the config state here. | ||||
except KeyError: | ||||
fsmonitorenabled = False | ||||
if (fsmonitorwarning | ||||
and not fsmonitorenabled | ||||
and p1.node() == nullid | ||||
and len(actions['g']) >= fsmonitorthreshold | ||||
and pycompat.sysplatform.startswith(('linux', 'darwin'))): | ||||
repo.ui.warn( | ||||
_('(warning: large working directory being used without ' | ||||
'fsmonitor enabled; enable fsmonitor to improve performance; ' | ||||
'see "hg help -e fsmonitor")\n')) | ||||
|
r26957 | stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels) | ||
|
r35285 | if not partial and not wc.isinmemory(): | ||
|
r32351 | with repo.dirstate.parentchange(): | ||
repo.setparents(fp1, fp2) | ||||
recordupdates(repo, actions, branchmerge) | ||||
# update completed, clear state | ||||
util.unlink(repo.vfs.join('updatestate')) | ||||
|
r19482 | |||
|
r32351 | if not branchmerge: | ||
repo.dirstate.setbranch(p2.branch()) | ||||
|
r10492 | |||
|
r33321 | # If we're updating to a location, clean up any stale temporary includes | ||
# (ex: this happens during hg rebase --abort). | ||||
if not branchmerge: | ||||
sparse.prunetemporaryincludes(repo) | ||||
|
r10492 | if not partial: | ||
|
r26752 | repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||
|
r10492 | return stats | ||
|
r22902 | |||
|
r27267 | def graft(repo, ctx, pctx, labels, keepparent=False): | ||
|
r22902 | """Do a graft-like merge. | ||
This is a merge where the merge ancestor is chosen such that one | ||||
or more changesets are grafted onto the current changeset. In | ||||
addition to the merge, this fixes up the dirstate to include only | ||||
|
r27267 | a single parent (if keepparent is False) and tries to duplicate any | ||
renames/copies appropriately. | ||||
|
r22902 | |||
ctx - changeset to rebase | ||||
pctx - merge base, usually ctx.p1() | ||||
labels - merge labels eg ['local', 'graft'] | ||||
|
r27267 | keepparent - keep second parent if any | ||
|
r22902 | |||
""" | ||||
|
r24643 | # If we're grafting a descendant onto an ancestor, be sure to pass | ||
# mergeancestor=True to update. This does two things: 1) allows the merge if | ||||
# the destination is the same as the parent of the ctx (so we can use graft | ||||
# to copy commits), and 2) informs update that the incoming changes are | ||||
# newer than the destination so it doesn't prompt about "remote changed foo | ||||
# which local deleted". | ||||
mergeancestor = repo.changelog.isancestor(repo['.'].node(), ctx.node()) | ||||
|
r22902 | |||
|
r27344 | stats = update(repo, ctx.node(), True, True, pctx.node(), | ||
|
r24643 | mergeancestor=mergeancestor, labels=labels) | ||
|
r27267 | pother = nullid | ||
parents = ctx.parents() | ||||
if keepparent and len(parents) == 2 and pctx in parents: | ||||
parents.remove(pctx) | ||||
pother = parents[0].node() | ||||
|
r32351 | with repo.dirstate.parentchange(): | ||
repo.setparents(repo['.'].node(), pother) | ||||
repo.dirstate.write(repo.currenttransaction()) | ||||
# fix up dirstate for copies and renames | ||||
|
r34788 | copies.duplicatecopies(repo, repo[None], ctx.rev(), pctx.rev()) | ||
|
r22902 | return stats | ||