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