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.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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++;
}