Commit 634f10f69090b325a50c8bca6cb303397576305e

Russell Belfer 2013-09-24T10:11:20

Fix incorrect return code in crlf filter The git_buf_text_gather_stats call returns a boolean indicating if the file looks like binary data. That shouldn't be an error; it should be used to skip CRLF processing though.

diff --git a/src/crlf.c b/src/crlf.c
index b4eda26..b25c02c 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -133,9 +133,9 @@ static int crlf_apply_to_odb(
 	if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
 		git_buf_text_stats stats;
 
-		/* Check heuristics for binary vs text... */
+		/* Check heuristics for binary vs text - returns true if binary */
 		if (git_buf_text_gather_stats(&stats, from, false))
-			return -1;
+			return GIT_PASSTHROUGH;
 
 		/*
 		 * We're currently not going to even try to convert stuff
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index d7e2883..555af38 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -30,24 +30,26 @@ void cl_git_mkfile(const char *filename, const char *content)
 }
 
 void cl_git_write2file(
-	const char *filename, const char *new_content, int flags, unsigned int mode)
+	const char *path, const char *content, size_t content_len,
+	int flags, unsigned int mode)
 {
-	int fd = p_open(filename, flags, mode);
-	cl_assert(fd >= 0);
-	if (!new_content)
-		new_content = "\n";
-	cl_must_pass(p_write(fd, new_content, strlen(new_content)));
+	int fd;
+	cl_assert(path && content);
+	cl_assert((fd = p_open(path, flags, mode)) >= 0);
+	if (!content_len)
+		content_len = strlen(content);
+	cl_must_pass(p_write(fd, content, content_len));
 	cl_must_pass(p_close(fd));
 }
 
-void cl_git_append2file(const char *filename, const char *new_content)
+void cl_git_append2file(const char *path, const char *content)
 {
-	cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND, 0644);
+	cl_git_write2file(path, content, 0, O_WRONLY | O_CREAT | O_APPEND, 0644);
 }
 
-void cl_git_rewritefile(const char *filename, const char *new_content)
+void cl_git_rewritefile(const char *path, const char *content)
 {
-	cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+	cl_git_write2file(path, content, 0, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 }
 
 #ifdef GIT_WIN32
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index c37306b..9d4d63e 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -78,7 +78,8 @@ void clar__assert_equal_file(
 void cl_git_mkfile(const char *filename, const char *content);
 void cl_git_append2file(const char *filename, const char *new_content);
 void cl_git_rewritefile(const char *filename, const char *new_content);
-void cl_git_write2file(const char *filename, const char *new_content, int flags, unsigned int mode);
+void cl_git_write2file(const char *path, const char *data,
+	size_t datalen, int flags, unsigned int mode);
 
 bool cl_toggle_filemode(const char *filename);
 bool cl_is_chmod_supported(void);
diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c
index c7fac1e..6c17b41 100644
--- a/tests-clar/diff/workdir.c
+++ b/tests-clar/diff/workdir.c
@@ -1266,3 +1266,28 @@ void test_diff_workdir__untracked_directory_comes_last(void)
 
 	git_diff_list_free(diff);
 }
+
+void test_diff_workdir__untracked_with_bom(void)
+{
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	git_diff_list *diff = NULL;
+	const git_diff_delta *delta;
+
+	g_repo = cl_git_sandbox_init("empty_standard_repo");
+	cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+	cl_git_write2file("empty_standard_repo/bom.txt",
+		"\xFF\xFE\x31\x00\x32\x00\x33\x00\x34\x00", 10, O_WRONLY|O_CREAT, 0664);
+
+	opts.flags =
+		GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_UNTRACKED_CONTENT;
+
+	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
+
+	cl_assert_equal_i(1, git_diff_num_deltas(diff));
+	cl_git_pass(git_diff_get_patch(NULL, &delta, diff, 0));
+	cl_assert_equal_i(GIT_DELTA_UNTRACKED, delta->status);
+	cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
+
+	git_diff_list_free(diff);
+}
diff --git a/tests-clar/index/filemodes.c b/tests-clar/index/filemodes.c
index e56a9c0..02f83ef 100644
--- a/tests-clar/index/filemodes.c
+++ b/tests-clar/index/filemodes.c
@@ -44,7 +44,8 @@ static void replace_file_with_mode(
 
 	cl_git_pass(p_rename(path.ptr, backup));
 	cl_git_write2file(
-		path.ptr, content.ptr, O_WRONLY|O_CREAT|O_TRUNC, create_mode);
+		path.ptr, content.ptr, content.size,
+		O_WRONLY|O_CREAT|O_TRUNC, create_mode);
 
 	git_buf_free(&path);
 	git_buf_free(&content);
@@ -91,7 +92,7 @@ void test_index_filemodes__untrusted(void)
 	add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
 
 	/*  5 - add new 0644 -> expect 0644 */
-	cl_git_write2file("filemodes/new_off", "blah",
+	cl_git_write2file("filemodes/new_off", "blah", 0,
 		O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
 
@@ -100,7 +101,7 @@ void test_index_filemodes__untrusted(void)
 	 */
 	if (can_filemode) {
 		/* 6 - add 0755 -> expect 0755 */
-		cl_git_write2file("filemodes/new_on", "blah",
+		cl_git_write2file("filemodes/new_on", "blah", 0,
 			O_WRONLY | O_CREAT | O_TRUNC, 0755);
 		add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);
 	}
@@ -140,12 +141,12 @@ void test_index_filemodes__trusted(void)
 	add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
 
 	/*  5 - add new 0644 -> expect 0644 */
-	cl_git_write2file("filemodes/new_off", "blah",
+	cl_git_write2file("filemodes/new_off", "blah", 0,
 		O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
 
 	/* 6 - add 0755 -> expect 0755 */
-	cl_git_write2file("filemodes/new_on", "blah",
+	cl_git_write2file("filemodes/new_on", "blah", 0,
 		O_WRONLY | O_CREAT | O_TRUNC, 0755);
 	add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);