|
| 1 | +<?php |
| 2 | +# Take unix/emacs reports as STDIN, such as: |
| 3 | +# foo/js/bar.js:503:55: Missing radix parameter. |
| 4 | +# And takes a single argument pointing to the file containing the output |
| 5 | +# of parse-diff-ranges.php, such as: |
| 6 | +# path/to/file.ext:123-456 |
| 7 | +# And then echos the line from STDIN if the path (here, foo/js/bar.hs) exists |
| 8 | +# in the latter, and the line number (here 503) is among the ranges represented. |
| 9 | +# If there are any matches, this script will return an exit code 1. |
| 10 | +# If there are no matches, the script will return exit code 0. |
| 11 | + |
| 12 | +if ( empty( $argv[1] ) ) { |
| 13 | + echo 'Missing argument for file containing output of parse-diff-ranges.php.'; |
| 14 | + exit( 2 ); |
| 15 | +} |
| 16 | +if ( ! file_exists( $argv[1] ) ) { |
| 17 | + echo 'Argument for file containing output of parse-diff-ranges.php does not exist.'; |
| 18 | + exit( 2 ); |
| 19 | +} |
| 20 | + |
| 21 | +$matched_patch_count = 0; |
| 22 | + |
| 23 | +$parsed_diff_ranges = array(); |
| 24 | +foreach ( explode( "\n", trim( file_get_contents( $argv[1] ) ) ) as $line ) { |
| 25 | + if ( preg_match( '/^(?P<file_path>.+):(?P<start_line>\d+)-(?P<end_line>\d+)$/', $line, $matches ) ) { |
| 26 | + $file_path = realpath( $matches['file_path'] ); |
| 27 | + if ( ! array_key_exists( $file_path, $parsed_diff_ranges ) ) { |
| 28 | + $parsed_diff_ranges[ $file_path ] = array(); |
| 29 | + } |
| 30 | + $parsed_diff_ranges[ $file_path ][] = array( |
| 31 | + 'start_line' => intval( $matches['start_line'] ), |
| 32 | + 'end_line' => intval( $matches['end_line'] ), |
| 33 | + ); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +while ( $line = fgets( STDIN ) ) { |
| 38 | + $matched = ( |
| 39 | + preg_match( '#^(?P<file_path>.+):(?P<line_number>\d+):\d+:.+$$#', $line, $matches ) |
| 40 | + || |
| 41 | + preg_match( '/^(?P<file_path>.+): line (?P<line_number>\d+),/', $line, $matches ) |
| 42 | + ); |
| 43 | + if ( ! $matched ) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + $file_path = realpath( $matches['file_path'] ); |
| 47 | + if ( ! array_key_exists( $file_path, $parsed_diff_ranges ) ) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $line_number = intval( $matches['line_number'] ); |
| 51 | + $matched = false; |
| 52 | + foreach ( $parsed_diff_ranges[ $file_path ] as $range ) { |
| 53 | + if ( $line_number >= $range['start_line'] && $line_number <= $range['end_line'] ) { |
| 54 | + $matched = true; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + if ( ! $matched ) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $matched_patch_count += 1; |
| 63 | + echo $line; |
| 64 | +} |
| 65 | + |
| 66 | +if ( $matched_patch_count > 0 ) { |
| 67 | + exit( 1 ); |
| 68 | +} else { |
| 69 | + exit( 0 ); |
| 70 | +} |
0 commit comments