sapling/edenscm/hgext/commitcloud/workspace.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

128 lines
3.8 KiB
Python

# Copyright 2019 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 edenscm.mercurial import config, error, util
from edenscm.mercurial.i18n import _
workspaceopts = [
(
"u",
"user",
"",
_(
"use the workspaces of a different user (specified by username or "
"email address) instead of the current user"
),
),
("w", "workspace", "", _("name of workspace to use (default: 'default')")),
(
"",
"raw-workspace",
"",
_("raw workspace name (e.g. 'user/<username>/<workspace>') (ADVANCED)"),
),
]
def parseworkspace(ui, repo, **opts):
"""Parse the command line options to get a workspace name.
Returns None if the user specifies no workspace command line arguments.
Workspace naming convention:
section/section_name/workspace_name
where section is one of ('user', 'group', 'team', 'project')
Examples:
team/source_control/shared
user/<username>/default
project/commit_cloud/default
"""
rawworkspace = opts.get("raw_workspace")
if rawworkspace:
for opt in "user", "workspace":
if opts.get(opt):
raise error.Abort(_("--raw-workspace and --%s are incompatible") % opt)
return rawworkspace
user = opts.get("user")
workspace = opts.get("workspace")
if not any([user, workspace]):
# User has not specified any workspace options.
return None
# Currently only "user" workspaces are implemented
if not user:
user = ui.username()
domain = ui.config("commitcloud", "email_domain")
user = util.emaildomainuser(user, domain)
prefix = "user/%s/" % user
if not workspace:
workspace = "default"
# Workaround for users specifying the full workspace name with "-w"
if workspace.startswith("user/") and not opts.get("user"):
msg = (
"specifying full workspace names with '-w' is deprecated\n"
"(use '-u' to select another user's workspaces)\n"
)
ui.warn(msg)
return workspace
return prefix + workspace
def defaultworkspace(ui, user=None):
"""Returns the default workspace for the given or current user"""
if user is None:
domain = ui.config("commitcloud", "email_domain")
user = util.emaildomainuser(ui.username(), domain)
return "user/%s/default" % user
filename = "commitcloudrc"
def _get(repo, name):
"""Read commitcloudrc file to get a value"""
if repo.svfs.exists(filename):
with repo.svfs.open(filename, r"rb") as f:
cloudconfig = config.config()
cloudconfig.read(filename, f)
return cloudconfig.get("commitcloud", name)
else:
return None
def currentworkspace(repo):
"""
Returns the currently connected workspace, or None if the repo is not
connected to a workspace.
"""
return _get(repo, "current_workspace")
def disconnected(repo):
"""
Returns True if the user has manually disconnected from a workspace.
"""
disconnected = _get(repo, "disconnected")
if disconnected is None or isinstance(disconnected, bool):
return disconnected
return util.parsebool(disconnected)
def setworkspace(repo, workspace):
"""Sets the currently connected workspace."""
with repo.wlock(), repo.lock(), repo.svfs.open(filename, "w", atomictemp=True) as f:
f.write("[commitcloud]\ncurrent_workspace=%s\n" % workspace)
def clearworkspace(repo):
"""Clears the currently connected workspace."""
with repo.wlock(), repo.lock(), repo.svfs.open(filename, "w", atomictemp=True) as f:
f.write("[commitcloud]\ndisconnected=true\n")