index: Introduce git_index_has_conflicts()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
diff --git a/include/git2/index.h b/include/git2/index.h
index bca9791..1efca72 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -431,6 +431,13 @@ GIT_EXTERN(int) git_index_conflict_remove(git_index *index, const char *path);
*/
GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index);
+/**
+ * Determine if the index contains entries representing file conflicts.
+ *
+ * @return 1 if at least one conflict is found, 0 otherwise.
+ */
+GIT_EXTERN(int) git_index_has_conflicts(git_index *index);
+
/**@}*/
/** @name Resolve Undo (REUC) index entry manipulation.
diff --git a/src/index.c b/src/index.c
index 214d29d..0a1cd03 100644
--- a/src/index.c
+++ b/src/index.c
@@ -959,6 +959,21 @@ void git_index_conflict_cleanup(git_index *index)
git_vector_remove_matching(&index->entries, index_conflicts_match);
}
+int git_index_has_conflicts(git_index *index)
+{
+ unsigned int i;
+ git_index_entry *entry;
+
+ assert(index);
+
+ git_vector_foreach(&index->entries, i, entry) {
+ if (index_entry_stage(entry) > 0)
+ return 1;
+ }
+
+ return 0;
+}
+
unsigned int git_index_reuc_entrycount(git_index *index)
{
assert(index);
diff --git a/tests-clar/index/conflicts.c b/tests-clar/index/conflicts.c
index 59df257..e101b16 100644
--- a/tests-clar/index/conflicts.c
+++ b/tests-clar/index/conflicts.c
@@ -180,8 +180,12 @@ void test_index_conflicts__remove_all_conflicts(void)
cl_assert(git_index_entrycount(repo_index) == 8);
+ cl_assert_equal_i(true, git_index_has_conflicts(repo_index));
+
git_index_conflict_cleanup(repo_index);
+ cl_assert_equal_i(false, git_index_has_conflicts(repo_index));
+
cl_assert(git_index_entrycount(repo_index) == 2);
for (i = 0; i < git_index_entrycount(repo_index); i++) {