protected function changeRepo($cachePath, $repo, $branch, $repoFilename, $name, $email, $comment, $callback)
{
$temporaryDirectory = '';
$tempRepo = '';
$hadError = false;
$command = '';
$output = '';
$outputs = [];
$trace = null;
try {
$temporaryDirectory = (new TemporaryDirectory($cachePath))->create();
$client = $this->getClient();
$repoPath = realpath($this->getPath());
$tempRepo = $temporaryDirectory->path();
$output = $client->run($this, 'clone ' . $repoPath . ' ' . $tempRepo);
$this->setPath($tempRepo);
$normalizedRepoFilePath = $this->isValidPath($tempRepo, $repoFilename, $outputs);
$filename = realpath($tempRepo . DIRECTORY_SEPARATOR . $repoFilename);
$command = "checkout $branch";
$output = $client->run($this, $command);
array_push($outputs, $output);
$message = $callback($client, $filename, $outputs, $tempRepo, $normalizedRepoFilePath);
array_push($outputs, $message);
$command = " -c \"user.name=$name\" -c \"user.email=$email\" commit -am \"$comment\" ";
$output = $client->run($this, $command);
array_push($outputs, $output);
// $command = "commit -am \"$comment\"";
// $output = $client->run($repository, $command);
$command = "push";
$output = $client->run($this, $command);
array_push($outputs, $output);
$command = " rev-parse HEAD ";
$lastCommit = $client->run($this, $command);
$result = (object)[
'status' => 'ok',
'output' => $message,
'outputs' => $outputs,
'last-commit' => $lastCommit,
'branch' => $branch,
];
return $result;
} catch (\Throwable $e) {
$hadError = $e;
} finally {
if ($temporaryDirectory !== '') {
@$temporaryDirectory->delete();
}
if ($hadError !== false) {
$message = $hadError->getMessage();
if ($message === '') {
$exceptionName = get_class($hadError);
$message = "Received exception without message with type '{$exceptionName}'. " . $hadError->getMessage();
$trace = $hadError->getTrace();
}
return ((object)[
'status' => $message === '' ? 'ok' : 'error',
'error' => $message === '' ? false : true,
//'temporaryDirectory' => $tempRepo,
'message' => $message,
//'currentdir' => getcwd(),
//'command' => $command,
'output' => $output,
'outputs' => $outputs,
'trace' => $trace,
//'$filename' => $filename,
//'$value' => $value,
]);
}
}
}
protected function isValidPath($tempRepo, $repoFilename, &$outputs)
{
$basePath = FileSystemPath::fromString($tempRepo);
//array_push($outputs, "$basePath {$basePath}");
$repoFilenameItem = FileSystemPath::fromString($repoFilename);
//array_push($outputs, "$repoFilenameItem {$repoFilenameItem}");
$normalizing = $basePath->resolve($repoFilenameItem);
//array_push($outputs, "$normalizing {$normalizing}");
// array_unshift($outputs, '$normalizing: ' . $normalizing);
$normalized = FileSystemPath::fromString($normalizing)->normalize();
//array_push($outputs, "$normalized {$normalized}");
// array_unshift($outputs, '$normalized: ' . $normalized);
// array_unshift($outputs, '$normalizing type: ' . gettype($normalizing));
/// array_unshift($outputs, '$normalized type: ' . gettype($normalized));
$normalizedRepoFilePath = $normalized->__toString();
$validPath = strpos($normalizing->__toString(), $normalizedRepoFilePath) !== false;
//array_push($outputs, "$normalizing {$normalizing->__toString()}");
//array_push($outputs, "$normalizedRepoFilePath {$normalizedRepoFilePath}");
//array_push($outputs, (substr($normalizedRepoFilePath, 0, strlen($basePath)) . " $basePath"));
if ($validPath === false || substr($normalizedRepoFilePath, 0, strlen($basePath)) != $basePath) {
throw new \Exception("This '{$repoFilename}' path is invalid.");
}
return $normalizedRepoFilePath;
}