Skip to content

Commit e421845

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#3847 from libgit2/ethomson/read_index_conflicts
Include conflicts in `git_index_read_index`
2 parents 59a0005 + 6249d96 commit e421845

File tree

4 files changed

+150
-22
lines changed

4 files changed

+150
-22
lines changed

src/index.c

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,38 +2925,39 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
29252925
return error;
29262926
}
29272927

2928-
int git_index_read_index(
2928+
static int git_index_read_iterator(
29292929
git_index *index,
2930-
const git_index *new_index)
2930+
git_iterator *new_iterator,
2931+
size_t new_length_hint)
29312932
{
29322933
git_vector new_entries = GIT_VECTOR_INIT,
29332934
remove_entries = GIT_VECTOR_INIT;
29342935
git_idxmap *new_entries_map = NULL;
29352936
git_iterator *index_iterator = NULL;
2936-
git_iterator *new_iterator = NULL;
29372937
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
29382938
const git_index_entry *old_entry, *new_entry;
29392939
git_index_entry *entry;
29402940
size_t i;
29412941
int error;
29422942

2943-
if ((error = git_vector_init(&new_entries, new_index->entries.length, index->entries._cmp)) < 0 ||
2943+
assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
2944+
2945+
if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
29442946
(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
29452947
(error = git_idxmap_alloc(&new_entries_map)) < 0)
29462948
goto done;
29472949

2948-
if (index->ignore_case)
2949-
kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_index->entries.length);
2950-
else
2951-
kh_resize(idx, new_entries_map, new_index->entries.length);
2950+
if (index->ignore_case && new_length_hint)
2951+
kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_length_hint);
2952+
else if (new_length_hint)
2953+
kh_resize(idx, new_entries_map, new_length_hint);
29522954

2953-
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2955+
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
2956+
GIT_ITERATOR_INCLUDE_CONFLICTS;
29542957

2955-
if ((error = git_iterator_for_index(&index_iterator, git_index_owner(index), index, &opts)) < 0 ||
2956-
(error = git_iterator_for_index(&new_iterator, git_index_owner(new_index), (git_index *)new_index, &opts)) < 0)
2957-
goto done;
2958-
2959-
if (((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
2958+
if ((error = git_iterator_for_index(&index_iterator,
2959+
git_index_owner(index), index, &opts)) < 0 ||
2960+
((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
29602961
error != GIT_ITEROVER) ||
29612962
((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
29622963
error != GIT_ITEROVER))
@@ -3050,13 +3051,36 @@ int git_index_read_index(
30503051
index_entry_free(entry);
30513052
}
30523053

3054+
clear_uptodate(index);
3055+
30533056
error = 0;
30543057

30553058
done:
30563059
git_idxmap_free(new_entries_map);
30573060
git_vector_free(&new_entries);
30583061
git_vector_free(&remove_entries);
30593062
git_iterator_free(index_iterator);
3063+
return error;
3064+
}
3065+
3066+
int git_index_read_index(
3067+
git_index *index,
3068+
const git_index *new_index)
3069+
{
3070+
git_iterator *new_iterator = NULL;
3071+
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3072+
int error;
3073+
3074+
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
3075+
GIT_ITERATOR_INCLUDE_CONFLICTS;
3076+
3077+
if ((error = git_iterator_for_index(&new_iterator,
3078+
git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
3079+
(error = git_index_read_iterator(index, new_iterator,
3080+
new_index->entries.length)) < 0)
3081+
goto done;
3082+
3083+
done:
30603084
git_iterator_free(new_iterator);
30613085
return error;
30623086
}

tests/index/conflicts.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
#include "clar_libgit2.h"
22
#include "index.h"
33
#include "git2/repository.h"
4+
#include "conflicts.h"
45

56
static git_repository *repo;
67
static git_index *repo_index;
78

89
#define TEST_REPO_PATH "mergedrepo"
910
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
1011

11-
#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
12-
#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
13-
#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"
14-
15-
#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
16-
#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
17-
#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"
18-
1912
// Fixture setup and teardown
2013
void test_index_conflicts__initialize(void)
2114
{

tests/index/conflicts.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
2+
#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
3+
#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"
4+
5+
#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
6+
#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
7+
#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"

tests/index/read_index.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "clar_libgit2.h"
22
#include "posix.h"
33
#include "index.h"
4+
#include "conflicts.h"
45

56
static git_repository *_repo;
67
static git_index *_index;
@@ -126,3 +127,106 @@ void test_index_read_index__read_and_writes(void)
126127
git_index_free(tree_index);
127128
git_index_free(new_index);
128129
}
130+
131+
static void add_conflicts(git_index *index, const char *filename)
132+
{
133+
git_index_entry ancestor_entry, our_entry, their_entry;
134+
static int conflict_idx = 0;
135+
char *ancestor_ids[] =
136+
{ CONFLICTS_ONE_ANCESTOR_OID, CONFLICTS_TWO_ANCESTOR_OID };
137+
char *our_ids[] =
138+
{ CONFLICTS_ONE_OUR_OID, CONFLICTS_TWO_OUR_OID };
139+
char *their_ids[] =
140+
{ CONFLICTS_ONE_THEIR_OID, CONFLICTS_TWO_THEIR_OID };
141+
142+
conflict_idx = (conflict_idx + 1) % 2;
143+
144+
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
145+
memset(&our_entry, 0x0, sizeof(git_index_entry));
146+
memset(&their_entry, 0x0, sizeof(git_index_entry));
147+
148+
ancestor_entry.path = filename;
149+
ancestor_entry.mode = 0100644;
150+
GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1);
151+
git_oid_fromstr(&ancestor_entry.id, ancestor_ids[conflict_idx]);
152+
153+
our_entry.path = filename;
154+
our_entry.mode = 0100644;
155+
GIT_IDXENTRY_STAGE_SET(&our_entry, 2);
156+
git_oid_fromstr(&our_entry.id, our_ids[conflict_idx]);
157+
158+
their_entry.path = filename;
159+
their_entry.mode = 0100644;
160+
GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 2);
161+
git_oid_fromstr(&their_entry.id, their_ids[conflict_idx]);
162+
163+
cl_git_pass(git_index_conflict_add(index, &ancestor_entry,
164+
&our_entry, &their_entry));
165+
}
166+
167+
void test_index_read_index__handles_conflicts(void)
168+
{
169+
git_oid tree_id;
170+
git_tree *tree;
171+
git_index *index, *new_index;
172+
git_index_conflict_iterator *conflict_iterator;
173+
const git_index_entry *ancestor, *ours, *theirs;
174+
175+
cl_git_pass(git_oid_fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12"));
176+
cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
177+
cl_git_pass(git_index_new(&index));
178+
cl_git_pass(git_index_new(&new_index));
179+
cl_git_pass(git_index_read_tree(index, tree));
180+
cl_git_pass(git_index_read_tree(new_index, tree));
181+
182+
/* put some conflicts in only the old side, these should be removed */
183+
add_conflicts(index, "orig_side-1.txt");
184+
add_conflicts(index, "orig_side-2.txt");
185+
186+
/* put some conflicts in both indexes, these should be unchanged */
187+
add_conflicts(index, "both_sides-1.txt");
188+
add_conflicts(new_index, "both_sides-1.txt");
189+
add_conflicts(index, "both_sides-2.txt");
190+
add_conflicts(new_index, "both_sides-2.txt");
191+
192+
/* put some conflicts in the new index, these should be added */
193+
add_conflicts(new_index, "new_side-1.txt");
194+
add_conflicts(new_index, "new_side-2.txt");
195+
196+
cl_git_pass(git_index_read_index(index, new_index));
197+
cl_git_pass(git_index_conflict_iterator_new(&conflict_iterator, index));
198+
199+
cl_git_pass(git_index_conflict_next(
200+
&ancestor, &ours, &theirs, conflict_iterator));
201+
cl_assert_equal_s("both_sides-1.txt", ancestor->path);
202+
cl_assert_equal_s("both_sides-1.txt", ours->path);
203+
cl_assert_equal_s("both_sides-1.txt", theirs->path);
204+
205+
cl_git_pass(git_index_conflict_next(
206+
&ancestor, &ours, &theirs, conflict_iterator));
207+
cl_assert_equal_s("both_sides-2.txt", ancestor->path);
208+
cl_assert_equal_s("both_sides-2.txt", ours->path);
209+
cl_assert_equal_s("both_sides-2.txt", theirs->path);
210+
211+
cl_git_pass(git_index_conflict_next(
212+
&ancestor, &ours, &theirs, conflict_iterator));
213+
cl_assert_equal_s("new_side-1.txt", ancestor->path);
214+
cl_assert_equal_s("new_side-1.txt", ours->path);
215+
cl_assert_equal_s("new_side-1.txt", theirs->path);
216+
217+
cl_git_pass(git_index_conflict_next(
218+
&ancestor, &ours, &theirs, conflict_iterator));
219+
cl_assert_equal_s("new_side-2.txt", ancestor->path);
220+
cl_assert_equal_s("new_side-2.txt", ours->path);
221+
cl_assert_equal_s("new_side-2.txt", theirs->path);
222+
223+
224+
cl_git_fail_with(GIT_ITEROVER, git_index_conflict_next(
225+
&ancestor, &ours, &theirs, conflict_iterator));
226+
227+
git_index_conflict_iterator_free(conflict_iterator);
228+
229+
git_tree_free(tree);
230+
git_index_free(new_index);
231+
git_index_free(index);
232+
}

0 commit comments

Comments
 (0)