From 218956200db224c8c7b3dc38cdfc0f78ecced62a Mon Sep 17 00:00:00 2001 From: Manuel Voss Date: Wed, 28 Feb 2018 09:24:36 +0100 Subject: [PATCH 1/3] Added git clean method and improved comments --- src/GitRepository.php | 179 ++++++++++++++++++++++++---------------- src/IGit.php | 26 +++--- tests/GitPhp/basic.phpt | 10 +++ 3 files changed, 131 insertions(+), 84 deletions(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index 3452897..fe90bdb 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -17,9 +17,10 @@ class GitRepository implements IGit protected $cwd; - /** - * @param string - */ + /** + * @param string + * @throws GitException + */ public function __construct($repository) { if(basename($repository) === '.git') @@ -50,7 +51,7 @@ public function getRepositoryPath() * `git tag ` * @param string * @param array|NULL - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function createTag($name, $options = NULL) @@ -65,7 +66,7 @@ public function createTag($name, $options = NULL) * Removes tag. * `git tag -d ` * @param string - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function removeTag($name) @@ -84,7 +85,7 @@ public function removeTag($name) * `git tag -d ` * @param string * @param string - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function renameTag($oldName, $newName) @@ -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'); @@ -114,7 +116,7 @@ public function getTags() * `git merge ` * @param string * @param array|NULL - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function merge($branch, $options = NULL) @@ -131,7 +133,7 @@ public function merge($branch, $options = NULL) * (optionaly) `git checkout ` * @param string * @param bool - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function createBranch($name, $checkout = FALSE) @@ -154,7 +156,7 @@ public function createBranch($name, $checkout = FALSE) * Removes branch. * `git branch -d ` * @param string - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function removeBranch($name) @@ -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() { @@ -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) { @@ -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) { @@ -224,7 +228,7 @@ public function getLocalBranches() * Checkout branch. * `git checkout ` * @param string - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function checkout($name) @@ -239,7 +243,7 @@ public function checkout($name) * Removes file(s). * `git rm ` * @param string|string[] - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function removeFile($file) @@ -260,11 +264,37 @@ public function removeFile($file) } + /** + * Clean repo. + * `git clean` + * @param bool $cleanDirectories + * @param bool $force + * @throws GitException + * @return self + */ + public function clean($cleanDirectories = true, $force = true) + { + 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 ` * @param string|string[] - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function addFile($file) @@ -289,7 +319,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() @@ -305,7 +335,7 @@ public function addAllChanges() * `git mv ` * @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) @@ -333,7 +363,7 @@ public function renameFile($file, $to = NULL) * `git commit -m ` * @param string * @param string[] param => value - * @throws Cz\Git\GitException + * @throws GitException * @return self */ public function commit($message, $params = NULL) @@ -351,11 +381,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. @@ -439,13 +470,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() @@ -454,12 +486,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() @@ -468,11 +501,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() @@ -481,13 +515,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() @@ -526,11 +561,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(); @@ -577,7 +613,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*/) { @@ -665,13 +701,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")) diff --git a/src/IGit.php b/src/IGit.php index 110dfaf..200f096 100644 --- a/src/IGit.php +++ b/src/IGit.php @@ -14,7 +14,7 @@ interface IGit * Creates a tag. * @param string * @param array|NULL - * @throws Cz\Git\GitException + * @throws GitException */ function createTag($name, $options = NULL); @@ -22,7 +22,7 @@ function createTag($name, $options = NULL); /** * Removes tag. * @param string - * @throws Cz\Git\GitException + * @throws GitException */ function removeTag($name); @@ -31,7 +31,7 @@ function removeTag($name); * Renames tag. * @param string * @param string - * @throws Cz\Git\GitException + * @throws GitException */ function renameTag($oldName, $newName); @@ -47,7 +47,7 @@ function getTags(); * Merges branches. * @param string * @param array|NULL - * @throws Cz\Git\GitException + * @throws GitException */ function merge($branch, $options = NULL); @@ -56,7 +56,7 @@ function merge($branch, $options = NULL); * Creates new branch. * @param string * @param bool - * @throws Cz\Git\GitException + * @throws GitException */ function createBranch($name, $checkout = FALSE); @@ -64,7 +64,7 @@ function createBranch($name, $checkout = FALSE); /** * Removes branch. * @param string - * @throws Cz\Git\GitException + * @throws GitException */ function removeBranch($name); @@ -72,7 +72,7 @@ function removeBranch($name); /** * Gets name of current branch * @return string - * @throws Cz\Git\GitException + * @throws GitException */ function getCurrentBranchName(); @@ -94,7 +94,7 @@ function getLocalBranches(); /** * Checkout branch. * @param string - * @throws Cz\Git\GitException + * @throws GitException */ function checkout($name); @@ -102,7 +102,7 @@ function checkout($name); /** * Removes file(s). * @param string|string[] - * @throws Cz\Git\GitException + * @throws GitException */ function removeFile($file); @@ -110,14 +110,14 @@ function removeFile($file); /** * Adds file(s). * @param string|string[] - * @throws Cz\Git\GitException + * @throws GitException */ function addFile($file); /** * Adds all created, modified & removed files. - * @throws Cz\Git\GitException + * @throws GitException */ function addAllChanges(); @@ -126,7 +126,7 @@ function addAllChanges(); * Renames file(s). * @param string|string[] from: array('from' => 'to', ...) || (from, to) * @param string|NULL - * @throws Cz\Git\GitException + * @throws GitException */ function renameFile($file, $to = NULL); @@ -135,7 +135,7 @@ function renameFile($file, $to = NULL); * Commits changes * @param string * @param string[] param => value - * @throws Cz\Git\GitException + * @throws GitException */ function commit($message, $params = NULL); diff --git a/tests/GitPhp/basic.phpt b/tests/GitPhp/basic.phpt index 74df9fb..86df327 100644 --- a/tests/GitPhp/basic.phpt +++ b/tests/GitPhp/basic.phpt @@ -54,6 +54,16 @@ $repo->commit('Removed second file'); Assert::false($repo->hasChanges()); +// clean +$file = TEMP_DIR . '/somefile.txt'; +$dir = TEMP_DIR . '/somedir'; +file_put_contents($file, "Sit amet dolor ipsum lorem.\n"); +mkdir($dir); +Assert::true($repo->hasChanges()); +$repo->clean(); +Assert::false($repo->hasChanges()); + + // Branches $repo->createBranch('develop', TRUE); Assert::same(array( From 4d7e4830d237535b825dfb0f777a32efab5ed565 Mon Sep 17 00:00:00 2001 From: Manuel Voss Date: Wed, 28 Feb 2018 09:26:34 +0100 Subject: [PATCH 2/3] Added clean to the readme --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 51b2e93..6dbd036 100644 --- a/readme.md +++ b/readme.md @@ -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(); ``` From 640e080e6e9da162616b8b0bf77c68390c3e7eaf Mon Sep 17 00:00:00 2001 From: Manuel Voss Date: Wed, 28 Feb 2018 09:29:33 +0100 Subject: [PATCH 3/3] Fixed a potential bug --- src/GitRepository.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GitRepository.php b/src/GitRepository.php index fe90bdb..09f7d1b 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -274,6 +274,7 @@ public function removeFile($file) */ public function clean($cleanDirectories = true, $force = true) { + $options = null; if (!($cleanDirectories === false && $force === false)) { $options = '-'; if ($force) {