Mercurial > public > mercurial-scm > hg
comparison mercurial/context.py @ 37174:bb47dc2f71a0
context: move reuse of context object to repo.__getitem__ (API)
As an example of how weird the basectx.__new__ is: whenever you create
a workingctx, basectx.__new__ gets called first. Since our __new__ has
a "changeid" argument as second parameter, when create the
workingctx(repo, text="blah"), the text gets bound to
"changeid". Since a string isn't a basectx, our __new__ ends up not
doing anything funny, but that's still very confusing code.
Another case is metadataonlyctx.__new__(), which I think exists in
order to prevent metadataonlyctx.__init__'s third argument
(originalctx) from being interpreted as a changeid in
basectx.__new__(), thereby getting reused.
Let's move this to repo.__getitem__ instead, where it will be pretty
obvious what the code does.
After this patch, changectx(ctx) will be an error (it will fail when
trying to see if it's a 20-byte string).
Differential Revision: https://phab.mercurial-scm.org/D2969
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 29 Mar 2018 22:51:45 -0700 |
parents | 05ff1a155a21 |
children | fbe34945220d |
comparison
equal
deleted
inserted
replaced
37173:05ff1a155a21 | 37174:bb47dc2f71a0 |
---|---|
60 changectx: read-only context that is already present in the repo, | 60 changectx: read-only context that is already present in the repo, |
61 workingctx: a context that represents the working directory and can | 61 workingctx: a context that represents the working directory and can |
62 be committed, | 62 be committed, |
63 memctx: a context that represents changes in-memory and can also | 63 memctx: a context that represents changes in-memory and can also |
64 be committed.""" | 64 be committed.""" |
65 def __new__(cls, repo, changeid='', *args, **kwargs): | |
66 if isinstance(changeid, basectx): | |
67 return changeid | |
68 | |
69 return super(basectx, cls).__new__(cls) | |
70 | 65 |
71 def __bytes__(self): | 66 def __bytes__(self): |
72 return short(self.node()) | 67 return short(self.node()) |
73 | 68 |
74 __str__ = encoding.strmethod(__bytes__) | 69 __str__ = encoding.strmethod(__bytes__) |
409 """A changecontext object makes access to data related to a particular | 404 """A changecontext object makes access to data related to a particular |
410 changeset convenient. It represents a read-only context already present in | 405 changeset convenient. It represents a read-only context already present in |
411 the repo.""" | 406 the repo.""" |
412 def __init__(self, repo, changeid='.'): | 407 def __init__(self, repo, changeid='.'): |
413 """changeid is a revision number, node, or tag""" | 408 """changeid is a revision number, node, or tag""" |
414 | |
415 # since basectx.__new__ already took care of copying the object, we | |
416 # don't need to do anything in __init__, so we just exit here | |
417 if isinstance(changeid, basectx): | |
418 return | |
419 | 409 |
420 if changeid == '': | 410 if changeid == '': |
421 changeid = '.' | 411 changeid = '.' |
422 self._repo = repo | 412 self._repo = repo |
423 | 413 |
2484 user receives the committer name and defaults to current repository | 2474 user receives the committer name and defaults to current repository |
2485 username, date is the commit date in any format supported by | 2475 username, date is the commit date in any format supported by |
2486 dateutil.parsedate() and defaults to current date, extra is a dictionary of | 2476 dateutil.parsedate() and defaults to current date, extra is a dictionary of |
2487 metadata or is left empty. | 2477 metadata or is left empty. |
2488 """ | 2478 """ |
2489 def __new__(cls, repo, originalctx, *args, **kwargs): | |
2490 return super(metadataonlyctx, cls).__new__(cls, repo) | |
2491 | |
2492 def __init__(self, repo, originalctx, parents=None, text=None, user=None, | 2479 def __init__(self, repo, originalctx, parents=None, text=None, user=None, |
2493 date=None, extra=None, editor=False): | 2480 date=None, extra=None, editor=False): |
2494 if text is None: | 2481 if text is None: |
2495 text = originalctx.description() | 2482 text = originalctx.description() |
2496 super(metadataonlyctx, self).__init__(repo, text, user, date, extra) | 2483 super(metadataonlyctx, self).__init__(repo, text, user, date, extra) |