Commit 9f545b9d by Edward Thomson

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.
parent 2f1080ea
...@@ -108,6 +108,10 @@ support for HTTPS connections insead of OpenSSL. ...@@ -108,6 +108,10 @@ support for HTTPS connections insead of OpenSSL.
`GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file `GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file
in the index. 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 ### API removals
* `git_remote_save()` and `git_remote_clear_refspecs()` has been * `git_remote_save()` and `git_remote_clear_refspecs()` has been
......
...@@ -430,6 +430,15 @@ GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_en ...@@ -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); 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 /** @name Workdir Index Entry Functions
......
...@@ -180,14 +180,14 @@ static int diff_delta__from_two( ...@@ -180,14 +180,14 @@ static int diff_delta__from_two(
GITERR_CHECK_ALLOC(delta); GITERR_CHECK_ALLOC(delta);
delta->nfiles = 2; 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.size = old_entry->file_size;
delta->old_file.mode = old_mode; delta->old_file.mode = old_mode;
git_oid_cpy(&delta->old_file.id, old_id); git_oid_cpy(&delta->old_file.id, old_id);
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_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); git_oid_cpy(&delta->new_file.id, new_id);
delta->new_file.size = new_entry->file_size; delta->new_file.size = new_entry->file_size;
delta->new_file.mode = new_mode; delta->new_file.mode = new_mode;
...@@ -765,7 +765,8 @@ static int maybe_modified( ...@@ -765,7 +765,8 @@ static int maybe_modified(
nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK); nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
/* if one side is a conflict, mark the whole delta as conflicted */ /* 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; status = GIT_DELTA_CONFLICTED;
/* support "assume unchanged" (poorly, b/c we still stat everything) */ /* support "assume unchanged" (poorly, b/c we still stat everything) */
...@@ -923,8 +924,8 @@ static int iterator_advance( ...@@ -923,8 +924,8 @@ static int iterator_advance(
*/ */
while ((error = git_iterator_advance(entry, iterator)) == 0) { while ((error = git_iterator_advance(entry, iterator)) == 0) {
if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) || if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
git_index_entry_stage(prev_entry) == 0 || !git_index_entry_is_conflict(prev_entry) ||
git_index_entry_stage(*entry) == 0) !git_index_entry_is_conflict(*entry))
break; break;
cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ? cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
...@@ -985,7 +986,7 @@ static int handle_unmatched_new_item( ...@@ -985,7 +986,7 @@ static int handle_unmatched_new_item(
contains_oitem = entry_is_prefixed(diff, info->oitem, nitem); contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
/* update delta_type if this item is conflicted */ /* 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; delta_type = GIT_DELTA_CONFLICTED;
/* update delta_type if this item is ignored */ /* update delta_type if this item is ignored */
...@@ -1133,7 +1134,7 @@ static int handle_unmatched_old_item( ...@@ -1133,7 +1134,7 @@ static int handle_unmatched_old_item(
int error; int error;
/* update delta_type if this item is conflicted */ /* 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; delta_type = GIT_DELTA_CONFLICTED;
if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0) if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
......
...@@ -1537,7 +1537,7 @@ int git_index_conflict_next( ...@@ -1537,7 +1537,7 @@ int git_index_conflict_next(
while (iterator->cur < iterator->index->entries.length) { while (iterator->cur < iterator->index->entries.length) {
entry = git_index_get_byindex(iterator->index, iterator->cur); 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( if ((len = index_conflict__get_byindex(
ancestor_out, ancestor_out,
our_out, our_out,
...@@ -2383,6 +2383,11 @@ int git_index_entry_stage(const git_index_entry *entry) ...@@ -2383,6 +2383,11 @@ int git_index_entry_stage(const git_index_entry *entry)
return GIT_IDXENTRY_STAGE(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 { typedef struct read_tree_data {
git_index *index; git_index *index;
git_vector *old_entries; git_vector *old_entries;
......
...@@ -674,7 +674,7 @@ static const git_index_entry *index_iterator__advance_over_conflicts(index_itera ...@@ -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); const git_index_entry *ie = index_iterator__index_entry(ii);
if (!iterator__include_conflicts(ii)) { if (!iterator__include_conflicts(ii)) {
while (ie && git_index_entry_stage(ie) != 0) { while (ie && git_index_entry_is_conflict(ie)) {
ii->current++; ii->current++;
ie = index_iterator__index_entry(ii); ie = index_iterator__index_entry(ii);
} }
......
...@@ -2492,7 +2492,7 @@ int git_merge__check_result(git_repository *repo, git_index *index_new) ...@@ -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++) { for (i = 0; i < git_index_entrycount(index_new); i++) {
e = git_index_get_byindex(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 || (git_vector_last(&paths) == NULL ||
strcmp(git_vector_last(&paths), e->path) != 0)) { strcmp(git_vector_last(&paths), e->path) != 0)) {
...@@ -2544,7 +2544,7 @@ int git_merge__append_conflicts_to_merge_msg( ...@@ -2544,7 +2544,7 @@ int git_merge__append_conflicts_to_merge_msg(
for (i = 0; i < git_index_entrycount(index); i++) { for (i = 0; i < git_index_entrycount(index); i++) {
const git_index_entry *e = git_index_get_byindex(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; continue;
if (last == NULL || strcmp(e->path, last) != 0) if (last == NULL || strcmp(e->path, last) != 0)
......
...@@ -272,7 +272,7 @@ void test_index_conflicts__moved_to_reuc_on_add(void) ...@@ -272,7 +272,7 @@ void test_index_conflicts__moved_to_reuc_on_add(void)
cl_assert(entry = git_index_get_byindex(repo_index, i)); cl_assert(entry = git_index_get_byindex(repo_index, i));
if (strcmp(entry->path, "conflicts-one.txt") == 0) 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) ...@@ -312,7 +312,7 @@ void test_index_conflicts__remove_all_conflicts(void)
for (i = 0; i < git_index_entrycount(repo_index); i++) { for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(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));
} }
} }
......
...@@ -71,7 +71,7 @@ static int merge_trivial_conflict_entrycount(git_index *index) ...@@ -71,7 +71,7 @@ static int merge_trivial_conflict_entrycount(git_index *index)
for (i = 0; i < git_index_entrycount(index); i++) { for (i = 0; i < git_index_entrycount(index); i++) {
cl_assert(entry = git_index_get_byindex(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++; count++;
} }
......
...@@ -66,7 +66,7 @@ static size_t merge_trivial_conflict_entrycount(void) ...@@ -66,7 +66,7 @@ static size_t merge_trivial_conflict_entrycount(void)
for (i = 0; i < git_index_entrycount(repo_index); i++) { for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(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++; count++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment