Merge pull request #3847 from libgit2/ethomson/read_index_conflicts Include conflicts in `git_index_read_index`
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
diff --git a/src/index.c b/src/index.c
index 32f585f..6546ea1 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2925,38 +2925,39 @@ cleanup:
return error;
}
-int git_index_read_index(
+static int git_index_read_iterator(
git_index *index,
- const git_index *new_index)
+ git_iterator *new_iterator,
+ size_t new_length_hint)
{
git_vector new_entries = GIT_VECTOR_INIT,
remove_entries = GIT_VECTOR_INIT;
git_idxmap *new_entries_map = NULL;
git_iterator *index_iterator = NULL;
- git_iterator *new_iterator = NULL;
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *old_entry, *new_entry;
git_index_entry *entry;
size_t i;
int error;
- if ((error = git_vector_init(&new_entries, new_index->entries.length, index->entries._cmp)) < 0 ||
+ assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
+
+ if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
(error = git_idxmap_alloc(&new_entries_map)) < 0)
goto done;
- if (index->ignore_case)
- kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_index->entries.length);
- else
- kh_resize(idx, new_entries_map, new_index->entries.length);
+ if (index->ignore_case && new_length_hint)
+ kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_length_hint);
+ else if (new_length_hint)
+ kh_resize(idx, new_entries_map, new_length_hint);
- opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
+ opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
- if ((error = git_iterator_for_index(&index_iterator, git_index_owner(index), index, &opts)) < 0 ||
- (error = git_iterator_for_index(&new_iterator, git_index_owner(new_index), (git_index *)new_index, &opts)) < 0)
- goto done;
-
- if (((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
+ if ((error = git_iterator_for_index(&index_iterator,
+ git_index_owner(index), index, &opts)) < 0 ||
+ ((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
error != GIT_ITEROVER) ||
((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
error != GIT_ITEROVER))
@@ -3050,6 +3051,8 @@ int git_index_read_index(
index_entry_free(entry);
}
+ clear_uptodate(index);
+
error = 0;
done:
@@ -3057,6 +3060,27 @@ done:
git_vector_free(&new_entries);
git_vector_free(&remove_entries);
git_iterator_free(index_iterator);
+ return error;
+}
+
+int git_index_read_index(
+ git_index *index,
+ const git_index *new_index)
+{
+ git_iterator *new_iterator = NULL;
+ git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
+ int error;
+
+ opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
+
+ if ((error = git_iterator_for_index(&new_iterator,
+ git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
+ (error = git_index_read_iterator(index, new_iterator,
+ new_index->entries.length)) < 0)
+ goto done;
+
+done:
git_iterator_free(new_iterator);
return error;
}
diff --git a/tests/index/conflicts.c b/tests/index/conflicts.c
index d400468..cabef88 100644
--- a/tests/index/conflicts.c
+++ b/tests/index/conflicts.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
+#include "conflicts.h"
static git_repository *repo;
static git_index *repo_index;
@@ -8,14 +9,6 @@ static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
-#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
-#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
-#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"
-
-#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
-#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
-#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"
-
// Fixture setup and teardown
void test_index_conflicts__initialize(void)
{
diff --git a/tests/index/conflicts.h b/tests/index/conflicts.h
new file mode 100644
index 0000000..3e26e6d
--- /dev/null
+++ b/tests/index/conflicts.h
@@ -0,0 +1,7 @@
+#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
+#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
+#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"
+
+#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
+#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
+#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"
diff --git a/tests/index/read_index.c b/tests/index/read_index.c
index 6d14bc9..2df7cc8 100644
--- a/tests/index/read_index.c
+++ b/tests/index/read_index.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "index.h"
+#include "conflicts.h"
static git_repository *_repo;
static git_index *_index;
@@ -126,3 +127,106 @@ void test_index_read_index__read_and_writes(void)
git_index_free(tree_index);
git_index_free(new_index);
}
+
+static void add_conflicts(git_index *index, const char *filename)
+{
+ git_index_entry ancestor_entry, our_entry, their_entry;
+ static int conflict_idx = 0;
+ char *ancestor_ids[] =
+ { CONFLICTS_ONE_ANCESTOR_OID, CONFLICTS_TWO_ANCESTOR_OID };
+ char *our_ids[] =
+ { CONFLICTS_ONE_OUR_OID, CONFLICTS_TWO_OUR_OID };
+ char *their_ids[] =
+ { CONFLICTS_ONE_THEIR_OID, CONFLICTS_TWO_THEIR_OID };
+
+ conflict_idx = (conflict_idx + 1) % 2;
+
+ memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
+ memset(&our_entry, 0x0, sizeof(git_index_entry));
+ memset(&their_entry, 0x0, sizeof(git_index_entry));
+
+ ancestor_entry.path = filename;
+ ancestor_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1);
+ git_oid_fromstr(&ancestor_entry.id, ancestor_ids[conflict_idx]);
+
+ our_entry.path = filename;
+ our_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&our_entry, 2);
+ git_oid_fromstr(&our_entry.id, our_ids[conflict_idx]);
+
+ their_entry.path = filename;
+ their_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 2);
+ git_oid_fromstr(&their_entry.id, their_ids[conflict_idx]);
+
+ cl_git_pass(git_index_conflict_add(index, &ancestor_entry,
+ &our_entry, &their_entry));
+}
+
+void test_index_read_index__handles_conflicts(void)
+{
+ git_oid tree_id;
+ git_tree *tree;
+ git_index *index, *new_index;
+ git_index_conflict_iterator *conflict_iterator;
+ const git_index_entry *ancestor, *ours, *theirs;
+
+ cl_git_pass(git_oid_fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12"));
+ cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
+ cl_git_pass(git_index_new(&index));
+ cl_git_pass(git_index_new(&new_index));
+ cl_git_pass(git_index_read_tree(index, tree));
+ cl_git_pass(git_index_read_tree(new_index, tree));
+
+ /* put some conflicts in only the old side, these should be removed */
+ add_conflicts(index, "orig_side-1.txt");
+ add_conflicts(index, "orig_side-2.txt");
+
+ /* put some conflicts in both indexes, these should be unchanged */
+ add_conflicts(index, "both_sides-1.txt");
+ add_conflicts(new_index, "both_sides-1.txt");
+ add_conflicts(index, "both_sides-2.txt");
+ add_conflicts(new_index, "both_sides-2.txt");
+
+ /* put some conflicts in the new index, these should be added */
+ add_conflicts(new_index, "new_side-1.txt");
+ add_conflicts(new_index, "new_side-2.txt");
+
+ cl_git_pass(git_index_read_index(index, new_index));
+ cl_git_pass(git_index_conflict_iterator_new(&conflict_iterator, index));
+
+ cl_git_pass(git_index_conflict_next(
+ &ancestor, &ours, &theirs, conflict_iterator));
+ cl_assert_equal_s("both_sides-1.txt", ancestor->path);
+ cl_assert_equal_s("both_sides-1.txt", ours->path);
+ cl_assert_equal_s("both_sides-1.txt", theirs->path);
+
+ cl_git_pass(git_index_conflict_next(
+ &ancestor, &ours, &theirs, conflict_iterator));
+ cl_assert_equal_s("both_sides-2.txt", ancestor->path);
+ cl_assert_equal_s("both_sides-2.txt", ours->path);
+ cl_assert_equal_s("both_sides-2.txt", theirs->path);
+
+ cl_git_pass(git_index_conflict_next(
+ &ancestor, &ours, &theirs, conflict_iterator));
+ cl_assert_equal_s("new_side-1.txt", ancestor->path);
+ cl_assert_equal_s("new_side-1.txt", ours->path);
+ cl_assert_equal_s("new_side-1.txt", theirs->path);
+
+ cl_git_pass(git_index_conflict_next(
+ &ancestor, &ours, &theirs, conflict_iterator));
+ cl_assert_equal_s("new_side-2.txt", ancestor->path);
+ cl_assert_equal_s("new_side-2.txt", ours->path);
+ cl_assert_equal_s("new_side-2.txt", theirs->path);
+
+
+ cl_git_fail_with(GIT_ITEROVER, git_index_conflict_next(
+ &ancestor, &ours, &theirs, conflict_iterator));
+
+ git_index_conflict_iterator_free(conflict_iterator);
+
+ git_tree_free(tree);
+ git_index_free(new_index);
+ git_index_free(index);
+}