比较php中的SVN修订文件

时间:2012-08-21 11:47:10

标签: php arrays svn

我还在使用在线SVN工具,这次又在diff计算时被卡住了。

我制作了一个测试文件test.txt,它在diff上提供了这个结果:

Index: C:/data/aaxc/test.txt
===================================================================
--- C:/data/aaxc/test.txt   (revision 8)
+++ C:/data/aaxc/test.txt   (working copy)
@@ -1,3 +1,5 @@
-Fully new line
+Fully new line 1
{2nd modified line}
Specia$ čhar līne
+
+Nice one!
\ No newline at end of file

之后,我正在创建一个数组:

$data = explode( "\n", $svn_result );

$result = array();
for ( $k=2; $k<sizeof($data); $k++ ) {

    # checks for filename
    if ( substr( $data[$k], 0, 3 ) == '---' ) $result['left'] = substr( $data[$k], 4 );
    else if ( substr( $data[$k], 0, 3 ) == '+++' ) $result['right'] = substr( $data[$k], 4 );

    # check for changes
    else if ( substr( $data[$k], 0, 1 ) == '-' ) $result['-'][] = substr( $data[$k], 1 );
    else if ( substr( $data[$k], 0, 1 ) == '+' ) $result['+'][] = substr( $data[$k], 1 );

}

输出:

Array
(
[left] => C:/data/aaxc/test.txt (revision 8)
[right] => C:/data/aaxc/test.txt    (working copy)
[-] => Array
    (
        [0] => Fully new line
    )

[+] => Array
    (
        [0] => Fully new line 1
        [1] => 
        [2] => Nice one!
    )

)

到目前为止一直很好,但是我现在怎样才能确定女巫线已经改变了什么?因为目前,当我试图强调这些变化时,我绝不可能确保它能正确地照亮它。

也许有一个脚本已经这样做了?

enter image description here

目前它在小变化方面表现良好,但绝对不会在大变化上失败。

1 个答案:

答案 0 :(得分:1)

您需要存储given on the 5th line of the diff的行位置,即以@@开头的行位置。

所以你可以这样做:

$data = explode( "\n", $svn_result );

$result = array();
for ( $k=2; $k<sizeof($data); $k++ ) {

    # checks for filename
    if ( substr( $data[$k], 0, 3 ) == '---' ) {
        $result['left'] = substr( $data[$k], 4 );
    } else if ( substr( $data[$k], 0, 3 ) == '+++' ) {
        $result['right'] = substr( $data[$k], 4 );

    # stores line starting positions
    } else if ( substr( $data[$k], 0, 2 ) == '@@') {
        // Remove @ symbols and trim whitespace
        $diff_line_nums = explode( ' ', trim( str_replace( '@@', '', $data[$k] ) ) );
        // Split by the comma
        $left_nums = explode( ',', $diff_line_nums[0] );
        $left_diff = array('line' => abs( $left_nums[0] ) ,
                                'length' => $left_nums[1] );
        // Set the counter for this specific part of the diff back to 0
        $left_line_count = 0;

        // Split by the comma
        $right_nums = explode( ',', $diff_line_nums[1] );
        $right_diff = array('line' => abs( $right_nums[0] ) ,
                                 'length' => $right_nums[1] );
        // Set the counter for this specific part of the diff back to 0
        $right_line_count = 0;

    # check for changes
    } else if ( substr( $data[$k], 0, 1 ) == '-' ) {
        $result['-'][ $left_diff['line'] + $left_line_count ] = substr( $data[$k], 1 );
        $left_line_count++;
    } else if ( substr( $data[$k], 0, 1 ) == '+' ) {
        $result['+'][ $right_diff['line'] + $right_line_count ] = substr( $data[$k], 1 );
        $right_line_count++;

    // Otherwise assume there is no difference, so increment both left 
    // and right line counters
    } else {
        $right_line_count++;
        $left_line_count++;
    }
}

这将为您提供如下输出:

array (
    'left' => 'C:/data/aaxc/test.txt   (revision 8)',
    'right' => 'C:/data/aaxc/test.txt   (working copy)',
    '-' => array (
            1 => 'Fully new line'
           ),
    '+' => array (
            1 => 'Fully new line 1',
            4 => '', 
            5 => 'Nice one!'
           )
)

然后当您遍历$result数组时,您将知道索引键已对哪些行进行了更改:

foreach ($result['-'] as $line_number => $change) {
    // display removal changes
}
foreach ($result['+'] as $line_number => $change) {
    // display insert changes
}