##// END OF EJS Templates
histedit: let the state expose a context but serialize correctly to nodes...
David Soria Parra -
r22979:bb22cd70 default
parent child Browse files
Show More
@@ -1,975 +1,971 b''
1 # histedit.py - interactive history editing for mercurial
1 # histedit.py - interactive history editing for mercurial
2 #
2 #
3 # Copyright 2009 Augie Fackler <raf@durin42.com>
3 # Copyright 2009 Augie Fackler <raf@durin42.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7 """interactive history editing
7 """interactive history editing
8
8
9 With this extension installed, Mercurial gains one new command: histedit. Usage
9 With this extension installed, Mercurial gains one new command: histedit. Usage
10 is as follows, assuming the following history::
10 is as follows, assuming the following history::
11
11
12 @ 3[tip] 7c2fd3b9020c 2009-04-27 18:04 -0500 durin42
12 @ 3[tip] 7c2fd3b9020c 2009-04-27 18:04 -0500 durin42
13 | Add delta
13 | Add delta
14 |
14 |
15 o 2 030b686bedc4 2009-04-27 18:04 -0500 durin42
15 o 2 030b686bedc4 2009-04-27 18:04 -0500 durin42
16 | Add gamma
16 | Add gamma
17 |
17 |
18 o 1 c561b4e977df 2009-04-27 18:04 -0500 durin42
18 o 1 c561b4e977df 2009-04-27 18:04 -0500 durin42
19 | Add beta
19 | Add beta
20 |
20 |
21 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
21 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
22 Add alpha
22 Add alpha
23
23
24 If you were to run ``hg histedit c561b4e977df``, you would see the following
24 If you were to run ``hg histedit c561b4e977df``, you would see the following
25 file open in your editor::
25 file open in your editor::
26
26
27 pick c561b4e977df Add beta
27 pick c561b4e977df Add beta
28 pick 030b686bedc4 Add gamma
28 pick 030b686bedc4 Add gamma
29 pick 7c2fd3b9020c Add delta
29 pick 7c2fd3b9020c Add delta
30
30
31 # Edit history between c561b4e977df and 7c2fd3b9020c
31 # Edit history between c561b4e977df and 7c2fd3b9020c
32 #
32 #
33 # Commits are listed from least to most recent
33 # Commits are listed from least to most recent
34 #
34 #
35 # Commands:
35 # Commands:
36 # p, pick = use commit
36 # p, pick = use commit
37 # e, edit = use commit, but stop for amending
37 # e, edit = use commit, but stop for amending
38 # f, fold = use commit, but combine it with the one above
38 # f, fold = use commit, but combine it with the one above
39 # r, roll = like fold, but discard this commit's description
39 # r, roll = like fold, but discard this commit's description
40 # d, drop = remove commit from history
40 # d, drop = remove commit from history
41 # m, mess = edit message without changing commit content
41 # m, mess = edit message without changing commit content
42 #
42 #
43
43
44 In this file, lines beginning with ``#`` are ignored. You must specify a rule
44 In this file, lines beginning with ``#`` are ignored. You must specify a rule
45 for each revision in your history. For example, if you had meant to add gamma
45 for each revision in your history. For example, if you had meant to add gamma
46 before beta, and then wanted to add delta in the same revision as beta, you
46 before beta, and then wanted to add delta in the same revision as beta, you
47 would reorganize the file to look like this::
47 would reorganize the file to look like this::
48
48
49 pick 030b686bedc4 Add gamma
49 pick 030b686bedc4 Add gamma
50 pick c561b4e977df Add beta
50 pick c561b4e977df Add beta
51 fold 7c2fd3b9020c Add delta
51 fold 7c2fd3b9020c Add delta
52
52
53 # Edit history between c561b4e977df and 7c2fd3b9020c
53 # Edit history between c561b4e977df and 7c2fd3b9020c
54 #
54 #
55 # Commits are listed from least to most recent
55 # Commits are listed from least to most recent
56 #
56 #
57 # Commands:
57 # Commands:
58 # p, pick = use commit
58 # p, pick = use commit
59 # e, edit = use commit, but stop for amending
59 # e, edit = use commit, but stop for amending
60 # f, fold = use commit, but combine it with the one above
60 # f, fold = use commit, but combine it with the one above
61 # r, roll = like fold, but discard this commit's description
61 # r, roll = like fold, but discard this commit's description
62 # d, drop = remove commit from history
62 # d, drop = remove commit from history
63 # m, mess = edit message without changing commit content
63 # m, mess = edit message without changing commit content
64 #
64 #
65
65
66 At which point you close the editor and ``histedit`` starts working. When you
66 At which point you close the editor and ``histedit`` starts working. When you
67 specify a ``fold`` operation, ``histedit`` will open an editor when it folds
67 specify a ``fold`` operation, ``histedit`` will open an editor when it folds
68 those revisions together, offering you a chance to clean up the commit message::
68 those revisions together, offering you a chance to clean up the commit message::
69
69
70 Add beta
70 Add beta
71 ***
71 ***
72 Add delta
72 Add delta
73
73
74 Edit the commit message to your liking, then close the editor. For
74 Edit the commit message to your liking, then close the editor. For
75 this example, let's assume that the commit message was changed to
75 this example, let's assume that the commit message was changed to
76 ``Add beta and delta.`` After histedit has run and had a chance to
76 ``Add beta and delta.`` After histedit has run and had a chance to
77 remove any old or temporary revisions it needed, the history looks
77 remove any old or temporary revisions it needed, the history looks
78 like this::
78 like this::
79
79
80 @ 2[tip] 989b4d060121 2009-04-27 18:04 -0500 durin42
80 @ 2[tip] 989b4d060121 2009-04-27 18:04 -0500 durin42
81 | Add beta and delta.
81 | Add beta and delta.
82 |
82 |
83 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
83 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
84 | Add gamma
84 | Add gamma
85 |
85 |
86 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
86 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
87 Add alpha
87 Add alpha
88
88
89 Note that ``histedit`` does *not* remove any revisions (even its own temporary
89 Note that ``histedit`` does *not* remove any revisions (even its own temporary
90 ones) until after it has completed all the editing operations, so it will
90 ones) until after it has completed all the editing operations, so it will
91 probably perform several strip operations when it's done. For the above example,
91 probably perform several strip operations when it's done. For the above example,
92 it had to run strip twice. Strip can be slow depending on a variety of factors,
92 it had to run strip twice. Strip can be slow depending on a variety of factors,
93 so you might need to be a little patient. You can choose to keep the original
93 so you might need to be a little patient. You can choose to keep the original
94 revisions by passing the ``--keep`` flag.
94 revisions by passing the ``--keep`` flag.
95
95
96 The ``edit`` operation will drop you back to a command prompt,
96 The ``edit`` operation will drop you back to a command prompt,
97 allowing you to edit files freely, or even use ``hg record`` to commit
97 allowing you to edit files freely, or even use ``hg record`` to commit
98 some changes as a separate commit. When you're done, any remaining
98 some changes as a separate commit. When you're done, any remaining
99 uncommitted changes will be committed as well. When done, run ``hg
99 uncommitted changes will be committed as well. When done, run ``hg
100 histedit --continue`` to finish this step. You'll be prompted for a
100 histedit --continue`` to finish this step. You'll be prompted for a
101 new commit message, but the default commit message will be the
101 new commit message, but the default commit message will be the
102 original message for the ``edit`` ed revision.
102 original message for the ``edit`` ed revision.
103
103
104 The ``message`` operation will give you a chance to revise a commit
104 The ``message`` operation will give you a chance to revise a commit
105 message without changing the contents. It's a shortcut for doing
105 message without changing the contents. It's a shortcut for doing
106 ``edit`` immediately followed by `hg histedit --continue``.
106 ``edit`` immediately followed by `hg histedit --continue``.
107
107
108 If ``histedit`` encounters a conflict when moving a revision (while
108 If ``histedit`` encounters a conflict when moving a revision (while
109 handling ``pick`` or ``fold``), it'll stop in a similar manner to
109 handling ``pick`` or ``fold``), it'll stop in a similar manner to
110 ``edit`` with the difference that it won't prompt you for a commit
110 ``edit`` with the difference that it won't prompt you for a commit
111 message when done. If you decide at this point that you don't like how
111 message when done. If you decide at this point that you don't like how
112 much work it will be to rearrange history, or that you made a mistake,
112 much work it will be to rearrange history, or that you made a mistake,
113 you can use ``hg histedit --abort`` to abandon the new changes you
113 you can use ``hg histedit --abort`` to abandon the new changes you
114 have made and return to the state before you attempted to edit your
114 have made and return to the state before you attempted to edit your
115 history.
115 history.
116
116
117 If we clone the histedit-ed example repository above and add four more
117 If we clone the histedit-ed example repository above and add four more
118 changes, such that we have the following history::
118 changes, such that we have the following history::
119
119
120 @ 6[tip] 038383181893 2009-04-27 18:04 -0500 stefan
120 @ 6[tip] 038383181893 2009-04-27 18:04 -0500 stefan
121 | Add theta
121 | Add theta
122 |
122 |
123 o 5 140988835471 2009-04-27 18:04 -0500 stefan
123 o 5 140988835471 2009-04-27 18:04 -0500 stefan
124 | Add eta
124 | Add eta
125 |
125 |
126 o 4 122930637314 2009-04-27 18:04 -0500 stefan
126 o 4 122930637314 2009-04-27 18:04 -0500 stefan
127 | Add zeta
127 | Add zeta
128 |
128 |
129 o 3 836302820282 2009-04-27 18:04 -0500 stefan
129 o 3 836302820282 2009-04-27 18:04 -0500 stefan
130 | Add epsilon
130 | Add epsilon
131 |
131 |
132 o 2 989b4d060121 2009-04-27 18:04 -0500 durin42
132 o 2 989b4d060121 2009-04-27 18:04 -0500 durin42
133 | Add beta and delta.
133 | Add beta and delta.
134 |
134 |
135 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
135 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
136 | Add gamma
136 | Add gamma
137 |
137 |
138 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
138 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
139 Add alpha
139 Add alpha
140
140
141 If you run ``hg histedit --outgoing`` on the clone then it is the same
141 If you run ``hg histedit --outgoing`` on the clone then it is the same
142 as running ``hg histedit 836302820282``. If you need plan to push to a
142 as running ``hg histedit 836302820282``. If you need plan to push to a
143 repository that Mercurial does not detect to be related to the source
143 repository that Mercurial does not detect to be related to the source
144 repo, you can add a ``--force`` option.
144 repo, you can add a ``--force`` option.
145 """
145 """
146
146
147 try:
147 try:
148 import cPickle as pickle
148 import cPickle as pickle
149 pickle.dump # import now
149 pickle.dump # import now
150 except ImportError:
150 except ImportError:
151 import pickle
151 import pickle
152 import errno
152 import errno
153 import os
153 import os
154 import sys
154 import sys
155
155
156 from mercurial import cmdutil
156 from mercurial import cmdutil
157 from mercurial import discovery
157 from mercurial import discovery
158 from mercurial import error
158 from mercurial import error
159 from mercurial import copies
159 from mercurial import copies
160 from mercurial import context
160 from mercurial import context
161 from mercurial import hg
161 from mercurial import hg
162 from mercurial import node
162 from mercurial import node
163 from mercurial import repair
163 from mercurial import repair
164 from mercurial import scmutil
164 from mercurial import scmutil
165 from mercurial import util
165 from mercurial import util
166 from mercurial import obsolete
166 from mercurial import obsolete
167 from mercurial import merge as mergemod
167 from mercurial import merge as mergemod
168 from mercurial.lock import release
168 from mercurial.lock import release
169 from mercurial.i18n import _
169 from mercurial.i18n import _
170
170
171 cmdtable = {}
171 cmdtable = {}
172 command = cmdutil.command(cmdtable)
172 command = cmdutil.command(cmdtable)
173
173
174 testedwith = 'internal'
174 testedwith = 'internal'
175
175
176 # i18n: command names and abbreviations must remain untranslated
176 # i18n: command names and abbreviations must remain untranslated
177 editcomment = _("""# Edit history between %s and %s
177 editcomment = _("""# Edit history between %s and %s
178 #
178 #
179 # Commits are listed from least to most recent
179 # Commits are listed from least to most recent
180 #
180 #
181 # Commands:
181 # Commands:
182 # p, pick = use commit
182 # p, pick = use commit
183 # e, edit = use commit, but stop for amending
183 # e, edit = use commit, but stop for amending
184 # f, fold = use commit, but combine it with the one above
184 # f, fold = use commit, but combine it with the one above
185 # r, roll = like fold, but discard this commit's description
185 # r, roll = like fold, but discard this commit's description
186 # d, drop = remove commit from history
186 # d, drop = remove commit from history
187 # m, mess = edit message without changing commit content
187 # m, mess = edit message without changing commit content
188 #
188 #
189 """)
189 """)
190
190
191 class histeditstate(object):
191 class histeditstate(object):
192 def __init__(self, repo, parentctxnode=None, rules=None, keep=None,
192 def __init__(self, repo, parentctx=None, rules=None, keep=None,
193 topmost=None, replacements=None):
193 topmost=None, replacements=None):
194 self.repo = repo
194 self.repo = repo
195 self.parentctxnode = parentctxnode
196 self.rules = rules
195 self.rules = rules
197 self.keep = keep
196 self.keep = keep
198 self.topmost = topmost
197 self.topmost = topmost
198 self.parentctx = parentctx
199 if replacements is None:
199 if replacements is None:
200 self.replacements = []
200 self.replacements = []
201 else:
201 else:
202 self.replacements = replacements
202 self.replacements = replacements
203
203
204 def write(self):
204 def write(self):
205 fp = self.repo.vfs('histedit-state', 'w')
205 fp = self.repo.vfs('histedit-state', 'w')
206 pickle.dump((self.parentctxnode, self.rules, self.keep,
206 pickle.dump((self.parentctx.node(), self.rules, self.keep,
207 self.topmost, self.replacements), fp)
207 self.topmost, self.replacements), fp)
208 fp.close()
208 fp.close()
209
209
210 def clear(self):
210 def clear(self):
211 self.repo.vfs.unlink('histedit-state')
211 self.repo.vfs.unlink('histedit-state')
212
212
213 def commitfuncfor(repo, src):
213 def commitfuncfor(repo, src):
214 """Build a commit function for the replacement of <src>
214 """Build a commit function for the replacement of <src>
215
215
216 This function ensure we apply the same treatment to all changesets.
216 This function ensure we apply the same treatment to all changesets.
217
217
218 - Add a 'histedit_source' entry in extra.
218 - Add a 'histedit_source' entry in extra.
219
219
220 Note that fold have its own separated logic because its handling is a bit
220 Note that fold have its own separated logic because its handling is a bit
221 different and not easily factored out of the fold method.
221 different and not easily factored out of the fold method.
222 """
222 """
223 phasemin = src.phase()
223 phasemin = src.phase()
224 def commitfunc(**kwargs):
224 def commitfunc(**kwargs):
225 phasebackup = repo.ui.backupconfig('phases', 'new-commit')
225 phasebackup = repo.ui.backupconfig('phases', 'new-commit')
226 try:
226 try:
227 repo.ui.setconfig('phases', 'new-commit', phasemin,
227 repo.ui.setconfig('phases', 'new-commit', phasemin,
228 'histedit')
228 'histedit')
229 extra = kwargs.get('extra', {}).copy()
229 extra = kwargs.get('extra', {}).copy()
230 extra['histedit_source'] = src.hex()
230 extra['histedit_source'] = src.hex()
231 kwargs['extra'] = extra
231 kwargs['extra'] = extra
232 return repo.commit(**kwargs)
232 return repo.commit(**kwargs)
233 finally:
233 finally:
234 repo.ui.restoreconfig(phasebackup)
234 repo.ui.restoreconfig(phasebackup)
235 return commitfunc
235 return commitfunc
236
236
237 def applychanges(ui, repo, ctx, opts):
237 def applychanges(ui, repo, ctx, opts):
238 """Merge changeset from ctx (only) in the current working directory"""
238 """Merge changeset from ctx (only) in the current working directory"""
239 wcpar = repo.dirstate.parents()[0]
239 wcpar = repo.dirstate.parents()[0]
240 if ctx.p1().node() == wcpar:
240 if ctx.p1().node() == wcpar:
241 # edition ar "in place" we do not need to make any merge,
241 # edition ar "in place" we do not need to make any merge,
242 # just applies changes on parent for edition
242 # just applies changes on parent for edition
243 cmdutil.revert(ui, repo, ctx, (wcpar, node.nullid), all=True)
243 cmdutil.revert(ui, repo, ctx, (wcpar, node.nullid), all=True)
244 stats = None
244 stats = None
245 else:
245 else:
246 try:
246 try:
247 # ui.forcemerge is an internal variable, do not document
247 # ui.forcemerge is an internal variable, do not document
248 repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
248 repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
249 'histedit')
249 'histedit')
250 stats = mergemod.graft(repo, ctx, ctx.p1(), ['local', 'histedit'])
250 stats = mergemod.graft(repo, ctx, ctx.p1(), ['local', 'histedit'])
251 finally:
251 finally:
252 repo.ui.setconfig('ui', 'forcemerge', '', 'histedit')
252 repo.ui.setconfig('ui', 'forcemerge', '', 'histedit')
253 return stats
253 return stats
254
254
255 def collapse(repo, first, last, commitopts):
255 def collapse(repo, first, last, commitopts):
256 """collapse the set of revisions from first to last as new one.
256 """collapse the set of revisions from first to last as new one.
257
257
258 Expected commit options are:
258 Expected commit options are:
259 - message
259 - message
260 - date
260 - date
261 - username
261 - username
262 Commit message is edited in all cases.
262 Commit message is edited in all cases.
263
263
264 This function works in memory."""
264 This function works in memory."""
265 ctxs = list(repo.set('%d::%d', first, last))
265 ctxs = list(repo.set('%d::%d', first, last))
266 if not ctxs:
266 if not ctxs:
267 return None
267 return None
268 base = first.parents()[0]
268 base = first.parents()[0]
269
269
270 # commit a new version of the old changeset, including the update
270 # commit a new version of the old changeset, including the update
271 # collect all files which might be affected
271 # collect all files which might be affected
272 files = set()
272 files = set()
273 for ctx in ctxs:
273 for ctx in ctxs:
274 files.update(ctx.files())
274 files.update(ctx.files())
275
275
276 # Recompute copies (avoid recording a -> b -> a)
276 # Recompute copies (avoid recording a -> b -> a)
277 copied = copies.pathcopies(base, last)
277 copied = copies.pathcopies(base, last)
278
278
279 # prune files which were reverted by the updates
279 # prune files which were reverted by the updates
280 def samefile(f):
280 def samefile(f):
281 if f in last.manifest():
281 if f in last.manifest():
282 a = last.filectx(f)
282 a = last.filectx(f)
283 if f in base.manifest():
283 if f in base.manifest():
284 b = base.filectx(f)
284 b = base.filectx(f)
285 return (a.data() == b.data()
285 return (a.data() == b.data()
286 and a.flags() == b.flags())
286 and a.flags() == b.flags())
287 else:
287 else:
288 return False
288 return False
289 else:
289 else:
290 return f not in base.manifest()
290 return f not in base.manifest()
291 files = [f for f in files if not samefile(f)]
291 files = [f for f in files if not samefile(f)]
292 # commit version of these files as defined by head
292 # commit version of these files as defined by head
293 headmf = last.manifest()
293 headmf = last.manifest()
294 def filectxfn(repo, ctx, path):
294 def filectxfn(repo, ctx, path):
295 if path in headmf:
295 if path in headmf:
296 fctx = last[path]
296 fctx = last[path]
297 flags = fctx.flags()
297 flags = fctx.flags()
298 mctx = context.memfilectx(repo,
298 mctx = context.memfilectx(repo,
299 fctx.path(), fctx.data(),
299 fctx.path(), fctx.data(),
300 islink='l' in flags,
300 islink='l' in flags,
301 isexec='x' in flags,
301 isexec='x' in flags,
302 copied=copied.get(path))
302 copied=copied.get(path))
303 return mctx
303 return mctx
304 return None
304 return None
305
305
306 if commitopts.get('message'):
306 if commitopts.get('message'):
307 message = commitopts['message']
307 message = commitopts['message']
308 else:
308 else:
309 message = first.description()
309 message = first.description()
310 user = commitopts.get('user')
310 user = commitopts.get('user')
311 date = commitopts.get('date')
311 date = commitopts.get('date')
312 extra = commitopts.get('extra')
312 extra = commitopts.get('extra')
313
313
314 parents = (first.p1().node(), first.p2().node())
314 parents = (first.p1().node(), first.p2().node())
315 editor = None
315 editor = None
316 if not commitopts.get('rollup'):
316 if not commitopts.get('rollup'):
317 editor = cmdutil.getcommiteditor(edit=True, editform='histedit.fold')
317 editor = cmdutil.getcommiteditor(edit=True, editform='histedit.fold')
318 new = context.memctx(repo,
318 new = context.memctx(repo,
319 parents=parents,
319 parents=parents,
320 text=message,
320 text=message,
321 files=files,
321 files=files,
322 filectxfn=filectxfn,
322 filectxfn=filectxfn,
323 user=user,
323 user=user,
324 date=date,
324 date=date,
325 extra=extra,
325 extra=extra,
326 editor=editor)
326 editor=editor)
327 return repo.commitctx(new)
327 return repo.commitctx(new)
328
328
329 def pick(ui, repo, ctx, ha, opts):
329 def pick(ui, repo, ctx, ha, opts):
330 oldctx = repo[ha]
330 oldctx = repo[ha]
331 if oldctx.parents()[0] == ctx:
331 if oldctx.parents()[0] == ctx:
332 ui.debug('node %s unchanged\n' % ha)
332 ui.debug('node %s unchanged\n' % ha)
333 return oldctx, []
333 return oldctx, []
334 hg.update(repo, ctx.node())
334 hg.update(repo, ctx.node())
335 stats = applychanges(ui, repo, oldctx, opts)
335 stats = applychanges(ui, repo, oldctx, opts)
336 if stats and stats[3] > 0:
336 if stats and stats[3] > 0:
337 raise error.InterventionRequired(_('Fix up the change and run '
337 raise error.InterventionRequired(_('Fix up the change and run '
338 'hg histedit --continue'))
338 'hg histedit --continue'))
339 # drop the second merge parent
339 # drop the second merge parent
340 commit = commitfuncfor(repo, oldctx)
340 commit = commitfuncfor(repo, oldctx)
341 n = commit(text=oldctx.description(), user=oldctx.user(),
341 n = commit(text=oldctx.description(), user=oldctx.user(),
342 date=oldctx.date(), extra=oldctx.extra())
342 date=oldctx.date(), extra=oldctx.extra())
343 if n is None:
343 if n is None:
344 ui.warn(_('%s: empty changeset\n')
344 ui.warn(_('%s: empty changeset\n')
345 % node.hex(ha))
345 % node.hex(ha))
346 return ctx, []
346 return ctx, []
347 new = repo[n]
347 new = repo[n]
348 return new, [(oldctx.node(), (n,))]
348 return new, [(oldctx.node(), (n,))]
349
349
350
350
351 def edit(ui, repo, ctx, ha, opts):
351 def edit(ui, repo, ctx, ha, opts):
352 oldctx = repo[ha]
352 oldctx = repo[ha]
353 hg.update(repo, ctx.node())
353 hg.update(repo, ctx.node())
354 applychanges(ui, repo, oldctx, opts)
354 applychanges(ui, repo, oldctx, opts)
355 raise error.InterventionRequired(
355 raise error.InterventionRequired(
356 _('Make changes as needed, you may commit or record as needed now.\n'
356 _('Make changes as needed, you may commit or record as needed now.\n'
357 'When you are finished, run hg histedit --continue to resume.'))
357 'When you are finished, run hg histedit --continue to resume.'))
358
358
359 def rollup(ui, repo, ctx, ha, opts):
359 def rollup(ui, repo, ctx, ha, opts):
360 rollupopts = opts.copy()
360 rollupopts = opts.copy()
361 rollupopts['rollup'] = True
361 rollupopts['rollup'] = True
362 return fold(ui, repo, ctx, ha, rollupopts)
362 return fold(ui, repo, ctx, ha, rollupopts)
363
363
364 def fold(ui, repo, ctx, ha, opts):
364 def fold(ui, repo, ctx, ha, opts):
365 oldctx = repo[ha]
365 oldctx = repo[ha]
366 hg.update(repo, ctx.node())
366 hg.update(repo, ctx.node())
367 stats = applychanges(ui, repo, oldctx, opts)
367 stats = applychanges(ui, repo, oldctx, opts)
368 if stats and stats[3] > 0:
368 if stats and stats[3] > 0:
369 raise error.InterventionRequired(
369 raise error.InterventionRequired(
370 _('Fix up the change and run hg histedit --continue'))
370 _('Fix up the change and run hg histedit --continue'))
371 n = repo.commit(text='fold-temp-revision %s' % ha, user=oldctx.user(),
371 n = repo.commit(text='fold-temp-revision %s' % ha, user=oldctx.user(),
372 date=oldctx.date(), extra=oldctx.extra())
372 date=oldctx.date(), extra=oldctx.extra())
373 if n is None:
373 if n is None:
374 ui.warn(_('%s: empty changeset')
374 ui.warn(_('%s: empty changeset')
375 % node.hex(ha))
375 % node.hex(ha))
376 return ctx, []
376 return ctx, []
377 return finishfold(ui, repo, ctx, oldctx, n, opts, [])
377 return finishfold(ui, repo, ctx, oldctx, n, opts, [])
378
378
379 def finishfold(ui, repo, ctx, oldctx, newnode, opts, internalchanges):
379 def finishfold(ui, repo, ctx, oldctx, newnode, opts, internalchanges):
380 parent = ctx.parents()[0].node()
380 parent = ctx.parents()[0].node()
381 hg.update(repo, parent)
381 hg.update(repo, parent)
382 ### prepare new commit data
382 ### prepare new commit data
383 commitopts = opts.copy()
383 commitopts = opts.copy()
384 commitopts['user'] = ctx.user()
384 commitopts['user'] = ctx.user()
385 # commit message
385 # commit message
386 if opts.get('rollup'):
386 if opts.get('rollup'):
387 newmessage = ctx.description()
387 newmessage = ctx.description()
388 else:
388 else:
389 newmessage = '\n***\n'.join(
389 newmessage = '\n***\n'.join(
390 [ctx.description()] +
390 [ctx.description()] +
391 [repo[r].description() for r in internalchanges] +
391 [repo[r].description() for r in internalchanges] +
392 [oldctx.description()]) + '\n'
392 [oldctx.description()]) + '\n'
393 commitopts['message'] = newmessage
393 commitopts['message'] = newmessage
394 # date
394 # date
395 commitopts['date'] = max(ctx.date(), oldctx.date())
395 commitopts['date'] = max(ctx.date(), oldctx.date())
396 extra = ctx.extra().copy()
396 extra = ctx.extra().copy()
397 # histedit_source
397 # histedit_source
398 # note: ctx is likely a temporary commit but that the best we can do here
398 # note: ctx is likely a temporary commit but that the best we can do here
399 # This is sufficient to solve issue3681 anyway
399 # This is sufficient to solve issue3681 anyway
400 extra['histedit_source'] = '%s,%s' % (ctx.hex(), oldctx.hex())
400 extra['histedit_source'] = '%s,%s' % (ctx.hex(), oldctx.hex())
401 commitopts['extra'] = extra
401 commitopts['extra'] = extra
402 phasebackup = repo.ui.backupconfig('phases', 'new-commit')
402 phasebackup = repo.ui.backupconfig('phases', 'new-commit')
403 try:
403 try:
404 phasemin = max(ctx.phase(), oldctx.phase())
404 phasemin = max(ctx.phase(), oldctx.phase())
405 repo.ui.setconfig('phases', 'new-commit', phasemin, 'histedit')
405 repo.ui.setconfig('phases', 'new-commit', phasemin, 'histedit')
406 n = collapse(repo, ctx, repo[newnode], commitopts)
406 n = collapse(repo, ctx, repo[newnode], commitopts)
407 finally:
407 finally:
408 repo.ui.restoreconfig(phasebackup)
408 repo.ui.restoreconfig(phasebackup)
409 if n is None:
409 if n is None:
410 return ctx, []
410 return ctx, []
411 hg.update(repo, n)
411 hg.update(repo, n)
412 replacements = [(oldctx.node(), (newnode,)),
412 replacements = [(oldctx.node(), (newnode,)),
413 (ctx.node(), (n,)),
413 (ctx.node(), (n,)),
414 (newnode, (n,)),
414 (newnode, (n,)),
415 ]
415 ]
416 for ich in internalchanges:
416 for ich in internalchanges:
417 replacements.append((ich, (n,)))
417 replacements.append((ich, (n,)))
418 return repo[n], replacements
418 return repo[n], replacements
419
419
420 def drop(ui, repo, ctx, ha, opts):
420 def drop(ui, repo, ctx, ha, opts):
421 return ctx, [(repo[ha].node(), ())]
421 return ctx, [(repo[ha].node(), ())]
422
422
423
423
424 def message(ui, repo, ctx, ha, opts):
424 def message(ui, repo, ctx, ha, opts):
425 oldctx = repo[ha]
425 oldctx = repo[ha]
426 hg.update(repo, ctx.node())
426 hg.update(repo, ctx.node())
427 stats = applychanges(ui, repo, oldctx, opts)
427 stats = applychanges(ui, repo, oldctx, opts)
428 if stats and stats[3] > 0:
428 if stats and stats[3] > 0:
429 raise error.InterventionRequired(
429 raise error.InterventionRequired(
430 _('Fix up the change and run hg histedit --continue'))
430 _('Fix up the change and run hg histedit --continue'))
431 message = oldctx.description()
431 message = oldctx.description()
432 commit = commitfuncfor(repo, oldctx)
432 commit = commitfuncfor(repo, oldctx)
433 editor = cmdutil.getcommiteditor(edit=True, editform='histedit.mess')
433 editor = cmdutil.getcommiteditor(edit=True, editform='histedit.mess')
434 new = commit(text=message, user=oldctx.user(), date=oldctx.date(),
434 new = commit(text=message, user=oldctx.user(), date=oldctx.date(),
435 extra=oldctx.extra(),
435 extra=oldctx.extra(),
436 editor=editor)
436 editor=editor)
437 newctx = repo[new]
437 newctx = repo[new]
438 if oldctx.node() != newctx.node():
438 if oldctx.node() != newctx.node():
439 return newctx, [(oldctx.node(), (new,))]
439 return newctx, [(oldctx.node(), (new,))]
440 # We didn't make an edit, so just indicate no replaced nodes
440 # We didn't make an edit, so just indicate no replaced nodes
441 return newctx, []
441 return newctx, []
442
442
443 def findoutgoing(ui, repo, remote=None, force=False, opts={}):
443 def findoutgoing(ui, repo, remote=None, force=False, opts={}):
444 """utility function to find the first outgoing changeset
444 """utility function to find the first outgoing changeset
445
445
446 Used by initialisation code"""
446 Used by initialisation code"""
447 dest = ui.expandpath(remote or 'default-push', remote or 'default')
447 dest = ui.expandpath(remote or 'default-push', remote or 'default')
448 dest, revs = hg.parseurl(dest, None)[:2]
448 dest, revs = hg.parseurl(dest, None)[:2]
449 ui.status(_('comparing with %s\n') % util.hidepassword(dest))
449 ui.status(_('comparing with %s\n') % util.hidepassword(dest))
450
450
451 revs, checkout = hg.addbranchrevs(repo, repo, revs, None)
451 revs, checkout = hg.addbranchrevs(repo, repo, revs, None)
452 other = hg.peer(repo, opts, dest)
452 other = hg.peer(repo, opts, dest)
453
453
454 if revs:
454 if revs:
455 revs = [repo.lookup(rev) for rev in revs]
455 revs = [repo.lookup(rev) for rev in revs]
456
456
457 outgoing = discovery.findcommonoutgoing(repo, other, revs, force=force)
457 outgoing = discovery.findcommonoutgoing(repo, other, revs, force=force)
458 if not outgoing.missing:
458 if not outgoing.missing:
459 raise util.Abort(_('no outgoing ancestors'))
459 raise util.Abort(_('no outgoing ancestors'))
460 roots = list(repo.revs("roots(%ln)", outgoing.missing))
460 roots = list(repo.revs("roots(%ln)", outgoing.missing))
461 if 1 < len(roots):
461 if 1 < len(roots):
462 msg = _('there are ambiguous outgoing revisions')
462 msg = _('there are ambiguous outgoing revisions')
463 hint = _('see "hg help histedit" for more detail')
463 hint = _('see "hg help histedit" for more detail')
464 raise util.Abort(msg, hint=hint)
464 raise util.Abort(msg, hint=hint)
465 return repo.lookup(roots[0])
465 return repo.lookup(roots[0])
466
466
467 actiontable = {'p': pick,
467 actiontable = {'p': pick,
468 'pick': pick,
468 'pick': pick,
469 'e': edit,
469 'e': edit,
470 'edit': edit,
470 'edit': edit,
471 'f': fold,
471 'f': fold,
472 'fold': fold,
472 'fold': fold,
473 'r': rollup,
473 'r': rollup,
474 'roll': rollup,
474 'roll': rollup,
475 'd': drop,
475 'd': drop,
476 'drop': drop,
476 'drop': drop,
477 'm': message,
477 'm': message,
478 'mess': message,
478 'mess': message,
479 }
479 }
480
480
481 @command('histedit',
481 @command('histedit',
482 [('', 'commands', '',
482 [('', 'commands', '',
483 _('Read history edits from the specified file.')),
483 _('Read history edits from the specified file.')),
484 ('c', 'continue', False, _('continue an edit already in progress')),
484 ('c', 'continue', False, _('continue an edit already in progress')),
485 ('k', 'keep', False,
485 ('k', 'keep', False,
486 _("don't strip old nodes after edit is complete")),
486 _("don't strip old nodes after edit is complete")),
487 ('', 'abort', False, _('abort an edit in progress')),
487 ('', 'abort', False, _('abort an edit in progress')),
488 ('o', 'outgoing', False, _('changesets not found in destination')),
488 ('o', 'outgoing', False, _('changesets not found in destination')),
489 ('f', 'force', False,
489 ('f', 'force', False,
490 _('force outgoing even for unrelated repositories')),
490 _('force outgoing even for unrelated repositories')),
491 ('r', 'rev', [], _('first revision to be edited'))],
491 ('r', 'rev', [], _('first revision to be edited'))],
492 _("ANCESTOR | --outgoing [URL]"))
492 _("ANCESTOR | --outgoing [URL]"))
493 def histedit(ui, repo, *freeargs, **opts):
493 def histedit(ui, repo, *freeargs, **opts):
494 """interactively edit changeset history
494 """interactively edit changeset history
495
495
496 This command edits changesets between ANCESTOR and the parent of
496 This command edits changesets between ANCESTOR and the parent of
497 the working directory.
497 the working directory.
498
498
499 With --outgoing, this edits changesets not found in the
499 With --outgoing, this edits changesets not found in the
500 destination repository. If URL of the destination is omitted, the
500 destination repository. If URL of the destination is omitted, the
501 'default-push' (or 'default') path will be used.
501 'default-push' (or 'default') path will be used.
502
502
503 For safety, this command is aborted, also if there are ambiguous
503 For safety, this command is aborted, also if there are ambiguous
504 outgoing revisions which may confuse users: for example, there are
504 outgoing revisions which may confuse users: for example, there are
505 multiple branches containing outgoing revisions.
505 multiple branches containing outgoing revisions.
506
506
507 Use "min(outgoing() and ::.)" or similar revset specification
507 Use "min(outgoing() and ::.)" or similar revset specification
508 instead of --outgoing to specify edit target revision exactly in
508 instead of --outgoing to specify edit target revision exactly in
509 such ambiguous situation. See :hg:`help revsets` for detail about
509 such ambiguous situation. See :hg:`help revsets` for detail about
510 selecting revisions.
510 selecting revisions.
511
511
512 Returns 0 on success, 1 if user intervention is required (not only
512 Returns 0 on success, 1 if user intervention is required (not only
513 for intentional "edit" command, but also for resolving unexpected
513 for intentional "edit" command, but also for resolving unexpected
514 conflicts).
514 conflicts).
515 """
515 """
516 lock = wlock = None
516 lock = wlock = None
517 try:
517 try:
518 wlock = repo.wlock()
518 wlock = repo.wlock()
519 lock = repo.lock()
519 lock = repo.lock()
520 _histedit(ui, repo, *freeargs, **opts)
520 _histedit(ui, repo, *freeargs, **opts)
521 finally:
521 finally:
522 release(lock, wlock)
522 release(lock, wlock)
523
523
524 def _histedit(ui, repo, *freeargs, **opts):
524 def _histedit(ui, repo, *freeargs, **opts):
525 # TODO only abort if we try and histedit mq patches, not just
525 # TODO only abort if we try and histedit mq patches, not just
526 # blanket if mq patches are applied somewhere
526 # blanket if mq patches are applied somewhere
527 mq = getattr(repo, 'mq', None)
527 mq = getattr(repo, 'mq', None)
528 if mq and mq.applied:
528 if mq and mq.applied:
529 raise util.Abort(_('source has mq patches applied'))
529 raise util.Abort(_('source has mq patches applied'))
530
530
531 # basic argument incompatibility processing
531 # basic argument incompatibility processing
532 outg = opts.get('outgoing')
532 outg = opts.get('outgoing')
533 cont = opts.get('continue')
533 cont = opts.get('continue')
534 abort = opts.get('abort')
534 abort = opts.get('abort')
535 force = opts.get('force')
535 force = opts.get('force')
536 rules = opts.get('commands', '')
536 rules = opts.get('commands', '')
537 revs = opts.get('rev', [])
537 revs = opts.get('rev', [])
538 goal = 'new' # This invocation goal, in new, continue, abort
538 goal = 'new' # This invocation goal, in new, continue, abort
539 if force and not outg:
539 if force and not outg:
540 raise util.Abort(_('--force only allowed with --outgoing'))
540 raise util.Abort(_('--force only allowed with --outgoing'))
541 if cont:
541 if cont:
542 if util.any((outg, abort, revs, freeargs, rules)):
542 if util.any((outg, abort, revs, freeargs, rules)):
543 raise util.Abort(_('no arguments allowed with --continue'))
543 raise util.Abort(_('no arguments allowed with --continue'))
544 goal = 'continue'
544 goal = 'continue'
545 elif abort:
545 elif abort:
546 if util.any((outg, revs, freeargs, rules)):
546 if util.any((outg, revs, freeargs, rules)):
547 raise util.Abort(_('no arguments allowed with --abort'))
547 raise util.Abort(_('no arguments allowed with --abort'))
548 goal = 'abort'
548 goal = 'abort'
549 else:
549 else:
550 if os.path.exists(os.path.join(repo.path, 'histedit-state')):
550 if os.path.exists(os.path.join(repo.path, 'histedit-state')):
551 raise util.Abort(_('history edit already in progress, try '
551 raise util.Abort(_('history edit already in progress, try '
552 '--continue or --abort'))
552 '--continue or --abort'))
553 if outg:
553 if outg:
554 if revs:
554 if revs:
555 raise util.Abort(_('no revisions allowed with --outgoing'))
555 raise util.Abort(_('no revisions allowed with --outgoing'))
556 if len(freeargs) > 1:
556 if len(freeargs) > 1:
557 raise util.Abort(
557 raise util.Abort(
558 _('only one repo argument allowed with --outgoing'))
558 _('only one repo argument allowed with --outgoing'))
559 else:
559 else:
560 revs.extend(freeargs)
560 revs.extend(freeargs)
561 if len(revs) != 1:
561 if len(revs) != 1:
562 raise util.Abort(
562 raise util.Abort(
563 _('histedit requires exactly one ancestor revision'))
563 _('histedit requires exactly one ancestor revision'))
564
564
565
565
566 replacements = []
566 replacements = []
567 keep = opts.get('keep', False)
567 keep = opts.get('keep', False)
568
568
569 # rebuild state
569 # rebuild state
570 if goal == 'continue':
570 if goal == 'continue':
571 state = readstate(repo)
571 state = readstate(repo)
572 parentctx = repo[state.parentctxnode]
572 state.parentctx, repl = bootstrapcontinue(ui, repo, state.parentctx,
573 parentctx, repl = bootstrapcontinue(ui, repo, parentctx, state.rules,
573 state.rules, opts)
574 opts)
575 state.replacements.extend(repl)
574 state.replacements.extend(repl)
576 state.parentctxnode = parentctx.node()
577 elif goal == 'abort':
575 elif goal == 'abort':
578 state = readstate(repo)
576 state = readstate(repo)
579 mapping, tmpnodes, leafs, _ntm = processreplacement(repo,
577 mapping, tmpnodes, leafs, _ntm = processreplacement(repo,
580 state.replacements)
578 state.replacements)
581 ui.debug('restore wc to old parent %s\n' % node.short(state.topmost))
579 ui.debug('restore wc to old parent %s\n' % node.short(state.topmost))
582 # check whether we should update away
580 # check whether we should update away
583 parentnodes = [c.node() for c in repo[None].parents()]
581 parentnodes = [c.node() for c in repo[None].parents()]
584 for n in leafs | set([state.parentctxnode]):
582 for n in leafs | set([state.parentctx.node()]):
585 if n in parentnodes:
583 if n in parentnodes:
586 hg.clean(repo, state.topmost)
584 hg.clean(repo, state.topmost)
587 break
585 break
588 else:
586 else:
589 pass
587 pass
590 cleanupnode(ui, repo, 'created', tmpnodes)
588 cleanupnode(ui, repo, 'created', tmpnodes)
591 cleanupnode(ui, repo, 'temp', leafs)
589 cleanupnode(ui, repo, 'temp', leafs)
592 state.clear()
590 state.clear()
593 return
591 return
594 else:
592 else:
595 cmdutil.checkunfinished(repo)
593 cmdutil.checkunfinished(repo)
596 cmdutil.bailifchanged(repo)
594 cmdutil.bailifchanged(repo)
597
595
598 topmost, empty = repo.dirstate.parents()
596 topmost, empty = repo.dirstate.parents()
599 if outg:
597 if outg:
600 if freeargs:
598 if freeargs:
601 remote = freeargs[0]
599 remote = freeargs[0]
602 else:
600 else:
603 remote = None
601 remote = None
604 root = findoutgoing(ui, repo, remote, force, opts)
602 root = findoutgoing(ui, repo, remote, force, opts)
605 else:
603 else:
606 rr = list(repo.set('roots(%ld)', scmutil.revrange(repo, revs)))
604 rr = list(repo.set('roots(%ld)', scmutil.revrange(repo, revs)))
607 if len(rr) != 1:
605 if len(rr) != 1:
608 raise util.Abort(_('The specified revisions must have '
606 raise util.Abort(_('The specified revisions must have '
609 'exactly one common root'))
607 'exactly one common root'))
610 root = rr[0].node()
608 root = rr[0].node()
611
609
612 revs = between(repo, root, topmost, keep)
610 revs = between(repo, root, topmost, keep)
613 if not revs:
611 if not revs:
614 raise util.Abort(_('%s is not an ancestor of working directory') %
612 raise util.Abort(_('%s is not an ancestor of working directory') %
615 node.short(root))
613 node.short(root))
616
614
617 ctxs = [repo[r] for r in revs]
615 ctxs = [repo[r] for r in revs]
618 if not rules:
616 if not rules:
619 rules = '\n'.join([makedesc(c) for c in ctxs])
617 rules = '\n'.join([makedesc(c) for c in ctxs])
620 rules += '\n\n'
618 rules += '\n\n'
621 rules += editcomment % (node.short(root), node.short(topmost))
619 rules += editcomment % (node.short(root), node.short(topmost))
622 rules = ui.edit(rules, ui.username())
620 rules = ui.edit(rules, ui.username())
623 # Save edit rules in .hg/histedit-last-edit.txt in case
621 # Save edit rules in .hg/histedit-last-edit.txt in case
624 # the user needs to ask for help after something
622 # the user needs to ask for help after something
625 # surprising happens.
623 # surprising happens.
626 f = open(repo.join('histedit-last-edit.txt'), 'w')
624 f = open(repo.join('histedit-last-edit.txt'), 'w')
627 f.write(rules)
625 f.write(rules)
628 f.close()
626 f.close()
629 else:
627 else:
630 if rules == '-':
628 if rules == '-':
631 f = sys.stdin
629 f = sys.stdin
632 else:
630 else:
633 f = open(rules)
631 f = open(rules)
634 rules = f.read()
632 rules = f.read()
635 f.close()
633 f.close()
636 rules = [l for l in (r.strip() for r in rules.splitlines())
634 rules = [l for l in (r.strip() for r in rules.splitlines())
637 if l and not l.startswith('#')]
635 if l and not l.startswith('#')]
638 rules = verifyrules(rules, repo, ctxs)
636 rules = verifyrules(rules, repo, ctxs)
639
637
640 parentctx = repo[root].parents()[0]
638 parentctx = repo[root].parents()[0]
641
639
642 state = histeditstate(repo, parentctx.node(), rules, keep,
640 state = histeditstate(repo, parentctx, rules, keep,
643 topmost, replacements)
641 topmost, replacements)
644
642
645 while state.rules:
643 while state.rules:
646 state.write()
644 state.write()
647 action, ha = state.rules.pop(0)
645 action, ha = state.rules.pop(0)
648 ui.debug('histedit: processing %s %s\n' % (action, ha))
646 ui.debug('histedit: processing %s %s\n' % (action, ha))
649 actfunc = actiontable[action]
647 actfunc = actiontable[action]
650 parentctx = repo[state.parentctxnode]
648 state.parentctx, replacement_ = actfunc(ui, repo, state.parentctx,
651 parentctx, replacement_ = actfunc(ui, repo, parentctx,
652 ha, opts)
649 ha, opts)
653 state.parentctxnode = parentctx.node()
654 state.replacements.extend(replacement_)
650 state.replacements.extend(replacement_)
655
651
656 hg.update(repo, state.parentctxnode)
652 hg.update(repo, state.parentctx.node())
657
653
658 mapping, tmpnodes, created, ntm = processreplacement(repo,
654 mapping, tmpnodes, created, ntm = processreplacement(repo,
659 state.replacements)
655 state.replacements)
660 if mapping:
656 if mapping:
661 for prec, succs in mapping.iteritems():
657 for prec, succs in mapping.iteritems():
662 if not succs:
658 if not succs:
663 ui.debug('histedit: %s is dropped\n' % node.short(prec))
659 ui.debug('histedit: %s is dropped\n' % node.short(prec))
664 else:
660 else:
665 ui.debug('histedit: %s is replaced by %s\n' % (
661 ui.debug('histedit: %s is replaced by %s\n' % (
666 node.short(prec), node.short(succs[0])))
662 node.short(prec), node.short(succs[0])))
667 if len(succs) > 1:
663 if len(succs) > 1:
668 m = 'histedit: %s'
664 m = 'histedit: %s'
669 for n in succs[1:]:
665 for n in succs[1:]:
670 ui.debug(m % node.short(n))
666 ui.debug(m % node.short(n))
671
667
672 if not keep:
668 if not keep:
673 if mapping:
669 if mapping:
674 movebookmarks(ui, repo, mapping, state.topmost, ntm)
670 movebookmarks(ui, repo, mapping, state.topmost, ntm)
675 # TODO update mq state
671 # TODO update mq state
676 if obsolete.isenabled(repo, obsolete.createmarkersopt):
672 if obsolete.isenabled(repo, obsolete.createmarkersopt):
677 markers = []
673 markers = []
678 # sort by revision number because it sound "right"
674 # sort by revision number because it sound "right"
679 for prec in sorted(mapping, key=repo.changelog.rev):
675 for prec in sorted(mapping, key=repo.changelog.rev):
680 succs = mapping[prec]
676 succs = mapping[prec]
681 markers.append((repo[prec],
677 markers.append((repo[prec],
682 tuple(repo[s] for s in succs)))
678 tuple(repo[s] for s in succs)))
683 if markers:
679 if markers:
684 obsolete.createmarkers(repo, markers)
680 obsolete.createmarkers(repo, markers)
685 else:
681 else:
686 cleanupnode(ui, repo, 'replaced', mapping)
682 cleanupnode(ui, repo, 'replaced', mapping)
687
683
688 cleanupnode(ui, repo, 'temp', tmpnodes)
684 cleanupnode(ui, repo, 'temp', tmpnodes)
689 state.clear()
685 state.clear()
690 if os.path.exists(repo.sjoin('undo')):
686 if os.path.exists(repo.sjoin('undo')):
691 os.unlink(repo.sjoin('undo'))
687 os.unlink(repo.sjoin('undo'))
692
688
693 def gatherchildren(repo, ctx):
689 def gatherchildren(repo, ctx):
694 # is there any new commit between the expected parent and "."
690 # is there any new commit between the expected parent and "."
695 #
691 #
696 # note: does not take non linear new change in account (but previous
692 # note: does not take non linear new change in account (but previous
697 # implementation didn't used them anyway (issue3655)
693 # implementation didn't used them anyway (issue3655)
698 newchildren = [c.node() for c in repo.set('(%d::.)', ctx)]
694 newchildren = [c.node() for c in repo.set('(%d::.)', ctx)]
699 if ctx.node() != node.nullid:
695 if ctx.node() != node.nullid:
700 if not newchildren:
696 if not newchildren:
701 # `ctx` should match but no result. This means that
697 # `ctx` should match but no result. This means that
702 # currentnode is not a descendant from ctx.
698 # currentnode is not a descendant from ctx.
703 msg = _('%s is not an ancestor of working directory')
699 msg = _('%s is not an ancestor of working directory')
704 hint = _('use "histedit --abort" to clear broken state')
700 hint = _('use "histedit --abort" to clear broken state')
705 raise util.Abort(msg % ctx, hint=hint)
701 raise util.Abort(msg % ctx, hint=hint)
706 newchildren.pop(0) # remove ctx
702 newchildren.pop(0) # remove ctx
707 return newchildren
703 return newchildren
708
704
709 def bootstrapcontinue(ui, repo, parentctx, rules, opts):
705 def bootstrapcontinue(ui, repo, parentctx, rules, opts):
710 action, currentnode = rules.pop(0)
706 action, currentnode = rules.pop(0)
711 ctx = repo[currentnode]
707 ctx = repo[currentnode]
712
708
713 newchildren = gatherchildren(repo, parentctx)
709 newchildren = gatherchildren(repo, parentctx)
714
710
715 # Commit dirty working directory if necessary
711 # Commit dirty working directory if necessary
716 new = None
712 new = None
717 s = repo.status()
713 s = repo.status()
718 if s.modified or s.added or s.removed or s.deleted:
714 if s.modified or s.added or s.removed or s.deleted:
719 # prepare the message for the commit to comes
715 # prepare the message for the commit to comes
720 if action in ('f', 'fold', 'r', 'roll'):
716 if action in ('f', 'fold', 'r', 'roll'):
721 message = 'fold-temp-revision %s' % currentnode
717 message = 'fold-temp-revision %s' % currentnode
722 else:
718 else:
723 message = ctx.description()
719 message = ctx.description()
724 editopt = action in ('e', 'edit', 'm', 'mess')
720 editopt = action in ('e', 'edit', 'm', 'mess')
725 canonaction = {'e': 'edit', 'm': 'mess', 'p': 'pick'}
721 canonaction = {'e': 'edit', 'm': 'mess', 'p': 'pick'}
726 editform = 'histedit.%s' % canonaction.get(action, action)
722 editform = 'histedit.%s' % canonaction.get(action, action)
727 editor = cmdutil.getcommiteditor(edit=editopt, editform=editform)
723 editor = cmdutil.getcommiteditor(edit=editopt, editform=editform)
728 commit = commitfuncfor(repo, ctx)
724 commit = commitfuncfor(repo, ctx)
729 new = commit(text=message, user=ctx.user(),
725 new = commit(text=message, user=ctx.user(),
730 date=ctx.date(), extra=ctx.extra(),
726 date=ctx.date(), extra=ctx.extra(),
731 editor=editor)
727 editor=editor)
732 if new is not None:
728 if new is not None:
733 newchildren.append(new)
729 newchildren.append(new)
734
730
735 replacements = []
731 replacements = []
736 # track replacements
732 # track replacements
737 if ctx.node() not in newchildren:
733 if ctx.node() not in newchildren:
738 # note: new children may be empty when the changeset is dropped.
734 # note: new children may be empty when the changeset is dropped.
739 # this happen e.g during conflicting pick where we revert content
735 # this happen e.g during conflicting pick where we revert content
740 # to parent.
736 # to parent.
741 replacements.append((ctx.node(), tuple(newchildren)))
737 replacements.append((ctx.node(), tuple(newchildren)))
742
738
743 if action in ('f', 'fold', 'r', 'roll'):
739 if action in ('f', 'fold', 'r', 'roll'):
744 if newchildren:
740 if newchildren:
745 # finalize fold operation if applicable
741 # finalize fold operation if applicable
746 if new is None:
742 if new is None:
747 new = newchildren[-1]
743 new = newchildren[-1]
748 else:
744 else:
749 newchildren.pop() # remove new from internal changes
745 newchildren.pop() # remove new from internal changes
750 foldopts = opts
746 foldopts = opts
751 if action in ('r', 'roll'):
747 if action in ('r', 'roll'):
752 foldopts = foldopts.copy()
748 foldopts = foldopts.copy()
753 foldopts['rollup'] = True
749 foldopts['rollup'] = True
754 parentctx, repl = finishfold(ui, repo, parentctx, ctx, new,
750 parentctx, repl = finishfold(ui, repo, parentctx, ctx, new,
755 foldopts, newchildren)
751 foldopts, newchildren)
756 replacements.extend(repl)
752 replacements.extend(repl)
757 else:
753 else:
758 # newchildren is empty if the fold did not result in any commit
754 # newchildren is empty if the fold did not result in any commit
759 # this happen when all folded change are discarded during the
755 # this happen when all folded change are discarded during the
760 # merge.
756 # merge.
761 replacements.append((ctx.node(), (parentctx.node(),)))
757 replacements.append((ctx.node(), (parentctx.node(),)))
762 elif newchildren:
758 elif newchildren:
763 # otherwise update "parentctx" before proceeding to further operation
759 # otherwise update "parentctx" before proceeding to further operation
764 parentctx = repo[newchildren[-1]]
760 parentctx = repo[newchildren[-1]]
765 return parentctx, replacements
761 return parentctx, replacements
766
762
767
763
768 def between(repo, old, new, keep):
764 def between(repo, old, new, keep):
769 """select and validate the set of revision to edit
765 """select and validate the set of revision to edit
770
766
771 When keep is false, the specified set can't have children."""
767 When keep is false, the specified set can't have children."""
772 ctxs = list(repo.set('%n::%n', old, new))
768 ctxs = list(repo.set('%n::%n', old, new))
773 if ctxs and not keep:
769 if ctxs and not keep:
774 if (not obsolete.isenabled(repo, obsolete.allowunstableopt) and
770 if (not obsolete.isenabled(repo, obsolete.allowunstableopt) and
775 repo.revs('(%ld::) - (%ld)', ctxs, ctxs)):
771 repo.revs('(%ld::) - (%ld)', ctxs, ctxs)):
776 raise util.Abort(_('cannot edit history that would orphan nodes'))
772 raise util.Abort(_('cannot edit history that would orphan nodes'))
777 if repo.revs('(%ld) and merge()', ctxs):
773 if repo.revs('(%ld) and merge()', ctxs):
778 raise util.Abort(_('cannot edit history that contains merges'))
774 raise util.Abort(_('cannot edit history that contains merges'))
779 root = ctxs[0] # list is already sorted by repo.set
775 root = ctxs[0] # list is already sorted by repo.set
780 if not root.mutable():
776 if not root.mutable():
781 raise util.Abort(_('cannot edit immutable changeset: %s') % root)
777 raise util.Abort(_('cannot edit immutable changeset: %s') % root)
782 return [c.node() for c in ctxs]
778 return [c.node() for c in ctxs]
783
779
784 def readstate(repo):
780 def readstate(repo):
785 """Reads a state from file and returns a histeditstate object
781 """Reads a state from file and returns a histeditstate object
786 """
782 """
787 try:
783 try:
788 fp = repo.vfs('histedit-state', 'r')
784 fp = repo.vfs('histedit-state', 'r')
789 except IOError, err:
785 except IOError, err:
790 if err.errno != errno.ENOENT:
786 if err.errno != errno.ENOENT:
791 raise
787 raise
792 raise util.Abort(_('no histedit in progress'))
788 raise util.Abort(_('no histedit in progress'))
793
789
794 (parentctxnode, rules, keep, topmost, replacements) = pickle.load(fp)
790 (parentctxnode, rules, keep, topmost, replacements) = pickle.load(fp)
795
791
796 return histeditstate(repo, parentctxnode, rules,
792 return histeditstate(repo, repo[parentctxnode], rules,
797 keep, topmost, replacements)
793 keep, topmost, replacements)
798
794
799 def makedesc(c):
795 def makedesc(c):
800 """build a initial action line for a ctx `c`
796 """build a initial action line for a ctx `c`
801
797
802 line are in the form:
798 line are in the form:
803
799
804 pick <hash> <rev> <summary>
800 pick <hash> <rev> <summary>
805 """
801 """
806 summary = ''
802 summary = ''
807 if c.description():
803 if c.description():
808 summary = c.description().splitlines()[0]
804 summary = c.description().splitlines()[0]
809 line = 'pick %s %d %s' % (c, c.rev(), summary)
805 line = 'pick %s %d %s' % (c, c.rev(), summary)
810 # trim to 80 columns so it's not stupidly wide in my editor
806 # trim to 80 columns so it's not stupidly wide in my editor
811 return util.ellipsis(line, 80)
807 return util.ellipsis(line, 80)
812
808
813 def verifyrules(rules, repo, ctxs):
809 def verifyrules(rules, repo, ctxs):
814 """Verify that there exists exactly one edit rule per given changeset.
810 """Verify that there exists exactly one edit rule per given changeset.
815
811
816 Will abort if there are to many or too few rules, a malformed rule,
812 Will abort if there are to many or too few rules, a malformed rule,
817 or a rule on a changeset outside of the user-given range.
813 or a rule on a changeset outside of the user-given range.
818 """
814 """
819 parsed = []
815 parsed = []
820 expected = set(str(c) for c in ctxs)
816 expected = set(str(c) for c in ctxs)
821 seen = set()
817 seen = set()
822 for r in rules:
818 for r in rules:
823 if ' ' not in r:
819 if ' ' not in r:
824 raise util.Abort(_('malformed line "%s"') % r)
820 raise util.Abort(_('malformed line "%s"') % r)
825 action, rest = r.split(' ', 1)
821 action, rest = r.split(' ', 1)
826 ha = rest.strip().split(' ', 1)[0]
822 ha = rest.strip().split(' ', 1)[0]
827 try:
823 try:
828 ha = str(repo[ha]) # ensure its a short hash
824 ha = str(repo[ha]) # ensure its a short hash
829 except error.RepoError:
825 except error.RepoError:
830 raise util.Abort(_('unknown changeset %s listed') % ha)
826 raise util.Abort(_('unknown changeset %s listed') % ha)
831 if ha not in expected:
827 if ha not in expected:
832 raise util.Abort(
828 raise util.Abort(
833 _('may not use changesets other than the ones listed'))
829 _('may not use changesets other than the ones listed'))
834 if ha in seen:
830 if ha in seen:
835 raise util.Abort(_('duplicated command for changeset %s') % ha)
831 raise util.Abort(_('duplicated command for changeset %s') % ha)
836 seen.add(ha)
832 seen.add(ha)
837 if action not in actiontable:
833 if action not in actiontable:
838 raise util.Abort(_('unknown action "%s"') % action)
834 raise util.Abort(_('unknown action "%s"') % action)
839 parsed.append([action, ha])
835 parsed.append([action, ha])
840 missing = sorted(expected - seen) # sort to stabilize output
836 missing = sorted(expected - seen) # sort to stabilize output
841 if missing:
837 if missing:
842 raise util.Abort(_('missing rules for changeset %s') % missing[0],
838 raise util.Abort(_('missing rules for changeset %s') % missing[0],
843 hint=_('do you want to use the drop action?'))
839 hint=_('do you want to use the drop action?'))
844 return parsed
840 return parsed
845
841
846 def processreplacement(repo, replacements):
842 def processreplacement(repo, replacements):
847 """process the list of replacements to return
843 """process the list of replacements to return
848
844
849 1) the final mapping between original and created nodes
845 1) the final mapping between original and created nodes
850 2) the list of temporary node created by histedit
846 2) the list of temporary node created by histedit
851 3) the list of new commit created by histedit"""
847 3) the list of new commit created by histedit"""
852 allsuccs = set()
848 allsuccs = set()
853 replaced = set()
849 replaced = set()
854 fullmapping = {}
850 fullmapping = {}
855 # initialise basic set
851 # initialise basic set
856 # fullmapping record all operation recorded in replacement
852 # fullmapping record all operation recorded in replacement
857 for rep in replacements:
853 for rep in replacements:
858 allsuccs.update(rep[1])
854 allsuccs.update(rep[1])
859 replaced.add(rep[0])
855 replaced.add(rep[0])
860 fullmapping.setdefault(rep[0], set()).update(rep[1])
856 fullmapping.setdefault(rep[0], set()).update(rep[1])
861 new = allsuccs - replaced
857 new = allsuccs - replaced
862 tmpnodes = allsuccs & replaced
858 tmpnodes = allsuccs & replaced
863 # Reduce content fullmapping into direct relation between original nodes
859 # Reduce content fullmapping into direct relation between original nodes
864 # and final node created during history edition
860 # and final node created during history edition
865 # Dropped changeset are replaced by an empty list
861 # Dropped changeset are replaced by an empty list
866 toproceed = set(fullmapping)
862 toproceed = set(fullmapping)
867 final = {}
863 final = {}
868 while toproceed:
864 while toproceed:
869 for x in list(toproceed):
865 for x in list(toproceed):
870 succs = fullmapping[x]
866 succs = fullmapping[x]
871 for s in list(succs):
867 for s in list(succs):
872 if s in toproceed:
868 if s in toproceed:
873 # non final node with unknown closure
869 # non final node with unknown closure
874 # We can't process this now
870 # We can't process this now
875 break
871 break
876 elif s in final:
872 elif s in final:
877 # non final node, replace with closure
873 # non final node, replace with closure
878 succs.remove(s)
874 succs.remove(s)
879 succs.update(final[s])
875 succs.update(final[s])
880 else:
876 else:
881 final[x] = succs
877 final[x] = succs
882 toproceed.remove(x)
878 toproceed.remove(x)
883 # remove tmpnodes from final mapping
879 # remove tmpnodes from final mapping
884 for n in tmpnodes:
880 for n in tmpnodes:
885 del final[n]
881 del final[n]
886 # we expect all changes involved in final to exist in the repo
882 # we expect all changes involved in final to exist in the repo
887 # turn `final` into list (topologically sorted)
883 # turn `final` into list (topologically sorted)
888 nm = repo.changelog.nodemap
884 nm = repo.changelog.nodemap
889 for prec, succs in final.items():
885 for prec, succs in final.items():
890 final[prec] = sorted(succs, key=nm.get)
886 final[prec] = sorted(succs, key=nm.get)
891
887
892 # computed topmost element (necessary for bookmark)
888 # computed topmost element (necessary for bookmark)
893 if new:
889 if new:
894 newtopmost = sorted(new, key=repo.changelog.rev)[-1]
890 newtopmost = sorted(new, key=repo.changelog.rev)[-1]
895 elif not final:
891 elif not final:
896 # Nothing rewritten at all. we won't need `newtopmost`
892 # Nothing rewritten at all. we won't need `newtopmost`
897 # It is the same as `oldtopmost` and `processreplacement` know it
893 # It is the same as `oldtopmost` and `processreplacement` know it
898 newtopmost = None
894 newtopmost = None
899 else:
895 else:
900 # every body died. The newtopmost is the parent of the root.
896 # every body died. The newtopmost is the parent of the root.
901 newtopmost = repo[sorted(final, key=repo.changelog.rev)[0]].p1().node()
897 newtopmost = repo[sorted(final, key=repo.changelog.rev)[0]].p1().node()
902
898
903 return final, tmpnodes, new, newtopmost
899 return final, tmpnodes, new, newtopmost
904
900
905 def movebookmarks(ui, repo, mapping, oldtopmost, newtopmost):
901 def movebookmarks(ui, repo, mapping, oldtopmost, newtopmost):
906 """Move bookmark from old to newly created node"""
902 """Move bookmark from old to newly created node"""
907 if not mapping:
903 if not mapping:
908 # if nothing got rewritten there is not purpose for this function
904 # if nothing got rewritten there is not purpose for this function
909 return
905 return
910 moves = []
906 moves = []
911 for bk, old in sorted(repo._bookmarks.iteritems()):
907 for bk, old in sorted(repo._bookmarks.iteritems()):
912 if old == oldtopmost:
908 if old == oldtopmost:
913 # special case ensure bookmark stay on tip.
909 # special case ensure bookmark stay on tip.
914 #
910 #
915 # This is arguably a feature and we may only want that for the
911 # This is arguably a feature and we may only want that for the
916 # active bookmark. But the behavior is kept compatible with the old
912 # active bookmark. But the behavior is kept compatible with the old
917 # version for now.
913 # version for now.
918 moves.append((bk, newtopmost))
914 moves.append((bk, newtopmost))
919 continue
915 continue
920 base = old
916 base = old
921 new = mapping.get(base, None)
917 new = mapping.get(base, None)
922 if new is None:
918 if new is None:
923 continue
919 continue
924 while not new:
920 while not new:
925 # base is killed, trying with parent
921 # base is killed, trying with parent
926 base = repo[base].p1().node()
922 base = repo[base].p1().node()
927 new = mapping.get(base, (base,))
923 new = mapping.get(base, (base,))
928 # nothing to move
924 # nothing to move
929 moves.append((bk, new[-1]))
925 moves.append((bk, new[-1]))
930 if moves:
926 if moves:
931 marks = repo._bookmarks
927 marks = repo._bookmarks
932 for mark, new in moves:
928 for mark, new in moves:
933 old = marks[mark]
929 old = marks[mark]
934 ui.note(_('histedit: moving bookmarks %s from %s to %s\n')
930 ui.note(_('histedit: moving bookmarks %s from %s to %s\n')
935 % (mark, node.short(old), node.short(new)))
931 % (mark, node.short(old), node.short(new)))
936 marks[mark] = new
932 marks[mark] = new
937 marks.write()
933 marks.write()
938
934
939 def cleanupnode(ui, repo, name, nodes):
935 def cleanupnode(ui, repo, name, nodes):
940 """strip a group of nodes from the repository
936 """strip a group of nodes from the repository
941
937
942 The set of node to strip may contains unknown nodes."""
938 The set of node to strip may contains unknown nodes."""
943 ui.debug('should strip %s nodes %s\n' %
939 ui.debug('should strip %s nodes %s\n' %
944 (name, ', '.join([node.short(n) for n in nodes])))
940 (name, ', '.join([node.short(n) for n in nodes])))
945 lock = None
941 lock = None
946 try:
942 try:
947 lock = repo.lock()
943 lock = repo.lock()
948 # Find all node that need to be stripped
944 # Find all node that need to be stripped
949 # (we hg %lr instead of %ln to silently ignore unknown item
945 # (we hg %lr instead of %ln to silently ignore unknown item
950 nm = repo.changelog.nodemap
946 nm = repo.changelog.nodemap
951 nodes = sorted(n for n in nodes if n in nm)
947 nodes = sorted(n for n in nodes if n in nm)
952 roots = [c.node() for c in repo.set("roots(%ln)", nodes)]
948 roots = [c.node() for c in repo.set("roots(%ln)", nodes)]
953 for c in roots:
949 for c in roots:
954 # We should process node in reverse order to strip tip most first.
950 # We should process node in reverse order to strip tip most first.
955 # but this trigger a bug in changegroup hook.
951 # but this trigger a bug in changegroup hook.
956 # This would reduce bundle overhead
952 # This would reduce bundle overhead
957 repair.strip(ui, repo, c)
953 repair.strip(ui, repo, c)
958 finally:
954 finally:
959 release(lock)
955 release(lock)
960
956
961 def summaryhook(ui, repo):
957 def summaryhook(ui, repo):
962 if not os.path.exists(repo.join('histedit-state')):
958 if not os.path.exists(repo.join('histedit-state')):
963 return
959 return
964 state = readstate(repo)
960 state = readstate(repo)
965 if state.rules:
961 if state.rules:
966 # i18n: column positioning for "hg summary"
962 # i18n: column positioning for "hg summary"
967 ui.write(_('hist: %s (histedit --continue)\n') %
963 ui.write(_('hist: %s (histedit --continue)\n') %
968 (ui.label(_('%d remaining'), 'histedit.remaining') %
964 (ui.label(_('%d remaining'), 'histedit.remaining') %
969 len(state.rules)))
965 len(state.rules)))
970
966
971 def extsetup(ui):
967 def extsetup(ui):
972 cmdutil.summaryhooks.add('histedit', summaryhook)
968 cmdutil.summaryhooks.add('histedit', summaryhook)
973 cmdutil.unfinishedstates.append(
969 cmdutil.unfinishedstates.append(
974 ['histedit-state', False, True, _('histedit in progress'),
970 ['histedit-state', False, True, _('histedit in progress'),
975 _("use 'hg histedit --continue' or 'hg histedit --abort'")])
971 _("use 'hg histedit --continue' or 'hg histedit --abort'")])
General Comments 0
You need to be logged in to leave comments. Login now