Commit 093d579f7be5636c52290a517eee1eebb7471e31

Edward Thomson 2021-07-22T15:07:35

attr: cache nonexistent attr files from commits When looking up an attribute file in a commit, we can cache a nonexistent attribute file indefinitely (since a commit could not somehow later contain an attribute file). Cache an empty buffer when an attribute file does not exist in a given commit.

diff --git a/src/attr_file.c b/src/attr_file.c
index b586023..3b8965a 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -163,9 +163,24 @@ int git_attr_file__load(
 		break;
 	}
 	case GIT_ATTR_FILE_SOURCE_COMMIT: {
-		if ((error = git_repository_head_tree(&tree, repo)) < 0 ||
-		    (error = git_tree_entry_bypath(&tree_entry, tree, entry->path)) < 0 ||
-		    (error = git_blob_lookup(&blob, repo, git_tree_entry_id(tree_entry))) < 0)
+		if ((error = git_repository_head_tree(&tree, repo)) < 0)
+			goto cleanup;
+
+		if ((error = git_tree_entry_bypath(&tree_entry, tree, entry->path)) < 0) {
+			/*
+			 * If the attributes file does not exist, we can
+			 * cache an empty file for this commit to prevent
+			 * needless future lookups.
+			 */
+			if (error == GIT_ENOTFOUND) {
+				error = 0;
+				break;
+			}
+
+			goto cleanup;
+		}
+
+		if ((error = git_blob_lookup(&blob, repo, git_tree_entry_id(tree_entry))) < 0)
 			goto cleanup;
 
 		/*