15 php -f get_github_changes.php --
16 --
output-file=
"<output_file>" 17 --base-path=
"<base_path>" 20 [--file-extensions=
"<comma_separated_list_of_formats>"]
25 $options = getopt(
'', [
'output-file:',
'base-path:',
'repo:',
'file-extensions:',
'branch:']);
35 include_once
__DIR__ .
'/framework/autoload.php';
67 foreach (
$repo->getChangedContentFiles() as $key => $changedContentFile) {
68 $filePath = sprintf($changedFilesContentFileName, $key);
70 $oldData = json_decode($oldContent,
true);
71 $data = array_merge($oldData, $changedContentFile);
85 $changedFilesList =
fopen($outputFile,
'w');
87 fwrite($changedFilesList, $file . PHP_EOL);
89 fclose($changedFilesList);
102 $branchOutputFile = str_replace(
'changed_files',
'branches', $outputFile);
103 $branchesList =
fopen($branchOutputFile,
'w');
104 fwrite($branchesList, $branchName . PHP_EOL);
106 fwrite($branchesList, substr(strrchr($branch,
'/'), 1) . PHP_EOL);
108 fclose($branchesList);
123 $isFileExension = strpos(
$fileName,
'.' . $extensions);
124 if ($isFileExension) {
192 if (!isset(
$options[$requiredOption]) || empty(
$options[$requiredOption])) {
217 private $remoteList = [];
229 private $changedContentFiles = [];
236 if (empty($workTree) || !
is_dir($workTree)) {
237 throw new UnexpectedValueException(
'Working tree should be a valid path to directory');
239 $this->workTree = $workTree;
250 if (isset($this->remoteList[
$alias])) {
255 $this->call(sprintf(
'remote add %s %s',
$alias,
$url));
265 if (isset($this->remoteList[
$alias])) {
266 $this->call(sprintf(
'remote rm %s',
$alias));
267 unset($this->remoteList[
$alias]);
276 public function fetch($remoteAlias)
278 if (!isset($this->remoteList[$remoteAlias])) {
279 throw new LogicException(
'Alias "' . $remoteAlias .
'" is not defined');
282 $this->call(sprintf(
'fetch %s', $remoteAlias));
306 public function compareChanges($remoteAlias, $remoteBranch, $changesType = self::CHANGE_TYPE_ALL)
308 if (!isset($this->remoteList[$remoteAlias])) {
309 throw new LogicException(
'Alias "' . $remoteAlias .
'" is not defined');
312 $result = $this->call(sprintf(
'log %s/%s..HEAD --name-status --oneline', $remoteAlias, $remoteBranch));
337 $changesType = self::CHANGE_TYPE_ALL
339 $countScannedFiles = 0;
340 $changedFilesMasks = $this->buildChangedFilesMask($changesType);
341 $filteredChanges = [];
343 $countScannedFiles++;
344 if (($countScannedFiles % 5000) == 0) {
345 echo $countScannedFiles .
" files scanned so far\n";
348 $changeTypeMask = $this->detectChangeTypeMask(
$fileName, $changedFilesMasks);
349 if (
null === $changeTypeMask) {
354 if (in_array(
$fileName, $filteredChanges)) {
358 $fileChanges = $this->getFileChangeDetails(
$fileName, $remoteAlias, $remoteBranch);
359 if (empty($fileChanges)) {
363 if (!(isset($this->changedContentFiles[
$fileName]))) {
364 $this->setChangedContentFile($fileChanges,
$fileName);
368 echo $countScannedFiles .
" files scanned\n";
370 return $filteredChanges;
379 private function buildChangedFilesMask(
int $changesType): array
381 $changedFilesMasks = [];
383 self::CHANGE_TYPE_ADDED =>
"A\t",
384 self::CHANGE_TYPE_MODIFIED =>
"M\t",
385 ] as $changeType => $changedFilesMask) {
386 if ($changeType & $changesType) {
387 $changedFilesMasks[] = $changedFilesMask;
390 return $changedFilesMasks;
401 private function detectChangeTypeMask(
string $changeRecord, array $allowedMasks)
403 foreach ($allowedMasks as
$mask) {
404 if (strpos($changeRecord,
$mask) === 0) {
419 private function getFileChangeDetails(
string $fileName,
string $remoteAlias,
string $remoteBranch): array
427 'diff HEAD %s/%s -- %s',
446 $changedContent =
'';
450 if (strpos(
$item,
'---') !== 0 && strpos(
$item,
'-') === 0 && $line = ltrim(
$item,
'-')) {
451 $changedContent .= $line .
"\n";
454 if ($changedContent !==
'') {
466 return $this->changedContentFiles;
475 private function call($command)
478 'git --git-dir %s --work-tree %s',
479 escapeshellarg(
"{$this->workTree}/.git"),
480 escapeshellarg($this->workTree)
482 $tmp = sprintf(
'%s %s', $gitCmd, $command);
const CHANGED_FILES_CONTENT_FILE
retrieveNewFilesAcrossForks($mainline, GitRepo $repo, $branchName)
output($string, $level=INFO, $label='')
cleanup($repo, $mainline)
getBranches($source='--all')
const CHANGE_TYPE_MODIFIED
exec($command, array &$output=null, &$return_var=null)
compareChanges($remoteAlias, $remoteBranch, $changesType=self::CHANGE_TYPE_ALL)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
validateInput(array $options, array $requiredOptions)
saveChangedFileContent(GitRepo $repo)
getRepo($options, $mainline)
retrieveChangesAcrossForks($mainline, GitRepo $repo, $branchName)
if(!validateInput($options, $requiredOptions)) $fileExtensions
generateBranchesList($outputFile, $branches, $branchName)
filterChangedFiles(array $changes, $remoteAlias, $remoteBranch, $changesType=self::CHANGE_TYPE_ALL)
foreach($appDirs as $dir) $files
static getFileExtension($fileName)
generateChangedFilesList($outputFile, $changedFiles)
getChangedFiles(array $changes, array $fileExtensions)