git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 6/7 v2] Compute prefix at runtime if RUNTIME_PREFIX is set

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 19, 2009, 17:41 UTC
Message-ID
<alpine.DEB.1.00.0901191825040.3586@pacific.mpi-cbg.de>
In-Reply-To
<1232280015-8144-7-git-send-email-prohaska@zib.de>
Hi,
On Sun, 18 Jan 2009, Steffen Prohaska wrote:
Show 10 quoted lines
> +	if (!prefix) {
> +		const char *strip[] = {
> +			GIT_EXEC_PATH,
> +			BINDIR,
> +			0
> +		};
> +		const char **s;
> +
> +		for (s = strip; *s; s++) {
> +			const char *sargv = argv0_path + strlen(argv0_path);
This does not need to be recomputed all the time, right?
Show 17 quoted lines
> +			const char *ss = *s + strlen(*s);
> +			while (argv0_path < sargv && *s < ss
> +				&& (*sargv == *ss ||
> +				    (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
> +				sargv--;
> +				ss--;
> +			}
> +			if (*s == ss) {
> +				struct strbuf d = STRBUF_INIT;
> +				/* We also skip the trailing directory separator. */
> +				assert(sargv - argv0_path - 1 >= 0);
> +				strbuf_add(&d, argv0_path, sargv - argv0_path - 1);
> +				prefix = strbuf_detach(&d, NULL);
> +				break;
> +			}
> +		}
> +	}

I have a definite feeling that this code would be easier to read if it resembled this:

/* sets prefix if the suffix matches (modulo separator changes) */
static int strip_path_suffix(const char *path, const char *suffix,
	char **prefix)
{
	int path_len = strlen(path), suffix_len = strlen(suffix);
	while (suffix_len) {
		if (!path_len)
			return 1;
		path_len--; suffix_len--;
		/* strip arbitrary amount of directory separators */
		if (is_dir_sep(path[path_len]) && 
				is_dir_sep(suffix[suffix_len])) {
			while (path_len && is_dir_sep(path[path_len]))
				path_len--;
			while (suffix_len && is_dir_sep(suffix[suffix_len]))
				suffix_len--;
		}
		else if (path[path_len] != suffix[suffix_len])
			return 1;
	}
	while (path_len && is_dir_sep(path[path_len - 1]))
		path_len--;
	prefix = xstrndup(path, path_len);
	return 0;
}
...
	if (!prefix && strip_path_suffix(argv0_path, GIT_EXEC_PATH) &&
			strip_path_suffix(argv0_path, BINDIR)) {
		prefix = PREFIX;
		...
Note: this function could be exported in path.c, as it also handles the 
"a//bin" vs "a/bin" case.

Ciao, Dscho

Previous: Steffen ProhaskaNext: Johannes Sixt
Message 9 of 12 in “RUNTIME_PREFIX”
  1. 0/7 RUNTIME_PREFIXSteffen Prohaska, Jan 18, 2009
  2. 1/7 Move computation of absolute paths from Makefile to runtime (in preparation for RUNTIME_PREFIX)Steffen Prohaska, Jan 18, 2009
  3. 2/7 Refactor git_set_argv0_path() to git_extract_argv0_path()Steffen Prohaska, Jan 18, 2009
  4. 3/7 git_extract_argv0_path(): Move check for valid argv0 from caller to calleeSteffen Prohaska, Jan 18, 2009
  5. 4/7 Add calls to git_extract_argv0_path() in programs that call git_config_*Steffen Prohaska, Jan 18, 2009
  6. 5/7 Modify setup_path() to only add git_exec_path() to PATHSteffen Prohaska, Jan 18, 2009
  7. 6/7 Compute prefix at runtime if RUNTIME_PREFIX is setSteffen Prohaska, Jan 18, 2009
  8. 7/7 Windows: Revert to default paths and convert them by RUNTIME_PREFIXSteffen Prohaska, Jan 18, 2009
  9. Johannes SchindelinJan 19, 2009
  10. Johannes SixtJan 18, 2009
  11. Johannes SixtJan 18, 2009
  12. Junio C HamanoJan 18, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.