##// END OF EJS Templates
destupdate: extract logic based on bookmarks in its own function...
Pierre-Yves David -
r26724:7fc759c0 default
parent child Browse files
Show More
@@ -1,167 +1,173 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 .i18n import _
9 9 from . import (
10 10 bookmarks,
11 11 error,
12 12 obsolete,
13 13 )
14 14
15 15 def _destupdatevalidate(repo, rev, clean, check):
16 16 """validate that the destination comply to various rules
17 17
18 18 This exists as its own function to help wrapping from extensions."""
19 19 wc = repo[None]
20 20 p1 = wc.p1()
21 21 if not clean:
22 22 # Check that the update is linear.
23 23 #
24 24 # Mercurial do not allow update-merge for non linear pattern
25 25 # (that would be technically possible but was considered too confusing
26 26 # for user a long time ago)
27 27 #
28 28 # See mercurial.merge.update for details
29 29 if p1.rev() not in repo.changelog.ancestors([rev], inclusive=True):
30 30 dirty = wc.dirty(missing=True)
31 31 foreground = obsolete.foreground(repo, [p1.node()])
32 32 if not repo[rev].node() in foreground:
33 33 if dirty:
34 34 msg = _("uncommitted changes")
35 35 hint = _("commit and merge, or update --clean to"
36 36 " discard changes")
37 37 raise error.UpdateAbort(msg, hint=hint)
38 38 elif not check: # destination is not a descendant.
39 39 msg = _("not a linear update")
40 40 hint = _("merge or update --check to force update")
41 41 raise error.UpdateAbort(msg, hint=hint)
42 42
43 43 def _destupdateobs(repo, clean, check):
44 44 """decide of an update destination from obsolescence markers"""
45 45 node = None
46 46 wc = repo[None]
47 47 p1 = wc.p1()
48 48 movemark = None
49 49
50 50 if p1.obsolete() and not p1.children():
51 51 # allow updating to successors
52 52 successors = obsolete.successorssets(repo, p1.node())
53 53
54 54 # behavior of certain cases is as follows,
55 55 #
56 56 # divergent changesets: update to highest rev, similar to what
57 57 # is currently done when there are more than one head
58 58 # (i.e. 'tip')
59 59 #
60 60 # replaced changesets: same as divergent except we know there
61 61 # is no conflict
62 62 #
63 63 # pruned changeset: no update is done; though, we could
64 64 # consider updating to the first non-obsolete parent,
65 65 # similar to what is current done for 'hg prune'
66 66
67 67 if successors:
68 68 # flatten the list here handles both divergent (len > 1)
69 69 # and the usual case (len = 1)
70 70 successors = [n for sub in successors for n in sub]
71 71
72 72 # get the max revision for the given successors set,
73 73 # i.e. the 'tip' of a set
74 74 node = repo.revs('max(%ln)', successors).first()
75 75 if bookmarks.isactivewdirparent(repo):
76 76 movemark = repo['.'].node()
77 77 return node, movemark, None
78 78
79 def _destupdatebook(repo, clean, check):
80 """decide on an update destination from active bookmark"""
81 # we also move the active bookmark, if any
82 activemark = None
83 node, movemark = bookmarks.calculateupdate(repo.ui, repo, None)
84 if node is not None:
85 activemark = node
86 return node, movemark, activemark
87
79 88 def destupdate(repo, clean=False, check=False):
80 89 """destination for bare update operation
81 90
82 91 return (rev, movemark, activemark)
83 92
84 93 - rev: the revision to update to,
85 94 - movemark: node to move the active bookmark from
86 95 (cf bookmark.calculate update),
87 96 - activemark: a bookmark to activate at the end of the update.
88 97 """
89 98 node = None
90 99 wc = repo[None]
91 100 movemark = activemark = None
92 101
93 102 node, movemark, activemark = _destupdateobs(repo, clean, check)
103 if node is None:
104 node, movemark, activemark = _destupdatebook(repo, clean, check)
94 105
95 106 if node is None:
96 # we also move the active bookmark, if any
97 node, movemark = bookmarks.calculateupdate(repo.ui, repo, None)
98 if node is not None:
99 activemark = node
100
101 107 if node is None:
102 108 try:
103 109 node = repo.branchtip(wc.branch())
104 110 except error.RepoLookupError:
105 111 if wc.branch() == 'default': # no default branch!
106 112 node = repo.lookup('tip') # update to tip
107 113 else:
108 114 raise error.Abort(_("branch %s not found") % wc.branch())
109 115 rev = repo[node].rev()
110 116
111 117 _destupdatevalidate(repo, rev, clean, check)
112 118
113 119 return rev, movemark, activemark
114 120
115 121 def destmerge(repo):
116 122 if repo._activebookmark:
117 123 bmheads = repo.bookmarkheads(repo._activebookmark)
118 124 curhead = repo[repo._activebookmark].node()
119 125 if len(bmheads) == 2:
120 126 if curhead == bmheads[0]:
121 127 node = bmheads[1]
122 128 else:
123 129 node = bmheads[0]
124 130 elif len(bmheads) > 2:
125 131 raise error.Abort(_("multiple matching bookmarks to merge - "
126 132 "please merge with an explicit rev or bookmark"),
127 133 hint=_("run 'hg heads' to see all heads"))
128 134 elif len(bmheads) <= 1:
129 135 raise error.Abort(_("no matching bookmark to merge - "
130 136 "please merge with an explicit rev or bookmark"),
131 137 hint=_("run 'hg heads' to see all heads"))
132 138 else:
133 139 branch = repo[None].branch()
134 140 bheads = repo.branchheads(branch)
135 141 nbhs = [bh for bh in bheads if not repo[bh].bookmarks()]
136 142
137 143 if len(nbhs) > 2:
138 144 raise error.Abort(_("branch '%s' has %d heads - "
139 145 "please merge with an explicit rev")
140 146 % (branch, len(bheads)),
141 147 hint=_("run 'hg heads .' to see heads"))
142 148
143 149 parent = repo.dirstate.p1()
144 150 if len(nbhs) <= 1:
145 151 if len(bheads) > 1:
146 152 raise error.Abort(_("heads are bookmarked - "
147 153 "please merge with an explicit rev"),
148 154 hint=_("run 'hg heads' to see all heads"))
149 155 if len(repo.heads()) > 1:
150 156 raise error.Abort(_("branch '%s' has one head - "
151 157 "please merge with an explicit rev")
152 158 % branch,
153 159 hint=_("run 'hg heads' to see all heads"))
154 160 msg, hint = _('nothing to merge'), None
155 161 if parent != repo.lookup(branch):
156 162 hint = _("use 'hg update' instead")
157 163 raise error.Abort(msg, hint=hint)
158 164
159 165 if parent not in bheads:
160 166 raise error.Abort(_('working directory not at a head revision'),
161 167 hint=_("use 'hg update' or merge with an "
162 168 "explicit revision"))
163 169 if parent == nbhs[0]:
164 170 node = nbhs[-1]
165 171 else:
166 172 node = nbhs[0]
167 173 return repo[node].rev()
General Comments 0
You need to be logged in to leave comments. Login now