From 5f62dd22e6734f391682afa19c5d1e3df4dc09ef Mon Sep 17 00:00:00 2001 From: Chris Stretton Date: Thu, 2 Mar 2023 09:38:50 +0000 Subject: [PATCH 1/3] Added detection for .git files that point to alternative git folder locations --- src/Gitonomy/Git/Repository.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Gitonomy/Git/Repository.php b/src/Gitonomy/Git/Repository.php index fe77a88..17a3395 100644 --- a/src/Gitonomy/Git/Repository.php +++ b/src/Gitonomy/Git/Repository.php @@ -166,9 +166,19 @@ private function initDir($gitDir, $workingDir = null) throw new InvalidArgumentException(sprintf('Directory "%s" does not exist or is not a directory', $gitDir)); } elseif (!is_dir($realGitDir)) { throw new InvalidArgumentException(sprintf('Directory "%s" does not exist or is not a directory', $realGitDir)); - } elseif (null === $workingDir && is_dir($realGitDir.'/.git')) { + } elseif (null === $workingDir && is_file($realGitDir . '/.git')) { + if (!preg_match('/^gitdir: ?(.+)$/', file_get_contents($realGitDir . '/.git'), $matches)) { + throw new InvalidArgumentException(sprintf('Directory "%s" contains a .git file, but it is not in the expected format', $realGitDir)); + } + $foundGitPath = realpath($realGitDir . DIRECTORY_SEPARATOR . $matches[1]); + if (!is_dir($foundGitPath)) { + throw new InvalidArgumentException(sprintf('Directory "%s" contains a .git file, but the directory it points to cannot be found', $realGitDir)); + } + $workingDir = $realGitDir; + $realGitDir = $foundGitPath; + } elseif (null === $workingDir && is_dir($realGitDir . '/.git')) { $workingDir = $realGitDir; - $realGitDir = $realGitDir.'/.git'; + $realGitDir = $realGitDir . '/.git'; } $this->gitDir = $realGitDir; From 12a4ca95ad0fb666a33841de935e9f7fbe74a9f7 Mon Sep 17 00:00:00 2001 From: Chris Stretton Date: Thu, 2 Mar 2023 09:42:42 +0000 Subject: [PATCH 2/3] Added options to cloneTo to also allow passting of git cli arguments --- src/Gitonomy/Git/Admin.php | 5 +++-- src/Gitonomy/Git/Repository.php | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Gitonomy/Git/Admin.php b/src/Gitonomy/Git/Admin.php index 7b32794..41a7862 100644 --- a/src/Gitonomy/Git/Admin.php +++ b/src/Gitonomy/Git/Admin.php @@ -98,12 +98,13 @@ public static function isValidRepositoryAndBranch($url, $branchName, array $opti * @param string $url url of repository to clone * @param bool $bare indicates if repository should be bare or have a working copy * @param array $options options for Repository creation + * @param array $args arguments to be added to the command-line * * @return Repository */ - public static function cloneTo($path, $url, $bare = true, array $options = []) + public static function cloneTo($path, $url, $bare = true, array $options = [], array $args = []) { - $args = $bare ? ['--bare'] : []; + $args = array_merge($args,$bare ? ['--bare'] : []); return static::cloneRepository($path, $url, $args, $options); } diff --git a/src/Gitonomy/Git/Repository.php b/src/Gitonomy/Git/Repository.php index 17a3395..82b10fd 100644 --- a/src/Gitonomy/Git/Repository.php +++ b/src/Gitonomy/Git/Repository.php @@ -608,12 +608,14 @@ public function getLogger() * * @param string $path path to the new repository in which current repository will be cloned * @param bool $bare flag indicating if repository is bare or has a working-copy + * @param array $options options for Repository creation + * @param array $args arguments to be added to the command-line * * @return Repository the newly created repository */ - public function cloneTo($path, $bare = true, array $options = []) + public function cloneTo($path, $bare = true, array $options = [], array $args = []) { - return Admin::cloneTo($path, $this->gitDir, $bare, $options); + return Admin::cloneTo($path, $this->gitDir, $bare, $options, $args); } /** From 71b85b8ee09df94e26580bf2df065de9072da376 Mon Sep 17 00:00:00 2001 From: Chris Stretton Date: Thu, 2 Mar 2023 09:43:37 +0000 Subject: [PATCH 3/3] Test data provider also now creates a repo with an altertiave .git directory --- tests/Gitonomy/Git/Tests/AbstractTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/Gitonomy/Git/Tests/AbstractTest.php b/tests/Gitonomy/Git/Tests/AbstractTest.php index 0b9a51b..3731208 100644 --- a/tests/Gitonomy/Git/Tests/AbstractTest.php +++ b/tests/Gitonomy/Git/Tests/AbstractTest.php @@ -39,7 +39,7 @@ abstract class AbstractTest extends TestCase * * @return Repository */ - public static function createEmptyRepository($bare = true) + public static function createEmptyRepository($bare = true, $separateGitDir = false) { $dir = self::createTempDir(); $repository = Admin::init($dir, $bare, self::getOptions()); @@ -56,6 +56,7 @@ public static function provideFoobar() return [ [self::createFoobarRepository()], [self::createFoobarRepository(false)], + [self::createFoobarRepository(false, true)] ]; } @@ -75,10 +76,12 @@ public static function provideEmpty() * * @return Repository */ - public static function createFoobarRepository($bare = true) + public static function createFoobarRepository($bare = true, $separateGitDirectory = false) { + $args = $separateGitDirectory ? ['--separate-git-dir=' . tempnam(sys_get_temp_dir(), 'gitlib_')] : []; + if (null === self::$localRepository) { - self::$localRepository = Admin::cloneTo(self::createTempDir(), self::REPOSITORY_URL, $bare, self::getOptions()); + self::$localRepository = Admin::cloneTo(self::createTempDir(), self::REPOSITORY_URL, $bare, self::getOptions(), $args); } $repository = self::$localRepository->cloneTo(self::createTempDir(), $bare, self::getOptions());