|
| 1 | +<?php |
| 2 | +# +----------------------------------------------------------------------+ |
| 3 | +# | Copyright (c) 1997-2024 The PHP Group | |
| 4 | +# +----------------------------------------------------------------------+ |
| 5 | +# | This source file is subject to version 3.01 of the PHP license, | |
| 6 | +# | that is bundled with this package in the file LICENSE, and is | |
| 7 | +# | available through the world-wide-web at the following url: | |
| 8 | +# | https://www.php.net/license/3_01.txt. | |
| 9 | +# | If you did not receive a copy of the PHP license and are unable to | |
| 10 | +# | obtain it through the world-wide-web, please send a note to | |
| 11 | +# | [email protected], so we can mail you a copy immediately. | |
| 12 | +# +----------------------------------------------------------------------+ |
| 13 | +# | Authors: André L F S Bacci <ae php.net> | |
| 14 | +# +----------------------------------------------------------------------+ |
| 15 | +# | Description: Common functions that interact with git command line. | |
| 16 | +# +----------------------------------------------------------------------+ |
| 17 | + |
| 18 | +require_once __DIR__ . '/all.php'; |
| 19 | + |
| 20 | +class GitSlowUtils |
| 21 | +{ |
| 22 | + public static function checkDiffOnlyWsChange( string $gdir , RevcheckDataFile $file ) : bool |
| 23 | + { |
| 24 | + $hash = $file->hashRvtg; |
| 25 | + $flnm = $file->path == "" ? $file->name : $file->path . "/" . $file->name; |
| 26 | + |
| 27 | + $gdir = escapeshellarg( $gdir ); |
| 28 | + $flnm = escapeshellarg( $flnm ); |
| 29 | + $hash = escapeshellarg( $hash ); |
| 30 | + |
| 31 | + $func = '[' . __CLASS__ . ':' . __FUNCTION__ . ']'; |
| 32 | + |
| 33 | + // Fast path |
| 34 | + |
| 35 | + // The git -b option is a bit misleading. It will ignore ws change |
| 36 | + // on existing ws runs, but will report insertion or remotion of |
| 37 | + // ws runs. This suffices for detecting significant ws changes and |
| 38 | + // also ignoring insignificant ws changes in most cases we are |
| 39 | + // interessed. |
| 40 | + |
| 41 | + $output = `git -C $gdir diff -b $hash -- $flnm`; |
| 42 | + $onlyws = $output == ""; |
| 43 | + |
| 44 | + // Slow path |
| 45 | + |
| 46 | + if ( $onlyws ) |
| 47 | + { |
| 48 | + $prev = `git -C $gdir show $hash:$flnm )`; |
| 49 | + $next = `git -C $gdir show HEAD:$flnm )`; |
| 50 | + |
| 51 | + if ( $prev == "" || $next == "" ) |
| 52 | + { |
| 53 | + fprintf( STDERR , "$func Failed to read file contents.\n" ); |
| 54 | + return $onlyws; |
| 55 | + } |
| 56 | + |
| 57 | + $prev = GitUtils::discardPrefixSuffixEmptyWs( $prev ); |
| 58 | + $next = GitUtils::discardPrefixSuffixEmptyWs( $next ); |
| 59 | + |
| 60 | + if ( $prev != $next ) |
| 61 | + { |
| 62 | + // Not really an error, but a theory. Report this bug/issue |
| 63 | + // to start a discussion if this ws change must be ignored |
| 64 | + // or tracked. |
| 65 | + |
| 66 | + fprintf( STDERR , "$func Debug: Fast and slow path differ.\n" ); |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return $onlyws; |
| 72 | + } |
| 73 | + |
| 74 | + private static function discardPrefixSuffixEmptyWs( string $text ) : string |
| 75 | + { |
| 76 | + $lines = explode( "\n" , $text ); |
| 77 | + $trimLines = []; |
| 78 | + foreach ( $lines as $line ) |
| 79 | + $trimLines[] = trim( $line ); |
| 80 | + return implode( "" , $trimLines ); |
| 81 | + } |
| 82 | + |
| 83 | + public static function parseAddsDels( string $gdir , RevcheckDataFile $file ) |
| 84 | + { |
| 85 | + $hash = $file->hashRvtg; |
| 86 | + $name = $file->path == "" ? $file->name : $file->path . "/" . $file->name; |
| 87 | + |
| 88 | + $gdir = escapeshellarg( $gdir ); |
| 89 | + $hash = escapeshellarg( $hash ); |
| 90 | + $name = escapeshellarg( $name ); |
| 91 | + |
| 92 | + $output = `git -C $gdir diff --numstat $hash -- $name`; |
| 93 | + if ( $output ) |
| 94 | + { |
| 95 | + preg_match( '/(\d+)\s+(\d+)/' , $output , $matches ); |
| 96 | + if ( $matches ) |
| 97 | + { |
| 98 | + $file->adds = $matches[1]; |
| 99 | + $file->dels = $matches[2]; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments