Skip to content

Commit c0ef65b

Browse files
committed
Implemented simple tree merging and a simple test, more elaborate testing is in progress
1 parent be97c45 commit c0ef65b

File tree

2 files changed

+135
-15
lines changed

2 files changed

+135
-15
lines changed

lib/git/index/fun.py

+78-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from stat import S_IFDIR
66
from cStringIO import StringIO
77

8+
from git.utils import IndexFileSHA1Writer
89
from git.errors import UnmergedEntriesError
9-
from git.objects.fun import tree_to_stream
10-
from git.utils import (
11-
IndexFileSHA1Writer,
12-
)
10+
from git.objects.fun import (
11+
tree_to_stream,
12+
traverse_tree_recursive,
13+
traverse_trees_recursive
14+
)
1315

1416
from typ import (
1517
BaseIndexEntry,
@@ -196,21 +198,89 @@ def write_tree_from_cache(entries, odb, sl, si=0):
196198
return (istream.binsha, tree_items)
197199

198200
def _tree_entry_to_baseindexentry(tree_entry, stage):
199-
return BaseIndexEntry(tree_entry[1], tree_entry[0], stage <<CE_STAGESHIFT, tree_entry[2])
201+
return BaseIndexEntry((tree_entry[1], tree_entry[0], stage <<CE_STAGESHIFT, tree_entry[2]))
200202

201203
def aggressive_tree_merge(odb, tree_shas):
202204
"""
203205
:return: list of BaseIndexEntries representing the aggressive merge of the given
204206
trees. All valid entries are on stage 0, whereas the conflicting ones are left
205207
on stage 1, 2 or 3, whereas stage 1 corresponds to the common ancestor tree,
206208
2 to our tree and 3 to 'their' tree.
207-
:param tree_shas: 1, 2 or 3 trees as identified by their shas"""
209+
:param tree_shas: 1, 2 or 3 trees as identified by their shas
210+
If 1 or two, the entries will effectively correspond to the last given tree
211+
If 3 are given, a 3 way merge is performed"""
208212
out = list()
209213
out_append = out.append
210-
if len(tree_shas) == 1:
211-
for entry in traverse_tree_recursive(odb, tree_shas[0]):
214+
215+
# one and two way is the same for us, as we don't have to handle an existing
216+
# index, instrea
217+
if len(tree_shas) in (1,2):
218+
for entry in traverse_tree_recursive(odb, tree_shas[-1], ''):
212219
out_append(_tree_entry_to_baseindexentry(entry, 0))
213220
# END for each entry
221+
elif len(tree_shas) == 3:
222+
for base, ours, theirs in traverse_trees_recursive(odb, tree_shas, ''):
223+
if base is not None:
224+
# base version exists
225+
if ours is not None:
226+
# ours exists
227+
if theirs is not None:
228+
# it exists in all branches, if it was changed in both
229+
# its a conflict, otherwise we take the changed version
230+
# This should be the most common branch, so it comes first
231+
if( base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0] ) or \
232+
( base[1] != ours[1] and base[1] != theirs[1] and ourse[1] != theirs[1] ):
233+
# changed by both
234+
out_append(_tree_entry_to_baseindexentry(base, 1))
235+
out_append(_tree_entry_to_baseindexentry(ours, 2))
236+
out_append(_tree_entry_to_baseindexentry(theirs, 3))
237+
elif base[0] != ours[0] or base[1] != ours[1]:
238+
# only we changed it
239+
out_append(_tree_entry_to_baseindexentry(ours, 0))
240+
else:
241+
# either nobody changed it, or they did. In either
242+
# case, use theirs
243+
out_append(_tree_entry_to_baseindexentry(theirs, 0))
244+
# END handle modification
245+
else:
246+
247+
if ours[0] != base[0] or ours[1] != base[1]:
248+
# they deleted it, we changed it, conflict
249+
out_append(_tree_entry_to_baseindexentry(base, 1))
250+
out_append(_tree_entry_to_baseindexentry(ours, 2))
251+
out_append(_tree_entry_to_baseindexentry(theirs, 3))
252+
# else:
253+
# we didn't change it, ignore
254+
# pass
255+
# END handle our change
256+
# END handle theirs
257+
else:
258+
if theirs is None:
259+
# deleted in both, its fine - its out
260+
pass
261+
else:
262+
if theirs[0] != base[0] or theirs[1] != base[1]:
263+
# deleted in ours, changed theirs, conflict
264+
out_append(_tree_entry_to_baseindexentry(base, 1))
265+
out_append(_tree_entry_to_baseindexentry(ours, 2))
266+
out_append(_tree_entry_to_baseindexentry(theirs, 3))
267+
# END theirs changed
268+
#else:
269+
# theirs didnt change
270+
# pass
271+
# END handle theirs
272+
# END handle ours
273+
else:
274+
# all three can't be None
275+
if ours is None:
276+
# added in their branch
277+
out_append(_tree_entry_to_baseindexentry(theirs, 0))
278+
elif theirs is None:
279+
# added in our branch
280+
out_append(_tree_entry_to_baseindexentry(ours, 0))
281+
# END hanle heads
282+
# END handle base exists
283+
# END for each entries tuple
214284
else:
215285
raise ValueError("Cannot handle %i trees at once" % len(tree_shas))
216286
# END handle tree shas

test/git/test_fun.py

+57-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,67 @@
88
aggressive_tree_merge
99
)
1010

11+
from git.index import IndexFile
12+
from stat import (
13+
S_IFDIR,
14+
S_IFREG,
15+
S_IFLNK
16+
)
17+
1118
class TestFun(TestBase):
1219

20+
def _assert_index_entries(self, entries, trees):
21+
index = IndexFile.from_tree(self.rorepo, *trees)
22+
assert entries
23+
assert len(index.entries) == len(entries)
24+
for entry in entries:
25+
assert (entry.path, entry.stage) in index.entries
26+
# END assert entry matches fully
27+
1328
def test_aggressive_tree_merge(self):
1429
# head tree with additions, removals and modification compared to its predecessor
30+
odb = self.rorepo.odb
1531
HC = self.rorepo.commit("6c1faef799095f3990e9970bc2cb10aa0221cf9c")
1632
H = HC.tree
1733
B = HC.parents[0].tree
1834

19-
# test new index from single tree
35+
# entries from single tree
36+
trees = [H.sha]
37+
self._assert_index_entries(aggressive_tree_merge(odb, trees), trees)
38+
39+
# from multiple trees
40+
trees = [B.sha, H.sha]
41+
self._assert_index_entries(aggressive_tree_merge(odb, trees), trees)
42+
43+
# three way, no conflict
44+
tree = self.rorepo.tree
45+
B = tree("35a09c0534e89b2d43ec4101a5fb54576b577905")
46+
H = tree("4fe5cfa0e063a8d51a1eb6f014e2aaa994e5e7d4")
47+
M = tree("1f2b19de3301e76ab3a6187a49c9c93ff78bafbd")
48+
trees = [B.sha, H.sha, M.sha]
49+
self._assert_index_entries(aggressive_tree_merge(odb, trees), trees)
50+
51+
# three-way, conflict in at least one file, both modified
52+
B = tree("a7a4388eeaa4b6b94192dce67257a34c4a6cbd26")
53+
H = tree("f9cec00938d9059882bb8eabdaf2f775943e00e5")
54+
M = tree("44a601a068f4f543f73fd9c49e264c931b1e1652")
55+
trees = [B.sha, H.sha, M.sha]
56+
self._assert_index_entries(aggressive_tree_merge(odb, trees), trees)
57+
58+
def make_tree(odb, entries):
59+
"""create a tree from the given tree entries and safe it to the database"""
60+
61+
62+
@with_rw_repo('0.1.6')
63+
def test_three_way_merge(self, rwrepo):
64+
def mkfile(name, sha, executable=0):
65+
return (sha, S_IFREG | 644 | executable*0111, name)
66+
def mkcommit(name, sha):
67+
return (sha, S_IFDIR | S_IFLNK, name)
68+
odb = rwrepo.odb
69+
2070

21-
def _assert_entries(self, entries, num_trees):
71+
def _assert_tree_entries(self, entries, num_trees):
2272
assert len(entries[0]) == num_trees
2373
for entry in entries:
2474
paths = set(e[2] for e in entry if e)
@@ -37,25 +87,25 @@ def test_tree_traversal(self):
3787

3888
# two very different trees
3989
entries = traverse_trees_recursive(odb, [B_old.sha, H.sha], '')
40-
self._assert_entries(entries, 2)
90+
self._assert_tree_entries(entries, 2)
4191

4292
oentries = traverse_trees_recursive(odb, [H.sha, B_old.sha], '')
4393
assert len(oentries) == len(entries)
44-
self._assert_entries(oentries, 2)
94+
self._assert_tree_entries(oentries, 2)
4595

4696
# single tree
4797
is_no_tree = lambda i, d: i.type != 'tree'
4898
entries = traverse_trees_recursive(odb, [B.sha], '')
4999
assert len(entries) == len(list(B.traverse(predicate=is_no_tree)))
50-
self._assert_entries(entries, 1)
100+
self._assert_tree_entries(entries, 1)
51101

52102
# two trees
53103
entries = traverse_trees_recursive(odb, [B.sha, H.sha], '')
54-
self._assert_entries(entries, 2)
104+
self._assert_tree_entries(entries, 2)
55105

56106
# tree trees
57107
entries = traverse_trees_recursive(odb, [B.sha, H.sha, M.sha], '')
58-
self._assert_entries(entries, 3)
108+
self._assert_tree_entries(entries, 3)
59109

60110
def test_tree_traversal_single(self):
61111
max_count = 50

0 commit comments

Comments
 (0)