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

[PATCH] Teach ignore machinery about pattern "/"

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 25, 2012, 12:47 UTC
Message-ID
<1337950056-26491-1-git-send-email-pclouds@gmail.com>

Pattern "/" is ambiguous because the slash can mean "anchor the pattern to this location" (e.g. /path), but it can also mean "match directories only" (e.g. path/). Currently git interprets it as the latter except that 'path' is an empty string, which makes this pattern totally useless.

On the other hand, it's intuitive to read '/' as "match root directory", or equivalent to "/*". (The other way to see it is "match all directories", which leads to the same result).

One may wonder why bother an "ignore all" pattern. The pattern is useful when you want to keep just a small portion of the working directory. But that's still a rare case.

A more popular case would be sparse checkout, where ignore rules are used to _include_. The thus now "include all" pattern is used to say "make a sparse checkout that includes everything except this and this".

Recognize this special pattern and turn it the working equivalence pattern "/*"

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 dir.c                              | 3 +++
 t/t3001-ls-files-others-exclude.sh | 6 ++++++
 2 files changed, 9 insertions(+)
diff --git a/dir.c b/dir.c
index c6a98cc..b65d37c 100644
--- a/dir.c
+++ b/dir.c
@@ -308,6 +308,9 @@ void add_exclude(const char *string, const char *base,
 	int to_exclude = 1;
 	int flags = 0;
 
+	if (string[0] == '/' && string[1] == '\0')
+		string = "/*";
+
 	if (*string == '!') {
 		to_exclude = 0;
 		string++;
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index c8fe978..7cb790d 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -175,6 +175,12 @@ test_expect_success 'negated exclude matches can override previous ones' '
 	grep "^a.1" output
 '
 
+test_expect_success '"/" pattern is equivalent to "/*" (exclude all)' '
+	git ls-files --others --exclude=/ >output &&
+	: >expected &&
+	test_cmp expected output
+'
+
 test_expect_success 'subdirectory ignore (setup)' '
 	mkdir -p top/l1/l2 &&
 	(
-- 
1.7.10.2.549.g9354186
Next: Junio C Hamano
Message 1 of 7 in “Teach ignore machinery about pattern "/"”
  1. Teach ignore machinery about pattern "/"Nguyễn Thái Ngọc Duy, May 25, 2012
  2. Junio C HamanoMay 25, 2012
  3. Nguyen Thai Ngoc DuyMay 26, 2012
  4. Junio C HamanoMay 27, 2012
  5. Nguyen Thai Ngoc DuyJun 3, 2012
  6. Junio C HamanoJun 3, 2012
  7. Caleb MarchentJun 3, 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.