Commit 5b9eac32c39358d8d65fbeb2ad7c5fd366532557

Vicent Marti 2014-04-02T13:42:14

Merge pull request #2233 from libgit2/rb/fix-untracked-repo-status Skip untracked contained repo contents even with gitlink files

diff --git a/src/diff.c b/src/diff.c
index 25c5937..484273f 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -880,8 +880,10 @@ static int handle_unmatched_new_item(
 			git_buf *full = NULL;
 			if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
 				return -1;
-			if (full && git_path_contains_dir(full, DOT_GIT))
+			if (full && git_path_contains(full, DOT_GIT)) {
+				/* TODO: warning if not a valid git repository */
 				recurse_into_dir = false;
+			}
 		}
 
 		/* still have to look into untracked directories to match core git -
diff --git a/tests/status/status_helpers.h b/tests/status/status_helpers.h
index f1f009e..242076c 100644
--- a/tests/status/status_helpers.h
+++ b/tests/status/status_helpers.h
@@ -8,9 +8,19 @@ typedef struct {
 	const unsigned int* expected_statuses;
 	const char** expected_paths;
 	int expected_entry_count;
+	const char *file;
+	int line;
 	bool debug;
 } status_entry_counts;
 
+#define status_counts_init(counts, paths, statuses) do { \
+	memset(&(counts), 0, sizeof(counts)); \
+	(counts).expected_statuses = (statuses); \
+	(counts).expected_paths = (paths); \
+	(counts).file = __FILE__; \
+	(counts).line = __LINE__; \
+	} while (0)
+
 /* cb_status__normal takes payload of "status_entry_counts *" */
 
 extern int cb_status__normal(
diff --git a/tests/status/submodules.c b/tests/status/submodules.c
index 8575f9f..6d0d63a 100644
--- a/tests/status/submodules.c
+++ b/tests/status/submodules.c
@@ -72,8 +72,15 @@ static int cb_status__match(const char *p, unsigned int s, void *payload)
 	status_entry_counts *counts = payload;
 	int idx = counts->entry_count++;
 
-	cl_assert_equal_s(counts->expected_paths[idx], p);
-	cl_assert(counts->expected_statuses[idx] == s);
+	clar__assert_equal(
+		counts->file, counts->line,
+		"Status path mismatch", 1,
+		"%s", counts->expected_paths[idx], p);
+
+	clar__assert_equal(
+		counts->file, counts->line,
+		"Status code mismatch", 1,
+		"%o", counts->expected_statuses[idx], s);
 
 	return 0;
 }
@@ -88,13 +95,9 @@ void test_status_submodules__1(void)
 	cl_assert(git_path_isdir("submodules/testrepo/.git"));
 	cl_assert(git_path_isfile("submodules/.gitmodules"));
 
-	memset(&counts, 0, sizeof(counts));
-	counts.expected_paths = expected_files;
-	counts.expected_statuses = expected_status;
+	status_counts_init(counts, expected_files, expected_status);
 
-	cl_git_pass(
-		git_status_foreach(g_repo, cb_status__match, &counts)
-	);
+	cl_git_pass( git_status_foreach(g_repo, cb_status__match, &counts) );
 
 	cl_assert_equal_i(6, counts.entry_count);
 }
@@ -146,24 +149,19 @@ void test_status_submodules__moved_head(void)
 
 	/* first do a normal status, which should now include the submodule */
 
-	memset(&counts, 0, sizeof(counts));
-	counts.expected_paths = expected_files_with_sub;
-	counts.expected_statuses = expected_status_with_sub;
-
 	opts.flags = GIT_STATUS_OPT_DEFAULTS;
 
+	status_counts_init(
+		counts, expected_files_with_sub, expected_status_with_sub);
 	cl_git_pass(
 		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
 	cl_assert_equal_i(7, counts.entry_count);
 
 	/* try again with EXCLUDE_SUBMODULES which should skip it */
 
-	memset(&counts, 0, sizeof(counts));
-	counts.expected_paths = expected_files;
-	counts.expected_statuses = expected_status;
-
 	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
 
+	status_counts_init(counts, expected_files, expected_status);
 	cl_git_pass(
 		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
 	cl_assert_equal_i(6, counts.entry_count);
@@ -201,24 +199,19 @@ void test_status_submodules__dirty_workdir_only(void)
 
 	/* first do a normal status, which should now include the submodule */
 
-	memset(&counts, 0, sizeof(counts));
-	counts.expected_paths = expected_files_with_sub;
-	counts.expected_statuses = expected_status_with_sub;
-
 	opts.flags = GIT_STATUS_OPT_DEFAULTS;
 
+	status_counts_init(
+		counts, expected_files_with_sub, expected_status_with_sub);
 	cl_git_pass(
 		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
 	cl_assert_equal_i(7, counts.entry_count);
 
 	/* try again with EXCLUDE_SUBMODULES which should skip it */
 
-	memset(&counts, 0, sizeof(counts));
-	counts.expected_paths = expected_files;
-	counts.expected_statuses = expected_status;
-
 	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
 
+	status_counts_init(counts, expected_files, expected_status);
 	cl_git_pass(
 		git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
 	cl_assert_equal_i(6, counts.entry_count);
@@ -240,3 +233,159 @@ void test_status_submodules__uninitialized(void)
 	git_repository_free(cloned_repo);
 	cl_git_sandbox_cleanup();
 }
+
+void test_status_submodules__contained_untracked_repo(void)
+{
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	status_entry_counts counts;
+	git_repository *contained;
+	static const char *expected_files_not_ignored[] = {
+		".gitmodules",
+		"added",
+		"deleted",
+		"modified",
+		"untracked"
+	};
+	static unsigned int expected_status_not_ignored[] = {
+		GIT_STATUS_WT_MODIFIED,
+		GIT_STATUS_INDEX_NEW,
+		GIT_STATUS_INDEX_DELETED,
+		GIT_STATUS_WT_MODIFIED,
+		GIT_STATUS_WT_NEW,
+	};
+	static const char *expected_files_with_untracked[] = {
+		".gitmodules",
+		"added",
+		"deleted",
+		"dir/file.md",
+		"modified",
+		"untracked"
+	};
+	static const char *expected_files_with_untracked_dir[] = {
+		".gitmodules",
+		"added",
+		"deleted",
+		"dir/",
+		"modified",
+		"untracked"
+	};
+	static unsigned int expected_status_with_untracked[] = {
+		GIT_STATUS_WT_MODIFIED,
+		GIT_STATUS_INDEX_NEW,
+		GIT_STATUS_INDEX_DELETED,
+		GIT_STATUS_WT_NEW,
+		GIT_STATUS_WT_MODIFIED,
+		GIT_STATUS_WT_NEW
+	};
+
+	g_repo = setup_fixture_submodules();
+
+	/* skip empty directory */
+
+	cl_must_pass(p_mkdir("submodules/dir", 0777));
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+
+	status_counts_init(
+		counts, expected_files_not_ignored, expected_status_not_ignored);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(5, counts.entry_count);
+
+	/* still skipping because empty == ignored */
+
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_not_ignored, expected_status_not_ignored);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(5, counts.entry_count);
+
+	/* find non-ignored contents of directory */
+
+	cl_git_mkfile("submodules/dir/file.md", "hello");
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_with_untracked, expected_status_with_untracked);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(6, counts.entry_count);
+
+	/* but skip if all content is ignored */
+
+	cl_git_append2file("submodules/.git/info/exclude", "\n*.md\n\n");
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_not_ignored, expected_status_not_ignored);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(5, counts.entry_count);
+
+	/* same is true if it contains a git link */
+
+	cl_git_mkfile("submodules/dir/.git", "gitlink: ../.git");
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_not_ignored, expected_status_not_ignored);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(5, counts.entry_count);
+
+	/* but if it contains tracked files, it should just show up as a
+	 * directory and exclude the files in it
+	 */
+
+	cl_git_mkfile("submodules/dir/another_file", "hello");
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_with_untracked_dir,
+		expected_status_with_untracked);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(6, counts.entry_count);
+
+	/* that applies to a git repo with a .git directory too */
+
+	cl_must_pass(p_unlink("submodules/dir/.git"));
+	cl_git_pass(git_repository_init(&contained, "submodules/dir", false));
+	git_repository_free(contained);
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+	status_counts_init(
+		counts, expected_files_with_untracked_dir,
+		expected_status_with_untracked);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(6, counts.entry_count);
+
+	/* same result even if we don't recurse into subdirectories */
+
+	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+
+	status_counts_init(
+		counts, expected_files_with_untracked_dir,
+		expected_status_with_untracked);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(6, counts.entry_count);
+
+	/* and if we remove the untracked file, it goes back to ignored */
+
+	cl_must_pass(p_unlink("submodules/dir/another_file"));
+
+	status_counts_init(
+		counts, expected_files_not_ignored, expected_status_not_ignored);
+	cl_git_pass(git_status_foreach_ext(
+		g_repo, &opts, cb_status__match, &counts));
+	cl_assert_equal_i(5, counts.entry_count);
+}