Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate git directory support #201

Open
wants to merge 3 commits into
base: 1.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Gitonomy/Git/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 16 additions & 4 deletions src/Gitonomy/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Copy link

@tsterker tsterker Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris114782 This assumes that the gitdir is a relative path, which might not be true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try and take a look this weekend, it's been a few months (nearly a year now!) since I looked at this and I need to get my head back into it.

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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions tests/Gitonomy/Git/Tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -56,6 +56,7 @@ public static function provideFoobar()
return [
[self::createFoobarRepository()],
[self::createFoobarRepository(false)],
[self::createFoobarRepository(false, true)]
];
}

Expand All @@ -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());
Expand Down