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

差分の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 に答える