| 1 |
# Patch transplanting extension for Mercurial |
| 2 |
# |
| 3 |
# Copyright 2006, 2007 Brendan Cully <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
'''command to transplant changesets from another branch |
| 9 |
|
| 10 |
This extension allows you to transplant changes to another parent revision, |
| 11 |
possibly in another repository. The transplant is done using 'diff' patches. |
| 12 |
|
| 13 |
Transplanted patches are recorded in .hg/transplant/transplants, as a |
| 14 |
map from a changeset hash to its hash in the source repository. |
| 15 |
''' |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import os |
| 20 |
|
| 21 |
from mercurial.i18n import _ |
| 22 |
from mercurial.node import ( |
| 23 |
bin, |
| 24 |
hex, |
| 25 |
short, |
| 26 |
) |
| 27 |
from mercurial import ( |
| 28 |
bundlerepo, |
| 29 |
cmdutil, |
| 30 |
error, |
| 31 |
exchange, |
| 32 |
logcmdutil, |
| 33 |
match, |
| 34 |
merge, |
| 35 |
patch, |
| 36 |
pycompat, |
| 37 |
registrar, |
| 38 |
revset, |
| 39 |
scmutil, |
| 40 |
smartset, |
| 41 |
state as statemod, |
| 42 |
util, |
| 43 |
vfs as vfsmod, |
| 44 |
) |
| 45 |
from mercurial.repo import ( |
| 46 |
factory as repo_factory, |
| 47 |
) |
| 48 |
from mercurial.utils import ( |
| 49 |
procutil, |
| 50 |
stringutil, |
| 51 |
urlutil, |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
class TransplantError(error.Abort): |
| 56 |
pass |
| 57 |
|
| 58 |
|
| 59 |
cmdtable = {} |
| 60 |
command = registrar.command(cmdtable) |
| 61 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 62 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 63 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 64 |
# leave the attribute unspecified. |
| 65 |
testedwith = b'ships-with-hg-core' |
| 66 |
|
| 67 |
configtable = {} |
| 68 |
configitem = registrar.configitem(configtable) |
| 69 |
|
| 70 |
configitem( |
| 71 |
b'transplant', |
| 72 |
b'filter', |
| 73 |
default=None, |
| 74 |
) |
| 75 |
configitem( |
| 76 |
b'transplant', |
| 77 |
b'log', |
| 78 |
default=None, |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
class transplantentry: |
| 83 |
def __init__(self, lnode, rnode): |
| 84 |
self.lnode = lnode |
| 85 |
self.rnode = rnode |
| 86 |
|
| 87 |
|
| 88 |
class transplants: |
| 89 |
def __init__(self, path=None, transplantfile=None, opener=None): |
| 90 |
self.path = path |
| 91 |
self.transplantfile = transplantfile |
| 92 |
self.opener = opener |
| 93 |
|
| 94 |
if not opener: |
| 95 |
self.opener = vfsmod.vfs(self.path) |
| 96 |
self.transplants = {} |
| 97 |
self.dirty = False |
| 98 |
self.read() |
| 99 |
|
| 100 |
def read(self): |
| 101 |
abspath = os.path.join(self.path, self.transplantfile) |
| 102 |
if self.transplantfile and os.path.exists(abspath): |
| 103 |
for line in self.opener.read(self.transplantfile).splitlines(): |
| 104 |
lnode, rnode = map(bin, line.split(b':')) |
| 105 |
list = self.transplants.setdefault(rnode, []) |
| 106 |
list.append(transplantentry(lnode, rnode)) |
| 107 |
|
| 108 |
def write(self): |
| 109 |
if self.dirty and self.transplantfile: |
| 110 |
if not os.path.isdir(self.path): |
| 111 |
os.mkdir(self.path) |
| 112 |
fp = self.opener(self.transplantfile, b'w') |
| 113 |
for list in self.transplants.values(): |
| 114 |
for t in list: |
| 115 |
l, r = map(hex, (t.lnode, t.rnode)) |
| 116 |
fp.write(l + b':' + r + b'\n') |
| 117 |
fp.close() |
| 118 |
self.dirty = False |
| 119 |
|
| 120 |
def get(self, rnode): |
| 121 |
return self.transplants.get(rnode) or [] |
| 122 |
|
| 123 |
def set(self, lnode, rnode): |
| 124 |
list = self.transplants.setdefault(rnode, []) |
| 125 |
list.append(transplantentry(lnode, rnode)) |
| 126 |
self.dirty = True |
| 127 |
|
| 128 |
def remove(self, transplant): |
| 129 |
list = self.transplants.get(transplant.rnode) |
| 130 |
if list: |
| 131 |
del list[list.index(transplant)] |
| 132 |
self.dirty = True |
| 133 |
|
| 134 |
|
| 135 |
class transplanter: |
| 136 |
def __init__(self, ui, repo, opts): |
| 137 |
self.ui = ui |
| 138 |
self.repo = repo |
| 139 |
self.path = repo.vfs.join(b'transplant') |
| 140 |
self.opener = vfsmod.vfs(self.path) |
| 141 |
self.transplants = transplants( |
| 142 |
self.path, b'transplants', opener=self.opener |
| 143 |
) |
| 144 |
|
| 145 |
def getcommiteditor(): |
| 146 |
editform = cmdutil.mergeeditform(repo[None], b'transplant') |
| 147 |
return cmdutil.getcommiteditor( |
| 148 |
editform=editform, **pycompat.strkwargs(opts) |
| 149 |
) |
| 150 |
|
| 151 |
self.getcommiteditor = getcommiteditor |
| 152 |
|
| 153 |
def applied(self, repo, node, parent): |
| 154 |
"""returns True if a node is already an ancestor of parent |
| 155 |
or is parent or has already been transplanted""" |
| 156 |
if hasnode(repo, parent): |
| 157 |
parentrev = repo.changelog.rev(parent) |
| 158 |
if hasnode(repo, node): |
| 159 |
rev = repo.changelog.rev(node) |
| 160 |
reachable = repo.changelog.ancestors( |
| 161 |
[parentrev], rev, inclusive=True |
| 162 |
) |
| 163 |
if rev in reachable: |
| 164 |
return True |
| 165 |
for t in self.transplants.get(node): |
| 166 |
# it might have been stripped |
| 167 |
if not hasnode(repo, t.lnode): |
| 168 |
self.transplants.remove(t) |
| 169 |
return False |
| 170 |
lnoderev = repo.changelog.rev(t.lnode) |
| 171 |
if lnoderev in repo.changelog.ancestors( |
| 172 |
[parentrev], lnoderev, inclusive=True |
| 173 |
): |
| 174 |
return True |
| 175 |
return False |
| 176 |
|
| 177 |
def apply(self, repo, source, revmap, merges, opts=None): |
| 178 |
'''apply the revisions in revmap one by one in revision order''' |
| 179 |
if opts is None: |
| 180 |
opts = {} |
| 181 |
revs = sorted(revmap) |
| 182 |
p1 = repo.dirstate.p1() |
| 183 |
pulls = [] |
| 184 |
diffopts = patch.difffeatureopts(self.ui, opts) |
| 185 |
diffopts.git = True |
| 186 |
|
| 187 |
lock = tr = None |
| 188 |
try: |
| 189 |
lock = repo.lock() |
| 190 |
tr = repo.transaction(b'transplant') |
| 191 |
for rev in revs: |
| 192 |
node = revmap[rev] |
| 193 |
revstr = b'%d:%s' % (rev, short(node)) |
| 194 |
|
| 195 |
if self.applied(repo, node, p1): |
| 196 |
self.ui.warn( |
| 197 |
_(b'skipping already applied revision %s\n') % revstr |
| 198 |
) |
| 199 |
continue |
| 200 |
|
| 201 |
parents = source.changelog.parents(node) |
| 202 |
if not (opts.get(b'filter') or opts.get(b'log')): |
| 203 |
# If the changeset parent is the same as the |
| 204 |
# wdir's parent, just pull it. |
| 205 |
if parents[0] == p1: |
| 206 |
pulls.append(node) |
| 207 |
p1 = node |
| 208 |
continue |
| 209 |
if pulls: |
| 210 |
if source != repo: |
| 211 |
exchange.pull(repo, source.peer(), heads=pulls) |
| 212 |
merge.update(repo[pulls[-1]]) |
| 213 |
p1 = repo.dirstate.p1() |
| 214 |
pulls = [] |
| 215 |
|
| 216 |
domerge = False |
| 217 |
if node in merges: |
| 218 |
# pulling all the merge revs at once would mean we |
| 219 |
# couldn't transplant after the latest even if |
| 220 |
# transplants before them fail. |
| 221 |
domerge = True |
| 222 |
if not hasnode(repo, node): |
| 223 |
exchange.pull(repo, source.peer(), heads=[node]) |
| 224 |
|
| 225 |
skipmerge = False |
| 226 |
if parents[1] != repo.nullid: |
| 227 |
if not opts.get(b'parent'): |
| 228 |
self.ui.note( |
| 229 |
_(b'skipping merge changeset %d:%s\n') |
| 230 |
% (rev, short(node)) |
| 231 |
) |
| 232 |
skipmerge = True |
| 233 |
else: |
| 234 |
parent = source.lookup(opts[b'parent']) |
| 235 |
if parent not in parents: |
| 236 |
raise error.Abort( |
| 237 |
_(b'%s is not a parent of %s') |
| 238 |
% (short(parent), short(node)) |
| 239 |
) |
| 240 |
else: |
| 241 |
parent = parents[0] |
| 242 |
|
| 243 |
if skipmerge: |
| 244 |
patchfile = None |
| 245 |
else: |
| 246 |
fd, patchfile = pycompat.mkstemp(prefix=b'hg-transplant-') |
| 247 |
fp = os.fdopen(fd, 'wb') |
| 248 |
gen = patch.diff(source, parent, node, opts=diffopts) |
| 249 |
for chunk in gen: |
| 250 |
fp.write(chunk) |
| 251 |
fp.close() |
| 252 |
|
| 253 |
del revmap[rev] |
| 254 |
if patchfile or domerge: |
| 255 |
try: |
| 256 |
try: |
| 257 |
n = self.applyone( |
| 258 |
repo, |
| 259 |
node, |
| 260 |
source.changelog.read(node), |
| 261 |
patchfile, |
| 262 |
merge=domerge, |
| 263 |
log=opts.get(b'log'), |
| 264 |
filter=opts.get(b'filter'), |
| 265 |
) |
| 266 |
except TransplantError: |
| 267 |
# Do not rollback, it is up to the user to |
| 268 |
# fix the merge or cancel everything |
| 269 |
tr.close() |
| 270 |
raise |
| 271 |
if n and domerge: |
| 272 |
self.ui.status( |
| 273 |
_(b'%s merged at %s\n') % (revstr, short(n)) |
| 274 |
) |
| 275 |
elif n: |
| 276 |
self.ui.status( |
| 277 |
_(b'%s transplanted to %s\n') |
| 278 |
% (short(node), short(n)) |
| 279 |
) |
| 280 |
finally: |
| 281 |
if patchfile: |
| 282 |
os.unlink(patchfile) |
| 283 |
tr.close() |
| 284 |
if pulls: |
| 285 |
exchange.pull(repo, source.peer(), heads=pulls) |
| 286 |
merge.update(repo[pulls[-1]]) |
| 287 |
finally: |
| 288 |
self.saveseries(revmap, merges) |
| 289 |
self.transplants.write() |
| 290 |
if tr: |
| 291 |
tr.release() |
| 292 |
if lock: |
| 293 |
lock.release() |
| 294 |
|
| 295 |
def filter(self, filter, node, changelog, patchfile): |
| 296 |
'''arbitrarily rewrite changeset before applying it''' |
| 297 |
|
| 298 |
self.ui.status(_(b'filtering %s\n') % patchfile) |
| 299 |
user, date, msg = (changelog[1], changelog[2], changelog[4]) |
| 300 |
fd, headerfile = pycompat.mkstemp(prefix=b'hg-transplant-') |
| 301 |
|
| 302 |
with os.fdopen(fd, 'wb') as fp: |
| 303 |
fp.write(b"# HG changeset patch\n") |
| 304 |
fp.write(b"# User %s\n" % user) |
| 305 |
fp.write(b"# Date %d %d\n" % date) |
| 306 |
fp.write(msg + b'\n') |
| 307 |
|
| 308 |
try: |
| 309 |
self.ui.system( |
| 310 |
b'%s %s %s' |
| 311 |
% ( |
| 312 |
filter, |
| 313 |
procutil.shellquote(headerfile), |
| 314 |
procutil.shellquote(patchfile), |
| 315 |
), |
| 316 |
environ={ |
| 317 |
b'HGUSER': changelog[1], |
| 318 |
b'HGREVISION': hex(node), |
| 319 |
}, |
| 320 |
onerr=error.Abort, |
| 321 |
errprefix=_(b'filter failed'), |
| 322 |
blockedtag=b'transplant_filter', |
| 323 |
) |
| 324 |
|
| 325 |
with open(headerfile, 'rb') as fp: |
| 326 |
user, date, msg = self.parselog(fp)[1:4] |
| 327 |
finally: |
| 328 |
os.unlink(headerfile) |
| 329 |
|
| 330 |
return (user, date, msg) |
| 331 |
|
| 332 |
def applyone( |
| 333 |
self, repo, node, cl, patchfile, merge=False, log=False, filter=None |
| 334 |
): |
| 335 |
'''apply the patch in patchfile to the repository as a transplant''' |
| 336 |
(manifest, user, (time, timezone), files, message) = cl[:5] |
| 337 |
date = b"%d %d" % (time, timezone) |
| 338 |
extra = {b'transplant_source': node} |
| 339 |
if filter: |
| 340 |
(user, date, message) = self.filter(filter, node, cl, patchfile) |
| 341 |
|
| 342 |
if log: |
| 343 |
# we don't translate messages inserted into commits |
| 344 |
message += b'\n(transplanted from %s)' % hex(node) |
| 345 |
|
| 346 |
self.ui.status(_(b'applying %s\n') % short(node)) |
| 347 |
self.ui.note(b'%s %s\n%s\n' % (user, date, message)) |
| 348 |
|
| 349 |
if not patchfile and not merge: |
| 350 |
raise error.Abort(_(b'can only omit patchfile if merging')) |
| 351 |
if patchfile: |
| 352 |
try: |
| 353 |
files = set() |
| 354 |
patch.patch(self.ui, repo, patchfile, files=files, eolmode=None) |
| 355 |
files = list(files) |
| 356 |
except Exception as inst: |
| 357 |
seriespath = os.path.join(self.path, b'series') |
| 358 |
if os.path.exists(seriespath): |
| 359 |
os.unlink(seriespath) |
| 360 |
p1 = repo.dirstate.p1() |
| 361 |
p2 = node |
| 362 |
self.log(user, date, message, p1, p2, merge=merge) |
| 363 |
self.ui.write(stringutil.forcebytestr(inst) + b'\n') |
| 364 |
raise TransplantError( |
| 365 |
_( |
| 366 |
b'fix up the working directory and run ' |
| 367 |
b'hg transplant --continue' |
| 368 |
) |
| 369 |
) |
| 370 |
else: |
| 371 |
files = None |
| 372 |
if merge: |
| 373 |
p1 = repo.dirstate.p1() |
| 374 |
repo.setparents(p1, node) |
| 375 |
m = match.always() |
| 376 |
else: |
| 377 |
m = match.exact(files) |
| 378 |
|
| 379 |
n = repo.commit( |
| 380 |
message, |
| 381 |
user, |
| 382 |
date, |
| 383 |
extra=extra, |
| 384 |
match=m, |
| 385 |
editor=self.getcommiteditor(), |
| 386 |
) |
| 387 |
if not n: |
| 388 |
self.ui.warn(_(b'skipping emptied changeset %s\n') % short(node)) |
| 389 |
return None |
| 390 |
if not merge: |
| 391 |
self.transplants.set(n, node) |
| 392 |
|
| 393 |
return n |
| 394 |
|
| 395 |
def canresume(self): |
| 396 |
return os.path.exists(os.path.join(self.path, b'journal')) |
| 397 |
|
| 398 |
def resume(self, repo, source, opts): |
| 399 |
'''recover last transaction and apply remaining changesets''' |
| 400 |
if os.path.exists(os.path.join(self.path, b'journal')): |
| 401 |
n, node = self.recover(repo, source, opts) |
| 402 |
if n: |
| 403 |
self.ui.status( |
| 404 |
_(b'%s transplanted as %s\n') % (short(node), short(n)) |
| 405 |
) |
| 406 |
else: |
| 407 |
self.ui.status( |
| 408 |
_(b'%s skipped due to empty diff\n') % (short(node),) |
| 409 |
) |
| 410 |
seriespath = os.path.join(self.path, b'series') |
| 411 |
if not os.path.exists(seriespath): |
| 412 |
self.transplants.write() |
| 413 |
return |
| 414 |
nodes, merges = self.readseries() |
| 415 |
revmap = {} |
| 416 |
for n in nodes: |
| 417 |
revmap[source.changelog.rev(n)] = n |
| 418 |
os.unlink(seriespath) |
| 419 |
|
| 420 |
self.apply(repo, source, revmap, merges, opts) |
| 421 |
|
| 422 |
def recover(self, repo, source, opts): |
| 423 |
'''commit working directory using journal metadata''' |
| 424 |
node, user, date, message, parents = self.readlog() |
| 425 |
merge = False |
| 426 |
|
| 427 |
if not user or not date or not message or not parents[0]: |
| 428 |
raise error.Abort(_(b'transplant log file is corrupt')) |
| 429 |
|
| 430 |
parent = parents[0] |
| 431 |
if len(parents) > 1: |
| 432 |
if opts.get(b'parent'): |
| 433 |
parent = source.lookup(opts[b'parent']) |
| 434 |
if parent not in parents: |
| 435 |
raise error.Abort( |
| 436 |
_(b'%s is not a parent of %s') |
| 437 |
% (short(parent), short(node)) |
| 438 |
) |
| 439 |
else: |
| 440 |
merge = True |
| 441 |
|
| 442 |
extra = {b'transplant_source': node} |
| 443 |
try: |
| 444 |
p1 = repo.dirstate.p1() |
| 445 |
if p1 != parent: |
| 446 |
raise error.Abort( |
| 447 |
_(b'working directory not at transplant parent %s') |
| 448 |
% hex(parent) |
| 449 |
) |
| 450 |
if merge: |
| 451 |
repo.setparents(p1, parents[1]) |
| 452 |
st = repo.status() |
| 453 |
modified, added, removed, deleted = ( |
| 454 |
st.modified, |
| 455 |
st.added, |
| 456 |
st.removed, |
| 457 |
st.deleted, |
| 458 |
) |
| 459 |
if merge or modified or added or removed or deleted: |
| 460 |
n = repo.commit( |
| 461 |
message, |
| 462 |
user, |
| 463 |
date, |
| 464 |
extra=extra, |
| 465 |
editor=self.getcommiteditor(), |
| 466 |
) |
| 467 |
if not n: |
| 468 |
raise error.Abort(_(b'commit failed')) |
| 469 |
if not merge: |
| 470 |
self.transplants.set(n, node) |
| 471 |
else: |
| 472 |
n = None |
| 473 |
self.unlog() |
| 474 |
|
| 475 |
return n, node |
| 476 |
finally: |
| 477 |
# TODO: get rid of this meaningless try/finally enclosing. |
| 478 |
# this is kept only to reduce changes in a patch. |
| 479 |
pass |
| 480 |
|
| 481 |
def stop(self, ui, repo): |
| 482 |
"""logic to stop an interrupted transplant""" |
| 483 |
if self.canresume(): |
| 484 |
startctx = repo[b'.'] |
| 485 |
merge.clean_update(startctx) |
| 486 |
ui.status(_(b"stopped the interrupted transplant\n")) |
| 487 |
ui.status( |
| 488 |
_(b"working directory is now at %s\n") % startctx.hex()[:12] |
| 489 |
) |
| 490 |
self.unlog() |
| 491 |
return 0 |
| 492 |
|
| 493 |
def readseries(self): |
| 494 |
nodes = [] |
| 495 |
merges = [] |
| 496 |
cur = nodes |
| 497 |
for line in self.opener.read(b'series').splitlines(): |
| 498 |
if line.startswith(b'# Merges'): |
| 499 |
cur = merges |
| 500 |
continue |
| 501 |
cur.append(bin(line)) |
| 502 |
|
| 503 |
return (nodes, merges) |
| 504 |
|
| 505 |
def saveseries(self, revmap, merges): |
| 506 |
if not revmap: |
| 507 |
return |
| 508 |
|
| 509 |
if not os.path.isdir(self.path): |
| 510 |
os.mkdir(self.path) |
| 511 |
series = self.opener(b'series', b'w') |
| 512 |
for rev in sorted(revmap): |
| 513 |
series.write(hex(revmap[rev]) + b'\n') |
| 514 |
if merges: |
| 515 |
series.write(b'# Merges\n') |
| 516 |
for m in merges: |
| 517 |
series.write(hex(m) + b'\n') |
| 518 |
series.close() |
| 519 |
|
| 520 |
def parselog(self, fp): |
| 521 |
parents = [] |
| 522 |
message = [] |
| 523 |
node = self.repo.nullid |
| 524 |
inmsg = False |
| 525 |
user = None |
| 526 |
date = None |
| 527 |
for line in fp.read().splitlines(): |
| 528 |
if inmsg: |
| 529 |
message.append(line) |
| 530 |
elif line.startswith(b'# User '): |
| 531 |
user = line[7:] |
| 532 |
elif line.startswith(b'# Date '): |
| 533 |
date = line[7:] |
| 534 |
elif line.startswith(b'# Node ID '): |
| 535 |
node = bin(line[10:]) |
| 536 |
elif line.startswith(b'# Parent '): |
| 537 |
parents.append(bin(line[9:])) |
| 538 |
elif not line.startswith(b'# '): |
| 539 |
inmsg = True |
| 540 |
message.append(line) |
| 541 |
if None in (user, date): |
| 542 |
raise error.Abort( |
| 543 |
_(b"filter corrupted changeset (no user or date)") |
| 544 |
) |
| 545 |
return (node, user, date, b'\n'.join(message), parents) |
| 546 |
|
| 547 |
def log(self, user, date, message, p1, p2, merge=False): |
| 548 |
'''journal changelog metadata for later recover''' |
| 549 |
|
| 550 |
if not os.path.isdir(self.path): |
| 551 |
os.mkdir(self.path) |
| 552 |
fp = self.opener(b'journal', b'w') |
| 553 |
fp.write(b'# User %s\n' % user) |
| 554 |
fp.write(b'# Date %s\n' % date) |
| 555 |
fp.write(b'# Node ID %s\n' % hex(p2)) |
| 556 |
fp.write(b'# Parent ' + hex(p1) + b'\n') |
| 557 |
if merge: |
| 558 |
fp.write(b'# Parent ' + hex(p2) + b'\n') |
| 559 |
fp.write(message.rstrip() + b'\n') |
| 560 |
fp.close() |
| 561 |
|
| 562 |
def readlog(self): |
| 563 |
return self.parselog(self.opener(b'journal')) |
| 564 |
|
| 565 |
def unlog(self): |
| 566 |
'''remove changelog journal''' |
| 567 |
absdst = os.path.join(self.path, b'journal') |
| 568 |
if os.path.exists(absdst): |
| 569 |
os.unlink(absdst) |
| 570 |
|
| 571 |
def transplantfilter(self, repo, source, root): |
| 572 |
def matchfn(node): |
| 573 |
if self.applied(repo, node, root): |
| 574 |
return False |
| 575 |
if source.changelog.parents(node)[1] != repo.nullid: |
| 576 |
return False |
| 577 |
extra = source.changelog.read(node)[5] |
| 578 |
cnode = extra.get(b'transplant_source') |
| 579 |
if cnode and self.applied(repo, cnode, root): |
| 580 |
return False |
| 581 |
return True |
| 582 |
|
| 583 |
return matchfn |
| 584 |
|
| 585 |
|
| 586 |
def hasnode(repo, node): |
| 587 |
try: |
| 588 |
return repo.changelog.rev(node) is not None |
| 589 |
except error.StorageError: |
| 590 |
return False |
| 591 |
|
| 592 |
|
| 593 |
def browserevs(ui, repo, nodes, opts): |
| 594 |
'''interactively transplant changesets''' |
| 595 |
displayer = logcmdutil.changesetdisplayer(ui, repo, opts) |
| 596 |
transplants = [] |
| 597 |
merges = [] |
| 598 |
prompt = _( |
| 599 |
b'apply changeset? [ynmpcq?]:' |
| 600 |
b'$$ &yes, transplant this changeset' |
| 601 |
b'$$ &no, skip this changeset' |
| 602 |
b'$$ &merge at this changeset' |
| 603 |
b'$$ show &patch' |
| 604 |
b'$$ &commit selected changesets' |
| 605 |
b'$$ &quit and cancel transplant' |
| 606 |
b'$$ &? (show this help)' |
| 607 |
) |
| 608 |
for node in nodes: |
| 609 |
displayer.show(repo[node]) |
| 610 |
action = None |
| 611 |
while not action: |
| 612 |
choice = ui.promptchoice(prompt) |
| 613 |
action = b'ynmpcq?'[choice : choice + 1] |
| 614 |
if action == b'?': |
| 615 |
for c, t in ui.extractchoices(prompt)[1]: |
| 616 |
ui.write(b'%s: %s\n' % (c, t)) |
| 617 |
action = None |
| 618 |
elif action == b'p': |
| 619 |
parent = repo.changelog.parents(node)[0] |
| 620 |
for chunk in patch.diff(repo, parent, node): |
| 621 |
ui.write(chunk) |
| 622 |
action = None |
| 623 |
if action == b'y': |
| 624 |
transplants.append(node) |
| 625 |
elif action == b'm': |
| 626 |
merges.append(node) |
| 627 |
elif action == b'c': |
| 628 |
break |
| 629 |
elif action == b'q': |
| 630 |
transplants = () |
| 631 |
merges = () |
| 632 |
break |
| 633 |
displayer.close() |
| 634 |
return (transplants, merges) |
| 635 |
|
| 636 |
|
| 637 |
@command( |
| 638 |
b'transplant', |
| 639 |
[ |
| 640 |
( |
| 641 |
b's', |
| 642 |
b'source', |
| 643 |
b'', |
| 644 |
_(b'transplant changesets from REPO'), |
| 645 |
_(b'REPO'), |
| 646 |
), |
| 647 |
( |
| 648 |
b'b', |
| 649 |
b'branch', |
| 650 |
[], |
| 651 |
_(b'use this source changeset as head'), |
| 652 |
_(b'REV'), |
| 653 |
), |
| 654 |
( |
| 655 |
b'a', |
| 656 |
b'all', |
| 657 |
None, |
| 658 |
_(b'pull all changesets up to the --branch revisions'), |
| 659 |
), |
| 660 |
(b'p', b'prune', [], _(b'skip over REV'), _(b'REV')), |
| 661 |
(b'm', b'merge', [], _(b'merge at REV'), _(b'REV')), |
| 662 |
( |
| 663 |
b'', |
| 664 |
b'parent', |
| 665 |
b'', |
| 666 |
_(b'parent to choose when transplanting merge'), |
| 667 |
_(b'REV'), |
| 668 |
), |
| 669 |
(b'e', b'edit', False, _(b'invoke editor on commit messages')), |
| 670 |
(b'', b'log', None, _(b'append transplant info to log message')), |
| 671 |
(b'', b'stop', False, _(b'stop interrupted transplant')), |
| 672 |
( |
| 673 |
b'c', |
| 674 |
b'continue', |
| 675 |
None, |
| 676 |
_(b'continue last transplant session after fixing conflicts'), |
| 677 |
), |
| 678 |
( |
| 679 |
b'', |
| 680 |
b'filter', |
| 681 |
b'', |
| 682 |
_(b'filter changesets through command'), |
| 683 |
_(b'CMD'), |
| 684 |
), |
| 685 |
], |
| 686 |
_( |
| 687 |
b'hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] ' |
| 688 |
b'[-m REV] [REV]...' |
| 689 |
), |
| 690 |
helpcategory=command.CATEGORY_CHANGE_MANAGEMENT, |
| 691 |
) |
| 692 |
def transplant(ui, repo, *revs, **opts): |
| 693 |
"""transplant changesets from another branch |
| 694 |
|
| 695 |
Selected changesets will be applied on top of the current working |
| 696 |
directory with the log of the original changeset. The changesets |
| 697 |
are copied and will thus appear twice in the history with different |
| 698 |
identities. |
| 699 |
|
| 700 |
Consider using the graft command if everything is inside the same |
| 701 |
repository - it will use merges and will usually give a better result. |
| 702 |
Use the rebase extension if the changesets are unpublished and you want |
| 703 |
to move them instead of copying them. |
| 704 |
|
| 705 |
If --log is specified, log messages will have a comment appended |
| 706 |
of the form:: |
| 707 |
|
| 708 |
(transplanted from CHANGESETHASH) |
| 709 |
|
| 710 |
You can rewrite the changelog message with the --filter option. |
| 711 |
Its argument will be invoked with the current changelog message as |
| 712 |
$1 and the patch as $2. |
| 713 |
|
| 714 |
--source/-s specifies another repository to use for selecting changesets, |
| 715 |
just as if it temporarily had been pulled. |
| 716 |
If --branch/-b is specified, these revisions will be used as |
| 717 |
heads when deciding which changesets to transplant, just as if only |
| 718 |
these revisions had been pulled. |
| 719 |
If --all/-a is specified, all the revisions up to the heads specified |
| 720 |
with --branch will be transplanted. |
| 721 |
|
| 722 |
Example: |
| 723 |
|
| 724 |
- transplant all changes up to REV on top of your current revision:: |
| 725 |
|
| 726 |
hg transplant --branch REV --all |
| 727 |
|
| 728 |
You can optionally mark selected transplanted changesets as merge |
| 729 |
changesets. You will not be prompted to transplant any ancestors |
| 730 |
of a merged transplant, and you can merge descendants of them |
| 731 |
normally instead of transplanting them. |
| 732 |
|
| 733 |
Merge changesets may be transplanted directly by specifying the |
| 734 |
proper parent changeset by calling :hg:`transplant --parent`. |
| 735 |
|
| 736 |
If no merges or revisions are provided, :hg:`transplant` will |
| 737 |
start an interactive changeset browser. |
| 738 |
|
| 739 |
If a changeset application fails, you can fix the merge by hand |
| 740 |
and then resume where you left off by calling :hg:`transplant |
| 741 |
--continue/-c`. |
| 742 |
""" |
| 743 |
with repo.wlock(): |
| 744 |
return _dotransplant(ui, repo, *revs, **opts) |
| 745 |
|
| 746 |
|
| 747 |
def _dotransplant(ui, repo, *revs, **opts): |
| 748 |
def incwalk(repo, csets, match=util.always): |
| 749 |
for node in csets: |
| 750 |
if match(node): |
| 751 |
yield node |
| 752 |
|
| 753 |
def transplantwalk(repo, dest, heads, match=util.always): |
| 754 |
"""Yield all nodes that are ancestors of a head but not ancestors |
| 755 |
of dest. |
| 756 |
If no heads are specified, the heads of repo will be used.""" |
| 757 |
if not heads: |
| 758 |
heads = repo.heads() |
| 759 |
ancestors = [] |
| 760 |
ctx = repo[dest] |
| 761 |
for head in heads: |
| 762 |
ancestors.append(ctx.ancestor(repo[head]).node()) |
| 763 |
for node in repo.changelog.nodesbetween(ancestors, heads)[0]: |
| 764 |
if match(node): |
| 765 |
yield node |
| 766 |
|
| 767 |
def checkopts(opts, revs): |
| 768 |
if opts.get(b'continue'): |
| 769 |
cmdutil.check_incompatible_arguments( |
| 770 |
opts, b'continue', [b'branch', b'all', b'merge'] |
| 771 |
) |
| 772 |
return |
| 773 |
if opts.get(b'stop'): |
| 774 |
cmdutil.check_incompatible_arguments( |
| 775 |
opts, b'stop', [b'branch', b'all', b'merge'] |
| 776 |
) |
| 777 |
return |
| 778 |
if not ( |
| 779 |
opts.get(b'source') |
| 780 |
or revs |
| 781 |
or opts.get(b'merge') |
| 782 |
or opts.get(b'branch') |
| 783 |
): |
| 784 |
raise error.Abort( |
| 785 |
_( |
| 786 |
b'no source URL, branch revision, or revision ' |
| 787 |
b'list provided' |
| 788 |
) |
| 789 |
) |
| 790 |
if opts.get(b'all'): |
| 791 |
if not opts.get(b'branch'): |
| 792 |
raise error.Abort(_(b'--all requires a branch revision')) |
| 793 |
if revs: |
| 794 |
raise error.Abort( |
| 795 |
_(b'--all is incompatible with a revision list') |
| 796 |
) |
| 797 |
|
| 798 |
opts = pycompat.byteskwargs(opts) |
| 799 |
checkopts(opts, revs) |
| 800 |
|
| 801 |
if not opts.get(b'log'): |
| 802 |
# deprecated config: transplant.log |
| 803 |
opts[b'log'] = ui.config(b'transplant', b'log') |
| 804 |
if not opts.get(b'filter'): |
| 805 |
# deprecated config: transplant.filter |
| 806 |
opts[b'filter'] = ui.config(b'transplant', b'filter') |
| 807 |
|
| 808 |
tp = transplanter(ui, repo, opts) |
| 809 |
|
| 810 |
p1 = repo.dirstate.p1() |
| 811 |
if len(repo) > 0 and p1 == repo.nullid: |
| 812 |
raise error.Abort(_(b'no revision checked out')) |
| 813 |
if opts.get(b'continue'): |
| 814 |
if not tp.canresume(): |
| 815 |
raise error.StateError(_(b'no transplant to continue')) |
| 816 |
elif opts.get(b'stop'): |
| 817 |
if not tp.canresume(): |
| 818 |
raise error.StateError(_(b'no interrupted transplant found')) |
| 819 |
return tp.stop(ui, repo) |
| 820 |
else: |
| 821 |
cmdutil.checkunfinished(repo) |
| 822 |
scmutil.bail_if_changed(repo) |
| 823 |
|
| 824 |
sourcerepo = opts.get(b'source') |
| 825 |
if sourcerepo: |
| 826 |
path = urlutil.get_unique_pull_path_obj(b'transplant', ui, sourcerepo) |
| 827 |
peer = repo_factory.peer(repo, opts, path) |
| 828 |
heads = pycompat.maplist(peer.lookup, opts.get(b'branch', ())) |
| 829 |
target = set(heads) |
| 830 |
for r in revs: |
| 831 |
try: |
| 832 |
target.add(peer.lookup(r)) |
| 833 |
except error.RepoError: |
| 834 |
pass |
| 835 |
source, csets, cleanupfn = bundlerepo.getremotechanges( |
| 836 |
ui, repo, peer, onlyheads=sorted(target), force=True |
| 837 |
) |
| 838 |
else: |
| 839 |
source = repo |
| 840 |
heads = pycompat.maplist(source.lookup, opts.get(b'branch', ())) |
| 841 |
cleanupfn = None |
| 842 |
|
| 843 |
try: |
| 844 |
if opts.get(b'continue'): |
| 845 |
tp.resume(repo, source, opts) |
| 846 |
return |
| 847 |
|
| 848 |
tf = tp.transplantfilter(repo, source, p1) |
| 849 |
if opts.get(b'prune'): |
| 850 |
prune = { |
| 851 |
source[r].node() |
| 852 |
for r in logcmdutil.revrange(source, opts.get(b'prune')) |
| 853 |
} |
| 854 |
matchfn = lambda x: tf(x) and x not in prune |
| 855 |
else: |
| 856 |
matchfn = tf |
| 857 |
merges = pycompat.maplist(source.lookup, opts.get(b'merge', ())) |
| 858 |
revmap = {} |
| 859 |
if revs: |
| 860 |
for r in logcmdutil.revrange(source, revs): |
| 861 |
revmap[int(r)] = source[r].node() |
| 862 |
elif opts.get(b'all') or not merges: |
| 863 |
if source != repo: |
| 864 |
alltransplants = incwalk(source, csets, match=matchfn) |
| 865 |
else: |
| 866 |
alltransplants = transplantwalk( |
| 867 |
source, p1, heads, match=matchfn |
| 868 |
) |
| 869 |
if opts.get(b'all'): |
| 870 |
revs = alltransplants |
| 871 |
else: |
| 872 |
revs, newmerges = browserevs(ui, source, alltransplants, opts) |
| 873 |
merges.extend(newmerges) |
| 874 |
for r in revs: |
| 875 |
revmap[source.changelog.rev(r)] = r |
| 876 |
for r in merges: |
| 877 |
revmap[source.changelog.rev(r)] = r |
| 878 |
|
| 879 |
tp.apply(repo, source, revmap, merges, opts) |
| 880 |
finally: |
| 881 |
if cleanupfn: |
| 882 |
cleanupfn() |
| 883 |
|
| 884 |
|
| 885 |
def continuecmd(ui, repo): |
| 886 |
"""logic to resume an interrupted transplant using |
| 887 |
'hg continue'""" |
| 888 |
with repo.wlock(): |
| 889 |
tp = transplanter(ui, repo, {}) |
| 890 |
return tp.resume(repo, repo, {}) |
| 891 |
|
| 892 |
|
| 893 |
revsetpredicate = registrar.revsetpredicate() |
| 894 |
|
| 895 |
|
| 896 |
@revsetpredicate(b'transplanted([set])') |
| 897 |
def revsettransplanted(repo, subset, x): |
| 898 |
"""Transplanted changesets in set, or all transplanted changesets.""" |
| 899 |
if x: |
| 900 |
s = revset.getset(repo, subset, x) |
| 901 |
else: |
| 902 |
s = subset |
| 903 |
return smartset.baseset( |
| 904 |
[r for r in s if repo[r].extra().get(b'transplant_source')] |
| 905 |
) |
| 906 |
|
| 907 |
|
| 908 |
templatekeyword = registrar.templatekeyword() |
| 909 |
|
| 910 |
|
| 911 |
@templatekeyword(b'transplanted', requires={b'ctx'}) |
| 912 |
def kwtransplanted(context, mapping): |
| 913 |
"""String. The node identifier of the transplanted |
| 914 |
changeset if any.""" |
| 915 |
ctx = context.resource(mapping, b'ctx') |
| 916 |
n = ctx.extra().get(b'transplant_source') |
| 917 |
return n and hex(n) or b'' |
| 918 |
|
| 919 |
|
| 920 |
def extsetup(ui): |
| 921 |
statemod.addunfinished( |
| 922 |
b'transplant', |
| 923 |
fname=b'transplant/journal', |
| 924 |
clearable=True, |
| 925 |
continuefunc=continuecmd, |
| 926 |
statushint=_( |
| 927 |
b'To continue: hg transplant --continue\n' |
| 928 |
b'To stop: hg transplant --stop' |
| 929 |
), |
| 930 |
cmdhint=_(b"use 'hg transplant --continue' or 'hg transplant --stop'"), |
| 931 |
) |
| 932 |
|
| 933 |
|
| 934 |
# tell hggettext to extract docstrings from these functions: |
| 935 |
i18nfunctions = [revsettransplanted, kwtransplanted] |