Merge pull request #551 from schu/treebuilder-entries

treebuilder: remove needless variable entry_count
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7858164..3a09605 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -65,7 +65,7 @@
 	SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
 	SET(WIN_RC "src/win32/git2.rc")
 ELSE ()
-	SET(CMAKE_C_FLAGS "-O2 -g -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
+	SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
 	SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
 	IF (NOT MINGW) # MinGW always does PIC and complains if we tell it to
 		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
diff --git a/src/attr.c b/src/attr.c
index da0f723..17571f6 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -21,7 +21,8 @@
 
 	*value = NULL;
 
-	if ((error = git_attr_path__init(&path, pathname)) < GIT_SUCCESS ||
+	if ((error = git_attr_path__init(
+			&path, pathname, git_repository_workdir(repo))) < GIT_SUCCESS ||
 		(error = collect_attr_files(repo, pathname, &files)) < GIT_SUCCESS)
 		return git__rethrow(error, "Could not get attribute for %s", pathname);
 
@@ -69,7 +70,8 @@
 
 	memset((void *)values, 0, sizeof(const char *) * num_attr);
 
-	if ((error = git_attr_path__init(&path, pathname)) < GIT_SUCCESS ||
+	if ((error = git_attr_path__init(
+			&path, pathname, git_repository_workdir(repo))) < GIT_SUCCESS ||
 		(error = collect_attr_files(repo, pathname, &files)) < GIT_SUCCESS)
 		return git__rethrow(error, "Could not get attributes for %s", pathname);
 
@@ -130,7 +132,8 @@
 	git_attr_assignment *assign;
 	git_hashtable *seen = NULL;
 
-	if ((error = git_attr_path__init(&path, pathname)) < GIT_SUCCESS ||
+	if ((error = git_attr_path__init(
+			&path, pathname, git_repository_workdir(repo))) < GIT_SUCCESS ||
 		(error = collect_attr_files(repo, pathname, &files)) < GIT_SUCCESS)
 		return git__rethrow(error, "Could not get attributes for %s", pathname);
 
@@ -207,6 +210,13 @@
 	return error;
 }
 
+int git_attr_cache__is_cached(git_repository *repo, const char *path)
+{
+	const char *cache_key = path;
+	if (repo && git__prefixcmp(cache_key, git_repository_workdir(repo)) == 0)
+		cache_key += strlen(git_repository_workdir(repo));
+	return (git_hashtable_lookup(repo->attrcache.files, cache_key) == NULL);
+}
 
 /* add git_attr_file to vector of files, loading if needed */
 int git_attr_cache__push_file(
@@ -219,8 +229,9 @@
 	int error = GIT_SUCCESS;
 	git_attr_cache *cache = &repo->attrcache;
 	git_buf path = GIT_BUF_INIT;
-	git_attr_file *file;
+	git_attr_file *file = NULL;
 	int add_to_cache = 0;
+	const char *cache_key;
 
 	if (base != NULL) {
 		if ((error = git_buf_joinpath(&path, base, filename)) < GIT_SUCCESS)
@@ -229,10 +240,18 @@
 	}
 
 	/* either get attr_file from cache or read from disk */
-	file = git_hashtable_lookup(cache->files, filename);
+	cache_key = filename;
+	if (repo && git__prefixcmp(cache_key, git_repository_workdir(repo)) == 0)
+		cache_key += strlen(git_repository_workdir(repo));
+
+	file = git_hashtable_lookup(cache->files, cache_key);
 	if (file == NULL && git_path_exists(filename) == GIT_SUCCESS) {
-		if ((error = git_attr_file__new(&file)) == GIT_SUCCESS)
-			error = loader(repo, filename, file);
+		if ((error = git_attr_file__new(&file)) == GIT_SUCCESS) {
+			if ((error = loader(repo, filename, file)) < GIT_SUCCESS) {
+				git_attr_file__free(file);
+				file = NULL;
+			}
+		}
 		add_to_cache = (error == GIT_SUCCESS);
 	}
 
diff --git a/src/attr.h b/src/attr.h
index a758cc4..ea27259 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -27,4 +27,7 @@
 	const char     *filename,
 	int (*loader)(git_repository *, const char *, git_attr_file *));
 
+/* returns GIT_SUCCESS if path is in cache */
+extern int git_attr_cache__is_cached(git_repository *repo, const char *path);
+
 #endif
diff --git a/src/attr_file.c b/src/attr_file.c
index 5fd136c..7911381 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -200,6 +200,8 @@
 
 	if (match->flags & GIT_ATTR_FNMATCH_FULLPATH)
 		matched = p_fnmatch(match->pattern, path->path, FNM_PATHNAME);
+	else if (path->is_dir)
+		matched = p_fnmatch(match->pattern, path->basename, FNM_LEADING_DIR);
 	else
 		matched = p_fnmatch(match->pattern, path->basename, 0);
 
@@ -234,7 +236,7 @@
 }
 
 int git_attr_path__init(
-	git_attr_path *info, const char *path)
+	git_attr_path *info, const char *path, const char *base)
 {
 	assert(info && path);
 	info->path = path;
@@ -243,7 +245,17 @@
 		info->basename++;
 	if (!info->basename || !*info->basename)
 		info->basename = path;
+
+	if (base != NULL && git_path_root(path) < 0) {
+		git_buf full_path = GIT_BUF_INIT;
+		int error = git_buf_joinpath(&full_path, base, path);
+		if (error == GIT_SUCCESS)
+			info->is_dir = (git_path_isdir(full_path.ptr) == GIT_SUCCESS);
+		git_buf_free(&full_path);
+		return error;
+	}
 	info->is_dir = (git_path_isdir(path) == GIT_SUCCESS);
+
 	return GIT_SUCCESS;
 }
 
diff --git a/src/attr_file.h b/src/attr_file.h
index 304c7a8..dcb66c5 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -110,7 +110,7 @@
 	git_attr_rule *rule, const char *name);
 
 extern int git_attr_path__init(
-	git_attr_path *info, const char *path);
+	git_attr_path *info, const char *path, const char *base);
 
 extern int git_attr_assignment__parse(
 	git_repository *repo, /* needed to expand macros */
diff --git a/src/ignore.c b/src/ignore.c
index 516da64..9690eba 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -66,24 +66,20 @@
 #define push_ignore(R,S,B,F) \
 	git_attr_cache__push_file((R),(S),(B),(F),load_ignore_file)
 
-typedef struct {
-	git_repository *repo;
-	git_vector *stack;
-} ignore_walk_up_info;
-
 static int push_one_ignore(void *ref, git_buf *path)
 {
-	ignore_walk_up_info *info = (ignore_walk_up_info *)ref;
-	return push_ignore(info->repo, info->stack, path->ptr, GIT_IGNORE_FILE);
+	git_ignores *ign = (git_ignores *)ref;
+	return push_ignore(ign->repo, &ign->stack, path->ptr, GIT_IGNORE_FILE);
 }
 
-int git_ignore__for_path(git_repository *repo, const char *path, git_vector *stack)
+int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ignores)
 {
 	int error = GIT_SUCCESS;
 	git_buf dir = GIT_BUF_INIT;
 	git_config *cfg;
 	const char *workdir = git_repository_workdir(repo);
-	ignore_walk_up_info info;
+
+	assert(ignores);
 
 	if ((error = git_attr_cache__init(repo)) < GIT_SUCCESS)
 		goto cleanup;
@@ -91,18 +87,20 @@
 	if ((error = git_path_find_dir(&dir, path, workdir)) < GIT_SUCCESS)
 		goto cleanup;
 
+	ignores->repo = repo;
+	ignores->dir  = NULL;
+	git_vector_init(&ignores->stack, 2, NULL);
+
 	/* insert internals */
-	if ((error = push_ignore(repo, stack, NULL, GIT_IGNORE_INTERNAL)) < GIT_SUCCESS)
+	if ((error = push_ignore(repo, &ignores->stack, NULL, GIT_IGNORE_INTERNAL)) < GIT_SUCCESS)
 		goto cleanup;
 
 	/* load .gitignore up the path */
-	info.repo = repo;
-	info.stack = stack;
-	if ((error = git_path_walk_up(&dir, workdir, push_one_ignore, &info)) < GIT_SUCCESS)
+	if ((error = git_path_walk_up(&dir, workdir, push_one_ignore, ignores)) < GIT_SUCCESS)
 		goto cleanup;
 
 	/* load .git/info/exclude */
-	if ((error = push_ignore(repo, stack, repo->path_repository, GIT_IGNORE_FILE_INREPO)) < GIT_SUCCESS)
+	if ((error = push_ignore(repo, &ignores->stack, repo->path_repository, GIT_IGNORE_FILE_INREPO)) < GIT_SUCCESS)
 		goto cleanup;
 
 	/* load core.excludesfile */
@@ -110,7 +108,7 @@
 		const char *core_ignore;
 		error = git_config_get_string(cfg, GIT_IGNORE_CONFIG, &core_ignore);
 		if (error == GIT_SUCCESS && core_ignore != NULL)
-			error = push_ignore(repo, stack, NULL, core_ignore);
+			error = push_ignore(repo, &ignores->stack, NULL, core_ignore);
 		else {
 			error = GIT_SUCCESS;
 			git_clearerror(); /* don't care if attributesfile is not set */
@@ -121,18 +119,22 @@
 cleanup:
 	if (error < GIT_SUCCESS)
 		git__rethrow(error, "Could not get ignore files for '%s'", path);
+	else
+		ignores->dir = git_buf_detach(&dir);
 
 	git_buf_free(&dir);
 
 	return error;
 }
 
-void git_ignore__free(git_vector *stack)
+void git_ignore__free(git_ignores *ignores)
 {
-	git_vector_free(stack);
+	git__free(ignores->dir);
+	ignores->dir = NULL;
+	git_vector_free(&ignores->stack);
 }
 
-int git_ignore__lookup(git_vector *stack, const char *pathname, int *ignored)
+int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
 {
 	int error;
 	unsigned int i, j;
@@ -140,12 +142,13 @@
 	git_attr_path path;
 	git_attr_fnmatch *match;
 
-	if ((error = git_attr_path__init(&path, pathname)) < GIT_SUCCESS)
+	if ((error = git_attr_path__init(
+		&path, pathname, git_repository_workdir(ignores->repo))) < GIT_SUCCESS)
 		return git__rethrow(error, "Could not get attribute for '%s'", pathname);
 
 	*ignored = 0;
 
-	git_vector_foreach(stack, i, file) {
+	git_vector_foreach(&ignores->stack, i, file) {
 		git_vector_rforeach(&file->rules, j, match) {
 			if (git_attr_fnmatch__match(match, &path) == GIT_SUCCESS) {
 				*ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0);
diff --git a/src/ignore.h b/src/ignore.h
index 2954445..9f87ae5 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -10,8 +10,14 @@
 #include "repository.h"
 #include "vector.h"
 
-extern int git_ignore__for_path(git_repository *repo, const char *path, git_vector *stack);
-extern void git_ignore__free(git_vector *stack);
-extern int git_ignore__lookup(git_vector *stack, const char *path, int *ignored);
+typedef struct {
+	git_repository *repo;
+	char *dir;
+	git_vector stack;
+} git_ignores;
+
+extern int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *stack);
+extern void git_ignore__free(git_ignores *stack);
+extern int git_ignore__lookup(git_ignores *stack, const char *path, int *ignored);
 
 #endif
diff --git a/src/netops.c b/src/netops.c
index 73375d7..0f85eae 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -8,6 +8,7 @@
 #	include <sys/types.h>
 #	include <sys/socket.h>
 #	include <sys/select.h>
+#	include <sys/time.h>
 #	include <netdb.h>
 #else
 #	define _WIN32_WINNT 0x0501
diff --git a/src/refspec.c b/src/refspec.c
index 7694be5..48265bc 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -107,7 +107,7 @@
 		return GIT_SUCCESS;
 
 	git_buf_truncate(out, out->size - 1); /* remove trailing '*' */
-	git_buf_puts(out, name);
+	git_buf_puts(out, name + strlen(spec->src) - 1);
 
 	return git_buf_lasterror(out);
 }
diff --git a/src/repository.c b/src/repository.c
index 74f0d8f..536522a 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -166,7 +166,7 @@
 	 * of the working dir, by testing if it contains a `.git`
 	 * folder inside of it.
 	 */
-	git_path_contains_dir(&path_buf, DOT_GIT, 1); /* append on success */
+	git_path_contains_dir(&path_buf, GIT_DIR, 1); /* append on success */
 	/* ignore error, since it just means `path/.git` doesn't exist */
 
 	if (quickcheck_repository_dir(&path_buf) < GIT_SUCCESS) {
diff --git a/src/status.c b/src/status.c
index 492edf5..62ef356 100644
--- a/src/status.c
+++ b/src/status.c
@@ -124,7 +124,7 @@
 	return (e->status_flags == GIT_STATUS_WT_NEW);
 }
 
-static int status_entry_update_ignore(struct status_entry *e, git_vector *ignores, const char *path)
+static int status_entry_update_ignore(struct status_entry *e, git_ignores *ignores, const char *path)
 {
 	int error, ignored;
 
@@ -141,7 +141,7 @@
 	git_vector *vector;
 	git_index *index;
 	git_tree *tree;
-	git_vector *ignores;
+	git_ignores *ignores;
 
 	int workdir_path_len;
 	git_buf head_tree_relative_path;
@@ -226,14 +226,18 @@
 			/* No op */
 			break;
 
+		case GIT_OBJ_COMMIT:
+			/* TODO: proper submodule support */
+			break;
+
 		default:
-			error = git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type");	/* TODO: How should we deal with submodules? */
+			return git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type");
 		}
 	}
 
 
 	if (full_path != NULL && path_type == GIT_STATUS_PATH_FOLDER) {
-		git_vector ignores = GIT_VECTOR_INIT, *old_ignores;
+		git_ignores ignores, *old_ignores;
 
 		if ((error = git_ignore__for_path(st->repo,
 			full_path->ptr + st->workdir_path_len, &ignores)) == GIT_SUCCESS)
@@ -246,8 +250,9 @@
 			git_ignore__free(st->ignores);
 			st->ignores = old_ignores;
 		}
-	} else
+	} else {
 		error = dirent_cb(st, NULL);
+	}
 
 	if (tree_entry_type == GIT_OBJ_TREE) {
 		git_object_free(subtree);
@@ -320,9 +325,19 @@
 		return store_if_changed(st, e);
 	}
 
-	/* Last option, we're dealing with a leftover folder tree entry */
-	assert(in_head && !in_index && !in_workdir && (tree_entry_type == GIT_OBJ_TREE));
-	return process_folder(st, tree_entry, full_path, path_type);
+	/* Are we dealing with a subtree? */
+	if (tree_entry_type == GIT_OBJ_TREE) {
+		assert(in_head && !in_index && !in_workdir);
+		return process_folder(st, tree_entry, full_path, path_type);
+	}
+
+	/* We're dealing with something else -- most likely a submodule;
+	 * skip it for now */
+	if (in_head)
+		st->tree_position++;
+	if (in_index)
+		st->index_position++;
+	return GIT_SUCCESS;
 }
 
 static int path_type_from(git_buf *full_path, int is_dir)
@@ -451,7 +466,8 @@
 	int (*callback)(const char *, unsigned int, void *),
 	void *payload)
 {
-	git_vector entries, ignores = GIT_VECTOR_INIT;
+	git_vector entries;
+	git_ignores ignores;
 	git_index *index = NULL;
 	git_buf temp_path = GIT_BUF_INIT;
 	struct status_st dirent_st = {0};
@@ -533,7 +549,7 @@
 	git_buf_free(&dirent_st.head_tree_relative_path);
 	git_buf_free(&temp_path);
 	git_vector_free(&entries);
-	git_vector_free(&ignores);
+	git_ignore__free(&ignores);
 	git_tree_free(tree);
 	return error;
 }
@@ -651,7 +667,7 @@
 	}
 
 	if (status_entry_is_ignorable(e)) {
-		git_vector ignores = GIT_VECTOR_INIT;
+		git_ignores ignores;
 
 		if ((error = git_ignore__for_path(repo, path, &ignores)) == GIT_SUCCESS)
 			error = status_entry_update_ignore(e, &ignores, path);
@@ -766,7 +782,7 @@
 int git_status_should_ignore(git_repository *repo, const char *path, int *ignored)
 {
 	int error;
-	git_vector ignores = GIT_VECTOR_INIT;
+	git_ignores ignores;
 
 	if ((error = git_ignore__for_path(repo, path, &ignores)) == GIT_SUCCESS)
 		error = git_ignore__lookup(&ignores, path, ignored);
diff --git a/tests-clar/attr/lookup.c b/tests-clar/attr/lookup.c
index 7779e04..9462bbe 100644
--- a/tests-clar/attr/lookup.c
+++ b/tests-clar/attr/lookup.c
@@ -12,7 +12,7 @@
 	cl_assert_strequal(cl_fixture("attr/attr0"), file->path);
 	cl_assert(file->rules.length == 1);
 
-	cl_git_pass(git_attr_path__init(&path, "test"));
+	cl_git_pass(git_attr_path__init(&path, "test", NULL));
 	cl_assert_strequal("test", path.path);
 	cl_assert_strequal("test", path.basename);
 	cl_assert(!path.is_dir);
@@ -42,7 +42,7 @@
 	int error;
 
 	for (c = cases; c->path != NULL; c++) {
-		cl_git_pass(git_attr_path__init(&path, c->path));
+		cl_git_pass(git_attr_path__init(&path, c->path, NULL));
 
 		if (c->force_dir)
 			path.is_dir = 1;
@@ -138,7 +138,7 @@
 	cl_assert_strequal(cl_fixture("attr/attr1"), file->path);
 	cl_assert(file->rules.length == 10);
 
-	cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0"));
+	cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL));
 	cl_assert_strequal("pat0", path.basename);
 
 	run_test_cases(file, cases);
diff --git a/tests-clar/attr/repo.c b/tests-clar/attr/repo.c
index 6fc36d2..7a71604 100644
--- a/tests-clar/attr/repo.c
+++ b/tests-clar/attr/repo.c
@@ -1,6 +1,7 @@
 #include "clar_libgit2.h"
 #include "fileops.h"
 #include "git2/attr.h"
+#include "attr.h"
 
 static git_repository *g_repo = NULL;
 
@@ -89,6 +90,10 @@
 
 		git_buf_free(&b);
 	}
+
+	cl_git_pass(git_attr_cache__is_cached(g_repo, ".git/info/attributes"));
+	cl_git_pass(git_attr_cache__is_cached(g_repo, ".gitattributes"));
+	cl_git_pass(git_attr_cache__is_cached(g_repo, "sub/.gitattributes"));
 }
 
 void test_attr_repo__get_many(void)
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 2abaccb..f3a45d6 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -1,4 +1,6 @@
 #include "clar_libgit2.h"
+#include "buffer.h"
+#include "refspec.h"
 
 static git_remote *_remote;
 static git_repository *_repo;
@@ -48,3 +50,12 @@
 	cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
 	cl_assert(!strcmp(ref, "refs/remotes/test/master"));
 }
+
+void test_network_remotes__transform_r(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+
+	cl_git_pass(git_refspec_transform_r(&buf,  _refspec, "refs/heads/master"));
+	cl_assert(!strcmp(git_buf_cstr(&buf), "refs/remotes/test/master"));
+	git_buf_free(&buf);
+}
diff --git a/tests-clar/refs/listall.c b/tests-clar/refs/listall.c
new file mode 100644
index 0000000..4aa7051
--- /dev/null
+++ b/tests-clar/refs/listall.c
@@ -0,0 +1,36 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+
+static git_repository *repo;
+static git_strarray ref_list;
+
+static void ensure_no_refname_starts_with_a_forward_slash(const char *path)
+{
+	int i;
+
+	cl_git_pass(git_repository_open(&repo, path));
+	cl_git_pass(git_reference_listall(&ref_list, repo, GIT_REF_LISTALL));
+
+	cl_assert(ref_list.count > 0);
+
+	for (i = 0; i < ref_list.count; i++)
+		cl_assert(git__prefixcmp(ref_list.strings[i], "/") != 0);
+
+	git_strarray_free(&ref_list);
+	git_repository_free(repo);
+}
+
+void test_refs_listall__from_repository_opened_through_workdir_path(void)
+{
+	cl_fixture_sandbox("status");
+	cl_git_pass(p_rename("status/.gitted", "status/.git"));
+
+	ensure_no_refname_starts_with_a_forward_slash("status");
+
+	cl_fixture_cleanup("status");
+}
+
+void test_refs_listall__from_repository_opened_through_gitdir_path(void)
+{
+	ensure_no_refname_starts_with_a_forward_slash(cl_fixture("testrepo.git"));
+}
diff --git a/tests-clar/repo/open.c b/tests-clar/repo/open.c
index b500284..c3a7dad 100644
--- a/tests-clar/repo/open.c
+++ b/tests-clar/repo/open.c
@@ -1,24 +1,46 @@
 #include "clar_libgit2.h"
 #include "posix.h"
 
-void test_repo_open__bare_empty_repo(void)
+static git_repository *repo;
+
+void test_repo_open__cleanup(void)
 {
-	git_repository *repo;
-
-	cl_git_pass(git_repository_open(&repo, cl_fixture("empty_bare.git")));
-	cl_assert(git_repository_path(repo) != NULL);
-	cl_assert(git_repository_workdir(repo) == NULL);
-
 	git_repository_free(repo);
 }
 
-void test_repo_open__standard_empty_repo(void)
+void test_repo_open__bare_empty_repo(void)
 {
-	git_repository *repo;
+	cl_git_pass(git_repository_open(&repo, cl_fixture("empty_bare.git")));
 
-	cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
 	cl_assert(git_repository_path(repo) != NULL);
-	cl_assert(git_repository_workdir(repo) != NULL);
+	cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
 
-	git_repository_free(repo);
+	cl_assert(git_repository_workdir(repo) == NULL);
+}
+
+void test_repo_open__standard_empty_repo_through_gitdir(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
+
+	cl_assert(git_repository_path(repo) != NULL);
+	cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
+
+	cl_assert(git_repository_workdir(repo) != NULL);
+	cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
+}
+
+void test_repo_open__standard_empty_repo_through_workdir(void)
+{
+	cl_fixture_sandbox("empty_standard_repo");
+	cl_git_pass(p_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
+
+	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+
+	cl_assert(git_repository_path(repo) != NULL);
+	cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
+
+	cl_assert(git_repository_workdir(repo) != NULL);
+	cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
+
+	cl_fixture_cleanup("empty_standard_repo");
 }
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 3a66b3a..67aecba 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -1,6 +1,7 @@
 #include "clar_libgit2.h"
 #include "fileops.h"
 #include "git2/attr.h"
+#include "attr.h"
 
 static git_repository *g_repo = NULL;
 
@@ -29,6 +30,7 @@
 		const char *path;
 		int expected;
 	} test_cases[] = {
+		/* patterns "sub" and "ign" from .gitignore */
 		{ "file", 0 },
 		{ "ign", 1 },
 		{ "sub", 1 },
@@ -38,6 +40,12 @@
 		{ "sub/sub/file", 0 },
 		{ "sub/sub/ign", 1 },
 		{ "sub/sub/sub", 1 },
+		/* pattern "dir/" from .gitignore */
+		{ "dir", 1 },
+		{ "dir/", 1 },
+		{ "sub/dir", 1 },
+		{ "sub/dir/", 1 },
+		{ "sub/sub/dir", 0 }, /* dir is not actually a dir, but a file */
 		{ NULL, 0 }
 	}, *one_test;
 
@@ -46,4 +54,8 @@
 		cl_git_pass(git_status_should_ignore(g_repo, one_test->path, &ignored));
 		cl_assert_(ignored == one_test->expected, one_test->path);
 	}
+
+	/* confirm that ignore files were cached */
+	cl_git_pass(git_attr_cache__is_cached(g_repo, ".git/info/exclude"));
+	cl_git_pass(git_attr_cache__is_cached(g_repo, ".gitignore"));
 }
diff --git a/tests/resources/attr/dir/file b/tests/resources/attr/dir/file
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/resources/attr/dir/file
diff --git a/tests/resources/attr/gitignore b/tests/resources/attr/gitignore
index 66f7769..546d48f 100644
--- a/tests/resources/attr/gitignore
+++ b/tests/resources/attr/gitignore
@@ -1,2 +1,3 @@
 sub
 ign
+dir/
diff --git a/tests/resources/attr/sub/dir/file b/tests/resources/attr/sub/dir/file
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/resources/attr/sub/dir/file
diff --git a/tests/resources/attr/sub/sub/dir b/tests/resources/attr/sub/sub/dir
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/resources/attr/sub/sub/dir
diff --git a/tests/test_main.c b/tests/test_main.c
index 1ebb222..732d25a 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -77,6 +77,8 @@
 	GIT_UNUSED_ARG(argc);
 	GIT_UNUSED_ARG(argv);
 
+	git_threads_init();
+
 	p_umask(0);
 
 	failures = 0;
@@ -84,6 +86,8 @@
 	for (i = 0; i < GIT_SUITE_COUNT; ++i)
 		failures += git_testsuite_run(suite_methods[i]());
 
+	git_threads_shutdown();
+
 	return failures ? -1 : 0;
 }