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

[PATCH 1/3] Add 'fill_directory()' helper function for directory traversal

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Jul 9, 2009, 02:40 UTC
Message-ID
<alpine.LFD.2.01.0907081936470.3352@localhost.localdomain>
In-Reply-To
<alpine.LFD.2.01.0907081933530.3352@localhost.localdomain>
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Thu, 14 May 2009 13:22:36 -0700
Subject: [PATCH 1/3] Add 'fill_directory()' helper function for directory traversal

Most of the users of "read_directory()" actually want a much simpler interface than the whole complex (but rather powerful) one.

In fact 'git add' had already largely abstracted out the core interface issues into a private "fill_directory()" function that was largely applicable almost as-is to a number of callers. Yes, 'git add' wants to do some extra work of its own, specific to the add semantics, but we can easily split that out, and use the core as a generic function.

This function does exactly that, and now that much simplified 'fill_directory()' function can be shared with a number of callers, while also ensuring that the rather more complex calling conventions of read_directory() are used by fewer call-sites.

This also makes the 'common_prefix()' helper function private to dir.c, since all callers are now in that file.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

This is a cleaned-up version of a patch that I had done earlier for the pathname character set conversion series. It's basically a cleanup of b99acc690de27aaf437676c9e3077493a885b642 in 'pu'.

 builtin-add.c      |   45 ++++++++++++++-------------------------------
 builtin-clean.c    |   12 +-----------
 builtin-ls-files.c |    7 +------
 dir.c              |   23 ++++++++++++++++++++++-
 dir.h              |    3 +--
 wt-status.c        |    2 +-
 6 files changed, 40 insertions(+), 52 deletions(-)
diff --git a/builtin-add.c b/builtin-add.c
index 78989da..581a2a1 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -97,35 +97,6 @@ static void treat_gitlinks(const char **pathspec)
 	}
 }
 
-static void fill_directory(struct dir_struct *dir, const char **pathspec,
-		int ignored_too)
-{
-	const char *path, *base;
-	int baselen;
-
-	/* Set up the default git porcelain excludes */
-	memset(dir, 0, sizeof(*dir));
-	if (!ignored_too) {
-		dir->flags |= DIR_COLLECT_IGNORED;
-		setup_standard_excludes(dir);
-	}
-
-	/*
-	 * Calculate common prefix for the pathspec, and
-	 * use that to optimize the directory walk
-	 */
-	baselen = common_prefix(pathspec);
-	path = ".";
-	base = "";
-	if (baselen)
-		path = base = xmemdupz(*pathspec, baselen);
-
-	/* Read the directory and prune it */
-	read_directory(dir, path, base, baselen, pathspec);
-	if (pathspec)
-		prune_directory(dir, pathspec, baselen);
-}
-
 static void refresh(int verbose, const char **pathspec)
 {
 	char *seen;
@@ -343,9 +314,21 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		die("index file corrupt");
 	treat_gitlinks(pathspec);
 
-	if (add_new_files)
+	if (add_new_files) {
+		int baselen;
+
+		/* Set up the default git porcelain excludes */
+		memset(&dir, 0, sizeof(dir));
+		if (!ignored_too) {
+			dir.flags |= DIR_COLLECT_IGNORED;
+			setup_standard_excludes(&dir);
+		}
+
 		/* This picks up the paths that are not tracked */
-		fill_directory(&dir, pathspec, ignored_too);
+		baselen = fill_directory(&dir, pathspec);
+		if (pathspec)
+			prune_directory(&dir, pathspec, baselen);
+	}
 
 	if (refresh_only) {
 		refresh(verbose, pathspec);
diff --git a/builtin-clean.c b/builtin-clean.c
index 1c1b6d2..2d8c735 100644
--- a/builtin-clean.c
+++ b/builtin-clean.c
@@ -33,7 +33,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
 	struct strbuf directory = STRBUF_INIT;
 	struct dir_struct dir;
-	const char *path, *base;
 	static const char **pathspec;
 	struct strbuf buf = STRBUF_INIT;
 	const char *qname;
@@ -78,16 +77,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	pathspec = get_pathspec(prefix, argv);
 	read_cache();
 
-	/*
-	 * Calculate common prefix for the pathspec, and
-	 * use that to optimize the directory walk
-	 */
-	baselen = common_prefix(pathspec);
-	path = ".";
-	base = "";
-	if (baselen)
-		path = base = xmemdupz(*pathspec, baselen);
-	read_directory(&dir, path, base, baselen, pathspec);
+	fill_directory(&dir, pathspec);
 
 	if (pathspec)
 		seen = xmalloc(argc > 0 ? argc : 1);
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index 2312866..f473220 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -161,12 +161,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
 
 	/* For cached/deleted files we don't need to even do the readdir */
 	if (show_others || show_killed) {
-		const char *path = ".", *base = "";
-		int baselen = prefix_len;
-
-		if (baselen)
-			path = base = prefix;
-		read_directory(dir, path, base, baselen, pathspec);
+		fill_directory(dir, pathspec);
 		if (show_others)
 			show_other_files(dir);
 		if (show_killed)
diff --git a/dir.c b/dir.c
index 74b3bbf..0c8553b 100644
--- a/dir.c
+++ b/dir.c
@@ -19,7 +19,7 @@ static int read_directory_recursive(struct dir_struct *dir,
 	int check_only, const struct path_simplify *simplify);
 static int get_dtype(struct dirent *de, const char *path);
 
-int common_prefix(const char **pathspec)
+static int common_prefix(const char **pathspec)
 {
 	const char *path, *slash, *next;
 	int prefix;
@@ -52,6 +52,27 @@ int common_prefix(const char **pathspec)
 	return prefix;
 }
 
+int fill_directory(struct dir_struct *dir, const char **pathspec)
+{
+	const char *path, *base;
+	int baselen;
+
+	/*
+	 * Calculate common prefix for the pathspec, and
+	 * use that to optimize the directory walk
+	 */
+	baselen = common_prefix(pathspec);
+	path = "";
+	base = "";
+
+	if (baselen)
+		path = base = xmemdupz(*pathspec, baselen);
+
+	/* Read the directory and prune it */
+	read_directory(dir, path, base, baselen, pathspec);
+	return baselen;
+}
+
 /*
  * Does 'match' match the given name?
  * A match is found if
diff --git a/dir.h b/dir.h
index 541286a..f9d69dd 100644
--- a/dir.h
+++ b/dir.h
@@ -61,13 +61,12 @@ struct dir_struct {
 	char basebuf[PATH_MAX];
 };
 
-extern int common_prefix(const char **pathspec);
-
 #define MATCHED_RECURSIVELY 1
 #define MATCHED_FNMATCH 2
 #define MATCHED_EXACTLY 3
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
 
+extern int fill_directory(struct dir_struct *dir, const char **pathspec);
 extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
 
 extern int excluded(struct dir_struct *, const char *, int *);
diff --git a/wt-status.c b/wt-status.c
index 0ca4b13..47735d8 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -255,7 +255,7 @@ static void wt_status_print_untracked(struct wt_status *s)
 			DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
 	setup_standard_excludes(&dir);
 
-	read_directory(&dir, ".", "", 0, NULL);
+	fill_directory(&dir, NULL);
 	for(i = 0; i < dir.nr; i++) {
 		struct dir_entry *ent = dir.entries[i];
 		if (!cache_name_is_other(ent->name, ent->len))
-- 
1.6.3.3.412.gf581d
Previous: Linus TorvaldsNext: Linus Torvalds
Message 5 of 39 in “Too many 'stat' calls by git-status on Windows”
  1. Dmitry PotapovJul 7, 2009
  2. Ramsay JonesJul 8, 2009
  3. Linus TorvaldsJul 9, 2009
  4. Linus TorvaldsJul 9, 2009
  5. 1/3 Add 'fill_directory()' helper function for directory traversalLinus Torvalds, Jul 9, 2009
  6. 2/3 Simplify read_directory[_recursive]() argumentsLinus Torvalds, Jul 9, 2009
  7. 3/3 Avoid doing extra 'lstat()'s for d_type if we have an up-to-date cache entryLinus Torvalds, Jul 9, 2009
  8. Junio C HamanoJul 9, 2009
  9. Linus TorvaldsJul 9, 2009
  10. Junio C HamanoJul 9, 2009
  11. Linus TorvaldsJul 9, 2009
  12. Junio C HamanoJul 9, 2009
  13. Linus TorvaldsJul 9, 2009
  14. Linus TorvaldsJul 9, 2009
  15. Junio C HamanoJul 9, 2009
  16. Linus TorvaldsJul 9, 2009
  17. 4/3 Avoid using 'lstat()' to figure out directoriesLinus Torvalds, Jul 9, 2009
  18. 5/3 Prepare symlink caching for thread-safetyLinus Torvalds, Jul 9, 2009
  19. 6/3 Export thread-safe version of 'has_symlink_leading_path()'Linus Torvalds, Jul 9, 2009
  20. 7/3 Make index preloading check the whole path to the fileLinus Torvalds, Jul 9, 2009
  21. Linus TorvaldsJul 9, 2009
  22. Junio C HamanoJul 10, 2009
  23. Linus TorvaldsJul 10, 2009
  24. Linus TorvaldsJul 10, 2009
  25. Junio C HamanoJul 11, 2009
  26. Linus TorvaldsJul 11, 2009
  27. Kjetil BarvikJul 12, 2009
  28. Junio C HamanoJul 12, 2009
  29. Paolo BonziniJul 9, 2009
  30. Linus TorvaldsJul 9, 2009
  31. Linus TorvaldsJul 9, 2009
  32. Linus TorvaldsJul 10, 2009
  33. Junio C HamanoJul 9, 2009
  34. Dmitry PotapovJul 9, 2009
  35. Eric BlakeJul 9, 2009
  36. Dmitry PotapovJul 9, 2009
  37. Dmitry PotapovJul 10, 2009
  38. Dmitry PotapovJul 9, 2009
  39. Dmitry PotapovJul 9, 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.