Skip to content
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
Fix relative paths not resolving against the repository in Repository…
…::run()

The git process spawned by Repository::run() never had its working
directory set, so Symfony Process defaulted it to the calling PHP
script's cwd. Commands such as "apply" resolve the paths referenced
inside their arguments (e.g. the files listed in a patch) against the
process cwd rather than --work-tree, so running them from outside the
repository directory failed with errors like "No such file or
directory" even though --git-dir/--work-tree were correctly set.

Fixes #67
  • Loading branch information
lyrixx committed Sep 7, 2026
commit 24c16ed2ad9d42585423ca99d69b7342bc582744
1 change: 1 addition & 0 deletions src/Gitonomy/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ private function getProcess(string $command, array $args = []): Process
$base[] = $command;

$process = new Process(array_merge($base, $args));
$process->setWorkingDirectory($this->getPath());

if ($this->inheritEnvironmentVariables) {
$process->setEnv(array_replace($_SERVER, $this->environmentVariables));
Expand Down
34 changes: 34 additions & 0 deletions tests/Gitonomy/Git/Tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,38 @@ public function testLoggerNOk(Repository $repository): void

$repository->run('not-work');
}

/**
* @see https://github.com/gitonomy/gitlib/issues/67
*/
public function testRunResolvesRelativePathsAgainstTheRepositoryRegardlessOfCwd(): void
{
$repository = self::createFoobarRepository(false);

$file = $repository->getWorkingDir().'/README.md';
$original = file_get_contents($file);
file_put_contents($file, $original."Applied line.\n");

$patch = $repository->run('diff', ['--', 'README.md']);
file_put_contents($file, $original);

$patchFile = tempnam(sys_get_temp_dir(), 'gitlib_patch_');
file_put_contents($patchFile, $patch);

$previousCwd = getcwd();
$this->assertIsString($previousCwd);
chdir(sys_get_temp_dir());

try {
// "README.md" is relative to the repository work-tree, not to the
// process cwd (which is an unrelated directory here). This only
// works if the git process is run with its cwd set to the repository.
$repository->run('apply', [$patchFile]);
} finally {
chdir($previousCwd);
unlink($patchFile);
}

$this->assertSame($original."Applied line.\n", file_get_contents($file));
}
}