Skip to content

Commit

Permalink
GitRepository: addFile() checks if file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Rindle authored and janpecha committed Dec 8, 2018
1 parent ebc5b3c commit a1398bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,14 @@ public function addFile($file)

foreach($file as $item)
{
// TODO: ?? is file($repo . / . $item) ??
// make sure the given item exists
// this can be a file or an directory, git supports both
$path = self::isAbsolute($item) ? $item : ($this->getRepositoryPath() . DIRECTORY_SEPARATOR . $item);

if (!file_exists($path)) {
throw new GitException("The path at '$item' does not represent a valid file.");
}

$this->run('git add', $item);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/GitPhp/basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ Assert::false(is_file($file));
Assert::true(is_file($newFile));
Assert::same($newContent, file_get_contents($newFile));

// missing file
Assert::exception(function () use ($repo) {
$repo->addFile('missing-file.txt');
}, 'Cz\Git\GitException', "The path at 'missing-file.txt' does not represent a valid file.");

Assert::exception(function () use ($repo) {
$repo->addFile($repo->getRepositoryPath() . '/missing-file.txt');
}, 'Cz\Git\GitException', "The path at '" . $repo->getRepositoryPath() . "/missing-file.txt' does not represent a valid file.");

// creating repo object
$newRepo = new GitRepository(TEMP_DIR . '/.git');
Assert::same(realpath(TEMP_DIR), $newRepo->getRepositoryPath());
Expand Down

0 comments on commit a1398bd

Please sign in to comment.