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

Fix retrieving git worktree path #1008

Open
wants to merge 4 commits into
base: v2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Locator/GitRepositoryDirLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace GrumPHP\Locator;

use GrumPHP\Exception\RuntimeException;
use GrumPHP\Util\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class GitRepositoryDirLocator
{
Expand Down Expand Up @@ -36,7 +39,22 @@ public function locate(string $gitDir): string
$gitRepositoryDir = $matches[1];

if ($this->filesystem->isAbsolutePath($gitRepositoryDir)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was a bit confused whilst trying to figure out how this works exactly. (took around 30 minutes to click)

Would you mind moving this logic into a private function and add documentation on how this logic works in that function's docblock?

return $gitRepositoryDir;
if (!$this->filesystem->isFile($gitRepositoryDir.DIRECTORY_SEPARATOR.'commondir')) {
throw new RuntimeException('The git directory for worktree could not be found.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this expected behaviour? Or should we just link to the absolute dir instead?
Either way : we'll need a test for this

}

$worktreeRelativeRoot = trim(
$this->filesystem->readPath(
$gitRepositoryDir.DIRECTORY_SEPARATOR.'commondir'
)
);

return $this->filesystem->realpath(
$this->filesystem->makePathAbsolute(
$worktreeRelativeRoot,
$gitRepositoryDir
)
);
}

return $this->filesystem->buildPath(
Expand Down
9 changes: 6 additions & 3 deletions test/Unit/Locator/GitRepositoryDirLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void

$this->filesystem = new Filesystem();
$this->locator = new GitRepositoryDirLocator($this->filesystem);
$this->gitDir = $this->workspace . DIRECTORY_SEPARATOR . '.git';
$this->gitDir = $this->workspace . DIRECTORY_SEPARATOR . '.git';
}

/**
Expand Down Expand Up @@ -69,7 +69,10 @@ public function it_can_passthrough_git_dir_path_if_file_is_not_parseable(): void
*/
public function it_can_locate_git_dir_in_workspaces(): void
{
$this->filesystem->dumpFile($this->gitDir, 'gitdir: /dev/null');
$this->assertEquals('/dev/null', $this->locator->locate($this->gitDir));
$worktreeRoot = $this->workspace.'/git_root/worktrees/git_worktree/';
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe better to name git_root -> .git
and maybe git_worktree to worktree1 or myworktree (since I though this might have been a git folder)

mkdir($worktreeRoot, 0777, true);
$this->filesystem->dumpFile($worktreeRoot.'/commondir', '../..');
$this->filesystem->dumpFile($this->gitDir, 'gitdir: '.$this->workspace.'/git_root/worktrees/git_worktree');
$this->assertEquals($this->workspace.DIRECTORY_SEPARATOR.'git_root', $this->locator->locate($this->gitDir));
}
}