Commit Graph

183 Commits

Author SHA1 Message Date
Boris Feld
34737ca53d configitems: register the 'ui.mergemarkertemplate' config 2017-06-30 03:44:56 +02:00
Jun Wu
e47f7dc2fa codemod: register core configitems using a script
This is done by a script [2] using RedBaron [1], a tool designed for doing
code refactoring. All "default" values are decided by the script and are
strongly consistent with the existing code.

There are 2 changes done manually to fix tests:

  [warn] mercurial/exchange.py: experimental.bundle2-output-capture: default needs manual removal
  [warn] mercurial/localrepo.py: experimental.hook-track-tags: default needs manual removal

Since RedBaron is not confident about how to indent things [2].

[1]: https://github.com/PyCQA/redbaron
[2]: https://github.com/PyCQA/redbaron/issues/100
[3]:

#!/usr/bin/env python
# codemod_configitems.py - codemod tool to fill configitems
#
# Copyright 2017 Facebook, Inc.
#
# 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, print_function

import os
import sys

import redbaron

def readpath(path):
    with open(path) as f:
        return f.read()

def writepath(path, content):
    with open(path, 'w') as f:
        f.write(content)

_configmethods = {'config', 'configbool', 'configint', 'configbytes',
                  'configlist', 'configdate'}

def extractstring(rnode):
    """get the string from a RedBaron string or call_argument node"""
    while rnode.type != 'string':
        rnode = rnode.value
    return rnode.value[1:-1]  # unquote, "'str'" -> "str"

def uiconfigitems(red):
    """match *.ui.config* pattern, yield (node, method, args, section, name)"""
    for node in red.find_all('atomtrailers'):
        entry = None
        try:
            obj = node[-3].value
            method = node[-2].value
            args = node[-1]
            section = args[0].value
            name = args[1].value
            if (obj in ('ui', 'self') and method in _configmethods
                and section.type == 'string' and name.type == 'string'):
                entry = (node, method, args, extractstring(section),
                         extractstring(name))
        except Exception:
            pass
        else:
            if entry:
                yield entry

def coreconfigitems(red):
    """match coreconfigitem(...) pattern, yield (node, args, section, name)"""
    for node in red.find_all('atomtrailers'):
        entry = None
        try:
            args = node[1]
            section = args[0].value
            name = args[1].value
            if (node[0].value == 'coreconfigitem' and section.type == 'string'
                and name.type == 'string'):
                entry = (node, args, extractstring(section),
                         extractstring(name))
        except Exception:
            pass
        else:
            if entry:
                yield entry

def registercoreconfig(cfgred, section, name, defaultrepr):
    """insert coreconfigitem to cfgred AST

    section and name are plain string, defaultrepr is a string
    """
    # find a place to insert the "coreconfigitem" item
    entries = list(coreconfigitems(cfgred))
    for node, args, nodesection, nodename in reversed(entries):
        if (nodesection, nodename) < (section, name):
            # insert after this entry
            node.insert_after(
                'coreconfigitem(%r, %r,\n'
                '    default=%s,\n'
                ')' % (section, name, defaultrepr))
            return

def main(argv):
    if not argv:
        print('Usage: codemod_configitems.py FILES\n'
              'For example, FILES could be "{hgext,mercurial}/*/**.py"')
    dirname = os.path.dirname
    reporoot = dirname(dirname(dirname(os.path.abspath(__file__))))

    # register configitems to this destination
    cfgpath = os.path.join(reporoot, 'mercurial', 'configitems.py')
    cfgred = redbaron.RedBaron(readpath(cfgpath))

    # state about what to do
    registered = set((s, n) for n, a, s, n in coreconfigitems(cfgred))
    toregister = {} # {(section, name): defaultrepr}
    coreconfigs = set() # {(section, name)}, whether it's used in core

    # first loop: scan all files before taking any action
    for i, path in enumerate(argv):
        print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
        iscore = ('mercurial' in path) and ('hgext' not in path)
        red = redbaron.RedBaron(readpath(path))
        # find all repo.ui.config* and ui.config* calls, and collect their
        # section, name and default value information.
        for node, method, args, section, name in uiconfigitems(red):
            if section == 'web':
                # [web] section has some weirdness, ignore them for now
                continue
            defaultrepr = None
            key = (section, name)
            if len(args) == 2:
                if key in registered:
                    continue
                if method == 'configlist':
                    defaultrepr = 'list'
                elif method == 'configbool':
                    defaultrepr = 'False'
                else:
                    defaultrepr = 'None'
            elif len(args) >= 3 and (args[2].target is None or
                                     args[2].target.value == 'default'):
                # try to understand the "default" value
                dnode = args[2].value
                if dnode.type == 'name':
                    if dnode.value in {'None', 'True', 'False'}:
                        defaultrepr = dnode.value
                elif dnode.type == 'string':
                    defaultrepr = repr(dnode.value[1:-1])
                elif dnode.type in ('int', 'float'):
                    defaultrepr = dnode.value
            # inconsistent default
            if key in toregister and toregister[key] != defaultrepr:
                defaultrepr = None
            # interesting to rewrite
            if key not in registered:
                if defaultrepr is None:
                    print('[note] %s: %s.%s: unsupported default'
                          % (path, section, name))
                    registered.add(key) # skip checking it again
                else:
                    toregister[key] = defaultrepr
                    if iscore:
                        coreconfigs.add(key)

    # second loop: rewrite files given "toregister" result
    for path in argv:
        # reconstruct redbaron - trade CPU for memory
        red = redbaron.RedBaron(readpath(path))
        changed = False
        for node, method, args, section, name in uiconfigitems(red):
            key = (section, name)
            defaultrepr = toregister.get(key)
            if defaultrepr is None or key not in coreconfigs:
                continue
            if len(args) >= 3 and (args[2].target is None or
                                   args[2].target.value == 'default'):
                try:
                    del args[2]
                    changed = True
                except Exception:
                    # redbaron fails to do the rewrite due to indentation
                    # see https://github.com/PyCQA/redbaron/issues/100
                    print('[warn] %s: %s.%s: default needs manual removal'
                          % (path, section, name))
            if key not in registered:
                print('registering %s.%s' % (section, name))
                registercoreconfig(cfgred, section, name, defaultrepr)
                registered.add(key)
        if changed:
            print('updating %s' % path)
            writepath(path, red.dumps())

    if toregister:
        print('updating configitems.py')
        writepath(cfgpath, cfgred.dumps())

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
2017-07-14 14:22:40 -07:00
Phil Cohen
f7d70b9c31 filemerge: convert a couple of wvfs calls in internal mergetools to contexts
One hitch is that sometimes fcd is actually an absentfilectx which does not
expose any mutator functions. In order to still use the context functions,
we look up the underlying workingfilectx to perform the write there.

One alternate way would be to put the write functions on the absentfilectx and
have them pass-through. While this makes the callsites cleaner, we would need
to decide what its getter functions would return after this point, since
returning None for `data` (and True for `isabsent()`) might no longer be
correct after a write. I discussed with Sidd about just having the getters
raise RuntimeErrors after a mutator has been called, but we actually call
isabsent() in merge.py after running the internal merge tools.
2017-06-26 22:52:15 -07:00
Yuya Nishihara
41654e97a9 templater: add simple interface for unnamed template (API)
This provides a simpler API for callers which don't need full templating
stack. Instead of storing the given template as the name specified by topic,
use '' as the default template to be rendered.
2017-04-22 19:56:47 +09:00
Pulkit Goyal
228f493dac py3: convert bool variables to bytes 2017-06-02 16:57:21 +05:30
Stanislau Hlebik
32f126a2e0 filemerge: store error messages in module variables
Copytracing may be disabled because it's too slow (see
experimental.disablecopytrace config option). In that case user may get errors
like 'local changed FILE which other deleted'. It would be nice to give user a
hint to rerun command with `--config experimental.disablecopytrace=False`. To
make it possible let's extract error message to variables so that extension may
overwrite them.
2017-05-19 03:47:43 -07:00
FUJIWARA Katsunori
aa0e47b129 filemerge: add internal merge tool to dump files forcibly
Internal merge tool :dump implies premerge. Therefore, files aren't
dumped, if premerge runs successfully.

This undocumented behavior might confuse users, if they want to always
dump files. But just making :dump omit premerge might cause backward
compatibility issue for existing automation.

This patch adds new internal merge tool :forcedump, which works as
same as :dump, but omits premerge always.

Internal tools annotated with "nomerge" should merge "change and
delete" correctly, but _forcedump() can't. Therefore, it is annotated
with "mergeonly" to always omit premerge, even though it doesn't merge
files actually.

This patch also adds explanation about premerge to :dump, to clarify
how :dump actually works.

BTW, this patch specifies internal tools with "internal:" prefix in
newly added test scenario in test-merge-tools.t, even though this
prefix is already deprecated. This is only for similarity to other
tests in test-merge-tools.t.
2017-05-13 03:31:42 +09:00
FUJIWARA Katsunori
40936d94b1 filemerge: make warning message more i18n friendly
Before this patch, " specified for " part of warning messages
(e.g. "couldn't find merge tool TOOL specified for PAT") isn't
translatable.
2017-05-13 03:28:36 +09:00
FUJIWARA Katsunori
3018e1b77b filemerge: show warning about choice of :prompt only at an actual fallback
Before this patch, internal merge tool :prompt shows "no tool found to
merge FILE" line, even if :prompt is explicitly specified as a tool to
be used.

This patch shows warning message about choice of :prompt only at an
actual fallback, in which case any tool is rejected by capability for
binary or symlink.

It is for backward compatibility to omit warning message in
"changedelete" case.
2017-05-13 03:28:36 +09:00
Yuya Nishihara
8af2b4d601 filemerge: optionally strip quotes from merge marker template (BC)
For consistency with the other template options. Quotes are necessary if
you want to preserve leading/trailing whitespace, which would be stripped
by config parser.
2017-02-25 19:36:02 +09:00
Pierre-Yves David
4fa55e8b7e filemerge: explicitly tests for None
Changeset 3495cae22a41 removed the mutable default value, but did not explicitly
tested for None. Such implicit testing can introduce semantic and performance
issue. We move to an explicit testing for None as recommended by PEP8:

https://www.python.org/dev/peps/pep-0008/#programming-recommendations
2017-03-15 15:11:52 -07:00
Gregory Szorc
9bc6055676 filemerge: don't use mutable default argument value 2016-12-26 16:54:33 -07:00
Simon Farnsworth
17c528e46d filemerge: tag merge tool for blocked times
Merge tools can take a while - let's ensure that they're appropriately tagged
2017-03-06 03:19:40 -08:00
Pulkit Goyal
07314d0686 py3: convert the mode argument of os.fdopen to unicodes (1 of 2)
os.fdopen() does not accepts bytes as its second argument which represent the
mode in which the file is to be opened. This patch makes sure unicodes are
passed in py3 by using pycompat.sysstr().
2017-02-13 20:06:38 +05:30
Martin von Zweigbergk
d96c319788 merge: print status message before launching external merge tool
It seems somewhat common that people run into a merge conflict and
don't notice the launched merge tool, and instead they think hg just
hung. Let's print a message for each file that we launch a GUI merge
tool for.
2017-02-09 09:32:25 -08:00
Pulkit Goyal
bb08d44667 py3: replace os.environ with encoding.environ (part 3 of 5) 2016-12-18 01:54:36 +05:30
Mads Kiilerich
a31c060801 merge: use original file extension for temporary files
Some merge tools (like Araxis?) can pick merge mode based on the file
extension. That didn't work well when temporary files were given random
suffixes. It seems to work better when the random part is before the extension.

As usual, when using $output, $local will have the .orig extension. That could
perhaps be the subject of another change another day.
2016-11-23 23:47:38 +01:00
Kostia Balytskyi
279f8962a7 conflicts: make spacing consistent in conflict markers
The way default marker template was defined before this patch,
the spacing before dash in conflict markes was dependent on
whether changeset is a tip one or not. This is a relevant part
of template:
    '{ifeq(tags, "tip", "", "{tags} "}'
If revision is a tip revision with no other tags, this would
resolve to an empty string, but for revisions which are not tip
and don't have any other tags, this would resolve to a single
space string. In the end this causes weirdnesses like the ones
you can see in the affected tests.

This is a not a big deal, but double spacing may be visually
less pleasant.

Please note that test changes where commit hashes change are
the result of marking files as resolved without removing markers.
2016-11-19 15:41:37 -08:00
Augie Fackler
727b0d4499 filemerge: avoid shadowing a variable in a list comprehension 2016-11-10 16:33:07 -05:00
Pulkit Goyal
e4b15b1b8c py3: make format strings unicodes and not bytes
Fixes issues on Python 3, wherein docstrings are unicodes.
Shouldn't break anything on Python 2.
2016-10-08 16:10:58 +02:00
Simon Farnsworth
1b7185f6d1 merge: always use other, not remote, in user prompts
Now that we store and display merge labels in user prompts (not just
conflict markets), we should rely on labels to clarify the two sides of a
merge operation (hg merge, hg update, hg rebase etc).

"remote" is not a great name here, as it conflates "remote" as in "remote
server" with "remote" as in "the side of the merge that's further away". In
cases where you're merging the "wrong way" around, remote can even be the
"local" commit that you're merging with something pulled from the remote
server.
2016-08-12 05:56:40 -07:00
Simon Farnsworth
906104f96d merge: use labels in prompts to the user
Now that we persist the labels, we can consistently use the labels in
prompts for the user without risk of confusion. This changes a huge amount
of command output:

This means that merge prompts like:
  no tool found to merge a
  keep (l)ocal, take (o)ther, or leave (u)nresolved? u
and
  remote changed a which local deleted
  use (c)hanged version, leave (d)eleted, or leave (u)nresolved? c
become:
  no tool found to merge a
  keep (l)ocal [working copy], take (o)ther [destination], or leave (u)nresolved? u
and
  remote [source] changed a which local [dest] deleted
  use (c)hanged version, leave (d)eleted, or leave (u)nresolved? c
where "working copy" and "destination" were supplied by the command that
requested the merge as labels for conflict markers, and thus should be
human-friendly.
2016-08-12 06:01:42 -07:00
Yuya Nishihara
b432ce21ff merge: concatenate default conflict marker at parsing phase of .py
"+" operations are unnecessary.
2015-05-05 10:51:34 +09:00
Yuya Nishihara
84a8ba9511 templater: factor out function that creates templater from string template
This function will host loading of template aliases. It is not defined at
templater, but at formatter, since formatter is the module handling ui stuff
in front of templater.
2016-04-10 17:23:09 +09:00
Yuya Nishihara
3f981af86b templater: separate function to create templater from map file (API)
New frommapfile() function will make it clear when template aliases will be
loaded. They should be applied to command arguments and templates in hgrc,
but not to map files. Otherwise, our stock styles and web templates
(i.e map-file templates) could be modified unintentionally.

Future patches will add "aliases" argument to __init__(), but not to
frommapfile().
2016-04-03 23:26:48 +09:00
timeless
81737a7154 filemerge: use revset notation for p1/p2 of local/other descriptions 2016-03-17 00:36:01 +00:00
timeless
a136072319 filemerge: indicate that local/other are p1/p2 2016-03-17 00:36:01 +00:00
Siddharth Agarwal
0efe3372e4 origpath: move from cmdutil to scmutil
This is a lower-level function so it doesn't need to be in cmdutil, and putting
it here avoids a bunch of potential import cycle issues.
2016-01-02 03:02:57 -08:00
Siddharth Agarwal
1c3dcd1546 filemerge: default change/delete conflicts to 'leave unresolved' (BC)
It makes far more sense to leave these conflicts unresolved and kick back to
the user than to just assume that the local version be chosen. There are almost
certainly buggy scripts and applications using Mercurial in the wild that do
merges or rebases non-interactively, and then assume that if the operation
succeeded there's nothing the user needs to pay attention to.

(This wasn't possible earlier because there was no way to re-resolve
change/delete conflicts -- but now it is.)
2015-12-23 12:51:45 -08:00
Siddharth Agarwal
106adbe75b filemerge: default regular prompts to 'leave unresolved' (BC)
It makes far more sense to leave these conflicts unresolved and kick back to
the user than to just assume that the local version be chosen. There are almost
certainly buggy scripts and applications using Mercurial in the wild that do
merges or rebases non-interactively, and then assume that if the operation
succeeded there's nothing the user needs to pay attention to.
2015-12-01 09:48:38 -08:00
Siddharth Agarwal
7382fa0a4e filemerge: add a 'leave unresolved' option to change/delete prompts
We're going to make this option the default in an upcoming patch.
2015-11-30 13:43:55 -08:00
Siddharth Agarwal
7be324f9ab filemerge: add a 'leave unresolved' option to regular prompts
'Regular' here means anything that isn't a change/delete prompt. We'll add this
option to change/delete prompts in a subsequent patch.
2015-11-30 11:17:18 -08:00
Siddharth Agarwal
807dab9ed1 filemerge: add debug output for whether this is a change/delete conflict
Just like binary and symlink conflicts, change/delete conflicts influence the
tool picked.
2015-11-25 14:25:26 -08:00
Siddharth Agarwal
6775160009 filemerge: in ':prompt', use ':fail' tool rather than returning directly
The ':fail' tool now knows to write out the changed side for change/delete
conflicts.

This has no impact right now but will make things better when we move
change/delete conflicts in here.
2015-11-24 10:58:35 -08:00
Siddharth Agarwal
1fc099f7ba filemerge: in ':fail' tool, write out other side if local side is deleted
We do this because we don't want to modify the dirstate for failures, and don't
just want to leave the file missing from disk. Plus it's more useful for the
user if the changed side is written out -- it is easier to delete a file than
to get it back via hg revert.
2015-11-24 10:57:01 -08:00
Siddharth Agarwal
2264cfb327 filemerge: don't try to copy files known to be absent
We set 'back' to None in this case, so we need to handle that as well.
2015-11-14 00:00:46 -08:00
Siddharth Agarwal
830ec886fc filemerge: don't try using external tools on change/delete conflicts
This is mostly for completeness' sake -- the current code shouldn't get to this
point.
2015-11-13 23:57:43 -08:00
Siddharth Agarwal
5222eee8d6 filemerge: don't attempt to premerge change/delete conflicts
This is mostly for completeness' sake -- at the moment we don't support any
tools for change/delete conflicts that would do a premerge.
2015-11-13 23:56:00 -08:00
Siddharth Agarwal
a5600d30a6 filemerge._mergecheck: add check for change/delete conflicts
Merge tools that perform an actual 3-way merge can't handle change/delete
conflicts. This adds a check for that.
2015-11-13 23:58:05 -08:00
Siddharth Agarwal
1154e3eab0 filemerge._picktool: only pick from nomerge tools for change/delete conflicts
For --tool or HGMERGE, we could have either:
(a) proceeded with the particular tool, then failed the merge.
(b) chosen to prompt regardless.

We're explicitly choosing (b) here, because it's effectively what we've been
doing so far and helps maintain an easier-to-use interface.

However, in future patches we're going to change the default selection from
'pick changed version' to 'leave unresolved'. That fixes most of the brokenness
involved with choice (b).
2015-11-15 21:40:15 -08:00
Siddharth Agarwal
dfb75ef0b1 filemerge: add support for change/delete conflicts to the ':prompt' tool
We haven't added the 'leave unresolved' option yet -- that will come in a
future patch.
2015-11-13 23:52:26 -08:00
Siddharth Agarwal
1b647216ac filemerge: add support for change/delete conflicts to the ':other' merge tool
This, along with the previous patch to the :local merge tool, covers the full
matrix of change/delete conflicts.
2015-11-18 15:41:50 -08:00
Siddharth Agarwal
b3c05df6b2 filemerge: add support for change/delete conflicts to the ':local' merge tool
This covers two of the four cases of change/delete conflicts -- in an upcoming
patch we'll make :other cover the other two.
2015-11-18 15:40:28 -08:00
Siddharth Agarwal
4904ded295 filemerge: return whether the file was deleted
This is required for change/delete conflict resolution -- see previous patches
for more details.
2015-11-18 14:22:52 -08:00
Siddharth Agarwal
c33d958db2 filemerge: return whether the file is deleted from all other merge tools
This is required for change/delete conflicts -- see the previous patch for more
information.
2015-11-18 13:55:31 -08:00
Siddharth Agarwal
4155f9cde2 filemerge: return whether the file is deleted for nomerge internal tools
We're going to support the filemerge code resolving change/delete conflicts in
upcoming patches. Some of these resolutions require that the dirstate be
modified. Modifying the dirstate directly from in here would be (a) a pretty
bad layering violation and (b) wrong because all dirstate removals should
happen before adds. So in this and upcoming patches we're instead going to pass
whether the file is deleted up to merge.mergestate, then in there figure out
what dirstate action needs to be taken.
2015-11-18 13:52:28 -08:00
Siddharth Agarwal
d26567458a filemerge: introduce class whose objects represent files not in a context
Most code is going to barf at the return values here (particularly from data
and size), so we restrict it to the filemerge code.

This is already somewhat supported via:

  ctx.filectx(f, fileid=nullid)

Indeed, for add/add conflicts (ancestor doesn't have the file) we use precisely
that. However, that is broken in subtle ways:
- The cmp() function in filectx returns False (identical) for such a filectx
  when compared to a zero-length file.
- size() returns 0 rather than some sort of value indicating that the file isn't
  present.
- data() returns '' rather than some sort of value indicating that the file isn't
  present.

Given the relatively niche use of such filectxes, this seems to be the simplest
way to fix all these issues.
2015-11-16 11:45:35 -08:00
Martin von Zweigbergk
b2254eccbe filemerge: remove leading space from " no tool found..."
I could not find or see a reason for the unusual formatting. The lines
following it in the test cases are not indented.
2015-11-11 10:19:11 -08:00
Siddharth Agarwal
482e3440bf filemerge: rename _symlinkcheck to _mergecheck
We're going to be adding other checks in here soon.
2015-11-11 17:34:28 -08:00
Christian Delahousse
4642f4298b filemerge: choose where .orig files are kept
Having .orig files litter your working copy is a common complaint. This patch
uses cmdutil.orig to let the user determine where those files should reside.
2015-11-10 16:25:59 -08:00