split.py
197 lines
| 6.4 KiB
| text/x-python
|
PythonLexer
/ hgext / split.py
|
r35471 | # split.py - split a changeset into smaller ones | ||
# | ||||
# Copyright 2015 Laurent Charignon <lcharignon@fb.com> | ||||
# Copyright 2017 Facebook, Inc. | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
"""command to split a changeset into smaller ones (EXPERIMENTAL)""" | ||||
from __future__ import absolute_import | ||||
from mercurial.i18n import _ | ||||
from mercurial.node import ( | ||||
nullid, | ||||
short, | ||||
) | ||||
from mercurial import ( | ||||
bookmarks, | ||||
cmdutil, | ||||
commands, | ||||
error, | ||||
hg, | ||||
|
r36418 | pycompat, | ||
|
r35471 | registrar, | ||
revsetlang, | ||||
|
r44387 | rewriteutil, | ||
|
r35471 | scmutil, | ||
|
r47492 | util, | ||
|
r35471 | ) | ||
# allow people to use split without explicitly enabling rebase extension | ||||
|
r43346 | from . import rebase | ||
|
r35471 | |||
cmdtable = {} | ||||
command = registrar.command(cmdtable) | ||||
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
|
r43347 | testedwith = b'ships-with-hg-core' | ||
|
r35471 | |||
|
r43346 | |||
@command( | ||||
|
r43347 | b'split', | ||
|
r43346 | [ | ||
|
r43347 | (b'r', b'rev', b'', _(b"revision to split"), _(b'REV')), | ||
(b'', b'rebase', True, _(b'rebase descendants after split')), | ||||
|
r43346 | ] | ||
+ cmdutil.commitopts2, | ||||
|
r43347 | _(b'hg split [--no-rebase] [[-r] REV]'), | ||
|
r43346 | helpcategory=command.CATEGORY_CHANGE_MANAGEMENT, | ||
helpbasic=True, | ||||
) | ||||
|
r35471 | def split(ui, repo, *revs, **opts): | ||
"""split a changeset into smaller ones | ||||
Repeatedly prompt changes and commit message for new changesets until there | ||||
is nothing left in the original changeset. | ||||
If --rev was not given, split the working directory parent. | ||||
By default, rebase connected non-obsoleted descendants onto the new | ||||
changeset. Use --no-rebase to avoid the rebase. | ||||
""" | ||||
|
r38080 | opts = pycompat.byteskwargs(opts) | ||
|
r35471 | revlist = [] | ||
|
r43347 | if opts.get(b'rev'): | ||
revlist.append(opts.get(b'rev')) | ||||
|
r35471 | revlist.extend(revs) | ||
|
r47492 | with repo.wlock(), repo.lock(): | ||
tr = repo.transaction(b'split') | ||||
# If the rebase somehow runs into conflicts, make sure | ||||
# we close the transaction so the user can continue it. | ||||
with util.acceptintervention(tr): | ||||
revs = scmutil.revrange(repo, revlist or [b'.']) | ||||
if len(revs) > 1: | ||||
raise error.InputError(_(b'cannot split multiple revisions')) | ||||
|
r35471 | |||
|
r47492 | rev = revs.first() | ||
ctx = repo[rev] | ||||
# Handle nullid specially here (instead of leaving for precheck() | ||||
# below) so we get a nicer message and error code. | ||||
if rev is None or ctx.node() == nullid: | ||||
ui.status(_(b'nothing to split\n')) | ||||
return 1 | ||||
if ctx.node() is None: | ||||
raise error.InputError(_(b'cannot split working directory')) | ||||
|
r35471 | |||
|
r47492 | if opts.get(b'rebase'): | ||
# Skip obsoleted descendants and their descendants so the rebase | ||||
# won't cause conflicts for sure. | ||||
descendants = list(repo.revs(b'(%d::) - (%d)', rev, rev)) | ||||
torebase = list( | ||||
repo.revs( | ||||
b'%ld - (%ld & obsolete())::', descendants, descendants | ||||
) | ||||
|
r43346 | ) | ||
|
r47492 | else: | ||
torebase = [] | ||||
rewriteutil.precheck(repo, [rev] + torebase, b'split') | ||||
|
r35471 | |||
|
r47492 | if len(ctx.parents()) > 1: | ||
raise error.InputError(_(b'cannot split a merge changeset')) | ||||
|
r35471 | |||
|
r47492 | cmdutil.bailifchanged(repo) | ||
|
r35471 | |||
|
r47492 | # Deactivate bookmark temporarily so it won't get moved | ||
# unintentionally | ||||
bname = repo._activebookmark | ||||
if bname and repo._bookmarks[bname] != ctx.node(): | ||||
bookmarks.deactivate(repo) | ||||
|
r35471 | |||
|
r47492 | wnode = repo[b'.'].node() | ||
top = None | ||||
try: | ||||
top = dosplit(ui, repo, tr, ctx, opts) | ||||
finally: | ||||
# top is None: split failed, need update --clean recovery. | ||||
# wnode == ctx.node(): wnode split, no need to update. | ||||
if top is None or wnode != ctx.node(): | ||||
hg.clean(repo, wnode, show_stats=False) | ||||
if bname: | ||||
bookmarks.activate(repo, bname) | ||||
if torebase and top: | ||||
dorebase(ui, repo, torebase, top) | ||||
|
r35471 | |||
|
r43346 | |||
|
r35471 | def dosplit(ui, repo, tr, ctx, opts): | ||
|
r43346 | committed = [] # [ctx] | ||
|
r35471 | |||
# Set working parent to ctx.p1(), and keep working copy as ctx's content | ||||
|
r42132 | if ctx.node() != repo.dirstate.p1(): | ||
hg.clean(repo, ctx.node(), show_stats=False) | ||||
with repo.dirstate.parentchange(): | ||||
scmutil.movedirstate(repo, ctx.p1()) | ||||
|
r35471 | |||
# Any modified, added, removed, deleted result means split is incomplete | ||||
|
r44040 | def incomplete(repo): | ||
st = repo.status() | ||||
return any((st.modified, st.added, st.removed, st.deleted)) | ||||
|
r35471 | |||
# Main split loop | ||||
while incomplete(repo): | ||||
if committed: | ||||
|
r43346 | header = _( | ||
|
r43347 | b'HG: Splitting %s. So far it has been split into:\n' | ||
|
r43346 | ) % short(ctx.node()) | ||
|
r46460 | # We don't want color codes in the commit message template, so | ||
# disable the label() template function while we render it. | ||||
with ui.configoverride( | ||||
{(b'templatealias', b'label(l,x)'): b"x"}, b'split' | ||||
): | ||||
for c in committed: | ||||
summary = cmdutil.format_changeset_summary(ui, c, b'split') | ||||
header += _(b'HG: - %s\n') % summary | ||||
|
r43346 | header += _( | ||
|
r43387 | b'HG: Write commit message for the next split changeset.\n' | ||
|
r43346 | ) | ||
|
r35471 | else: | ||
|
r43346 | header = _( | ||
|
r43347 | b'HG: Splitting %s. Write commit message for the ' | ||
b'first split changeset.\n' | ||||
|
r43346 | ) % short(ctx.node()) | ||
opts.update( | ||||
{ | ||||
|
r43347 | b'edit': True, | ||
b'interactive': True, | ||||
b'message': header + ctx.description(), | ||||
|
r43346 | } | ||
) | ||||
|
r36418 | commands.commit(ui, repo, **pycompat.strkwargs(opts)) | ||
|
r43347 | newctx = repo[b'.'] | ||
|
r35471 | committed.append(newctx) | ||
if not committed: | ||||
|
r46488 | raise error.InputError(_(b'cannot split an empty revision')) | ||
|
r35471 | |||
|
r43346 | scmutil.cleanupnodes( | ||
repo, | ||||
{ctx.node(): [c.node() for c in committed]}, | ||||
|
r43347 | operation=b'split', | ||
|
r43346 | fixphase=True, | ||
) | ||||
|
r35471 | |||
return committed[-1] | ||||
|
r43346 | |||
|
r36426 | def dorebase(ui, repo, src, destctx): | ||
|
r43346 | rebase.rebase( | ||
ui, | ||||
repo, | ||||
|
r43347 | rev=[revsetlang.formatspec(b'%ld', src)], | ||
dest=revsetlang.formatspec(b'%d', destctx.rev()), | ||||
|
r43346 | ) | ||