8000 GitRepository: Create isFileCommitted() method. by Taitava · Pull Request #48 · czproject/git-php · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

GitRepository: Create isFileCommitted() method. #48

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

Open
wants to merge 1 commit into
base: master
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
37 changes: 37 additions & 0 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,5 +958,42 @@ public function getCommitData($commit)

return $data;
}

/**
* Checks whether the given file is committed to the git repository. Staged files are not counted as committed
* files - unless they are both staged an committed previously.
*
* Uses `git ls-files --error-unmatch <filename>`
*
* @param string $filename
* @return bool
* @throws GitException
*/
public function isFileCommitted($filename)
{
$this->begin();
try {
$this->run('git ls-files --error-unmatch', [$filename]);
}
catch (GitException $git_exception) {
switch ($git_exception->getCode()) {
case 1:
// The `git ls-files --error-unmatch` command didn't find the given file in git and has yelled an error
// number 1. This exception can be considered normal. We can just report that the file is not in git and
// continue execution normally.
$this->end();
return FALSE;
break;
default:
// An unrecognised error has occurred. Rethrow the exception.
$this->end();
throw $git_exception;
break;
}
}
// As the command didn't give any error code when exiting, it's a sign for us to know that the file _does_ exist in git.
$this->end();
return TRUE;
}

}
8 changes: 8 additions & 0 deletions src/IGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ static function init($directory, array $params = NULL);
* @return self
*/
static function cloneRepository($url, $directory = NULL);

/**
* Checks whether the given file is committed to the git repository.
*
* @param string $filename
* @return bool
*/
public function isFileCommitted($filename);
}


Expand Down
0