Commit 4a1b40159b4b0b2a954d40e4489331e3f22c8994

Vicent Martí 2013-09-24T10:32:40

Merge pull request #1865 from arrbee/various-cleanups Various warning cleanup and minor fixes

diff --git a/Makefile.embed b/Makefile.embed
index 2f3b057..eb8a78e 100644
--- a/Makefile.embed
+++ b/Makefile.embed
@@ -31,7 +31,7 @@ CC:=$(PREFIX)$(CC)
 INCLUDES= -I. -Isrc -Iinclude -Ideps/http-parser -Ideps/zlib
 
 DEFINES= $(INCLUDES) -DNO_VIZ -DSTDC -DNO_GZIP -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(EXTRA_DEFINES)
-CFLAGS= -g $(DEFINES) -Wall -Wextra -O2 $(EXTRA_CFLAGS)
+CFLAGS= -g $(DEFINES) -Wall -Wextra -Wno-missing-field-initializers -O2 $(EXTRA_CFLAGS)
 
 SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/xdiff/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c) src/hash/hash_generic.c
 
diff --git a/include/git2/index.h b/include/git2/index.h
index ba0bcc9..131d049 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -120,9 +120,9 @@ typedef struct git_index_entry {
 
 /** Capabilities of system that affect index actions. */
 typedef enum {
-	GIT_INDEXCAP_IGNORE_CASE = 1,
-	GIT_INDEXCAP_NO_FILEMODE = 2,
-	GIT_INDEXCAP_NO_SYMLINKS = 4,
+	GIT_INDEXCAP_IGNORE_CASE = 1u,
+	GIT_INDEXCAP_NO_FILEMODE = 2u,
+	GIT_INDEXCAP_NO_SYMLINKS = 4u,
 	GIT_INDEXCAP_FROM_OWNER  = ~0u
 } git_indexcap_t;
 
@@ -219,7 +219,7 @@ GIT_EXTERN(unsigned int) git_index_caps(const git_index *index);
  * @param caps A combination of GIT_INDEXCAP values
  * @return 0 on success, -1 on failure
  */
-GIT_EXTERN(int) git_index_set_caps(git_index *index, int caps);
+GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
 
 /**
  * Update the contents of an existing index object in memory
diff --git a/src/buffer.c b/src/buffer.c
index 1371607..2068232 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -211,6 +211,8 @@ int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
 			format, args
 		);
 
+		va_end(args);
+
 		if (len < 0) {
 			git__free(buf->ptr);
 			buf->ptr = git_buf__oom;
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/src/diff_print.c b/src/diff_print.c
index ee4b5fc..fd18b67 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -336,7 +336,7 @@ static int diff_print_patch_hunk(
 		return 0;
 
 	git_buf_clear(pi->buf);
-	if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0)
+	if (git_buf_put(pi->buf, header, header_len) < 0)
 		return -1;
 
 	if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR,
@@ -360,13 +360,14 @@ static int diff_print_patch_line(
 		return 0;
 
 	git_buf_clear(pi->buf);
+	git_buf_grow(pi->buf, content_len + 2);
 
 	if (line_origin == GIT_DIFF_LINE_ADDITION ||
 		line_origin == GIT_DIFF_LINE_DELETION ||
 		line_origin == GIT_DIFF_LINE_CONTEXT)
-		git_buf_printf(pi->buf, "%c%.*s", line_origin, (int)content_len, content);
-	else if (content_len > 0)
-		git_buf_printf(pi->buf, "%.*s", (int)content_len, content);
+		git_buf_putc(pi->buf, line_origin);
+
+	git_buf_put(pi->buf, content, content_len);
 
 	if (git_buf_oom(pi->buf))
 		return -1;
diff --git a/src/filter.c b/src/filter.c
index 503f185..9f866fe 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -657,9 +657,17 @@ int git_filter_list_apply_to_blob(
 	git_filter_list *filters,
 	git_blob *blob)
 {
-	git_buf in = {
-		(char *)git_blob_rawcontent(blob), 0, git_blob_rawsize(blob)
-	};
+	git_buf in = GIT_BUF_INIT;
+	git_off_t rawsize = git_blob_rawsize(blob);
+
+	if (!git__is_sizet(rawsize)) {
+		giterr_set(GITERR_OS, "Blob is too large to filter");
+		return -1;
+	}
+
+	in.ptr   = (char *)git_blob_rawcontent(blob);
+	in.asize = 0;
+	in.size  = (size_t)rawsize;
 
 	if (filters)
 		git_oid_cpy(&filters->source.oid, git_blob_id(blob));
diff --git a/src/index.c b/src/index.c
index b4f2a3b..21a8d31 100644
--- a/src/index.c
+++ b/src/index.c
@@ -408,7 +408,7 @@ static int create_index_error(int error, const char *msg)
 	return error;
 }
 
-int git_index_set_caps(git_index *index, int caps)
+int git_index_set_caps(git_index *index, unsigned int caps)
 {
 	unsigned int old_ignore_case;
 
diff --git a/src/repository.c b/src/repository.c
index 6dea487..0d7c094 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1145,17 +1145,19 @@ static int repo_init_structure(
 		}
 
 		if (!tdir) {
-			if ((error = git_futils_find_template_dir(&template_buf)) >= 0);
+			if (!(error = git_futils_find_template_dir(&template_buf)))
 				tdir = template_buf.ptr;
 			default_template = true;
 		}
 
-		error = git_futils_cp_r(tdir, repo_dir,
-			GIT_CPDIR_COPY_SYMLINKS | GIT_CPDIR_CHMOD_DIRS |
-			GIT_CPDIR_SIMPLE_TO_MODE, dmode);
+		if (tdir)
+			error = git_futils_cp_r(tdir, repo_dir,
+				GIT_CPDIR_COPY_SYMLINKS | GIT_CPDIR_CHMOD_DIRS |
+				GIT_CPDIR_SIMPLE_TO_MODE, dmode);
 
 		git_buf_free(&template_buf);
 		git_config_free(cfg);
+
 		if (error < 0) {
 			if (!default_template)
 				return error;
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/core/filebuf.c b/tests-clar/core/filebuf.c
index 4451c01..bf21670 100644
--- a/tests-clar/core/filebuf.c
+++ b/tests-clar/core/filebuf.c
@@ -24,18 +24,16 @@ void test_core_filebuf__0(void)
 void test_core_filebuf__1(void)
 {
 	git_filebuf file = GIT_FILEBUF_INIT;
-	int fd;
 	char test[] = "test";
 
-	fd = p_creat(test, 0666); //-V536
-	cl_must_pass(fd);
-	cl_must_pass(p_write(fd, "libgit2 rocks\n", 14));
-	cl_must_pass(p_close(fd));
+	cl_git_mkfile(test, "libgit2 rocks\n");
 
 	cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND));
 	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
 	cl_git_pass(git_filebuf_commit(&file, 0666));
 
+	cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test);
+
 	cl_must_pass(p_unlink(test));
 }
 
@@ -53,6 +51,8 @@ void test_core_filebuf__2(void)
 	cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf)));
 	cl_git_pass(git_filebuf_commit(&file, 0666));
 
+	cl_assert_equal_file((char *)buf, sizeof(buf), test);
+
 	cl_must_pass(p_unlink(test));
 }
 
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);
 
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index caa211e..392be20 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -382,7 +382,7 @@ static void assert_hooks_match(
 	cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path));
 	cl_git_pass(git_path_lstat(actual.ptr, &st));
 
-	cl_assert_equal_sz(expected_st.st_size, st.st_size);
+	cl_assert(expected_st.st_size == st.st_size);
 
 	if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) {
 		mode_t expected_mode =