Commit 1aa1b09e73371891ef4327fb123e66d675f615e5

Vicent Martí 2011-06-15T12:11:59

Merge pull request #260 from nulltoken/fix/git_index_add Fix git_index_add()

diff --git a/src/blob.c b/src/blob.c
index c95d018..ceb2c9c 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -119,9 +119,9 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat
 		ssize_t read_len;
 
 		if (!islnk)
-			read_len = gitfo_read(fd, buffer, sizeof(buffer));
+			read_len = gitfo_read(fd, buffer, (size_t)(size < sizeof(buffer) ? size : sizeof(buffer)));
 		else
-			read_len = gitfo_readlink(full_path, buffer, sizeof(buffer));
+			read_len = gitfo_readlink(full_path, buffer, (size_t)size);
 
 		if (read_len < 0) {
 			if (!islnk)
diff --git a/src/fileops.c b/src/fileops.c
index 2a78764..2136c91 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -97,7 +97,7 @@ int gitfo_read(git_file fd, void *buf, size_t cnt)
 		cnt -= r;
 		b += r;
 	}
-	return GIT_SUCCESS;
+	return (int)(b - (char *)buf);
 }
 
 int gitfo_write(git_file fd, void *buf, size_t cnt)
diff --git a/tests/t06-index.c b/tests/t06-index.c
index 25adbf7..621deab 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -161,7 +161,6 @@ BEGIN_TEST(sort0, "sort the entires in an index")
 	 */
 END_TEST
 
-
 BEGIN_TEST(sort1, "sort the entires in an empty index")
 	git_index *index;
 
@@ -173,6 +172,46 @@ BEGIN_TEST(sort1, "sort the entires in an empty index")
 	git_index_free(index);
 END_TEST
 
+BEGIN_TEST(add0, "add a new file to the index")
+	git_index *index;
+	git_filebuf file;
+	git_repository *repo;
+	git_index_entry *entry;
+	git_oid id1;
+
+	/* Intialize a new repository */
+	must_pass(git_repository_init(&repo, TEMP_REPO_FOLDER "myrepo", 0));
+
+	/* Ensure we're the only guy in the room */
+	must_pass(git_repository_index(&index, repo));
+	must_pass(git_index_entrycount(index) == 0);
+
+	/* Create a new file in the working directory */
+	must_pass(gitfo_mkdir_2file(TEMP_REPO_FOLDER "myrepo/test.txt"));
+	must_pass(git_filebuf_open(&file, TEMP_REPO_FOLDER "myrepo/test.txt", 0));
+	must_pass(git_filebuf_write(&file, "hey there\n", 10));
+	must_pass(git_filebuf_commit(&file));
+
+	/* Store the expected hash of the file/blob
+	 * This has been generated by executing the following
+	 * $ echo "hey there" | git hash-object --stdin
+	 */
+	must_pass(git_oid_mkstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
+
+	/* Add the new file to the index */
+	must_pass(git_index_add(index, "test.txt", 0));
+
+	/* Wow... it worked! */
+	must_pass(git_index_entrycount(index) == 1);
+	entry = git_index_get(index, 0);
+
+	/* And the built-in hashing mechanism worked as expected */
+    must_be_true(git_oid_cmp(&id1, &entry->oid) == 0);
+
+	git_repository_free(repo);
+	rmdir_recurs(TEMP_REPO_FOLDER);
+END_TEST
+
 BEGIN_SUITE(index)
 	ADD_TEST(read0);
 	ADD_TEST(read1);
@@ -185,4 +224,6 @@ BEGIN_SUITE(index)
 
 	ADD_TEST(sort0);
 	ADD_TEST(sort1);
+
+	ADD_TEST(add0);
 END_SUITE