Commit 8e5a8ef86f1d528472884f737612083abda86e17

Russell Belfer 2013-11-01T09:51:01

Convert git_index_read to have a "force" flag This is a little more intuitive than the turned-around option that I originally wrote.

diff --git a/include/git2/index.h b/include/git2/index.h
index 7de1066..a60db37 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -225,20 +225,20 @@ GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
  * Update the contents of an existing index object in memory by reading
  * from the hard disk.
  *
- * Pass 0 for `only_if_changed` to perform a "hard" read that discards
- * in-memory changes and always reloads the on-disk index data.  If there
- * is no on-disk version, the index will be cleared.
+ * If `force` is true, this performs a "hard" read that discards in-memory
+ * changes and always reloads the on-disk index data.  If there is no
+ * on-disk version, the index will be cleared.
  *
- * Pass non-zero for `only_if_changed` to perform a "soft" read that only
- * reloads the index data if it has changed since the last time it was
- * loaded.  In-memory index data will be untouched.  Be aware: if there
- * are changes on disk, unwritten in-memory changes will be discarded.
+ * If `force` is false, this does a "soft" read that reloads the index
+ * data from disk only if it has changed since the last time it was
+ * loaded.  Purely in-memory index data will be untouched.  Be aware: if
+ * there are changes on disk, unwritten in-memory changes are discarded.
  *
  * @param index an existing index object
- * @param only_if_changed only read if on-disk file is newer than last read
+ * @param force if true, always reload, vs. only read if file has changed
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_index_read(git_index *index, int only_if_changed);
+GIT_EXTERN(int) git_index_read(git_index *index, int force);
 
 /**
  * Write an existing index object from memory back to disk
diff --git a/src/checkout.c b/src/checkout.c
index 94968e3..dce9afd 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1837,7 +1837,7 @@ static int checkout_data_init(
 		} else {
 			/* otherwise, grab and reload the index */
 			if ((error = git_repository_index(&data->index, data->repo)) < 0 ||
-				(error = git_index_read(data->index, false)) < 0)
+				(error = git_index_read(data->index, true)) < 0)
 				goto cleanup;
 
 			/* cannot checkout if unresolved conflicts exist */
diff --git a/src/diff.c b/src/diff.c
index d1ff04b..5c89b6e 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1189,7 +1189,7 @@ static int diff_load_index(git_index **index, git_repository *repo)
 	int error = git_repository_index__weakptr(index, repo);
 
 	/* reload the repository index when user did not pass one in */
-	if (!error && git_index_read(*index, true) < 0)
+	if (!error && git_index_read(*index, false) < 0)
 		giterr_clear();
 
 	return error;
diff --git a/src/index.c b/src/index.c
index 19de43d..dbf1ab5 100644
--- a/src/index.c
+++ b/src/index.c
@@ -349,7 +349,7 @@ int git_index_open(git_index **index_out, const char *index_path)
 	*index_out = index;
 	GIT_REFCOUNT_INC(index);
 
-	return (index_path != NULL) ? git_index_read(index, false) : 0;
+	return (index_path != NULL) ? git_index_read(index, true) : 0;
 }
 
 int git_index_new(git_index **out)
@@ -451,7 +451,7 @@ unsigned int git_index_caps(const git_index *index)
 			(index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0));
 }
 
-int git_index_read(git_index *index, int only_if_changed)
+int git_index_read(git_index *index, int force)
 {
 	int error = 0, updated;
 	git_buf buffer = GIT_BUF_INIT;
@@ -464,13 +464,13 @@ int git_index_read(git_index *index, int only_if_changed)
 	index->on_disk = git_path_exists(index->index_file_path);
 
 	if (!index->on_disk) {
-		if (!only_if_changed)
+		if (force)
 			git_index_clear(index);
 		return 0;
 	}
 
 	updated = git_futils_filestamp_check(&stamp, index->index_file_path);
-	if (updated < 0 || (only_if_changed && !updated))
+	if (updated < 0 || (!updated && !force))
 		return updated;
 
 	error = git_futils_readbuffer(&buffer, index->index_file_path);
diff --git a/src/status.c b/src/status.c
index 74bccf7..07fdcb5 100644
--- a/src/status.c
+++ b/src/status.c
@@ -259,7 +259,7 @@ int git_status_list_new(
 
 	/* refresh index from disk unless prevented */
 	if ((flags & GIT_STATUS_OPT_NO_REFRESH) == 0 &&
-		git_index_read(index, true) < 0)
+		git_index_read(index, false) < 0)
 		giterr_clear();
 
 	status = git_status_list_alloc(index);
diff --git a/tests-clar/index/names.c b/tests-clar/index/names.c
index 9a86b14..9007b1b 100644
--- a/tests-clar/index/names.c
+++ b/tests-clar/index/names.c
@@ -63,7 +63,7 @@ void test_index_names__roundtrip(void)
 	git_index_clear(repo_index);
 	cl_assert(git_index_name_entrycount(repo_index) == 0);
 
-	cl_git_pass(git_index_read(repo_index, 0));
+	cl_git_pass(git_index_read(repo_index, true));
 	cl_assert(git_index_name_entrycount(repo_index) == 3);
 
 	conflict_name = git_index_name_get_byindex(repo_index, 0);
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c
index f538bc2..09b05bf 100644
--- a/tests-clar/index/tests.c
+++ b/tests-clar/index/tests.c
@@ -8,7 +8,7 @@ static const size_t index_entry_count_2 = 1437;
 #define TEST_INDEXBIG_PATH cl_fixture("big.index")
 
 
-// Suite data
+/* Suite data */
 struct test_entry {
    size_t index;
    char path[128];
@@ -24,7 +24,7 @@ static struct test_entry test_entries[] = {
    {48, "src/revobject.h", 1448, 0x4C3F7FE2}
 };
 
-// Helpers
+/* Helpers */
 static void copy_file(const char *src, const char *dst)
 {
 	git_buf source_buf = GIT_BUF_INIT;
@@ -32,7 +32,7 @@ static void copy_file(const char *src, const char *dst)
 
 	cl_git_pass(git_futils_readbuffer(&source_buf, src));
 
-	dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536
+	dst_fd = git_futils_creat_withpath(dst, 0777, 0666); /* -V536 */
 	if (dst_fd < 0)
 		goto cleanup;
 
@@ -66,7 +66,7 @@ static void files_are_equal(const char *a, const char *b)
 }
 
 
-// Fixture setup and teardown
+/* Fixture setup and teardown */
 void test_index_tests__initialize(void)
 {
 }
@@ -173,7 +173,8 @@ void test_index_tests__write(void)
 
 void test_index_tests__sort0(void)
 {
-   // sort the entires in an index
+	/* sort the entires in an index */
+
    /*
    * TODO: This no longer applies:
    * index sorting in Git uses some specific changes to the way
@@ -187,7 +188,7 @@ void test_index_tests__sort0(void)
 
 void test_index_tests__sort1(void)
 {
-   // sort the entires in an empty index
+   /* sort the entires in an empty index */
    git_index *index;
 
    cl_git_pass(git_index_open(&index, "fake-index"));
@@ -349,14 +350,14 @@ void test_index_tests__remove_entry(void)
 	cl_git_pass(git_index_add_bypath(index, "hello"));
 	cl_git_pass(git_index_write(index));
 
-	cl_git_pass(git_index_read(index, false)); /* reload */
+	cl_git_pass(git_index_read(index, true)); /* reload */
 	cl_assert(git_index_entrycount(index) == 1);
 	cl_assert(git_index_get_bypath(index, "hello", 0) != NULL);
 
 	cl_git_pass(git_index_remove(index, "hello", 0));
 	cl_git_pass(git_index_write(index));
 
-	cl_git_pass(git_index_read(index, false)); /* reload */
+	cl_git_pass(git_index_read(index, true)); /* reload */
 	cl_assert(git_index_entrycount(index) == 0);
 	cl_assert(git_index_get_bypath(index, "hello", 0) == NULL);
 
@@ -388,7 +389,7 @@ void test_index_tests__remove_directory(void)
 	cl_git_pass(git_index_add_bypath(index, "b.txt"));
 	cl_git_pass(git_index_write(index));
 
-	cl_git_pass(git_index_read(index, false)); /* reload */
+	cl_git_pass(git_index_read(index, true)); /* reload */
 	cl_assert_equal_i(4, (int)git_index_entrycount(index));
 	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL);
 	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
@@ -397,7 +398,7 @@ void test_index_tests__remove_directory(void)
 	cl_git_pass(git_index_remove(index, "a/1.txt", 0));
 	cl_git_pass(git_index_write(index));
 
-	cl_git_pass(git_index_read(index, false)); /* reload */
+	cl_git_pass(git_index_read(index, true)); /* reload */
 	cl_assert_equal_i(3, (int)git_index_entrycount(index));
 	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
 	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
@@ -406,7 +407,7 @@ void test_index_tests__remove_directory(void)
 	cl_git_pass(git_index_remove_directory(index, "a", 0));
 	cl_git_pass(git_index_write(index));
 
-	cl_git_pass(git_index_read(index, false)); /* reload */
+	cl_git_pass(git_index_read(index, true)); /* reload */
 	cl_assert_equal_i(1, (int)git_index_entrycount(index));
 	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
 	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL);
@@ -517,7 +518,7 @@ void test_index_tests__reload_from_disk(void)
 	/* Sync the changes back into the read_index */
 	cl_assert_equal_sz(0, git_index_entrycount(read_index));
 
-	cl_git_pass(git_index_read(read_index, false));
+	cl_git_pass(git_index_read(read_index, true));
 	cl_assert_equal_i(true, read_index->on_disk);
 
 	cl_assert_equal_sz(2, git_index_entrycount(read_index));
@@ -526,7 +527,7 @@ void test_index_tests__reload_from_disk(void)
 	cl_git_pass(p_unlink(write_index->index_file_path));
 
 	/* Sync the changes back into the read_index */
-	cl_git_pass(git_index_read(read_index, false));
+	cl_git_pass(git_index_read(read_index, true));
 	cl_assert_equal_i(false, read_index->on_disk);
 	cl_assert_equal_sz(0, git_index_entrycount(read_index));