From 84ede575e34faa02b371b4e13d53381fa7e1108d Mon Sep 17 00:00:00 2001 From: William Desportes Date: Wed, 24 Mar 2021 18:29:52 +0100 Subject: [PATCH] Remove dependency on Config class of Git class Signed-off-by: William Desportes --- .../classes/Controllers/HomeController.php | 6 +- libraries/classes/Git.php | 41 +++++---- test/classes/GitTest.php | 85 ++++--------------- 3 files changed, 47 insertions(+), 85 deletions(-) diff --git a/libraries/classes/Controllers/HomeController.php b/libraries/classes/Controllers/HomeController.php index 0a0dfe784b..85ade46365 100644 --- a/libraries/classes/Controllers/HomeController.php +++ b/libraries/classes/Controllers/HomeController.php @@ -228,7 +228,7 @@ class HomeController extends AbstractController $this->checkRequirements(); - $git = new Git($this->config); + $git = new Git($this->config->get('ShowGitRevision')); $this->render('home/index', [ 'message' => $displayMessage ?? '', @@ -284,7 +284,7 @@ class HomeController extends AbstractController return; } - $git = new Git($this->config); + $git = new Git($this->config->get('ShowGitRevision')); if (! $git->isGitRevision()) { return; @@ -292,7 +292,7 @@ class HomeController extends AbstractController $commit = $git->checkGitRevision(); - if (! $this->config->get('PMA_VERSION_GIT') || $commit === null) { + if (! $git->hasGitInformation() || $commit === null) { $this->response->setRequestStatus(false); return; diff --git a/libraries/classes/Git.php b/libraries/classes/Git.php index a41e8811cc..6575cfc947 100644 --- a/libraries/classes/Git.php +++ b/libraries/classes/Git.php @@ -49,15 +49,27 @@ use const PHP_EOL; class Git { /** - * Build a Git class + * Enable Git information search and process * - * @var Config + * @var bool */ - private $config; + private $showGitRevision; - public function __construct(Config $config) + /** + * Git has been found and the data fetched + * + * @var bool + */ + private $hasGit = false; + + public function __construct(bool $showGitRevision) { - $this->config = $config; + $this->showGitRevision = $showGitRevision; + } + + public function hasGitInformation(): bool + { + return $this->hasGit; } /** @@ -67,8 +79,7 @@ class Git */ public function isGitRevision(&$git_location = null): bool { - // PMA config check - if (! $this->config->get('ShowGitRevision')) { + if (! $this->showGitRevision) { return false; } @@ -275,7 +286,7 @@ class Git $commit = @file_get_contents($gitFileName); if ($commit === false) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return null; } @@ -478,7 +489,7 @@ class Git if (@file_exists($refFile)) { $hash = @file_get_contents($refFile); if ($hash === false) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return [null, null]; } @@ -489,7 +500,7 @@ class Git // deal with packed refs $packedRefs = @file_get_contents($gitFolder . '/packed-refs'); if ($packedRefs === false) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return [null, null]; } @@ -517,7 +528,7 @@ class Git } if (! isset($hash)) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; // Could not find ref return [null, null]; @@ -534,7 +545,7 @@ class Git // find out if there is a .git folder $gitFolder = ''; if (! $this->isGitRevision($gitFolder)) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return null; } @@ -542,7 +553,7 @@ class Git $ref_head = @file_get_contents($gitFolder . '/HEAD'); if (! $ref_head) { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return null; } @@ -613,12 +624,12 @@ class Git ]; $message = trim($commit_json->message); } else { - $this->config->set('PMA_VERSION_GIT', 0); + $this->hasGit = false; return null; } - $this->config->set('PMA_VERSION_GIT', 1); + $this->hasGit = true; return [ 'hash' => $hash, diff --git a/test/classes/GitTest.php b/test/classes/GitTest.php index 574efcf768..b0376fee7a 100644 --- a/test/classes/GitTest.php +++ b/test/classes/GitTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests; -use PhpMyAdmin\Config; use PhpMyAdmin\Git; use function chdir; @@ -17,7 +16,6 @@ use function rmdir; use function sys_get_temp_dir; use function unlink; -use const CONFIG_FILE; use const DIRECTORY_SEPARATOR; use const PHP_EOL; @@ -29,9 +27,6 @@ class GitTest extends AbstractTestCase /** @var Git */ protected $object; - /** @var Config */ - protected $config; - /** @var string */ protected $testDir; @@ -46,9 +41,7 @@ class GitTest extends AbstractTestCase { parent::setUp(); parent::setProxySettings(); - $this->config = new Config(CONFIG_FILE); - $this->config->set('ShowGitRevision', true); - $this->object = new Git($this->config); + $this->object = new Git(true); $this->testDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'gittempdir_' . mt_rand(); unset($_SESSION['git_location']); @@ -80,14 +73,9 @@ class GitTest extends AbstractTestCase $git_location = ''; - $this->assertTrue( - $this->object->isGitRevision($git_location) - ); + $this->assertTrue($this->object->isGitRevision($git_location)); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); $this->assertEquals('.cachedgitlocation', $git_location); } @@ -97,7 +85,7 @@ class GitTest extends AbstractTestCase */ public function testIsGitRevisionSkipped(): void { - $this->config->set('ShowGitRevision', false); + $this->object = new Git(false); $this->assertFalse( $this->object->isGitRevision($git_location) ); @@ -114,10 +102,7 @@ class GitTest extends AbstractTestCase $this->object->isGitRevision() ); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unset($_SESSION['git_location']); unset($_SESSION['is_git_revision']); @@ -128,24 +113,16 @@ class GitTest extends AbstractTestCase $this->object->isGitRevision() ); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unset($_SESSION['git_location']); unset($_SESSION['is_git_revision']); file_put_contents('.git/config', ''); - $this->assertTrue( - $this->object->isGitRevision() - ); + $this->assertTrue($this->object->isGitRevision()); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unlink('.git/config'); rmdir('.git'); @@ -163,24 +140,16 @@ class GitTest extends AbstractTestCase $this->object->isGitRevision() ); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unset($_SESSION['git_location']); unset($_SESSION['is_git_revision']); mkdir('.customgitdir'); - $this->assertTrue( - $this->object->isGitRevision() - ); + $this->assertTrue($this->object->isGitRevision()); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unset($_SESSION['git_location']); unset($_SESSION['is_git_revision']); @@ -191,10 +160,7 @@ class GitTest extends AbstractTestCase $this->object->isGitRevision() ); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unlink('.git'); rmdir('.customgitdir'); @@ -213,10 +179,7 @@ class GitTest extends AbstractTestCase $commit = $this->object->checkGitRevision(); $this->assertNull($commit); - $this->assertEquals( - '0', - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master'); @@ -290,10 +253,7 @@ class GitTest extends AbstractTestCase $commit = $this->object->checkGitRevision(); $this->assertNull($commit); - $this->assertEquals( - '0', - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master'); mkdir('.git/refs/remotes/origin', 0777, true); @@ -302,10 +262,7 @@ class GitTest extends AbstractTestCase $commit = $this->object->checkGitRevision(); $this->assertNull($commit); - $this->assertEquals( - 0, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); unlink('.git/refs/remotes/origin/master'); rmdir('.git/refs/remotes/origin'); @@ -331,10 +288,7 @@ class GitTest extends AbstractTestCase $commit = $this->object->checkGitRevision(); $this->assertNull($commit); - $this->assertEquals( - '0', - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); file_put_contents('.git/HEAD', 'ref: refs/remotes/origin/master'); @@ -409,15 +363,12 @@ class GitTest extends AbstractTestCase */ public function testCheckGitRevisionSkipped(): void { - $this->config->set('ShowGitRevision', false); + $this->object = new Git(false); $commit = $this->object->checkGitRevision(); $this->assertNull($commit); - $this->assertEquals( - null, - $this->config->get('PMA_VERSION_GIT') - ); + $this->assertFalse($this->object->hasGitInformation()); } /**