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

[PATCH v2 5/9] longest_ancestor_length(): explicitly filter list before loop

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Sep 29, 2012, 06:15 UTC
Message-ID
<1348899362-4057-6-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1348899362-4057-1-git-send-email-mhagger@alum.mit.edu>

Separate the step of filtering and normalizing elements of the prefixes list from the iteration that looks for the longest prefix. This will help keep the function testable after we not only normalize the paths, but also convert them into real paths.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 path.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/path.c b/path.c
index f455e8e..b255a74 100644
--- a/path.c
+++ b/path.c
@@ -568,6 +568,24 @@ int normalize_path_copy(char *dst, const char *src)
 	return 0;
 }
 
+static int normalize_path_callback(struct string_list_item *item, void *cb_data)
+{
+	char buf[PATH_MAX+1];
+	const char *ceil = item->string;
+	int len = strlen(ceil);
+
+	if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
+		return 0;
+	if (normalize_path_copy(buf, ceil) < 0)
+		return 0;
+	len = strlen(buf);
+	if (len > 0 && buf[len-1] == '/')
+		buf[--len] = '\0';
+	free(item->string);
+	item->string = xstrdup(buf);
+	return 1;
+}
+
 /*
  * path = Canonical absolute path
  * prefix_list = Colon-separated list of absolute paths
@@ -584,27 +602,19 @@ int normalize_path_copy(char *dst, const char *src)
 int longest_ancestor_length(const char *path, const char *prefix_list)
 {
 	struct string_list prefixes = STRING_LIST_INIT_DUP;
-	char buf[PATH_MAX+1];
 	int i, max_len = -1;
 
 	if (prefix_list == NULL || !strcmp(path, "/"))
 		return -1;
 
 	string_list_split(&prefixes, prefix_list, PATH_SEP, -1);
+	filter_string_list(&prefixes, 0, normalize_path_callback, NULL);
 
 	for (i = 0; i < prefixes.nr; i++) {
 		const char *ceil = prefixes.items[i].string;
 		int len = strlen(ceil);
 
-		if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
-			continue;
-		if (normalize_path_copy(buf, ceil) < 0)
-			continue;
-		len = strlen(buf);
-		if (len > 0 && buf[len-1] == '/')
-			buf[--len] = '\0';
-
-		if (!strncmp(path, buf, len) &&
+		if (!strncmp(path, ceil, len) &&
 		    path[len] == '/' &&
 		    len > max_len) {
 			max_len = len;
-- 
1.7.11.3
Previous: Michael HaggertyNext: Michael Haggerty
Message 6 of 17 in “Fix GIT_CEILING_DIRECTORIES that contain symlinks”
  1. 0/9 Fix GIT_CEILING_DIRECTORIES that contain symlinksMichael Haggerty, Sep 29, 2012
  2. 1/9 Introduce new static function real_path_internal()Michael Haggerty, Sep 29, 2012
  3. 2/9 real_path_internal(): add comment explaining use of cwdMichael Haggerty, Sep 29, 2012
  4. 3/9 Introduce new function real_path_if_valid()Michael Haggerty, Sep 29, 2012
  5. 4/9 longest_ancestor_length(): use string_list_split()Michael Haggerty, Sep 29, 2012
  6. 5/9 longest_ancestor_length(): explicitly filter list before loopMichael Haggerty, Sep 29, 2012
  7. 6/9 longest_ancestor_length(): always add a slash to the end of prefixesMichael Haggerty, Sep 29, 2012
  8. 7/9 longest_ancestor_length(): use string_list_longest_prefix()Michael Haggerty, Sep 29, 2012
  9. 8/9 longest_ancestor_length(): resolve symlinks before comparing pathsMichael Haggerty, Sep 29, 2012
  10. Junio C HamanoSep 30, 2012
  11. Michael HaggertyOct 1, 2012
  12. Junio C HamanoOct 1, 2012
  13. Proposed function path_in_directory() [was: Re: [PATCH v2 8/9] longest_ancestor_length(): resolve symlinks before comparing paths]Michael Haggerty, Oct 6, 2012
  14. Junio C HamanoOct 8, 2012
  15. Johannes SixtOct 8, 2012
  16. Junio C HamanoOct 8, 2012
  17. 9/9 t1504: stop resolving symlinks in GIT_CEILING_DIRECTORIESMichael Haggerty, Sep 29, 2012

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.