##// END OF EJS Templates
rebase: don't update state dict same way for each root...
Martin von Zweigbergk -
r32175:456b4a32 default
parent child Browse files
Show More
@@ -1,1522 +1,1521 b''
1 # rebase.py - rebasing feature for mercurial
1 # rebase.py - rebasing feature for mercurial
2 #
2 #
3 # Copyright 2008 Stefano Tortarolo <stefano.tortarolo at gmail dot com>
3 # Copyright 2008 Stefano Tortarolo <stefano.tortarolo at gmail dot 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
7
8 '''command to move sets of revisions to a different ancestor
8 '''command to move sets of revisions to a different ancestor
9
9
10 This extension lets you rebase changesets in an existing Mercurial
10 This extension lets you rebase changesets in an existing Mercurial
11 repository.
11 repository.
12
12
13 For more information:
13 For more information:
14 https://mercurial-scm.org/wiki/RebaseExtension
14 https://mercurial-scm.org/wiki/RebaseExtension
15 '''
15 '''
16
16
17 from __future__ import absolute_import
17 from __future__ import absolute_import
18
18
19 import errno
19 import errno
20 import os
20 import os
21
21
22 from mercurial.i18n import _
22 from mercurial.i18n import _
23 from mercurial.node import (
23 from mercurial.node import (
24 hex,
24 hex,
25 nullid,
25 nullid,
26 nullrev,
26 nullrev,
27 short,
27 short,
28 )
28 )
29 from mercurial import (
29 from mercurial import (
30 bookmarks,
30 bookmarks,
31 cmdutil,
31 cmdutil,
32 commands,
32 commands,
33 copies,
33 copies,
34 destutil,
34 destutil,
35 dirstateguard,
35 dirstateguard,
36 error,
36 error,
37 extensions,
37 extensions,
38 hg,
38 hg,
39 lock,
39 lock,
40 merge as mergemod,
40 merge as mergemod,
41 mergeutil,
41 mergeutil,
42 obsolete,
42 obsolete,
43 patch,
43 patch,
44 phases,
44 phases,
45 registrar,
45 registrar,
46 repair,
46 repair,
47 repoview,
47 repoview,
48 revset,
48 revset,
49 scmutil,
49 scmutil,
50 smartset,
50 smartset,
51 util,
51 util,
52 )
52 )
53
53
54 release = lock.release
54 release = lock.release
55 templateopts = commands.templateopts
55 templateopts = commands.templateopts
56
56
57 # The following constants are used throughout the rebase module. The ordering of
57 # The following constants are used throughout the rebase module. The ordering of
58 # their values must be maintained.
58 # their values must be maintained.
59
59
60 # Indicates that a revision needs to be rebased
60 # Indicates that a revision needs to be rebased
61 revtodo = -1
61 revtodo = -1
62 nullmerge = -2
62 nullmerge = -2
63 revignored = -3
63 revignored = -3
64 # successor in rebase destination
64 # successor in rebase destination
65 revprecursor = -4
65 revprecursor = -4
66 # plain prune (no successor)
66 # plain prune (no successor)
67 revpruned = -5
67 revpruned = -5
68 revskipped = (revignored, revprecursor, revpruned)
68 revskipped = (revignored, revprecursor, revpruned)
69
69
70 cmdtable = {}
70 cmdtable = {}
71 command = cmdutil.command(cmdtable)
71 command = cmdutil.command(cmdtable)
72 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
72 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
73 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
73 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
74 # be specifying the version(s) of Mercurial they are tested with, or
74 # be specifying the version(s) of Mercurial they are tested with, or
75 # leave the attribute unspecified.
75 # leave the attribute unspecified.
76 testedwith = 'ships-with-hg-core'
76 testedwith = 'ships-with-hg-core'
77
77
78 def _nothingtorebase():
78 def _nothingtorebase():
79 return 1
79 return 1
80
80
81 def _savegraft(ctx, extra):
81 def _savegraft(ctx, extra):
82 s = ctx.extra().get('source', None)
82 s = ctx.extra().get('source', None)
83 if s is not None:
83 if s is not None:
84 extra['source'] = s
84 extra['source'] = s
85 s = ctx.extra().get('intermediate-source', None)
85 s = ctx.extra().get('intermediate-source', None)
86 if s is not None:
86 if s is not None:
87 extra['intermediate-source'] = s
87 extra['intermediate-source'] = s
88
88
89 def _savebranch(ctx, extra):
89 def _savebranch(ctx, extra):
90 extra['branch'] = ctx.branch()
90 extra['branch'] = ctx.branch()
91
91
92 def _makeextrafn(copiers):
92 def _makeextrafn(copiers):
93 """make an extrafn out of the given copy-functions.
93 """make an extrafn out of the given copy-functions.
94
94
95 A copy function takes a context and an extra dict, and mutates the
95 A copy function takes a context and an extra dict, and mutates the
96 extra dict as needed based on the given context.
96 extra dict as needed based on the given context.
97 """
97 """
98 def extrafn(ctx, extra):
98 def extrafn(ctx, extra):
99 for c in copiers:
99 for c in copiers:
100 c(ctx, extra)
100 c(ctx, extra)
101 return extrafn
101 return extrafn
102
102
103 def _destrebase(repo, sourceset, destspace=None):
103 def _destrebase(repo, sourceset, destspace=None):
104 """small wrapper around destmerge to pass the right extra args
104 """small wrapper around destmerge to pass the right extra args
105
105
106 Please wrap destutil.destmerge instead."""
106 Please wrap destutil.destmerge instead."""
107 return destutil.destmerge(repo, action='rebase', sourceset=sourceset,
107 return destutil.destmerge(repo, action='rebase', sourceset=sourceset,
108 onheadcheck=False, destspace=destspace)
108 onheadcheck=False, destspace=destspace)
109
109
110 revsetpredicate = registrar.revsetpredicate()
110 revsetpredicate = registrar.revsetpredicate()
111
111
112 @revsetpredicate('_destrebase')
112 @revsetpredicate('_destrebase')
113 def _revsetdestrebase(repo, subset, x):
113 def _revsetdestrebase(repo, subset, x):
114 # ``_rebasedefaultdest()``
114 # ``_rebasedefaultdest()``
115
115
116 # default destination for rebase.
116 # default destination for rebase.
117 # # XXX: Currently private because I expect the signature to change.
117 # # XXX: Currently private because I expect the signature to change.
118 # # XXX: - bailing out in case of ambiguity vs returning all data.
118 # # XXX: - bailing out in case of ambiguity vs returning all data.
119 # i18n: "_rebasedefaultdest" is a keyword
119 # i18n: "_rebasedefaultdest" is a keyword
120 sourceset = None
120 sourceset = None
121 if x is not None:
121 if x is not None:
122 sourceset = revset.getset(repo, smartset.fullreposet(repo), x)
122 sourceset = revset.getset(repo, smartset.fullreposet(repo), x)
123 return subset & smartset.baseset([_destrebase(repo, sourceset)])
123 return subset & smartset.baseset([_destrebase(repo, sourceset)])
124
124
125 class rebaseruntime(object):
125 class rebaseruntime(object):
126 """This class is a container for rebase runtime state"""
126 """This class is a container for rebase runtime state"""
127 def __init__(self, repo, ui, opts=None):
127 def __init__(self, repo, ui, opts=None):
128 if opts is None:
128 if opts is None:
129 opts = {}
129 opts = {}
130
130
131 self.repo = repo
131 self.repo = repo
132 self.ui = ui
132 self.ui = ui
133 self.opts = opts
133 self.opts = opts
134 self.originalwd = None
134 self.originalwd = None
135 self.external = nullrev
135 self.external = nullrev
136 # Mapping between the old revision id and either what is the new rebased
136 # Mapping between the old revision id and either what is the new rebased
137 # revision or what needs to be done with the old revision. The state
137 # revision or what needs to be done with the old revision. The state
138 # dict will be what contains most of the rebase progress state.
138 # dict will be what contains most of the rebase progress state.
139 self.state = {}
139 self.state = {}
140 self.activebookmark = None
140 self.activebookmark = None
141 self.currentbookmarks = None
141 self.currentbookmarks = None
142 self.target = None
142 self.target = None
143 self.skipped = set()
143 self.skipped = set()
144 self.targetancestors = set()
144 self.targetancestors = set()
145
145
146 self.collapsef = opts.get('collapse', False)
146 self.collapsef = opts.get('collapse', False)
147 self.collapsemsg = cmdutil.logmessage(ui, opts)
147 self.collapsemsg = cmdutil.logmessage(ui, opts)
148 self.date = opts.get('date', None)
148 self.date = opts.get('date', None)
149
149
150 e = opts.get('extrafn') # internal, used by e.g. hgsubversion
150 e = opts.get('extrafn') # internal, used by e.g. hgsubversion
151 self.extrafns = [_savegraft]
151 self.extrafns = [_savegraft]
152 if e:
152 if e:
153 self.extrafns = [e]
153 self.extrafns = [e]
154
154
155 self.keepf = opts.get('keep', False)
155 self.keepf = opts.get('keep', False)
156 self.keepbranchesf = opts.get('keepbranches', False)
156 self.keepbranchesf = opts.get('keepbranches', False)
157 # keepopen is not meant for use on the command line, but by
157 # keepopen is not meant for use on the command line, but by
158 # other extensions
158 # other extensions
159 self.keepopen = opts.get('keepopen', False)
159 self.keepopen = opts.get('keepopen', False)
160 self.obsoletenotrebased = {}
160 self.obsoletenotrebased = {}
161
161
162 def storestatus(self, tr=None):
162 def storestatus(self, tr=None):
163 """Store the current status to allow recovery"""
163 """Store the current status to allow recovery"""
164 if tr:
164 if tr:
165 tr.addfilegenerator('rebasestate', ('rebasestate',),
165 tr.addfilegenerator('rebasestate', ('rebasestate',),
166 self._writestatus, location='plain')
166 self._writestatus, location='plain')
167 else:
167 else:
168 with self.repo.vfs("rebasestate", "w") as f:
168 with self.repo.vfs("rebasestate", "w") as f:
169 self._writestatus(f)
169 self._writestatus(f)
170
170
171 def _writestatus(self, f):
171 def _writestatus(self, f):
172 repo = self.repo.unfiltered()
172 repo = self.repo.unfiltered()
173 f.write(repo[self.originalwd].hex() + '\n')
173 f.write(repo[self.originalwd].hex() + '\n')
174 f.write(repo[self.target].hex() + '\n')
174 f.write(repo[self.target].hex() + '\n')
175 f.write(repo[self.external].hex() + '\n')
175 f.write(repo[self.external].hex() + '\n')
176 f.write('%d\n' % int(self.collapsef))
176 f.write('%d\n' % int(self.collapsef))
177 f.write('%d\n' % int(self.keepf))
177 f.write('%d\n' % int(self.keepf))
178 f.write('%d\n' % int(self.keepbranchesf))
178 f.write('%d\n' % int(self.keepbranchesf))
179 f.write('%s\n' % (self.activebookmark or ''))
179 f.write('%s\n' % (self.activebookmark or ''))
180 for d, v in self.state.iteritems():
180 for d, v in self.state.iteritems():
181 oldrev = repo[d].hex()
181 oldrev = repo[d].hex()
182 if v >= 0:
182 if v >= 0:
183 newrev = repo[v].hex()
183 newrev = repo[v].hex()
184 elif v == revtodo:
184 elif v == revtodo:
185 # To maintain format compatibility, we have to use nullid.
185 # To maintain format compatibility, we have to use nullid.
186 # Please do remove this special case when upgrading the format.
186 # Please do remove this special case when upgrading the format.
187 newrev = hex(nullid)
187 newrev = hex(nullid)
188 else:
188 else:
189 newrev = v
189 newrev = v
190 f.write("%s:%s\n" % (oldrev, newrev))
190 f.write("%s:%s\n" % (oldrev, newrev))
191 repo.ui.debug('rebase status stored\n')
191 repo.ui.debug('rebase status stored\n')
192
192
193 def restorestatus(self):
193 def restorestatus(self):
194 """Restore a previously stored status"""
194 """Restore a previously stored status"""
195 repo = self.repo
195 repo = self.repo
196 keepbranches = None
196 keepbranches = None
197 target = None
197 target = None
198 collapse = False
198 collapse = False
199 external = nullrev
199 external = nullrev
200 activebookmark = None
200 activebookmark = None
201 state = {}
201 state = {}
202
202
203 try:
203 try:
204 f = repo.vfs("rebasestate")
204 f = repo.vfs("rebasestate")
205 for i, l in enumerate(f.read().splitlines()):
205 for i, l in enumerate(f.read().splitlines()):
206 if i == 0:
206 if i == 0:
207 originalwd = repo[l].rev()
207 originalwd = repo[l].rev()
208 elif i == 1:
208 elif i == 1:
209 target = repo[l].rev()
209 target = repo[l].rev()
210 elif i == 2:
210 elif i == 2:
211 external = repo[l].rev()
211 external = repo[l].rev()
212 elif i == 3:
212 elif i == 3:
213 collapse = bool(int(l))
213 collapse = bool(int(l))
214 elif i == 4:
214 elif i == 4:
215 keep = bool(int(l))
215 keep = bool(int(l))
216 elif i == 5:
216 elif i == 5:
217 keepbranches = bool(int(l))
217 keepbranches = bool(int(l))
218 elif i == 6 and not (len(l) == 81 and ':' in l):
218 elif i == 6 and not (len(l) == 81 and ':' in l):
219 # line 6 is a recent addition, so for backwards
219 # line 6 is a recent addition, so for backwards
220 # compatibility check that the line doesn't look like the
220 # compatibility check that the line doesn't look like the
221 # oldrev:newrev lines
221 # oldrev:newrev lines
222 activebookmark = l
222 activebookmark = l
223 else:
223 else:
224 oldrev, newrev = l.split(':')
224 oldrev, newrev = l.split(':')
225 if newrev in (str(nullmerge), str(revignored),
225 if newrev in (str(nullmerge), str(revignored),
226 str(revprecursor), str(revpruned)):
226 str(revprecursor), str(revpruned)):
227 state[repo[oldrev].rev()] = int(newrev)
227 state[repo[oldrev].rev()] = int(newrev)
228 elif newrev == nullid:
228 elif newrev == nullid:
229 state[repo[oldrev].rev()] = revtodo
229 state[repo[oldrev].rev()] = revtodo
230 # Legacy compat special case
230 # Legacy compat special case
231 else:
231 else:
232 state[repo[oldrev].rev()] = repo[newrev].rev()
232 state[repo[oldrev].rev()] = repo[newrev].rev()
233
233
234 except IOError as err:
234 except IOError as err:
235 if err.errno != errno.ENOENT:
235 if err.errno != errno.ENOENT:
236 raise
236 raise
237 cmdutil.wrongtooltocontinue(repo, _('rebase'))
237 cmdutil.wrongtooltocontinue(repo, _('rebase'))
238
238
239 if keepbranches is None:
239 if keepbranches is None:
240 raise error.Abort(_('.hg/rebasestate is incomplete'))
240 raise error.Abort(_('.hg/rebasestate is incomplete'))
241
241
242 skipped = set()
242 skipped = set()
243 # recompute the set of skipped revs
243 # recompute the set of skipped revs
244 if not collapse:
244 if not collapse:
245 seen = set([target])
245 seen = set([target])
246 for old, new in sorted(state.items()):
246 for old, new in sorted(state.items()):
247 if new != revtodo and new in seen:
247 if new != revtodo and new in seen:
248 skipped.add(old)
248 skipped.add(old)
249 seen.add(new)
249 seen.add(new)
250 repo.ui.debug('computed skipped revs: %s\n' %
250 repo.ui.debug('computed skipped revs: %s\n' %
251 (' '.join(str(r) for r in sorted(skipped)) or None))
251 (' '.join(str(r) for r in sorted(skipped)) or None))
252 repo.ui.debug('rebase status resumed\n')
252 repo.ui.debug('rebase status resumed\n')
253 _setrebasesetvisibility(repo, set(state.keys()) | set([originalwd]))
253 _setrebasesetvisibility(repo, set(state.keys()) | set([originalwd]))
254
254
255 self.originalwd = originalwd
255 self.originalwd = originalwd
256 self.target = target
256 self.target = target
257 self.state = state
257 self.state = state
258 self.skipped = skipped
258 self.skipped = skipped
259 self.collapsef = collapse
259 self.collapsef = collapse
260 self.keepf = keep
260 self.keepf = keep
261 self.keepbranchesf = keepbranches
261 self.keepbranchesf = keepbranches
262 self.external = external
262 self.external = external
263 self.activebookmark = activebookmark
263 self.activebookmark = activebookmark
264
264
265 def _handleskippingobsolete(self, rebaserevs, obsoleterevs, target):
265 def _handleskippingobsolete(self, rebaserevs, obsoleterevs, target):
266 """Compute structures necessary for skipping obsolete revisions
266 """Compute structures necessary for skipping obsolete revisions
267
267
268 rebaserevs: iterable of all revisions that are to be rebased
268 rebaserevs: iterable of all revisions that are to be rebased
269 obsoleterevs: iterable of all obsolete revisions in rebaseset
269 obsoleterevs: iterable of all obsolete revisions in rebaseset
270 target: a destination revision for the rebase operation
270 target: a destination revision for the rebase operation
271 """
271 """
272 self.obsoletenotrebased = {}
272 self.obsoletenotrebased = {}
273 if not self.ui.configbool('experimental', 'rebaseskipobsolete',
273 if not self.ui.configbool('experimental', 'rebaseskipobsolete',
274 default=True):
274 default=True):
275 return
275 return
276 rebaseset = set(rebaserevs)
276 rebaseset = set(rebaserevs)
277 obsoleteset = set(obsoleterevs)
277 obsoleteset = set(obsoleterevs)
278 self.obsoletenotrebased = _computeobsoletenotrebased(self.repo,
278 self.obsoletenotrebased = _computeobsoletenotrebased(self.repo,
279 obsoleteset, target)
279 obsoleteset, target)
280 skippedset = set(self.obsoletenotrebased)
280 skippedset = set(self.obsoletenotrebased)
281 _checkobsrebase(self.repo, self.ui, obsoleteset, rebaseset, skippedset)
281 _checkobsrebase(self.repo, self.ui, obsoleteset, rebaseset, skippedset)
282
282
283 def _prepareabortorcontinue(self, isabort):
283 def _prepareabortorcontinue(self, isabort):
284 try:
284 try:
285 self.restorestatus()
285 self.restorestatus()
286 self.collapsemsg = restorecollapsemsg(self.repo, isabort)
286 self.collapsemsg = restorecollapsemsg(self.repo, isabort)
287 except error.RepoLookupError:
287 except error.RepoLookupError:
288 if isabort:
288 if isabort:
289 clearstatus(self.repo)
289 clearstatus(self.repo)
290 clearcollapsemsg(self.repo)
290 clearcollapsemsg(self.repo)
291 self.repo.ui.warn(_('rebase aborted (no revision is removed,'
291 self.repo.ui.warn(_('rebase aborted (no revision is removed,'
292 ' only broken state is cleared)\n'))
292 ' only broken state is cleared)\n'))
293 return 0
293 return 0
294 else:
294 else:
295 msg = _('cannot continue inconsistent rebase')
295 msg = _('cannot continue inconsistent rebase')
296 hint = _('use "hg rebase --abort" to clear broken state')
296 hint = _('use "hg rebase --abort" to clear broken state')
297 raise error.Abort(msg, hint=hint)
297 raise error.Abort(msg, hint=hint)
298 if isabort:
298 if isabort:
299 return abort(self.repo, self.originalwd, self.target,
299 return abort(self.repo, self.originalwd, self.target,
300 self.state, activebookmark=self.activebookmark)
300 self.state, activebookmark=self.activebookmark)
301
301
302 obsrevs = (r for r, st in self.state.items() if st == revprecursor)
302 obsrevs = (r for r, st in self.state.items() if st == revprecursor)
303 self._handleskippingobsolete(self.state.keys(), obsrevs, self.target)
303 self._handleskippingobsolete(self.state.keys(), obsrevs, self.target)
304
304
305 def _preparenewrebase(self, dest, rebaseset):
305 def _preparenewrebase(self, dest, rebaseset):
306 if dest is None:
306 if dest is None:
307 return _nothingtorebase()
307 return _nothingtorebase()
308
308
309 allowunstable = obsolete.isenabled(self.repo, obsolete.allowunstableopt)
309 allowunstable = obsolete.isenabled(self.repo, obsolete.allowunstableopt)
310 if (not (self.keepf or allowunstable)
310 if (not (self.keepf or allowunstable)
311 and self.repo.revs('first(children(%ld) - %ld)',
311 and self.repo.revs('first(children(%ld) - %ld)',
312 rebaseset, rebaseset)):
312 rebaseset, rebaseset)):
313 raise error.Abort(
313 raise error.Abort(
314 _("can't remove original changesets with"
314 _("can't remove original changesets with"
315 " unrebased descendants"),
315 " unrebased descendants"),
316 hint=_('use --keep to keep original changesets'))
316 hint=_('use --keep to keep original changesets'))
317
317
318 obsrevs = _filterobsoleterevs(self.repo, set(rebaseset))
318 obsrevs = _filterobsoleterevs(self.repo, set(rebaseset))
319 self._handleskippingobsolete(rebaseset, obsrevs, dest)
319 self._handleskippingobsolete(rebaseset, obsrevs, dest)
320
320
321 result = buildstate(self.repo, dest, rebaseset, self.collapsef,
321 result = buildstate(self.repo, dest, rebaseset, self.collapsef,
322 self.obsoletenotrebased)
322 self.obsoletenotrebased)
323
323
324 if not result:
324 if not result:
325 # Empty state built, nothing to rebase
325 # Empty state built, nothing to rebase
326 self.ui.status(_('nothing to rebase\n'))
326 self.ui.status(_('nothing to rebase\n'))
327 return _nothingtorebase()
327 return _nothingtorebase()
328
328
329 for root in self.repo.set('roots(%ld)', rebaseset):
329 for root in self.repo.set('roots(%ld)', rebaseset):
330 if not self.keepf and not root.mutable():
330 if not self.keepf and not root.mutable():
331 raise error.Abort(_("can't rebase public changeset %s")
331 raise error.Abort(_("can't rebase public changeset %s")
332 % root,
332 % root,
333 hint=_("see 'hg help phases' for details"))
333 hint=_("see 'hg help phases' for details"))
334
334
335 (self.originalwd, self.target, self.state) = result
335 (self.originalwd, self.target, self.state) = result
336 if self.collapsef:
336 if self.collapsef:
337 self.targetancestors = self.repo.changelog.ancestors(
337 self.targetancestors = self.repo.changelog.ancestors(
338 [self.target],
338 [self.target],
339 inclusive=True)
339 inclusive=True)
340 self.external = externalparent(self.repo, self.state,
340 self.external = externalparent(self.repo, self.state,
341 self.targetancestors)
341 self.targetancestors)
342
342
343 if dest.closesbranch() and not self.keepbranchesf:
343 if dest.closesbranch() and not self.keepbranchesf:
344 self.ui.status(_('reopening closed branch head %s\n') % dest)
344 self.ui.status(_('reopening closed branch head %s\n') % dest)
345
345
346 def _performrebase(self, tr):
346 def _performrebase(self, tr):
347 repo, ui, opts = self.repo, self.ui, self.opts
347 repo, ui, opts = self.repo, self.ui, self.opts
348 if self.keepbranchesf:
348 if self.keepbranchesf:
349 # insert _savebranch at the start of extrafns so if
349 # insert _savebranch at the start of extrafns so if
350 # there's a user-provided extrafn it can clobber branch if
350 # there's a user-provided extrafn it can clobber branch if
351 # desired
351 # desired
352 self.extrafns.insert(0, _savebranch)
352 self.extrafns.insert(0, _savebranch)
353 if self.collapsef:
353 if self.collapsef:
354 branches = set()
354 branches = set()
355 for rev in self.state:
355 for rev in self.state:
356 branches.add(repo[rev].branch())
356 branches.add(repo[rev].branch())
357 if len(branches) > 1:
357 if len(branches) > 1:
358 raise error.Abort(_('cannot collapse multiple named '
358 raise error.Abort(_('cannot collapse multiple named '
359 'branches'))
359 'branches'))
360
360
361 # Rebase
361 # Rebase
362 if not self.targetancestors:
362 if not self.targetancestors:
363 self.targetancestors = repo.changelog.ancestors([self.target],
363 self.targetancestors = repo.changelog.ancestors([self.target],
364 inclusive=True)
364 inclusive=True)
365
365
366 # Keep track of the current bookmarks in order to reset them later
366 # Keep track of the current bookmarks in order to reset them later
367 self.currentbookmarks = repo._bookmarks.copy()
367 self.currentbookmarks = repo._bookmarks.copy()
368 self.activebookmark = self.activebookmark or repo._activebookmark
368 self.activebookmark = self.activebookmark or repo._activebookmark
369 if self.activebookmark:
369 if self.activebookmark:
370 bookmarks.deactivate(repo)
370 bookmarks.deactivate(repo)
371
371
372 # Store the state before we begin so users can run 'hg rebase --abort'
372 # Store the state before we begin so users can run 'hg rebase --abort'
373 # if we fail before the transaction closes.
373 # if we fail before the transaction closes.
374 self.storestatus()
374 self.storestatus()
375
375
376 sortedrevs = repo.revs('sort(%ld, -topo)', self.state)
376 sortedrevs = repo.revs('sort(%ld, -topo)', self.state)
377 cands = [k for k, v in self.state.iteritems() if v == revtodo]
377 cands = [k for k, v in self.state.iteritems() if v == revtodo]
378 total = len(cands)
378 total = len(cands)
379 pos = 0
379 pos = 0
380 for rev in sortedrevs:
380 for rev in sortedrevs:
381 ctx = repo[rev]
381 ctx = repo[rev]
382 desc = '%d:%s "%s"' % (ctx.rev(), ctx,
382 desc = '%d:%s "%s"' % (ctx.rev(), ctx,
383 ctx.description().split('\n', 1)[0])
383 ctx.description().split('\n', 1)[0])
384 names = repo.nodetags(ctx.node()) + repo.nodebookmarks(ctx.node())
384 names = repo.nodetags(ctx.node()) + repo.nodebookmarks(ctx.node())
385 if names:
385 if names:
386 desc += ' (%s)' % ' '.join(names)
386 desc += ' (%s)' % ' '.join(names)
387 if self.state[rev] == revtodo:
387 if self.state[rev] == revtodo:
388 pos += 1
388 pos += 1
389 ui.status(_('rebasing %s\n') % desc)
389 ui.status(_('rebasing %s\n') % desc)
390 ui.progress(_("rebasing"), pos, ("%d:%s" % (rev, ctx)),
390 ui.progress(_("rebasing"), pos, ("%d:%s" % (rev, ctx)),
391 _('changesets'), total)
391 _('changesets'), total)
392 p1, p2, base = defineparents(repo, rev, self.target,
392 p1, p2, base = defineparents(repo, rev, self.target,
393 self.state,
393 self.state,
394 self.targetancestors,
394 self.targetancestors,
395 self.obsoletenotrebased)
395 self.obsoletenotrebased)
396 self.storestatus(tr=tr)
396 self.storestatus(tr=tr)
397 storecollapsemsg(repo, self.collapsemsg)
397 storecollapsemsg(repo, self.collapsemsg)
398 if len(repo[None].parents()) == 2:
398 if len(repo[None].parents()) == 2:
399 repo.ui.debug('resuming interrupted rebase\n')
399 repo.ui.debug('resuming interrupted rebase\n')
400 else:
400 else:
401 try:
401 try:
402 ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
402 ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
403 'rebase')
403 'rebase')
404 stats = rebasenode(repo, rev, p1, base, self.state,
404 stats = rebasenode(repo, rev, p1, base, self.state,
405 self.collapsef, self.target)
405 self.collapsef, self.target)
406 if stats and stats[3] > 0:
406 if stats and stats[3] > 0:
407 raise error.InterventionRequired(
407 raise error.InterventionRequired(
408 _('unresolved conflicts (see hg '
408 _('unresolved conflicts (see hg '
409 'resolve, then hg rebase --continue)'))
409 'resolve, then hg rebase --continue)'))
410 finally:
410 finally:
411 ui.setconfig('ui', 'forcemerge', '', 'rebase')
411 ui.setconfig('ui', 'forcemerge', '', 'rebase')
412 if not self.collapsef:
412 if not self.collapsef:
413 merging = p2 != nullrev
413 merging = p2 != nullrev
414 editform = cmdutil.mergeeditform(merging, 'rebase')
414 editform = cmdutil.mergeeditform(merging, 'rebase')
415 editor = cmdutil.getcommiteditor(editform=editform, **opts)
415 editor = cmdutil.getcommiteditor(editform=editform, **opts)
416 newnode = concludenode(repo, rev, p1, p2,
416 newnode = concludenode(repo, rev, p1, p2,
417 extrafn=_makeextrafn(self.extrafns),
417 extrafn=_makeextrafn(self.extrafns),
418 editor=editor,
418 editor=editor,
419 keepbranches=self.keepbranchesf,
419 keepbranches=self.keepbranchesf,
420 date=self.date)
420 date=self.date)
421 else:
421 else:
422 # Skip commit if we are collapsing
422 # Skip commit if we are collapsing
423 repo.dirstate.beginparentchange()
423 repo.dirstate.beginparentchange()
424 repo.setparents(repo[p1].node())
424 repo.setparents(repo[p1].node())
425 repo.dirstate.endparentchange()
425 repo.dirstate.endparentchange()
426 newnode = None
426 newnode = None
427 # Update the state
427 # Update the state
428 if newnode is not None:
428 if newnode is not None:
429 self.state[rev] = repo[newnode].rev()
429 self.state[rev] = repo[newnode].rev()
430 ui.debug('rebased as %s\n' % short(newnode))
430 ui.debug('rebased as %s\n' % short(newnode))
431 else:
431 else:
432 if not self.collapsef:
432 if not self.collapsef:
433 ui.warn(_('note: rebase of %d:%s created no changes '
433 ui.warn(_('note: rebase of %d:%s created no changes '
434 'to commit\n') % (rev, ctx))
434 'to commit\n') % (rev, ctx))
435 self.skipped.add(rev)
435 self.skipped.add(rev)
436 self.state[rev] = p1
436 self.state[rev] = p1
437 ui.debug('next revision set to %s\n' % p1)
437 ui.debug('next revision set to %s\n' % p1)
438 elif self.state[rev] == nullmerge:
438 elif self.state[rev] == nullmerge:
439 ui.debug('ignoring null merge rebase of %s\n' % rev)
439 ui.debug('ignoring null merge rebase of %s\n' % rev)
440 elif self.state[rev] == revignored:
440 elif self.state[rev] == revignored:
441 ui.status(_('not rebasing ignored %s\n') % desc)
441 ui.status(_('not rebasing ignored %s\n') % desc)
442 elif self.state[rev] == revprecursor:
442 elif self.state[rev] == revprecursor:
443 targetctx = repo[self.obsoletenotrebased[rev]]
443 targetctx = repo[self.obsoletenotrebased[rev]]
444 desctarget = '%d:%s "%s"' % (targetctx.rev(), targetctx,
444 desctarget = '%d:%s "%s"' % (targetctx.rev(), targetctx,
445 targetctx.description().split('\n', 1)[0])
445 targetctx.description().split('\n', 1)[0])
446 msg = _('note: not rebasing %s, already in destination as %s\n')
446 msg = _('note: not rebasing %s, already in destination as %s\n')
447 ui.status(msg % (desc, desctarget))
447 ui.status(msg % (desc, desctarget))
448 elif self.state[rev] == revpruned:
448 elif self.state[rev] == revpruned:
449 msg = _('note: not rebasing %s, it has no successor\n')
449 msg = _('note: not rebasing %s, it has no successor\n')
450 ui.status(msg % desc)
450 ui.status(msg % desc)
451 else:
451 else:
452 ui.status(_('already rebased %s as %s\n') %
452 ui.status(_('already rebased %s as %s\n') %
453 (desc, repo[self.state[rev]]))
453 (desc, repo[self.state[rev]]))
454
454
455 ui.progress(_('rebasing'), None)
455 ui.progress(_('rebasing'), None)
456 ui.note(_('rebase merging completed\n'))
456 ui.note(_('rebase merging completed\n'))
457
457
458 def _finishrebase(self):
458 def _finishrebase(self):
459 repo, ui, opts = self.repo, self.ui, self.opts
459 repo, ui, opts = self.repo, self.ui, self.opts
460 if self.collapsef and not self.keepopen:
460 if self.collapsef and not self.keepopen:
461 p1, p2, _base = defineparents(repo, min(self.state),
461 p1, p2, _base = defineparents(repo, min(self.state),
462 self.target, self.state,
462 self.target, self.state,
463 self.targetancestors,
463 self.targetancestors,
464 self.obsoletenotrebased)
464 self.obsoletenotrebased)
465 editopt = opts.get('edit')
465 editopt = opts.get('edit')
466 editform = 'rebase.collapse'
466 editform = 'rebase.collapse'
467 if self.collapsemsg:
467 if self.collapsemsg:
468 commitmsg = self.collapsemsg
468 commitmsg = self.collapsemsg
469 else:
469 else:
470 commitmsg = 'Collapsed revision'
470 commitmsg = 'Collapsed revision'
471 for rebased in self.state:
471 for rebased in self.state:
472 if rebased not in self.skipped and\
472 if rebased not in self.skipped and\
473 self.state[rebased] > nullmerge:
473 self.state[rebased] > nullmerge:
474 commitmsg += '\n* %s' % repo[rebased].description()
474 commitmsg += '\n* %s' % repo[rebased].description()
475 editopt = True
475 editopt = True
476 editor = cmdutil.getcommiteditor(edit=editopt, editform=editform)
476 editor = cmdutil.getcommiteditor(edit=editopt, editform=editform)
477 revtoreuse = max(self.state)
477 revtoreuse = max(self.state)
478 dsguard = dirstateguard.dirstateguard(repo, 'rebase')
478 dsguard = dirstateguard.dirstateguard(repo, 'rebase')
479 try:
479 try:
480 newnode = concludenode(repo, revtoreuse, p1, self.external,
480 newnode = concludenode(repo, revtoreuse, p1, self.external,
481 commitmsg=commitmsg,
481 commitmsg=commitmsg,
482 extrafn=_makeextrafn(self.extrafns),
482 extrafn=_makeextrafn(self.extrafns),
483 editor=editor,
483 editor=editor,
484 keepbranches=self.keepbranchesf,
484 keepbranches=self.keepbranchesf,
485 date=self.date)
485 date=self.date)
486 dsguard.close()
486 dsguard.close()
487 release(dsguard)
487 release(dsguard)
488 except error.InterventionRequired:
488 except error.InterventionRequired:
489 dsguard.close()
489 dsguard.close()
490 release(dsguard)
490 release(dsguard)
491 raise
491 raise
492 except Exception:
492 except Exception:
493 release(dsguard)
493 release(dsguard)
494 raise
494 raise
495
495
496 if newnode is None:
496 if newnode is None:
497 newrev = self.target
497 newrev = self.target
498 else:
498 else:
499 newrev = repo[newnode].rev()
499 newrev = repo[newnode].rev()
500 for oldrev in self.state.iterkeys():
500 for oldrev in self.state.iterkeys():
501 if self.state[oldrev] > nullmerge:
501 if self.state[oldrev] > nullmerge:
502 self.state[oldrev] = newrev
502 self.state[oldrev] = newrev
503
503
504 if 'qtip' in repo.tags():
504 if 'qtip' in repo.tags():
505 updatemq(repo, self.state, self.skipped, **opts)
505 updatemq(repo, self.state, self.skipped, **opts)
506
506
507 if self.currentbookmarks:
507 if self.currentbookmarks:
508 # Nodeids are needed to reset bookmarks
508 # Nodeids are needed to reset bookmarks
509 nstate = {}
509 nstate = {}
510 for k, v in self.state.iteritems():
510 for k, v in self.state.iteritems():
511 if v > nullmerge:
511 if v > nullmerge:
512 nstate[repo[k].node()] = repo[v].node()
512 nstate[repo[k].node()] = repo[v].node()
513 elif v == revprecursor:
513 elif v == revprecursor:
514 succ = self.obsoletenotrebased[k]
514 succ = self.obsoletenotrebased[k]
515 nstate[repo[k].node()] = repo[succ].node()
515 nstate[repo[k].node()] = repo[succ].node()
516 # XXX this is the same as dest.node() for the non-continue path --
516 # XXX this is the same as dest.node() for the non-continue path --
517 # this should probably be cleaned up
517 # this should probably be cleaned up
518 targetnode = repo[self.target].node()
518 targetnode = repo[self.target].node()
519
519
520 # restore original working directory
520 # restore original working directory
521 # (we do this before stripping)
521 # (we do this before stripping)
522 newwd = self.state.get(self.originalwd, self.originalwd)
522 newwd = self.state.get(self.originalwd, self.originalwd)
523 if newwd == revprecursor:
523 if newwd == revprecursor:
524 newwd = self.obsoletenotrebased[self.originalwd]
524 newwd = self.obsoletenotrebased[self.originalwd]
525 elif newwd < 0:
525 elif newwd < 0:
526 # original directory is a parent of rebase set root or ignored
526 # original directory is a parent of rebase set root or ignored
527 newwd = self.originalwd
527 newwd = self.originalwd
528 if newwd not in [c.rev() for c in repo[None].parents()]:
528 if newwd not in [c.rev() for c in repo[None].parents()]:
529 ui.note(_("update back to initial working directory parent\n"))
529 ui.note(_("update back to initial working directory parent\n"))
530 hg.updaterepo(repo, newwd, False)
530 hg.updaterepo(repo, newwd, False)
531
531
532 if self.currentbookmarks:
532 if self.currentbookmarks:
533 with repo.transaction('bookmark') as tr:
533 with repo.transaction('bookmark') as tr:
534 updatebookmarks(repo, targetnode, nstate,
534 updatebookmarks(repo, targetnode, nstate,
535 self.currentbookmarks, tr)
535 self.currentbookmarks, tr)
536 if self.activebookmark not in repo._bookmarks:
536 if self.activebookmark not in repo._bookmarks:
537 # active bookmark was divergent one and has been deleted
537 # active bookmark was divergent one and has been deleted
538 self.activebookmark = None
538 self.activebookmark = None
539
539
540 if not self.keepf:
540 if not self.keepf:
541 collapsedas = None
541 collapsedas = None
542 if self.collapsef:
542 if self.collapsef:
543 collapsedas = newnode
543 collapsedas = newnode
544 clearrebased(ui, repo, self.state, self.skipped, collapsedas)
544 clearrebased(ui, repo, self.state, self.skipped, collapsedas)
545
545
546 clearstatus(repo)
546 clearstatus(repo)
547 clearcollapsemsg(repo)
547 clearcollapsemsg(repo)
548
548
549 ui.note(_("rebase completed\n"))
549 ui.note(_("rebase completed\n"))
550 util.unlinkpath(repo.sjoin('undo'), ignoremissing=True)
550 util.unlinkpath(repo.sjoin('undo'), ignoremissing=True)
551 if self.skipped:
551 if self.skipped:
552 skippedlen = len(self.skipped)
552 skippedlen = len(self.skipped)
553 ui.note(_("%d revisions have been skipped\n") % skippedlen)
553 ui.note(_("%d revisions have been skipped\n") % skippedlen)
554
554
555 if (self.activebookmark and
555 if (self.activebookmark and
556 repo['.'].node() == repo._bookmarks[self.activebookmark]):
556 repo['.'].node() == repo._bookmarks[self.activebookmark]):
557 bookmarks.activate(repo, self.activebookmark)
557 bookmarks.activate(repo, self.activebookmark)
558
558
559 @command('rebase',
559 @command('rebase',
560 [('s', 'source', '',
560 [('s', 'source', '',
561 _('rebase the specified changeset and descendants'), _('REV')),
561 _('rebase the specified changeset and descendants'), _('REV')),
562 ('b', 'base', '',
562 ('b', 'base', '',
563 _('rebase everything from branching point of specified changeset'),
563 _('rebase everything from branching point of specified changeset'),
564 _('REV')),
564 _('REV')),
565 ('r', 'rev', [],
565 ('r', 'rev', [],
566 _('rebase these revisions'),
566 _('rebase these revisions'),
567 _('REV')),
567 _('REV')),
568 ('d', 'dest', '',
568 ('d', 'dest', '',
569 _('rebase onto the specified changeset'), _('REV')),
569 _('rebase onto the specified changeset'), _('REV')),
570 ('', 'collapse', False, _('collapse the rebased changesets')),
570 ('', 'collapse', False, _('collapse the rebased changesets')),
571 ('m', 'message', '',
571 ('m', 'message', '',
572 _('use text as collapse commit message'), _('TEXT')),
572 _('use text as collapse commit message'), _('TEXT')),
573 ('e', 'edit', False, _('invoke editor on commit messages')),
573 ('e', 'edit', False, _('invoke editor on commit messages')),
574 ('l', 'logfile', '',
574 ('l', 'logfile', '',
575 _('read collapse commit message from file'), _('FILE')),
575 _('read collapse commit message from file'), _('FILE')),
576 ('k', 'keep', False, _('keep original changesets')),
576 ('k', 'keep', False, _('keep original changesets')),
577 ('', 'keepbranches', False, _('keep original branch names')),
577 ('', 'keepbranches', False, _('keep original branch names')),
578 ('D', 'detach', False, _('(DEPRECATED)')),
578 ('D', 'detach', False, _('(DEPRECATED)')),
579 ('i', 'interactive', False, _('(DEPRECATED)')),
579 ('i', 'interactive', False, _('(DEPRECATED)')),
580 ('t', 'tool', '', _('specify merge tool')),
580 ('t', 'tool', '', _('specify merge tool')),
581 ('c', 'continue', False, _('continue an interrupted rebase')),
581 ('c', 'continue', False, _('continue an interrupted rebase')),
582 ('a', 'abort', False, _('abort an interrupted rebase'))] +
582 ('a', 'abort', False, _('abort an interrupted rebase'))] +
583 templateopts,
583 templateopts,
584 _('[-s REV | -b REV] [-d REV] [OPTION]'))
584 _('[-s REV | -b REV] [-d REV] [OPTION]'))
585 def rebase(ui, repo, **opts):
585 def rebase(ui, repo, **opts):
586 """move changeset (and descendants) to a different branch
586 """move changeset (and descendants) to a different branch
587
587
588 Rebase uses repeated merging to graft changesets from one part of
588 Rebase uses repeated merging to graft changesets from one part of
589 history (the source) onto another (the destination). This can be
589 history (the source) onto another (the destination). This can be
590 useful for linearizing *local* changes relative to a master
590 useful for linearizing *local* changes relative to a master
591 development tree.
591 development tree.
592
592
593 Published commits cannot be rebased (see :hg:`help phases`).
593 Published commits cannot be rebased (see :hg:`help phases`).
594 To copy commits, see :hg:`help graft`.
594 To copy commits, see :hg:`help graft`.
595
595
596 If you don't specify a destination changeset (``-d/--dest``), rebase
596 If you don't specify a destination changeset (``-d/--dest``), rebase
597 will use the same logic as :hg:`merge` to pick a destination. if
597 will use the same logic as :hg:`merge` to pick a destination. if
598 the current branch contains exactly one other head, the other head
598 the current branch contains exactly one other head, the other head
599 is merged with by default. Otherwise, an explicit revision with
599 is merged with by default. Otherwise, an explicit revision with
600 which to merge with must be provided. (destination changeset is not
600 which to merge with must be provided. (destination changeset is not
601 modified by rebasing, but new changesets are added as its
601 modified by rebasing, but new changesets are added as its
602 descendants.)
602 descendants.)
603
603
604 Here are the ways to select changesets:
604 Here are the ways to select changesets:
605
605
606 1. Explicitly select them using ``--rev``.
606 1. Explicitly select them using ``--rev``.
607
607
608 2. Use ``--source`` to select a root changeset and include all of its
608 2. Use ``--source`` to select a root changeset and include all of its
609 descendants.
609 descendants.
610
610
611 3. Use ``--base`` to select a changeset; rebase will find ancestors
611 3. Use ``--base`` to select a changeset; rebase will find ancestors
612 and their descendants which are not also ancestors of the destination.
612 and their descendants which are not also ancestors of the destination.
613
613
614 4. If you do not specify any of ``--rev``, ``source``, or ``--base``,
614 4. If you do not specify any of ``--rev``, ``source``, or ``--base``,
615 rebase will use ``--base .`` as above.
615 rebase will use ``--base .`` as above.
616
616
617 Rebase will destroy original changesets unless you use ``--keep``.
617 Rebase will destroy original changesets unless you use ``--keep``.
618 It will also move your bookmarks (even if you do).
618 It will also move your bookmarks (even if you do).
619
619
620 Some changesets may be dropped if they do not contribute changes
620 Some changesets may be dropped if they do not contribute changes
621 (e.g. merges from the destination branch).
621 (e.g. merges from the destination branch).
622
622
623 Unlike ``merge``, rebase will do nothing if you are at the branch tip of
623 Unlike ``merge``, rebase will do nothing if you are at the branch tip of
624 a named branch with two heads. You will need to explicitly specify source
624 a named branch with two heads. You will need to explicitly specify source
625 and/or destination.
625 and/or destination.
626
626
627 If you need to use a tool to automate merge/conflict decisions, you
627 If you need to use a tool to automate merge/conflict decisions, you
628 can specify one with ``--tool``, see :hg:`help merge-tools`.
628 can specify one with ``--tool``, see :hg:`help merge-tools`.
629 As a caveat: the tool will not be used to mediate when a file was
629 As a caveat: the tool will not be used to mediate when a file was
630 deleted, there is no hook presently available for this.
630 deleted, there is no hook presently available for this.
631
631
632 If a rebase is interrupted to manually resolve a conflict, it can be
632 If a rebase is interrupted to manually resolve a conflict, it can be
633 continued with --continue/-c or aborted with --abort/-a.
633 continued with --continue/-c or aborted with --abort/-a.
634
634
635 .. container:: verbose
635 .. container:: verbose
636
636
637 Examples:
637 Examples:
638
638
639 - move "local changes" (current commit back to branching point)
639 - move "local changes" (current commit back to branching point)
640 to the current branch tip after a pull::
640 to the current branch tip after a pull::
641
641
642 hg rebase
642 hg rebase
643
643
644 - move a single changeset to the stable branch::
644 - move a single changeset to the stable branch::
645
645
646 hg rebase -r 5f493448 -d stable
646 hg rebase -r 5f493448 -d stable
647
647
648 - splice a commit and all its descendants onto another part of history::
648 - splice a commit and all its descendants onto another part of history::
649
649
650 hg rebase --source c0c3 --dest 4cf9
650 hg rebase --source c0c3 --dest 4cf9
651
651
652 - rebase everything on a branch marked by a bookmark onto the
652 - rebase everything on a branch marked by a bookmark onto the
653 default branch::
653 default branch::
654
654
655 hg rebase --base myfeature --dest default
655 hg rebase --base myfeature --dest default
656
656
657 - collapse a sequence of changes into a single commit::
657 - collapse a sequence of changes into a single commit::
658
658
659 hg rebase --collapse -r 1520:1525 -d .
659 hg rebase --collapse -r 1520:1525 -d .
660
660
661 - move a named branch while preserving its name::
661 - move a named branch while preserving its name::
662
662
663 hg rebase -r "branch(featureX)" -d 1.3 --keepbranches
663 hg rebase -r "branch(featureX)" -d 1.3 --keepbranches
664
664
665 Configuration Options:
665 Configuration Options:
666
666
667 You can make rebase require a destination if you set the following config
667 You can make rebase require a destination if you set the following config
668 option::
668 option::
669
669
670 [commands]
670 [commands]
671 rebase.requiredest = True
671 rebase.requiredest = True
672
672
673 Return Values:
673 Return Values:
674
674
675 Returns 0 on success, 1 if nothing to rebase or there are
675 Returns 0 on success, 1 if nothing to rebase or there are
676 unresolved conflicts.
676 unresolved conflicts.
677
677
678 """
678 """
679 rbsrt = rebaseruntime(repo, ui, opts)
679 rbsrt = rebaseruntime(repo, ui, opts)
680
680
681 lock = wlock = None
681 lock = wlock = None
682 try:
682 try:
683 wlock = repo.wlock()
683 wlock = repo.wlock()
684 lock = repo.lock()
684 lock = repo.lock()
685
685
686 # Validate input and define rebasing points
686 # Validate input and define rebasing points
687 destf = opts.get('dest', None)
687 destf = opts.get('dest', None)
688 srcf = opts.get('source', None)
688 srcf = opts.get('source', None)
689 basef = opts.get('base', None)
689 basef = opts.get('base', None)
690 revf = opts.get('rev', [])
690 revf = opts.get('rev', [])
691 # search default destination in this space
691 # search default destination in this space
692 # used in the 'hg pull --rebase' case, see issue 5214.
692 # used in the 'hg pull --rebase' case, see issue 5214.
693 destspace = opts.get('_destspace')
693 destspace = opts.get('_destspace')
694 contf = opts.get('continue')
694 contf = opts.get('continue')
695 abortf = opts.get('abort')
695 abortf = opts.get('abort')
696 if opts.get('interactive'):
696 if opts.get('interactive'):
697 try:
697 try:
698 if extensions.find('histedit'):
698 if extensions.find('histedit'):
699 enablehistedit = ''
699 enablehistedit = ''
700 except KeyError:
700 except KeyError:
701 enablehistedit = " --config extensions.histedit="
701 enablehistedit = " --config extensions.histedit="
702 help = "hg%s help -e histedit" % enablehistedit
702 help = "hg%s help -e histedit" % enablehistedit
703 msg = _("interactive history editing is supported by the "
703 msg = _("interactive history editing is supported by the "
704 "'histedit' extension (see \"%s\")") % help
704 "'histedit' extension (see \"%s\")") % help
705 raise error.Abort(msg)
705 raise error.Abort(msg)
706
706
707 if rbsrt.collapsemsg and not rbsrt.collapsef:
707 if rbsrt.collapsemsg and not rbsrt.collapsef:
708 raise error.Abort(
708 raise error.Abort(
709 _('message can only be specified with collapse'))
709 _('message can only be specified with collapse'))
710
710
711 if contf or abortf:
711 if contf or abortf:
712 if contf and abortf:
712 if contf and abortf:
713 raise error.Abort(_('cannot use both abort and continue'))
713 raise error.Abort(_('cannot use both abort and continue'))
714 if rbsrt.collapsef:
714 if rbsrt.collapsef:
715 raise error.Abort(
715 raise error.Abort(
716 _('cannot use collapse with continue or abort'))
716 _('cannot use collapse with continue or abort'))
717 if srcf or basef or destf:
717 if srcf or basef or destf:
718 raise error.Abort(
718 raise error.Abort(
719 _('abort and continue do not allow specifying revisions'))
719 _('abort and continue do not allow specifying revisions'))
720 if abortf and opts.get('tool', False):
720 if abortf and opts.get('tool', False):
721 ui.warn(_('tool option will be ignored\n'))
721 ui.warn(_('tool option will be ignored\n'))
722 if contf:
722 if contf:
723 ms = mergemod.mergestate.read(repo)
723 ms = mergemod.mergestate.read(repo)
724 mergeutil.checkunresolved(ms)
724 mergeutil.checkunresolved(ms)
725
725
726 retcode = rbsrt._prepareabortorcontinue(abortf)
726 retcode = rbsrt._prepareabortorcontinue(abortf)
727 if retcode is not None:
727 if retcode is not None:
728 return retcode
728 return retcode
729 else:
729 else:
730 dest, rebaseset = _definesets(ui, repo, destf, srcf, basef, revf,
730 dest, rebaseset = _definesets(ui, repo, destf, srcf, basef, revf,
731 destspace=destspace)
731 destspace=destspace)
732 retcode = rbsrt._preparenewrebase(dest, rebaseset)
732 retcode = rbsrt._preparenewrebase(dest, rebaseset)
733 if retcode is not None:
733 if retcode is not None:
734 return retcode
734 return retcode
735
735
736 with repo.transaction('rebase') as tr:
736 with repo.transaction('rebase') as tr:
737 dsguard = dirstateguard.dirstateguard(repo, 'rebase')
737 dsguard = dirstateguard.dirstateguard(repo, 'rebase')
738 try:
738 try:
739 rbsrt._performrebase(tr)
739 rbsrt._performrebase(tr)
740 dsguard.close()
740 dsguard.close()
741 release(dsguard)
741 release(dsguard)
742 except error.InterventionRequired:
742 except error.InterventionRequired:
743 dsguard.close()
743 dsguard.close()
744 release(dsguard)
744 release(dsguard)
745 tr.close()
745 tr.close()
746 raise
746 raise
747 except Exception:
747 except Exception:
748 release(dsguard)
748 release(dsguard)
749 raise
749 raise
750 rbsrt._finishrebase()
750 rbsrt._finishrebase()
751 finally:
751 finally:
752 release(lock, wlock)
752 release(lock, wlock)
753
753
754 def _definesets(ui, repo, destf=None, srcf=None, basef=None, revf=None,
754 def _definesets(ui, repo, destf=None, srcf=None, basef=None, revf=None,
755 destspace=None):
755 destspace=None):
756 """use revisions argument to define destination and rebase set
756 """use revisions argument to define destination and rebase set
757 """
757 """
758 if revf is None:
758 if revf is None:
759 revf = []
759 revf = []
760
760
761 # destspace is here to work around issues with `hg pull --rebase` see
761 # destspace is here to work around issues with `hg pull --rebase` see
762 # issue5214 for details
762 # issue5214 for details
763 if srcf and basef:
763 if srcf and basef:
764 raise error.Abort(_('cannot specify both a source and a base'))
764 raise error.Abort(_('cannot specify both a source and a base'))
765 if revf and basef:
765 if revf and basef:
766 raise error.Abort(_('cannot specify both a revision and a base'))
766 raise error.Abort(_('cannot specify both a revision and a base'))
767 if revf and srcf:
767 if revf and srcf:
768 raise error.Abort(_('cannot specify both a revision and a source'))
768 raise error.Abort(_('cannot specify both a revision and a source'))
769
769
770 cmdutil.checkunfinished(repo)
770 cmdutil.checkunfinished(repo)
771 cmdutil.bailifchanged(repo)
771 cmdutil.bailifchanged(repo)
772
772
773 if ui.configbool('commands', 'rebase.requiredest') and not destf:
773 if ui.configbool('commands', 'rebase.requiredest') and not destf:
774 raise error.Abort(_('you must specify a destination'),
774 raise error.Abort(_('you must specify a destination'),
775 hint=_('use: hg rebase -d REV'))
775 hint=_('use: hg rebase -d REV'))
776
776
777 if destf:
777 if destf:
778 dest = scmutil.revsingle(repo, destf)
778 dest = scmutil.revsingle(repo, destf)
779
779
780 if revf:
780 if revf:
781 rebaseset = scmutil.revrange(repo, revf)
781 rebaseset = scmutil.revrange(repo, revf)
782 if not rebaseset:
782 if not rebaseset:
783 ui.status(_('empty "rev" revision set - nothing to rebase\n'))
783 ui.status(_('empty "rev" revision set - nothing to rebase\n'))
784 return None, None
784 return None, None
785 elif srcf:
785 elif srcf:
786 src = scmutil.revrange(repo, [srcf])
786 src = scmutil.revrange(repo, [srcf])
787 if not src:
787 if not src:
788 ui.status(_('empty "source" revision set - nothing to rebase\n'))
788 ui.status(_('empty "source" revision set - nothing to rebase\n'))
789 return None, None
789 return None, None
790 rebaseset = repo.revs('(%ld)::', src)
790 rebaseset = repo.revs('(%ld)::', src)
791 assert rebaseset
791 assert rebaseset
792 else:
792 else:
793 base = scmutil.revrange(repo, [basef or '.'])
793 base = scmutil.revrange(repo, [basef or '.'])
794 if not base:
794 if not base:
795 ui.status(_('empty "base" revision set - '
795 ui.status(_('empty "base" revision set - '
796 "can't compute rebase set\n"))
796 "can't compute rebase set\n"))
797 return None, None
797 return None, None
798 if not destf:
798 if not destf:
799 dest = repo[_destrebase(repo, base, destspace=destspace)]
799 dest = repo[_destrebase(repo, base, destspace=destspace)]
800 destf = str(dest)
800 destf = str(dest)
801
801
802 roots = [] # selected children of branching points
802 roots = [] # selected children of branching points
803 bpbase = {} # {branchingpoint: [origbase]}
803 bpbase = {} # {branchingpoint: [origbase]}
804 for b in base: # group bases by branching points
804 for b in base: # group bases by branching points
805 bp = repo.revs('ancestor(%d, %d)', b, dest).first()
805 bp = repo.revs('ancestor(%d, %d)', b, dest).first()
806 bpbase[bp] = bpbase.get(bp, []) + [b]
806 bpbase[bp] = bpbase.get(bp, []) + [b]
807 if None in bpbase:
807 if None in bpbase:
808 # emulate the old behavior, showing "nothing to rebase" (a better
808 # emulate the old behavior, showing "nothing to rebase" (a better
809 # behavior may be abort with "cannot find branching point" error)
809 # behavior may be abort with "cannot find branching point" error)
810 bpbase.clear()
810 bpbase.clear()
811 for bp, bs in bpbase.iteritems(): # calculate roots
811 for bp, bs in bpbase.iteritems(): # calculate roots
812 roots += list(repo.revs('children(%d) & ancestors(%ld)', bp, bs))
812 roots += list(repo.revs('children(%d) & ancestors(%ld)', bp, bs))
813
813
814 rebaseset = repo.revs('%ld::', roots)
814 rebaseset = repo.revs('%ld::', roots)
815
815
816 if not rebaseset:
816 if not rebaseset:
817 # transform to list because smartsets are not comparable to
817 # transform to list because smartsets are not comparable to
818 # lists. This should be improved to honor laziness of
818 # lists. This should be improved to honor laziness of
819 # smartset.
819 # smartset.
820 if list(base) == [dest.rev()]:
820 if list(base) == [dest.rev()]:
821 if basef:
821 if basef:
822 ui.status(_('nothing to rebase - %s is both "base"'
822 ui.status(_('nothing to rebase - %s is both "base"'
823 ' and destination\n') % dest)
823 ' and destination\n') % dest)
824 else:
824 else:
825 ui.status(_('nothing to rebase - working directory '
825 ui.status(_('nothing to rebase - working directory '
826 'parent is also destination\n'))
826 'parent is also destination\n'))
827 elif not repo.revs('%ld - ::%d', base, dest):
827 elif not repo.revs('%ld - ::%d', base, dest):
828 if basef:
828 if basef:
829 ui.status(_('nothing to rebase - "base" %s is '
829 ui.status(_('nothing to rebase - "base" %s is '
830 'already an ancestor of destination '
830 'already an ancestor of destination '
831 '%s\n') %
831 '%s\n') %
832 ('+'.join(str(repo[r]) for r in base),
832 ('+'.join(str(repo[r]) for r in base),
833 dest))
833 dest))
834 else:
834 else:
835 ui.status(_('nothing to rebase - working '
835 ui.status(_('nothing to rebase - working '
836 'directory parent is already an '
836 'directory parent is already an '
837 'ancestor of destination %s\n') % dest)
837 'ancestor of destination %s\n') % dest)
838 else: # can it happen?
838 else: # can it happen?
839 ui.status(_('nothing to rebase from %s to %s\n') %
839 ui.status(_('nothing to rebase from %s to %s\n') %
840 ('+'.join(str(repo[r]) for r in base), dest))
840 ('+'.join(str(repo[r]) for r in base), dest))
841 return None, None
841 return None, None
842
842
843 if not destf:
843 if not destf:
844 dest = repo[_destrebase(repo, rebaseset, destspace=destspace)]
844 dest = repo[_destrebase(repo, rebaseset, destspace=destspace)]
845 destf = str(dest)
845 destf = str(dest)
846
846
847 return dest, rebaseset
847 return dest, rebaseset
848
848
849 def externalparent(repo, state, targetancestors):
849 def externalparent(repo, state, targetancestors):
850 """Return the revision that should be used as the second parent
850 """Return the revision that should be used as the second parent
851 when the revisions in state is collapsed on top of targetancestors.
851 when the revisions in state is collapsed on top of targetancestors.
852 Abort if there is more than one parent.
852 Abort if there is more than one parent.
853 """
853 """
854 parents = set()
854 parents = set()
855 source = min(state)
855 source = min(state)
856 for rev in state:
856 for rev in state:
857 if rev == source:
857 if rev == source:
858 continue
858 continue
859 for p in repo[rev].parents():
859 for p in repo[rev].parents():
860 if (p.rev() not in state
860 if (p.rev() not in state
861 and p.rev() not in targetancestors):
861 and p.rev() not in targetancestors):
862 parents.add(p.rev())
862 parents.add(p.rev())
863 if not parents:
863 if not parents:
864 return nullrev
864 return nullrev
865 if len(parents) == 1:
865 if len(parents) == 1:
866 return parents.pop()
866 return parents.pop()
867 raise error.Abort(_('unable to collapse on top of %s, there is more '
867 raise error.Abort(_('unable to collapse on top of %s, there is more '
868 'than one external parent: %s') %
868 'than one external parent: %s') %
869 (max(targetancestors),
869 (max(targetancestors),
870 ', '.join(str(p) for p in sorted(parents))))
870 ', '.join(str(p) for p in sorted(parents))))
871
871
872 def concludenode(repo, rev, p1, p2, commitmsg=None, editor=None, extrafn=None,
872 def concludenode(repo, rev, p1, p2, commitmsg=None, editor=None, extrafn=None,
873 keepbranches=False, date=None):
873 keepbranches=False, date=None):
874 '''Commit the wd changes with parents p1 and p2. Reuse commit info from rev
874 '''Commit the wd changes with parents p1 and p2. Reuse commit info from rev
875 but also store useful information in extra.
875 but also store useful information in extra.
876 Return node of committed revision.'''
876 Return node of committed revision.'''
877 repo.setparents(repo[p1].node(), repo[p2].node())
877 repo.setparents(repo[p1].node(), repo[p2].node())
878 ctx = repo[rev]
878 ctx = repo[rev]
879 if commitmsg is None:
879 if commitmsg is None:
880 commitmsg = ctx.description()
880 commitmsg = ctx.description()
881 keepbranch = keepbranches and repo[p1].branch() != ctx.branch()
881 keepbranch = keepbranches and repo[p1].branch() != ctx.branch()
882 extra = {'rebase_source': ctx.hex()}
882 extra = {'rebase_source': ctx.hex()}
883 if extrafn:
883 if extrafn:
884 extrafn(ctx, extra)
884 extrafn(ctx, extra)
885
885
886 targetphase = max(ctx.phase(), phases.draft)
886 targetphase = max(ctx.phase(), phases.draft)
887 overrides = {('phases', 'new-commit'): targetphase}
887 overrides = {('phases', 'new-commit'): targetphase}
888 with repo.ui.configoverride(overrides, 'rebase'):
888 with repo.ui.configoverride(overrides, 'rebase'):
889 if keepbranch:
889 if keepbranch:
890 repo.ui.setconfig('ui', 'allowemptycommit', True)
890 repo.ui.setconfig('ui', 'allowemptycommit', True)
891 # Commit might fail if unresolved files exist
891 # Commit might fail if unresolved files exist
892 if date is None:
892 if date is None:
893 date = ctx.date()
893 date = ctx.date()
894 newnode = repo.commit(text=commitmsg, user=ctx.user(),
894 newnode = repo.commit(text=commitmsg, user=ctx.user(),
895 date=date, extra=extra, editor=editor)
895 date=date, extra=extra, editor=editor)
896
896
897 repo.dirstate.setbranch(repo[newnode].branch())
897 repo.dirstate.setbranch(repo[newnode].branch())
898 return newnode
898 return newnode
899
899
900 def rebasenode(repo, rev, p1, base, state, collapse, target):
900 def rebasenode(repo, rev, p1, base, state, collapse, target):
901 'Rebase a single revision rev on top of p1 using base as merge ancestor'
901 'Rebase a single revision rev on top of p1 using base as merge ancestor'
902 # Merge phase
902 # Merge phase
903 # Update to target and merge it with local
903 # Update to target and merge it with local
904 if repo['.'].rev() != p1:
904 if repo['.'].rev() != p1:
905 repo.ui.debug(" update to %d:%s\n" % (p1, repo[p1]))
905 repo.ui.debug(" update to %d:%s\n" % (p1, repo[p1]))
906 mergemod.update(repo, p1, False, True)
906 mergemod.update(repo, p1, False, True)
907 else:
907 else:
908 repo.ui.debug(" already in target\n")
908 repo.ui.debug(" already in target\n")
909 repo.dirstate.write(repo.currenttransaction())
909 repo.dirstate.write(repo.currenttransaction())
910 repo.ui.debug(" merge against %d:%s\n" % (rev, repo[rev]))
910 repo.ui.debug(" merge against %d:%s\n" % (rev, repo[rev]))
911 if base is not None:
911 if base is not None:
912 repo.ui.debug(" detach base %d:%s\n" % (base, repo[base]))
912 repo.ui.debug(" detach base %d:%s\n" % (base, repo[base]))
913 # When collapsing in-place, the parent is the common ancestor, we
913 # When collapsing in-place, the parent is the common ancestor, we
914 # have to allow merging with it.
914 # have to allow merging with it.
915 stats = mergemod.update(repo, rev, True, True, base, collapse,
915 stats = mergemod.update(repo, rev, True, True, base, collapse,
916 labels=['dest', 'source'])
916 labels=['dest', 'source'])
917 if collapse:
917 if collapse:
918 copies.duplicatecopies(repo, rev, target)
918 copies.duplicatecopies(repo, rev, target)
919 else:
919 else:
920 # If we're not using --collapse, we need to
920 # If we're not using --collapse, we need to
921 # duplicate copies between the revision we're
921 # duplicate copies between the revision we're
922 # rebasing and its first parent, but *not*
922 # rebasing and its first parent, but *not*
923 # duplicate any copies that have already been
923 # duplicate any copies that have already been
924 # performed in the destination.
924 # performed in the destination.
925 p1rev = repo[rev].p1().rev()
925 p1rev = repo[rev].p1().rev()
926 copies.duplicatecopies(repo, rev, p1rev, skiprev=target)
926 copies.duplicatecopies(repo, rev, p1rev, skiprev=target)
927 return stats
927 return stats
928
928
929 def nearestrebased(repo, rev, state):
929 def nearestrebased(repo, rev, state):
930 """return the nearest ancestors of rev in the rebase result"""
930 """return the nearest ancestors of rev in the rebase result"""
931 rebased = [r for r in state if state[r] > nullmerge]
931 rebased = [r for r in state if state[r] > nullmerge]
932 candidates = repo.revs('max(%ld and (::%d))', rebased, rev)
932 candidates = repo.revs('max(%ld and (::%d))', rebased, rev)
933 if candidates:
933 if candidates:
934 return state[candidates.first()]
934 return state[candidates.first()]
935 else:
935 else:
936 return None
936 return None
937
937
938 def _checkobsrebase(repo, ui, rebaseobsrevs, rebasesetrevs, rebaseobsskipped):
938 def _checkobsrebase(repo, ui, rebaseobsrevs, rebasesetrevs, rebaseobsskipped):
939 """
939 """
940 Abort if rebase will create divergence or rebase is noop because of markers
940 Abort if rebase will create divergence or rebase is noop because of markers
941
941
942 `rebaseobsrevs`: set of obsolete revision in source
942 `rebaseobsrevs`: set of obsolete revision in source
943 `rebasesetrevs`: set of revisions to be rebased from source
943 `rebasesetrevs`: set of revisions to be rebased from source
944 `rebaseobsskipped`: set of revisions from source skipped because they have
944 `rebaseobsskipped`: set of revisions from source skipped because they have
945 successors in destination
945 successors in destination
946 """
946 """
947 # Obsolete node with successors not in dest leads to divergence
947 # Obsolete node with successors not in dest leads to divergence
948 divergenceok = ui.configbool('experimental',
948 divergenceok = ui.configbool('experimental',
949 'allowdivergence')
949 'allowdivergence')
950 divergencebasecandidates = rebaseobsrevs - rebaseobsskipped
950 divergencebasecandidates = rebaseobsrevs - rebaseobsskipped
951
951
952 if divergencebasecandidates and not divergenceok:
952 if divergencebasecandidates and not divergenceok:
953 divhashes = (str(repo[r])
953 divhashes = (str(repo[r])
954 for r in divergencebasecandidates)
954 for r in divergencebasecandidates)
955 msg = _("this rebase will cause "
955 msg = _("this rebase will cause "
956 "divergences from: %s")
956 "divergences from: %s")
957 h = _("to force the rebase please set "
957 h = _("to force the rebase please set "
958 "experimental.allowdivergence=True")
958 "experimental.allowdivergence=True")
959 raise error.Abort(msg % (",".join(divhashes),), hint=h)
959 raise error.Abort(msg % (",".join(divhashes),), hint=h)
960
960
961 def defineparents(repo, rev, target, state, targetancestors,
961 def defineparents(repo, rev, target, state, targetancestors,
962 obsoletenotrebased):
962 obsoletenotrebased):
963 'Return the new parent relationship of the revision that will be rebased'
963 'Return the new parent relationship of the revision that will be rebased'
964 parents = repo[rev].parents()
964 parents = repo[rev].parents()
965 p1 = p2 = nullrev
965 p1 = p2 = nullrev
966 rp1 = None
966 rp1 = None
967
967
968 p1n = parents[0].rev()
968 p1n = parents[0].rev()
969 if p1n in targetancestors:
969 if p1n in targetancestors:
970 p1 = target
970 p1 = target
971 elif p1n in state:
971 elif p1n in state:
972 if state[p1n] == nullmerge:
972 if state[p1n] == nullmerge:
973 p1 = target
973 p1 = target
974 elif state[p1n] in revskipped:
974 elif state[p1n] in revskipped:
975 p1 = nearestrebased(repo, p1n, state)
975 p1 = nearestrebased(repo, p1n, state)
976 if p1 is None:
976 if p1 is None:
977 p1 = target
977 p1 = target
978 else:
978 else:
979 p1 = state[p1n]
979 p1 = state[p1n]
980 else: # p1n external
980 else: # p1n external
981 p1 = target
981 p1 = target
982 p2 = p1n
982 p2 = p1n
983
983
984 if len(parents) == 2 and parents[1].rev() not in targetancestors:
984 if len(parents) == 2 and parents[1].rev() not in targetancestors:
985 p2n = parents[1].rev()
985 p2n = parents[1].rev()
986 # interesting second parent
986 # interesting second parent
987 if p2n in state:
987 if p2n in state:
988 if p1 == target: # p1n in targetancestors or external
988 if p1 == target: # p1n in targetancestors or external
989 p1 = state[p2n]
989 p1 = state[p2n]
990 if p1 == revprecursor:
990 if p1 == revprecursor:
991 rp1 = obsoletenotrebased[p2n]
991 rp1 = obsoletenotrebased[p2n]
992 elif state[p2n] in revskipped:
992 elif state[p2n] in revskipped:
993 p2 = nearestrebased(repo, p2n, state)
993 p2 = nearestrebased(repo, p2n, state)
994 if p2 is None:
994 if p2 is None:
995 # no ancestors rebased yet, detach
995 # no ancestors rebased yet, detach
996 p2 = target
996 p2 = target
997 else:
997 else:
998 p2 = state[p2n]
998 p2 = state[p2n]
999 else: # p2n external
999 else: # p2n external
1000 if p2 != nullrev: # p1n external too => rev is a merged revision
1000 if p2 != nullrev: # p1n external too => rev is a merged revision
1001 raise error.Abort(_('cannot use revision %d as base, result '
1001 raise error.Abort(_('cannot use revision %d as base, result '
1002 'would have 3 parents') % rev)
1002 'would have 3 parents') % rev)
1003 p2 = p2n
1003 p2 = p2n
1004 repo.ui.debug(" future parents are %d and %d\n" %
1004 repo.ui.debug(" future parents are %d and %d\n" %
1005 (repo[rp1 or p1].rev(), repo[p2].rev()))
1005 (repo[rp1 or p1].rev(), repo[p2].rev()))
1006
1006
1007 if not any(p.rev() in state for p in parents):
1007 if not any(p.rev() in state for p in parents):
1008 # Case (1) root changeset of a non-detaching rebase set.
1008 # Case (1) root changeset of a non-detaching rebase set.
1009 # Let the merge mechanism find the base itself.
1009 # Let the merge mechanism find the base itself.
1010 base = None
1010 base = None
1011 elif not repo[rev].p2():
1011 elif not repo[rev].p2():
1012 # Case (2) detaching the node with a single parent, use this parent
1012 # Case (2) detaching the node with a single parent, use this parent
1013 base = repo[rev].p1().rev()
1013 base = repo[rev].p1().rev()
1014 else:
1014 else:
1015 # Assuming there is a p1, this is the case where there also is a p2.
1015 # Assuming there is a p1, this is the case where there also is a p2.
1016 # We are thus rebasing a merge and need to pick the right merge base.
1016 # We are thus rebasing a merge and need to pick the right merge base.
1017 #
1017 #
1018 # Imagine we have:
1018 # Imagine we have:
1019 # - M: current rebase revision in this step
1019 # - M: current rebase revision in this step
1020 # - A: one parent of M
1020 # - A: one parent of M
1021 # - B: other parent of M
1021 # - B: other parent of M
1022 # - D: destination of this merge step (p1 var)
1022 # - D: destination of this merge step (p1 var)
1023 #
1023 #
1024 # Consider the case where D is a descendant of A or B and the other is
1024 # Consider the case where D is a descendant of A or B and the other is
1025 # 'outside'. In this case, the right merge base is the D ancestor.
1025 # 'outside'. In this case, the right merge base is the D ancestor.
1026 #
1026 #
1027 # An informal proof, assuming A is 'outside' and B is the D ancestor:
1027 # An informal proof, assuming A is 'outside' and B is the D ancestor:
1028 #
1028 #
1029 # If we pick B as the base, the merge involves:
1029 # If we pick B as the base, the merge involves:
1030 # - changes from B to M (actual changeset payload)
1030 # - changes from B to M (actual changeset payload)
1031 # - changes from B to D (induced by rebase) as D is a rebased
1031 # - changes from B to D (induced by rebase) as D is a rebased
1032 # version of B)
1032 # version of B)
1033 # Which exactly represent the rebase operation.
1033 # Which exactly represent the rebase operation.
1034 #
1034 #
1035 # If we pick A as the base, the merge involves:
1035 # If we pick A as the base, the merge involves:
1036 # - changes from A to M (actual changeset payload)
1036 # - changes from A to M (actual changeset payload)
1037 # - changes from A to D (with include changes between unrelated A and B
1037 # - changes from A to D (with include changes between unrelated A and B
1038 # plus changes induced by rebase)
1038 # plus changes induced by rebase)
1039 # Which does not represent anything sensible and creates a lot of
1039 # Which does not represent anything sensible and creates a lot of
1040 # conflicts. A is thus not the right choice - B is.
1040 # conflicts. A is thus not the right choice - B is.
1041 #
1041 #
1042 # Note: The base found in this 'proof' is only correct in the specified
1042 # Note: The base found in this 'proof' is only correct in the specified
1043 # case. This base does not make sense if is not D a descendant of A or B
1043 # case. This base does not make sense if is not D a descendant of A or B
1044 # or if the other is not parent 'outside' (especially not if the other
1044 # or if the other is not parent 'outside' (especially not if the other
1045 # parent has been rebased). The current implementation does not
1045 # parent has been rebased). The current implementation does not
1046 # make it feasible to consider different cases separately. In these
1046 # make it feasible to consider different cases separately. In these
1047 # other cases we currently just leave it to the user to correctly
1047 # other cases we currently just leave it to the user to correctly
1048 # resolve an impossible merge using a wrong ancestor.
1048 # resolve an impossible merge using a wrong ancestor.
1049 #
1049 #
1050 # xx, p1 could be -4, and both parents could probably be -4...
1050 # xx, p1 could be -4, and both parents could probably be -4...
1051 for p in repo[rev].parents():
1051 for p in repo[rev].parents():
1052 if state.get(p.rev()) == p1:
1052 if state.get(p.rev()) == p1:
1053 base = p.rev()
1053 base = p.rev()
1054 break
1054 break
1055 else: # fallback when base not found
1055 else: # fallback when base not found
1056 base = None
1056 base = None
1057
1057
1058 # Raise because this function is called wrong (see issue 4106)
1058 # Raise because this function is called wrong (see issue 4106)
1059 raise AssertionError('no base found to rebase on '
1059 raise AssertionError('no base found to rebase on '
1060 '(defineparents called wrong)')
1060 '(defineparents called wrong)')
1061 return rp1 or p1, p2, base
1061 return rp1 or p1, p2, base
1062
1062
1063 def isagitpatch(repo, patchname):
1063 def isagitpatch(repo, patchname):
1064 'Return true if the given patch is in git format'
1064 'Return true if the given patch is in git format'
1065 mqpatch = os.path.join(repo.mq.path, patchname)
1065 mqpatch = os.path.join(repo.mq.path, patchname)
1066 for line in patch.linereader(file(mqpatch, 'rb')):
1066 for line in patch.linereader(file(mqpatch, 'rb')):
1067 if line.startswith('diff --git'):
1067 if line.startswith('diff --git'):
1068 return True
1068 return True
1069 return False
1069 return False
1070
1070
1071 def updatemq(repo, state, skipped, **opts):
1071 def updatemq(repo, state, skipped, **opts):
1072 'Update rebased mq patches - finalize and then import them'
1072 'Update rebased mq patches - finalize and then import them'
1073 mqrebase = {}
1073 mqrebase = {}
1074 mq = repo.mq
1074 mq = repo.mq
1075 original_series = mq.fullseries[:]
1075 original_series = mq.fullseries[:]
1076 skippedpatches = set()
1076 skippedpatches = set()
1077
1077
1078 for p in mq.applied:
1078 for p in mq.applied:
1079 rev = repo[p.node].rev()
1079 rev = repo[p.node].rev()
1080 if rev in state:
1080 if rev in state:
1081 repo.ui.debug('revision %d is an mq patch (%s), finalize it.\n' %
1081 repo.ui.debug('revision %d is an mq patch (%s), finalize it.\n' %
1082 (rev, p.name))
1082 (rev, p.name))
1083 mqrebase[rev] = (p.name, isagitpatch(repo, p.name))
1083 mqrebase[rev] = (p.name, isagitpatch(repo, p.name))
1084 else:
1084 else:
1085 # Applied but not rebased, not sure this should happen
1085 # Applied but not rebased, not sure this should happen
1086 skippedpatches.add(p.name)
1086 skippedpatches.add(p.name)
1087
1087
1088 if mqrebase:
1088 if mqrebase:
1089 mq.finish(repo, mqrebase.keys())
1089 mq.finish(repo, mqrebase.keys())
1090
1090
1091 # We must start import from the newest revision
1091 # We must start import from the newest revision
1092 for rev in sorted(mqrebase, reverse=True):
1092 for rev in sorted(mqrebase, reverse=True):
1093 if rev not in skipped:
1093 if rev not in skipped:
1094 name, isgit = mqrebase[rev]
1094 name, isgit = mqrebase[rev]
1095 repo.ui.note(_('updating mq patch %s to %s:%s\n') %
1095 repo.ui.note(_('updating mq patch %s to %s:%s\n') %
1096 (name, state[rev], repo[state[rev]]))
1096 (name, state[rev], repo[state[rev]]))
1097 mq.qimport(repo, (), patchname=name, git=isgit,
1097 mq.qimport(repo, (), patchname=name, git=isgit,
1098 rev=[str(state[rev])])
1098 rev=[str(state[rev])])
1099 else:
1099 else:
1100 # Rebased and skipped
1100 # Rebased and skipped
1101 skippedpatches.add(mqrebase[rev][0])
1101 skippedpatches.add(mqrebase[rev][0])
1102
1102
1103 # Patches were either applied and rebased and imported in
1103 # Patches were either applied and rebased and imported in
1104 # order, applied and removed or unapplied. Discard the removed
1104 # order, applied and removed or unapplied. Discard the removed
1105 # ones while preserving the original series order and guards.
1105 # ones while preserving the original series order and guards.
1106 newseries = [s for s in original_series
1106 newseries = [s for s in original_series
1107 if mq.guard_re.split(s, 1)[0] not in skippedpatches]
1107 if mq.guard_re.split(s, 1)[0] not in skippedpatches]
1108 mq.fullseries[:] = newseries
1108 mq.fullseries[:] = newseries
1109 mq.seriesdirty = True
1109 mq.seriesdirty = True
1110 mq.savedirty()
1110 mq.savedirty()
1111
1111
1112 def updatebookmarks(repo, targetnode, nstate, originalbookmarks, tr):
1112 def updatebookmarks(repo, targetnode, nstate, originalbookmarks, tr):
1113 'Move bookmarks to their correct changesets, and delete divergent ones'
1113 'Move bookmarks to their correct changesets, and delete divergent ones'
1114 marks = repo._bookmarks
1114 marks = repo._bookmarks
1115 for k, v in originalbookmarks.iteritems():
1115 for k, v in originalbookmarks.iteritems():
1116 if v in nstate:
1116 if v in nstate:
1117 # update the bookmarks for revs that have moved
1117 # update the bookmarks for revs that have moved
1118 marks[k] = nstate[v]
1118 marks[k] = nstate[v]
1119 bookmarks.deletedivergent(repo, [targetnode], k)
1119 bookmarks.deletedivergent(repo, [targetnode], k)
1120 marks.recordchange(tr)
1120 marks.recordchange(tr)
1121
1121
1122 def storecollapsemsg(repo, collapsemsg):
1122 def storecollapsemsg(repo, collapsemsg):
1123 'Store the collapse message to allow recovery'
1123 'Store the collapse message to allow recovery'
1124 collapsemsg = collapsemsg or ''
1124 collapsemsg = collapsemsg or ''
1125 f = repo.vfs("last-message.txt", "w")
1125 f = repo.vfs("last-message.txt", "w")
1126 f.write("%s\n" % collapsemsg)
1126 f.write("%s\n" % collapsemsg)
1127 f.close()
1127 f.close()
1128
1128
1129 def clearcollapsemsg(repo):
1129 def clearcollapsemsg(repo):
1130 'Remove collapse message file'
1130 'Remove collapse message file'
1131 repo.vfs.unlinkpath("last-message.txt", ignoremissing=True)
1131 repo.vfs.unlinkpath("last-message.txt", ignoremissing=True)
1132
1132
1133 def restorecollapsemsg(repo, isabort):
1133 def restorecollapsemsg(repo, isabort):
1134 'Restore previously stored collapse message'
1134 'Restore previously stored collapse message'
1135 try:
1135 try:
1136 f = repo.vfs("last-message.txt")
1136 f = repo.vfs("last-message.txt")
1137 collapsemsg = f.readline().strip()
1137 collapsemsg = f.readline().strip()
1138 f.close()
1138 f.close()
1139 except IOError as err:
1139 except IOError as err:
1140 if err.errno != errno.ENOENT:
1140 if err.errno != errno.ENOENT:
1141 raise
1141 raise
1142 if isabort:
1142 if isabort:
1143 # Oh well, just abort like normal
1143 # Oh well, just abort like normal
1144 collapsemsg = ''
1144 collapsemsg = ''
1145 else:
1145 else:
1146 raise error.Abort(_('missing .hg/last-message.txt for rebase'))
1146 raise error.Abort(_('missing .hg/last-message.txt for rebase'))
1147 return collapsemsg
1147 return collapsemsg
1148
1148
1149 def clearstatus(repo):
1149 def clearstatus(repo):
1150 'Remove the status files'
1150 'Remove the status files'
1151 _clearrebasesetvisibiliy(repo)
1151 _clearrebasesetvisibiliy(repo)
1152 repo.vfs.unlinkpath("rebasestate", ignoremissing=True)
1152 repo.vfs.unlinkpath("rebasestate", ignoremissing=True)
1153
1153
1154 def needupdate(repo, state):
1154 def needupdate(repo, state):
1155 '''check whether we should `update --clean` away from a merge, or if
1155 '''check whether we should `update --clean` away from a merge, or if
1156 somehow the working dir got forcibly updated, e.g. by older hg'''
1156 somehow the working dir got forcibly updated, e.g. by older hg'''
1157 parents = [p.rev() for p in repo[None].parents()]
1157 parents = [p.rev() for p in repo[None].parents()]
1158
1158
1159 # Are we in a merge state at all?
1159 # Are we in a merge state at all?
1160 if len(parents) < 2:
1160 if len(parents) < 2:
1161 return False
1161 return False
1162
1162
1163 # We should be standing on the first as-of-yet unrebased commit.
1163 # We should be standing on the first as-of-yet unrebased commit.
1164 firstunrebased = min([old for old, new in state.iteritems()
1164 firstunrebased = min([old for old, new in state.iteritems()
1165 if new == nullrev])
1165 if new == nullrev])
1166 if firstunrebased in parents:
1166 if firstunrebased in parents:
1167 return True
1167 return True
1168
1168
1169 return False
1169 return False
1170
1170
1171 def abort(repo, originalwd, target, state, activebookmark=None):
1171 def abort(repo, originalwd, target, state, activebookmark=None):
1172 '''Restore the repository to its original state. Additional args:
1172 '''Restore the repository to its original state. Additional args:
1173
1173
1174 activebookmark: the name of the bookmark that should be active after the
1174 activebookmark: the name of the bookmark that should be active after the
1175 restore'''
1175 restore'''
1176
1176
1177 try:
1177 try:
1178 # If the first commits in the rebased set get skipped during the rebase,
1178 # If the first commits in the rebased set get skipped during the rebase,
1179 # their values within the state mapping will be the target rev id. The
1179 # their values within the state mapping will be the target rev id. The
1180 # dstates list must must not contain the target rev (issue4896)
1180 # dstates list must must not contain the target rev (issue4896)
1181 dstates = [s for s in state.values() if s >= 0 and s != target]
1181 dstates = [s for s in state.values() if s >= 0 and s != target]
1182 immutable = [d for d in dstates if not repo[d].mutable()]
1182 immutable = [d for d in dstates if not repo[d].mutable()]
1183 cleanup = True
1183 cleanup = True
1184 if immutable:
1184 if immutable:
1185 repo.ui.warn(_("warning: can't clean up public changesets %s\n")
1185 repo.ui.warn(_("warning: can't clean up public changesets %s\n")
1186 % ', '.join(str(repo[r]) for r in immutable),
1186 % ', '.join(str(repo[r]) for r in immutable),
1187 hint=_("see 'hg help phases' for details"))
1187 hint=_("see 'hg help phases' for details"))
1188 cleanup = False
1188 cleanup = False
1189
1189
1190 descendants = set()
1190 descendants = set()
1191 if dstates:
1191 if dstates:
1192 descendants = set(repo.changelog.descendants(dstates))
1192 descendants = set(repo.changelog.descendants(dstates))
1193 if descendants - set(dstates):
1193 if descendants - set(dstates):
1194 repo.ui.warn(_("warning: new changesets detected on target branch, "
1194 repo.ui.warn(_("warning: new changesets detected on target branch, "
1195 "can't strip\n"))
1195 "can't strip\n"))
1196 cleanup = False
1196 cleanup = False
1197
1197
1198 if cleanup:
1198 if cleanup:
1199 shouldupdate = False
1199 shouldupdate = False
1200 rebased = filter(lambda x: x >= 0 and x != target, state.values())
1200 rebased = filter(lambda x: x >= 0 and x != target, state.values())
1201 if rebased:
1201 if rebased:
1202 strippoints = [
1202 strippoints = [
1203 c.node() for c in repo.set('roots(%ld)', rebased)]
1203 c.node() for c in repo.set('roots(%ld)', rebased)]
1204
1204
1205 updateifonnodes = set(rebased)
1205 updateifonnodes = set(rebased)
1206 updateifonnodes.add(target)
1206 updateifonnodes.add(target)
1207 updateifonnodes.add(originalwd)
1207 updateifonnodes.add(originalwd)
1208 shouldupdate = repo['.'].rev() in updateifonnodes
1208 shouldupdate = repo['.'].rev() in updateifonnodes
1209
1209
1210 # Update away from the rebase if necessary
1210 # Update away from the rebase if necessary
1211 if shouldupdate or needupdate(repo, state):
1211 if shouldupdate or needupdate(repo, state):
1212 mergemod.update(repo, originalwd, False, True)
1212 mergemod.update(repo, originalwd, False, True)
1213
1213
1214 # Strip from the first rebased revision
1214 # Strip from the first rebased revision
1215 if rebased:
1215 if rebased:
1216 # no backup of rebased cset versions needed
1216 # no backup of rebased cset versions needed
1217 repair.strip(repo.ui, repo, strippoints)
1217 repair.strip(repo.ui, repo, strippoints)
1218
1218
1219 if activebookmark and activebookmark in repo._bookmarks:
1219 if activebookmark and activebookmark in repo._bookmarks:
1220 bookmarks.activate(repo, activebookmark)
1220 bookmarks.activate(repo, activebookmark)
1221
1221
1222 finally:
1222 finally:
1223 clearstatus(repo)
1223 clearstatus(repo)
1224 clearcollapsemsg(repo)
1224 clearcollapsemsg(repo)
1225 repo.ui.warn(_('rebase aborted\n'))
1225 repo.ui.warn(_('rebase aborted\n'))
1226 return 0
1226 return 0
1227
1227
1228 def buildstate(repo, dest, rebaseset, collapse, obsoletenotrebased):
1228 def buildstate(repo, dest, rebaseset, collapse, obsoletenotrebased):
1229 '''Define which revisions are going to be rebased and where
1229 '''Define which revisions are going to be rebased and where
1230
1230
1231 repo: repo
1231 repo: repo
1232 dest: context
1232 dest: context
1233 rebaseset: set of rev
1233 rebaseset: set of rev
1234 '''
1234 '''
1235 originalwd = repo['.'].rev()
1235 originalwd = repo['.'].rev()
1236 _setrebasesetvisibility(repo, set(rebaseset) | set([originalwd]))
1236 _setrebasesetvisibility(repo, set(rebaseset) | set([originalwd]))
1237
1237
1238 # This check isn't strictly necessary, since mq detects commits over an
1238 # This check isn't strictly necessary, since mq detects commits over an
1239 # applied patch. But it prevents messing up the working directory when
1239 # applied patch. But it prevents messing up the working directory when
1240 # a partially completed rebase is blocked by mq.
1240 # a partially completed rebase is blocked by mq.
1241 if 'qtip' in repo.tags() and (dest.node() in
1241 if 'qtip' in repo.tags() and (dest.node() in
1242 [s.node for s in repo.mq.applied]):
1242 [s.node for s in repo.mq.applied]):
1243 raise error.Abort(_('cannot rebase onto an applied mq patch'))
1243 raise error.Abort(_('cannot rebase onto an applied mq patch'))
1244
1244
1245 roots = list(repo.set('roots(%ld)', rebaseset))
1245 roots = list(repo.set('roots(%ld)', rebaseset))
1246 if not roots:
1246 if not roots:
1247 raise error.Abort(_('no matching revisions'))
1247 raise error.Abort(_('no matching revisions'))
1248 roots.sort()
1248 roots.sort()
1249 state = {}
1249 state = dict.fromkeys(rebaseset, revtodo)
1250 detachset = set()
1250 detachset = set()
1251 for root in roots:
1251 for root in roots:
1252 commonbase = root.ancestor(dest)
1252 commonbase = root.ancestor(dest)
1253 if commonbase == root:
1253 if commonbase == root:
1254 raise error.Abort(_('source is ancestor of destination'))
1254 raise error.Abort(_('source is ancestor of destination'))
1255 if commonbase == dest:
1255 if commonbase == dest:
1256 wctx = repo[None]
1256 wctx = repo[None]
1257 if dest == wctx.p1():
1257 if dest == wctx.p1():
1258 # when rebasing to '.', it will use the current wd branch name
1258 # when rebasing to '.', it will use the current wd branch name
1259 samebranch = root.branch() == wctx.branch()
1259 samebranch = root.branch() == wctx.branch()
1260 else:
1260 else:
1261 samebranch = root.branch() == dest.branch()
1261 samebranch = root.branch() == dest.branch()
1262 if not collapse and samebranch and root in dest.children():
1262 if not collapse and samebranch and root in dest.children():
1263 repo.ui.debug('source is a child of destination\n')
1263 repo.ui.debug('source is a child of destination\n')
1264 return None
1264 return None
1265
1265
1266 repo.ui.debug('rebase onto %s starting from %s\n' % (dest, root))
1266 repo.ui.debug('rebase onto %s starting from %s\n' % (dest, root))
1267 state.update(dict.fromkeys(rebaseset, revtodo))
1268 # Rebase tries to turn <dest> into a parent of <root> while
1267 # Rebase tries to turn <dest> into a parent of <root> while
1269 # preserving the number of parents of rebased changesets:
1268 # preserving the number of parents of rebased changesets:
1270 #
1269 #
1271 # - A changeset with a single parent will always be rebased as a
1270 # - A changeset with a single parent will always be rebased as a
1272 # changeset with a single parent.
1271 # changeset with a single parent.
1273 #
1272 #
1274 # - A merge will be rebased as merge unless its parents are both
1273 # - A merge will be rebased as merge unless its parents are both
1275 # ancestors of <dest> or are themselves in the rebased set and
1274 # ancestors of <dest> or are themselves in the rebased set and
1276 # pruned while rebased.
1275 # pruned while rebased.
1277 #
1276 #
1278 # If one parent of <root> is an ancestor of <dest>, the rebased
1277 # If one parent of <root> is an ancestor of <dest>, the rebased
1279 # version of this parent will be <dest>. This is always true with
1278 # version of this parent will be <dest>. This is always true with
1280 # --base option.
1279 # --base option.
1281 #
1280 #
1282 # Otherwise, we need to *replace* the original parents with
1281 # Otherwise, we need to *replace* the original parents with
1283 # <dest>. This "detaches" the rebased set from its former location
1282 # <dest>. This "detaches" the rebased set from its former location
1284 # and rebases it onto <dest>. Changes introduced by ancestors of
1283 # and rebases it onto <dest>. Changes introduced by ancestors of
1285 # <root> not common with <dest> (the detachset, marked as
1284 # <root> not common with <dest> (the detachset, marked as
1286 # nullmerge) are "removed" from the rebased changesets.
1285 # nullmerge) are "removed" from the rebased changesets.
1287 #
1286 #
1288 # - If <root> has a single parent, set it to <dest>.
1287 # - If <root> has a single parent, set it to <dest>.
1289 #
1288 #
1290 # - If <root> is a merge, we cannot decide which parent to
1289 # - If <root> is a merge, we cannot decide which parent to
1291 # replace, the rebase operation is not clearly defined.
1290 # replace, the rebase operation is not clearly defined.
1292 #
1291 #
1293 # The table below sums up this behavior:
1292 # The table below sums up this behavior:
1294 #
1293 #
1295 # +------------------+----------------------+-------------------------+
1294 # +------------------+----------------------+-------------------------+
1296 # | | one parent | merge |
1295 # | | one parent | merge |
1297 # +------------------+----------------------+-------------------------+
1296 # +------------------+----------------------+-------------------------+
1298 # | parent in | new parent is <dest> | parents in ::<dest> are |
1297 # | parent in | new parent is <dest> | parents in ::<dest> are |
1299 # | ::<dest> | | remapped to <dest> |
1298 # | ::<dest> | | remapped to <dest> |
1300 # +------------------+----------------------+-------------------------+
1299 # +------------------+----------------------+-------------------------+
1301 # | unrelated source | new parent is <dest> | ambiguous, abort |
1300 # | unrelated source | new parent is <dest> | ambiguous, abort |
1302 # +------------------+----------------------+-------------------------+
1301 # +------------------+----------------------+-------------------------+
1303 #
1302 #
1304 # The actual abort is handled by `defineparents`
1303 # The actual abort is handled by `defineparents`
1305 if len(root.parents()) <= 1:
1304 if len(root.parents()) <= 1:
1306 # ancestors of <root> not ancestors of <dest>
1305 # ancestors of <root> not ancestors of <dest>
1307 detachset.update(repo.changelog.findmissingrevs([commonbase.rev()],
1306 detachset.update(repo.changelog.findmissingrevs([commonbase.rev()],
1308 [root.rev()]))
1307 [root.rev()]))
1309 for r in detachset:
1308 for r in detachset:
1310 if r not in state:
1309 if r not in state:
1311 state[r] = nullmerge
1310 state[r] = nullmerge
1312 if len(roots) > 1:
1311 if len(roots) > 1:
1313 # If we have multiple roots, we may have "hole" in the rebase set.
1312 # If we have multiple roots, we may have "hole" in the rebase set.
1314 # Rebase roots that descend from those "hole" should not be detached as
1313 # Rebase roots that descend from those "hole" should not be detached as
1315 # other root are. We use the special `revignored` to inform rebase that
1314 # other root are. We use the special `revignored` to inform rebase that
1316 # the revision should be ignored but that `defineparents` should search
1315 # the revision should be ignored but that `defineparents` should search
1317 # a rebase destination that make sense regarding rebased topology.
1316 # a rebase destination that make sense regarding rebased topology.
1318 rebasedomain = set(repo.revs('%ld::%ld', rebaseset, rebaseset))
1317 rebasedomain = set(repo.revs('%ld::%ld', rebaseset, rebaseset))
1319 for ignored in set(rebasedomain) - set(rebaseset):
1318 for ignored in set(rebasedomain) - set(rebaseset):
1320 state[ignored] = revignored
1319 state[ignored] = revignored
1321 for r in obsoletenotrebased:
1320 for r in obsoletenotrebased:
1322 if obsoletenotrebased[r] is None:
1321 if obsoletenotrebased[r] is None:
1323 state[r] = revpruned
1322 state[r] = revpruned
1324 else:
1323 else:
1325 state[r] = revprecursor
1324 state[r] = revprecursor
1326 return originalwd, dest.rev(), state
1325 return originalwd, dest.rev(), state
1327
1326
1328 def clearrebased(ui, repo, state, skipped, collapsedas=None):
1327 def clearrebased(ui, repo, state, skipped, collapsedas=None):
1329 """dispose of rebased revision at the end of the rebase
1328 """dispose of rebased revision at the end of the rebase
1330
1329
1331 If `collapsedas` is not None, the rebase was a collapse whose result if the
1330 If `collapsedas` is not None, the rebase was a collapse whose result if the
1332 `collapsedas` node."""
1331 `collapsedas` node."""
1333 if obsolete.isenabled(repo, obsolete.createmarkersopt):
1332 if obsolete.isenabled(repo, obsolete.createmarkersopt):
1334 markers = []
1333 markers = []
1335 for rev, newrev in sorted(state.items()):
1334 for rev, newrev in sorted(state.items()):
1336 if newrev >= 0:
1335 if newrev >= 0:
1337 if rev in skipped:
1336 if rev in skipped:
1338 succs = ()
1337 succs = ()
1339 elif collapsedas is not None:
1338 elif collapsedas is not None:
1340 succs = (repo[collapsedas],)
1339 succs = (repo[collapsedas],)
1341 else:
1340 else:
1342 succs = (repo[newrev],)
1341 succs = (repo[newrev],)
1343 markers.append((repo[rev], succs))
1342 markers.append((repo[rev], succs))
1344 if markers:
1343 if markers:
1345 obsolete.createmarkers(repo, markers)
1344 obsolete.createmarkers(repo, markers)
1346 else:
1345 else:
1347 rebased = [rev for rev in state if state[rev] > nullmerge]
1346 rebased = [rev for rev in state if state[rev] > nullmerge]
1348 if rebased:
1347 if rebased:
1349 stripped = []
1348 stripped = []
1350 for root in repo.set('roots(%ld)', rebased):
1349 for root in repo.set('roots(%ld)', rebased):
1351 if set(repo.changelog.descendants([root.rev()])) - set(state):
1350 if set(repo.changelog.descendants([root.rev()])) - set(state):
1352 ui.warn(_("warning: new changesets detected "
1351 ui.warn(_("warning: new changesets detected "
1353 "on source branch, not stripping\n"))
1352 "on source branch, not stripping\n"))
1354 else:
1353 else:
1355 stripped.append(root.node())
1354 stripped.append(root.node())
1356 if stripped:
1355 if stripped:
1357 # backup the old csets by default
1356 # backup the old csets by default
1358 repair.strip(ui, repo, stripped, "all")
1357 repair.strip(ui, repo, stripped, "all")
1359
1358
1360
1359
1361 def pullrebase(orig, ui, repo, *args, **opts):
1360 def pullrebase(orig, ui, repo, *args, **opts):
1362 'Call rebase after pull if the latter has been invoked with --rebase'
1361 'Call rebase after pull if the latter has been invoked with --rebase'
1363 ret = None
1362 ret = None
1364 if opts.get('rebase'):
1363 if opts.get('rebase'):
1365 if ui.configbool('commands', 'rebase.requiredest'):
1364 if ui.configbool('commands', 'rebase.requiredest'):
1366 msg = _('rebase destination required by configuration')
1365 msg = _('rebase destination required by configuration')
1367 hint = _('use hg pull followed by hg rebase -d DEST')
1366 hint = _('use hg pull followed by hg rebase -d DEST')
1368 raise error.Abort(msg, hint=hint)
1367 raise error.Abort(msg, hint=hint)
1369
1368
1370 wlock = lock = None
1369 wlock = lock = None
1371 try:
1370 try:
1372 wlock = repo.wlock()
1371 wlock = repo.wlock()
1373 lock = repo.lock()
1372 lock = repo.lock()
1374 if opts.get('update'):
1373 if opts.get('update'):
1375 del opts['update']
1374 del opts['update']
1376 ui.debug('--update and --rebase are not compatible, ignoring '
1375 ui.debug('--update and --rebase are not compatible, ignoring '
1377 'the update flag\n')
1376 'the update flag\n')
1378
1377
1379 cmdutil.checkunfinished(repo)
1378 cmdutil.checkunfinished(repo)
1380 cmdutil.bailifchanged(repo, hint=_('cannot pull with rebase: '
1379 cmdutil.bailifchanged(repo, hint=_('cannot pull with rebase: '
1381 'please commit or shelve your changes first'))
1380 'please commit or shelve your changes first'))
1382
1381
1383 revsprepull = len(repo)
1382 revsprepull = len(repo)
1384 origpostincoming = commands.postincoming
1383 origpostincoming = commands.postincoming
1385 def _dummy(*args, **kwargs):
1384 def _dummy(*args, **kwargs):
1386 pass
1385 pass
1387 commands.postincoming = _dummy
1386 commands.postincoming = _dummy
1388 try:
1387 try:
1389 ret = orig(ui, repo, *args, **opts)
1388 ret = orig(ui, repo, *args, **opts)
1390 finally:
1389 finally:
1391 commands.postincoming = origpostincoming
1390 commands.postincoming = origpostincoming
1392 revspostpull = len(repo)
1391 revspostpull = len(repo)
1393 if revspostpull > revsprepull:
1392 if revspostpull > revsprepull:
1394 # --rev option from pull conflict with rebase own --rev
1393 # --rev option from pull conflict with rebase own --rev
1395 # dropping it
1394 # dropping it
1396 if 'rev' in opts:
1395 if 'rev' in opts:
1397 del opts['rev']
1396 del opts['rev']
1398 # positional argument from pull conflicts with rebase's own
1397 # positional argument from pull conflicts with rebase's own
1399 # --source.
1398 # --source.
1400 if 'source' in opts:
1399 if 'source' in opts:
1401 del opts['source']
1400 del opts['source']
1402 # revsprepull is the len of the repo, not revnum of tip.
1401 # revsprepull is the len of the repo, not revnum of tip.
1403 destspace = list(repo.changelog.revs(start=revsprepull))
1402 destspace = list(repo.changelog.revs(start=revsprepull))
1404 opts['_destspace'] = destspace
1403 opts['_destspace'] = destspace
1405 try:
1404 try:
1406 rebase(ui, repo, **opts)
1405 rebase(ui, repo, **opts)
1407 except error.NoMergeDestAbort:
1406 except error.NoMergeDestAbort:
1408 # we can maybe update instead
1407 # we can maybe update instead
1409 rev, _a, _b = destutil.destupdate(repo)
1408 rev, _a, _b = destutil.destupdate(repo)
1410 if rev == repo['.'].rev():
1409 if rev == repo['.'].rev():
1411 ui.status(_('nothing to rebase\n'))
1410 ui.status(_('nothing to rebase\n'))
1412 else:
1411 else:
1413 ui.status(_('nothing to rebase - updating instead\n'))
1412 ui.status(_('nothing to rebase - updating instead\n'))
1414 # not passing argument to get the bare update behavior
1413 # not passing argument to get the bare update behavior
1415 # with warning and trumpets
1414 # with warning and trumpets
1416 commands.update(ui, repo)
1415 commands.update(ui, repo)
1417 finally:
1416 finally:
1418 release(lock, wlock)
1417 release(lock, wlock)
1419 else:
1418 else:
1420 if opts.get('tool'):
1419 if opts.get('tool'):
1421 raise error.Abort(_('--tool can only be used with --rebase'))
1420 raise error.Abort(_('--tool can only be used with --rebase'))
1422 ret = orig(ui, repo, *args, **opts)
1421 ret = orig(ui, repo, *args, **opts)
1423
1422
1424 return ret
1423 return ret
1425
1424
1426 def _setrebasesetvisibility(repo, revs):
1425 def _setrebasesetvisibility(repo, revs):
1427 """store the currently rebased set on the repo object
1426 """store the currently rebased set on the repo object
1428
1427
1429 This is used by another function to prevent rebased revision to because
1428 This is used by another function to prevent rebased revision to because
1430 hidden (see issue4504)"""
1429 hidden (see issue4504)"""
1431 repo = repo.unfiltered()
1430 repo = repo.unfiltered()
1432 repo._rebaseset = revs
1431 repo._rebaseset = revs
1433 # invalidate cache if visibility changes
1432 # invalidate cache if visibility changes
1434 hiddens = repo.filteredrevcache.get('visible', set())
1433 hiddens = repo.filteredrevcache.get('visible', set())
1435 if revs & hiddens:
1434 if revs & hiddens:
1436 repo.invalidatevolatilesets()
1435 repo.invalidatevolatilesets()
1437
1436
1438 def _clearrebasesetvisibiliy(repo):
1437 def _clearrebasesetvisibiliy(repo):
1439 """remove rebaseset data from the repo"""
1438 """remove rebaseset data from the repo"""
1440 repo = repo.unfiltered()
1439 repo = repo.unfiltered()
1441 if '_rebaseset' in vars(repo):
1440 if '_rebaseset' in vars(repo):
1442 del repo._rebaseset
1441 del repo._rebaseset
1443
1442
1444 def _rebasedvisible(orig, repo):
1443 def _rebasedvisible(orig, repo):
1445 """ensure rebased revs stay visible (see issue4504)"""
1444 """ensure rebased revs stay visible (see issue4504)"""
1446 blockers = orig(repo)
1445 blockers = orig(repo)
1447 blockers.update(getattr(repo, '_rebaseset', ()))
1446 blockers.update(getattr(repo, '_rebaseset', ()))
1448 return blockers
1447 return blockers
1449
1448
1450 def _filterobsoleterevs(repo, revs):
1449 def _filterobsoleterevs(repo, revs):
1451 """returns a set of the obsolete revisions in revs"""
1450 """returns a set of the obsolete revisions in revs"""
1452 return set(r for r in revs if repo[r].obsolete())
1451 return set(r for r in revs if repo[r].obsolete())
1453
1452
1454 def _computeobsoletenotrebased(repo, rebaseobsrevs, dest):
1453 def _computeobsoletenotrebased(repo, rebaseobsrevs, dest):
1455 """return a mapping obsolete => successor for all obsolete nodes to be
1454 """return a mapping obsolete => successor for all obsolete nodes to be
1456 rebased that have a successors in the destination
1455 rebased that have a successors in the destination
1457
1456
1458 obsolete => None entries in the mapping indicate nodes with no successor"""
1457 obsolete => None entries in the mapping indicate nodes with no successor"""
1459 obsoletenotrebased = {}
1458 obsoletenotrebased = {}
1460
1459
1461 # Build a mapping successor => obsolete nodes for the obsolete
1460 # Build a mapping successor => obsolete nodes for the obsolete
1462 # nodes to be rebased
1461 # nodes to be rebased
1463 allsuccessors = {}
1462 allsuccessors = {}
1464 cl = repo.changelog
1463 cl = repo.changelog
1465 for r in rebaseobsrevs:
1464 for r in rebaseobsrevs:
1466 node = cl.node(r)
1465 node = cl.node(r)
1467 for s in obsolete.allsuccessors(repo.obsstore, [node]):
1466 for s in obsolete.allsuccessors(repo.obsstore, [node]):
1468 try:
1467 try:
1469 allsuccessors[cl.rev(s)] = cl.rev(node)
1468 allsuccessors[cl.rev(s)] = cl.rev(node)
1470 except LookupError:
1469 except LookupError:
1471 pass
1470 pass
1472
1471
1473 if allsuccessors:
1472 if allsuccessors:
1474 # Look for successors of obsolete nodes to be rebased among
1473 # Look for successors of obsolete nodes to be rebased among
1475 # the ancestors of dest
1474 # the ancestors of dest
1476 ancs = cl.ancestors([repo[dest].rev()],
1475 ancs = cl.ancestors([repo[dest].rev()],
1477 stoprev=min(allsuccessors),
1476 stoprev=min(allsuccessors),
1478 inclusive=True)
1477 inclusive=True)
1479 for s in allsuccessors:
1478 for s in allsuccessors:
1480 if s in ancs:
1479 if s in ancs:
1481 obsoletenotrebased[allsuccessors[s]] = s
1480 obsoletenotrebased[allsuccessors[s]] = s
1482 elif (s == allsuccessors[s] and
1481 elif (s == allsuccessors[s] and
1483 allsuccessors.values().count(s) == 1):
1482 allsuccessors.values().count(s) == 1):
1484 # plain prune
1483 # plain prune
1485 obsoletenotrebased[s] = None
1484 obsoletenotrebased[s] = None
1486
1485
1487 return obsoletenotrebased
1486 return obsoletenotrebased
1488
1487
1489 def summaryhook(ui, repo):
1488 def summaryhook(ui, repo):
1490 if not repo.vfs.exists('rebasestate'):
1489 if not repo.vfs.exists('rebasestate'):
1491 return
1490 return
1492 try:
1491 try:
1493 rbsrt = rebaseruntime(repo, ui, {})
1492 rbsrt = rebaseruntime(repo, ui, {})
1494 rbsrt.restorestatus()
1493 rbsrt.restorestatus()
1495 state = rbsrt.state
1494 state = rbsrt.state
1496 except error.RepoLookupError:
1495 except error.RepoLookupError:
1497 # i18n: column positioning for "hg summary"
1496 # i18n: column positioning for "hg summary"
1498 msg = _('rebase: (use "hg rebase --abort" to clear broken state)\n')
1497 msg = _('rebase: (use "hg rebase --abort" to clear broken state)\n')
1499 ui.write(msg)
1498 ui.write(msg)
1500 return
1499 return
1501 numrebased = len([i for i in state.itervalues() if i >= 0])
1500 numrebased = len([i for i in state.itervalues() if i >= 0])
1502 # i18n: column positioning for "hg summary"
1501 # i18n: column positioning for "hg summary"
1503 ui.write(_('rebase: %s, %s (rebase --continue)\n') %
1502 ui.write(_('rebase: %s, %s (rebase --continue)\n') %
1504 (ui.label(_('%d rebased'), 'rebase.rebased') % numrebased,
1503 (ui.label(_('%d rebased'), 'rebase.rebased') % numrebased,
1505 ui.label(_('%d remaining'), 'rebase.remaining') %
1504 ui.label(_('%d remaining'), 'rebase.remaining') %
1506 (len(state) - numrebased)))
1505 (len(state) - numrebased)))
1507
1506
1508 def uisetup(ui):
1507 def uisetup(ui):
1509 #Replace pull with a decorator to provide --rebase option
1508 #Replace pull with a decorator to provide --rebase option
1510 entry = extensions.wrapcommand(commands.table, 'pull', pullrebase)
1509 entry = extensions.wrapcommand(commands.table, 'pull', pullrebase)
1511 entry[1].append(('', 'rebase', None,
1510 entry[1].append(('', 'rebase', None,
1512 _("rebase working directory to branch head")))
1511 _("rebase working directory to branch head")))
1513 entry[1].append(('t', 'tool', '',
1512 entry[1].append(('t', 'tool', '',
1514 _("specify merge tool for rebase")))
1513 _("specify merge tool for rebase")))
1515 cmdutil.summaryhooks.add('rebase', summaryhook)
1514 cmdutil.summaryhooks.add('rebase', summaryhook)
1516 cmdutil.unfinishedstates.append(
1515 cmdutil.unfinishedstates.append(
1517 ['rebasestate', False, False, _('rebase in progress'),
1516 ['rebasestate', False, False, _('rebase in progress'),
1518 _("use 'hg rebase --continue' or 'hg rebase --abort'")])
1517 _("use 'hg rebase --continue' or 'hg rebase --abort'")])
1519 cmdutil.afterresolvedstates.append(
1518 cmdutil.afterresolvedstates.append(
1520 ['rebasestate', _('hg rebase --continue')])
1519 ['rebasestate', _('hg rebase --continue')])
1521 # ensure rebased rev are not hidden
1520 # ensure rebased rev are not hidden
1522 extensions.wrapfunction(repoview, '_getdynamicblockers', _rebasedvisible)
1521 extensions.wrapfunction(repoview, '_getdynamicblockers', _rebasedvisible)
General Comments 0
You need to be logged in to leave comments. Login now