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 fe77a88..82b10fd 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; @@ -598,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); } /** 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());