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

[PATCH v2 7/9] longest_ancestor_length(): use string_list_longest_prefix()

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

Use string_list_longest_prefix() in the implementation of longest_ancestor_length(), instead of an equivalent loop.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 path.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
diff --git a/path.c b/path.c
index a56c64a..b20f2fb 100644
--- a/path.c
+++ b/path.c
@@ -604,23 +604,16 @@ static int normalize_path_callback(struct string_list_item *item, void *cb_data)
 int longest_ancestor_length(const char *path, const char *prefix_list)
 {
 	struct string_list prefixes = STRING_LIST_INIT_DUP;
-	int i, max_len = -1;
+	int max_len;
+	const char *longest_prefix;
 
 	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 (!strncmp(path, ceil, len) &&
-		    len - 1 > max_len) {
-			max_len = len - 1;
-		}
-	}
+	longest_prefix = string_list_longest_prefix(&prefixes, path);
+	max_len = longest_prefix ? strlen(longest_prefix) - 1 : -1;
 
 	string_list_clear(&prefixes, 0);
 	return max_len;
-- 
1.7.11.3
Previous: Michael HaggertyNext: Michael Haggerty
Message 8 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.