Mercurial > hg
comparison contrib/chg/chg.c @ 45551:4c8d9b53b1c7
chg: make is possible to call by default an hg binary located next to chg
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.
Differential Revision: https://phab.mercurial-scm.org/D9006
author | Valentin Gatien-Baron <vgatien-baron@janestreet.com> |
---|---|
date | Thu, 03 Sep 2020 11:07:47 -0400 |
parents | 5eee6f4f3d0d |
children | 8711dc13474c |
comparison
equal
deleted
inserted
replaced
45550:29a259be6424 | 45551:4c8d9b53b1c7 |
---|---|
182 opts->sockname, (unsigned)getpid()); | 182 opts->sockname, (unsigned)getpid()); |
183 if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) | 183 if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) |
184 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | 184 abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); |
185 } | 185 } |
186 | 186 |
187 /* If the current program is, say, /a/b/c/chg, returns /a/b/c/hg. */ | |
188 static char *getrelhgcmd(void) | |
189 { | |
190 ssize_t n; | |
191 char *res, *slash; | |
192 int maxsize = 4096; | |
193 res = malloc(maxsize); | |
194 if (res == NULL) | |
195 goto cleanup; | |
196 n = readlink("/proc/self/exe", res, maxsize); | |
197 if (n < 0 || n >= maxsize) | |
198 goto cleanup; | |
199 res[n] = '\0'; | |
200 slash = strrchr(res, '/'); | |
201 if (slash == NULL) | |
202 goto cleanup; | |
203 /* 4 is strlen("/hg") + nul byte */ | |
204 if (slash + 4 >= res + maxsize) | |
205 goto cleanup; | |
206 memcpy(slash, "/hg", 4); | |
207 return res; | |
208 cleanup: | |
209 free(res); | |
210 return NULL; | |
211 } | |
212 | |
187 static const char *gethgcmd(void) | 213 static const char *gethgcmd(void) |
188 { | 214 { |
189 static const char *hgcmd = NULL; | 215 static const char *hgcmd = NULL; |
216 #ifdef HGPATHREL | |
217 int tryrelhgcmd = 1; | |
218 #else | |
219 int tryrelhgcmd = 0; | |
220 #endif | |
190 if (!hgcmd) { | 221 if (!hgcmd) { |
191 hgcmd = getenv("CHGHG"); | 222 hgcmd = getenv("CHGHG"); |
192 if (!hgcmd || hgcmd[0] == '\0') | 223 if (!hgcmd || hgcmd[0] == '\0') |
193 hgcmd = getenv("HG"); | 224 hgcmd = getenv("HG"); |
225 if (tryrelhgcmd && (!hgcmd || hgcmd[0] == '\0')) | |
226 hgcmd = getrelhgcmd(); | |
194 if (!hgcmd || hgcmd[0] == '\0') | 227 if (!hgcmd || hgcmd[0] == '\0') |
195 #ifdef HGPATH | 228 #ifdef HGPATH |
196 hgcmd = (HGPATH); | 229 hgcmd = (HGPATH); |
197 #else | 230 #else |
198 hgcmd = "hg"; | 231 hgcmd = "hg"; |