Skip to content

Commit

Permalink
Merge pull request #15 from fr3nch13/dev
Browse files Browse the repository at this point in the history
Remove unused Helpers, add conditions for MySQL.
  • Loading branch information
fr3nch13 authored Jun 22, 2023
2 parents baac59c + c389ef3 commit fa3ffae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
9 changes: 9 additions & 0 deletions config/Migrations/00000000000001_InitialMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class InitialMigration extends AbstractMigration
*/
public function change(): void
{
if ($this->getAdapter()->getAdapterType() == 'mysql') {
$this->execute('SET UNIQUE_CHECKS = 0;');
$this->execute('SET FOREIGN_KEY_CHECKS = 0;');
}

$table = $this->table('books');
$table->addColumn('name', 'string', ['length' => '255'])
Expand Down Expand Up @@ -47,5 +51,10 @@ public function change(): void
->addColumn('student_id', 'integer')
->addColumn('grade', 'integer')
->create();

if ($this->getAdapter()->getAdapterType() == 'mysql') {
$this->execute('SET FOREIGN_KEY_CHECKS = 1;');
$this->execute('SET UNIQUE_CHECKS = 1;');
}
}
}
45 changes: 26 additions & 19 deletions src/View/Helper/VersionsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@
*
* Uses the Composer Lock Parser to get detailed version onformation from the composer.lock file.
* It also uses git to try to determine the version of the application itself.
*
* @property \Cake\View\Helper\UrlHelper $Url
* @property \Cake\View\Helper\HtmlHelper $Html
*/
class VersionsHelper extends Helper
{
/**
* List of loaded helpers.
*
* @var array<int|string, mixed>
*/
public $helpers = ['Url', 'Html'];

/**
* Contains the loaded composer info.
*
Expand Down Expand Up @@ -293,15 +283,19 @@ public function getRootDir(): string
* Runs the git command with the args
*
* @param array<string> $args List of arguments to pass to the git command
* @param bool $addSafe If git complains about a "fatal: detected dubious ownership in repository at" error, add it.
* @return array<int, string> The result of the git command
* @throws \Fr3nch13\Utilities\Exception\UtilitiesException if the git command fails.
*/
public function runGit(array $args = []): array
public function runGit(array $args = [], bool $addSafe = false): array
{
if (!trim($this->gitCmd)) {
throw new UtilitiesException(__('Empty git command.'), 404);
}
$cmd = 'cd ' . $this->getRootDir() . '; ';
if ($addSafe) {
$cmd .= 'git config --global --add safe.directory ' . $this->getRootDir() . '; ';
}
$cmd .= $this->gitCmd . ' ' . implode(' ', $args);
$cmd .= ' 2>&1';
$output = [];
Expand All @@ -310,14 +304,27 @@ public function runGit(array $args = []): array
$last_line = $this->exec($cmd, $output, $result_code);

if ($result_code) {
/** @var string $msg */
$msg = json_encode([
'message' => 'Command failed',
'cmd' => $cmd,
'code' => $result_code,
'output' => implode(' ', $output) . ' ' . $last_line,
]);
throw new UtilitiesException($msg, $result_code);
$output = implode(' ', $output) . ' ' . $last_line;
if (
!$addSafe &&
$result_code == 128 &&
stripos($output, 'fatal: detected dubious ownership in repository') !== false
) {
$output = $this->runGit($args, true);
} else {
if (stripos($output, 'fatal: detected dubious ownership in repository') !== false) {
$output .= ' (hint: the repository needs to be owned by the user executing the git command.)';
}

/** @var string $msg */
$msg = json_encode([
'message' => 'Command failed',
'cmd' => $cmd,
'code' => $result_code,
'output' => $output,
]);
throw new UtilitiesException($msg, $result_code);
}
}
// trim the results
foreach ($output as $i => $value) {
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/View/Helper/VersionsHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public function testRunGit(): void
}
}

public function testRunGitWithAddSafe(): void
{
$results = $this->Versions->runGit(['branch'], true);
if (count($results) == 1) {
$this->assertMatchesRegularExpression('/^\*\s+([\(\)\w\s\/]+)$/i', $results[0]);
} else {
$result = '';
foreach ($results as $i => $line) {
if (substr($results[$i], 0, 1) == '*') {
$result = $results[$i];
break;
}
}
$this-> assertStringContainsString('*', $result);
}
}

public function testRunGitBad(): void
{
$this->expectException(UtilitiesException::class);
Expand Down

0 comments on commit fa3ffae

Please sign in to comment.