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

[PATCH 3/4] exclude: notice and report read failure of .gitignore files

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 18, 2024, 23:44 UTC
Message-ID
<20240618234436.4107855-4-gitster@pobox.com>
In-Reply-To
<20240618234436.4107855-1-gitster@pobox.com>

The code that reads .gitignore files was careless in dealing in unusual circumstances.

 - It let read errors silently ignored.
 - It tried to read ".gitignore" that is a directory on platforms
   that allow open(2) to open directories.

To make the latter consistent with what we do for fopen() on directories ("git grep" for FREAD_READS_DIRECTORIES for details), check if we opened a directory, silently close it and return success. Catch all read errors before we close and report as needed.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 dir.c              |  6 ++++++
 t/t0008-ignores.sh | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+)
diff --git a/dir.c b/dir.c
index ec875e3878..73f89f4d8c 100644
--- a/dir.c
+++ b/dir.c
@@ -1127,6 +1127,10 @@ static int add_patterns(const char *fname, const char *base, int baselen,
 						       oid_stat);
 		if (r != 1)
 			return r;
+	} else if (S_ISDIR(st.st_mode)) {
+		/* On FREAD_READS_DIRECTORIES platforms */
+		close(fd);
+		return 0;
 	} else {
 		size = xsize_t(st.st_size);
 		if (size == 0) {
@@ -1140,6 +1144,8 @@ static int add_patterns(const char *fname, const char *base, int baselen,
 		}
 		buf = xmallocz(size);
 		if (read_in_full(fd, buf, size) != size) {
+			warning_errno(_("failed while reading gitignore file '%s'"),
+				      fname);
 			free(buf);
 			close(fd);
 			return -1;
diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh
index 02a18d4fdb..c367824c66 100755
--- a/t/t0008-ignores.sh
+++ b/t/t0008-ignores.sh
@@ -953,4 +953,22 @@ test_expect_success EXPENSIVE 'large exclude file ignored in tree' '
 	test_cmp expect err
 '
 
+test_expect_success POSIXPERM 'unreadable exclude file reported' '
+	test_when_finished "rm -f .gitignore" &&
+	>.gitignore &&
+	chmod a= .gitignore &&
+	# we do not care if the pattern matches
+	{ git check-ignore xyzzy 2>err || :; } &&
+	test_grep "unable to access ${SQ}\.gitignore${SQ}:" err
+'
+
+test_expect_success '.gitignore directory ignored' '
+	test_when_finished "rm -rf .gitignore" &&
+	rm -f .gitignore &&
+	mkdir .gitignore &&
+	# we do not care if the pattern matches
+	{ git check-ignore xyzzy 2>err || :; } &&
+	test_grep ! "failed while reading gitignore file ${SQ}\.gitignore${SQ}:" err
+'
+
 test_done
-- 
2.45.2-711-gd2c001ca14
Previous: Junio C Hamano
Message 17 of 17 in “Non-blob .gitmodules and .gitattributes”
  1. Alexey PelykhJun 14, 2024
  2. Junio C HamanoJun 14, 2024
  3. Alexey PelykhJun 14, 2024
  4. Jeff KingJun 18, 2024
  5. Alexey PelykhJun 18, 2024
  6. Junio C HamanoJun 18, 2024
  7. Jeff KingJun 18, 2024
  8. 0/4 .git{ignore,attributes} directories?Junio C Hamano, Jun 18, 2024
  9. 1/4 .gitignore: introduce GITIGNORE_FILE CPP macroJunio C Hamano, Jun 18, 2024
  10. 2/4 attr: notice and report read failure of .gitattributes filesJunio C Hamano, Jun 18, 2024
  11. Eric SunshineJun 19, 2024
  12. Junio C HamanoJun 19, 2024
  13. Eric SunshineJun 19, 2024
  14. Jeff KingJun 19, 2024
  15. Junio C HamanoJun 20, 2024
  16. 4/4 submodule: ignore .gitmodules that is not a regular fileJunio C Hamano, Jun 18, 2024
  17. 3/4 exclude: notice and report read failure of .gitignore filesJunio C Hamano, Jun 18, 2024

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.