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

Merged
merged 4 commits into from
Nov 29, 2024
Merged
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
46 changes: 44 additions & 2 deletions src/Locator/GitRepositoryDirLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,55 @@ public function locate(string $gitDir): string

$gitRepositoryDir = $matches[1];

if ($this->filesystem->isAbsolutePath($gitRepositoryDir)) {
return $gitRepositoryDir;
if ($this->isWorktree($gitRepositoryDir)) {
return $this->locateWorktreeRoot($gitRepositoryDir);
}

return $this->filesystem->buildPath(
dirname($gitDir),
$gitRepositoryDir
);
}

/**
* If a given path (from gitdir value) is absolute and there is a commondir file, it is
* a worktree.
*/
private function isWorktree(string $gitDir): bool
{
return $this->filesystem->isAbsolutePath($gitDir)
&& $this->filesystem->isFile($gitDir.DIRECTORY_SEPARATOR.'commondir');
}

/**
* Retreiving repository dir for worktree nominally returns path to the configured worktree,
* which does not hold hooks. We need to resolve the actual repository root.
*
* Example directory structure:
* ```
* /project
* .git/
* .git/hooks/
* .git/worktrees/
* worktree1
* commondir: relative path to /project/.git
* /worktree1
* .git: file with path to /project/.git/worktrees/worktree1
* ```
*/
private function locateWorktreeRoot(string $gitRepositoryDir): string
{
$worktreeRelativeRoot = trim(
$this->filesystem->readPath(
$gitRepositoryDir.DIRECTORY_SEPARATOR.'commondir'
)
);

return $this->filesystem->realpath(
$this->filesystem->makePathAbsolute(
$worktreeRelativeRoot,
$gitRepositoryDir
)
);
}
}
10 changes: 7 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,11 @@ 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));
$ourWorktreeProject = $this->workspace.'/project1/';
$worktreeGitRoot = $this->gitDir.'/worktrees/worktree1/';
mkdir($worktreeGitRoot, 0777, true);
$this->filesystem->dumpFile($worktreeGitRoot.'/commondir', '../..');
$this->filesystem->dumpFile($ourWorktreeProject.'/.git', 'gitdir: '.$this->gitDir.'/worktrees/worktree1');
$this->assertEquals($this->gitDir, $this->locator->locate($this->gitDir));
}
}
Loading