Ejemplo n.º 1
0
def _parse_hunk(diff, source_start, source_len, target_start, target_len):
    hunk = Hunk(source_start, source_len, target_start, target_len)
    modified = 0
    deleting = 0
    for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if valid_line:
            action = valid_line.group(0)
            original_line = line[1:]
            if action == LINE_TYPE_ADD:
                hunk.append_added_line(original_line)
                # modified lines == deleted immediately followed by added
                if deleting > 0:
                    modified += 1
                    deleting -= 1
            elif action == LINE_TYPE_DELETE:
                hunk.append_deleted_line(original_line)
                deleting += 1
            elif action == LINE_TYPE_CONTEXT:
                hunk.append_context_line(original_line)
                hunk.add_to_modified_counter(modified)
                # reset modified auxiliar variables
                deleting = 0
                modified = 0
        else:
            raise UnidiffParseException('Hunk diff data expected')

        # check hunk len(old_lines) and len(new_lines) are ok
        if hunk.is_valid():
            break

    return hunk
Ejemplo n.º 2
0
def _parse_hunk(diff, source_start, source_len, target_start, target_len):
    hunk = Hunk(source_start, source_len, target_start, target_len)
    modified = 0
    deleting = 0
    for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if valid_line:
            action = valid_line.group(0)
            original_line = line[1:]
            if action == '+':
                hunk.append_added_line(original_line)
                # modified lines == deleted immediately followed by added
                if deleting > 0:
                    modified += 1
                    deleting -= 1
            elif action == '-':
                hunk.append_deleted_line(original_line)
                deleting += 1
               
            # If invoked with 'diff -u0 file_a_name file_b_name', then modified lines always keep to 0
            # In theory, there can be combining only deleting lines and adding lines in diff files.
            # 'diff -u0 file_a_name file_b_name' --> generate a unified diff whith no contexts
            elif action == ' ':
                hunk.append_context_line(original_line)
                hunk.add_to_modified_counter(modified)
                # reset modified auxiliar variables
                deleting = 0
                modified = 0
        else:
            raise UnidiffParseException('Hunk diff data expected')

        # check hunk len(old_lines) and len(new_lines) are ok
        if hunk.is_valid():
            break

    return hunk
Ejemplo n.º 3
0
def _parse_hunk(diff, source_start, source_len, target_start, target_len):
    hunk = Hunk(source_start, source_len, target_start, target_len)
    modified = 0
    deleting = 0
    for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if valid_line:
            action = valid_line.group(0)
            original_line = line[1:]
            if action == '+':
                hunk.append_added_line(original_line)
                # modified lines == deleted immediately followed by added
                if deleting > 0:
                    modified += 1
                    deleting -= 1
            elif action == '-':
                hunk.append_deleted_line(original_line)
                deleting += 1
            elif action == ' ':
                hunk.append_context_line(original_line)
                hunk.add_to_modified_counter(modified)
                # reset modified auxiliar variables
                deleting = 0
                modified = 0
        else:
            raise UnidiffParseException('Hunk diff data expected')

        # check hunk len(old_lines) and len(new_lines) are ok
        if hunk.is_valid():
            break

    return hunk