Mercurial > hg
comparison mercurial/localrepo.py @ 39958:3d35304bd09b
context: move logic from changectx.__init__ to localrepo.__getitem__ (API)
My motivation for this change was to make repo[node] not load the
dirstate (more about that in the next patch), but I think it makes
more sense this way too. For example, raising RepoLookupError seems to
belong better in the repo lookup function
(i.e. localrepo.__getitem__).
This makes the changectx constructor very simple -- it just assigns
the given repo, revnum, and nodeid to properties.
Differential Revision: https://phab.mercurial-scm.org/D4827
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Wed, 26 Sep 2018 22:53:14 -0700 |
parents | e1e3d1b498d3 |
children | 43d3b09b3e5a |
comparison
equal
deleted
inserted
replaced
39957:e1e3d1b498d3 | 39958:3d35304bd09b |
---|---|
15 import time | 15 import time |
16 import weakref | 16 import weakref |
17 | 17 |
18 from .i18n import _ | 18 from .i18n import _ |
19 from .node import ( | 19 from .node import ( |
20 bin, | |
20 hex, | 21 hex, |
21 nullid, | 22 nullid, |
23 nullrev, | |
22 short, | 24 short, |
23 ) | 25 ) |
24 from . import ( | 26 from . import ( |
25 bookmarks, | 27 bookmarks, |
26 branchmap, | 28 branchmap, |
1212 # wdirrev isn't contiguous so the slice shouldn't include it | 1214 # wdirrev isn't contiguous so the slice shouldn't include it |
1213 return [self[i] | 1215 return [self[i] |
1214 for i in pycompat.xrange(*changeid.indices(len(self))) | 1216 for i in pycompat.xrange(*changeid.indices(len(self))) |
1215 if i not in self.changelog.filteredrevs] | 1217 if i not in self.changelog.filteredrevs] |
1216 try: | 1218 try: |
1217 return context.changectx(self, changeid) | 1219 if isinstance(changeid, int): |
1220 node = self.changelog.node(changeid) | |
1221 rev = changeid | |
1222 return context.changectx(self, rev, node) | |
1223 elif changeid == 'null': | |
1224 node = nullid | |
1225 rev = nullrev | |
1226 return context.changectx(self, rev, node) | |
1227 elif changeid == 'tip': | |
1228 node = self.changelog.tip() | |
1229 rev = self.changelog.rev(node) | |
1230 return context.changectx(self, rev, node) | |
1231 elif (changeid == '.' | |
1232 or self.local() and changeid == self.dirstate.p1()): | |
1233 # this is a hack to delay/avoid loading obsmarkers | |
1234 # when we know that '.' won't be hidden | |
1235 node = self.dirstate.p1() | |
1236 rev = self.unfiltered().changelog.rev(node) | |
1237 return context.changectx(self, rev, node) | |
1238 elif len(changeid) == 20: | |
1239 try: | |
1240 node = changeid | |
1241 rev = self.changelog.rev(changeid) | |
1242 return context.changectx(self, rev, node) | |
1243 except error.FilteredLookupError: | |
1244 changeid = hex(changeid) # for the error message | |
1245 raise | |
1246 except LookupError: | |
1247 # check if it might have come from damaged dirstate | |
1248 # | |
1249 # XXX we could avoid the unfiltered if we had a recognizable | |
1250 # exception for filtered changeset access | |
1251 if (self.local() | |
1252 and changeid in self.unfiltered().dirstate.parents()): | |
1253 msg = _("working directory has unknown parent '%s'!") | |
1254 raise error.Abort(msg % short(changeid)) | |
1255 changeid = hex(changeid) # for the error message | |
1256 | |
1257 elif len(changeid) == 40: | |
1258 try: | |
1259 node = bin(changeid) | |
1260 rev = self.changelog.rev(node) | |
1261 return context.changectx(self, rev, node) | |
1262 except error.FilteredLookupError: | |
1263 raise | |
1264 except LookupError: | |
1265 pass | |
1266 else: | |
1267 raise error.ProgrammingError( | |
1268 "unsupported changeid '%s' of type %s" % | |
1269 (changeid, type(changeid))) | |
1270 | |
1271 except (error.FilteredIndexError, error.FilteredLookupError): | |
1272 raise error.FilteredRepoLookupError(_("filtered revision '%s'") | |
1273 % pycompat.bytestr(changeid)) | |
1274 except IndexError: | |
1275 pass | |
1218 except error.WdirUnsupported: | 1276 except error.WdirUnsupported: |
1219 return context.workingctx(self) | 1277 return context.workingctx(self) |
1278 raise error.RepoLookupError( | |
1279 _("unknown revision '%s'") % changeid) | |
1220 | 1280 |
1221 def __contains__(self, changeid): | 1281 def __contains__(self, changeid): |
1222 """True if the given changeid exists | 1282 """True if the given changeid exists |
1223 | 1283 |
1224 error.AmbiguousPrefixLookupError is raised if an ambiguous node | 1284 error.AmbiguousPrefixLookupError is raised if an ambiguous node |