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

[PATCH] Clean up git-ls-file directory walking library interface

From
Linus Torvalds <torvalds@osdl.org>
Date
May 17, 2006, 02:46 UTC
Message-ID
<Pine.LNX.4.64.0605161944480.10823@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.64.0605161859050.16475@g5.osdl.org>

This moves the code to add the per-directory ignore files for the base directory into the library routine.

That not only allows us to turn the function push_exclude_per_directory() static again, it also simplifies the library interface a lot (the caller no longer needs to worry about any of the per-directory exclude files at all).

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
 dir.c      |   28 +++++++++++++++++++++++++++-
 dir.h      |    2 --
 ls-files.c |   22 +---------------------
 3 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/dir.c b/dir.c
index 3f41a5d..d40b62e 100644
--- a/dir.c
+++ b/dir.c
@@ -78,7 +78,7 @@ void add_excludes_from_file(struct dir_s
 		die("cannot use %s as an exclude file", fname);
 }
 
-int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
+static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
 {
 	char exclude_file[PATH_MAX];
 	struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
@@ -289,6 +289,32 @@ static int cmp_name(const void *p1, cons
 
 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
 {
+	/*
+	 * Make sure to do the per-directory exclude for all the
+	 * directories leading up to our base.
+	 */
+	if (baselen) {
+		if (dir->exclude_per_dir) {
+			char *p, *pp = xmalloc(baselen+1);
+			memcpy(pp, base, baselen+1);
+			p = pp;
+			while (1) {
+				char save = *p;
+				*p = 0;
+				push_exclude_per_directory(dir, pp, p-pp);
+				*p++ = save;
+				if (!save)
+					break;
+				p = strchr(p, '/');
+				if (p)
+					p++;
+				else
+					p = pp + baselen;
+			}
+			free(pp);
+		}
+	}
+
 	read_directory_recursive(dir, path, base, baselen);
 	qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 	return dir->nr;
diff --git a/dir.h b/dir.h
index e8fc441..4f65f57 100644
--- a/dir.h
+++ b/dir.h
@@ -44,7 +44,5 @@ extern int excluded(struct dir_struct *,
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
 extern void add_exclude(const char *string, const char *base,
 			int baselen, struct exclude_list *which);
-extern int push_exclude_per_directory(struct dir_struct *,
-				      const char *base, int baselen);
 
 #endif
diff --git a/ls-files.c b/ls-files.c
index 89941a3..dfe1481 100644
--- a/ls-files.c
+++ b/ls-files.c
@@ -215,28 +215,8 @@ static void show_files(struct dir_struct
 		const char *path = ".", *base = "";
 		int baselen = prefix_len;
 
-		if (baselen) {
+		if (baselen)
 			path = base = prefix;
-			if (dir->exclude_per_dir) {
-				char *p, *pp = xmalloc(baselen+1);
-				memcpy(pp, prefix, baselen+1);
-				p = pp;
-				while (1) {
-					char save = *p;
-					*p = 0;
-					push_exclude_per_directory(dir, pp, p-pp);
-					*p++ = save;
-					if (!save)
-						break;
-					p = strchr(p, '/');
-					if (p)
-						p++;
-					else
-						p = pp + baselen;
-				}
-				free(pp);
-			}
-		}
 		read_directory(dir, path, base, baselen);
 		if (show_others)
 			show_other_files(dir);
Previous: Linus TorvaldsNext: Linus Torvalds
Message 3 of 4 in “libify git-ls-files directory traversal”
  1. libify git-ls-files directory traversalLinus Torvalds, May 17, 2006
  2. Linus TorvaldsMay 17, 2006
  3. Clean up git-ls-file directory walking library interfaceLinus Torvalds, May 17, 2006
  4. Linus TorvaldsMay 17, 2006

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.