stablerev: pass information using environment variables

Summary:
Instead of hardcoding `--target <target>` and hardcode `$2` in the script.
Let's just use environment variables so `target` and other things have explicit
names.

Explicit set `REAL_CWD` so the script can learn the current directory before
it gets reset to the repo root.

Reviewed By: xavierd

Differential Revision: D17213186

fbshipit-source-id: 6a4fc4cf2cbf6e2c623400bc6bc13f7758a46c49
This commit is contained in:
Jun Wu 2019-09-05 15:51:45 -07:00 committed by Facebook Github Bot
parent 0cd7a9cb8f
commit 0b570f65a7
2 changed files with 12 additions and 7 deletions

View File

@ -27,7 +27,7 @@ thus multiple stable commits.
import re
import subprocess
from edenscm.mercurial import commands, error, json, registrar, util
from edenscm.mercurial import commands, encoding, error, json, pycompat, registrar, util
from edenscm.mercurial.i18n import _
from edenscm.mercurial.revsetlang import getargsdict, getstring
from edenscm.mercurial.smartset import baseset
@ -65,20 +65,26 @@ def _execute(ui, repo, target=None):
if script is None:
raise error.ConfigError(_("must set stablerev.script"))
# Assume the specified script is relative to the repo:
# Pass '--target $TARGET' for compatibility.
# XXX: Remove this once the new code has been rolled out for some time.
if target is not None:
script += " --target %s" % util.shellquote(target)
try:
ui.debug("repo-specific script for stable: %s\n" % script)
cwd = repo.wvfs.join("")
ui.debug("setting current working directory to: %s\n" % cwd)
reporoot = repo.wvfs.join("")
env = encoding.environ.copy()
env.update({"REAL_CWD": pycompat.getcwd(), "HG_ROOT": reporoot})
if target is not None:
env["TARGET"] = target
ui.debug("setting current working directory to: %s\n" % reporoot)
p = subprocess.Popen(
script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=util.closefds,
cwd=cwd,
cwd=reporoot,
env=env,
)
res = p.communicate()
ui.debug("stable script returns: %r\n" % (res,))

View File

@ -131,8 +131,7 @@ But they can be made optional or required:
Try making the script return different locations, based on the target:
$ cat <<'EOF' > stable.sh
> #!/bin/bash
> # The script is passed "--target <TARGET>", so look at $2:
> if [ "$2" = "foo" ]; then
> if [ "$TARGET" = "foo" ]; then
> echo 'D'
> else
> echo 'C'