diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c --- a/contrib/chg/chg.c +++ b/contrib/chg/chg.c @@ -234,18 +234,12 @@ static const char *gethgcmd(void) hgcmd = "hg"; #endif } - /* Set $CHGHG to the path to the seleted hg executable if it wasn't - * already set. This has the effect of ensuring that a new command - * server will be spawned if the existing command server is running from - * an executable at a different path. */ - if (setenv("CHGHG", hgcmd, 1) != 0) - abortmsgerrno("failed to setenv"); return hgcmd; } -static void execcmdserver(const char *hgcmd, const struct cmdserveropts *opts) +static void execcmdserver(const struct cmdserveropts *opts) { - + const char *hgcmd = gethgcmd(); const char *baseargv[] = { hgcmd, "serve", "--no-profile", "--cmdserver", "chgunix", "--address", opts->initsockname, "--daemon-postexec", @@ -382,16 +376,11 @@ static hgclient_t *connectcmdserver(stru debugmsg("start cmdserver at %s", opts->initsockname); - /* Get the path to the hg executable before we fork because this - * function might update the environment, and we want this to be - * reflected in both the parent and child processes. */ - const char *hgcmd = gethgcmd(); - pid_t pid = fork(); if (pid < 0) abortmsg("failed to fork cmdserver process"); if (pid == 0) { - execcmdserver(hgcmd, opts); + execcmdserver(opts); } else { hgc = retryconnectcmdserver(opts, pid); } @@ -525,6 +514,16 @@ int main(int argc, const char *argv[]) } } + /* Set $CHGHG to the path of the hg executable we intend to use. This + * is a no-op if $CHGHG was expliclty specified, but otherwise this + * ensures that we will spawn a new command server if we connect to an + * existing one running from a different executable. This should only + * only be needed when chg is built with HGPATHREL since otherwise the + * hg executable used when CHGHG is absent should be deterministic. + * */ + if (setenv("CHGHG", gethgcmd(), 1) != 0) + abortmsgerrno("failed to setenv"); + hgclient_t *hgc; size_t retry = 0; while (1) { diff --git a/tests/test-chg.t b/tests/test-chg.t --- a/tests/test-chg.t +++ b/tests/test-chg.t @@ -553,3 +553,20 @@ FIXME: Run 4 should not be >3x Run 1's n $ filteredchg log -r . --no-profile $ filteredchg log -r . Sample count: * (glob) + +chg setting CHGHG itself +------------------------ + +If CHGHG is not set, chg will set it before spawning the command server. + $ hg --kill-chg-daemon + $ HG=$CHGHG CHGHG= CHGDEBUG= hg debugshell -c \ + > 'ui.write(b"CHGHG=%s\n" % ui.environ.get(b"CHGHG"))' 2>&1 \ + > | egrep 'CHGHG|start' + chg: debug: * start cmdserver at * (glob) + CHGHG=/*/install/bin/hg (glob) + +Running the same command a second time shouldn't spawn a new command server. + $ HG=$CHGHG CHGHG= CHGDEBUG= hg debugshell -c \ + > 'ui.write(b"CHGHG=%s\n" % ui.environ.get(b"CHGHG"))' 2>&1 \ + > | egrep 'CHGHG|start' + CHGHG=/*/install/bin/hg (glob)