1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
<?php
/**
* +----------------------------------------------------------------------+
* | Copyright (c) 1997-2023 The PHP Group |
* +----------------------------------------------------------------------+
* | This source file is subject to version 3.01 of the PHP license, |
* | that is bundled with this package in the file LICENSE, and is |
* | available through the world-wide-web at the following url: |
* | https://www.php.net/license/3_01.txt. |
* | If you did not receive a copy of the PHP license and are unable to |
* | obtain it through the world-wide-web, please send a note to |
* | license@php.net, so we can mail you a copy immediately. |
* +----------------------------------------------------------------------+
* | Authors: André L F S Bacci <ae php.net> |
* +----------------------------------------------------------------------+
* | Description: Parse `git log` to complement file state. |
* +----------------------------------------------------------------------+
*/
require_once __DIR__ . '/all.php';
class GitLogParser
{
static function parseInto( string $lang , RevcheckFileList & $list )
{
$cwd = getcwd();
chdir( $lang );
$fp = popen( "git log --name-only" , "r" );
chdir( $cwd );
$hash = "";
$date = "";
$skip = false;
$mcnt = 0;
while ( ( $line = fgets( $fp ) ) !== false )
{
// new commit block
if ( substr( $line , 0 , 7 ) == "commit " )
{
$hash = trim( substr( $line , 7 ) );
$date = "";
$skip = false;
$mcnt = 0;
continue;
}
// datetime of commit
if ( strpos( $line , 'Date:' ) === 0 )
{
$line = trim( substr( $line , 5 ) );
$date = strtotime( $line );
continue;
}
// empty lines
if ( trim( $line ) == "" )
continue;
// commit message
if ( str_starts_with( $line , ' ' ) )
{
if ( LOOSE_SKIP_REVCHECK ) // See below, and https://github.com/php/doc-base/pull/132
{
// commits with [skip-revcheck] anywhere commit message flags skip
if ( str_contains( $line, '[skip-revcheck]' ) )
$skip = true;
}
else
{
$mcnt++;
// [skip-revcheck] at start of first line of commit message flags a skip
if ( $mcnt == 1 && str_starts_with( trim( $line ) , '[skip-revcheck]' ) )
$skip = true;
}
continue;
}
// other headers
if ( strpos( $line , ': ' ) > 0 )
continue;
// otherwise, a filename
$filename = trim( $line );
$info = $list->get( $filename );
// untracked file (deleted, renamed)
if ( $info == null )
continue;
$info->addGitLogData( $hash , $date , $skip );
}
pclose( $fp );
}
static function parseDir( string $gdir , RevcheckFileList $list )
{
$gdir = escapeshellarg( $gdir );
$proc = new GitLogParserProc( "git -C $gdir log --name-only" );
$hash = "";
$date = "";
$skip = false;
$lcnt = 0;
while ( $proc->live )
{
// Hash
if ( str_starts_with( $proc->line , "commit " ) )
{
$hash = trim( substr( $proc->line , 7 ) );
$date = "";
$skip = false;
$lcnt = 0;
$proc->next();
}
else
throw new Exception( "Expected commit hash." );
// Headers
while ( $proc->live && strlen( trim( $proc->line ) ) > 0 )
{
// Date
if ( str_starts_with( $proc->line , 'Date:' ) )
{
$line = trim( substr( $proc->line , 5 ) );
$date = strtotime( $line );
$proc->next();
continue;
}
// Other headers
if ( $proc->line[0] != ' ' && strpos( $proc->line , ':' ) > 0 )
{
$proc->next();
continue;
}
break;
}
$proc->skip(); // Empty Line
// Message
while ( $proc->live && str_starts_with( $proc->line , ' ' ) )
{
if ( LOOSE_SKIP_REVCHECK ) // https://github.com/php/doc-base/pull/132
{
// Messages that contains [skip-revcheck] flags entire commit as ignored.
if ( str_contains( $proc->line , '[skip-revcheck]' ) )
$skip = true;
}
else
{
// Messages that start with [skip-revcheck] flags entire commit as ignored.
$lcnt++;
if ( $lcnt == 1 && str_starts_with( trim( $line ) , '[skip-revcheck]' ) )
$skip = true;
}
$proc->next();
}
$proc->skip(); // Empty Line
// Merge commits and empty files commits
// Merge commits are not followed with file listings.
// Some normal commits also not have file listings
// (see b73609198d4606621f57e165efc457f30e403217).
if ( str_starts_with( $proc->line , "commit " ) )
continue;
// Files
while ( $proc->live && strlen( trim( $proc->line ) ) > 0 )
{
$file = $list->get( trim( $proc->line ) );
if ( $file != null )
$file->addGitLogData( $hash , $date , $skip );
$proc->next();
}
$proc->skip(); // Empty Line
}
}
}
class GitLogParserProc
{
public bool $live;
public string $line;
private $proc = null;
function __construct( string $command )
{
$this->proc = popen( $command , "r" );
$this->live = true;
$this->next();
}
function next()
{
if ( $this->proc == null )
return;
$ret = fgets( $this->proc );
if ( $ret === false )
$this->stop();
else
$this->line = $ret;
}
function skip()
{
if ( trim( $this->line ) != "" )
throw new Exception( "Skipping non-blank line." );
$this->next();
}
function stop()
{
pclose( $this->proc );
$this->live = false;
$this->line = "";
$this->proc = null;
}
}
|