transplant.py
678 lines
| 25.0 KiB
| text/x-python
|
PythonLexer
/ hgext / transplant.py
|
r3714 | # Patch transplanting extension for Mercurial | ||
# | ||||
|
r4635 | # Copyright 2006, 2007 Brendan Cully <brendan@kublai.com> | ||
|
r3714 | # | ||
|
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. | ||
|
r3714 | |||
|
r8934 | '''command to transplant changesets from another branch | ||
|
r3714 | |||
This extension allows you to transplant patches from another branch. | ||||
|
r8000 | Transplanted patches are recorded in .hg/transplant/transplants, as a | ||
map from a changeset hash to its hash in the source repository. | ||||
|
r3714 | ''' | ||
|
r7629 | from mercurial.i18n import _ | ||
import os, tempfile | ||||
|
r14393 | from mercurial.node import short | ||
|
r14319 | from mercurial import bundlerepo, hg, merge, match | ||
from mercurial import patch, revlog, scmutil, util, error, cmdutil | ||||
|
r13689 | from mercurial import revset, templatekw | ||
|
r7629 | |||
|
r16507 | class TransplantError(error.Abort): | ||
pass | ||||
|
r14308 | cmdtable = {} | ||
command = cmdutil.command(cmdtable) | ||||
|
r16743 | testedwith = 'internal' | ||
|
r14308 | |||
|
r8778 | class transplantentry(object): | ||
|
r3714 | def __init__(self, lnode, rnode): | ||
self.lnode = lnode | ||||
self.rnode = rnode | ||||
|
r8778 | class transplants(object): | ||
|
r3714 | def __init__(self, path=None, transplantfile=None, opener=None): | ||
self.path = path | ||||
self.transplantfile = transplantfile | ||||
self.opener = opener | ||||
if not opener: | ||||
|
r13970 | self.opener = scmutil.opener(self.path) | ||
|
r12313 | self.transplants = {} | ||
|
r3714 | self.dirty = False | ||
self.read() | ||||
def read(self): | ||||
abspath = os.path.join(self.path, self.transplantfile) | ||||
if self.transplantfile and os.path.exists(abspath): | ||||
|
r14168 | for line in self.opener.read(self.transplantfile).splitlines(): | ||
|
r3714 | lnode, rnode = map(revlog.bin, line.split(':')) | ||
|
r12313 | list = self.transplants.setdefault(rnode, []) | ||
list.append(transplantentry(lnode, rnode)) | ||||
|
r3714 | |||
def write(self): | ||||
if self.dirty and self.transplantfile: | ||||
if not os.path.isdir(self.path): | ||||
os.mkdir(self.path) | ||||
fp = self.opener(self.transplantfile, 'w') | ||||
|
r12349 | for list in self.transplants.itervalues(): | ||
for t in list: | ||||
l, r = map(revlog.hex, (t.lnode, t.rnode)) | ||||
|
r12313 | fp.write(l + ':' + r + '\n') | ||
|
r3714 | fp.close() | ||
self.dirty = False | ||||
def get(self, rnode): | ||||
|
r12313 | return self.transplants.get(rnode) or [] | ||
|
r3714 | |||
def set(self, lnode, rnode): | ||||
|
r12313 | list = self.transplants.setdefault(rnode, []) | ||
list.append(transplantentry(lnode, rnode)) | ||||
|
r3714 | self.dirty = True | ||
def remove(self, transplant): | ||||
|
r12313 | list = self.transplants.get(transplant.rnode) | ||
if list: | ||||
del list[list.index(transplant)] | ||||
self.dirty = True | ||||
|
r3714 | |||
|
r8778 | class transplanter(object): | ||
|
r3714 | def __init__(self, ui, repo): | ||
self.ui = ui | ||||
self.path = repo.join('transplant') | ||||
|
r13970 | self.opener = scmutil.opener(self.path) | ||
|
r7744 | self.transplants = transplants(self.path, 'transplants', | ||
opener=self.opener) | ||||
|
r15220 | self.editor = None | ||
|
r3714 | |||
def applied(self, repo, node, parent): | ||||
'''returns True if a node is already an ancestor of parent | ||||
|
r17010 | or is parent or has already been transplanted''' | ||
if hasnode(repo, parent): | ||||
parentrev = repo.changelog.rev(parent) | ||||
|
r3714 | if hasnode(repo, node): | ||
|
r17010 | rev = repo.changelog.rev(node) | ||
|
r18082 | reachable = repo.changelog.ancestors([parentrev], rev, | ||
inclusive=True) | ||||
|
r17010 | if rev in reachable: | ||
|
r3714 | return True | ||
for t in self.transplants.get(node): | ||||
# it might have been stripped | ||||
if not hasnode(repo, t.lnode): | ||||
self.transplants.remove(t) | ||||
return False | ||||
|
r17010 | lnoderev = repo.changelog.rev(t.lnode) | ||
|
r18082 | if lnoderev in repo.changelog.ancestors([parentrev], lnoderev, | ||
inclusive=True): | ||||
|
r3714 | return True | ||
return False | ||||
def apply(self, repo, source, revmap, merges, opts={}): | ||||
'''apply the revisions in revmap one by one in revision order''' | ||||
|
r8209 | revs = sorted(revmap) | ||
|
r3714 | p1, p2 = repo.dirstate.parents() | ||
pulls = [] | ||||
diffopts = patch.diffopts(self.ui, opts) | ||||
diffopts.git = True | ||||
|
r15204 | lock = wlock = tr = None | ||
|
r3714 | try: | ||
|
r4915 | wlock = repo.wlock() | ||
lock = repo.lock() | ||||
|
r15204 | tr = repo.transaction('transplant') | ||
|
r3714 | for rev in revs: | ||
node = revmap[rev] | ||||
|
r14393 | revstr = '%s:%s' % (rev, short(node)) | ||
|
r3714 | |||
if self.applied(repo, node, p1): | ||||
self.ui.warn(_('skipping already applied revision %s\n') % | ||||
revstr) | ||||
continue | ||||
parents = source.changelog.parents(node) | ||||
|
r16627 | if not (opts.get('filter') or opts.get('log')): | ||
|
r7744 | # If the changeset parent is the same as the | ||
# wdir's parent, just pull it. | ||||
|
r3714 | if parents[0] == p1: | ||
pulls.append(node) | ||||
p1 = node | ||||
continue | ||||
if pulls: | ||||
if source != repo: | ||||
|
r17191 | repo.pull(source.peer(), heads=pulls) | ||
|
r4917 | merge.update(repo, pulls[-1], False, False, None) | ||
|
r3714 | p1, p2 = repo.dirstate.parents() | ||
pulls = [] | ||||
domerge = False | ||||
if node in merges: | ||||
|
r7744 | # pulling all the merge revs at once would mean we | ||
# couldn't transplant after the latest even if | ||||
# transplants before them fail. | ||||
|
r3714 | domerge = True | ||
if not hasnode(repo, node): | ||||
|
r4917 | repo.pull(source, heads=[node]) | ||
|
r3714 | |||
|
r16400 | skipmerge = False | ||
|
r3714 | if parents[1] != revlog.nullid: | ||
|
r16400 | if not opts.get('parent'): | ||
self.ui.note(_('skipping merge changeset %s:%s\n') | ||||
% (rev, short(node))) | ||||
skipmerge = True | ||||
else: | ||||
parent = source.lookup(opts['parent']) | ||||
if parent not in parents: | ||||
raise util.Abort(_('%s is not a parent of %s') % | ||||
(short(parent), short(node))) | ||||
else: | ||||
parent = parents[0] | ||||
if skipmerge: | ||||
|
r3714 | patchfile = None | ||
else: | ||||
fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-') | ||||
fp = os.fdopen(fd, 'w') | ||||
|
r16400 | gen = patch.diff(source, parent, node, opts=diffopts) | ||
|
r7308 | for chunk in gen: | ||
fp.write(chunk) | ||||
|
r3714 | fp.close() | ||
del revmap[rev] | ||||
if patchfile or domerge: | ||||
try: | ||||
|
r16507 | try: | ||
n = self.applyone(repo, node, | ||||
source.changelog.read(node), | ||||
patchfile, merge=domerge, | ||||
log=opts.get('log'), | ||||
filter=opts.get('filter')) | ||||
except TransplantError: | ||||
# Do not rollback, it is up to the user to | ||||
# fix the merge or cancel everything | ||||
tr.close() | ||||
raise | ||||
|
r4251 | if n and domerge: | ||
|
r3714 | self.ui.status(_('%s merged at %s\n') % (revstr, | ||
|
r14393 | short(n))) | ||
|
r4251 | elif n: | ||
|
r7744 | self.ui.status(_('%s transplanted to %s\n') | ||
|
r14393 | % (short(node), | ||
short(n))) | ||||
|
r3714 | finally: | ||
if patchfile: | ||||
os.unlink(patchfile) | ||||
|
r15204 | tr.close() | ||
|
r3714 | if pulls: | ||
|
r17191 | repo.pull(source.peer(), heads=pulls) | ||
|
r4917 | merge.update(repo, pulls[-1], False, False, None) | ||
|
r3714 | finally: | ||
self.saveseries(revmap, merges) | ||||
self.transplants.write() | ||||
|
r15204 | if tr: | ||
tr.release() | ||||
|
r8112 | lock.release() | ||
wlock.release() | ||||
|
r3714 | |||
|
r13579 | def filter(self, filter, node, changelog, patchfile): | ||
|
r3714 | '''arbitrarily rewrite changeset before applying it''' | ||
|
r6966 | self.ui.status(_('filtering %s\n') % patchfile) | ||
|
r3759 | user, date, msg = (changelog[1], changelog[2], changelog[4]) | ||
fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-') | ||||
fp = os.fdopen(fd, 'w') | ||||
fp.write("# HG changeset patch\n") | ||||
fp.write("# User %s\n" % user) | ||||
fp.write("# Date %d %d\n" % date) | ||||
|
r9433 | fp.write(msg + '\n') | ||
|
r3759 | fp.close() | ||
try: | ||||
util.system('%s %s %s' % (filter, util.shellquote(headerfile), | ||||
util.shellquote(patchfile)), | ||||
|
r13579 | environ={'HGUSER': changelog[1], | ||
'HGREVISION': revlog.hex(node), | ||||
}, | ||||
|
r14741 | onerr=util.Abort, errprefix=_('filter failed'), | ||
out=self.ui.fout) | ||||
|
r3759 | user, date, msg = self.parselog(file(headerfile))[1:4] | ||
finally: | ||||
os.unlink(headerfile) | ||||
return (user, date, msg) | ||||
|
r3714 | |||
def applyone(self, repo, node, cl, patchfile, merge=False, log=False, | ||||
|
r4917 | filter=None): | ||
|
r3714 | '''apply the patch in patchfile to the repository as a transplant''' | ||
(manifest, user, (time, timezone), files, message) = cl[:5] | ||||
date = "%d %d" % (time, timezone) | ||||
extra = {'transplant_source': node} | ||||
if filter: | ||||
|
r13579 | (user, date, message) = self.filter(filter, node, cl, patchfile) | ||
|
r3714 | |||
if log: | ||||
|
r9183 | # we don't translate messages inserted into commits | ||
|
r3714 | message += '\n(transplanted from %s)' % revlog.hex(node) | ||
|
r14393 | self.ui.status(_('applying %s\n') % short(node)) | ||
|
r3714 | self.ui.note('%s %s\n%s\n' % (user, date, message)) | ||
if not patchfile and not merge: | ||||
raise util.Abort(_('can only omit patchfile if merging')) | ||||
if patchfile: | ||||
try: | ||||
|
r14564 | files = set() | ||
|
r14382 | patch.patch(self.ui, repo, patchfile, files=files, eolmode=None) | ||
|
r14260 | files = list(files) | ||
|
r3714 | except Exception, inst: | ||
|
r3757 | seriespath = os.path.join(self.path, 'series') | ||
if os.path.exists(seriespath): | ||||
os.unlink(seriespath) | ||||
|
r13878 | p1 = repo.dirstate.p1() | ||
|
r3714 | p2 = node | ||
|
r3725 | self.log(user, date, message, p1, p2, merge=merge) | ||
|
r3714 | self.ui.write(str(inst) + '\n') | ||
|
r16507 | raise TransplantError(_('fix up the merge and run ' | ||
'hg transplant --continue')) | ||||
|
r3714 | else: | ||
files = None | ||||
if merge: | ||||
p1, p2 = repo.dirstate.parents() | ||||
|
r16551 | repo.setparents(p1, node) | ||
|
r8703 | m = match.always(repo.root, '') | ||
else: | ||||
m = match.exact(repo.root, '', files) | ||||
|
r3714 | |||
|
r15220 | n = repo.commit(message, user, date, extra=extra, match=m, | ||
editor=self.editor) | ||||
|
r11638 | if not n: | ||
|
r17320 | self.ui.warn(_('skipping emptied changeset %s\n') % short(node)) | ||
|
r17319 | return None | ||
|
r3714 | if not merge: | ||
self.transplants.set(n, node) | ||||
return n | ||||
def resume(self, repo, source, opts=None): | ||||
'''recover last transaction and apply remaining changesets''' | ||||
if os.path.exists(os.path.join(self.path, 'journal')): | ||||
n, node = self.recover(repo) | ||||
|
r14393 | self.ui.status(_('%s transplanted as %s\n') % (short(node), | ||
short(n))) | ||||
|
r3714 | seriespath = os.path.join(self.path, 'series') | ||
if not os.path.exists(seriespath): | ||||
|
r3758 | self.transplants.write() | ||
|
r3714 | return | ||
nodes, merges = self.readseries() | ||||
revmap = {} | ||||
for n in nodes: | ||||
revmap[source.changelog.rev(n)] = n | ||||
os.unlink(seriespath) | ||||
self.apply(repo, source, revmap, merges, opts) | ||||
def recover(self, repo): | ||||
'''commit working directory using journal metadata''' | ||||
node, user, date, message, parents = self.readlog() | ||||
|
r16400 | merge = False | ||
|
r3714 | |||
if not user or not date or not message or not parents[0]: | ||||
raise util.Abort(_('transplant log file is corrupt')) | ||||
|
r16400 | parent = parents[0] | ||
if len(parents) > 1: | ||||
if opts.get('parent'): | ||||
parent = source.lookup(opts['parent']) | ||||
if parent not in parents: | ||||
raise util.Abort(_('%s is not a parent of %s') % | ||||
(short(parent), short(node))) | ||||
else: | ||||
merge = True | ||||
|
r3758 | extra = {'transplant_source': node} | ||
|
r3714 | wlock = repo.wlock() | ||
|
r4915 | try: | ||
p1, p2 = repo.dirstate.parents() | ||||
|
r16400 | if p1 != parent: | ||
|
r4915 | raise util.Abort( | ||
_('working dir not at transplant parent %s') % | ||||
|
r16400 | revlog.hex(parent)) | ||
|
r4915 | if merge: | ||
|
r16551 | repo.setparents(p1, parents[1]) | ||
|
r15220 | n = repo.commit(message, user, date, extra=extra, | ||
editor=self.editor) | ||||
|
r4915 | if not n: | ||
raise util.Abort(_('commit failed')) | ||||
if not merge: | ||||
self.transplants.set(n, node) | ||||
self.unlog() | ||||
|
r3714 | |||
|
r4915 | return n, node | ||
finally: | ||||
|
r8112 | wlock.release() | ||
|
r3714 | |||
def readseries(self): | ||||
nodes = [] | ||||
merges = [] | ||||
cur = nodes | ||||
|
r14168 | for line in self.opener.read('series').splitlines(): | ||
|
r3714 | if line.startswith('# Merges'): | ||
cur = merges | ||||
continue | ||||
cur.append(revlog.bin(line)) | ||||
return (nodes, merges) | ||||
def saveseries(self, revmap, merges): | ||||
if not revmap: | ||||
return | ||||
if not os.path.isdir(self.path): | ||||
os.mkdir(self.path) | ||||
series = self.opener('series', 'w') | ||||
|
r8209 | for rev in sorted(revmap): | ||
|
r3714 | series.write(revlog.hex(revmap[rev]) + '\n') | ||
if merges: | ||||
series.write('# Merges\n') | ||||
for m in merges: | ||||
series.write(revlog.hex(m) + '\n') | ||||
series.close() | ||||
|
r3759 | def parselog(self, fp): | ||
parents = [] | ||||
message = [] | ||||
node = revlog.nullid | ||||
inmsg = False | ||||
|
r13789 | user = None | ||
date = None | ||||
|
r3759 | for line in fp.read().splitlines(): | ||
if inmsg: | ||||
message.append(line) | ||||
elif line.startswith('# User '): | ||||
user = line[7:] | ||||
elif line.startswith('# Date '): | ||||
date = line[7:] | ||||
elif line.startswith('# Node ID '): | ||||
node = revlog.bin(line[10:]) | ||||
elif line.startswith('# Parent '): | ||||
parents.append(revlog.bin(line[9:])) | ||||
|
r11411 | elif not line.startswith('# '): | ||
|
r3759 | inmsg = True | ||
message.append(line) | ||||
|
r13789 | if None in (user, date): | ||
|
r13790 | raise util.Abort(_("filter corrupted changeset (no user or date)")) | ||
|
r3759 | return (node, user, date, '\n'.join(message), parents) | ||
|
r4516 | |||
|
r3725 | def log(self, user, date, message, p1, p2, merge=False): | ||
|
r3714 | '''journal changelog metadata for later recover''' | ||
if not os.path.isdir(self.path): | ||||
os.mkdir(self.path) | ||||
fp = self.opener('journal', 'w') | ||||
|
r3725 | fp.write('# User %s\n' % user) | ||
fp.write('# Date %s\n' % date) | ||||
|
r3714 | fp.write('# Node ID %s\n' % revlog.hex(p2)) | ||
fp.write('# Parent ' + revlog.hex(p1) + '\n') | ||||
if merge: | ||||
fp.write('# Parent ' + revlog.hex(p2) + '\n') | ||||
|
r3725 | fp.write(message.rstrip() + '\n') | ||
|
r3714 | fp.close() | ||
def readlog(self): | ||||
|
r3759 | return self.parselog(self.opener('journal')) | ||
|
r3714 | |||
def unlog(self): | ||||
'''remove changelog journal''' | ||||
absdst = os.path.join(self.path, 'journal') | ||||
if os.path.exists(absdst): | ||||
os.unlink(absdst) | ||||
def transplantfilter(self, repo, source, root): | ||||
def matchfn(node): | ||||
if self.applied(repo, node, root): | ||||
return False | ||||
if source.changelog.parents(node)[1] != revlog.nullid: | ||||
return False | ||||
extra = source.changelog.read(node)[5] | ||||
cnode = extra.get('transplant_source') | ||||
if cnode and self.applied(repo, cnode, root): | ||||
return False | ||||
return True | ||||
return matchfn | ||||
def hasnode(repo, node): | ||||
try: | ||||
|
r13031 | return repo.changelog.rev(node) is not None | ||
|
r7633 | except error.RevlogError: | ||
|
r3714 | return False | ||
def browserevs(ui, repo, nodes, opts): | ||||
'''interactively transplant changesets''' | ||||
def browsehelp(ui): | ||||
|
r10510 | ui.write(_('y: transplant this changeset\n' | ||
'n: skip this changeset\n' | ||||
'm: merge at this changeset\n' | ||||
'p: show patch\n' | ||||
'c: commit selected changesets\n' | ||||
'q: cancel transplant\n' | ||||
'?: show this help\n')) | ||||
|
r3714 | |||
|
r3723 | displayer = cmdutil.show_changeset(ui, repo, opts) | ||
|
r3714 | transplants = [] | ||
merges = [] | ||||
for node in nodes: | ||||
|
r7369 | displayer.show(repo[node]) | ||
|
r3714 | action = None | ||
while not action: | ||||
action = ui.prompt(_('apply changeset? [ynmpcq?]:')) | ||||
if action == '?': | ||||
browsehelp(ui) | ||||
action = None | ||||
elif action == 'p': | ||||
parent = repo.changelog.parents(node)[0] | ||||
|
r7308 | for chunk in patch.diff(repo, parent, node): | ||
|
r8615 | ui.write(chunk) | ||
|
r3714 | action = None | ||
elif action not in ('y', 'n', 'm', 'c', 'q'): | ||||
|
r10510 | ui.write(_('no such option\n')) | ||
|
r3714 | action = None | ||
if action == 'y': | ||||
transplants.append(node) | ||||
elif action == 'm': | ||||
merges.append(node) | ||||
elif action == 'c': | ||||
break | ||||
elif action == 'q': | ||||
transplants = () | ||||
merges = () | ||||
break | ||||
|
r10152 | displayer.close() | ||
|
r3714 | return (transplants, merges) | ||
|
r14308 | @command('transplant', | ||
[('s', 'source', '', _('pull patches from REPO'), _('REPO')), | ||||
('b', 'branch', [], | ||||
_('pull patches from branch BRANCH'), _('BRANCH')), | ||||
('a', 'all', None, _('pull all changesets up to BRANCH')), | ||||
('p', 'prune', [], _('skip over REV'), _('REV')), | ||||
('m', 'merge', [], _('merge at REV'), _('REV')), | ||||
|
r16400 | ('', 'parent', '', | ||
_('parent to choose when transplanting merge'), _('REV')), | ||||
|
r15220 | ('e', 'edit', False, _('invoke editor on commit messages')), | ||
|
r14308 | ('', 'log', None, _('append transplant info to log message')), | ||
('c', 'continue', None, _('continue last transplant session ' | ||||
'after repair')), | ||||
('', 'filter', '', | ||||
_('filter changesets through command'), _('CMD'))], | ||||
_('hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] ' | ||||
'[-m REV] [REV]...')) | ||||
|
r3714 | def transplant(ui, repo, *revs, **opts): | ||
'''transplant changesets from another branch | ||||
Selected changesets will be applied on top of the current working | ||||
|
r13605 | directory with the log of the original changeset. The changesets | ||
are copied and will thus appear twice in the history. Use the | ||||
rebase extension instead if you want to move a whole branch of | ||||
unpublished changesets. | ||||
If --log is specified, log messages will have a comment appended | ||||
of the form:: | ||||
|
r3714 | |||
|
r9200 | (transplanted from CHANGESETHASH) | ||
|
r3714 | |||
You can rewrite the changelog message with the --filter option. | ||||
|
r8000 | Its argument will be invoked with the current changelog message as | ||
$1 and the patch as $2. | ||||
|
r3714 | |||
|
r8076 | If --source/-s is specified, selects changesets from the named | ||
repository. If --branch/-b is specified, selects changesets from | ||||
the branch holding the named revision, up to that revision. If | ||||
--all/-a is specified, all changesets on the branch will be | ||||
transplanted, otherwise you will be prompted to select the | ||||
changesets you want. | ||||
|
r3714 | |||
|
r16991 | :hg:`transplant --branch REV --all` will transplant the | ||
|
r13606 | selected branch (up to the named revision) onto your current | ||
working directory. | ||||
|
r3714 | |||
|
r8000 | You can optionally mark selected transplanted changesets as merge | ||
changesets. You will not be prompted to transplant any ancestors | ||||
of a merged transplant, and you can merge descendants of them | ||||
normally instead of transplanting them. | ||||
|
r3714 | |||
|
r16400 | Merge changesets may be transplanted directly by specifying the | ||
|
r16457 | proper parent changeset by calling :hg:`transplant --parent`. | ||
|
r16400 | |||
|
r11193 | If no merges or revisions are provided, :hg:`transplant` will | ||
start an interactive changeset browser. | ||||
|
r3714 | |||
|
r8000 | If a changeset application fails, you can fix the merge by hand | ||
|
r11193 | and then resume where you left off by calling :hg:`transplant | ||
--continue/-c`. | ||||
|
r3714 | ''' | ||
|
r14161 | def incwalk(repo, csets, match=util.always): | ||
for node in csets: | ||||
|
r3714 | if match(node): | ||
yield node | ||||
def transplantwalk(repo, root, branches, match=util.always): | ||||
if not branches: | ||||
branches = repo.heads() | ||||
ancestors = [] | ||||
for branch in branches: | ||||
ancestors.append(repo.changelog.ancestor(root, branch)) | ||||
for node in repo.changelog.nodesbetween(ancestors, branches)[0]: | ||||
if match(node): | ||||
yield node | ||||
def checkopts(opts, revs): | ||||
if opts.get('continue'): | ||||
|
r10282 | if opts.get('branch') or opts.get('all') or opts.get('merge'): | ||
|
r7744 | raise util.Abort(_('--continue is incompatible with ' | ||
'branch, all or merge')) | ||||
|
r3714 | return | ||
if not (opts.get('source') or revs or | ||||
opts.get('merge') or opts.get('branch')): | ||||
|
r7744 | raise util.Abort(_('no source URL, branch tag or revision ' | ||
'list provided')) | ||||
|
r3714 | if opts.get('all'): | ||
if not opts.get('branch'): | ||||
raise util.Abort(_('--all requires a branch revision')) | ||||
if revs: | ||||
|
r7744 | raise util.Abort(_('--all is incompatible with a ' | ||
'revision list')) | ||||
|
r3714 | |||
checkopts(opts, revs) | ||||
if not opts.get('log'): | ||||
opts['log'] = ui.config('transplant', 'log') | ||||
if not opts.get('filter'): | ||||
opts['filter'] = ui.config('transplant', 'filter') | ||||
tp = transplanter(ui, repo) | ||||
|
r15220 | if opts.get('edit'): | ||
tp.editor = cmdutil.commitforceeditor | ||||
|
r3714 | |||
p1, p2 = repo.dirstate.parents() | ||||
|
r8176 | if len(repo) > 0 and p1 == revlog.nullid: | ||
raise util.Abort(_('no revision checked out')) | ||||
|
r3714 | if not opts.get('continue'): | ||
if p2 != revlog.nullid: | ||||
raise util.Abort(_('outstanding uncommitted merges')) | ||||
m, a, r, d = repo.status()[:4] | ||||
if m or a or r or d: | ||||
raise util.Abort(_('outstanding local changes')) | ||||
|
r14161 | sourcerepo = opts.get('source') | ||
if sourcerepo: | ||||
|
r17874 | peer = hg.peer(repo, opts, ui.expandpath(sourcerepo)) | ||
|
r17191 | branches = map(peer.lookup, opts.get('branch', ())) | ||
source, csets, cleanupfn = bundlerepo.getremotechanges(ui, repo, peer, | ||||
|
r14161 | onlyheads=branches, force=True) | ||
|
r3714 | else: | ||
source = repo | ||||
|
r14161 | branches = map(source.lookup, opts.get('branch', ())) | ||
cleanupfn = None | ||||
|
r3714 | |||
try: | ||||
if opts.get('continue'): | ||||
|
r3724 | tp.resume(repo, source, opts) | ||
|
r3714 | return | ||
|
r10394 | tf = tp.transplantfilter(repo, source, p1) | ||
|
r3714 | if opts.get('prune'): | ||
prune = [source.lookup(r) | ||||
|
r14319 | for r in scmutil.revrange(source, opts.get('prune'))] | ||
|
r3714 | matchfn = lambda x: tf(x) and x not in prune | ||
else: | ||||
matchfn = tf | ||||
merges = map(source.lookup, opts.get('merge', ())) | ||||
revmap = {} | ||||
if revs: | ||||
|
r14319 | for r in scmutil.revrange(source, revs): | ||
|
r3714 | revmap[int(r)] = source.lookup(r) | ||
elif opts.get('all') or not merges: | ||||
if source != repo: | ||||
|
r14161 | alltransplants = incwalk(source, csets, match=matchfn) | ||
|
r3714 | else: | ||
|
r7744 | alltransplants = transplantwalk(source, p1, branches, | ||
match=matchfn) | ||||
|
r3714 | if opts.get('all'): | ||
revs = alltransplants | ||||
else: | ||||
revs, newmerges = browserevs(ui, source, alltransplants, opts) | ||||
merges.extend(newmerges) | ||||
for r in revs: | ||||
revmap[source.changelog.rev(r)] = r | ||||
for r in merges: | ||||
revmap[source.changelog.rev(r)] = r | ||||
tp.apply(repo, source, revmap, merges, opts) | ||||
finally: | ||||
|
r14161 | if cleanupfn: | ||
cleanupfn() | ||||
|
r3714 | |||
|
r12581 | def revsettransplanted(repo, subset, x): | ||
|
r14211 | """``transplanted([set])`` | ||
Transplanted changesets in set, or all transplanted changesets. | ||||
|
r12822 | """ | ||
|
r12581 | if x: | ||
|
r17299 | s = revset.getset(repo, subset, x) | ||
|
r12581 | else: | ||
|
r17299 | s = subset | ||
|
r14212 | return [r for r in s if repo[r].extra().get('transplant_source')] | ||
|
r12581 | |||
|
r13689 | def kwtransplanted(repo, ctx, **args): | ||
""":transplanted: String. The node identifier of the transplanted | ||||
changeset if any.""" | ||||
n = ctx.extra().get('transplant_source') | ||||
return n and revlog.hex(n) or '' | ||||
|
r12581 | |||
|
r12822 | def extsetup(ui): | ||
|
r12581 | revset.symbols['transplanted'] = revsettransplanted | ||
|
r13689 | templatekw.keywords['transplanted'] = kwtransplanted | ||
|
r12581 | |||
|
r12823 | # tell hggettext to extract docstrings from these functions: | ||
|
r13698 | i18nfunctions = [revsettransplanted, kwtransplanted] | ||