|
5 | 5 | from stat import S_IFDIR
|
6 | 6 | from cStringIO import StringIO
|
7 | 7 |
|
| 8 | +from git.utils import IndexFileSHA1Writer |
8 | 9 | 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 | + ) |
13 | 15 |
|
14 | 16 | from typ import (
|
15 | 17 | BaseIndexEntry,
|
@@ -196,21 +198,89 @@ def write_tree_from_cache(entries, odb, sl, si=0):
|
196 | 198 | return (istream.binsha, tree_items)
|
197 | 199 |
|
198 | 200 | 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])) |
200 | 202 |
|
201 | 203 | def aggressive_tree_merge(odb, tree_shas):
|
202 | 204 | """
|
203 | 205 | :return: list of BaseIndexEntries representing the aggressive merge of the given
|
204 | 206 | trees. All valid entries are on stage 0, whereas the conflicting ones are left
|
205 | 207 | on stage 1, 2 or 3, whereas stage 1 corresponds to the common ancestor tree,
|
206 | 208 | 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""" |
208 | 212 | out = list()
|
209 | 213 | 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], ''): |
212 | 219 | out_append(_tree_entry_to_baseindexentry(entry, 0))
|
213 | 220 | # 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 |
214 | 284 | else:
|
215 | 285 | raise ValueError("Cannot handle %i trees at once" % len(tree_shas))
|
216 | 286 | # END handle tree shas
|
|
0 commit comments