Patchwork D9006: chg: make is possible to call by default an hg binary located next to chg

login
register
mail settings
Submitter phabricator
Date Sept. 10, 2020, 6:12 p.m.
Message ID <differential-rev-PHID-DREV-lm6uql3sltqthkcgncjo-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47126/
State Superseded
Headers show

Comments

phabricator - Sept. 10, 2020, 6:12 p.m.
valentin.gatienbaron created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  When a single version of hg is in use and it's in the PATH, using chg
  is just a matter of calling chg.
  
  But when there are multiple installations of hg+chg around, and hg is
  referred to with an absolute path, using chg is more annoying because
  it requires both changing the invocation to hg to use chg, but also
  setting CHGHG.
  
  Currently, we set HGPATH when we build chg to remove the need to set
  CHGHG in the previous paragraph. But that means chg now hardcodes its
  installation path, which makes the installation not relocatable. Hence
  this proposal to make chg find ./hg relative to itself (as opposed to
  CHGHG=./hg which find hg relative to cwd).
  
  This only works on linux as written, but since it's opt-in, it sounds
  fine.
  
  Tested by hand, as I'm not sure how else to test this.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9006

AFFECTED FILES
  contrib/chg/Makefile
  contrib/chg/chg.c

CHANGE DETAILS




To: valentin.gatienbaron, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -184,13 +184,46 @@ 
 		abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r);
 }
 
+/* If the current program is, say, /a/b/c/chg, returns /a/b/c/hg. */
+static char *getrelhgcmd(void)
+{
+	ssize_t n;
+	char *res, *slash;
+	int maxsize = 4096;
+	res = malloc(maxsize);
+	if (res == NULL)
+		goto cleanup;
+	n = readlink("/proc/self/exe", res, maxsize);
+	if (n < 0 || n >= maxsize)
+		goto cleanup;
+	res[n] = '\0';
+	slash = strrchr(res, '/');
+	if (slash == NULL)
+		goto cleanup;
+	/* 4 is strlen("/hg") + nul byte */
+	if (slash + 4 >= res + maxsize)
+		goto cleanup;
+	memcpy(slash, "/hg", 4);
+	return res;
+ cleanup:
+	free(res);
+	return NULL;
+}
+
 static const char *gethgcmd(void)
 {
 	static const char *hgcmd = NULL;
+#ifdef HGPATHREL
+	int tryrelhgcmd = 1;
+#else
+	int tryrelhgcmd = 0;
+#endif
 	if (!hgcmd) {
 		hgcmd = getenv("CHGHG");
 		if (!hgcmd || hgcmd[0] == '\0')
 			hgcmd = getenv("HG");
+		if (tryrelhgcmd && (!hgcmd || hgcmd[0] == '\0'))
+			hgcmd = getrelhgcmd();
 		if (!hgcmd || hgcmd[0] == '\0')
 #ifdef HGPATH
 			hgcmd = (HGPATH);
diff --git a/contrib/chg/Makefile b/contrib/chg/Makefile
--- a/contrib/chg/Makefile
+++ b/contrib/chg/Makefile
@@ -8,6 +8,9 @@ 
 ifdef HGPATH
 override CPPFLAGS += -DHGPATH=\"$(HGPATH)\"
 endif
+ifdef HGPATHREL
+override CPPFLAGS += -DHGPATHREL=\"$(HGPATHREL)\"
+endif
 
 DESTDIR =
 PREFIX = /usr/local