2

我仍在研究在线 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!
    )

)

到目前为止一切顺利,但我现在如何确保女巫路线已更改以及更改为什么?因为目前,当我尝试突出显示更改时,我无法确定它会正确突出显示它。

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

在此处输入图像描述

目前它在小的变化上运行良好,但在大的变化上肯定会失败。

4

1 回答 1

1

您需要存储在diff 的第 5 行给出的行位置,即以 .开头的行位置@@

所以你可以这样做:

$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
}
于 2012-08-21T13:59:19.830 回答