Commit Graph

6229 Commits

Author SHA1 Message Date
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
Martin von Zweigbergk
f3c48a5fe0 sparse: override dirstate.walk() instead of dirstate._ignore
Instead of treating files that are outside the sparse config as
ignored, this makes it so we list only those that are within the
sparse config by passing the sparse matcher to dirstate.walk().

Once we add support for narrow (sparseness applied to history, not
just working copy), we will need to do a similar restriction of the
walk over manifests, so this will be more consistent then. It also
simplifies the code a bit.

Note that a side-effect of this change is that files outside the
sparse config used to be listed as ignored, but they will now not be
listed at all. This can be seen in the test case where "hg purge" no
longer has any effect because it doesn't see that the files outside
the space config exist. To fix that, I think we should add an option
to dirstate.walk() to walk outside the sparse config. We might expose
that to the user as --no-sparse flag to e.g. "hg status" and "hg
purge", but that's work for another day.

Differential Revision: https://phab.mercurial-scm.org/D59
2017-07-11 10:46:35 -07:00
Boris Feld
a55b1d3f9f bookmark: use 'applychanges' in the mq extension 2017-07-10 17:44:25 +02:00
Boris Feld
8cb892d933 bookmark: use 'applychanges' when stripping 2017-07-10 17:37:48 +02:00
Boris Feld
316d61deba bookmark: use 'applychanges' in the convert extension 2017-07-10 17:30:20 +02:00
Boris Feld
97880179f7 bookmark: use 'applychanges' when updating bookmark in histedit 2017-07-10 17:28:53 +02:00
Boris Feld
401ff7a191 bugzilla: move the default regexp for fix in the config declaration
This mimic the change requested by Yuya for '_default_bug_re'.
2017-07-14 16:17:37 +02:00
Boris Feld
686cdbf81b configitems: register the 'bugzilla.version' config 2017-07-07 10:04:21 +02:00
Boris Feld
bb132489a6 configitems: register the 'bugzilla.usermap' config 2017-07-07 10:04:19 +02:00
Boris Feld
25a3b99028 configitems: register the 'bugzilla.user' config 2017-07-07 10:04:17 +02:00
Boris Feld
24b9a8bd04 configitems: register the 'bugzilla.timeout' config 2017-07-07 10:04:15 +02:00
Boris Feld
451b8af3c0 configitems: register the 'bugzilla.template' config 2017-07-07 10:04:13 +02:00
Boris Feld
c6fb9fca34 configitems: register the 'bugzilla.style' config 2017-07-07 10:04:11 +02:00
Boris Feld
d2874aad08 configitems: register the 'bugzilla.strip' config 2017-07-07 10:04:09 +02:00
Boris Feld
6c9b5d378e configitems: register the 'bugzilla.regexp' config
The default value is moved from the class to the config registration.
2017-07-07 10:04:07 +02:00
Boris Feld
1d64a7418f convert: use the new 'phase.registernew' function 2017-07-11 00:59:23 +02:00
Martin von Zweigbergk
cce08d0996 histedit: extract InterventionRequired transaction handling to utils
rebase will have similar logic, so let's extract it. Besides, it makes
the histedit code more readable.

We may want to parametrize acceptintervention() by the exception(s)
that should result in transaction close.

Differential Revision: https://phab.mercurial-scm.org/D66
2017-07-12 13:57:03 -07:00
Martin von Zweigbergk
7259b500cc histedit: create transaction outside of try
Just a little refactoring to simplify the next patch.

Differential Revision: https://phab.mercurial-scm.org/D65
2017-07-12 13:17:49 -07:00
Martin von Zweigbergk
018f2d955f histedit: remove transaction from state object
The transaction is only used within a single function, so no need to
store it on the state object.

Differential Revision: https://phab.mercurial-scm.org/D64
2017-07-12 11:18:02 -07:00
Adam Simpkins
2a467faccd dirstate: update backup functions to take full backup filename
Update the dirstate functions so that the caller supplies the full backup
filename rather than just a prefix and suffix.

The localrepo code was already hard-coding the fact that the backup name must
be (exactly prefix + "dirstate" + suffix): it relied on this in _journalfiles()
and undofiles().  Making the caller responsible for specifying the full backup
name removes the need for the localrepo code to assume that dirstate._filename
is always "dirstate".

Differential Revision: https://phab.mercurial-scm.org/D68
2017-07-12 15:24:07 -07:00
Jun Wu
c41f5ca68e codemod: simplify nested withs
This is the result of running:

  python codemod_nestedwith.py **/*.py

where codemod_nestedwith.py looks like this:

#!/usr/bin/env python
# codemod_nestedwith.py - codemod tool to rewrite nested with
#
# 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 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)

def main(argv):
    if not argv:
        print('Usage: codemod_nestedwith.py FILES')

    for i, path in enumerate(argv):
        print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
        changed = False
        red = redbaron.RedBaron(readpath(path))
        processed = set()
        for node in red.find_all('with'):
            if node in processed or node.type != 'with':
                continue
            top = node
            child = top[0]
            while True:
                if len(top) > 1 or child.type != 'with':
                    break
                # estimate line length after merging two "with"s
                new = '%swith %s:' % (top.indentation, top.contexts.dumps())
                new += ', %s' % child.contexts.dumps()
                # only do the rewrite if the end result is within 80 chars
                if len(new) > 80:
                    break
                processed.add(child)
                top.contexts.extend(child.contexts)
                top.value = child.value
                top.value.decrease_indentation(4)
                child = child[0]
                changed = True
        if changed:
            print('updating %s' % path)
            writepath(path, red.dumps())

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))


Differential Revision: https://phab.mercurial-scm.org/D77
2017-07-13 18:31:35 -07:00
Boris Feld
c9fe43d98b repovfs: add a ward to check if locks are properly taken
When the appropriate developer warnings are enabled, We wrap 'repo.vfs.audit' to
check for locks when accessing file in '.hg' for writing. Another changeset will
add a 'ward' for the store vfs (svfs).

This check system has caught a handful of locking issues that have been fixed
in previous series (mostly in 4.0). I expect another batch to be caught in third
party extensions.

We introduce two real exceptions from extensions 'blackbox.log' (because a lot of
read-only operations add entry to it), and 'last-email.txt' (because 'hg email'
is currently a read only operation and there is value to keep it this way).

In addition we are currently allowing bisect to operate outside of the lock
because the current code is a bit hard to get properly locked for now. Multiple
clean up have been made but there is still a couple of them to do and the freeze
is coming.
2017-07-11 12:38:17 +02:00
Boris Feld
7b61f9660d configitems: register the 'bugzilla.password' config 2017-07-07 10:04:05 +02:00
Jun Wu
ac575fa28d amend: new extension providing the amend command
Various third parties have implemented the `amend` command, which is in high
demand. This patch adds it as an experimental extension so its interface
could be formalized in core directly.

Since `commit --amend` is basically what `amend` should do. The command is
just a thin wrapper around `commit --amend` and just prevent the editor from
popping up by passing `--message`.
2017-07-11 20:53:55 -07:00
Boris Feld
b9e133d9eb configitems: register the 'bugzilla.host' config 2017-07-07 10:04:01 +02:00
Boris Feld
a7ef58bfca configitems: register the 'bugzilla.fixstatus' config 2017-07-07 10:03:57 +02:00
Boris Feld
aff813dbc9 configitems: register the 'bugzilla.fixresolution' config 2017-07-07 10:05:40 +02:00
Boris Feld
809c3cabc9 configitems: register the 'bugzilla.fixregexp' config 2017-07-07 10:03:36 +02:00
Boris Feld
7b06a431b5 configitems: register the 'bugzilla.db' config 2017-07-07 10:03:34 +02:00
Boris Feld
5441be5220 configitems: register the 'bugzilla.bzuser' config 2017-07-07 10:03:31 +02:00
Boris Feld
589f730567 configitems: register the 'bugzilla.bzurl' config 2017-07-07 10:03:28 +02:00
Boris Feld
f0dd13014a configitems: register the 'bugzilla.bzemail' config 2017-07-07 10:03:26 +02:00
Boris Feld
f8e8380065 configitems: register the 'bugzilla.bzdir' config 2017-07-07 10:03:24 +02:00
Boris Feld
ccb17357a1 configitems: register the 'bugzilla.apikey' config 2017-07-07 10:03:22 +02:00
FUJIWARA Katsunori
cc2b59230c convert: transcode CVS log messages by specified encoding (issue5597)
Converting from CVS to Mercurial assumes that CVS log messages in "cvs
rlog" output are encoded in UTF-8 (or basic Latin-1). But cvs itself
is usually unaware of encoding of log messages, in practice.

Therefore, if there are commits, of which log message is encoded in
other than UTF-8, log message of corresponded revisions in the
converted repository will be broken.

To avoid such broken log messages, this patch transcodes CVS log
messages by encoding specified via "convert.cvsps.logencoding"
configuration.

This patch accepts multiple encoding for convenience, because
"multiple encoding mixed in a repository" easily occurs. For example,
UTF-8 (recent POSIX), cp932 (Windows), and EUC-JP (legacy POSIX) are
well known encoding for Japanese.
2017-07-11 02:10:04 +09:00
FUJIWARA Katsunori
080baefbeb fsmonitor: execute setup procedures only if dirstate is already instantiated
Before this patch, reposetup() of fsmonitor executes setup procedures
for dirstate, even if it isn't yet instantiated at that time.

On the other hand, dirstate might be already instantiated before
reposetup() intentionally (prefilling by chg, for example, see
69de86112468 for detail). If so, just discarding already instantiated
one in reposetup() causes issue.

To resolve both issues above, this patch executes setup procedures,
only if dirstate is already instantiated.

BTW, this patch removes "del repo.unfiltered().__dict__['dirstate']",
because it is responsibility of the code path, which causes
instantiation of dirstate before reposetup(). After this patch, using
localrepo.isfilecached() should avoid creating the corresponded entry
in repo.unfiltered().__dict__.
2017-07-10 23:09:52 +09:00
FUJIWARA Katsunori
5c0a559866 fsmonitor: centralize setup procedures for dirstate 2017-07-10 23:09:52 +09:00
FUJIWARA Katsunori
cb69e5fb17 fsmonitor: avoid needless instantiation of dirstate
Using repo.local() instead of util.safehasattr(repo, 'dirstate') also
avoids executing setup procedures for remote repository (including
statichttprepo).

This is reason why this patch also removes a part of subsequent
comment, and try/except for AttributeError at accessing to repo.wvfs.
2017-07-10 23:09:52 +09:00
FUJIWARA Katsunori
3df6cdcbd7 journal: use wrapfilecache instead of wrapfunction on func of filecache
wrapfilecache() on filecache-ed property works more strictly than
wrapfunction() directly on func() of filecache.
2017-07-10 23:09:51 +09:00
FUJIWARA Katsunori
c7a3756542 journal: execute setup procedures for already instantiated dirstate
If dirstate is instantiated before reposetup() of journal extension,
it doesn't have "journalstorage" property, even if it is instantiated
via wrapdirstate() wrapping repo.dirstate(), because wrapdirstate()
works as same as original one before marking repo as "journal"-ing in
reposetup().

This issue can be reproduced by running test-journal.t or
test-journal-share.t with fsmonitor-run-tests.py.

On the other hand, just discarding already instantiated dirstate in
reposetup() prevents chg from filling dirstate before reposetup() (see
69de86112468 for detail).

Therefore, this patch executes setup procedures for already
instantiated dirstate explicitly in reposetup().

To centralize setup procedures for dirstate, this patch also factors
them out from wrapdirstate().
2017-07-10 23:09:51 +09:00
Gregory Szorc
0ee6ecfbec sparse: move config updating function into core
As part of the move, the ui argument was dropped.

Additional fixups will be made in a follow-up commit.
2017-07-10 21:39:49 -07:00
Gregory Szorc
a7c49e2ec2 dirstate: expose a sparse matcher on dirstate (API)
The sparse extension performs a lot of monkeypatching of dirstate
to make it sparse aware. Essentially, various operations need to
take the active sparse config into account. They do this by obtaining
a matcher representing the sparse config and filtering paths through
it.

The monkeypatching is done by stuffing a reference to a repo on
dirstate and calling sparse.matcher() (which takes a repo instance)
during each function call. The reason this function takes a repo
instance is because resolving the sparse config may require resolving
file contents from filelogs, and that requires a repo. (If the
current sparse config references "profile" files, the contents of
those files from the dirstate's parent revisions is resolved.)

I seem to recall people having strong opinions that the dirstate
object not have a reference to a repo. So copying what the sparse
extension does probably won't fly in core. Plus, the dirstate
modifications shouldn't require a full repo: they only need a matcher.
So there's no good reason to stuff a reference to the repo in
dirstate.

This commit exposes a sparse matcher to dirstate via a property that
when looked up will call a function that eventually calls
sparse.matcher(). The repo instance is bound in a closure, so it
isn't exposed to dirstate.

This approach is functionally similar to what the sparse extension does
today, except it hides the repo instance from dirstate. The approach
is not optimal because we have to call a proxy function and
sparse.matcher() on every property lookup. There is room to cache
the matcher instance in dirstate. After all, the matcher only changes
if the dirstate's parents change or if the sparse config changes. It
feels like we should be able to detect both events and update the
matcher when this occurs. But for now we preserve the existing
semantics so we can move the dirstate sparseness bits into core. Once
in core, refactoring becomes a bit easier since it will be clearer how
all these components interact.

The sparse extension has been updated to use the new property.
Because all references to the repo on dirstate have been removed,
the code for setting it has been removed.
2017-07-08 16:18:04 -07:00
Gregory Szorc
48bb68b295 sparse: use self instead of repo.dirstate
"self" here is the dirstate instance. I'm pretty confident that self
and repo.dirstate will be the exact same object. So remove a dependency
on repo by just looking at self.
2017-07-08 15:42:11 -07:00
Gregory Szorc
6155516e71 sparse: move code for importing rules from files into core
This is a pretty straightforward port. Some code cleanup was
performed. But no major changes to the logic were made.

I'm not a huge fan of this function because it does multiple
things. I'd like to get things into core first to facilitate
refactoring later.

Please also note the added inline comment about the oddities
of writeconfig() and the try..except to undo it. This is because
of the hackiness in which the sparse matcher is obtained by
various consumers, notably dirstate. We'll need a massive
refactor to address this. That refactor is effectively blocked
on having the sparse dirstate hacks live in core.
2017-07-08 14:15:07 -07:00
Gregory Szorc
21d9237e1c sparse: refactor activeprofiles into a generic function (API)
activeprofiles() is a special case of a more generic function.
Furthermore, that generic function is essentially already
implemented inline in the sparse extension.

So, refactor activeprofiles() to a generic activeconfig(). Change
the only consumer of activeprofiles() to use it. And have the
inline implementation in the sparse extension use it.
2017-07-08 14:01:32 -07:00
Augie Fackler
48687498f9 convert: remove if False block
This code has never run since its introduction on July 18th,
2007. It's time for it to go.

Differential Revision: https://phab.mercurial-scm.org/D19
2017-07-07 15:08:23 -04:00
Matt Harbison
557dc0142f subrepo: consider the parent repo dirty when a file is missing
This simply passes the 'missing' argument down from the context of the parent
repo, so the same rules apply.  subrepo.bailifchanged() is hardcoded to care
about missing files, because cmdutil.bailifchanged() is too.

In the end, it looks like this addresses inconsistencies with 'archive',
'identify', blackbox logs, 'merge', and 'update --check'.  I wasn't sure how to
implement this in git, so that's left for someone more familiar with it.
2017-07-09 02:55:46 -04:00
Matt Harbison
fd53ff6bf6 blackbox: simplify the dirty check
Same idea (and possibly incorrect behavior) as the previous commit.
2017-07-09 00:23:03 -04:00
Gregory Szorc
d86e3657d2 sparse: move printing of sparse config changes function into core
As part of the port, all arguments now have default values of 0.
Strings are now also given the i18n treatment.
2017-07-08 13:34:19 -07:00
Gregory Szorc
519ece1048 sparse: move code for clearing rules to core
This is a pretty straightforward port.
2017-07-08 13:19:38 -07:00