Commit e9628e7b8d4f19a12a1cc98306c973e36c20f29c

Andy Doan 2017-10-30T11:38:33

branches: Check symlinked subdirectories Native Git allows symlinked directories under .git/refs. This change allows libgit2 to also look for references that live under symlinked directories. Signed-off-by: Andy Doan <andy@opensourcefoundries.com>

diff --git a/src/iterator.c b/src/iterator.c
index 9600312..b8f2e79 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -23,6 +23,7 @@
 #define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
 #define iterator__honor_ignores(I)     iterator__flag(I,HONOR_IGNORES)
 #define iterator__ignore_dot_git(I)    iterator__flag(I,IGNORE_DOT_GIT)
+#define iterator__symlinksdir(I)       iterator__flag(I,INCLUDE_SYMLINK_REFSDIR)
 
 
 static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
@@ -1491,6 +1492,25 @@ static int filesystem_iterator_current(
 	return 0;
 }
 
+static int is_directory(
+	const filesystem_iterator *iter, const filesystem_iterator_entry *entry)
+{
+	int isdir = 0;
+	struct stat st;
+	git_buf fullpath;
+
+	if (S_ISDIR(entry->st.st_mode))
+		return 1;
+	if (!iterator__symlinksdir(iter) || !S_ISLNK(entry->st.st_mode))
+		return 0;
+
+	git_buf_init(&fullpath, 0);
+	git_buf_joinpath(&fullpath, iter->root, entry->path);
+	isdir = !p_stat(fullpath.ptr, &st) && S_ISDIR(st.st_mode);
+	git_buf_free(&fullpath);
+	return isdir;
+}
+
 static int filesystem_iterator_advance(
 	const git_index_entry **out, git_iterator *i)
 {
@@ -1519,7 +1539,7 @@ static int filesystem_iterator_advance(
 		entry = frame->entries.contents[frame->next_idx];
 		frame->next_idx++;
 
-		if (S_ISDIR(entry->st.st_mode)) {
+		if (is_directory(iter, entry)) {
 			if (iterator__do_autoexpand(iter)) {
 				error = filesystem_iterator_frame_push(iter, entry);
 
diff --git a/src/iterator.h b/src/iterator.h
index 0bcb128..cde35cc 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -39,6 +39,8 @@ typedef enum {
 	GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
 	/** include conflicts */
 	GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
+	/** descend into symlinked directories when looking for references */
+	GIT_ITERATOR_INCLUDE_SYMLINK_REFSDIR = (1u << 7),
 } git_iterator_flag_t;
 
 typedef enum {
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index ade734c..c09b40c 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -2035,6 +2035,7 @@ int git_refdb_backend_fs(
 	if ((!git_repository__cvar(&t, backend->repo, GIT_CVAR_FSYNCOBJECTFILES) && t) ||
 		git_repository__fsync_gitdir)
 		backend->fsync = 1;
+	backend->iterator_flags |= GIT_ITERATOR_INCLUDE_SYMLINK_REFSDIR;
 
 	backend->parent.exists = &refdb_fs_backend__exists;
 	backend->parent.lookup = &refdb_fs_backend__lookup;