Commit 9f545b9d71c7bd316be80e5fe8b47135e9deb97e

Edward Thomson 2015-05-19T11:23:59

introduce `git_index_entry_is_conflict` It's not always obvious the mapping between stage level and conflict-ness. More importantly, this can lead otherwise sane people to write constructs like `if (!git_index_entry_stage(entry))`, which (while technically correct) is unreadable. Provide a nice method to help avoid such messy thinking.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 03e5273..cdd1a5d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -108,6 +108,10 @@ support for HTTPS connections insead of OpenSSL.
   `GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file
   in the index.
 
+* `git_index_entry_is_conflict()` is a utility function to determine if
+  a given index entry has a non-zero stage entry, indicating that it is
+  one side of a conflict.
+
 ### API removals
 
 * `git_remote_save()` and `git_remote_clear_refspecs()` has been
diff --git a/include/git2/index.h b/include/git2/index.h
index 1e1d5e7..49bbe16 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -430,6 +430,15 @@ GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_en
  */
 GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
 
+/**
+ * Return whether the given index entry is a conflict (has a high stage
+ * entry).  This is simply shorthand for `git_index_entry_stage > 0`.
+ *
+ * @param entry The entry
+ * @return 1 if the entry is a conflict entry, 0 otherwise
+ */
+GIT_EXTERN(int) git_index_entry_is_conflict(const git_index_entry *entry);
+
 /**@}*/
 
 /** @name Workdir Index Entry Functions
diff --git a/src/diff.c b/src/diff.c
index abe0de0..46f3396 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -180,14 +180,14 @@ static int diff_delta__from_two(
 	GITERR_CHECK_ALLOC(delta);
 	delta->nfiles = 2;
 
-	if (!git_index_entry_stage(old_entry)) {
+	if (!git_index_entry_is_conflict(old_entry)) {
 		delta->old_file.size = old_entry->file_size;
 		delta->old_file.mode = old_mode;
 		git_oid_cpy(&delta->old_file.id, old_id);
 		delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
 	}
 
-	if (!git_index_entry_stage(new_entry)) {
+	if (!git_index_entry_is_conflict(new_entry)) {
 		git_oid_cpy(&delta->new_file.id, new_id);
 		delta->new_file.size = new_entry->file_size;
 		delta->new_file.mode = new_mode;
@@ -765,7 +765,8 @@ static int maybe_modified(
 		nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
 
 	/* if one side is a conflict, mark the whole delta as conflicted */
-	if (git_index_entry_stage(oitem) > 0 || git_index_entry_stage(nitem) > 0)
+	if (git_index_entry_is_conflict(oitem) ||
+		git_index_entry_is_conflict(nitem))
 		status = GIT_DELTA_CONFLICTED;
 
 	/* support "assume unchanged" (poorly, b/c we still stat everything) */
@@ -923,8 +924,8 @@ static int iterator_advance(
 	 */
 	while ((error = git_iterator_advance(entry, iterator)) == 0) {
 		if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
-			git_index_entry_stage(prev_entry) == 0 ||
-			git_index_entry_stage(*entry) == 0)
+			!git_index_entry_is_conflict(prev_entry) ||
+			!git_index_entry_is_conflict(*entry))
 			break;
 
 		cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
@@ -985,7 +986,7 @@ static int handle_unmatched_new_item(
 	contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
 
 	/* update delta_type if this item is conflicted */
-	if (git_index_entry_stage(nitem))
+	if (git_index_entry_is_conflict(nitem))
 		delta_type = GIT_DELTA_CONFLICTED;
 
 	/* update delta_type if this item is ignored */
@@ -1133,7 +1134,7 @@ static int handle_unmatched_old_item(
 	int error;
 
 	/* update delta_type if this item is conflicted */
-	if (git_index_entry_stage(info->oitem))
+	if (git_index_entry_is_conflict(info->oitem))
 		delta_type = GIT_DELTA_CONFLICTED;
 
 	if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
diff --git a/src/index.c b/src/index.c
index e37cb1f..bd65d92 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1537,7 +1537,7 @@ int git_index_conflict_next(
 	while (iterator->cur < iterator->index->entries.length) {
 		entry = git_index_get_byindex(iterator->index, iterator->cur);
 
-		if (git_index_entry_stage(entry) > 0) {
+		if (git_index_entry_is_conflict(entry)) {
 			if ((len = index_conflict__get_byindex(
 				ancestor_out,
 				our_out,
@@ -2383,6 +2383,11 @@ int git_index_entry_stage(const git_index_entry *entry)
 	return GIT_IDXENTRY_STAGE(entry);
 }
 
+int git_index_entry_is_conflict(const git_index_entry *entry)
+{
+	return (GIT_IDXENTRY_STAGE(entry) > 0);
+}
+
 typedef struct read_tree_data {
 	git_index *index;
 	git_vector *old_entries;
diff --git a/src/iterator.c b/src/iterator.c
index 1e946fa..93303a8 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -674,7 +674,7 @@ static const git_index_entry *index_iterator__advance_over_conflicts(index_itera
 	const git_index_entry *ie = index_iterator__index_entry(ii);
 
 	if (!iterator__include_conflicts(ii)) {
-		while (ie && git_index_entry_stage(ie) != 0) {
+		while (ie && git_index_entry_is_conflict(ie)) {
 			ii->current++;
 			ie = index_iterator__index_entry(ii);
 		}
diff --git a/src/merge.c b/src/merge.c
index 5e77274..eaf7eee 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2492,7 +2492,7 @@ int git_merge__check_result(git_repository *repo, git_index *index_new)
 	for (i = 0; i < git_index_entrycount(index_new); i++) {
 		e = git_index_get_byindex(index_new, i);
 
-		if (git_index_entry_stage(e) != 0 &&
+		if (git_index_entry_is_conflict(e) &&
 			(git_vector_last(&paths) == NULL ||
 			strcmp(git_vector_last(&paths), e->path) != 0)) {
 
@@ -2544,7 +2544,7 @@ int git_merge__append_conflicts_to_merge_msg(
 	for (i = 0; i < git_index_entrycount(index); i++) {
 		const git_index_entry *e = git_index_get_byindex(index, i);
 
-		if (git_index_entry_stage(e) == 0)
+		if (!git_index_entry_is_conflict(e))
 			continue;
 
 		if (last == NULL || strcmp(e->path, last) != 0)
diff --git a/tests/index/conflicts.c b/tests/index/conflicts.c
index 65f8ed5..b7a2456 100644
--- a/tests/index/conflicts.c
+++ b/tests/index/conflicts.c
@@ -272,7 +272,7 @@ void test_index_conflicts__moved_to_reuc_on_add(void)
 		cl_assert(entry = git_index_get_byindex(repo_index, i));
 
 		if (strcmp(entry->path, "conflicts-one.txt") == 0)
-			cl_assert(git_index_entry_stage(entry) == 0);
+			cl_assert(!git_index_entry_is_conflict(entry));
 	}
 }
 
@@ -312,7 +312,7 @@ void test_index_conflicts__remove_all_conflicts(void)
 
 	for (i = 0; i < git_index_entrycount(repo_index); i++) {
 		cl_assert(entry = git_index_get_byindex(repo_index, i));
-		cl_assert(git_index_entry_stage(entry) == 0);
+		cl_assert(!git_index_entry_is_conflict(entry));
 	}
 }
 
diff --git a/tests/merge/trees/trivial.c b/tests/merge/trees/trivial.c
index 55f3824..2262edd 100644
--- a/tests/merge/trees/trivial.c
+++ b/tests/merge/trees/trivial.c
@@ -71,7 +71,7 @@ static int merge_trivial_conflict_entrycount(git_index *index)
 	for (i = 0; i < git_index_entrycount(index); i++) {
 		cl_assert(entry = git_index_get_byindex(index, i));
 
-		if (git_index_entry_stage(entry) > 0)
+		if (git_index_entry_is_conflict(entry))
 			count++;
 	}
 
diff --git a/tests/merge/workdir/trivial.c b/tests/merge/workdir/trivial.c
index 5cc20f7..4ddaf23 100644
--- a/tests/merge/workdir/trivial.c
+++ b/tests/merge/workdir/trivial.c
@@ -66,7 +66,7 @@ static size_t merge_trivial_conflict_entrycount(void)
 	for (i = 0; i < git_index_entrycount(repo_index); i++) {
 		cl_assert(entry = git_index_get_byindex(repo_index, i));
 
-		if (git_index_entry_stage(entry) > 0)
+		if (git_index_entry_is_conflict(entry))
 			count++;
 	}