##// END OF EJS Templates
destutil: remove current head from list of candidates early...
Pierre-Yves David -
r28138:5ad20174 default
parent child Browse files
Show More
@@ -1,315 +1,316 b''
1 1 # destutil.py - Mercurial utility function for command destination
2 2 #
3 3 # Copyright Matt Mackall <mpm@selenic.com> and other
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from . import (
12 12 bookmarks,
13 13 error,
14 14 obsolete,
15 15 )
16 16
17 17 def _destupdatevalidate(repo, rev, clean, check):
18 18 """validate that the destination comply to various rules
19 19
20 20 This exists as its own function to help wrapping from extensions."""
21 21 wc = repo[None]
22 22 p1 = wc.p1()
23 23 if not clean:
24 24 # Check that the update is linear.
25 25 #
26 26 # Mercurial do not allow update-merge for non linear pattern
27 27 # (that would be technically possible but was considered too confusing
28 28 # for user a long time ago)
29 29 #
30 30 # See mercurial.merge.update for details
31 31 if p1.rev() not in repo.changelog.ancestors([rev], inclusive=True):
32 32 dirty = wc.dirty(missing=True)
33 33 foreground = obsolete.foreground(repo, [p1.node()])
34 34 if not repo[rev].node() in foreground:
35 35 if dirty:
36 36 msg = _("uncommitted changes")
37 37 hint = _("commit and merge, or update --clean to"
38 38 " discard changes")
39 39 raise error.UpdateAbort(msg, hint=hint)
40 40 elif not check: # destination is not a descendant.
41 41 msg = _("not a linear update")
42 42 hint = _("merge or update --check to force update")
43 43 raise error.UpdateAbort(msg, hint=hint)
44 44
45 45 def _destupdateobs(repo, clean, check):
46 46 """decide of an update destination from obsolescence markers"""
47 47 node = None
48 48 wc = repo[None]
49 49 p1 = wc.p1()
50 50 movemark = None
51 51
52 52 if p1.obsolete() and not p1.children():
53 53 # allow updating to successors
54 54 successors = obsolete.successorssets(repo, p1.node())
55 55
56 56 # behavior of certain cases is as follows,
57 57 #
58 58 # divergent changesets: update to highest rev, similar to what
59 59 # is currently done when there are more than one head
60 60 # (i.e. 'tip')
61 61 #
62 62 # replaced changesets: same as divergent except we know there
63 63 # is no conflict
64 64 #
65 65 # pruned changeset: no update is done; though, we could
66 66 # consider updating to the first non-obsolete parent,
67 67 # similar to what is current done for 'hg prune'
68 68
69 69 if successors:
70 70 # flatten the list here handles both divergent (len > 1)
71 71 # and the usual case (len = 1)
72 72 successors = [n for sub in successors for n in sub]
73 73
74 74 # get the max revision for the given successors set,
75 75 # i.e. the 'tip' of a set
76 76 node = repo.revs('max(%ln)', successors).first()
77 77 if bookmarks.isactivewdirparent(repo):
78 78 movemark = repo['.'].node()
79 79 return node, movemark, None
80 80
81 81 def _destupdatebook(repo, clean, check):
82 82 """decide on an update destination from active bookmark"""
83 83 # we also move the active bookmark, if any
84 84 activemark = None
85 85 node, movemark = bookmarks.calculateupdate(repo.ui, repo, None)
86 86 if node is not None:
87 87 activemark = node
88 88 return node, movemark, activemark
89 89
90 90 def _destupdatebranch(repo, clean, check):
91 91 """decide on an update destination from current branch"""
92 92 wc = repo[None]
93 93 movemark = node = None
94 94 try:
95 95 node = repo.revs('max(.::(head() and branch(%s)))'
96 96 , wc.branch()).first()
97 97 if bookmarks.isactivewdirparent(repo):
98 98 movemark = repo['.'].node()
99 99 except error.RepoLookupError:
100 100 if wc.branch() == 'default': # no default branch!
101 101 node = repo.lookup('tip') # update to tip
102 102 else:
103 103 raise error.Abort(_("branch %s not found") % wc.branch())
104 104 return node, movemark, None
105 105
106 106 # order in which each step should be evalutated
107 107 # steps are run until one finds a destination
108 108 destupdatesteps = ['evolution', 'bookmark', 'branch']
109 109 # mapping to ease extension overriding steps.
110 110 destupdatestepmap = {'evolution': _destupdateobs,
111 111 'bookmark': _destupdatebook,
112 112 'branch': _destupdatebranch,
113 113 }
114 114
115 115 def destupdate(repo, clean=False, check=False):
116 116 """destination for bare update operation
117 117
118 118 return (rev, movemark, activemark)
119 119
120 120 - rev: the revision to update to,
121 121 - movemark: node to move the active bookmark from
122 122 (cf bookmark.calculate update),
123 123 - activemark: a bookmark to activate at the end of the update.
124 124 """
125 125 node = movemark = activemark = None
126 126
127 127 for step in destupdatesteps:
128 128 node, movemark, activemark = destupdatestepmap[step](repo, clean, check)
129 129 if node is not None:
130 130 break
131 131 rev = repo[node].rev()
132 132
133 133 _destupdatevalidate(repo, rev, clean, check)
134 134
135 135 return rev, movemark, activemark
136 136
137 137 msgdestmerge = {
138 138 # too many matching divergent bookmark
139 139 'toomanybookmarks':
140 140 {'merge':
141 141 (_("multiple matching bookmarks to merge -"
142 142 " please merge with an explicit rev or bookmark"),
143 143 _("run 'hg heads' to see all heads")),
144 144 },
145 145 # no other matching divergent bookmark
146 146 'nootherbookmarks':
147 147 {'merge':
148 148 (_("no matching bookmark to merge - "
149 149 "please merge with an explicit rev or bookmark"),
150 150 _("run 'hg heads' to see all heads")),
151 151 },
152 152 # branch have too many unbookmarked heads, no obvious destination
153 153 'toomanyheads':
154 154 {'merge':
155 155 (_("branch '%s' has %d heads - please merge with an explicit rev"),
156 156 _("run 'hg heads .' to see heads")),
157 157 },
158 158 # branch have no other unbookmarked heads
159 159 'bookmarkedheads':
160 160 {'merge':
161 161 (_("heads are bookmarked - please merge with an explicit rev"),
162 162 _("run 'hg heads' to see all heads")),
163 163 },
164 164 # branch have just a single heads, but there is other branches
165 165 'nootherbranchheads':
166 166 {'merge':
167 167 (_("branch '%s' has one head - please merge with an explicit rev"),
168 168 _("run 'hg heads' to see all heads")),
169 169 },
170 170 # repository have a single head
171 171 'nootherheads':
172 172 {'merge':
173 173 (_('nothing to merge'),
174 174 None),
175 175 },
176 176 # repository have a single head and we are not on it
177 177 'nootherheadsbehind':
178 178 {'merge':
179 179 (_('nothing to merge'),
180 180 _("use 'hg update' instead")),
181 181 },
182 182 # We are not on a head
183 183 'notatheads':
184 184 {'merge':
185 185 (_('working directory not at a head revision'),
186 186 _("use 'hg update' or merge with an explicit revision"))
187 187 },
188 188 }
189 189
190 190 def _destmergebook(repo, action='merge'):
191 191 """find merge destination in the active bookmark case"""
192 192 node = None
193 193 bmheads = repo.bookmarkheads(repo._activebookmark)
194 194 curhead = repo[repo._activebookmark].node()
195 195 if len(bmheads) == 2:
196 196 if curhead == bmheads[0]:
197 197 node = bmheads[1]
198 198 else:
199 199 node = bmheads[0]
200 200 elif len(bmheads) > 2:
201 201 msg, hint = msgdestmerge['toomanybookmarks'][action]
202 202 raise error.Abort(msg, hint=hint)
203 203 elif len(bmheads) <= 1:
204 204 msg, hint = msgdestmerge['nootherbookmarks'][action]
205 205 raise error.Abort(msg, hint=hint)
206 206 assert node is not None
207 207 return node
208 208
209 209 def _destmergebranch(repo, action='merge'):
210 210 """find merge destination based on branch heads"""
211 211 node = None
212 212 parent = repo.dirstate.p1()
213 213 branch = repo.dirstate.branch()
214 214 bheads = repo.branchheads(branch)
215 nbhs = [bh for bh in bheads if not repo[bh].bookmarks()]
216 215
217 216 if parent not in bheads:
218 217 # Case A: working copy if not on a head.
219 218 #
220 219 # This is probably a user mistake We bailout pointing at 'hg update'
221 220 if len(repo.heads()) <= 1:
222 221 msg, hint = msgdestmerge['nootherheadsbehind'][action]
223 222 else:
224 223 msg, hint = msgdestmerge['notatheads'][action]
225 224 raise error.Abort(msg, hint=hint)
226 elif len(nbhs) > 2:
227 # Case B: There is more than 2 anonymous heads
225 # remove current head from the set
226 bheads = [bh for bh in bheads if bh != parent]
227 # filters out bookmarked heads
228 nbhs = [bh for bh in bheads if not repo[bh].bookmarks()]
229 if len(nbhs) > 1:
230 # Case B: There is more than 1 other anonymous heads
228 231 #
229 232 # This means that there will be more than 1 candidate. This is
230 233 # ambiguous. We abort asking the user to pick as explicit destination
231 234 # instead.
232 235 msg, hint = msgdestmerge['toomanyheads'][action]
233 msg %= (branch, len(bheads))
236 msg %= (branch, len(bheads) + 1)
234 237 raise error.Abort(msg, hint=hint)
235 elif len(nbhs) <= 1:
236 # Case B: There is no other anonymous head that the one we are one
238 elif not nbhs:
239 # Case B: There is no other anonymous heads
237 240 #
238 241 # This means that there is no natural candidate to merge with.
239 242 # We abort, with various messages for various cases.
240 if len(bheads) > 1:
243 if bheads:
241 244 msg, hint = msgdestmerge['bookmarkedheads'][action]
242 245 elif len(repo.heads()) > 1:
243 246 msg, hint = msgdestmerge['nootherbranchheads'][action]
244 247 msg %= branch
245 248 else:
246 249 msg, hint = msgdestmerge['nootherheads'][action]
247 250 raise error.Abort(msg, hint=hint)
248 elif parent == nbhs[0]:
249 node = nbhs[-1]
250 251 else:
251 252 node = nbhs[0]
252 253 assert node is not None
253 254 return node
254 255
255 256 def destmerge(repo, action='merge'):
256 257 """return the default destination for a merge
257 258
258 259 (or raise exception about why it can't pick one)
259 260
260 261 :action: the action being performed, controls emitted error message
261 262 """
262 263 if repo._activebookmark:
263 264 node = _destmergebook(repo, action=action)
264 265 else:
265 266 node = _destmergebranch(repo, action=action)
266 267 return repo[node].rev()
267 268
268 269 histeditdefaultrevset = 'reverse(only(.) and not public() and not ::merge())'
269 270
270 271 def desthistedit(ui, repo):
271 272 """Default base revision to edit for `hg histedit`."""
272 273 # Avoid cycle: scmutil -> revset -> destutil
273 274 from . import scmutil
274 275
275 276 default = ui.config('histedit', 'defaultrev', histeditdefaultrevset)
276 277 if default:
277 278 revs = scmutil.revrange(repo, [default])
278 279 if revs:
279 280 # The revset supplied by the user may not be in ascending order nor
280 281 # take the first revision. So do this manually.
281 282 revs.sort()
282 283 return revs.first()
283 284
284 285 return None
285 286
286 287 def _statusotherbook(ui, repo):
287 288 bmheads = repo.bookmarkheads(repo._activebookmark)
288 289 curhead = repo[repo._activebookmark].node()
289 290 if repo.revs('%n and parents()', curhead):
290 291 # we are on the active bookmark
291 292 bmheads = [b for b in bmheads if curhead != b]
292 293 if bmheads:
293 294 msg = _('%i other divergent bookmarks for "%s"\n')
294 295 ui.status(msg % (len(bmheads), repo._activebookmark))
295 296
296 297 def _statusotherbranchheads(ui, repo):
297 298 currentbranch = repo.dirstate.branch()
298 299 heads = repo.branchheads(currentbranch)
299 300 l = len(heads)
300 301 if repo.revs('%ln and parents()', heads):
301 302 # we are on a head
302 303 heads = repo.revs('%ln - parents()', heads)
303 304 if heads and l != len(heads):
304 305 ui.status(_('%i other heads for branch "%s"\n') %
305 306 (len(heads), currentbranch))
306 307
307 308 def statusotherdests(ui, repo):
308 309 """Print message about other head"""
309 310 # XXX we should probably include a hint:
310 311 # - about what to do
311 312 # - how to see such heads
312 313 if repo._activebookmark:
313 314 _statusotherbook(ui, repo)
314 315 else:
315 316 _statusotherbranchheads(ui, repo)
General Comments 0
You need to be logged in to leave comments. Login now