Mercurial > hg
view mercurial/registrar.py @ 32979:66117dae87f9
patch: rewrite reversehunks (issue5337)
The old reversehunks code accesses "crecord.uihunk._hunk", which is the raw
recordhunk without crecord selection information, therefore "revert -i"
cannot revert individual lines, aka. issue5337.
The patch rewrites related logic to return the right reverse hunk for
revert. Namely,
1. "fromline" and "toline" are correctly swapped [1]
2. crecord.uihunk generates a correct reverse hunk [2]
Besides, reversehunks(hunks) will no longer modify its input "hunks", which
is more expected.
[1]: To explain why "fromline" and "toline" need to be swapped, take the
following example:
$ cat > a <<EOF
> 1
> 2
> 3
> 4
> EOF
$ cat > b <<EOF
> 2
> 3
> 5
> EOF
$ diff a b
1d0 <---- "1" is "fromline" and "0" is "toline"
< 1 and they are swapped if diff from the reversed direction
4c3 |
< 4 |
--- |
> 5 |
|
$ diff b a |
0a1 <---------+
> 1
3c4 <---- also "4c3" gets swapped to "3c4"
< 5
---
> 4
[2]: This is a bit tricky.
For example, given a file which is empty in working parent but has 3 lines
in working copy, and the user selection:
select hunk to discard
[x] +1
[ ] +2
[x] +3
The user intent is to drop "1" and "3" in working copy but keep "2", so the
reverse patch would be something like:
-1
2 (2 is a "context line")
-3
We cannot just take all selected lines and swap "-" and "+", which will be:
-1
-3
That patch won't apply because of "2". So the correct way is to insert "2"
as a "context line" by inserting it first then deleting it:
-2
+2
Therefore, the correct revert patch is:
-1
-2
+2
-3
It could be reordered to look more like a common diff hunk:
-1
-2
-3
+2
Note: It's possible to return multiple hunks so there won't be lines like
"-2", "+2". But the current implementation is much simpler.
For deletions, like the working parent has "1\n2\n3\n" and it was changed to
empty in working copy:
select hunk to discard
[x] -1
[ ] -2
[x] -3
The user intent is to drop the deletion of 1 and 3 (in other words, keep
those lines), but still delete "2".
The reverse patch is meant to be applied to working copy which is empty.
So the patch would be:
+1
+3
That is to say, there is no need to special handle the unselected "2" like
the above insertion case.
author | Jun Wu <quark@fb.com> |
---|---|
date | Tue, 20 Jun 2017 23:22:38 -0700 |
parents | 92de09a05d7f |
children | c467d13334ee |
line wrap: on
line source
# registrar.py - utilities to register function for specific purpose # # Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. from __future__ import absolute_import from . import ( error, pycompat, util, ) class _funcregistrarbase(object): """Base of decorator to register a function for specific purpose This decorator stores decorated functions into own dict 'table'. The least derived class can be defined by overriding 'formatdoc', for example:: class keyword(_funcregistrarbase): _docformat = ":%s: %s" This should be used as below: keyword = registrar.keyword() @keyword('bar') def barfunc(*args, **kwargs): '''Explanation of bar keyword .... ''' pass In this case: - 'barfunc' is stored as 'bar' in '_table' of an instance 'keyword' above - 'barfunc.__doc__' becomes ":bar: Explanation of bar keyword" """ def __init__(self, table=None): if table is None: self._table = {} else: self._table = table def __call__(self, decl, *args, **kwargs): return lambda func: self._doregister(func, decl, *args, **kwargs) def _doregister(self, func, decl, *args, **kwargs): name = self._getname(decl) if name in self._table: msg = 'duplicate registration for name: "%s"' % name raise error.ProgrammingError(msg) if func.__doc__ and not util.safehasattr(func, '_origdoc'): doc = pycompat.sysbytes(func.__doc__).strip() func._origdoc = doc func.__doc__ = pycompat.sysstr(self._formatdoc(decl, doc)) self._table[name] = func self._extrasetup(name, func, *args, **kwargs) return func def _parsefuncdecl(self, decl): """Parse function declaration and return the name of function in it """ i = decl.find('(') if i >= 0: return decl[:i] else: return decl def _getname(self, decl): """Return the name of the registered function from decl Derived class should override this, if it allows more descriptive 'decl' string than just a name. """ return decl _docformat = None def _formatdoc(self, decl, doc): """Return formatted document of the registered function for help 'doc' is '__doc__.strip()' of the registered function. """ return self._docformat % (decl, doc) def _extrasetup(self, name, func): """Execute exra setup for registered function, if needed """ pass class command(_funcregistrarbase): """Decorator to register a command function to table This class receives a command table as its argument. The table should be a dict. The created object can be used as a decorator for adding commands to that command table. This accepts multiple arguments to define a command. The first argument is the command name. The options argument is an iterable of tuples defining command arguments. See ``mercurial.fancyopts.fancyopts()`` for the format of each tuple. The synopsis argument defines a short, one line summary of how to use the command. This shows up in the help output. The norepo argument defines whether the command does not require a local repository. Most commands operate against a repository, thus the default is False. The optionalrepo argument defines whether the command optionally requires a local repository. The inferrepo argument defines whether to try to find a repository from the command line arguments. If True, arguments will be examined for potential repository locations. See ``findrepo()``. If a repository is found, it will be used. """ def _doregister(self, func, name, options=(), synopsis=None, norepo=False, optionalrepo=False, inferrepo=False): func.norepo = norepo func.optionalrepo = optionalrepo func.inferrepo = inferrepo if synopsis: self._table[name] = func, list(options), synopsis else: self._table[name] = func, list(options) return func class revsetpredicate(_funcregistrarbase): """Decorator to register revset predicate Usage:: revsetpredicate = registrar.revsetpredicate() @revsetpredicate('mypredicate(arg1, arg2[, arg3])') def mypredicatefunc(repo, subset, x): '''Explanation of this revset predicate .... ''' pass The first string argument is used also in online help. Optional argument 'safe' indicates whether a predicate is safe for DoS attack (False by default). Optional argument 'takeorder' indicates whether a predicate function takes ordering policy as the last argument. 'revsetpredicate' instance in example above can be used to decorate multiple functions. Decorated functions are registered automatically at loading extension, if an instance named as 'revsetpredicate' is used for decorating in extension. Otherwise, explicit 'revset.loadpredicate()' is needed. """ _getname = _funcregistrarbase._parsefuncdecl _docformat = "``%s``\n %s" def _extrasetup(self, name, func, safe=False, takeorder=False): func._safe = safe func._takeorder = takeorder class filesetpredicate(_funcregistrarbase): """Decorator to register fileset predicate Usage:: filesetpredicate = registrar.filesetpredicate() @filesetpredicate('mypredicate()') def mypredicatefunc(mctx, x): '''Explanation of this fileset predicate .... ''' pass The first string argument is used also in online help. Optional argument 'callstatus' indicates whether a predicate implies 'matchctx.status()' at runtime or not (False, by default). Optional argument 'callexisting' indicates whether a predicate implies 'matchctx.existing()' at runtime or not (False, by default). 'filesetpredicate' instance in example above can be used to decorate multiple functions. Decorated functions are registered automatically at loading extension, if an instance named as 'filesetpredicate' is used for decorating in extension. Otherwise, explicit 'fileset.loadpredicate()' is needed. """ _getname = _funcregistrarbase._parsefuncdecl _docformat = "``%s``\n %s" def _extrasetup(self, name, func, callstatus=False, callexisting=False): func._callstatus = callstatus func._callexisting = callexisting class _templateregistrarbase(_funcregistrarbase): """Base of decorator to register functions as template specific one """ _docformat = ":%s: %s" class templatekeyword(_templateregistrarbase): """Decorator to register template keyword Usage:: templatekeyword = registrar.templatekeyword() @templatekeyword('mykeyword') def mykeywordfunc(repo, ctx, templ, cache, revcache, **args): '''Explanation of this template keyword .... ''' pass The first string argument is used also in online help. 'templatekeyword' instance in example above can be used to decorate multiple functions. Decorated functions are registered automatically at loading extension, if an instance named as 'templatekeyword' is used for decorating in extension. Otherwise, explicit 'templatekw.loadkeyword()' is needed. """ class templatefilter(_templateregistrarbase): """Decorator to register template filer Usage:: templatefilter = registrar.templatefilter() @templatefilter('myfilter') def myfilterfunc(text): '''Explanation of this template filter .... ''' pass The first string argument is used also in online help. 'templatefilter' instance in example above can be used to decorate multiple functions. Decorated functions are registered automatically at loading extension, if an instance named as 'templatefilter' is used for decorating in extension. Otherwise, explicit 'templatefilters.loadkeyword()' is needed. """ class templatefunc(_templateregistrarbase): """Decorator to register template function Usage:: templatefunc = registrar.templatefunc() @templatefunc('myfunc(arg1, arg2[, arg3])', argspec='arg1 arg2 arg3') def myfuncfunc(context, mapping, args): '''Explanation of this template function .... ''' pass The first string argument is used also in online help. If optional 'argspec' is defined, the function will receive 'args' as a dict of named arguments. Otherwise 'args' is a list of positional arguments. 'templatefunc' instance in example above can be used to decorate multiple functions. Decorated functions are registered automatically at loading extension, if an instance named as 'templatefunc' is used for decorating in extension. Otherwise, explicit 'templater.loadfunction()' is needed. """ _getname = _funcregistrarbase._parsefuncdecl def _extrasetup(self, name, func, argspec=None): func._argspec = argspec