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

Added 'git clean' and improved code comments #21

Open
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ $repo->removeFile(array('file3.txt', 'file4.txt'));

// adds all changes in repository
$repo->addAllChanges();

// removes all unstaged files and directories in repository
$repo->clean();
```


Expand Down
180 changes: 109 additions & 71 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class GitRepository implements IGit
protected $cwd;


/**
* @param string
*/
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Please fix indentation from spaces to tabs.

* @param string
* @throws GitException
*/
public function __construct($repository)
{
if(basename($repository) === '.git')
Expand Down Expand Up @@ -50,7 +51,7 @@ public function getRepositoryPath()
* `git tag <name>`
* @param string
* @param array|NULL
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function createTag($name, $options = NULL)
Expand All @@ -65,7 +66,7 @@ public function createTag($name, $options = NULL)
* Removes tag.
* `git tag -d <name>`
* @param string
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function removeTag($name)
Expand All @@ -84,7 +85,7 @@ public function removeTag($name)
* `git tag -d <old>`
* @param string
* @param string
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function renameTag($oldName, $newName)
Expand All @@ -99,10 +100,11 @@ public function renameTag($oldName, $newName)
}


/**
* Returns list of tags in repo.
* @return string[]|NULL NULL => no tags
*/
/**
* Returns list of tags in repo.
* @return string[]|NULL NULL => no tags
* @throws GitException
*/
public function getTags()
{
return $this->extractFromCommand('git tag', 'trim');
Expand All @@ -114,7 +116,7 @@ public function getTags()
* `git merge <options> <name>`
* @param string
* @param array|NULL
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function merge($branch, $options = NULL)
Expand All @@ -131,7 +133,7 @@ public function merge($branch, $options = NULL)
* (optionaly) `git checkout <name>`
* @param string
* @param bool
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function createBranch($name, $checkout = FALSE)
Expand All @@ -154,7 +156,7 @@ public function createBranch($name, $checkout = FALSE)
* Removes branch.
* `git branch -d <name>`
* @param string
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function removeBranch($name)
Expand All @@ -171,7 +173,7 @@ public function removeBranch($name)
* Gets name of current branch
* `git branch` + magic
* @return string
* @throws Cz\Git\GitException
* @throws GitException
*/
public function getCurrentBranchName()
{
Expand All @@ -196,10 +198,11 @@ public function getCurrentBranchName()
}


/**
* Returns list of all (local & remote) branches in repo.
* @return string[]|NULL NULL => no branches
*/
/**
* Returns list of all (local & remote) branches in repo.
* @return string[]|NULL NULL => no branches
* @throws GitException
*/
public function getBranches()
{
return $this->extractFromCommand('git branch -a', function($value) {
Expand All @@ -208,10 +211,11 @@ public function getBranches()
}


/**
* Returns list of local branches in repo.
* @return string[]|NULL NULL => no branches
*/
/**
* Returns list of local branches in repo.
* @return string[]|NULL NULL => no branches
* @throws GitException
*/
public function getLocalBranches()
{
return $this->extractFromCommand('git branch', function($value) {
Expand All @@ -224,7 +228,7 @@ public function getLocalBranches()
* Checkout branch.
* `git checkout <branch>`
* @param string
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function checkout($name)
Expand All @@ -239,7 +243,7 @@ public function checkout($name)
* Removes file(s).
* `git rm <file>`
* @param string|string[]
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function removeFile($file)
Expand All @@ -260,11 +264,38 @@ public function removeFile($file)
}


/**
* Clean repo.
* `git clean`
* @param bool $cleanDirectories
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove $cleanDirectories part

* @param bool $force
* @throws GitException
* @return self
*/
public function clean($cleanDirectories = true, $force = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Change all true/false/null to uppercase TRUE/FALSE/NULL

{
$options = null;
if (!($cleanDirectories === false && $force === false)) {
$options = '-';
if ($force) {
$options .= 'f';
}
if ($cleanDirectories) {
$options .= 'd';
}
}

return $this->begin()
->run('git clean', $options)
->end();
}


/**
* Adds file(s).
* `git add <file>`
* @param string|string[]
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function addFile($file)
Expand All @@ -289,7 +320,7 @@ public function addFile($file)
/**
* Adds all created, modified & removed files.
* `git add --all`
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function addAllChanges()
Expand All @@ -305,7 +336,7 @@ public function addAllChanges()
* `git mv <file>`
* @param string|string[] from: array('from' => 'to', ...) || (from, to)
* @param string|NULL
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function renameFile($file, $to = NULL)
Expand Down Expand Up @@ -333,7 +364,7 @@ public function renameFile($file, $to = NULL)
* `git commit <params> -m <message>`
* @param string
* @param string[] param => value
* @throws Cz\Git\GitException
* @throws GitException
* @return self
*/
public function commit($message, $params = NULL)
Expand All @@ -351,11 +382,12 @@ public function commit($message, $params = NULL)
}


/**
* Exists changes?
* `git status` + magic
* @return bool
*/
/**
* Exists changes?
* `git status` + magic
* @return bool
* @throws GitException
*/
public function hasChanges()
{
// Make sure the `git status` gets a refreshed look at the working tree.
Expand Down Expand Up @@ -439,13 +471,14 @@ public function fetch($remote = NULL, array $params = NULL)
}


/**
* Adds new remote repository
* @param string
* @param string
* @param array|NULL
* @return self
*/
/**
* Adds new remote repository
* @param string
* @param string
* @param array|NULL
* @return self
* @throws GitException
*/
public function addRemote($name, $url, array $params = NULL)
{
return $this->begin()
Expand All @@ -454,12 +487,13 @@ public function addRemote($name, $url, array $params = NULL)
}


/**
* Renames remote repository
* @param string
* @param string
* @return self
*/
/**
* Renames remote repository
* @param string
* @param string
* @return self
* @throws GitException
*/
public function renameRemote($oldName, $newName)
{
return $this->begin()
Expand All @@ -468,11 +502,12 @@ public function renameRemote($oldName, $newName)
}


/**
* Removes remote repository
* @param string
* @return self
*/
/**
* Removes remote repository
* @param string
* @return self
* @throws GitException
*/
public function removeRemote($name)
{
return $this->begin()
Expand All @@ -481,13 +516,14 @@ public function removeRemote($name)
}


/**
* Changes remote repository URL
* @param string
* @param string
* @param array|NULL
* @return self
*/
/**
* Changes remote repository URL
* @param string
* @param string
* @param array|NULL
* @return self
* @throws GitException
*/
public function setRemoteUrl($name, $url, array $params = NULL)
{
return $this->begin()
Expand Down Expand Up @@ -526,11 +562,12 @@ protected function end()
}


/**
* @param string
* @param callback|NULL
* @return string[]|NULL
*/
/**
* @param string
* @param callback|NULL
* @return string[]|NULL
* @throws GitException
*/
protected function extractFromCommand($cmd, $filter = NULL)
{
$output = array();
Expand Down Expand Up @@ -577,7 +614,7 @@ protected function extractFromCommand($cmd, $filter = NULL)
* Runs command.
* @param string|array
* @return self
* @throws Cz\Git\GitException
* @throws GitException
*/
protected function run($cmd/*, $options = NULL*/)
{
Expand Down Expand Up @@ -665,13 +702,14 @@ public static function init($directory, array $params = NULL)
}


/**
* Clones GIT repository from $url into $directory
* @param string
* @param string|NULL
* @param array|NULL
* @return self
*/
/**
* Clones GIT repository from $url into $directory
* @param string
* @param string|NULL
* @param array|NULL
* @return self
* @throws GitException
*/
public static function cloneRepository($url, $directory = NULL, array $params = NULL)
{
if($directory !== NULL && is_dir("$directory/.git"))
Expand Down
Loading