From a0e0d8189dc522fbb5b4ff816a80bc6303e38447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20L=20F=20S=20Bacci?= Date: Mon, 18 Nov 2024 07:50:03 -0300 Subject: [PATCH] Rewrite git log parser (precise parsing) --- scripts/translation/lib/GitSlowUtils.php | 103 +++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 scripts/translation/lib/GitSlowUtils.php diff --git a/scripts/translation/lib/GitSlowUtils.php b/scripts/translation/lib/GitSlowUtils.php new file mode 100644 index 000000000..8dd20ddcc --- /dev/null +++ b/scripts/translation/lib/GitSlowUtils.php @@ -0,0 +1,103 @@ + | +# +----------------------------------------------------------------------+ +# | Description: Common functions that interact with git command line. | +# +----------------------------------------------------------------------+ + +require_once __DIR__ . '/all.php'; + +class GitSlowUtils +{ + public static function checkDiffOnlyWsChange( string $gdir , RevcheckDataFile $file ) : bool + { + $hash = $file->hashRvtg; + $flnm = $file->path == "" ? $file->name : $file->path . "/" . $file->name; + + $gdir = escapeshellarg( $gdir ); + $flnm = escapeshellarg( $flnm ); + $hash = escapeshellarg( $hash ); + + $func = '[' . __CLASS__ . ':' . __FUNCTION__ . ']'; + + // Fast path + + // The git -b option is a bit misleading. It will ignore ws change + // on existing ws runs, but will report insertion or remotion of + // ws runs. This suffices for detecting significant ws changes and + // also ignoring insignificant ws changes in most cases we are + // interessed. + + $output = `git -C $gdir diff -b $hash -- $flnm`; + $onlyws = $output == ""; + + // Slow path + + if ( $onlyws ) + { + $prev = `git -C $gdir show $hash:$flnm )`; + $next = `git -C $gdir show HEAD:$flnm )`; + + if ( $prev == "" || $next == "" ) + { + fprintf( STDERR , "$func Failed to read file contents.\n" ); + return $onlyws; + } + + $prev = GitUtils::discardPrefixSuffixEmptyWs( $prev ); + $next = GitUtils::discardPrefixSuffixEmptyWs( $next ); + + if ( $prev != $next ) + { + // Not really an error, but a theory. Report this bug/issue + // to start a discussion if this ws change must be ignored + // or tracked. + + fprintf( STDERR , "$func Debug: Fast and slow path differ.\n" ); + return false; + } + } + + return $onlyws; + } + + private static function discardPrefixSuffixEmptyWs( string $text ) : string + { + $lines = explode( "\n" , $text ); + $trimLines = []; + foreach ( $lines as $line ) + $trimLines[] = trim( $line ); + return implode( "" , $trimLines ); + } + + public static function parseAddsDels( string $gdir , RevcheckDataFile $file ) + { + $hash = $file->hashRvtg; + $name = $file->path == "" ? $file->name : $file->path . "/" . $file->name; + + $gdir = escapeshellarg( $gdir ); + $hash = escapeshellarg( $hash ); + $name = escapeshellarg( $name ); + + $output = `git -C $gdir diff --numstat $hash -- $name`; + if ( $output ) + { + preg_match( '/(\d+)\s+(\d+)/' , $output , $matches ); + if ( $matches ) + { + $file->adds = $matches[1]; + $file->dels = $matches[2]; + } + } + } +}