Commit 37f9e4093999498a25641018da36245d6a7cb008

Russell Belfer 2013-09-13T21:43:00

Some tests with ident and crlf filters Fixed the filter order to match core Git, too. This test demonstrates an interesting behavior of core Git (which is totally reasonable and which libgit2 matches, although mostly by coincidence). If you use the ident filter and commit a file with a garbage ident in it, like '$Id: this is just garbage$' and then immediately do a 'git checkout-index' with the new file, Git will not consider the file out of date and will not overwrite the file with an updated $Id$. Libgit2 has the same behavior. If you remove the file and then do a checkout-index, it will be replaced with a filtered version that has injected the OID correctly.

diff --git a/examples/showindex.c b/examples/showindex.c
index e92a9c8..93718c8 100644
--- a/examples/showindex.c
+++ b/examples/showindex.c
@@ -12,6 +12,8 @@ int main (int argc, char** argv)
 	char out[41];
 	out[40] = '\0';
 
+	git_threads_init();
+
 	if (argc > 1)
 		dir = argv[1];
 	if (!dir || argc > 2) {
@@ -62,6 +64,8 @@ int main (int argc, char** argv)
 	git_index_free(index);
 	git_repository_free(repo);
 
+	git_threads_shutdown();
+
 	return 0;
 }
 
diff --git a/src/filter.c b/src/filter.c
index 62aefb4..3782098 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -593,7 +593,7 @@ int git_filter_list_apply_to_data(
 
 	for (i = 0; i < git_array_size(fl->filters); ++i) {
 		unsigned int di = 1 - si;
-		uint32_t fidx = (fl->source.mode == GIT_FILTER_TO_ODB) ?
+		uint32_t fidx = (fl->source.mode == GIT_FILTER_TO_WORKTREE) ?
 			i : git_array_size(fl->filters) - 1 - i;
 		git_filter_entry *fe = git_array_get(fl->filters, fidx);
 
diff --git a/tests-clar/checkout/crlf.c b/tests-clar/checkout/crlf.c
index 5f5f1b7..4953609 100644
--- a/tests-clar/checkout/crlf.c
+++ b/tests-clar/checkout/crlf.c
@@ -4,6 +4,7 @@
 
 #include "git2/checkout.h"
 #include "repository.h"
+#include "posix.h"
 
 static git_repository *g_repo;
 
@@ -136,3 +137,67 @@ void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
 
 	git_index_free(index);
 }
+
+void test_checkout_crlf__with_ident(void)
+{
+	git_index *index;
+	git_blob *blob;
+	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+	cl_git_mkfile("crlf/.gitattributes",
+		"*.txt text\n*.bin binary\n"
+		"*.crlf text eol=crlf\n"
+		"*.lf text eol=lf\n"
+		"*.ident text ident\n"
+		"*.identcrlf ident text eol=crlf\n"
+		"*.identlf ident text eol=lf\n");
+
+	cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+	/* add files with $Id$ */
+
+	cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
+	cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+	cl_git_pass(git_index_add_bypath(index, "lf.ident"));
+	cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
+	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
+
+	git_checkout_head(g_repo, &opts);
+
+	/* check that blob has $Id$ */
+
+	cl_git_pass(git_blob_lookup(&blob, g_repo,
+		& git_index_get_bypath(index, "lf.ident", 0)->oid));
+	cl_assert_equal_s(
+		ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
+
+	git_blob_free(blob);
+
+	/* check that filesystem is initially untouched - matching core Git */
+
+	cl_assert_equal_file(
+		ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
+
+	/* check that forced checkout rewrites correctly */
+
+	p_unlink("crlf/lf.ident");
+	p_unlink("crlf/crlflf.ident");
+
+	git_checkout_head(g_repo, &opts);
+
+	if (GIT_EOL_NATIVE == GIT_EOL_LF)
+		cl_assert_equal_file(
+			ALL_LF_TEXT_RAW
+			"\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
+			0, "crlf/lf.ident");
+	else
+		cl_assert_equal_file(
+			ALL_LF_TEXT_AS_CRLF
+			"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
+			0, "crlf/lf.ident");
+
+	git_index_free(index);
+}
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 4cf6824..d7e2883 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -455,8 +455,15 @@ void clar__assert_equal_file(
 		if (ignore_cr)
 			bytes = strip_cr_from_buf(buf, bytes);
 
-		clar__assert(memcmp(expected_data, buf, bytes) == 0,
-			file, line, "file content mismatch", path, 1);
+		if (memcmp(expected_data, buf, bytes) != 0) {
+			int pos;
+			for (pos = 0; pos < bytes && expected_data[pos] == buf[pos]; ++pos)
+				/* find differing byte offset */;
+			p_snprintf(
+				buf, sizeof(buf), "file content mismatch at byte %d",
+				(int)(total_bytes + pos));
+			clar__fail(file, line, buf, path, 1);
+		}
 
 		expected_data += bytes;
 		total_bytes   += bytes;