Debian Codegraph @

hgext/split.py

raw
1 # split.py - split a changeset into smaller ones
2 #
3 # Copyright 2015 Laurent Charignon <[email protected]>
4 # Copyright 2017 Facebook, Inc.
5 #
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
8 """command to split a changeset into smaller ones (EXPERIMENTAL)"""
9
10 from __future__ import annotations
11
12 from mercurial.i18n import _
13
14 from mercurial.node import (
15 nullrev,
16 short,
17 )
18
19 from mercurial import (
20 bookmarks,
21 cmdutil,
22 commands,
23 error,
24 logcmdutil,
25 registrar,
26 revsetlang,
27 rewriteutil,
28 scmutil,
29 util,
30 )
31 from mercurial.cmd_impls import (
32 update as up_impl,
33 )
34
35 # allow people to use split without explicitly enabling rebase extension
36 from . import rebase
37
38 cmdtable = {}
39 command = registrar.command(cmdtable)
40
41 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
42 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
43 # be specifying the version(s) of Mercurial they are tested with, or
44 # leave the attribute unspecified.
45 testedwith = b'ships-with-hg-core'
46
47
48 @command(
49 b'split',
50 [
51 (b'r', b'rev', b'', _(b"revision to split"), _(b'REV')),
52 (b'', b'rebase', True, _(b'rebase descendants after split')),
53 ]
54 + cmdutil.commitopts2,
55 _(b'hg split [--no-rebase] [[-r] REV]'),
56 helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
57 helpbasic=True,
58 )
59 def split(ui, repo, *revs, **opts):
60 """split a changeset into smaller ones
61
62 Repeatedly prompt changes and commit message for new changesets until there
63 is nothing left in the original changeset.
64
65 If --rev was not given, split the working directory parent.
66
67 By default, rebase connected non-obsoleted descendants onto the new
68 changeset. Use --no-rebase to avoid the rebase.
69 """
70 revlist = []
71 if opts.get('rev'):
72 revlist.append(opts.get('rev'))
73 revlist.extend(revs)
74 with repo.wlock(), repo.lock():
75 tr = repo.transaction(b'split')
76 # If the rebase somehow runs into conflicts, make sure
77 # we close the transaction so the user can continue it.
78 with util.acceptintervention(tr):
79 revs = logcmdutil.revrange(repo, revlist or [b'.'])
80 if len(revs) > 1:
81 raise error.InputError(_(b'cannot split multiple revisions'))
82
83 rev = revs.first()
84 # Handle nullrev specially here (instead of leaving for precheck()
85 # below) so we get a nicer message and error code.
86 if rev is None or rev == nullrev:
87 ui.status(_(b'nothing to split\n'))
88 return 1
89 ctx = repo[rev]
90 if ctx.node() is None:
91 raise error.InputError(_(b'cannot split working directory'))
92
93 if opts.get('rebase'):
94 # Skip obsoleted descendants and their descendants so the rebase
95 # won't cause conflicts for sure.
96 descendants = list(repo.revs(b'(%d::) - (%d)', rev, rev))
97 torebase = list(
98 repo.revs(
99 b'%ld - (%ld & obsolete())::', descendants, descendants
100 )
101 )
102 else:
103 torebase = []
104 rewriteutil.precheck(repo, [rev] + torebase, b'split')
105
106 if len(ctx.parents()) > 1:
107 raise error.InputError(_(b'cannot split a merge changeset'))
108
109 scmutil.bail_if_changed(repo)
110
111 # Deactivate bookmark temporarily so it won't get moved
112 # unintentionally
113 bname = repo._activebookmark
114 if bname and repo._bookmarks[bname] != ctx.node():
115 bookmarks.deactivate(repo)
116
117 wnode = repo[b'.'].node()
118 top = None
119 try:
120 top = dosplit(ui, repo, tr, ctx, **opts)
121 finally:
122 # top is None: split failed, need update --clean recovery.
123 # wnode == ctx.node(): wnode split, no need to update.
124 if top is None or wnode != ctx.node():
125 up_impl.clean(repo, wnode, show_stats=False)
126 if bname:
127 bookmarks.activate(repo, bname)
128 if torebase and top:
129 dorebase(ui, repo, torebase, top)
130
131
132 def dosplit(ui, repo, tr, ctx, **opts):
133 committed = [] # [ctx]
134
135 # Set working parent to ctx.p1(), and keep working copy as ctx's content
136 if ctx.node() != repo.dirstate.p1():
137 up_impl.clean(repo, ctx.node(), show_stats=False)
138 with repo.dirstate.changing_parents(repo):
139 scmutil.movedirstate(repo, ctx.p1())
140
141 # Any modified, added, removed, deleted result means split is incomplete
142 def incomplete(repo):
143 st = repo.status()
144 return any((st.modified, st.added, st.removed, st.deleted))
145
146 # Main split loop
147 while incomplete(repo):
148 if committed:
149 header = _(
150 b'HG: Splitting %s. So far it has been split into:\n'
151 ) % short(ctx.node())
152 # We don't want color codes in the commit message template, so
153 # disable the label() template function while we render it.
154 with ui.configoverride(
155 {(b'templatealias', b'label(l,x)'): b"x"}, b'split'
156 ):
157 for c in committed:
158 summary = cmdutil.format_changeset_summary(ui, c, b'split')
159 header += _(b'HG: - %s\n') % summary
160 header += _(
161 b'HG: Write commit message for the next split changeset.\n'
162 )
163 else:
164 header = _(
165 b'HG: Splitting %s. Write commit message for the '
166 b'first split changeset.\n'
167 ) % short(ctx.node())
168 opts.update(
169 {
170 'edit': True,
171 'interactive': True,
172 'message': header + ctx.description(),
173 }
174 )
175 origctx = repo[b'.']
176 commands.commit(ui, repo, **opts)
177 newctx = repo[b'.']
178 # Ensure user didn't do a "no-op" split (such as deselecting
179 # everything).
180 if origctx.node() != newctx.node():
181 committed.append(newctx)
182
183 if not committed:
184 raise error.InputError(_(b'cannot split an empty revision'))
185
186 if len(committed) != 1 or committed[0].node() != ctx.node():
187 # Ensure we don't strip a node if we produce the same commit as already
188 # exists
189 cmdutil.cleanup_nodes(
190 repo,
191 {ctx.node(): [c.node() for c in committed]},
192 operation=b'split',
193 fixphase=True,
194 )
195
196 return committed[-1]
197
198
199 def dorebase(ui, repo, src, destctx):
200 rebase.rebase(
201 ui,
202 repo,
203 rev=[revsetlang.formatspec(b'%ld', src)],
204 dest=revsetlang.formatspec(b'%d', destctx.rev()),
205 )