Commit 82c7a9bcc6801aaa94dd63ab1add230dbc896211

Patrick Steinhardt 2019-06-26T14:49:24

attr: fix attribute lookup if repo has no common directory If creating a repository without a common directory (e.g. by using `git_repository_new`), then `git_repository_item_path` will return `GIT_ENOTFOUND` for every file that's usually located in this directory. While we do not care for this case when looking up the "info/attributes" file, we fail to properly ignore these errors when setting up or collecting attributes files. Thus, the gitattributes lookup is broken and will only ever return `GIT_ENOTFOUND`. Fix this issue by properly ignoring `GIT_ENOTFOUND` returned by `git_repository_item_path`.

diff --git a/src/attr.c b/src/attr.c
index d9ddcfb..f50d28c 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -335,8 +335,10 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
 
 	if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
 	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
-				       path.ptr, GIT_ATTR_FILE_INREPO)) < 0)
-		goto out;
+				       path.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
+		if (error != GIT_ENOTFOUND)
+			goto out;
+	}
 
 	if ((workdir = git_repository_workdir(repo)) != NULL &&
 	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
@@ -510,15 +512,12 @@ static int collect_attr_files(
 	 * - $GIT_PREFIX/etc/gitattributes
 	 */
 
-	error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO);
-	if (error < 0)
-		goto cleanup;
-
-	error = push_attr_file(
-		repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
-		attrfile.ptr, GIT_ATTR_FILE_INREPO);
-	if (error < 0)
-		goto cleanup;
+	if ((error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
+	    (error = push_attr_file(repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
+				    attrfile.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
+		if (error != GIT_ENOTFOUND)
+			goto cleanup;
+	}
 
 	info.repo = repo;
 	info.attr_session = attr_session;