[Pkg-bazaar-commits] ./bzr/unstable r825: - Merge3.find_sync_regions always returns a zero-length sentinal at the end to
Martin Pool
mbp at sourcefrog.net
Fri Apr 10 08:21:06 UTC 2009
------------------------------------------------------------
revno: 825
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Tue 2005-07-05 18:09:30 +1000
message:
- Merge3.find_sync_regions always returns a zero-length sentinal at the end to
ease matching.
- Merge3.merge partially done.
modified:
bzrlib/merge3.py
bzrlib/selftest/testmerge3.py
-------------- next part --------------
=== modified file 'bzrlib/merge3.py'
--- a/bzrlib/merge3.py 2005-07-05 07:32:22 +0000
+++ b/bzrlib/merge3.py 2005-07-05 08:09:30 +0000
@@ -68,9 +68,18 @@
self.b_ops = SequenceMatcher(None, base, b).get_opcodes()
- def merge(self):
+ def merge_regions(self):
"""Return sequences of matching and conflicting regions.
+ This returns tuples, where the first value says what kind we
+ have:
+
+ 'unchanged', start, end
+ Take a region of base[start:end]
+
+ 'a', start, end
+ Non-clashing insertion from a[start:end]
+
Method is as follows:
The two sequences align only on regions which match the base
@@ -82,13 +91,42 @@
The regions in between can be in any of three cases:
conflicted, or changed on only one side.
"""
+
+ # section a[0:ia] has been disposed of, etc
+ iz = ia = ib = 0
+
+ for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
+ matchlen = zend - zmatch
+ assert matchlen >= 0
+ assert matchlen == (aend - amatch)
+ assert matchlen == (bend - bmatch)
+
+ if amatch > ia: # or bmatch > ib:
+ # got an unmatched region; work out if either
+ # alternative is the same as the base
+
+ # kludge: return the whole thing as inserted into A
+ yield 'a', ia, amatch
+ ia = amatch
+
+
+ if matchlen > 0:
+ assert ia == amatch
+ assert ib == bmatch
+ assert iz == zmatch
+
+ yield 'unchanged', zmatch, zend
+ iz = zend
+ ia = aend
+ ib = bend
def find_sync_regions(self):
"""Return a list of sync regions, where both descendents match the base.
- Generates a list of (base1, base2, a1, a2, b1, b2).
+ Generates a list of (base1, base2, a1, a2, b1, b2). There is
+ always a zero-length sync region at the end of all the files.
"""
from difflib import SequenceMatcher
aiter = iter(SequenceMatcher(None, self.base, self.a).get_matching_blocks())
@@ -133,6 +171,11 @@
else:
bbase, bmatch, blen = biter.next()
+ intbase = len(self.base)
+ abase = len(self.a)
+ bbase = len(self.b)
+ yield (intbase, intbase, abase, abase, bbase, bbase)
+
def find_unconflicted(self):
=== modified file 'bzrlib/selftest/testmerge3.py'
--- a/bzrlib/selftest/testmerge3.py 2005-07-05 07:32:22 +0000
+++ b/bzrlib/selftest/testmerge3.py 2005-07-05 08:09:30 +0000
@@ -31,8 +31,45 @@
self.assertEquals(list(m3.find_sync_regions()),
[(0, 2,
0, 2,
- 0, 2)])
-
+ 0, 2),
+ (2,2, 2,2, 2,2)])
+
+ self.assertEquals(list(m3.merge_regions()),
+ [('unchanged', 0, 2)])
+
+
+class FrontInsert(TestBase):
+ def runTest(self):
+ m3 = Merge3(['zz'],
+ ['aaa', 'bbb', 'zz'],
+ ['zz'])
+
+ # todo: should use a sentinal at end as from get_matching_blocks
+ # to match without zz
+ self.assertEquals(list(m3.find_sync_regions()),
+ [(0,1, 2,3, 0,1),
+ (1,1, 3,3, 1,1),])
+
+ self.assertEquals(list(m3.merge_regions()),
+ [('a', 0, 2),
+ ('unchanged', 0, 1)])
+
+
+
+class NullInsert(TestBase):
+ def runTest(self):
+ m3 = Merge3([],
+ ['aaa', 'bbb'],
+ [])
+
+ # todo: should use a sentinal at end as from get_matching_blocks
+ # to match without zz
+ self.assertEquals(list(m3.find_sync_regions()),
+ [(0,0, 2,2, 0,0)])
+
+ self.assertEquals(list(m3.merge_regions()),
+ [('a', 0, 2)])
+
class NoConflicts(TestBase):
@@ -45,11 +82,12 @@
self.assertEquals(m3.find_unconflicted(),
[(0, 1), (1, 2)])
-
-
self.assertEquals(list(m3.find_sync_regions()),
- [(0, 1, 0, 1, 0, 1),
- (1, 2, 2, 3, 1, 2)])
+ [(0,1, 0,1, 0,1),
+ (1,2, 2,3, 1,2),
+ (2,2, 3,3, 2,2),])
+
+
class InsertClash(TestBase):
@@ -63,8 +101,9 @@
[(0, 1), (1, 2)])
self.assertEquals(list(m3.find_sync_regions()),
- [(0, 1, 0, 1, 0, 1),
- (1, 2, 2, 3, 2, 3)])
+ [(0,1, 0,1, 0,1),
+ (1,2, 2,3, 2,3),
+ (2,2, 3,3, 3,3),])
@@ -79,8 +118,9 @@
[(0, 1), (2, 3)])
self.assertEquals(list(m3.find_sync_regions()),
- [(0, 1, 0, 1, 0, 1),
- (2, 3, 2, 3, 2, 3)])
+ [(0,1, 0,1, 0,1),
+ (2,3, 2,3, 2,3),
+ (3,3, 3,3, 3,3),])
@@ -96,8 +136,9 @@
self.assertEquals(list(m3.find_sync_regions()),
- [(0, 1, 0, 1, 0, 1),
- (3, 4, 4, 5, 5, 6)])
+ [(0,1, 0,1, 0,1),
+ (3,4, 4,5, 5,6),
+ (4,4, 5,5, 6,6),])
More information about the Pkg-bazaar-commits
mailing list