diff/status: introduce conflicts When diffing against an index, return a new `GIT_DELTA_CONFLICTED` delta type for items that are conflicted. For a single file path, only one delta will be produced (despite the fact that there are multiple entries in the index). Index iterators now have the (optional) ability to return conflicts in the index. Prior to this change, they would be omitted, and callers (like diff) would omit conflicted index entries entirely.
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e4bdf9..03e5273 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -100,6 +100,14 @@ support for HTTPS connections insead of OpenSSL.
configuration of the server, and tools can use this to show messages
about failing to communicate with the server.
+* `git_diff_index_to_workdir()` and `git_diff_tree_to_index()` will now
+ produce deltas of type `GIT_DELTA_CONFLICTED` to indicate that the index
+ side of the delta is a conflict.
+
+* The `git_status` family of functions will now produce status of type
+ `GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file
+ in the index.
+
### API removals
* `git_remote_save()` and `git_remote_clear_refspecs()` has been
diff --git a/include/git2/diff.h b/include/git2/diff.h
index cac3b26..7505c08 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -239,16 +239,17 @@ typedef enum {
* DELETED pairs).
*/
typedef enum {
- GIT_DELTA_UNMODIFIED = 0, /**< no changes */
- GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */
- GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */
- GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */
- GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */
- GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */
- GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */
- GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */
- GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */
- GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */
+ GIT_DELTA_UNMODIFIED = 0, /**< no changes */
+ GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */
+ GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */
+ GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */
+ GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */
+ GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */
+ GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */
+ GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */
+ GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */
+ GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */
+ GIT_DELTA_CONFLICTED = 10, /**< entry in the index is conflicted */
} git_delta_t;
/**
diff --git a/include/git2/status.h b/include/git2/status.h
index 5f21181..6711139 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -46,6 +46,7 @@ typedef enum {
GIT_STATUS_WT_UNREADABLE = (1u << 12),
GIT_STATUS_IGNORED = (1u << 14),
+ GIT_STATUS_CONFLICTED = (1u << 15),
} git_status_t;
/**
diff --git a/src/diff.c b/src/diff.c
index f829fb0..765956a 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -327,6 +327,22 @@ static const char *diff_mnemonic_prefix(
return pfx;
}
+static int diff_entry_cmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcmp(entry_a->path, entry_b->path);
+}
+
+static int diff_entry_icmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcasecmp(entry_a->path, entry_b->path);
+}
+
static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
{
if (!ignore_case) {
@@ -335,7 +351,7 @@ static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
diff->strcomp = git__strcmp;
diff->strncomp = git__strncmp;
diff->pfxcomp = git__prefixcmp;
- diff->entrycomp = git_index_entry_cmp;
+ diff->entrycomp = diff_entry_cmp;
git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
} else {
@@ -344,7 +360,7 @@ static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
diff->strcomp = git__strcasecmp;
diff->strncomp = git__strncasecmp;
diff->pfxcomp = git__prefixcmp_icase;
- diff->entrycomp = git_index_entry_icmp;
+ diff->entrycomp = diff_entry_icmp;
git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
}
@@ -731,8 +747,12 @@ static int maybe_modified(
new_is_workdir)
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)
+ status = GIT_DELTA_CONFLICTED;
+
/* support "assume unchanged" (poorly, b/c we still stat everything) */
- if ((oitem->flags & GIT_IDXENTRY_VALID) != 0)
+ else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0)
status = GIT_DELTA_UNMODIFIED;
/* support "skip worktree" index bit */
@@ -875,9 +895,29 @@ static int iterator_advance(
const git_index_entry **entry,
git_iterator *iterator)
{
- int error;
+ const git_index_entry *prev_entry = *entry;
+ int cmp, error;
+
+ /* if we're looking for conflicts, we only want to report
+ * one conflict for each file, instead of all three sides.
+ * so if this entry is a conflict for this file, and the
+ * previous one was a conflict for the same file, skip it.
+ */
+ 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)
+ break;
+
+ cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
+ strcasecmp(prev_entry->path, (*entry)->path) :
+ strcmp(prev_entry->path, (*entry)->path);
+
+ if (cmp)
+ break;
+ }
- if ((error = git_iterator_advance(entry, iterator)) == GIT_ITEROVER) {
+ if (error == GIT_ITEROVER) {
*entry = NULL;
error = 0;
}
@@ -926,8 +966,12 @@ static int handle_unmatched_new_item(
/* check if this is a prefix of the other side */
contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_stage(nitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
/* update delta_type if this item is ignored */
- if (git_iterator_current_is_ignored(info->new_iter))
+ else if (git_iterator_current_is_ignored(info->new_iter))
delta_type = GIT_DELTA_IGNORED;
if (nitem->mode == GIT_FILEMODE_TREE) {
@@ -1067,8 +1111,14 @@ static int handle_unmatched_new_item(
static int handle_unmatched_old_item(
git_diff *diff, diff_in_progress *info)
{
- int error = diff_delta__from_one(diff, GIT_DELTA_DELETED, info->oitem);
- if (error != 0)
+ git_delta_t delta_type = GIT_DELTA_DELETED;
+ int error;
+
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_stage(info->oitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
+ if ((error = diff_delta__from_one(diff, delta_type, info->oitem)) < 0)
return error;
/* if we are generating TYPECHANGE records then check for that
@@ -1234,6 +1284,8 @@ int git_diff_tree_to_index(
{
int error = 0;
bool index_ignore_case = false;
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
assert(diff && repo);
@@ -1243,10 +1295,8 @@ int git_diff_tree_to_index(
index_ignore_case = index->ignore_case;
DIFF_FROM_ITERATORS(
- git_iterator_for_tree(
- &a, old_tree, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx),
- git_iterator_for_index(
- &b, index, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx)
+ git_iterator_for_tree(&a, old_tree, iflag, pfx, pfx),
+ git_iterator_for_index(&b, index, iflag, pfx, pfx)
);
/* if index is in case-insensitive order, re-sort deltas to match */
@@ -1270,7 +1320,8 @@ int git_diff_index_to_workdir(
return error;
DIFF_FROM_ITERATORS(
- git_iterator_for_index(&a, index, 0, pfx, pfx),
+ git_iterator_for_index(
+ &a, index, GIT_ITERATOR_INCLUDE_CONFLICTS, pfx, pfx),
git_iterator_for_workdir(
&b, repo, index, NULL, GIT_ITERATOR_DONT_AUTOEXPAND, pfx, pfx)
);
diff --git a/src/reset.c b/src/reset.c
index d08e48c..0ffa51b 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -63,6 +63,7 @@ int git_reset_default(
assert(delta->status == GIT_DELTA_ADDED ||
delta->status == GIT_DELTA_MODIFIED ||
+ delta->status == GIT_DELTA_CONFLICTED ||
delta->status == GIT_DELTA_DELETED);
error = git_index_conflict_remove(index, delta->old_file.path);
diff --git a/src/status.c b/src/status.c
index 720ccb7..b206b0e 100644
--- a/src/status.c
+++ b/src/status.c
@@ -44,6 +44,9 @@ static unsigned int index_delta2status(const git_diff_delta *head2idx)
case GIT_DELTA_TYPECHANGE:
st = GIT_STATUS_INDEX_TYPECHANGE;
break;
+ case GIT_DELTA_CONFLICTED:
+ st = GIT_STATUS_CONFLICTED;
+ break;
default:
break;
}
@@ -102,6 +105,9 @@ static unsigned int workdir_delta2status(
case GIT_DELTA_TYPECHANGE:
st = GIT_STATUS_WT_TYPECHANGE;
break;
+ case GIT_DELTA_CONFLICTED:
+ st = GIT_STATUS_CONFLICTED;
+ break;
default:
break;
}
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index de21a1c..9be0a41 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -487,7 +487,7 @@ void test_status_worktree__conflict_with_diff3(void)
cl_git_pass(git_status_file(&status, repo, "modified_file"));
- cl_assert_equal_i(GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW, status);
+ cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
}
static const char *filemode_paths[] = {
@@ -640,7 +640,7 @@ void test_status_worktree__conflicted_item(void)
&our_entry, &their_entry));
cl_git_pass(git_status_file(&status, repo, "modified_file"));
- cl_assert_equal_i(GIT_STATUS_INDEX_DELETED|GIT_STATUS_WT_NEW, status);
+ cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
git_index_free(index);
}