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

Re: Possible bug in git-completion.sh

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 8, 2010, 19:58 UTC
Message-ID
<7vvdfcfjxo.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v8wc8jw3k.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 13 quoted lines
> Nah, I should have checked the code.
>
> The implementation of ls-files does cd up and uses pathspec, so the intent
> is to apply higher level gitignore.
> 
> It however feeds paths from the already ignored directories, which _is_
> the real bug.
> ...
> I think a proper fix should be in dir.c::read_directory() where it calls
> read_directory_recursive() without first checking if the directory itself
> should be ignored.  read_directory_recursive() itself has a logic to see
> if the dirent it found is worth recursing into and a similar logic should
> be in the toplevel caller.

Actually doing less in fill_directory() turned out to be a simpler solution.

builtin_add() cares about the return value from fill_directory() and performs prune_directory() optimization magic, and we may want to change it not to do so as well, but I didn't want to worry about too many things at once, so this version still runs the "common_prefix()" that forgets to take .gitignore at higher levels (or a $GIT_DIR/info/exclude entry that ignores the common prefix directory of given pathspecs) into account.

Another possibility is to fix common_prefix() and make it walk the path it returns one level at a time from the top, making sure they are not ignored, and that would probably be a better fix, but at least this patch will give you a starting point and tests to check the result against.

diff --git a/dir.c b/dir.c
index d0999ba..56d3f60 100644
--- a/dir.c
+++ b/dir.c
@@ -53,7 +53,6 @@ static int common_prefix(const char **pathspec)
 
 int fill_directory(struct dir_struct *dir, const char **pathspec)
 {
-	const char *path;
 	int len;
 
 	/*
@@ -61,13 +60,8 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)
 	 * use that to optimize the directory walk
 	 */
 	len = common_prefix(pathspec);
-	path = "";
-
-	if (len)
-		path = xmemdupz(*pathspec, len);
-
 	/* Read the directory and prune it */
-	read_directory(dir, path, len, pathspec);
+	read_directory(dir, "", 0, pathspec);
 	return len;
 }
 
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index c65bca8..17d1764 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -153,4 +153,42 @@ test_expect_success 'negated exclude matches can override previous ones' '
 	grep "^a.1" output
 '
 
+test_expect_success 'subdirectory ignore (setup)' '
+	mkdir -p top/l1/l2 &&
+	(
+		cd top &&
+		git init &&
+		echo /.gitignore >.gitignore &&
+		echo l1 >>.gitignore &&
+		echo l2 >l1/.gitignore
+	)
+'
+
+test_expect_success 'subdirectory ignore (toplevel)' '
+	(
+		cd top &&
+		git ls-files -o --exclude-standard
+	) >actual &&
+	>expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'subdirectory ignore (l1/l2)' '
+	(
+		cd top/l1/l2 &&
+		git ls-files -o --exclude-standard
+	) >actual &&
+	>expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'subdirectory ignore (l1)' '
+	(
+		cd top/l1 &&
+		git ls-files -o --exclude-standard
+	) >actual &&
+	>expect &&
+	test_cmp expect actual
+'
+
 test_done
Previous: Junio C HamanoNext: Junio C Hamano
Message 11 of 28 in “Possible bug in git-completion.sh”
  1. Jon ScheweJan 8, 2010
  2. Michael J GruberJan 8, 2010
  3. Jeff KingJan 8, 2010
  4. Junio C HamanoJan 8, 2010
  5. Jeff KingJan 8, 2010
  6. Junio C HamanoJan 8, 2010
  7. Junio C HamanoJan 8, 2010
  8. Jeff KingJan 8, 2010
  9. Jeff KingJan 8, 2010
  10. Junio C HamanoJan 8, 2010
  11. Junio C HamanoJan 8, 2010
  12. ls-files: fix overeager pathspec optimizationJunio C Hamano, Jan 8, 2010
  13. Linus TorvaldsJan 8, 2010
  14. Junio C HamanoJan 8, 2010
  15. Junio C HamanoJan 9, 2010
  16. Linus TorvaldsJan 9, 2010
  17. Junio C HamanoJan 9, 2010
  18. Linus TorvaldsJan 9, 2010
  19. Jeff KingJan 9, 2010
  20. Junio C HamanoJan 9, 2010
  21. 1/4 t3001: test ls-files -o ignored/dirJunio C Hamano, Jan 9, 2010
  22. 2/4 read_directory_recursive(): refactor handling of a single path into a separate functionJunio C Hamano, Jan 9, 2010
  23. 3/4 read_directory(): further split treat_path()Junio C Hamano, Jan 9, 2010
  24. 4/4 ls-files: fix overeager pathspec optimizationJunio C Hamano, Jan 9, 2010
  25. Jeff KingJan 12, 2010
  26. Junio C HamanoJan 9, 2010
  27. Linus TorvaldsJan 9, 2010
  28. Junio C HamanoJan 10, 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.