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

Re: [PATCH] common_prefix: be more careful about pathspec bounds

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 15, 2010, 16:06 UTC
Message-ID
<7v4oh48elm.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v8w6g8hfx.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
> Isn't it more intuitive to structure the loop by saying 'Ok, if "path" up
> to the currently proposed "prefix" is too long to match, let's shorten it
> by one path component and try again'?

Another way of saying this, which probably needs less number of scans, would be to shorten prefix to what is known to match --- use of memcmp() discards this information.

static int common_prefix(const char **pathspec)
{
	const char *path, *slash, *next;
	int prefix;
	if (!pathspec)
		return 0;
	path = *pathspec;
	slash = strrchr(path, '/');
	if (!slash)
		return 0;
	prefix = slash - path + 1;
	while ((next = *++pathspec) != NULL) {
		int len, last_matching_slash = -1;
		for (len = 0;
		     len < prefix && next[len] == path[len];
		     len++)
			if (next[len] == '/')
				last_matching_slash = len;
		if (len == prefix)
			continue;
		if (last_matching_slash < 0)
			return 0;
		prefix = last_matching_slash + 1;
	}
	return prefix;
}
Previous: Junio C HamanoNext: Thomas Rast
Message 5 of 8 in “common_prefix: be more careful about pathspec bounds”
  1. common_prefix: be more careful about pathspec boundsThomas Rast, Jun 10, 2010
  2. Thomas RastJun 15, 2010
  3. Johannes SchindelinJun 15, 2010
  4. Junio C HamanoJun 15, 2010
  5. Junio C HamanoJun 15, 2010
  6. Thomas RastJun 15, 2010
  7. Junio C HamanoJun 15, 2010
  8. common_prefix: simplify and fix scanning for prefixesThomas Rast, Jun 15, 2010

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.