peerdrive/client/views/diff3.py
def merge(old, other, new, allow_conflicts=1,
          marker1='<<<<<<<<<<<<<<<<<<<<<<<<<\n',
          marker2='=========================\n',
          marker3='>>>>>>>>>>>>>>>>>>>>>>>>>\n'):
    """ do line by line diff3 merge
        input must be lists containing single lines
    """
    old_nr, other_nr, new_nr = 0, 0, 0
    old_len, other_len, new_len = len(old), len(other), len(new)
    result = []
    while old_nr < old_len and other_nr < other_len  and new_nr < new_len:
        # unchanged
        if old[old_nr] == other[other_nr] == new[new_nr]:
            result.append(old[old_nr])
            old_nr += 1
            other_nr += 1
            new_nr += 1
        else:
            new_match = find_match(old, new, old_nr, new_nr)
            other_match = find_match(old, other, old_nr, other_nr)
            # new is changed
            if new_match != (old_nr, new_nr):
                new_changed_lines = new_match[0] - old_nr
                # other is unchanged
                if match(old, other, old_nr, other_nr,
                         new_changed_lines) == new_changed_lines:
                    result.extend(new[new_nr:new_match[1]])
                    old_nr = new_match[0]
                    new_nr = new_match[1]
                    other_nr += new_changed_lines
                # both changed, conflict!
                else:
                    if not allow_conflicts: return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m
            # other is changed
            else:
                other_changed_lines = other_match[0] - other_nr
                # new is unchanged
                if match(old, new, old_nr, new_nr,
                         other_changed_lines) == other_changed_lines:
                    result.extend(other[other_nr:other_match[1]])
                    old_nr = other_match[0]
                    other_nr = other_match[1]
                    new_nr += other_changed_lines
                # both changed, conflict!
                else:
                    if not allow_conflicts: return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m

    # process tail
    # all finished
    if old_nr==old_len and other_nr==other_len  and new_nr==new_len:
        pass
    # new added lines
    elif old_nr==old_len and other_nr==other_len:
        result.extend(new[new_nr:])
    # other added lines
    elif old_nr==old_len and new_nr==new_len:
        result.extend(other[other_nr])
    # new deleted lines
    elif (new_nr==new_len and (old_len-old_nr==other_len-other_nr) and
          match(old, other, old_nr, other_nr, old_len-old_nr) ==
          old_len-old_nr):
        pass
    # other deleted lines
    elif (other_nr==other_len and (old_len-old_nr==new_len-new_nr) and
          match(old, new, old_nr, new_nr, old_len-old_nr) ==
          old_len-old_nr):
        pass
    # conflict
    else:
        if not allow_conflicts: return None
        result.append(marker1)
        result.extend(other[other_nr:])
        result.append(marker2)
        result.extend(new[new_nr:])
        result.append(marker3)
    return result
 do line by line diff3 merge
        input must be lists containing single lines
    
do line by line diff3 merge
input must be lists containing single lines
do line by line diff3 merge input must be lists containing single lines
def merge(old, other, new, allow_conflicts=1,
          marker1='<<<<<<<<<<<<<<<<<<<<<<<<<\n',
          marker2='=========================\n',
          marker3='>>>>>>>>>>>>>>>>>>>>>>>>>\n'):
    old_nr, other_nr, new_nr = 0, 0, 0
    old_len, other_len, new_len = len(old), len(other), len(new)
    result = []
    while old_nr < old_len and other_nr < other_len  and new_nr < new_len:
        if old[old_nr] == other[other_nr] == new[new_nr]:
            result.append(old[old_nr])
            old_nr += 1
            other_nr += 1
            new_nr += 1
        else:
            new_match = find_match(old, new, old_nr, new_nr)
            other_match = find_match(old, other, old_nr, other_nr)
            if new_match != (old_nr, new_nr):
                new_changed_lines = new_match[0] - old_nr
                if match(old, other, old_nr, other_nr,
                         new_changed_lines) == new_changed_lines:
                    result.extend(new[new_nr:new_match[1]])
                    old_nr = new_match[0]
                    new_nr = new_match[1]
                    other_nr += new_changed_lines
                else:
                    if not allow_conflicts: return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m
            else:
                other_changed_lines = other_match[0] - other_nr
                if match(old, new, old_nr, new_nr,
                         other_changed_lines) == other_changed_lines:
                    result.extend(other[other_nr:other_match[1]])
                    old_nr = other_match[0]
                    other_nr = other_match[1]
                    new_nr += other_changed_lines
                else:
                    if not allow_conflicts: return None
                    old_m, other_m, new_m = tripple_match(
                        old, other, new, other_match, new_match)
                    result.append(marker1)
                    result.extend(other[other_nr:other_m])
                    result.append(marker2)
                    result.extend(new[new_nr:new_m])
                    result.append(marker3)
                    old_nr, other_nr, new_nr = old_m, other_m, new_m
    if old_nr==old_len and other_nr==other_len  and new_nr==new_len:
        pass
    elif old_nr==old_len and other_nr==other_len:
        result.extend(new[new_nr:])
    elif old_nr==old_len and new_nr==new_len:
        result.extend(other[other_nr])
    elif (new_nr==new_len and (old_len-old_nr==other_len-other_nr) and
          match(old, other, old_nr, other_nr, old_len-old_nr) ==
          old_len-old_nr):
        pass
    elif (other_nr==other_len and (old_len-old_nr==new_len-new_nr) and
          match(old, new, old_nr, new_nr, old_len-old_nr) ==
          old_len-old_nr):
        pass
    else:
        if not allow_conflicts: return None
        result.append(marker1)
        result.extend(other[other_nr:])
        result.append(marker2)
        result.extend(new[new_nr:])
        result.append(marker3)
    return result
def merge ( old , other , new , allow_conflicts = 1 , marker1 = '<<<<<<<<<<<<<<<<<<<<<<<<<\n' , marker2 = '=========================\n' , marker3 = '>>>>>>>>>>>>>>>>>>>>>>>>>\n' ) : old_nr , other_nr , new_nr = 0 , 0 , 0 old_len , other_len , new_len = len ( old ) , len ( other ) , len ( new ) result = [ ] while old_nr < old_len and other_nr < other_len and new_nr < new_len : if old [ old_nr ] == other [ other_nr ] == new [ new_nr ] : result . append ( old [ old_nr ] ) old_nr += 1 other_nr += 1 new_nr += 1 else : new_match = find_match ( old , new , old_nr , new_nr ) other_match = find_match ( old , other , old_nr , other_nr ) if new_match != ( old_nr , new_nr ) : new_changed_lines = new_match [ 0 ] - old_nr if match ( old , other , old_nr , other_nr , new_changed_lines ) == new_changed_lines : result . extend ( new [ new_nr : new_match [ 1 ] ] ) old_nr = new_match [ 0 ] new_nr = new_match [ 1 ] other_nr += new_changed_lines else : if not allow_conflicts : return None old_m , other_m , new_m = tripple_match ( old , other , new , other_match , new_match ) result . append ( marker1 ) result . extend ( other [ other_nr : other_m ] ) result . append ( marker2 ) result . extend ( new [ new_nr : new_m ] ) result . append ( marker3 ) old_nr , other_nr , new_nr = old_m , other_m , new_m else : other_changed_lines = other_match [ 0 ] - other_nr if match ( old , new , old_nr , new_nr , other_changed_lines ) == other_changed_lines : result . extend ( other [ other_nr : other_match [ 1 ] ] ) old_nr = other_match [ 0 ] other_nr = other_match [ 1 ] new_nr += other_changed_lines else : if not allow_conflicts : return None old_m , other_m , new_m = tripple_match ( old , other , new , other_match , new_match ) result . append ( marker1 ) result . extend ( other [ other_nr : other_m ] ) result . append ( marker2 ) result . extend ( new [ new_nr : new_m ] ) result . append ( marker3 ) old_nr , other_nr , new_nr = old_m , other_m , new_m if old_nr == old_len and other_nr == other_len and new_nr == new_len : pass elif old_nr == old_len and other_nr == other_len : result . extend ( new [ new_nr : ] ) elif old_nr == old_len and new_nr == new_len : result . extend ( other [ other_nr ] ) elif ( new_nr == new_len and ( old_len - old_nr == other_len - other_nr ) and match ( old , other , old_nr , other_nr , old_len - old_nr ) == old_len - old_nr ) : pass elif ( other_nr == other_len and ( old_len - old_nr == new_len - new_nr ) and match ( old , new , old_nr , new_nr , old_len - old_nr ) == old_len - old_nr ) : pass else : if not allow_conflicts : return None result . append ( marker1 ) result . extend ( other [ other_nr : ] ) result . append ( marker2 ) result . extend ( new [ new_nr : ] ) result . append ( marker3 ) return result
do line by line diff3 merge
input must be lists containing single lines
do line by line diff3 merge input must be lists containing single lines
""" do line by line diff3 merge
        input must be lists containing single lines
    """ # unchanged # new is changed # other is unchanged # both changed, conflict! # other is changed # new is unchanged # both changed, conflict! # process tail # all finished # new added lines # other added lines # new deleted lines # other deleted lines # conflict
old
other
new
allow_conflicts
marker1
marker2
marker3