merge.py
1160 lines
| 42.3 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 | |||
|
r20590 | import struct | ||
|
r6518 | from node import nullid, nullrev, hex, bin | ||
|
r3891 | from i18n import _ | ||
|
r18985 | from mercurial import obsolete | ||
|
r22964 | import error as errormod, util, filemerge, copies, subrepo, worker | ||
|
r8312 | import errno, os, shutil | ||
|
r6512 | |||
|
r20590 | _pack = struct.pack | ||
_unpack = struct.unpack | ||||
|
r20593 | def _droponode(data): | ||
# used for compatibility for v1 | ||||
bits = data.split("\0") | ||||
bits = bits[:-2] + bits[-1:] | ||||
return "\0".join(bits) | ||||
|
r6512 | class mergestate(object): | ||
|
r20590 | '''track 3-way merge state of individual files | ||
it is stored on disk when needed. Two file are used, one with an old | ||||
format, one with a new format. Both contains similar data, but the new | ||||
format can store new kind of field. | ||||
Current new format is a list of arbitrary record of the form: | ||||
[type][length][content] | ||||
Type is a single character, length is a 4 bytes integer, content is an | ||||
|
r20607 | arbitrary suites of bytes of length `length`. | ||
|
r20590 | |||
Type should be a letter. Capital letter are mandatory record, Mercurial | ||||
should abort if they are unknown. lower case record can be safely ignored. | ||||
Currently known record: | ||||
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 | ||
''' | ||||
statepathv1 = "merge/state" | ||||
statepathv2 = "merge/state2" | ||||
|
r20651 | |||
|
r6512 | def __init__(self, repo): | ||
self._repo = repo | ||||
|
r12369 | self._dirty = False | ||
|
r6518 | self._read() | ||
|
r20651 | |||
|
r20591 | def reset(self, node=None, other=None): | ||
|
r6512 | self._state = {} | ||
|
r21261 | self._local = None | ||
self._other = None | ||||
|
r7848 | if node: | ||
self._local = node | ||||
|
r20591 | self._other = other | ||
|
r6512 | shutil.rmtree(self._repo.join("merge"), True) | ||
|
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 = {} | ||
|
r21261 | self._local = None | ||
self._other = None | ||||
|
r20589 | records = self._readrecords() | ||
for rtype, record in records: | ||||
if rtype == 'L': | ||||
self._local = bin(record) | ||||
|
r20591 | elif rtype == 'O': | ||
self._other = bin(record) | ||||
|
r20589 | elif rtype == "F": | ||
bits = record.split("\0") | ||||
self._state[bits[0]] = bits[1:] | ||||
elif not rtype.islower(): | ||||
|
r20868 | raise util.Abort(_('unsupported merge state record: %s') | ||
% rtype) | ||||
|
r20589 | self._dirty = False | ||
|
r20651 | |||
|
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() | ||||
|
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: | ||||
|
r20590 | # v1 file is newer than v2 file, use it | ||
|
r20592 | # 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())) | ||||
|
r20593 | # 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)) | ||||
|
r20590 | return v1records | ||
else: | ||||
return v2records | ||||
|
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: | ||
|
r20590 | f = self._repo.opener(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() | ||
|
r6518 | except IOError, err: | ||
if err.errno != errno.ENOENT: | ||||
raise | ||||
|
r20589 | return records | ||
|
r20651 | |||
|
r20590 | def _readrecordsv2(self): | ||
|
r20652 | """read on disk merge state for version 2 file | ||
returns list of record [(TYPE, data), ...] | ||||
""" | ||||
|
r20590 | records = [] | ||
try: | ||||
f = self._repo.opener(self.statepathv2) | ||||
data = f.read() | ||||
off = 0 | ||||
end = len(data) | ||||
while off < end: | ||||
rtype = data[off] | ||||
off += 1 | ||||
|
r20607 | length = _unpack('>I', data[off:(off + 4)])[0] | ||
|
r20590 | off += 4 | ||
|
r20607 | record = data[off:(off + length)] | ||
off += length | ||||
|
r20590 | records.append((rtype, record)) | ||
f.close() | ||||
except IOError, err: | ||||
if err.errno != errno.ENOENT: | ||||
raise | ||||
return records | ||||
|
r20651 | |||
|
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 \ | ||||
self._repo.opener.exists(self.statepathv1) or \ | ||||
self._repo.opener.exists(self.statepathv2) | ||||
|
r12369 | def commit(self): | ||
|
r20652 | """Write current state on disk (if necessary)""" | ||
|
r12369 | if self._dirty: | ||
|
r20589 | records = [] | ||
records.append(("L", hex(self._local))) | ||||
|
r20591 | records.append(("O", hex(self._other))) | ||
|
r12369 | for d, v in self._state.iteritems(): | ||
|
r20589 | records.append(("F", "\0".join([d] + v))) | ||
self._writerecords(records) | ||||
|
r12369 | self._dirty = False | ||
|
r20651 | |||
|
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""" | ||
|
r20590 | f = self._repo.opener(self.statepathv1, "w") | ||
|
r20589 | irecords = iter(records) | ||
lrecords = irecords.next() | ||||
assert lrecords[0] == 'L' | ||||
f.write(hex(self._local) + "\n") | ||||
for rtype, data in irecords: | ||||
if rtype == "F": | ||||
|
r20593 | f.write("%s\n" % _droponode(data)) | ||
|
r20589 | f.close() | ||
|
r20651 | |||
|
r20590 | def _writerecordsv2(self, records): | ||
|
r20652 | """Write current state on disk in a version 2 file""" | ||
|
r20590 | f = self._repo.opener(self.statepathv2, "w") | ||
for key, data in records: | ||||
assert len(key) == 1 | ||||
format = ">sI%is" % len(data) | ||||
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. | ||||
""" | ||||
|
r6517 | hash = util.sha1(fcl.path()).hexdigest() | ||
|
r14168 | self._repo.opener.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()] | ||||
|
r12369 | self._dirty = True | ||
|
r20651 | |||
|
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 | |||
|
r21266 | def unresolved(self): | ||
"""Obtain the paths of unresolved files.""" | ||||
for f, entry in self._state.items(): | ||||
if entry[0] == 'u': | ||||
yield f | ||||
|
r21524 | def resolve(self, dfile, wctx, labels=None): | ||
|
r20652 | """rerun merge process for file path `dfile`""" | ||
|
r6512 | if self[dfile] == 'r': | ||
return 0 | ||||
|
r20593 | stateentry = self._state[dfile] | ||
state, hash, lfile, afile, anode, ofile, onode, flags = stateentry | ||||
|
r20594 | octx = self._repo[self._other] | ||
|
r18338 | fcd = wctx[dfile] | ||
fco = octx[ofile] | ||||
fca = self._repo.filectx(afile, fileid=anode) | ||||
# "premerge" x flags | ||||
flo = fco.flags() | ||||
fla = fca.flags() | ||||
if 'x' in flags + flo + fla and 'l' not in flags + flo + fla: | ||||
if fca.node() == nullid: | ||||
self._repo.ui.warn(_('warning: cannot merge flags for %s\n') % | ||||
afile) | ||||
elif flags == fla: | ||||
flags = flo | ||||
# restore local | ||||
|
r6512 | f = self._repo.opener("merge/" + hash) | ||
self._repo.wwrite(dfile, f.read(), flags) | ||||
|
r13400 | f.close() | ||
|
r21524 | r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca, | ||
labels=labels) | ||||
|
r13536 | if r is None: | ||
# no real conflict | ||||
del self._state[dfile] | ||||
|
r20792 | self._dirty = True | ||
|
r13536 | elif not r: | ||
|
r6512 | self.mark(dfile, 'r') | ||
return r | ||||
|
r2775 | |||
|
r16093 | def _checkunknownfile(repo, wctx, mctx, f): | ||
return (not repo.dirstate._ignore(f) | ||||
|
r16534 | and os.path.isfile(repo.wjoin(f)) | ||
|
r19157 | and repo.wopener.audit.check(f) | ||
|
r16284 | and repo.dirstate.normalize(f) not in repo.dirstate | ||
|
r16093 | and mctx[f].cmp(wctx[f])) | ||
def _checkunknown(repo, wctx, mctx): | ||||
|
r3315 | "check for collisions between unknown files and files in mctx" | ||
|
r15894 | |||
error = False | ||||
|
r16093 | for f in mctx: | ||
if f not in wctx and _checkunknownfile(repo, wctx, mctx, f): | ||||
|
r15894 | error = True | ||
|
r16093 | wctx._repo.ui.warn(_("%s: untracked file differs\n") % f) | ||
|
r15894 | if error: | ||
raise util.Abort(_("untracked files in working directory differ " | ||||
"from files in requested revision")) | ||||
|
r3107 | |||
|
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 | """ | ||
|
r21545 | ractions = [] | ||
factions = xactions = [] | ||||
if branchmerge: | ||||
xactions = ractions | ||||
|
r6242 | for f in wctx.deleted(): | ||
|
r6272 | if f not in mctx: | ||
|
r21545 | xactions.append((f, None, "forget deleted")) | ||
|
r6242 | |||
if not branchmerge: | ||||
for f in wctx.removed(): | ||||
|
r6272 | if f not in mctx: | ||
|
r21545 | factions.append((f, None, "forget removed")) | ||
|
r3107 | |||
|
r21545 | return ractions, factions | ||
|
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 | ||||
for m in 'a', 'f', 'g', 'cd', 'dc': | ||||
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']: | ||||
f2, flags = args | ||||
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 = {} | ||||
for f in sorted(pmmf): | ||||
fold = util.normcase(f) | ||||
if fold in foldmap: | ||||
raise util.Abort(_("case-folding collision between %s and %s") | ||||
% (f, foldmap[fold])) | ||||
foldmap[fold] = f | ||||
|
r18778 | def manifestmerge(repo, wctx, p2, pa, branchmerge, force, partial, | ||
|
r21080 | acceptremote, followcopies): | ||
|
r3105 | """ | ||
|
r11817 | Merge p1 and p2 with ancestor pa and generate merge action list | ||
|
r3315 | |||
|
r18605 | branchmerge and force are as passed in to update | ||
|
r3315 | partial = function to filter file lists | ||
|
r18778 | acceptremote = accept the incoming changes without prompting | ||
|
r3105 | """ | ||
|
r21545 | actions = dict((m, []) for m in 'a f g cd dc r dm dg m dr e rd k'.split()) | ||
copy, movewithdir = {}, {} | ||||
|
r8753 | |||
|
r18651 | # manifests fetched in order are going to be faster, so prime the caches | ||
[x.manifest() for x in | ||||
sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())] | ||||
if followcopies: | ||||
|
r18611 | ret = copies.mergecopies(repo, wctx, p2, pa) | ||
|
r18134 | copy, movewithdir, diverge, renamedelete = ret | ||
|
r8753 | for of, fl in diverge.iteritems(): | ||
|
r21545 | actions['dr'].append((of, (fl,), "divergent renames")) | ||
|
r16794 | for of, fl in renamedelete.iteritems(): | ||
|
r21545 | actions['rd'].append((of, (fl,), "rename and delete")) | ||
|
r8753 | |||
repo.ui.note(_("resolving manifests\n")) | ||||
|
r18605 | repo.ui.debug(" branchmerge: %s, force: %s, partial: %s\n" | ||
% (bool(branchmerge), bool(force), bool(partial))) | ||||
|
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 | ||
|
r18611 | for s in sorted(wctx.substate): | ||
if wctx.sub(s).dirty(): | ||||
|
r9783 | m1['.hgsubstate'] += "+" | ||
break | ||||
|
r20640 | aborts = [] | ||
|
r3105 | # Compare manifests | ||
|
r22964 | diff = m1.diff(m2) | ||
|
r18822 | |||
|
r22966 | for f, ((n1, fl1), (n2, fl2)) in diff.iteritems(): | ||
|
r3248 | if partial and not partial(f): | ||
continue | ||||
|
r18822 | if n1 and n2: | ||
|
r20642 | fa = f | ||
a = ma.get(f, nullid) | ||||
if a == nullid: | ||||
fa = copy.get(f, f) | ||||
# Note: f as default is wrong - we can't really make a 3-way | ||||
# merge without an ancestor file. | ||||
fla = ma.flags(fa) | ||||
|
r18338 | nol = 'l' not in fl1 + fl2 + fla | ||
|
r18822 | if n2 == a and fl2 == fla: | ||
|
r21545 | actions['k'].append((f, (), "keep")) # remote unchanged | ||
|
r18818 | elif n1 == a and fl1 == fla: # local unchanged - use remote | ||
if n1 == n2: # optimization: keep local content | ||||
|
r21545 | actions['e'].append((f, (fl2,), "update permissions")) | ||
|
r18338 | else: | ||
|
r21545 | actions['g'].append((f, (fl2,), "remote is newer")) | ||
|
r18338 | elif nol and n2 == a: # remote only changed 'x' | ||
|
r21545 | actions['e'].append((f, (fl2,), "update permissions")) | ||
|
r18818 | elif nol and n1 == a: # local only changed 'x' | ||
|
r21545 | actions['g'].append((f, (fl1,), "remote is newer")) | ||
|
r18338 | else: # both changed something | ||
|
r21545 | actions['m'].append((f, (f, f, fa, False, pa.node()), | ||
|
r20943 | "versions differ")) | ||
|
r8752 | elif f in copied: # files we'll deal with on m2 side | ||
pass | ||||
|
r20944 | elif n1 and f in movewithdir: # directory rename, move local | ||
|
r18134 | f2 = movewithdir[f] | ||
|
r21545 | actions['dm'].append((f2, (f, fl1), | ||
|
r20944 | "remote directory rename - move from " + f)) | ||
|
r18822 | elif n1 and f in copy: | ||
|
r3249 | f2 = copy[f] | ||
|
r21545 | actions['m'].append((f, (f, f2, f2, False, pa.node()), | ||
|
r20945 | "local copied/moved from " + f2)) | ||
|
r18822 | elif n1 and f in ma: # clean, a different, no remote | ||
|
r18818 | if n1 != ma[f]: | ||
|
r20639 | if acceptremote: | ||
|
r21545 | actions['r'].append((f, None, "remote delete")) | ||
|
r20639 | else: | ||
|
r21545 | actions['cd'].append((f, None, "prompt changed/deleted")) | ||
|
r18818 | elif n1[20:] == "a": # added, no remote | ||
|
r21545 | actions['f'].append((f, None, "remote deleted")) | ||
|
r16094 | else: | ||
|
r21545 | actions['r'].append((f, None, "other deleted")) | ||
|
r18822 | elif n2 and f in movewithdir: | ||
|
r18134 | f2 = movewithdir[f] | ||
|
r21545 | actions['dg'].append((f2, (f, fl2), | ||
|
r20944 | "local directory rename - get from " + f)) | ||
|
r18822 | elif n2 and f in copy: | ||
|
r3249 | f2 = copy[f] | ||
|
r18339 | if f2 in m2: | ||
|
r21545 | actions['m'].append((f, (f2, f, f2, False, pa.node()), | ||
|
r20945 | "remote copied from " + f2)) | ||
|
r18339 | else: | ||
|
r21545 | actions['m'].append((f, (f2, f, f2, True, pa.node()), | ||
|
r20945 | "remote moved from " + f2)) | ||
|
r18822 | elif n2 and f not in ma: | ||
|
r18606 | # local unknown, remote created: the logic is described by the | ||
# following table: | ||||
# | ||||
# force branchmerge different | action | ||||
# n * n | get | ||||
# n * y | abort | ||||
# y n * | get | ||||
# y y n | get | ||||
# y y y | merge | ||||
# | ||||
# Checking whether the files are different is expensive, so we | ||||
# don't do that when we can avoid it. | ||||
if force and not branchmerge: | ||||
|
r21545 | actions['g'].append((f, (fl2,), "remote created")) | ||
|
r18339 | else: | ||
|
r18611 | different = _checkunknownfile(repo, wctx, p2, f) | ||
|
r18606 | if force and branchmerge and different: | ||
|
r20897 | # FIXME: This is wrong - f is not in ma ... | ||
|
r21545 | actions['m'].append((f, (f, f, f, False, pa.node()), | ||
|
r18606 | "remote differs from untracked local")) | ||
elif not force and different: | ||||
aborts.append((f, "ud")) | ||||
else: | ||||
|
r21545 | actions['g'].append((f, (fl2,), "remote created")) | ||
|
r18822 | elif n2 and n2 != ma[f]: | ||
|
r20415 | different = _checkunknownfile(repo, wctx, p2, f) | ||
if not force and different: | ||||
aborts.append((f, "ud")) | ||||
else: | ||||
# if different: old untracked f may be overwritten and lost | ||||
|
r20639 | if acceptremote: | ||
|
r21545 | actions['g'].append((f, (m2.flags(f),), | ||
|
r20639 | "remote recreating")) | ||
else: | ||||
|
r21545 | actions['dc'].append((f, (m2.flags(f),), | ||
|
r20640 | "prompt deleted/changed")) | ||
|
r18539 | |||
|
r18606 | for f, m in sorted(aborts): | ||
if m == "ud": | ||||
repo.ui.warn(_("%s: untracked file differs\n") % f) | ||||
else: assert False, m | ||||
if aborts: | ||||
raise util.Abort(_("untracked files in working directory differ " | ||||
"from files in requested revision")) | ||||
|
r19105 | if not util.checkcase(repo.path): | ||
# check collision between files only in p2 for clean update | ||||
if (not branchmerge and | ||||
(force or not wctx.dirty(missing=True, branch=False))): | ||||
|
r21545 | _checkcollision(repo, m2, None) | ||
|
r19105 | else: | ||
|
r20640 | _checkcollision(repo, m1, actions) | ||
|
r19105 | |||
|
r18330 | return actions | ||
|
r3105 | |||
|
r21392 | def batchremove(repo, actions): | ||
"""apply removes to the working directory | ||||
|
r18630 | |||
yields tuples for progress updates | ||||
""" | ||||
|
r18640 | verbose = repo.ui.verbose | ||
unlink = util.unlinkpath | ||||
wjoin = repo.wjoin | ||||
|
r18630 | audit = repo.wopener.audit | ||
|
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) | ||||
audit(f) | ||||
try: | ||||
unlink(wjoin(f), ignoremissing=True) | ||||
except OSError, inst: | ||||
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 | ||||
def batchget(repo, mctx, actions): | ||||
"""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 | ||||
wwrite = repo.wwrite | ||||
i = 0 | ||||
|
r21545 | for f, args, msg in actions: | ||
|
r21392 | repo.ui.debug(" %s: %s -> g\n" % (f, msg)) | ||
|
r21551 | if verbose: | ||
repo.ui.note(_("getting %s\n") % f) | ||||
wwrite(f, fctx(f).data(), args[0]) | ||||
|
r18633 | if i == 100: | ||
yield i, f | ||||
i = 0 | ||||
i += 1 | ||||
if i > 0: | ||||
|
r18630 | yield i, f | ||
|
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 | |||
|
r3111 | updated, merged, removed, unresolved = 0, 0, 0, 0 | ||
|
r6512 | ms = mergestate(repo) | ||
|
r20591 | ms.reset(wctx.p1().node(), mctx.node()) | ||
|
r6512 | moves = [] | ||
|
r21545 | for m, l in actions.items(): | ||
l.sort() | ||||
|
r6512 | |||
# prescan for merges | ||||
|
r21545 | for f, args, msg in actions['m']: | ||
|
r21551 | f1, f2, fa, move, anc = args | ||
if f == '.hgsubstate': # merged internally | ||||
continue | ||||
repo.ui.debug(" preserving %s for resolve of %s\n" % (f1, f)) | ||||
fcl = wctx[f1] | ||||
fco = mctx[f2] | ||||
actx = repo[anc] | ||||
if fa in actx: | ||||
fca = actx[fa] | ||||
else: | ||||
fca = repo.filectx(f1, fileid=nullrev) | ||||
ms.add(fcl, fco, fca, f) | ||||
if f1 != f and move: | ||||
moves.append(f1) | ||||
|
r6512 | |||
|
r18328 | audit = repo.wopener.audit | ||
|
r21390 | _updating = _('updating') | ||
_files = _('files') | ||||
progress = repo.ui.progress | ||||
|
r14398 | |||
|
r6512 | # remove renamed files after safely stored | ||
for f in moves: | ||||
|
r12032 | if os.path.lexists(repo.wjoin(f)): | ||
|
r9467 | repo.ui.debug("removing %s\n" % f) | ||
|
r14398 | audit(f) | ||
|
r18333 | util.unlinkpath(repo.wjoin(f)) | ||
|
r5042 | |||
|
r21545 | numupdates = sum(len(l) for m, l in actions.items() if m != 'k') | ||
|
r18630 | |||
|
r21545 | if [a for a in actions['r'] if a[0] == '.hgsubstate']: | ||
|
r18632 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
|
r21390 | # remove in parallel (must come first) | ||
|
r18633 | z = 0 | ||
|
r21545 | prog = worker.worker(repo.ui, 0.001, batchremove, (repo,), 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 | |||
# get in parallel | ||||
|
r21545 | prog = worker.worker(repo.ui, 0.001, batchget, (repo, mctx), 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']: | ||
|
r18632 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) | ||
|
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 | |||
|
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 | # merge | ||
for f, args, msg in actions['m']: | ||||
repo.ui.debug(" %s: %s -> m\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
f1, f2, fa, move, anc = args | ||||
if f == '.hgsubstate': # subrepo states need updating | ||||
subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx), | ||||
overwrite) | ||||
continue | ||||
audit(f) | ||||
r = ms.resolve(f, wctx, labels=labels) | ||||
if r is not None and r > 0: | ||||
unresolved += 1 | ||||
else: | ||||
if r is None: | ||||
updated += 1 | ||||
|
r3309 | else: | ||
|
r21551 | merged += 1 | ||
|
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)) | ||||
audit(f) | ||||
repo.wwrite(f, wctx.filectx(f0).data(), flags) | ||||
util.unlinkpath(repo.wjoin(f0)) | ||||
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)) | ||||
repo.wwrite(f, mctx.filectx(f0).data(), flags) | ||||
updated += 1 | ||||
|
r21391 | |||
|
r21551 | # divergent renames | ||
for f, args, msg in actions['dr']: | ||||
repo.ui.debug(" %s: %s -> dr\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
fl, = args | ||||
repo.ui.warn(_("note: possible conflict - %s was renamed " | ||||
"multiple times to:\n") % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
|
r21391 | |||
|
r21551 | # rename and delete | ||
for f, args, msg in actions['rd']: | ||||
repo.ui.debug(" %s: %s -> rd\n" % (f, msg)) | ||||
z += 1 | ||||
progress(_updating, z, item=f, total=numupdates, unit=_files) | ||||
fl, = args | ||||
repo.ui.warn(_("note: possible conflict - %s was deleted " | ||||
"and renamed to:\n") % f) | ||||
for nf in fl: | ||||
repo.ui.warn(" %s\n" % nf) | ||||
|
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 | ||||
audit(f) | ||||
util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags) | ||||
updated += 1 | ||||
|
r21391 | |||
|
r12369 | ms.commit() | ||
|
r18640 | progress(_updating, None, total=numupdates, unit=_files) | ||
|
r3111 | |||
return updated, merged, removed, unresolved | ||||
|
r21081 | def calculateupdates(repo, wctx, mctx, ancestors, branchmerge, force, partial, | ||
|
r21080 | acceptremote, followcopies): | ||
|
r21081 | "Calculate the actions needed to merge mctx into wctx using ancestors" | ||
|
r21128 | if len(ancestors) == 1: # default | ||
actions = manifestmerge(repo, wctx, mctx, ancestors[0], | ||||
branchmerge, force, | ||||
partial, acceptremote, followcopies) | ||||
|
r22672 | else: # only when merge.preferancestor=* - the default | ||
|
r22673 | repo.ui.note( | ||
|
r21171 | _("note: merging %s and %s using bids from ancestors %s\n") % | ||
(wctx, mctx, _(' and ').join(str(anc) for anc in ancestors))) | ||||
|
r21128 | # Call for bids | ||
|
r21545 | fbids = {} # mapping filename to bids (action method to list af actions) | ||
|
r21128 | for ancestor in ancestors: | ||
repo.ui.note(_('\ncalculating bids for ancestor %s\n') % ancestor) | ||||
actions = manifestmerge(repo, wctx, mctx, ancestor, | ||||
branchmerge, force, | ||||
partial, acceptremote, followcopies) | ||||
|
r21545 | for m, l in sorted(actions.items()): | ||
for a in l: | ||||
f, 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) | ||||
else: | ||||
d[m] = [a] | ||||
else: | ||||
fbids[f] = {m: [a]} | ||||
|
r21080 | |||
|
r21128 | # Pick the best bid for each file | ||
repo.ui.note(_('\nauction for merging merge bids\n')) | ||||
|
r21545 | actions = dict((m, []) for m in actions.keys()) | ||
for f, bids in sorted(fbids.items()): | ||||
# bids is a mapping from action method to list af actions | ||||
|
r21128 | # Consensus? | ||
|
r21545 | if len(bids) == 1: # all bids are the same kind of method | ||
m, l = bids.items()[0] | ||||
if util.all(a == l[0] for a in l[1:]): # len(bids) is > 1 | ||||
repo.ui.note(" %s: consensus for %s\n" % (f, m)) | ||||
actions[m].append(l[0]) | ||||
continue | ||||
|
r21128 | # If keep is an option, just do it. | ||
if "k" in bids: | ||||
repo.ui.note(" %s: picking 'keep' action\n" % f) | ||||
|
r21545 | actions['k'].append(bids["k"][0]) | ||
|
r21128 | continue | ||
|
r21545 | # If there are gets and they all agree [how could they not?], do it. | ||
|
r21128 | if "g" in bids: | ||
ga0 = bids["g"][0] | ||||
if util.all(a == ga0 for a in bids["g"][1:]): | ||||
repo.ui.note(" %s: picking 'get' action\n" % f) | ||||
|
r21545 | actions['g'].append(ga0) | ||
|
r21128 | continue | ||
# TODO: Consider other simple actions such as mode changes | ||||
# Handle inefficient democrazy. | ||||
|
r21172 | repo.ui.note(_(' %s: multiple bids for merge action:\n') % f) | ||
|
r21545 | for m, l in sorted(bids.items()): | ||
for _f, args, msg in l: | ||||
repo.ui.note(' %s -> %s\n' % (msg, m)) | ||||
|
r21128 | # Pick random action. TODO: Instead, prompt user when resolving | ||
|
r21545 | m, l = bids.items()[0] | ||
|
r21170 | repo.ui.warn(_(' %s: ambiguous merge - picked %s action\n') % | ||
|
r21545 | (f, m)) | ||
actions[m].append(l[0]) | ||||
|
r21128 | continue | ||
repo.ui.note(_('end of auction\n\n')) | ||||
|
r20640 | |||
# Prompt and create actions. TODO: Move this towards resolve phase. | ||||
|
r21551 | for f, args, msg in actions['cd']: | ||
if repo.ui.promptchoice( | ||||
_("local changed %s which remote deleted\n" | ||||
"use (c)hanged version or (d)elete?" | ||||
"$$ &Changed $$ &Delete") % f, 0): | ||||
actions['r'].append((f, None, "prompt delete")) | ||||
else: | ||||
actions['a'].append((f, None, "prompt keep")) | ||||
del actions['cd'][:] | ||||
|
r21545 | |||
|
r21551 | for f, args, msg in actions['dc']: | ||
flags, = args | ||||
if repo.ui.promptchoice( | ||||
_("remote changed %s which local deleted\n" | ||||
"use (c)hanged version or leave (d)eleted?" | ||||
"$$ &Changed $$ &Deleted") % f, 0) == 0: | ||||
actions['g'].append((f, (flags,), "prompt recreating")) | ||||
del actions['dc'][:] | ||||
|
r20640 | |||
|
r21080 | if wctx.rev() is None: | ||
|
r21545 | ractions, factions = _forgetremoved(wctx, mctx, branchmerge) | ||
actions['r'].extend(ractions) | ||||
actions['f'].extend(factions) | ||||
|
r20640 | |||
|
r21545 | return actions | ||
|
r18035 | |||
|
r18330 | def recordupdates(repo, actions, branchmerge): | ||
|
r3315 | "record merge actions to the dirstate" | ||
|
r21551 | # remove (must come first) | ||
for f, args, msg in actions['r']: | ||||
if branchmerge: | ||||
repo.dirstate.remove(f) | ||||
else: | ||||
|
r21389 | repo.dirstate.drop(f) | ||
|
r21391 | |||
|
r21551 | # forget (must come first) | ||
for f, args, msg in actions['f']: | ||||
repo.dirstate.drop(f) | ||||
|
r21391 | |||
|
r21551 | # re-add | ||
for f, args, msg in actions['a']: | ||||
if not branchmerge: | ||||
repo.dirstate.add(f) | ||||
|
r21391 | |||
|
r21551 | # exec change | ||
for f, args, msg in actions['e']: | ||||
repo.dirstate.normallookup(f) | ||||
|
r21391 | |||
|
r21551 | # keep | ||
for f, args, msg in actions['k']: | ||||
pass | ||||
# get | ||||
for f, args, msg in actions['g']: | ||||
if branchmerge: | ||||
repo.dirstate.otherparent(f) | ||||
else: | ||||
repo.dirstate.normal(f) | ||||
|
r21391 | |||
|
r21551 | # merge | ||
for f, args, msg in actions['m']: | ||||
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 | ||
for f, args, msg in actions['dm']: | ||||
f0, flag = args | ||||
if f0 not in repo.dirstate: | ||||
# untracked file moved | ||||
continue | ||||
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 | ||
for f, args, msg in actions['dg']: | ||||
f0, flag = args | ||||
if branchmerge: | ||||
repo.dirstate.add(f) | ||||
repo.dirstate.copy(f0, f) | ||||
else: | ||||
repo.dirstate.normal(f) | ||||
|
r3111 | |||
|
r16696 | def update(repo, node, branchmerge, force, partial, ancestor=None, | ||
|
r21524 | mergeancestor=False, labels=None): | ||
|
r3315 | """ | ||
Perform a merge between the working directory and the given node | ||||
|
r9716 | node = the node to update to, or None if unspecified | ||
|
r3315 | branchmerge = whether to merge between branches | ||
force = whether to force branch merging or file overwriting | ||||
partial = a function 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. | ||||
|
r9716 | |||
The table below shows all the behaviors of the update command | ||||
given the -c and -C 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, on the same named | ||||
branch, or on another named branch). | ||||
|
r12279 | This logic is tested by test-update-branches.t. | ||
|
r9716 | |||
-c -C dirty rev | linear same cross | ||||
n n n n | ok (1) x | ||||
|
r9717 | n n n y | ok ok ok | ||
|
r19799 | n n y n | merge (2) (2) | ||
n n y y | merge (3) (3) | ||||
|
r9716 | n y * * | --- discard --- | ||
|
r19799 | y n y * | --- (4) --- | ||
|
r9716 | y n n * | --- ok --- | ||
|
r19799 | y y * * | --- (5) --- | ||
|
r9716 | |||
x = can't happen | ||||
* = don't-care | ||||
|
r19798 | 1 = abort: not a linear update (merge or update --check to force update) | ||
|
r19800 | 2 = abort: uncommitted changes (commit and merge, or update --clean to | ||
discard changes) | ||||
|
r19799 | 3 = abort: uncommitted changes (commit or update --clean to discard changes) | ||
|
r19801 | 4 = abort: uncommitted changes (checked in commands.py) | ||
|
r19799 | 5 = incompatible options (checked in commands.py) | ||
|
r13162 | |||
Return the same tuple as applyupdates(). | ||||
|
r3315 | """ | ||
|
r2815 | |||
|
r9717 | onode = node | ||
|
r4917 | wlock = repo.wlock() | ||
|
r4915 | try: | ||
|
r6747 | wc = repo[None] | ||
|
r20279 | pl = wc.parents() | ||
p1 = pl[0] | ||||
|
r21081 | pas = [None] | ||
|
r20279 | if ancestor: | ||
|
r21081 | pas = [repo[ancestor]] | ||
|
r20279 | |||
|
r4915 | if node is None: | ||
|
r20278 | # Here is where we should consider bookmarks, divergent bookmarks, | ||
# foreground changesets (successors), and tip of current branch; | ||||
# but currently we are only checking the branch tips. | ||||
|
r4915 | try: | ||
|
r16719 | node = repo.branchtip(wc.branch()) | ||
|
r22200 | except errormod.RepoLookupError: | ||
|
r5570 | if wc.branch() == "default": # no default branch! | ||
node = repo.lookup("tip") # update to tip | ||||
else: | ||||
raise util.Abort(_("branch %s not found") % wc.branch()) | ||||
|
r20280 | |||
if p1.obsolete() and not p1.children(): | ||||
# allow updating to successors | ||||
successors = obsolete.successorssets(repo, p1.node()) | ||||
# behavior of certain cases is as follows, | ||||
# | ||||
# divergent changesets: update to highest rev, similar to what | ||||
# is currently done when there are more than one head | ||||
# (i.e. 'tip') | ||||
# | ||||
# replaced changesets: same as divergent except we know there | ||||
# is no conflict | ||||
# | ||||
# pruned changeset: no update is done; though, we could | ||||
# consider updating to the first non-obsolete parent, | ||||
# similar to what is current done for 'hg prune' | ||||
if successors: | ||||
# flatten the list here handles both divergent (len > 1) | ||||
# and the usual case (len = 1) | ||||
successors = [n for sub in successors for n in sub] | ||||
# get the max revision for the given successors set, | ||||
# i.e. the 'tip' of a set | ||||
|
r22822 | node = repo.revs("max(%ln)", successors).first() | ||
|
r21081 | pas = [p1] | ||
|
r20280 | |||
|
r4915 | overwrite = force and not branchmerge | ||
|
r20279 | |||
p2 = repo[node] | ||||
|
r21081 | if pas[0] is None: | ||
|
r22672 | if repo.ui.config("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 | |||
|
r4915 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) | ||
|
r3314 | |||
|
r4915 | ### check phase | ||
if not overwrite and len(pl) > 1: | ||||
|
r22841 | raise util.Abort(_("outstanding uncommitted merge")) | ||
|
r6375 | if branchmerge: | ||
|
r21081 | if pas == [p2]: | ||
|
r11417 | raise util.Abort(_("merging with a working directory ancestor" | ||
" has no effect")) | ||||
|
r21081 | elif pas == [p1]: | ||
|
r16696 | if not mergeancestor and p1.branch() == p2.branch(): | ||
|
r15619 | raise util.Abort(_("nothing to merge"), | ||
hint=_("use 'hg update' " | ||||
"or check 'hg heads'")) | ||||
|
r6375 | if not force and (wc.files() or wc.deleted()): | ||
|
r19802 | raise util.Abort(_("uncommitted changes"), | ||
|
r15619 | hint=_("use 'hg status' to list changes")) | ||
|
r18364 | for s in sorted(wc.substate): | ||
|
r13437 | if wc.sub(s).dirty(): | ||
|
r19803 | raise util.Abort(_("uncommitted changes in " | ||
|
r13437 | "subrepository '%s'") % s) | ||
|
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 | ||||
|
r21081 | if pas not in ([p1], [p2]): # nonlinear | ||
|
r18985 | dirty = wc.dirty(missing=True) | ||
if dirty or onode is None: | ||||
# Branching is a bit strange to ensure we do the minimal | ||||
# amount of call to obsolete.background. | ||||
foreground = obsolete.foreground(repo, [p1.node()]) | ||||
# note: the <node> variable contains a random identifier | ||||
if repo[node].node() in foreground: | ||||
|
r21081 | pas = [p1] # allow updating to successors | ||
|
r19799 | elif dirty: | ||
msg = _("uncommitted changes") | ||||
|
r19800 | if onode is None: | ||
hint = _("commit and merge, or update --clean to" | ||||
" discard changes") | ||||
else: | ||||
hint = _("commit or update --clean to discard" | ||||
" changes") | ||||
|
r19799 | raise util.Abort(msg, hint=hint) | ||
|
r18985 | else: # node is none | ||
|
r19798 | msg = _("not a linear update") | ||
hint = _("merge or update --check to force update") | ||||
raise util.Abort(msg, hint=hint) | ||||
|
r18985 | else: | ||
# Allow jumping branches if clean and specific rev given | ||||
|
r21081 | pas = [p1] | ||
|
r2814 | |||
|
r21080 | followcopies = False | ||
if overwrite: | ||||
|
r21081 | pas = [wc] | ||
elif pas == [p2]: # backwards | ||||
pas = [wc.p1()] | ||||
|
r21080 | elif not branchmerge and not wc.dirty(missing=True): | ||
pass | ||||
|
r21081 | elif pas[0] and repo.ui.configbool("merge", "followcopies", True): | ||
|
r21080 | followcopies = True | ||
|
r4915 | ### calculate phase | ||
|
r21081 | actions = calculateupdates(repo, wc, p2, pas, branchmerge, force, | ||
|
r21080 | partial, mergeancestor, followcopies) | ||
|
r2775 | |||
|
r4915 | ### apply phase | ||
|
r13550 | if not branchmerge: # just jump to the new rev | ||
|
r4915 | fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' | ||
if not partial: | ||||
repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) | ||||
|
r19482 | # note that we're in the middle of an update | ||
repo.vfs.write('updatestate', p2.hex()) | ||||
|
r2775 | |||
|
r21524 | stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels) | ||
|
r2899 | |||
|
r4915 | if not partial: | ||
|
r22405 | repo.dirstate.beginparentchange() | ||
|
r16551 | repo.setparents(fp1, fp2) | ||
|
r18330 | recordupdates(repo, actions, branchmerge) | ||
|
r19482 | # update completed, clear state | ||
util.unlink(repo.join('updatestate')) | ||||
|
r13561 | if not branchmerge: | ||
|
r4915 | repo.dirstate.setbranch(p2.branch()) | ||
|
r22405 | repo.dirstate.endparentchange() | ||
|
r4915 | finally: | ||
|
r8109 | wlock.release() | ||
|
r10492 | |||
if not partial: | ||||
repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) | ||||
return stats | ||||
|
r22902 | |||
def graft(repo, ctx, pctx, labels): | ||||
"""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 | ||||
a single parent and tries to duplicate any renames/copies | ||||
appropriately. | ||||
ctx - changeset to rebase | ||||
pctx - merge base, usually ctx.p1() | ||||
labels - merge labels eg ['local', 'graft'] | ||||
""" | ||||
stats = update(repo, ctx.node(), True, True, False, pctx.node(), | ||||
labels=labels) | ||||
# drop the second merge parent | ||||
repo.dirstate.beginparentchange() | ||||
repo.setparents(repo['.'].node(), nullid) | ||||
repo.dirstate.write() | ||||
# fix up dirstate for copies and renames | ||||
copies.duplicatecopies(repo, ctx.rev(), pctx.rev()) | ||||
repo.dirstate.endparentchange() | ||||
return stats | ||||