sapling/edenscm/hgext/infinitepush/infinitepushcommands.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

104 lines
3.1 KiB
Python

# Copyright 2016 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.
"""
config::
[infinitepush]
# limit number of files in the node metadata. This is to make sure we don't
# waste too much space on huge codemod commits.
metadatafilelimit = 100
"""
from __future__ import absolute_import
import json
from edenscm.mercurial import (
copies as copiesmod,
encoding,
error,
hg,
patch,
registrar,
scmutil,
util,
)
from edenscm.mercurial.i18n import _
# Mercurial
from edenscm.mercurial.node import bin
from . import common
cmdtable = {}
command = registrar.command(cmdtable)
@command(
"debugfillinfinitepushmetadata", [("", "node", [], "node to fill metadata for")]
)
def debugfillinfinitepushmetadata(ui, repo, **opts):
"""Special command that fills infinitepush metadata for a node
"""
nodes = opts["node"]
if not nodes:
raise error.Abort(_("nodes are not specified"))
filelimit = ui.configint("infinitepush", "metadatafilelimit", 100)
nodesmetadata = {}
for node in nodes:
index = repo.bundlestore.index
if not bool(index.getbundle(node)):
raise error.Abort(_("node %s is not found") % node)
if node not in repo:
newbundlefile = common.downloadbundle(repo, bin(node))
bundlepath = "bundle:%s+%s" % (repo.root, newbundlefile)
bundlerepo = hg.repository(ui, bundlepath)
repo = bundlerepo
p1 = repo[node].p1().node()
diffopts = patch.diffallopts(ui, {})
match = scmutil.matchall(repo)
chunks = patch.diff(repo, p1, node, match, None, diffopts, relroot="")
difflines = util.iterlines(chunks)
states = "modified added removed deleted unknown ignored clean".split()
status = repo.status(p1, node)
status = zip(states, status)
filestatus = {}
for state, files in status:
for f in files:
filestatus[f] = state
diffstat = patch.diffstatdata(difflines)
changed_files = {}
copies = copiesmod.pathcopies(repo[p1], repo[node])
for filename, adds, removes, isbinary in diffstat[:filelimit]:
# use special encoding that allows non-utf8 filenames
filename = encoding.jsonescape(filename, paranoid=True)
changed_files[filename] = {
"adds": adds,
"removes": removes,
"isbinary": isbinary,
"status": filestatus.get(filename, "unknown"),
}
if filename in copies:
changed_files[filename]["copies"] = copies[filename]
output = {}
output["changed_files"] = changed_files
if len(diffstat) > filelimit:
output["changed_files_truncated"] = True
nodesmetadata[node] = output
with index:
for node, metadata in nodesmetadata.iteritems():
dumped = json.dumps(metadata, sort_keys=True)
index.saveoptionaljsonmetadata(node, dumped)