├── .check-author.yml ├── .github └── workflows │ └── diagnostics.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.default.properties ├── build.xml ├── composer.json ├── phpunit.xml.dist ├── src ├── Command │ ├── AbstractCommandBuilder.php │ ├── AddCommandBuilder.php │ ├── BranchCommandBuilder.php │ ├── CheckoutCommandBuilder.php │ ├── CloneCommandBuilder.php │ ├── CommandBuilderInterface.php │ ├── CommandBuilderTrait.php │ ├── CommitCommandBuilder.php │ ├── ConfigCommandBuilder.php │ ├── DescribeCommandBuilder.php │ ├── FetchCommandBuilder.php │ ├── InitCommandBuilder.php │ ├── LogCommandBuilder.php │ ├── LsRemoteCommandBuilder.php │ ├── MergeCommandBuilder.php │ ├── PullCommandBuilder.php │ ├── PushCommandBuilder.php │ ├── RemoteCommandBuilder.php │ ├── ResetCommandBuilder.php │ ├── RevParseCommandBuilder.php │ ├── RmCommandBuilder.php │ ├── ShortLogCommandBuilder.php │ ├── ShowCommandBuilder.php │ ├── StashCommandBuilder.php │ ├── StatusCommandBuilder.php │ └── TagCommandBuilder.php ├── GitConfig.php ├── GitException.php └── GitRepository.php └── tests ├── GitRepositoryTest.php └── git.zip /.check-author.yml: -------------------------------------------------------------------------------- 1 | mapping: 2 | Tristan Lins : Tristan Lins 3 | David Molineus : David Molineus 4 | Ahmad Marzouq : ahmad-marzouq 5 | -------------------------------------------------------------------------------- /.github/workflows/diagnostics.yml: -------------------------------------------------------------------------------- 1 | name: Git php 2 | 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | php: [5.6, 7.1, 7.2, 7.3, 7.4] 13 | 14 | steps: 15 | - name: PHP ${{ matrix.php }} Pull source 16 | uses: actions/checkout@v2 17 | with: 18 | fetch-depth: 0 19 | 20 | # see https://github.com/shivammathur/setup-php 21 | - name: PHP ${{ matrix.php }} Setup PHP. 22 | uses: shivammathur/setup-php@v2 23 | with: 24 | php-version: ${{ matrix.php }} 25 | coverage: none 26 | 27 | - name: PHP ${{ matrix.php }} Cache composer cache directory 28 | uses: actions/cache@v1 29 | env: 30 | cache-name: composer-cache-dir 31 | with: 32 | path: ~/.cache/composer 33 | key: ${{ runner.os }}-build-${{ env.cache-name }} 34 | 35 | - name: PHP ${{ matrix.php }} Cache vendor directory 36 | uses: actions/cache@v1 37 | env: 38 | cache-name: composer-vendor 39 | with: 40 | path: vendor 41 | key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/composer.lock') }} 42 | restore-keys: | 43 | ${{ runner.os }}-build-${{ env.cache-name }}- 44 | - name: PHP ${{ matrix.php }} Install composer dependencies 45 | run: composer update --prefer-dist --no-interaction --no-suggest 46 | 47 | - name: PHP ${{ matrix.php }} Run tests 48 | run: ant -keep-going 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OS 2 | /.DS_Store 3 | /Thumbs.db 4 | 5 | # IDEs 6 | /.buildpath 7 | /.project 8 | /.settings/ 9 | /.build/ 10 | /.external*/ 11 | /.idea/ 12 | /nbproject/ 13 | 14 | # composer related 15 | /vendor/ 16 | /composer.phar 17 | /composer.lock 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Tristan Lins 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://github.com/bit3/git-php/actions/workflows/diagnostics.yml/badge.svg)](https://github.com/bit3/git-php/actions) 2 | [![Latest Version tagged](http://img.shields.io/github/tag/bit3/git-php.svg)](https://github.com/bit3/git-php/tags) 3 | [![Latest Version on Packagist](http://img.shields.io/packagist/v/bit3/git-php.svg)](https://packagist.org/packages/bit3/git-php) 4 | [![Installations via composer per month](http://img.shields.io/packagist/dm/bit3/git-php.svg)](https://packagist.org/packages/bit3/git-php) 5 | 6 | Easy to use GIT wrapper for php 7 | =============================== 8 | 9 | This is a lightweight wrapper, providing the git commands in PHP. 10 | 11 | Usage examples 12 | -------------- 13 | 14 | The API use command builders, that allow you to build a command and execute it one time. 15 | 16 | The main synopsis is: 17 | 18 | ```php 19 | $git->command()->option()->execute(); 20 | ``` 21 | 22 | `$git->command()` will create a new command, `*->option()` will add an option to the command and 23 | `*->execute()` will finally execute the command. 24 | 25 | The naming of commands and options follow the git naming. If you search for documentation of a specific command 26 | or option, just look into the git documentation. You will find the command/option there. 27 | 28 | #### init a new git repository 29 | 30 | ```php 31 | use Bit3\GitPhp\GitRepository; 32 | 33 | $directory = '/path/to/git/target/directory'; 34 | 35 | $git = new GitRepository($directory); 36 | $git->init()->execute(); 37 | ``` 38 | 39 | #### clone a git repository 40 | 41 | The `clone` command is named `cloneRepository()` because `clone` is a reserved word in PHP. 42 | 43 | ```php 44 | use Bit3\GitPhp\GitRepository; 45 | 46 | $directory = '/path/to/git/target/directory'; 47 | 48 | $git = new GitRepository($directory); 49 | $git->cloneRepository()->execute(); 50 | ``` 51 | 52 | #### describe 53 | 54 | ```php 55 | $annotatedTag = $git->describe()->execute(); 56 | $lightweightTag = $git->describe()->tags()->execute(); 57 | $recentRef = $git->describe()->all()->execute(); 58 | ``` 59 | 60 | #### set remote fetch url 61 | 62 | ```php 63 | $git->remote() 64 | ->setUrl('origin', 'git@github.com:bit3/git-php.git') 65 | ->execute(); 66 | ``` 67 | 68 | #### set remote push url 69 | 70 | ```php 71 | $git->remote() 72 | ->setPushUrl('origin', 'git@github.com:bit3/git-php.git') 73 | ->execute(); 74 | ``` 75 | 76 | #### add new remote 77 | 78 | ```php 79 | $git->remote() 80 | ->add('github', 'git@github.com:bit3/git-php.git') 81 | ->execute(); 82 | ``` 83 | 84 | #### fetch remote objects 85 | 86 | ```php 87 | $git->fetch()->execute('github'); 88 | ``` 89 | 90 | #### checkout 91 | 92 | ```php 93 | $git->checkout()->execute('hotfix/1.2.3'); 94 | ``` 95 | 96 | #### checkout specific path 97 | 98 | ```php 99 | $git->checkout()->execute('hotfix/1.2.3', '/fileA', '/fileB', '/dir/fileC'); 100 | ``` 101 | 102 | #### push objects 103 | 104 | ```php 105 | $git->push()->execute('github', 'hotfix/1.2.3'); 106 | ``` 107 | 108 | #### add file to staging index 109 | 110 | ```php 111 | $git->add()->execute('file/to/add.ext'); 112 | ``` 113 | 114 | #### remove file 115 | 116 | ```php 117 | $git->rm()->execute('file/to/remove.ext'); 118 | ``` 119 | 120 | #### commit changes 121 | 122 | ```php 123 | $git->commit()->message('Commit message')->execute(); 124 | ``` 125 | 126 | #### create a tag 127 | 128 | ```php 129 | $git->tag()->execute('v1.2.3'); 130 | ``` 131 | 132 | ### Convenience and shortcut methods 133 | 134 | #### list remotes 135 | 136 | ```php 137 | $remotes = $git->remote()->getNames(); 138 | 139 | // array( 140 | // 'origin', 141 | // 'composer', 142 | // ) 143 | ``` 144 | 145 | #### list branches 146 | 147 | ```php 148 | $remotes = $git->branch()->getNames(); 149 | 150 | // array( 151 | // 'master', 152 | // 'hotfix/1.2.3', 153 | // ) 154 | ``` 155 | 156 | #### list remote tracking branches 157 | 158 | ```php 159 | $remotes = $git->branch()->remotes()->->getNames(); 160 | 161 | // array( 162 | // 'origin/master', 163 | // 'origin/hotfix/1.2.3', 164 | // 'origin/release/4.5.6', 165 | // ) 166 | ``` 167 | 168 | #### list branches including remote tracking branches 169 | 170 | ```php 171 | $remotes = $git->branch()->all()->->getNames(); 172 | 173 | // array( 174 | // 'master', 175 | // 'hotfix/1.2.3', 176 | // 'remotes/origin/master', 177 | // 'remotes/origin/hotfix/1.2.3', 178 | // 'remotes/origin/release/4.5.6', 179 | // ) 180 | ``` 181 | 182 | #### get modification status 183 | 184 | ```php 185 | $status = $git->status()->getStatus(); 186 | 187 | // array( 188 | // 'existing-file.txt' => array('index' => 'D', 'worktree' => false), 189 | // 'removed-but-staged.txt' => array('index' => 'D', 'worktree' => 'A'), 190 | // 'staged-file.txt' => array('index' => false, 'worktree' => 'A'), 191 | // 'unknown-file.txt' => array('index' => '?', 'worktree' => '?'), 192 | // ) 193 | ``` 194 | 195 | #### get index modification status 196 | 197 | ```php 198 | $status = $git->status()->getIndexStatus(); 199 | 200 | // array( 201 | // 'existing-file.txt' => 'D', 202 | // 'removed-but-staged.txt' => 'D', 203 | // 'staged-file.txt' => false, 204 | // 'unknown-file.txt' => '?', 205 | // ) 206 | ``` 207 | 208 | #### get worktree modification status 209 | 210 | ```php 211 | $status = $git->status()->getWorkTreeStatus(); 212 | 213 | // array( 214 | // 'existing-file.txt' => 'worktree' => false, 215 | // 'removed-but-staged.txt' => 'worktree' => 'A', 216 | // 'staged-file.txt' => 'worktree' => 'A', 217 | // 'unknown-file.txt' => 'worktree' => '?', 218 | // ) 219 | ``` 220 | -------------------------------------------------------------------------------- /build.default.properties: -------------------------------------------------------------------------------- 1 | ##################################################### 2 | ## This project is using the ## 3 | ## PHP code quality project (phpcq) ## 4 | ## ## 5 | ## https://github.com/phpcq/phpcq ## 6 | ##################################################### 7 | 8 | phpcs.standard=${basedir}/vendor/phpcq/coding-standard/phpcs/PhpCodeQuality/ruleset.xml 9 | phpmd.ruleset=${basedir}/vendor/phpcq/coding-standard/phpmd/ruleset.xml 10 | 11 | phpcpd.excluded=Command 12 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bit3/git-php", 3 | "description": "Easy to use GIT wrapper for PHP.", 4 | "license": "MIT", 5 | "require": { 6 | "php": "^5.6 || ^7.0", 7 | "psr/log": "^1.0", 8 | "symfony/process": "^3.4 || ^4.0 || ^5.0" 9 | }, 10 | "require-dev": { 11 | "symfony/filesystem": "^3.4 || ^4.0 || ^5.0", 12 | "phpcq/all-tasks": "^1.1", 13 | "phpunit/phpunit": "^5.0 || ^6.0 || ^7.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Bit3\\GitPhp\\": "src", 18 | "Bit3\\GitPhp\\Test\\": "tests" 19 | } 20 | }, 21 | "extra": { 22 | "branch-alias": { 23 | "dev-master": "1.5.x-dev" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | ./tests 18 | 19 | 20 | 21 | 22 | ./src 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/Command/AbstractCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Abstract command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.NumberOfChildren) 29 | * 30 | * @deprecated This class is deprecated since 1.5 and where removed in 2.0. Use the trait CommandBuilderTrait. 31 | */ 32 | abstract class AbstractCommandBuilder implements CommandBuilderInterface 33 | { 34 | use CommandBuilderTrait; 35 | } 36 | -------------------------------------------------------------------------------- /src/Command/AddCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Add command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class AddCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'add'; 40 | } 41 | 42 | /** 43 | * Add the dry run option to the command line. 44 | * 45 | * @return AddCommandBuilder 46 | */ 47 | public function dryRun() 48 | { 49 | $this->arguments[] = '--dry-run'; 50 | return $this; 51 | } 52 | 53 | /** 54 | * Add the verbose option to the command line. 55 | * 56 | * @return AddCommandBuilder 57 | */ 58 | public function verbose() 59 | { 60 | $this->arguments[] = '--verbose'; 61 | return $this; 62 | } 63 | 64 | /** 65 | * Add the force option to the command line. 66 | * 67 | * @return AddCommandBuilder 68 | */ 69 | public function force() 70 | { 71 | $this->arguments[] = '--force'; 72 | return $this; 73 | } 74 | 75 | /** 76 | * Add the patch option to the command line. 77 | * 78 | * @return AddCommandBuilder 79 | */ 80 | public function patch() 81 | { 82 | $this->arguments[] = '--patch'; 83 | return $this; 84 | } 85 | 86 | /** 87 | * Add the update option to the command line. 88 | * 89 | * @return AddCommandBuilder 90 | */ 91 | public function update() 92 | { 93 | $this->arguments[] = '--update'; 94 | return $this; 95 | } 96 | 97 | /** 98 | * Add the all option to the command line. 99 | * 100 | * @return AddCommandBuilder 101 | */ 102 | public function all() 103 | { 104 | $this->arguments[] = '--all'; 105 | return $this; 106 | } 107 | 108 | /** 109 | * Add the no-all option to the command line. 110 | * 111 | * @return AddCommandBuilder 112 | */ 113 | public function noAll() 114 | { 115 | $this->arguments[] = '--no-all'; 116 | return $this; 117 | } 118 | 119 | /** 120 | * Add the intent-to-add option to the command line. 121 | * 122 | * @return AddCommandBuilder 123 | */ 124 | public function intentToAdd() 125 | { 126 | $this->arguments[] = '--intent-to-add'; 127 | return $this; 128 | } 129 | 130 | /** 131 | * Add the refresh option to the command line. 132 | * 133 | * @return AddCommandBuilder 134 | */ 135 | public function refresh() 136 | { 137 | $this->arguments[] = '--refresh'; 138 | return $this; 139 | } 140 | 141 | /** 142 | * Add the ignore-errors option to the command line. 143 | * 144 | * @return AddCommandBuilder 145 | */ 146 | public function ignoreErrors() 147 | { 148 | $this->arguments[] = '--ignore-errors'; 149 | return $this; 150 | } 151 | 152 | /** 153 | * Add the ignore-missing option to the command line. 154 | * 155 | * @return AddCommandBuilder 156 | */ 157 | public function ignoreMissing() 158 | { 159 | $this->arguments[] = '--ignore-missing'; 160 | return $this; 161 | } 162 | 163 | /** 164 | * Build the command and execute it. 165 | * 166 | * @param null|string $pathspec Optional path spec to add to the command line. 167 | * 168 | * @param null|string $_ More arguments to append to the command. 169 | * 170 | * @return mixed 171 | * 172 | * @SuppressWarnings(PHPMD.ShortVariableName) 173 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 174 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 175 | */ 176 | public function execute($pathspec = null, $_ = null) 177 | { 178 | $args = \func_get_args(); 179 | if (\count($args)) { 180 | $this->arguments[] = '--'; 181 | foreach ($args as $pathspec) { 182 | $this->arguments[] = $pathspec; 183 | } 184 | } 185 | return $this->run(); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/Command/BranchCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Branch command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class BranchCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const WHEN_ALWAYS = 'always'; 35 | 36 | const WHEN_NEVER = 'never'; 37 | 38 | const WHEN_AUTO = 'auto'; 39 | 40 | /** 41 | * {@inheritDoc} 42 | */ 43 | protected function initializeProcessBuilder() 44 | { 45 | $this->arguments[] = 'branch'; 46 | } 47 | 48 | /** 49 | * Add the D option to the command line. 50 | * 51 | * @return BranchCommandBuilder 52 | */ 53 | public function delete() 54 | { 55 | $this->arguments[] = '-D'; 56 | return $this; 57 | } 58 | 59 | /** 60 | * Add the create-reflog option to the command line. 61 | * 62 | * @return BranchCommandBuilder 63 | */ 64 | public function createReflog() 65 | { 66 | $this->arguments[] = '--create-reflog'; 67 | return $this; 68 | } 69 | 70 | /** 71 | * Add the force option to the command line. 72 | * 73 | * @return BranchCommandBuilder 74 | */ 75 | public function force() 76 | { 77 | $this->arguments[] = '--force'; 78 | return $this; 79 | } 80 | 81 | /** 82 | * Add the M option to the command line. 83 | * 84 | * @param bool|string $oldName Optionally pass the old name. 85 | * 86 | * @return BranchCommandBuilder 87 | */ 88 | public function move($oldName = false) 89 | { 90 | $this->arguments[] = '-M'; 91 | if ($oldName) { 92 | $this->arguments[] = $oldName; 93 | } 94 | return $this; 95 | } 96 | 97 | /** 98 | * Add the color option to the command line. 99 | * 100 | * @param string $when When to use colors. 101 | * 102 | * @return BranchCommandBuilder 103 | * 104 | * @see BranchCommandBuilder::WHEN_ALWAYS 105 | * @see BranchCommandBuilder::WHEN_NEVER 106 | * @see BranchCommandBuilder::WHEN_AUTO. 107 | */ 108 | public function color($when) 109 | { 110 | $this->arguments[] = '--color=' . $when; 111 | return $this; 112 | } 113 | 114 | /** 115 | * Add the no-color option to the command line. 116 | * 117 | * @return BranchCommandBuilder 118 | */ 119 | public function noColor() 120 | { 121 | $this->arguments[] = '--no-color'; 122 | return $this; 123 | } 124 | 125 | /** 126 | * Add the column option to the command line. 127 | * 128 | * @param bool|string $options Optional options to use. 129 | * 130 | * @return BranchCommandBuilder 131 | */ 132 | public function column($options = false) 133 | { 134 | $this->arguments[] = '--column' . ($options ? '=' . $options : ''); 135 | return $this; 136 | } 137 | 138 | /** 139 | * Add the no-column option to the command line. 140 | * 141 | * @return BranchCommandBuilder 142 | */ 143 | public function noColumn() 144 | { 145 | $this->arguments[] = '--no-column'; 146 | return $this; 147 | } 148 | 149 | /** 150 | * Add the remotes option to the command line. 151 | * 152 | * @return BranchCommandBuilder 153 | */ 154 | public function remotes() 155 | { 156 | $this->arguments[] = '--remotes'; 157 | return $this; 158 | } 159 | 160 | /** 161 | * Add the all option to the command line. 162 | * 163 | * @return BranchCommandBuilder 164 | */ 165 | public function all() 166 | { 167 | $this->arguments[] = '--all'; 168 | return $this; 169 | } 170 | 171 | /** 172 | * Add the list option to the command line. 173 | * 174 | * @return BranchCommandBuilder 175 | */ 176 | public function listBranches() 177 | { 178 | $this->arguments[] = '--list'; 179 | return $this; 180 | } 181 | 182 | /** 183 | * Add the verbose option to the command line. 184 | * 185 | * @return BranchCommandBuilder 186 | */ 187 | public function verbose() 188 | { 189 | $this->arguments[] = '--verbose'; 190 | return $this; 191 | } 192 | 193 | /** 194 | * Add the quiet option to the command line. 195 | * 196 | * @return BranchCommandBuilder 197 | */ 198 | public function quiet() 199 | { 200 | $this->arguments[] = '--quiet'; 201 | return $this; 202 | } 203 | 204 | /** 205 | * Add the abbrev option to the command line. 206 | * 207 | * @param int $length The length after which to cut. 208 | * 209 | * @return BranchCommandBuilder 210 | */ 211 | public function abbrev($length) 212 | { 213 | $this->arguments[] = '--abbrev=' . $length; 214 | return $this; 215 | } 216 | 217 | /** 218 | * Add the no-abbrev option to the command line. 219 | * 220 | * @return BranchCommandBuilder 221 | */ 222 | public function noAbbrev() 223 | { 224 | $this->arguments[] = '--no-abbrev'; 225 | return $this; 226 | } 227 | 228 | /** 229 | * Add the track option to the command line. 230 | * 231 | * @return BranchCommandBuilder 232 | */ 233 | public function track() 234 | { 235 | $this->arguments[] = '--track'; 236 | return $this; 237 | } 238 | 239 | /** 240 | * Add the no-track option to the command line. 241 | * 242 | * @return BranchCommandBuilder 243 | */ 244 | public function noTrack() 245 | { 246 | $this->arguments[] = '--no-track'; 247 | return $this; 248 | } 249 | 250 | /** 251 | * Add the set-upstream option to the command line. 252 | * 253 | * @return BranchCommandBuilder 254 | */ 255 | public function setUpstream() 256 | { 257 | $this->arguments[] = '--set-upstream'; 258 | return $this; 259 | } 260 | 261 | /** 262 | * Add the set-upstream-to option to the command line. 263 | * 264 | * @param string $upstream The upstream branch name. 265 | * 266 | * @return BranchCommandBuilder 267 | */ 268 | public function setUpstreamTo($upstream) 269 | { 270 | $this->arguments[] = '--set-upstream-to=' . $upstream; 271 | return $this; 272 | } 273 | 274 | /** 275 | * Add the unset-upstream option to the command line. 276 | * 277 | * @return BranchCommandBuilder 278 | */ 279 | public function unsetUpstream() 280 | { 281 | $this->arguments[] = '--unset-upstream'; 282 | return $this; 283 | } 284 | 285 | /** 286 | * Add the contains option to the command line. 287 | * 288 | * @param string $commit The commit hash. 289 | * 290 | * @return BranchCommandBuilder 291 | */ 292 | public function contains($commit) 293 | { 294 | $this->arguments[] = '--contains'; 295 | $this->arguments[] = $commit; 296 | return $this; 297 | } 298 | 299 | /** 300 | * Add the merged option to the command line. 301 | * 302 | * @param string $commit The commit hash. 303 | * 304 | * @return BranchCommandBuilder 305 | */ 306 | public function merged($commit) 307 | { 308 | $this->arguments[] = '--merged'; 309 | $this->arguments[] = $commit; 310 | return $this; 311 | } 312 | 313 | /** 314 | * Add the no-merged option to the command line. 315 | * 316 | * @param string $commit The commit hash. 317 | * 318 | * @return BranchCommandBuilder 319 | */ 320 | public function noMerged($commit) 321 | { 322 | $this->arguments[] = '--no-merged'; 323 | $this->arguments[] = $commit; 324 | return $this; 325 | } 326 | 327 | /** 328 | * Execute the command and return the result. 329 | * 330 | * @param null|string $branchName Optionally the branch name on which to work on. 331 | * 332 | * @return string 333 | */ 334 | public function execute($branchName = null) 335 | { 336 | if ($branchName) { 337 | $this->arguments[] = $branchName; 338 | } 339 | return $this->run(); 340 | } 341 | 342 | /** 343 | * Retrieve the branch names. 344 | * 345 | * @return string[] 346 | */ 347 | public function getNames() 348 | { 349 | $branches = $this->execute(); 350 | $branches = \explode("\n", $branches); 351 | $branches = \array_map( 352 | function ($branch) { 353 | return \ltrim($branch, '*'); 354 | }, 355 | $branches 356 | ); 357 | $branches = \array_map('trim', $branches); 358 | $branches = \array_filter($branches); 359 | 360 | return $branches; 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /src/Command/CheckoutCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2022 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Checkout command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class CheckoutCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'checkout'; 40 | } 41 | 42 | /** 43 | * Add the quiet option to the command line. 44 | * 45 | * @return CheckoutCommandBuilder 46 | */ 47 | public function quiet() 48 | { 49 | $this->arguments[] = '--quiet'; 50 | return $this; 51 | } 52 | 53 | /** 54 | * Add the force option to the command line. 55 | * 56 | * @return CheckoutCommandBuilder 57 | */ 58 | public function force() 59 | { 60 | $this->arguments[] = '--force'; 61 | return $this; 62 | } 63 | 64 | /** 65 | * Add the ours option to the command line. 66 | * 67 | * @return CheckoutCommandBuilder 68 | */ 69 | public function ours() 70 | { 71 | $this->arguments[] = '--ours'; 72 | return $this; 73 | } 74 | 75 | /** 76 | * Add the theirs option to the command line. 77 | * 78 | * @return CheckoutCommandBuilder 79 | */ 80 | public function theirs() 81 | { 82 | $this->arguments[] = '--theirs'; 83 | return $this; 84 | } 85 | 86 | /** 87 | * Add the b option to the command line. 88 | * 89 | * @return CheckoutCommandBuilder 90 | */ 91 | public function create() 92 | { 93 | $this->arguments[] = '-b'; 94 | return $this; 95 | } 96 | 97 | /** 98 | * Add the B option to the command line. 99 | * 100 | * @return CheckoutCommandBuilder 101 | */ 102 | public function overwrite() 103 | { 104 | $this->arguments[] = '-B'; 105 | return $this; 106 | } 107 | 108 | /** 109 | * Add the track option to the command line. 110 | * 111 | * @return CheckoutCommandBuilder 112 | */ 113 | public function track() 114 | { 115 | $this->arguments[] = '--track'; 116 | return $this; 117 | } 118 | 119 | /** 120 | * Add the no-track option to the command line. 121 | * 122 | * @return CheckoutCommandBuilder 123 | */ 124 | public function noTrack() 125 | { 126 | $this->arguments[] = '--no-track'; 127 | return $this; 128 | } 129 | 130 | /** 131 | * Add the l option to the command line. 132 | * 133 | * @return CheckoutCommandBuilder 134 | */ 135 | public function reflog() 136 | { 137 | $this->arguments[] = '-l'; 138 | return $this; 139 | } 140 | 141 | /** 142 | * Add the orphan option to the command line. 143 | * 144 | * @param null|string $newBranch The name of the new orphan branch. 145 | * 146 | * @return CheckoutCommandBuilder 147 | */ 148 | public function orphan($newBranch = null) 149 | { 150 | $this->arguments[] = '--orphan'; 151 | if ($newBranch) { 152 | $this->arguments[] = $newBranch; 153 | } 154 | return $this; 155 | } 156 | 157 | /** 158 | * Add the ignore-skip-worktree-bits option to the command line. 159 | * 160 | * @return CheckoutCommandBuilder 161 | */ 162 | public function ignoreSkipWorktreeBits() 163 | { 164 | $this->arguments[] = '--ignore-skip-worktree-bits'; 165 | return $this; 166 | } 167 | 168 | /** 169 | * Add the merge option to the command line. 170 | * 171 | * @return CheckoutCommandBuilder 172 | */ 173 | public function merge() 174 | { 175 | $this->arguments[] = '--merge'; 176 | return $this; 177 | } 178 | 179 | /** 180 | * Add the conflict option to the command line. 181 | * 182 | * @param string $style The conflicht handler style. 183 | * 184 | * @return CheckoutCommandBuilder 185 | */ 186 | public function conflict($style) 187 | { 188 | $this->arguments[] = '--conflict=' . $style; 189 | return $this; 190 | } 191 | 192 | /** 193 | * Add the patch option to the command line. 194 | * 195 | * @return CheckoutCommandBuilder 196 | */ 197 | public function patch() 198 | { 199 | $this->arguments[] = '--patch'; 200 | return $this; 201 | } 202 | 203 | /** 204 | * Build the command and execute it. 205 | * 206 | * @param null|string $branchOrTreeIsh Name of the branch or tree. 207 | * 208 | * @param null $path Path to which check out. 209 | * 210 | * @param null|string $_ More optional arguments to append to the command. 211 | * 212 | * @return mixed 213 | * 214 | * @SuppressWarnings(PHPMD.ShortVariableName) 215 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 216 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 217 | */ 218 | public function execute($branchOrTreeIsh = null, $path = null, $_ = null) 219 | { 220 | if ($branchOrTreeIsh) { 221 | $this->arguments[] = $branchOrTreeIsh; 222 | } 223 | 224 | $paths = \func_get_args(); 225 | \array_shift($paths); 226 | if (\count($paths)) { 227 | $this->arguments[] = '--'; 228 | foreach ($paths as $path) { 229 | $this->arguments[] = $path; 230 | } 231 | } 232 | 233 | return $this->run(); 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /src/Command/CloneCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Clone command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class CloneCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'clone'; 40 | } 41 | 42 | /** 43 | * Add the local option to the command line. 44 | * 45 | * @return CloneCommandBuilder 46 | */ 47 | public function local() 48 | { 49 | $this->arguments[] = '--local'; 50 | return $this; 51 | } 52 | 53 | /** 54 | * Add the no-hardlinks option to the command line. 55 | * 56 | * @return CloneCommandBuilder 57 | */ 58 | public function noHardlinks() 59 | { 60 | $this->arguments[] = '--no-hardlinks'; 61 | return $this; 62 | } 63 | 64 | /** 65 | * Add the shared option to the command line. 66 | * 67 | * @return CloneCommandBuilder 68 | */ 69 | public function shared() 70 | { 71 | $this->arguments[] = '--shared'; 72 | return $this; 73 | } 74 | 75 | /** 76 | * Add the reference option to the command line. 77 | * 78 | * @param string $repository The repository name. 79 | * 80 | * @return CloneCommandBuilder 81 | */ 82 | public function reference($repository) 83 | { 84 | $this->arguments[] = '--reference'; 85 | $this->arguments[] = $repository; 86 | return $this; 87 | } 88 | 89 | /** 90 | * Add the quiet option to the command line. 91 | * 92 | * @return CloneCommandBuilder 93 | */ 94 | public function quiet() 95 | { 96 | $this->arguments[] = '--quiet'; 97 | return $this; 98 | } 99 | 100 | /** 101 | * Add the verbose option to the command line. 102 | * 103 | * @return CloneCommandBuilder 104 | */ 105 | public function verbose() 106 | { 107 | $this->arguments[] = '--verbose'; 108 | return $this; 109 | } 110 | 111 | /** 112 | * Add the progress option to the command line. 113 | * 114 | * @return CloneCommandBuilder 115 | */ 116 | public function progress() 117 | { 118 | $this->arguments[] = '--progress'; 119 | return $this; 120 | } 121 | 122 | /** 123 | * Add the no-checkout option to the command line. 124 | * 125 | * @return CloneCommandBuilder 126 | */ 127 | public function noCheckout() 128 | { 129 | $this->arguments[] = '--no-checkout'; 130 | return $this; 131 | } 132 | 133 | /** 134 | * Add the bare option to the command line. 135 | * 136 | * @return CloneCommandBuilder 137 | */ 138 | public function bare() 139 | { 140 | $this->arguments[] = '--bare'; 141 | return $this; 142 | } 143 | 144 | /** 145 | * Add the mirror option to the command line. 146 | * 147 | * @return CloneCommandBuilder 148 | */ 149 | public function mirror() 150 | { 151 | $this->arguments[] = '--mirror'; 152 | return $this; 153 | } 154 | 155 | /** 156 | * Add the origin option to the command line. 157 | * 158 | * @param string $name The name of the origin. 159 | * 160 | * @return CloneCommandBuilder 161 | */ 162 | public function origin($name) 163 | { 164 | $this->arguments[] = '--origin'; 165 | $this->arguments[] = $name; 166 | return $this; 167 | } 168 | 169 | /** 170 | * Add the branch option to the command line. 171 | * 172 | * @param string $name The branch name. 173 | * 174 | * @return CloneCommandBuilder 175 | */ 176 | public function branch($name) 177 | { 178 | $this->arguments[] = '--branch'; 179 | $this->arguments[] = $name; 180 | return $this; 181 | } 182 | 183 | /** 184 | * Add the upload-pack option to the command line. 185 | * 186 | * @param string $uploadPack The upload pack. 187 | * 188 | * @return CloneCommandBuilder 189 | */ 190 | public function uploadPack($uploadPack) 191 | { 192 | $this->arguments[] = '--upload-pack'; 193 | $this->arguments[] = $uploadPack; 194 | return $this; 195 | } 196 | 197 | /** 198 | * Add the template option to the command line. 199 | * 200 | * @param string $templateDirectory The template directory. 201 | * 202 | * @return CloneCommandBuilder 203 | */ 204 | public function template($templateDirectory) 205 | { 206 | $this->arguments[] = '--template=' . $templateDirectory; 207 | return $this; 208 | } 209 | 210 | /** 211 | * Add the config option to the command line. 212 | * 213 | * @param string $key The config key. 214 | * 215 | * @param string $value The config value. 216 | * 217 | * @return CloneCommandBuilder 218 | */ 219 | public function config($key, $value) 220 | { 221 | $this->arguments[] = '--config'; 222 | $this->arguments[] = $key . '=' . $value; 223 | return $this; 224 | } 225 | 226 | /** 227 | * Add the depth option to the command line. 228 | * 229 | * @param string $depth The depth.arguments[] = rn CloneCommandBuildr. 230 | * 231 | * @return CloneCommandBuilder 232 | */ 233 | public function depth($depth) 234 | { 235 | $this->arguments[] = '--depth'; 236 | $this->arguments[] = $depth; 237 | return $this; 238 | } 239 | 240 | /** 241 | * Add the no-single-branch option to the command line. 242 | * 243 | * @return CloneCommandBuilder 244 | */ 245 | public function noSingleBranch() 246 | { 247 | $this->arguments[] = '--no-single-branch'; 248 | return $this; 249 | } 250 | 251 | /** 252 | * Add the single-branch option to the command line. 253 | * 254 | * @return CloneCommandBuilder 255 | */ 256 | public function singleBranch() 257 | { 258 | $this->arguments[] = '--single-branch'; 259 | return $this; 260 | } 261 | 262 | /** 263 | * Add the option to the command line. 264 | * 265 | * @return CloneCommandBuilder 266 | */ 267 | public function recursive() 268 | { 269 | $this->arguments[] = '--recursive'; 270 | return $this; 271 | } 272 | 273 | /** 274 | * Add the separate-git-dir option to the command line. 275 | * 276 | * @param string $gitDir The git dir to use. 277 | * 278 | * @return CloneCommandBuilder 279 | */ 280 | public function separateGitDir($gitDir) 281 | { 282 | $this->arguments[] = '--separate-git-dir=' . $gitDir; 283 | return $this; 284 | } 285 | 286 | /** 287 | * Execute the command and return the result. 288 | * 289 | * @param string $repositoryUrl The url of the repository. 290 | * 291 | * @return mixed 292 | */ 293 | public function execute($repositoryUrl) 294 | { 295 | $this->arguments[] = $repositoryUrl; 296 | $this->arguments[] = $this->repository->getRepositoryPath(); 297 | return $this->run(); 298 | } 299 | } 300 | -------------------------------------------------------------------------------- /src/Command/CommandBuilderInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @copyright 2014 Tristan Lins 17 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 18 | * @link https://github.com/bit3/git-php 19 | * @filesource 20 | */ 21 | 22 | namespace Bit3\GitPhp\Command; 23 | 24 | /** 25 | * Command builder interface. 26 | */ 27 | interface CommandBuilderInterface 28 | { 29 | } 30 | -------------------------------------------------------------------------------- /src/Command/CommandBuilderTrait.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2022 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | use Bit3\GitPhp\GitException; 26 | use Bit3\GitPhp\GitRepository; 27 | use Symfony\Component\Process\Exception\LogicException; 28 | use Symfony\Component\Process\Process; 29 | 30 | /** 31 | * Abstract command builder. 32 | * 33 | * @SuppressWarnings(PHPMD.NumberOfChildren) 34 | */ 35 | trait CommandBuilderTrait 36 | { 37 | /** 38 | * The path to the git repository. 39 | * 40 | * @var GitRepository 41 | */ 42 | public $repository; 43 | 44 | /** 45 | * The directory in their the process called. 46 | * 47 | * @var string 48 | */ 49 | protected $workingDirectory; 50 | 51 | /** 52 | * The arguments for the command line process. 53 | * 54 | * @var array 55 | */ 56 | protected $arguments = []; 57 | 58 | /** 59 | * The process output. 60 | * 61 | * @var null|string 62 | */ 63 | protected $output; 64 | 65 | /** 66 | * Flag if we want to dry run. 67 | * 68 | * @var bool 69 | */ 70 | protected $dryRun = false; 71 | 72 | /** 73 | * Constructor. 74 | * 75 | * @param GitRepository $repository The git repository to work on. 76 | */ 77 | public function __construct(GitRepository $repository) 78 | { 79 | $this->repository = $repository; 80 | $this->workingDirectory = $repository->getRepositoryPath(); 81 | $this->arguments[] = $this->repository->getConfig()->getGitExecutablePath(); 82 | 83 | $this->initializeProcessBuilder(); 84 | } 85 | 86 | /** 87 | * Enable dry run. If dry run is enabled, the execute() method return the executed command. 88 | * 89 | * @return $this 90 | */ 91 | public function enableDryRun() 92 | { 93 | $this->dryRun = true; 94 | return $this; 95 | } 96 | 97 | /** 98 | * Initialize the process builder. 99 | * 100 | * @return void 101 | */ 102 | abstract protected function initializeProcessBuilder(); 103 | 104 | /** 105 | * Retrieve the output text. 106 | * 107 | * @return null|string 108 | */ 109 | public function getOutput() 110 | { 111 | return $this->output; 112 | } 113 | 114 | /** 115 | * Build the the command line process. 116 | * 117 | * @return Process 118 | * 119 | * @throws LogicException In case no arguments have been provided. 120 | */ 121 | protected function buildProcess() 122 | { 123 | if (!\count($this->arguments)) { 124 | throw new LogicException('You must add command arguments before the process can build.'); 125 | } 126 | 127 | $process = new Process($this->arguments, $this->workingDirectory); 128 | 129 | return $process; 130 | } 131 | 132 | /** 133 | * Execute the command. 134 | * 135 | * @return mixed Depend on the command. 136 | * 137 | * @throws GitException When the command is executed the second time or could not be executed. 138 | */ 139 | protected function run() 140 | { 141 | $process = $this->buildProcess(); 142 | 143 | if ($this->output !== null) { 144 | throw new GitException( 145 | 'Command cannot be executed twice', 146 | $process->getWorkingDirectory(), 147 | $process->getCommandLine(), 148 | $this->output, 149 | '' 150 | ); 151 | } 152 | 153 | $this->repository->getConfig()->getLogger()->debug( 154 | \sprintf('[ccabs-repository-git] exec [%s] %s', $this->workingDirectory, $process->getCommandLine()) 155 | ); 156 | 157 | if ($this->dryRun) { 158 | return $process->getCommandLine(); 159 | } 160 | 161 | $process->run(); 162 | $this->output = $process->getOutput(); 163 | $this->output = \rtrim($this->output, "\r\n"); 164 | 165 | if (!$process->isSuccessful()) { 166 | throw GitException::createFromProcess('Could not execute git command', $process); 167 | } 168 | 169 | return $this->output; 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/Command/CommitCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2022 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Commit command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class CommitCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const CLEANUP_STRIP = 'strip'; 35 | 36 | const CLEANUP_WHITESPACE = 'whitespace'; 37 | 38 | const CLEANUP_VERBATIM = 'verbatim'; 39 | 40 | const CLEANUP_DEFAULT = 'default'; 41 | 42 | const UNTRACKED_FILES_NO = 'no'; 43 | 44 | const UNTRACKED_FILES_NORMAL = 'normal'; 45 | 46 | const UNTRACKED_FILES_ALL = 'all'; 47 | 48 | /** 49 | * Flag determining if gpg signing is desired. 50 | * 51 | * @var bool 52 | */ 53 | protected $gpgSignIsset = false; 54 | 55 | /** 56 | * {@inheritDoc} 57 | */ 58 | protected function initializeProcessBuilder() 59 | { 60 | $this->arguments[] = 'commit'; 61 | } 62 | 63 | /** 64 | * Add the option to the command line. 65 | * 66 | * @return CommitCommandBuilder 67 | */ 68 | public function all() 69 | { 70 | $this->arguments[] = '--all'; 71 | return $this; 72 | } 73 | 74 | /** 75 | * Add the patch option to the command line. 76 | * 77 | * @return CommitCommandBuilder 78 | */ 79 | public function patch() 80 | { 81 | $this->arguments[] = '--patch'; 82 | return $this; 83 | } 84 | 85 | /** 86 | * Add the reuse-message option to the command line. 87 | * 88 | * @param string $commit The commit from which the message should be reused. 89 | * 90 | * @return CommitCommandBuilder 91 | */ 92 | public function reuseMessage($commit) 93 | { 94 | $this->arguments[] = '--reuse-message=' . $commit; 95 | return $this; 96 | } 97 | 98 | /** 99 | * Add the fixup option to the command line. 100 | * 101 | * @param string $commit The commit to fixup. 102 | * 103 | * @return CommitCommandBuilder 104 | */ 105 | public function fixup($commit) 106 | { 107 | $this->arguments[] = '--fixup=' . $commit; 108 | return $this; 109 | } 110 | 111 | /** 112 | * Add the squash option to the command line. 113 | * 114 | * @param string $commit The commit to squash. 115 | * 116 | * @return CommitCommandBuilder 117 | */ 118 | public function squash($commit) 119 | { 120 | $this->arguments[] = '--squash=' . $commit; 121 | return $this; 122 | } 123 | 124 | /** 125 | * Add the reset-author option to the command line. 126 | * 127 | * @return CommitCommandBuilder 128 | */ 129 | public function resetAuthor() 130 | { 131 | $this->arguments[] = '--reset-author'; 132 | return $this; 133 | } 134 | 135 | /** 136 | * Add the short option to the command line. 137 | * 138 | * @return CommitCommandBuilder 139 | */ 140 | public function short() 141 | { 142 | $this->arguments[] = '--short'; 143 | return $this; 144 | } 145 | 146 | /** 147 | * Add the branch option to the command line. 148 | * 149 | * @return CommitCommandBuilder 150 | */ 151 | public function branch() 152 | { 153 | $this->arguments[] = '--branch'; 154 | return $this; 155 | } 156 | 157 | /** 158 | * Add the porcelain option to the command line. 159 | * 160 | * @return CommitCommandBuilder 161 | */ 162 | public function porcelain() 163 | { 164 | $this->arguments[] = '--porcelain'; 165 | return $this; 166 | } 167 | 168 | /** 169 | * Add the long option to the command line. 170 | * 171 | * @return CommitCommandBuilder 172 | */ 173 | public function long() 174 | { 175 | $this->arguments[] = '--long'; 176 | return $this; 177 | } 178 | 179 | /** 180 | * Add the null option to the command line. 181 | * 182 | * @return CommitCommandBuilder 183 | */ 184 | public function null() 185 | { 186 | $this->arguments[] = '--null'; 187 | return $this; 188 | } 189 | 190 | /** 191 | * Add the file option to the command line. 192 | * 193 | * @param string $file The file. 194 | * 195 | * @return CommitCommandBuilder 196 | */ 197 | public function file($file) 198 | { 199 | $this->arguments[] = '--file=' . $file; 200 | return $this; 201 | } 202 | 203 | /** 204 | * Add the author option to the command line. 205 | * 206 | * @param string $author The author to use. 207 | * 208 | * @return CommitCommandBuilder 209 | */ 210 | public function author($author) 211 | { 212 | $this->arguments[] = '--author=' . $author; 213 | return $this; 214 | } 215 | 216 | /** 217 | * Add the date option to the command line. 218 | * 219 | * @param \DateTime|string $date The timestamp to use for the commit (if string: Y-m-d H:i:s). 220 | * 221 | * @return CommitCommandBuilder 222 | */ 223 | public function date($date) 224 | { 225 | if ($date instanceof \DateTime) { 226 | $date = $date->format('Y-m-d H:i:s'); 227 | } 228 | $this->arguments[] = '--date=' . $date; 229 | return $this; 230 | } 231 | 232 | /** 233 | * Add the message option to the command line. 234 | * 235 | * @param string $message The message to use. 236 | * 237 | * @return CommitCommandBuilder 238 | */ 239 | public function message($message) 240 | { 241 | $this->arguments[] = '--message=' . $message; 242 | return $this; 243 | } 244 | 245 | /** 246 | * Add the template option to the command line. 247 | * 248 | * @param string $file The template file. 249 | * 250 | * @return CommitCommandBuilder 251 | */ 252 | public function template($file) 253 | { 254 | $this->arguments[] = '--template=' . $file; 255 | return $this; 256 | } 257 | 258 | /** 259 | * Add the signoff option to the command line. 260 | * 261 | * @return CommitCommandBuilder 262 | */ 263 | public function signoff() 264 | { 265 | $this->arguments[] = '--signoff'; 266 | return $this; 267 | } 268 | 269 | /** 270 | * Add the no-verify option to the command line. 271 | * 272 | * @return CommitCommandBuilder 273 | */ 274 | public function noVerity() 275 | { 276 | $this->arguments[] = '--no-verify'; 277 | return $this; 278 | } 279 | 280 | /** 281 | * Add the allow-empty option to the command line. 282 | * 283 | * @return CommitCommandBuilder 284 | */ 285 | public function allowEmpty() 286 | { 287 | $this->arguments[] = '--allow-empty'; 288 | return $this; 289 | } 290 | 291 | /** 292 | * Add the allow-empty-message option to the command line. 293 | * 294 | * @return CommitCommandBuilder 295 | */ 296 | public function allowEmptyMessage() 297 | { 298 | $this->arguments[] = '--allow-empty-message'; 299 | return $this; 300 | } 301 | 302 | /** 303 | * Add the cleanup option to the command line. 304 | * 305 | * @param string $mode The cleanup mode. 306 | * 307 | * @return CommitCommandBuilder 308 | */ 309 | public function cleanup($mode) 310 | { 311 | $this->arguments[] = '--cleanup=' . $mode; 312 | return $this; 313 | } 314 | 315 | /** 316 | * Add the amend option to the command line. 317 | * 318 | * @return CommitCommandBuilder 319 | */ 320 | public function amend() 321 | { 322 | $this->arguments[] = '--amend'; 323 | return $this; 324 | } 325 | 326 | /** 327 | * Add the no-post-rewrite option to the command line. 328 | * 329 | * @return CommitCommandBuilder 330 | */ 331 | public function noPostRewrite() 332 | { 333 | $this->arguments[] = '--no-post-rewrite'; 334 | return $this; 335 | } 336 | 337 | /** 338 | * Add the include option to the command line. 339 | * 340 | * @return CommitCommandBuilder 341 | */ 342 | public function includ() 343 | { 344 | $this->arguments[] = '--include'; 345 | return $this; 346 | } 347 | 348 | /** 349 | * Add the only option to the command line. 350 | * 351 | * @return CommitCommandBuilder 352 | */ 353 | public function only() 354 | { 355 | $this->arguments[] = '--only'; 356 | return $this; 357 | } 358 | 359 | /** 360 | * Add the untracked-files option to the command line. 361 | * 362 | * @param null|string $mode How to handle untracked files. 363 | * 364 | * @return CommitCommandBuilder 365 | */ 366 | public function untrackedFiles($mode = null) 367 | { 368 | $this->arguments[] = '--untracked-files' . ($mode ? '=' . $mode : ''); 369 | return $this; 370 | } 371 | 372 | /** 373 | * Add the verbose option to the command line. 374 | * 375 | * @return CommitCommandBuilder 376 | */ 377 | public function verbose() 378 | { 379 | $this->arguments[] = '--verbose'; 380 | return $this; 381 | } 382 | 383 | /** 384 | * Add the quiet option to the command line. 385 | * 386 | * @return CommitCommandBuilder 387 | */ 388 | public function quiet() 389 | { 390 | $this->arguments[] = '--quiet'; 391 | return $this; 392 | } 393 | 394 | /** 395 | * Add the dry-run option to the command line. 396 | * 397 | * @return CommitCommandBuilder 398 | */ 399 | public function dryRun() 400 | { 401 | $this->arguments[] = '--dry-run'; 402 | return $this; 403 | } 404 | 405 | /** 406 | * Add the status option to the command line. 407 | * 408 | * @return CommitCommandBuilder 409 | */ 410 | public function status() 411 | { 412 | $this->arguments[] = '--status'; 413 | return $this; 414 | } 415 | 416 | /** 417 | * Add the no-status option to the command line. 418 | * 419 | * @return CommitCommandBuilder 420 | */ 421 | public function noStatus() 422 | { 423 | $this->arguments[] = '--no-status'; 424 | return $this; 425 | } 426 | 427 | /** 428 | * Add the option to the command line. 429 | * 430 | * @param null|string $keyId Optional id of the GPG key to use. 431 | * 432 | * @return CommitCommandBuilder 433 | */ 434 | public function gpgSign($keyId = null) 435 | { 436 | $this->gpgSignIsset = true; 437 | $this->arguments[] = '--gpg-sign' . ($keyId ? '=' . $keyId : ''); 438 | return $this; 439 | } 440 | 441 | /** 442 | * Build the command and execute it. 443 | * 444 | * @param null $pathspec Path to commit. 445 | * 446 | * @param null|string $_ More optional pathes to commit. 447 | * 448 | * @return string 449 | * 450 | * @SuppressWarnings(PHPMD.ShortVariableName) 451 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 452 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 453 | */ 454 | public function execute($pathspec = null, $_ = null) 455 | { 456 | // prevent launching the editor 457 | $this->arguments[] = '--no-edit'; 458 | 459 | if (!$this->gpgSignIsset && $this->repository->getConfig()->isSignCommitsEnabled()) { 460 | $this->gpgSign($this->repository->getConfig()->getSignCommitUser()); 461 | } 462 | 463 | $args = \func_get_args(); 464 | if (\count($args)) { 465 | $this->arguments[] = '--'; 466 | foreach ($args as $pathspec) { 467 | $this->arguments[] = $pathspec; 468 | } 469 | } 470 | 471 | return $this->run(); 472 | } 473 | } 474 | -------------------------------------------------------------------------------- /src/Command/ConfigCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Matthew Gamble 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Config command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class ConfigCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'config'; 40 | } 41 | 42 | /** 43 | * Specify a single config file that should be looked at. 44 | * Has special support for the global, system and local flags. 45 | * 46 | * @param string $file The config file to use. 47 | * 48 | * @return ConfigCommandBuilder 49 | */ 50 | public function file($file) 51 | { 52 | if (\in_array($file, ['global', 'system', 'local'])) { 53 | $this->arguments[] = '--' . $file; 54 | } else { 55 | $this->arguments[] = '--file'; 56 | $this->arguments[] = $file; 57 | } 58 | return $this; 59 | } 60 | 61 | /** 62 | * Add the blob option to the command line. 63 | * 64 | * See "SPECIFYING REVISIONS" section in gitrevisions(7) for a list of ways to spell blob names. 65 | * 66 | * @param string $blobId The blob id to use. 67 | * 68 | * @return ConfigCommandBuilder 69 | */ 70 | public function blob($blobId) 71 | { 72 | $this->arguments[] = '--blob ' . $blobId; 73 | return $this; 74 | } 75 | 76 | /** 77 | * Add a type option to the command line. 78 | * 79 | * @param string $type The type name. 80 | * 81 | * @return ConfigCommandBuilder 82 | * 83 | * @throws \InvalidArgumentException When an invalid type name is encountered. 84 | */ 85 | public function type($type) 86 | { 87 | if (\in_array($type, ['bool', 'int', 'bool-or-int', 'path'])) { 88 | $this->arguments[] = '--' . $type; 89 | } else { 90 | throw new \InvalidArgumentException('Invalid configuration type supplied.'); 91 | } 92 | return $this; 93 | } 94 | 95 | /** 96 | * Add the add option and associated parameters to the command line. 97 | * 98 | * @param string $name The config value name. 99 | * 100 | * @param string $value The value to use. 101 | * 102 | * @return ConfigCommandBuilder 103 | */ 104 | public function add($name, $value) 105 | { 106 | $this->arguments[] = '--add'; 107 | $this->arguments[] = $name; 108 | $this->arguments[] = $value; 109 | return $this; 110 | } 111 | 112 | /** 113 | * Add the replace-all option and associated parameters to the command line. 114 | * 115 | * @param string $name The config value name. 116 | * 117 | * @param string $value The value to use. 118 | * 119 | * @param null|string $valueRegex The value regex to use. 120 | * 121 | * @return ConfigCommandBuilder 122 | */ 123 | public function replaceAll($name, $value, $valueRegex = null) 124 | { 125 | $this->arguments[] = '--replace-all'; 126 | $this->arguments[] = $name; 127 | $this->arguments[] = $value; 128 | if ($valueRegex !== null) { 129 | $this->arguments[] = $valueRegex; 130 | } 131 | return $this; 132 | } 133 | 134 | /** 135 | * Adds the NUL byte termination option to the command line. 136 | * 137 | * @return ConfigCommandBuilder 138 | */ 139 | public function terminateWithNUL() 140 | { 141 | $this->arguments[] = '--null'; 142 | return $this; 143 | } 144 | 145 | /** 146 | * Add the get option and associated parameters to the command line. 147 | * 148 | * @param string $name The config value name. 149 | * 150 | * @param null|string $valueRegex The value regex to use. 151 | * 152 | * @return ConfigCommandBuilder 153 | */ 154 | public function get($name, $valueRegex = null) 155 | { 156 | $this->arguments[] = '--get'; 157 | $this->addNameAndPattern($name, $valueRegex); 158 | return $this; 159 | } 160 | 161 | /** 162 | * Add the get-all option and associated parameters to the command line. 163 | * 164 | * @param string $name The config value name. 165 | * 166 | * @param null|string $valueRegex The value regex to use. 167 | * 168 | * @return ConfigCommandBuilder 169 | */ 170 | public function getAll($name, $valueRegex = null) 171 | { 172 | $this->arguments[] = '--get-all'; 173 | $this->addNameAndPattern($name, $valueRegex); 174 | return $this; 175 | } 176 | 177 | /** 178 | * Add the get-regexp option and associated parameters to the command line. 179 | * 180 | * @param string $nameRegex The config name regex. 181 | * 182 | * @param null|string $valueRegex The value regex to use. 183 | * 184 | * @return ConfigCommandBuilder 185 | */ 186 | public function getRegexp($nameRegex, $valueRegex = null) 187 | { 188 | $this->arguments[] = '--get-regexp'; 189 | $this->addNameAndPattern($nameRegex, $valueRegex); 190 | return $this; 191 | } 192 | 193 | /** 194 | * Add the get-urlmatch option and associated parameters to the command line. 195 | * 196 | * @param string $name The config name. 197 | * 198 | * @param string $url The url to match. 199 | * 200 | * @return ConfigCommandBuilder 201 | */ 202 | public function getUrlmatch($name, $url) 203 | { 204 | $this->arguments[] = '--get-urlmatch'; 205 | $this->addNameAndPattern($name, $url); 206 | return $this; 207 | } 208 | 209 | /** 210 | * Add the unset option and associated parameters to the command line. 211 | * 212 | * @param string $name The name of the config value to unset. 213 | * 214 | * @param null|string $valueRegex The value regex to use. 215 | * 216 | * @return ConfigCommandBuilder 217 | */ 218 | public function unsetOpt($name, $valueRegex = null) 219 | { 220 | $this->arguments[] = '--unset'; 221 | $this->addNameAndPattern($name, $valueRegex); 222 | return $this; 223 | } 224 | 225 | /** 226 | * Add the unset-all option and associated parameters to the command line. 227 | * 228 | * @param string $name The name of the config value to unset. 229 | * 230 | * @param null|string $valueRegex The value regex to use. 231 | * 232 | * @return ConfigCommandBuilder 233 | */ 234 | public function unsetAll($name, $valueRegex = null) 235 | { 236 | $this->arguments[] = '--unset-all'; 237 | $this->addNameAndPattern($name, $valueRegex); 238 | return $this; 239 | } 240 | 241 | /** 242 | * Add the passed name and the passed pattern if not null. 243 | * 244 | * @param string $name The name to add. 245 | * 246 | * @param null|string $pattern The pattern to add (if not null). 247 | * 248 | * @return void 249 | */ 250 | private function addNameAndPattern($name, $pattern) 251 | { 252 | $this->arguments[] = $name; 253 | if ($pattern !== null) { 254 | $this->arguments[] = $pattern; 255 | } 256 | } 257 | 258 | /** 259 | * Add the rename-section option and associated parameters to the command line. 260 | * 261 | * @param string $oldName The old section name. 262 | * 263 | * @param string $newName The new section name. 264 | * 265 | * @return ConfigCommandBuilder 266 | */ 267 | public function renameSection($oldName, $newName) 268 | { 269 | $this->arguments[] = '--rename-section'; 270 | $this->arguments[] = $oldName; 271 | $this->arguments[] = $newName; 272 | return $this; 273 | } 274 | 275 | /** 276 | * Add the remove-section option to the command line. 277 | * 278 | * @param string $name The section name. 279 | * 280 | * @return ConfigCommandBuilder 281 | */ 282 | public function removeSection($name) 283 | { 284 | $this->arguments[] = '--remove-section'; 285 | $this->arguments[] = $name; 286 | return $this; 287 | } 288 | 289 | /** 290 | * Add the list option to the command line. 291 | * 292 | * @return ConfigCommandBuilder 293 | */ 294 | public function listOpt() 295 | { 296 | $this->arguments[] = '--list'; 297 | return $this; 298 | } 299 | 300 | /** 301 | * Add the get-color option and associated parameters to the command line. 302 | * 303 | * @param string $name The name of the color to retrieve. 304 | * 305 | * @param null|string $default The default value to return. 306 | * 307 | * @return ConfigCommandBuilder 308 | */ 309 | public function getColor($name, $default = null) 310 | { 311 | $this->arguments[] = '--get-color'; 312 | $this->arguments[] = $name; 313 | if ($default !== null) { 314 | $this->arguments[] = $default; 315 | } 316 | return $this; 317 | } 318 | 319 | /** 320 | * Add the get-colorbool option and associated parameters to the command line. 321 | * 322 | * @param string $name The name of the color to check. 323 | * 324 | * @param bool|null $stdoutIsTty Flag if stdout is a tty. 325 | * 326 | * @return ConfigCommandBuilder 327 | */ 328 | public function getColorBool($name, $stdoutIsTty = null) 329 | { 330 | $this->arguments[] = '--get-colorbool'; 331 | $this->arguments[] = $name; 332 | if (\is_bool($stdoutIsTty)) { 333 | $this->arguments[] = $stdoutIsTty ? 'true' : 'false'; 334 | } 335 | return $this; 336 | } 337 | 338 | /** 339 | * Add the includes or no-includes option to the command line. 340 | * 341 | * @param bool $allow Flag if include.* directives in config files shall be respected when looking up values. 342 | * 343 | * @return ConfigCommandBuilder 344 | */ 345 | public function includes($allow = true) 346 | { 347 | $this->arguments[] = $allow ? '--includes' : '--no-includes'; 348 | return $this; 349 | } 350 | 351 | /** 352 | * Build the command and execute it. 353 | * 354 | * @param null|string $name Pass name of setting here to perform a basic get or set operation. 355 | * 356 | * @param null|string $value Pass a value for the setting to turn this into a set command. 357 | * 358 | * @param null|string $valueRegex Pass a regex to limit changing of a multi-value setting to certain settings. 359 | * 360 | * @return mixed 361 | * 362 | * @SuppressWarnings(PHPMD.ShortVariableName) 363 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 364 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 365 | */ 366 | public function execute($name = null, $value = null, $valueRegex = null) 367 | { 368 | if ($name !== null) { 369 | $this->arguments[] = $name; 370 | if ($value !== null) { 371 | $this->arguments[] = $value; 372 | if ($valueRegex !== null) { 373 | $this->arguments[] = $valueRegex; 374 | } 375 | } 376 | } 377 | return $this->run(); 378 | } 379 | } 380 | -------------------------------------------------------------------------------- /src/Command/DescribeCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Describe command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class DescribeCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'describe'; 40 | } 41 | 42 | /** 43 | * Add the dirty option to the command line. 44 | * 45 | * @param bool|string $mark The mark to use. 46 | * 47 | * @return DescribeCommandBuilder 48 | */ 49 | public function dirty($mark = false) 50 | { 51 | $this->arguments[] = '--dirty'; 52 | if ($mark) { 53 | $this->arguments[] = $mark; 54 | } 55 | return $this; 56 | } 57 | 58 | /** 59 | * Add the all option to the command line. 60 | * 61 | * @return DescribeCommandBuilder 62 | */ 63 | public function all() 64 | { 65 | $this->arguments[] = '--all'; 66 | return $this; 67 | } 68 | 69 | /** 70 | * Add the tags option to the command line. 71 | * 72 | * @return DescribeCommandBuilder 73 | */ 74 | public function tags() 75 | { 76 | $this->arguments[] = '--tags'; 77 | return $this; 78 | } 79 | 80 | /** 81 | * Add the contains option to the command line. 82 | * 83 | * @return DescribeCommandBuilder 84 | */ 85 | public function contains() 86 | { 87 | $this->arguments[] = '--contains'; 88 | return $this; 89 | } 90 | 91 | /** 92 | * Add the abbrev option to the command line. 93 | * 94 | * @param int $n Chars after which to abbreviate. 95 | * 96 | * @return DescribeCommandBuilder 97 | * 98 | * @SuppressWarnings(PHPMD.ShortVariableName) 99 | */ 100 | public function abbrev($n) 101 | { 102 | $this->arguments[] = '--abbrev=' . $n; 103 | return $this; 104 | } 105 | 106 | /** 107 | * Add the candidates option to the command line. 108 | * 109 | * @param int $n Amount of candidates. 110 | * 111 | * @return DescribeCommandBuilder 112 | * 113 | * @SuppressWarnings(PHPMD.ShortVariableName) 114 | */ 115 | public function candidates($n) 116 | { 117 | $this->arguments[] = '--candidates=' . $n; 118 | return $this; 119 | } 120 | 121 | /** 122 | * Add the exact-match option to the command line. 123 | * 124 | * @return DescribeCommandBuilder 125 | */ 126 | public function exactMatch() 127 | { 128 | $this->arguments[] = '--exact-match'; 129 | return $this; 130 | } 131 | 132 | /** 133 | * Add the debug option to the command line. 134 | * 135 | * @return DescribeCommandBuilder 136 | */ 137 | public function debug() 138 | { 139 | $this->arguments[] = '--debug'; 140 | return $this; 141 | } 142 | 143 | /** 144 | * Add the long option to the command line. 145 | * 146 | * @return DescribeCommandBuilder 147 | */ 148 | public function long() 149 | { 150 | $this->arguments[] = '--long'; 151 | return $this; 152 | } 153 | 154 | /** 155 | * Add the match option to the command line. 156 | * 157 | * @param string $pattern The pattern to use for filtering. 158 | * 159 | * @return DescribeCommandBuilder 160 | */ 161 | public function match($pattern) 162 | { 163 | $this->arguments[] = '--match'; 164 | $this->arguments[] = $pattern; 165 | return $this; 166 | } 167 | 168 | /** 169 | * Add the always option to the command line. 170 | * 171 | * @return DescribeCommandBuilder 172 | */ 173 | public function always() 174 | { 175 | $this->arguments[] = '--always'; 176 | return $this; 177 | } 178 | 179 | /** 180 | * Add the first-parent option to the command line. 181 | * 182 | * @return DescribeCommandBuilder 183 | */ 184 | public function firstParent() 185 | { 186 | $this->arguments[] = '--first-parent'; 187 | return $this; 188 | } 189 | 190 | /** 191 | * Execute the command and return the result. 192 | * 193 | * @param string $commit The commit to describe (defaults to HEAD). 194 | * 195 | * @return string 196 | */ 197 | public function execute($commit = 'HEAD') 198 | { 199 | $this->arguments[] = $commit; 200 | return $this->run(); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/Command/FetchCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Fetch command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class FetchCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const RECURSE_SUBMODULES_YES = 'yes'; 35 | 36 | const RECURSE_SUBMODULES_ON_DEMAND = 'on-demand'; 37 | 38 | const RECURSE_SUBMODULES_NO = 'no'; 39 | 40 | /** 41 | * {@inheritDoc} 42 | */ 43 | protected function initializeProcessBuilder() 44 | { 45 | $this->arguments[] = 'fetch'; 46 | } 47 | 48 | /** 49 | * Add the all option to the command line. 50 | * 51 | * @return FetchCommandBuilder 52 | */ 53 | public function all() 54 | { 55 | $this->arguments[] = '--all'; 56 | return $this; 57 | } 58 | 59 | /** 60 | * Add the append option to the command line. 61 | * 62 | * @return FetchCommandBuilder 63 | */ 64 | public function append() 65 | { 66 | $this->arguments[] = '--append'; 67 | return $this; 68 | } 69 | 70 | /** 71 | * Add the depth option to the command line. 72 | * 73 | * @param int $depth The commit depth to use. 74 | * 75 | * @return FetchCommandBuilder 76 | */ 77 | public function depth($depth) 78 | { 79 | $this->arguments[] = '--depth=' . $depth; 80 | return $this; 81 | } 82 | 83 | /** 84 | * Add the unshallow option to the command line. 85 | * 86 | * @return FetchCommandBuilder 87 | */ 88 | public function unshallow() 89 | { 90 | $this->arguments[] = '--unshallow'; 91 | return $this; 92 | } 93 | 94 | /** 95 | * Add the update-shallow option to the command line. 96 | * 97 | * @return FetchCommandBuilder 98 | */ 99 | public function updateShallow() 100 | { 101 | $this->arguments[] = '--update-shallow'; 102 | return $this; 103 | } 104 | 105 | /** 106 | * Add the dry-run option to the command line. 107 | * 108 | * @return FetchCommandBuilder 109 | */ 110 | public function dryRun() 111 | { 112 | $this->arguments[] = '--dry-run'; 113 | return $this; 114 | } 115 | 116 | /** 117 | * Add the force option to the command line. 118 | * 119 | * @return FetchCommandBuilder 120 | */ 121 | public function force() 122 | { 123 | $this->arguments[] = '--force'; 124 | return $this; 125 | } 126 | 127 | /** 128 | * Add the keep option to the command line. 129 | * 130 | * @return FetchCommandBuilder 131 | */ 132 | public function keep() 133 | { 134 | $this->arguments[] = '--keep'; 135 | return $this; 136 | } 137 | 138 | /** 139 | * Add the multiple option to the command line. 140 | * 141 | * @return FetchCommandBuilder 142 | */ 143 | public function multiple() 144 | { 145 | $this->arguments[] = '--multiple'; 146 | return $this; 147 | } 148 | 149 | /** 150 | * Add the prune option to the command line. 151 | * 152 | * @return FetchCommandBuilder 153 | */ 154 | public function prune() 155 | { 156 | $this->arguments[] = '--prune'; 157 | return $this; 158 | } 159 | 160 | /** 161 | * Add the no-tags option to the command line. 162 | * 163 | * @return FetchCommandBuilder 164 | */ 165 | public function noTags() 166 | { 167 | $this->arguments[] = '--no-tags'; 168 | return $this; 169 | } 170 | 171 | /** 172 | * Add the tags option to the command line. 173 | * 174 | * @return FetchCommandBuilder 175 | */ 176 | public function tags() 177 | { 178 | $this->arguments[] = '--tags'; 179 | return $this; 180 | } 181 | 182 | /** 183 | * Add the recurse-submodules option to the command line. 184 | * 185 | * @param bool|string $recurse The value. 186 | * 187 | * @return FetchCommandBuilder 188 | */ 189 | public function recurseSubmodules($recurse = false) 190 | { 191 | $this->arguments[] = '--recurse-submodules' . ($recurse ? '=' . $recurse : ''); 192 | return $this; 193 | } 194 | 195 | /** 196 | * Add the no-recurse-submodules option to the command line. 197 | * 198 | * @return FetchCommandBuilder 199 | */ 200 | public function noRecurseSubmodules() 201 | { 202 | $this->arguments[] = '--no-recurse-submodules'; 203 | return $this; 204 | } 205 | 206 | /** 207 | * Add the submodule-prefix option to the command line. 208 | * 209 | * @param string $path The path. 210 | * 211 | * @return FetchCommandBuilder 212 | */ 213 | public function submodulePrefix($path) 214 | { 215 | $this->arguments[] = '--submodule-prefix=' . $path; 216 | return $this; 217 | } 218 | 219 | /** 220 | * Add the recurse-submodules-default option to the command line. 221 | * 222 | * @param string $recurse The value. 223 | * 224 | * @return FetchCommandBuilder 225 | */ 226 | public function recurseSubmodulesDefault($recurse) 227 | { 228 | $this->arguments[] = '--recurse-submodules-default=' . $recurse; 229 | return $this; 230 | } 231 | 232 | /** 233 | * Add the update-head-ok option to the command line. 234 | * 235 | * @return FetchCommandBuilder 236 | */ 237 | public function updateHeadOk() 238 | { 239 | $this->arguments[] = '--update-head-ok'; 240 | return $this; 241 | } 242 | 243 | /** 244 | * Add the upload-pack option to the command line. 245 | * 246 | * @param string $uploadPack The pack. 247 | * 248 | * @return FetchCommandBuilder 249 | */ 250 | public function uploadPack($uploadPack) 251 | { 252 | $this->arguments[] = '--upload-pack'; 253 | $this->arguments[] = $uploadPack; 254 | return $this; 255 | } 256 | 257 | /** 258 | * Add the quiet option to the command line. 259 | * 260 | * @return FetchCommandBuilder 261 | */ 262 | public function quiet() 263 | { 264 | $this->arguments[] = '--quiet'; 265 | return $this; 266 | } 267 | 268 | /** 269 | * Add the verbose option to the command line. 270 | * 271 | * @return FetchCommandBuilder 272 | */ 273 | public function verbose() 274 | { 275 | $this->arguments[] = '--verbose'; 276 | return $this; 277 | } 278 | 279 | /** 280 | * Build the command and execute it. 281 | * 282 | * @param string $repository Name of the remote to use (Defaults to origin). 283 | * 284 | * @param null|string $refspec Refspec to fetch. 285 | * 286 | * @param null|string $_ More optional refspecs to commit. 287 | * 288 | * @return string 289 | * 290 | * @SuppressWarnings(PHPMD.ShortVariableName) 291 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 292 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 293 | */ 294 | public function execute($repository = 'origin', $refspec = null, $_ = null) 295 | { 296 | $this->arguments[] = $repository; 297 | 298 | $refspecs = \func_get_args(); 299 | \array_shift($refspecs); 300 | foreach ($refspecs as $refspec) { 301 | $this->arguments[] = $refspec; 302 | } 303 | 304 | return $this->run(); 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /src/Command/InitCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Init command builder. 27 | */ 28 | class InitCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | const SHARE_FALSE = 'false'; 33 | 34 | const SHARE_TRUE = 'true'; 35 | 36 | const SHARE_UMASK = 'umask'; 37 | 38 | const SHARE_GROUP = 'group'; 39 | 40 | const SHARE_ALL = 'all'; 41 | 42 | const SHARE_WORLD = 'world'; 43 | 44 | const SHARE_EVERYBODY = 'everybody'; 45 | 46 | /** 47 | * {@inheritDoc} 48 | */ 49 | protected function initializeProcessBuilder() 50 | { 51 | $this->arguments[] = 'init'; 52 | } 53 | 54 | /** 55 | * Add the quiet option to the command line. 56 | * 57 | * @return InitCommandBuilder 58 | */ 59 | public function quiet() 60 | { 61 | $this->arguments[] = '--quiet'; 62 | return $this; 63 | } 64 | 65 | /** 66 | * Add the bare option to the command line. 67 | * 68 | * @return InitCommandBuilder 69 | */ 70 | public function bare() 71 | { 72 | $this->arguments[] = '--bare'; 73 | return $this; 74 | } 75 | 76 | /** 77 | * Add the template option to the command line. 78 | * 79 | * @param string $templateDirectory Path to the template directory. 80 | * 81 | * @return InitCommandBuilder 82 | */ 83 | public function template($templateDirectory) 84 | { 85 | $this->arguments[] = '--template=' . $templateDirectory; 86 | return $this; 87 | } 88 | 89 | /** 90 | * Add the separate-git-dir option to the command line. 91 | * 92 | * @param string $gitDir Path to the .git dir. 93 | * 94 | * @return InitCommandBuilder 95 | */ 96 | public function separateGitDir($gitDir) 97 | { 98 | $this->arguments[] = '--separate-git-dir=' . $gitDir; 99 | return $this; 100 | } 101 | 102 | /** 103 | * Add the shared option to the command line. 104 | * 105 | * @param string $share The share value. 106 | * 107 | * @return InitCommandBuilder 108 | */ 109 | public function shared($share) 110 | { 111 | $this->arguments[] = '--shared=' . $share; 112 | return $this; 113 | } 114 | 115 | /** 116 | * Execute the command and return the output. 117 | * 118 | * @return string 119 | */ 120 | public function execute() 121 | { 122 | $this->arguments[] = $this->repository->getRepositoryPath(); 123 | return $this->run(); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Command/LsRemoteCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Ls remote command builder. 27 | */ 28 | class LsRemoteCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function initializeProcessBuilder() 36 | { 37 | $this->arguments[] = 'ls-remote'; 38 | } 39 | 40 | /** 41 | * Add the heads option to the command line. 42 | * 43 | * @return LsRemoteCommandBuilder 44 | */ 45 | public function heads() 46 | { 47 | $this->arguments[] = '--heads'; 48 | return $this; 49 | } 50 | 51 | /** 52 | * Add the tags option to the command line. 53 | * 54 | * @return LsRemoteCommandBuilder 55 | */ 56 | public function tags() 57 | { 58 | $this->arguments[] = '--tags'; 59 | return $this; 60 | } 61 | 62 | /** 63 | * Add the upload-pack option to the command line. 64 | * 65 | * @param string $exec The value. 66 | * 67 | * @return LsRemoteCommandBuilder 68 | */ 69 | public function uploadPack($exec) 70 | { 71 | $this->arguments[] = '--upload-pack'; 72 | $this->arguments[] = $exec; 73 | return $this; 74 | } 75 | 76 | /** 77 | * Add the exit-code option to the command line. 78 | * 79 | * @return LsRemoteCommandBuilder 80 | */ 81 | public function exitCode() 82 | { 83 | $this->arguments[] = '--exit-code'; 84 | return $this; 85 | } 86 | 87 | /** 88 | * Build the command and execute it. 89 | * 90 | * @param string $remote Name of the remote. 91 | * 92 | * @param null|string $refSpec Ref spec to list. 93 | * 94 | * @param null|string $_ More optional ref specs to log. 95 | * 96 | * @return string 97 | * 98 | * @SuppressWarnings(PHPMD.ShortVariableName) 99 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 100 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 101 | */ 102 | public function execute($remote, $refSpec = null, $_ = null) 103 | { 104 | $this->arguments[] = $remote; 105 | 106 | $refSpec = \func_get_args(); 107 | \array_shift($refSpec); 108 | foreach ($refSpec as $ref) { 109 | $this->arguments[] = $ref; 110 | } 111 | 112 | return $this->run(); 113 | } 114 | 115 | /** 116 | * Return a list of remote names. 117 | * 118 | * @param string $remote Name of the remote. 119 | * 120 | * @param null|string $refSpec Ref spec to list. 121 | * 122 | * @param null|string $_ More optional ref specs to log. 123 | * 124 | * @return array 125 | * 126 | * @SuppressWarnings(PHPMD.ShortVariableName) 127 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 128 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 129 | */ 130 | public function getRefs($remote, $refSpec = null, $_ = null) 131 | { 132 | $output = \call_user_func_array([$this, 'execute'], \func_get_args()); 133 | $output = \explode("\n", $output); 134 | $output = \array_map('trim', $output); 135 | $output = \array_filter($output); 136 | 137 | $refs = []; 138 | 139 | foreach ($output as $line) { 140 | $line = \preg_split('~\s+~', $line); 141 | 142 | if ('^{}' != \substr($line[1], -3)) { 143 | $refs[$line[1]] = $line[0]; 144 | } 145 | } 146 | 147 | return $refs; 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/Command/MergeCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Aaron Rubin 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2022 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Merge command builder. 27 | */ 28 | class MergeCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function initializeProcessBuilder() 36 | { 37 | $this->arguments[] = 'merge'; 38 | } 39 | 40 | /** 41 | * Add the quiet option to the command line. 42 | * 43 | * @return MergeCommandBuilder 44 | */ 45 | public function quiet() 46 | { 47 | $this->arguments[] = '--quiet'; 48 | return $this; 49 | } 50 | 51 | /** 52 | * Add the strategy option to the command line with the given strategy. 53 | * 54 | * @param string $strategy Strategy to use when merging. 55 | * 56 | * @return MergeCommandBuilder 57 | */ 58 | public function strategy($strategy) 59 | { 60 | $this->arguments[] = '--strategy=' . $strategy; 61 | return $this; 62 | } 63 | 64 | /** 65 | * Build the command and execute it. 66 | * 67 | * @param null|string $branchOrTreeIsh Name of the branch or tree. 68 | * 69 | * @param null $path Path to which check out. 70 | * 71 | * @param null|string $_ More optional arguments to append to the command. 72 | * 73 | * @return mixed 74 | * 75 | * @SuppressWarnings(PHPMD.ShortVariableName) 76 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 77 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 78 | */ 79 | public function execute($branchOrTreeIsh = null, $path = null, $_ = null) 80 | { 81 | if ($branchOrTreeIsh) { 82 | $this->arguments[] = $branchOrTreeIsh; 83 | } 84 | 85 | $paths = \func_get_args(); 86 | \array_shift($paths); 87 | if (\count($paths)) { 88 | $this->arguments[] = '--'; 89 | foreach ($paths as $path) { 90 | $this->arguments[] = $path; 91 | } 92 | } 93 | 94 | return $this->run(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Command/PullCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Ahmad Marzouq 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @author Idris Dose 18 | * @copyright 2014-2022 Tristan Lins 19 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 20 | * @link https://github.com/bit3/git-php 21 | * @filesource 22 | */ 23 | 24 | namespace Bit3\GitPhp\Command; 25 | 26 | /** 27 | * Pull command builder. 28 | * 29 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 30 | */ 31 | class PullCommandBuilder implements CommandBuilderInterface 32 | { 33 | use CommandBuilderTrait; 34 | 35 | /** 36 | * {@inheritDoc} 37 | */ 38 | protected function initializeProcessBuilder() 39 | { 40 | $this->arguments[] = 'pull'; 41 | } 42 | 43 | /** 44 | * Add the quiet option to the command line. 45 | * 46 | * @return PullCommandBuilder 47 | */ 48 | public function quiet() 49 | { 50 | $this->arguments[] = '--quiet'; 51 | return $this; 52 | } 53 | 54 | /** 55 | * Add the verbose option to the command line. 56 | * 57 | * @return PullCommandBuilder 58 | */ 59 | public function verbose() 60 | { 61 | $this->arguments[] = '--verbose'; 62 | return $this; 63 | } 64 | 65 | /** 66 | * Add the recurse-submodules option to the command line. 67 | * 68 | * @param string $recurse The value. 69 | * 70 | * @return PullCommandBuilder 71 | */ 72 | public function recurseSubmodules($recurse) 73 | { 74 | $this->arguments[] = '--recurse-submodules=' . $recurse; 75 | return $this; 76 | } 77 | 78 | 79 | /** 80 | * Build the command and execute it. 81 | * 82 | * @param string $repository Name of the remote to pull from. 83 | * 84 | * @param null|string $refspec Ref spec to pull. 85 | * 86 | * @param null|string $_ More optional ref specs to pull. 87 | * 88 | * @return string 89 | * 90 | * @SuppressWarnings(PHPMD.ShortVariableName) 91 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 92 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 93 | */ 94 | public function execute($repository = 'origin', $refspec = null, $_ = null) 95 | { 96 | $this->arguments[] = $repository; 97 | 98 | $refspecs = \func_get_args(); 99 | \array_shift($refspecs); 100 | foreach ($refspecs as $refspec) { 101 | $this->arguments[] = $refspec; 102 | } 103 | 104 | return $this->run(); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/Command/PushCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Push command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class PushCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const RECURSE_SUBMODULES_CHECK = 'check'; 35 | 36 | const RECURSE_SUBMODULES_ON_DEMAND = 'on-demand'; 37 | 38 | /** 39 | * {@inheritDoc} 40 | */ 41 | protected function initializeProcessBuilder() 42 | { 43 | $this->arguments[] = 'push'; 44 | } 45 | 46 | /** 47 | * Add the all option to the command line. 48 | * 49 | * @return PushCommandBuilder 50 | */ 51 | public function all() 52 | { 53 | $this->arguments[] = '--all'; 54 | return $this; 55 | } 56 | 57 | /** 58 | * Add the prune option to the command line. 59 | * 60 | * @return PushCommandBuilder 61 | */ 62 | public function prune() 63 | { 64 | $this->arguments[] = '--prune'; 65 | return $this; 66 | } 67 | 68 | /** 69 | * Add the mirror option to the command line. 70 | * 71 | * @return PushCommandBuilder 72 | */ 73 | public function mirror() 74 | { 75 | $this->arguments[] = '--mirror'; 76 | return $this; 77 | } 78 | 79 | /** 80 | * Add the dry-run option to the command line. 81 | * 82 | * @return PushCommandBuilder 83 | */ 84 | public function dryRun() 85 | { 86 | $this->arguments[] = '--dry-run'; 87 | return $this; 88 | } 89 | 90 | /** 91 | * Add the porcelain option to the command line. 92 | * 93 | * @return PushCommandBuilder 94 | */ 95 | public function porcelain() 96 | { 97 | $this->arguments[] = '--porcelain'; 98 | return $this; 99 | } 100 | 101 | /** 102 | * Add the delete option to the command line. 103 | * 104 | * @return PushCommandBuilder 105 | */ 106 | public function delete() 107 | { 108 | $this->arguments[] = '--delete'; 109 | return $this; 110 | } 111 | 112 | /** 113 | * Add the tags option to the command line. 114 | * 115 | * @return PushCommandBuilder 116 | */ 117 | public function tags() 118 | { 119 | $this->arguments[] = '--tags'; 120 | return $this; 121 | } 122 | 123 | /** 124 | * Add the follow-tags option to the command line. 125 | * 126 | * @return PushCommandBuilder 127 | */ 128 | public function followTags() 129 | { 130 | $this->arguments[] = '--follow-tags'; 131 | return $this; 132 | } 133 | 134 | /** 135 | * Add the receive-pack option to the command line. 136 | * 137 | * @param string $gitReceivePack The value. 138 | * 139 | * @return PushCommandBuilder 140 | */ 141 | public function receivePack($gitReceivePack) 142 | { 143 | $this->arguments[] = '--receive-pack=' . $gitReceivePack; 144 | return $this; 145 | } 146 | 147 | /** 148 | * Add the option to the command line. 149 | * 150 | * @param null|string $refname The ref name. 151 | * 152 | * @param null $expect The expect value. 153 | * 154 | * @return PushCommandBuilder 155 | */ 156 | public function forceWithLease($refname, $expect = null) 157 | { 158 | $this->arguments[] = '--force-with-lease' . ($refname ? ('=' . $refname . ($expect ? ':' . $expect : '')) : ''); 159 | return $this; 160 | } 161 | 162 | /** 163 | * Add the no-force-with-lease option to the command line. 164 | * 165 | * @return PushCommandBuilder 166 | */ 167 | public function noForceWithLease() 168 | { 169 | $this->arguments[] = '--no-force-with-lease'; 170 | return $this; 171 | } 172 | 173 | /** 174 | * Add the force option to the command line. 175 | * 176 | * @return PushCommandBuilder 177 | */ 178 | public function force() 179 | { 180 | $this->arguments[] = '--force'; 181 | return $this; 182 | } 183 | 184 | /** 185 | * Add the repo option to the command line. 186 | * 187 | * @param string $repository The repository name. 188 | * 189 | * @return PushCommandBuilder 190 | */ 191 | public function repo($repository) 192 | { 193 | $this->arguments[] = '--repo=' . $repository; 194 | return $this; 195 | } 196 | 197 | /** 198 | * Add the set-upstream option to the command line. 199 | * 200 | * @return PushCommandBuilder 201 | */ 202 | public function setUpstream() 203 | { 204 | $this->arguments[] = '--set-upstream'; 205 | return $this; 206 | } 207 | 208 | /** 209 | * Add the thin option to the command line. 210 | * 211 | * @return PushCommandBuilder 212 | */ 213 | public function thin() 214 | { 215 | $this->arguments[] = '--thin'; 216 | return $this; 217 | } 218 | 219 | /** 220 | * Add the no-thin option to the command line. 221 | * 222 | * @return PushCommandBuilder 223 | */ 224 | public function noThin() 225 | { 226 | $this->arguments[] = '--no-thin'; 227 | return $this; 228 | } 229 | 230 | /** 231 | * Add the quiet option to the command line. 232 | * 233 | * @return PushCommandBuilder 234 | */ 235 | public function quiet() 236 | { 237 | $this->arguments[] = '--quiet'; 238 | return $this; 239 | } 240 | 241 | /** 242 | * Add the verbose option to the command line. 243 | * 244 | * @return PushCommandBuilder 245 | */ 246 | public function verbose() 247 | { 248 | $this->arguments[] = '--verbose'; 249 | return $this; 250 | } 251 | 252 | /** 253 | * Add the recurse-submodules option to the command line. 254 | * 255 | * @param string $recurse The value. 256 | * 257 | * @return PushCommandBuilder 258 | */ 259 | public function recurseSubmodules($recurse) 260 | { 261 | $this->arguments[] = '--recurse-submodules=' . $recurse; 262 | return $this; 263 | } 264 | 265 | /** 266 | * Add the verify option to the command line. 267 | * 268 | * @return PushCommandBuilder 269 | */ 270 | public function verify() 271 | { 272 | $this->arguments[] = '--verify'; 273 | return $this; 274 | } 275 | 276 | /** 277 | * Add the no-verify option to the command line. 278 | * 279 | * @return PushCommandBuilder 280 | */ 281 | public function noVerify() 282 | { 283 | $this->arguments[] = '--no-verify'; 284 | return $this; 285 | } 286 | 287 | /** 288 | * Build the command and execute it. 289 | * 290 | * @param string $repository Name of the remote to push to. 291 | * 292 | * @param null|string $refspec Ref spec to push. 293 | * 294 | * @param null|string $_ More optional ref specs to push. 295 | * 296 | * @return string 297 | * 298 | * @SuppressWarnings(PHPMD.ShortVariableName) 299 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 300 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 301 | */ 302 | public function execute($repository, $refspec = null, $_ = null) 303 | { 304 | $this->arguments[] = $repository; 305 | 306 | $refspecs = \func_get_args(); 307 | \array_shift($refspecs); 308 | foreach ($refspecs as $refspec) { 309 | $this->arguments[] = $refspec; 310 | } 311 | 312 | return $this->run(); 313 | } 314 | } 315 | -------------------------------------------------------------------------------- /src/Command/RemoteCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Remote command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class RemoteCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'remote'; 40 | } 41 | 42 | /** 43 | * Add the option to the command line. 44 | * 45 | * @return RemoteCommandBuilder 46 | */ 47 | public function verbose() 48 | { 49 | $this->arguments[] = '--verbose'; 50 | return $this; 51 | } 52 | 53 | /** 54 | * Add the add option to the command line. 55 | * 56 | * @param string $name Name of the new remote to add. 57 | * 58 | * @param string $url URL to the new remote. 59 | * 60 | * @return RemoteCommandBuilder 61 | */ 62 | public function add($name, $url) 63 | { 64 | $this->arguments[] = 'add'; 65 | $this->arguments[] = $name; 66 | $this->arguments[] = $url; 67 | return $this; 68 | } 69 | 70 | /** 71 | * Add the rename option to the command line. 72 | * 73 | * @param string $new The new name. 74 | * 75 | * @param null|string $old The old name. 76 | * 77 | * @return RemoteCommandBuilder 78 | */ 79 | public function rename($new, $old = null) 80 | { 81 | $this->arguments[] = 'rename'; 82 | if ($old) { 83 | $this->arguments[] = $old; 84 | } 85 | $this->arguments[] = $new; 86 | return $this; 87 | } 88 | 89 | /** 90 | * Add the remove option to the command line. 91 | * 92 | * @param string $name The name of the remote to remove. 93 | * 94 | * @return RemoteCommandBuilder 95 | */ 96 | public function remove($name) 97 | { 98 | $this->arguments[] = 'remove'; 99 | $this->arguments[] = $name; 100 | return $this; 101 | } 102 | 103 | /** 104 | * Add the set-head option to the command line. 105 | * 106 | * @param string $name The name of the head. 107 | * 108 | * @param string $branch The name of the branch. 109 | * 110 | * @return RemoteCommandBuilder 111 | */ 112 | public function setHead($name, $branch) 113 | { 114 | $this->arguments[] = 'set-head'; 115 | $this->arguments[] = $name; 116 | $this->arguments[] = $branch; 117 | return $this; 118 | } 119 | 120 | /** 121 | * Add the set-head option to the command line. 122 | * 123 | * @param string $name The name of the head. 124 | * 125 | * @return RemoteCommandBuilder 126 | */ 127 | public function setHeadAuto($name) 128 | { 129 | $this->arguments[] = 'set-head'; 130 | $this->arguments[] = $name; 131 | $this->arguments[] = '--auto'; 132 | return $this; 133 | } 134 | 135 | /** 136 | * Add the set-head option to the command line. 137 | * 138 | * @param string $name The name of the head. 139 | * 140 | * @return RemoteCommandBuilder 141 | */ 142 | public function setHeadDelete($name) 143 | { 144 | $this->arguments[] = 'set-head'; 145 | $this->arguments[] = $name; 146 | $this->arguments[] = '--delete'; 147 | return $this; 148 | } 149 | 150 | /** 151 | * Add the set-branches option to the command line. 152 | * 153 | * @param string $name Name of the remote. 154 | * 155 | * @param string $branch Name of the branch. 156 | * 157 | * @param bool $add Flag if the name should be added. 158 | * 159 | * @return RemoteCommandBuilder 160 | */ 161 | public function setBranches($name, $branch, $add = false) 162 | { 163 | $this->arguments[] = 'set-branches'; 164 | if ($add) { 165 | $this->arguments[] = '--add'; 166 | } 167 | $this->arguments[] = $name; 168 | $this->arguments[] = $branch; 169 | return $this; 170 | } 171 | 172 | /** 173 | * Add the set-url option to the command line. 174 | * 175 | * @param string $name Name of the remote. 176 | * 177 | * @param string $url The URL. 178 | * 179 | * @param null|string $oldUrl The old URL. 180 | * 181 | * @return RemoteCommandBuilder 182 | */ 183 | public function setUrl($name, $url, $oldUrl = null) 184 | { 185 | $this->arguments[] = 'set-url'; 186 | $this->arguments[] = $name; 187 | $this->arguments[] = $url; 188 | if ($oldUrl) { 189 | $this->arguments[] = $oldUrl; 190 | } 191 | return $this; 192 | } 193 | 194 | /** 195 | * Add the set-url option to the command line. 196 | * 197 | * @param string $name Name of the remote. 198 | * 199 | * @param string $url The URL. 200 | * 201 | * @param null|string $oldUrl The old URL. 202 | * 203 | * @return RemoteCommandBuilder 204 | */ 205 | public function setPushUrl($name, $url, $oldUrl = null) 206 | { 207 | $this->arguments[] = 'set-url'; 208 | $this->arguments[] = $name; 209 | $this->arguments[] = '--push'; 210 | $this->arguments[] = $url; 211 | if ($oldUrl) { 212 | $this->arguments[] = $oldUrl; 213 | } 214 | return $this; 215 | } 216 | 217 | /** 218 | * Add the set-url option to the command line. 219 | * 220 | * @param string $name Name of the remote. 221 | * 222 | * @param string $url The URL. 223 | * 224 | * @return RemoteCommandBuilder 225 | */ 226 | public function addUrl($name, $url) 227 | { 228 | $this->arguments[] = 'set-url'; 229 | $this->arguments[] = '--add'; 230 | $this->arguments[] = $name; 231 | $this->arguments[] = $url; 232 | return $this; 233 | } 234 | 235 | /** 236 | * Add the set-url option to the command line. 237 | * 238 | * @param string $name Name of the remote. 239 | * 240 | * @param string $url The URL. 241 | * 242 | * @return RemoteCommandBuilder 243 | */ 244 | public function addPushUrl($name, $url) 245 | { 246 | $this->arguments[] = 'set-url'; 247 | $this->arguments[] = '--add'; 248 | $this->arguments[] = '--push'; 249 | $this->arguments[] = $name; 250 | $this->arguments[] = $url; 251 | return $this; 252 | } 253 | 254 | /** 255 | * Add the set-url option to the command line. 256 | * 257 | * @param string $name Name of the remote. 258 | * 259 | * @param string $url The URL. 260 | * 261 | * @return RemoteCommandBuilder 262 | */ 263 | public function deleteUrl($name, $url) 264 | { 265 | $this->arguments[] = 'set-url'; 266 | $this->arguments[] = '--delete'; 267 | $this->arguments[] = $name; 268 | $this->arguments[] = $url; 269 | return $this; 270 | } 271 | 272 | /** 273 | * Add the set-url option to the command line. 274 | * 275 | * @param string $name Name of the remote. 276 | * 277 | * @param string $url The URL. 278 | * 279 | * @return RemoteCommandBuilder 280 | */ 281 | public function deletePushUrl($name, $url) 282 | { 283 | $this->arguments[] = 'set-url'; 284 | $this->arguments[] = '--delete'; 285 | $this->arguments[] = '--push'; 286 | $this->arguments[] = $name; 287 | $this->arguments[] = $url; 288 | return $this; 289 | } 290 | 291 | /** 292 | * Add the show option to the command line. 293 | * 294 | * @param string $name Name of the remote. 295 | * 296 | * @return RemoteCommandBuilder 297 | */ 298 | public function show($name) 299 | { 300 | $this->arguments[] = 'show'; 301 | $this->arguments[] = $name; 302 | return $this; 303 | } 304 | 305 | /** 306 | * Add the prune option to the command line. 307 | * 308 | * @param string $name Name of the remote. 309 | * 310 | * @param bool $dryRun Flag if a dry run shall be done. 311 | * 312 | * @return RemoteCommandBuilder 313 | */ 314 | public function prune($name, $dryRun = false) 315 | { 316 | $this->arguments[] = 'prune'; 317 | if ($dryRun) { 318 | $this->arguments[] = '--dry-run'; 319 | } 320 | $this->arguments[] = $name; 321 | return $this; 322 | } 323 | 324 | /** 325 | * Add the update option to the command line. 326 | * 327 | * @param string $groupOrRemote Name of the remote or a remote group. 328 | * 329 | * @param bool $prune Flag if the remote shall be pruned. 330 | * 331 | * @return RemoteCommandBuilder 332 | */ 333 | public function update($groupOrRemote, $prune = false) 334 | { 335 | $this->arguments[] = 'update'; 336 | if ($prune) { 337 | $this->arguments[] = '--prune'; 338 | } 339 | $this->arguments[] = $groupOrRemote; 340 | return $this; 341 | } 342 | 343 | /** 344 | * Execute the command and return the output. 345 | * 346 | * @return string 347 | */ 348 | public function execute() 349 | { 350 | return $this->run(); 351 | } 352 | 353 | /** 354 | * Return a list of remote names. 355 | * 356 | * @return array 357 | */ 358 | public function getNames() 359 | { 360 | $remotes = $this->execute(); 361 | $remotes = \explode("\n", $remotes); 362 | $remotes = \array_map('trim', $remotes); 363 | $remotes = \array_filter($remotes); 364 | 365 | return $remotes; 366 | } 367 | } 368 | -------------------------------------------------------------------------------- /src/Command/ResetCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Reset command builder. 27 | */ 28 | class ResetCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function initializeProcessBuilder() 36 | { 37 | $this->arguments[] = 'reset'; 38 | } 39 | 40 | /** 41 | * Add the quiet option to the command line. 42 | * 43 | * @return ResetCommandBuilder 44 | */ 45 | public function quiet() 46 | { 47 | $this->arguments[] = '--quiet'; 48 | return $this; 49 | } 50 | 51 | /** 52 | * Add the patch option to the command line. 53 | * 54 | * @return ResetCommandBuilder 55 | */ 56 | public function patch() 57 | { 58 | $this->arguments[] = '--patch'; 59 | return $this; 60 | } 61 | 62 | /** 63 | * Add the soft option to the command line. 64 | * 65 | * @return ResetCommandBuilder 66 | */ 67 | public function soft() 68 | { 69 | $this->arguments[] = '--soft'; 70 | return $this; 71 | } 72 | 73 | /** 74 | * Add the mixed option to the command line. 75 | * 76 | * @return ResetCommandBuilder 77 | */ 78 | public function mixed() 79 | { 80 | $this->arguments[] = '--mixed'; 81 | return $this; 82 | } 83 | 84 | /** 85 | * Add the hard option to the command line. 86 | * 87 | * @return ResetCommandBuilder 88 | */ 89 | public function hard() 90 | { 91 | $this->arguments[] = '--hard'; 92 | return $this; 93 | } 94 | 95 | /** 96 | * Add the merge option to the command line. 97 | * 98 | * @return ResetCommandBuilder 99 | */ 100 | public function merge() 101 | { 102 | $this->arguments[] = '--merge'; 103 | return $this; 104 | } 105 | 106 | /** 107 | * Add the keep option to the command line. 108 | * 109 | * @return ResetCommandBuilder 110 | */ 111 | public function keep() 112 | { 113 | $this->arguments[] = '--keep'; 114 | return $this; 115 | } 116 | 117 | /** 118 | * Add the commit to the command line. 119 | * 120 | * @param string $commit A commit hash. 121 | * 122 | * @return ResetCommandBuilder 123 | */ 124 | public function commit($commit) 125 | { 126 | $this->arguments[] = $commit; 127 | return $this; 128 | } 129 | 130 | /** 131 | * Build the command and execute it. 132 | * 133 | * @param null|string $path Path to reset. 134 | * 135 | * @param null|string $_ More optional pathes to reset. 136 | * 137 | * @return string 138 | * 139 | * @SuppressWarnings(PHPMD.ShortVariableName) 140 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 141 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 142 | */ 143 | public function execute($path = null, $_ = null) 144 | { 145 | $this->arguments[] = '--'; 146 | foreach (\func_get_args() as $path) { 147 | $this->arguments[] = $path; 148 | } 149 | return $this->run(); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /src/Command/RevParseCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Radek Crlik 17 | * @author Sven Baumann 18 | * @copyright 2014-2018 Tristan Lins 19 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 20 | * @link https://github.com/bit3/git-php 21 | * @filesource 22 | */ 23 | 24 | namespace Bit3\GitPhp\Command; 25 | 26 | /** 27 | * Rev-parse command builder. 28 | * 29 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 30 | */ 31 | class RevParseCommandBuilder implements CommandBuilderInterface 32 | { 33 | use CommandBuilderTrait; 34 | 35 | const ABBREV_REF_STRICT = 'strict'; 36 | 37 | const ABBREV_REF_LOOSE = 'loose'; 38 | 39 | /** 40 | * {@inheritDoc} 41 | */ 42 | protected function initializeProcessBuilder() 43 | { 44 | $this->arguments[] = 'rev-parse'; 45 | } 46 | 47 | /** 48 | * Add the parseopt option to the command line. 49 | * 50 | * @return RevParseCommandBuilder 51 | */ 52 | public function parseopt() 53 | { 54 | $this->arguments[] = '--parseopt'; 55 | return $this; 56 | } 57 | 58 | /** 59 | * Add the keep-dashdash option to the command line. 60 | * 61 | * @return RevParseCommandBuilder 62 | */ 63 | public function keepDashDash() 64 | { 65 | $this->arguments[] = '--keep-dashdash'; 66 | return $this; 67 | } 68 | 69 | /** 70 | * Add the stop-at-non-option option to the command line. 71 | * 72 | * @return RevParseCommandBuilder 73 | */ 74 | public function stopAtNonOption() 75 | { 76 | $this->arguments[] = '--stop-at-non-option'; 77 | return $this; 78 | } 79 | 80 | /** 81 | * Add the stuck-long option to the command line. 82 | * 83 | * @return RevParseCommandBuilder 84 | */ 85 | public function stuckLong() 86 | { 87 | $this->arguments[] = '--stuck-long'; 88 | return $this; 89 | } 90 | 91 | /** 92 | * Add the sq-quote option to the command line. 93 | * 94 | * @return RevParseCommandBuilder 95 | */ 96 | public function sqQuote() 97 | { 98 | $this->arguments[] = '--sq-quote'; 99 | return $this; 100 | } 101 | 102 | /** 103 | * Add the revs-only option to the command line. 104 | * 105 | * @return RevParseCommandBuilder 106 | */ 107 | public function revsOnly() 108 | { 109 | $this->arguments[] = '--revs-only'; 110 | return $this; 111 | } 112 | 113 | /** 114 | * Add the no-revs option to the command line. 115 | * 116 | * @return RevParseCommandBuilder 117 | */ 118 | public function noRevs() 119 | { 120 | $this->arguments[] = '--no-revs'; 121 | return $this; 122 | } 123 | 124 | /** 125 | * Add the flags option to the command line. 126 | * 127 | * @return RevParseCommandBuilder 128 | */ 129 | public function flags() 130 | { 131 | $this->arguments[] = '--flags'; 132 | return $this; 133 | } 134 | 135 | /** 136 | * Add the no-flags option to the command line. 137 | * 138 | * @return RevParseCommandBuilder 139 | */ 140 | public function noFlags() 141 | { 142 | $this->arguments[] = '--no-flags'; 143 | return $this; 144 | } 145 | 146 | /** 147 | * Add the default option to the command line. 148 | * 149 | * @param string $arg Name of the default rev. 150 | * 151 | * @return RevParseCommandBuilder 152 | */ 153 | public function defaultRev($arg) 154 | { 155 | $this->arguments[] = '--default'; 156 | $this->arguments[] = $arg; 157 | return $this; 158 | } 159 | 160 | /** 161 | * Add the prefix option to the command line. 162 | * 163 | * @param string $arg The prefix. 164 | * 165 | * @return RevParseCommandBuilder 166 | */ 167 | public function prefix($arg) 168 | { 169 | $this->arguments[] = '--prefix'; 170 | $this->arguments[] = $arg; 171 | return $this; 172 | } 173 | 174 | /** 175 | * Add the verify option to the command line. 176 | * 177 | * @return RevParseCommandBuilder 178 | */ 179 | public function verify() 180 | { 181 | $this->arguments[] = '--verify'; 182 | return $this; 183 | } 184 | 185 | /** 186 | * Add the quiet option to the command line. 187 | * 188 | * @return RevParseCommandBuilder 189 | */ 190 | public function quiet() 191 | { 192 | $this->arguments[] = '--quiet'; 193 | return $this; 194 | } 195 | 196 | /** 197 | * Add the sq option to the command line. 198 | * 199 | * @return RevParseCommandBuilder 200 | * 201 | * @SuppressWarnings(PHPMD.ShortMethodName) 202 | */ 203 | public function sq() 204 | { 205 | $this->arguments[] = '--sq'; 206 | return $this; 207 | } 208 | 209 | /** 210 | * Add the not option to the command line. 211 | * 212 | * @return RevParseCommandBuilder 213 | */ 214 | public function not() 215 | { 216 | $this->arguments[] = '--not'; 217 | return $this; 218 | } 219 | 220 | /** 221 | * Add the abbref-ref option to the command line. 222 | * 223 | * @param null|string $abbrev The value. 224 | * 225 | * @return RevParseCommandBuilder 226 | */ 227 | public function abbrevRef($abbrev = null) 228 | { 229 | $this->arguments[] = '--abbrev-ref' . ($abbrev ? '=' . $abbrev : ''); 230 | return $this; 231 | } 232 | 233 | /** 234 | * Add the short option to the command line. 235 | * 236 | * @param null|int $number The amount. 237 | * 238 | * @return RevParseCommandBuilder 239 | */ 240 | public function short($number = null) 241 | { 242 | $this->arguments[] = '--short' . ($number ? '=' . $number : ''); 243 | return $this; 244 | } 245 | 246 | /** 247 | * Add the symbolic option to the command line. 248 | * 249 | * @return RevParseCommandBuilder 250 | */ 251 | public function symbolic() 252 | { 253 | $this->arguments[] = '--symbolic'; 254 | return $this; 255 | } 256 | 257 | /** 258 | * Add the symbolic-full-name option to the command line. 259 | * 260 | * @return RevParseCommandBuilder 261 | */ 262 | public function symbolicFullName() 263 | { 264 | $this->arguments[] = '--symbolic-full-name'; 265 | return $this; 266 | } 267 | 268 | /** 269 | * Add the all option to the command line. 270 | * 271 | * @return RevParseCommandBuilder 272 | */ 273 | public function all() 274 | { 275 | $this->arguments[] = '--all'; 276 | return $this; 277 | } 278 | 279 | /** 280 | * Add the branches option to the command line. 281 | * 282 | * @param string $pattern The pattern. 283 | * 284 | * @return RevParseCommandBuilder 285 | */ 286 | public function branches($pattern) 287 | { 288 | $this->arguments[] = '--branches=' . $pattern; 289 | return $this; 290 | } 291 | 292 | /** 293 | * Add the tags option to the command line. 294 | * 295 | * @param string $pattern The pattern. 296 | * 297 | * @return RevParseCommandBuilder 298 | */ 299 | public function tags($pattern) 300 | { 301 | $this->arguments[] = '--tags=' . $pattern; 302 | return $this; 303 | } 304 | 305 | /** 306 | * Add the remotes option to the command line. 307 | * 308 | * @param string $pattern The pattern. 309 | * 310 | * @return RevParseCommandBuilder 311 | */ 312 | public function remotes($pattern) 313 | { 314 | $this->arguments[] = '--remotes=' . $pattern; 315 | return $this; 316 | } 317 | 318 | /** 319 | * Add the glob option to the command line. 320 | * 321 | * @param string $pattern The pattern. 322 | * 323 | * @return RevParseCommandBuilder 324 | */ 325 | public function glob($pattern) 326 | { 327 | $this->arguments[] = '--glob=' . $pattern; 328 | return $this; 329 | } 330 | 331 | /** 332 | * Add the exclude option to the command line. 333 | * 334 | * @param string $pattern The pattern. 335 | * 336 | * @return RevParseCommandBuilder 337 | */ 338 | public function exclude($pattern) 339 | { 340 | $this->arguments[] = '--exclude=' . $pattern; 341 | return $this; 342 | } 343 | 344 | /** 345 | * Add the disambiguate option to the command line. 346 | * 347 | * @param string $prefix The prefix. 348 | * 349 | * @return RevParseCommandBuilder 350 | */ 351 | public function disambiguate($prefix) 352 | { 353 | $this->arguments[] = '--disambiguate=' . $prefix; 354 | return $this; 355 | } 356 | 357 | /** 358 | * Add the since option to the command line. 359 | * 360 | * @param \DateTime|string $date The date. 361 | * 362 | * @return RevParseCommandBuilder 363 | */ 364 | public function since($date) 365 | { 366 | if ($date instanceof \DateTime) { 367 | $date = $date->format('Y-m-d H:i:s'); 368 | } 369 | $this->arguments[] = '--since=' . $date; 370 | return $this; 371 | } 372 | 373 | /** 374 | * Add the after option to the command line. 375 | * 376 | * @param \DateTime|string $date The date. 377 | * 378 | * @return RevParseCommandBuilder 379 | */ 380 | public function after($date) 381 | { 382 | if ($date instanceof \DateTime) { 383 | $date = $date->format('Y-m-d H:i:s'); 384 | } 385 | $this->arguments[] = '--after=' . $date; 386 | return $this; 387 | } 388 | 389 | /** 390 | * Add the until option to the command line. 391 | * 392 | * @param \DateTime|string $date The date. 393 | * 394 | * @return RevParseCommandBuilder 395 | */ 396 | public function until($date) 397 | { 398 | if ($date instanceof \DateTime) { 399 | $date = $date->format('Y-m-d H:i:s'); 400 | } 401 | $this->arguments[] = '--until=' . $date; 402 | return $this; 403 | } 404 | 405 | /** 406 | * Add the before option to the command line. 407 | * 408 | * @param \DateTime|string $date The date. 409 | * 410 | * @return RevParseCommandBuilder 411 | */ 412 | public function before($date) 413 | { 414 | if ($date instanceof \DateTime) { 415 | $date = $date->format('Y-m-d H:i:s'); 416 | } 417 | $this->arguments[] = '--before=' . $date; 418 | return $this; 419 | } 420 | 421 | /** 422 | * Build the command and execute it. 423 | * 424 | * @param null|string $arg Optional additional argument. 425 | * 426 | * @param null|string $_ More optional arguments. 427 | * 428 | * @return string 429 | * 430 | * @SuppressWarnings(PHPMD.ShortVariableName) 431 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 432 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 433 | */ 434 | public function execute($arg = null, $_ = null) 435 | { 436 | foreach (\func_get_args() as $arg) { 437 | $this->arguments[] = $arg; 438 | } 439 | return $this->run(); 440 | } 441 | } 442 | -------------------------------------------------------------------------------- /src/Command/RmCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Rm command builder. 27 | */ 28 | class RmCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function initializeProcessBuilder() 36 | { 37 | $this->arguments[] = 'rm'; 38 | } 39 | 40 | /** 41 | * Add the force option to the command line. 42 | * 43 | * @return RmCommandBuilder 44 | */ 45 | public function force() 46 | { 47 | $this->arguments[] = '--force'; 48 | return $this; 49 | } 50 | 51 | /** 52 | * Add the dry-run option to the command line. 53 | * 54 | * @return RmCommandBuilder 55 | */ 56 | public function dryRun() 57 | { 58 | $this->arguments[] = '--dry-run'; 59 | return $this; 60 | } 61 | 62 | /** 63 | * Add the recursive option to the command line. 64 | * 65 | * @return RmCommandBuilder 66 | */ 67 | public function recursive() 68 | { 69 | $this->arguments[] = '-r'; 70 | return $this; 71 | } 72 | 73 | /** 74 | * Add the cached option to the command line. 75 | * 76 | * @return RmCommandBuilder 77 | */ 78 | public function cached() 79 | { 80 | $this->arguments[] = '--cached'; 81 | return $this; 82 | } 83 | 84 | /** 85 | * Add the ignore-unmatch option to the command line. 86 | * 87 | * @return RmCommandBuilder 88 | */ 89 | public function ignoreUnmatch() 90 | { 91 | $this->arguments[] = '--ignore-unmatch'; 92 | return $this; 93 | } 94 | 95 | /** 96 | * Add the quiet option to the command line. 97 | * 98 | * @return RmCommandBuilder 99 | */ 100 | public function quiet() 101 | { 102 | $this->arguments[] = '--quiet'; 103 | return $this; 104 | } 105 | 106 | /** 107 | * Build the command and execute it. 108 | * 109 | * @param null|string $pathspec Path spec. 110 | * 111 | * @param null|string $_ More optional path specs. 112 | * 113 | * @return string 114 | * 115 | * @SuppressWarnings(PHPMD.ShortVariableName) 116 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 117 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 118 | */ 119 | public function execute($pathspec = null, $_ = null) 120 | { 121 | $args = \func_get_args(); 122 | if (\count($args)) { 123 | $this->arguments[] = '--'; 124 | foreach ($args as $pathspec) { 125 | $this->arguments[] = $pathspec; 126 | } 127 | } 128 | return $this->run(); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/Command/ShortLogCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author David Molineus 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * ShortLog command builder. 27 | */ 28 | class ShortLogCommandBuilder implements CommandBuilderInterface 29 | { 30 | use CommandBuilderTrait; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function initializeProcessBuilder() 36 | { 37 | $this->arguments[] = 'shortlog'; 38 | } 39 | 40 | /** 41 | * Add the option to the command line. 42 | * 43 | * @return ShortLogCommandBuilder 44 | */ 45 | public function numbered() 46 | { 47 | $this->arguments[] = '--numbered'; 48 | 49 | return $this; 50 | } 51 | 52 | /** 53 | * Add the option to the command line. 54 | * 55 | * @return ShortLogCommandBuilder 56 | */ 57 | public function summary() 58 | { 59 | $this->arguments[] = '--summary'; 60 | 61 | return $this; 62 | } 63 | 64 | /** 65 | * Add the option to the command line. 66 | * 67 | * @return ShortLogCommandBuilder 68 | */ 69 | public function email() 70 | { 71 | $this->arguments[] = '--email'; 72 | 73 | return $this; 74 | } 75 | 76 | /** 77 | * Add the option to the command line. 78 | * 79 | * @param string $format The format. 80 | * 81 | * @return ShortLogCommandBuilder 82 | */ 83 | public function format($format) 84 | { 85 | $this->arguments[] = '--format=' . $format; 86 | 87 | return $this; 88 | } 89 | 90 | /** 91 | * Add the option to the command line. 92 | * 93 | * @param string $revisionRange The revision range. 94 | * 95 | * @return ShortLogCommandBuilder 96 | */ 97 | public function revisionRange($revisionRange) 98 | { 99 | $this->arguments[] = $revisionRange; 100 | 101 | return $this; 102 | } 103 | 104 | /** 105 | * Linewrap the output by wrapping each line at width. 106 | * 107 | * The first line of each entry is indented by indent1 spaces, and the second and subsequent lines are indented by 108 | * indent2 spaces. 109 | * 110 | * Width, indent1, and indent2 default to 76, 6 and 9 respectively. 111 | * 112 | * If width is 0 (zero) then indent the lines of the output without wrapping them. 113 | * 114 | * @param int $width The width or 0 to disable indenting. 115 | * 116 | * @param null|int $indent1 The amount of spaces the first line of each entry is indented by. 117 | * 118 | * @param null|int $indent2 The amount of spaces subsequent lines of each entry are indented by. 119 | * 120 | * @return ShortLogCommandBuilder 121 | * 122 | * @SuppressWarnings(PHPMD.ShortMethodName) 123 | */ 124 | public function w($width, $indent1 = null, $indent2 = null) 125 | { 126 | if ($indent1) { 127 | $width .= ',' . $indent1; 128 | 129 | if ($indent2) { 130 | $width .= ',' . $indent2; 131 | } 132 | } 133 | 134 | $this->arguments[] = '-w' . $width; 135 | 136 | return $this; 137 | } 138 | 139 | /** 140 | * Execute the command and return the result. 141 | * 142 | * @param null|string $pathSpec Optional path spec. 143 | * 144 | * @param null|string $_ Optional list of more path specs. 145 | * 146 | * @return string 147 | * 148 | * @SuppressWarnings(PHPMD.ShortVariableName) 149 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 150 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 151 | */ 152 | public function execute($pathSpec = null, $_ = null) 153 | { 154 | $args = \func_get_args(); 155 | if (\count($args)) { 156 | $this->arguments[] = '--'; 157 | foreach ($args as $pathSpec) { 158 | $this->arguments[] = $pathSpec; 159 | } 160 | } 161 | 162 | return $this->run(); 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/Command/ShowCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Show command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class ShowCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | protected function initializeProcessBuilder() 38 | { 39 | $this->arguments[] = 'show'; 40 | } 41 | 42 | /** 43 | * Add the option to the command line. 44 | * 45 | * @param null|string $format The format. 46 | * 47 | * @return ShowCommandBuilder 48 | */ 49 | public function pretty($format = null) 50 | { 51 | $this->arguments[] = '--pretty' . ($format ? '=' . $format : ''); 52 | return $this; 53 | } 54 | 55 | /** 56 | * Add the format option to the command line. 57 | * 58 | * @param null|string $format The format. 59 | * 60 | * @return ShowCommandBuilder 61 | */ 62 | public function format($format) 63 | { 64 | $this->arguments[] = '--format=' . $format; 65 | return $this; 66 | } 67 | 68 | /** 69 | * Add the abbrev-commit option to the command line. 70 | * 71 | * @return ShowCommandBuilder 72 | */ 73 | public function abbrevCommit() 74 | { 75 | $this->arguments[] = '--abbrev-commit'; 76 | return $this; 77 | } 78 | 79 | /** 80 | * Add the no-abbrev-commit option to the command line. 81 | * 82 | * @return ShowCommandBuilder 83 | */ 84 | public function noAbbrevCommit() 85 | { 86 | $this->arguments[] = '--no-abbrev-commit'; 87 | return $this; 88 | } 89 | 90 | /** 91 | * Add the oneline option to the command line. 92 | * 93 | * @return ShowCommandBuilder 94 | */ 95 | public function oneline() 96 | { 97 | $this->arguments[] = '--oneline'; 98 | return $this; 99 | } 100 | 101 | /** 102 | * Add the encoding option to the command line. 103 | * 104 | * @param string $encoding The encoding. 105 | * 106 | * @return ShowCommandBuilder 107 | */ 108 | public function encoding($encoding) 109 | { 110 | $this->arguments[] = '--encoding=' . $encoding; 111 | return $this; 112 | } 113 | 114 | /** 115 | * Add the notes option to the command line. 116 | * 117 | * @param null|string $ref The ref name. 118 | * 119 | * @return ShowCommandBuilder 120 | */ 121 | public function notes($ref = null) 122 | { 123 | $this->arguments[] = '--notes' . ($ref ? '=' . $ref : ''); 124 | return $this; 125 | } 126 | 127 | /** 128 | * Add the no-notes option to the command line. 129 | * 130 | * @return ShowCommandBuilder 131 | */ 132 | public function noNotes() 133 | { 134 | $this->arguments[] = '--no-notes'; 135 | return $this; 136 | } 137 | 138 | /** 139 | * Add the show-notes option to the command line. 140 | * 141 | * @param null|string $ref The ref name. 142 | * 143 | * @return ShowCommandBuilder 144 | */ 145 | public function showNotes($ref = null) 146 | { 147 | $this->arguments[] = '--show-notes' . ($ref ? '=' . $ref : ''); 148 | return $this; 149 | } 150 | 151 | /** 152 | * Add the standard-notes option to the command line. 153 | * 154 | * @return ShowCommandBuilder 155 | */ 156 | public function standardNotes() 157 | { 158 | $this->arguments[] = '--standard-notes'; 159 | return $this; 160 | } 161 | 162 | /** 163 | * Add the no-standard-notes option to the command line. 164 | * 165 | * @return ShowCommandBuilder 166 | */ 167 | public function noStandardNotes() 168 | { 169 | $this->arguments[] = '--no-standard-notes'; 170 | return $this; 171 | } 172 | 173 | /** 174 | * Add the show-signature option to the command line. 175 | * 176 | * @return ShowCommandBuilder 177 | */ 178 | public function showSignature() 179 | { 180 | $this->arguments[] = '--show-signature'; 181 | return $this; 182 | } 183 | 184 | /** 185 | * Add the no-patch option to the command line. 186 | * 187 | * @return ShowCommandBuilder 188 | */ 189 | public function noPatch() 190 | { 191 | $this->arguments[] = '--no-patch'; 192 | return $this; 193 | } 194 | 195 | /** 196 | * Build the command and execute it. 197 | * 198 | * @param string $object Name of the object to show. 199 | * 200 | * @return string 201 | * 202 | * @SuppressWarnings(PHPMD.ShortVariableName) 203 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 204 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 205 | */ 206 | public function execute($object) 207 | { 208 | $this->arguments[] = $object; 209 | return $this->run(); 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/Command/StashCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Ahmad Marzouq 15 | * @author Sven Baumann 16 | * @copyright 2014-2018 Tristan Lins 17 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 18 | * @link https://github.com/bit3/git-php 19 | * @filesource 20 | */ 21 | 22 | namespace Bit3\GitPhp\Command; 23 | 24 | /** 25 | * Stash command builder. 26 | * 27 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 28 | */ 29 | class StashCommandBuilder implements CommandBuilderInterface 30 | { 31 | use CommandBuilderTrait; 32 | 33 | /** 34 | * {@inheritDoc} 35 | */ 36 | protected function initializeProcessBuilder() 37 | { 38 | $this->arguments[] = 'stash'; 39 | } 40 | 41 | /** 42 | * Adds a message to StashCommandBuilder 43 | * 44 | * @param string $message The stash message. 45 | * 46 | * @return StashCommandBuilder 47 | */ 48 | public function message($message) 49 | { 50 | $this->arguments[] = $message; 51 | return $this; 52 | } 53 | 54 | /** 55 | * List stashes and return the results 56 | * 57 | * @param string $options Takes options applicable to the git log command. 58 | * 59 | * @return mixed 60 | */ 61 | public function listStash($options = null) 62 | { 63 | $this->arguments[] = 'list'; 64 | if ($options) { 65 | $this->arguments[] = $options; 66 | } 67 | return $this->run(); 68 | } 69 | 70 | /** 71 | * Show the changes recorded in the stash as a diff 72 | * between the stashed state and its original parent. 73 | * When no $stash is given, shows the latest one. 74 | * 75 | * @param int|null $stash Takes the stash number. 76 | * 77 | * @return mixed 78 | */ 79 | public function show($stash = null) 80 | { 81 | $this->arguments[] = 'show'; 82 | if ($stash) { 83 | $this->arguments[] = 'stash@{' . $stash . '}'; 84 | } 85 | return $this->run(); 86 | } 87 | 88 | /** 89 | * Remove a single stashed state from the stash list. 90 | * When no $stash is given, it removes the latest one. 91 | * 92 | * @param int|null $stash Takes the stash number. 93 | * 94 | * @return mixed 95 | */ 96 | public function drop($stash = null) 97 | { 98 | $this->arguments[] = 'show'; 99 | if ($stash) { 100 | $this->arguments[] = 'stash@{' . $stash . '}'; 101 | } 102 | return $this->run(); 103 | } 104 | 105 | /** 106 | * Remove a single stashed state from the stash list 107 | * and apply it on top of the current working tree state. 108 | * 109 | * @param int|null $stash Takes the stash number. 110 | * 111 | * @return mixed 112 | */ 113 | public function pop($stash = null) 114 | { 115 | $this->arguments[] = 'pop'; 116 | if ($stash) { 117 | $this->arguments[] = 'stash@{' . $stash . '}'; 118 | } 119 | return $this->run(); 120 | } 121 | 122 | /** 123 | * Like pop, but do not remove the state from the stash list. 124 | * Unlike pop, $stash may be any commit that looks like a commit 125 | * created by stash save or stash create. 126 | * 127 | * @param int|null $stash Takes the stash number. 128 | * 129 | * @return mixed 130 | */ 131 | public function apply($stash = null) 132 | { 133 | $this->arguments[] = 'pop'; 134 | if ($stash) { 135 | $this->arguments[] = 'stash@{' . $stash . '}'; 136 | } 137 | return $this->run(); 138 | } 139 | 140 | 141 | /** 142 | * Creates and checks out a new branch named $branchname starting from the commit at 143 | * which the $stash was originally created, 144 | * applies the changes recorded in $stash to the new working tree and index. 145 | * 146 | * @param string $branchname The branch name. 147 | * 148 | * @param int|null $stash Takes the stash number. 149 | * 150 | * @return mixed 151 | */ 152 | public function branch($branchname, $stash = null) 153 | { 154 | $this->arguments[] = 'branch'; 155 | $this->arguments[] = $branchname; 156 | if ($stash) { 157 | $this->arguments[] = 'stash@{' . $stash . '}'; 158 | } 159 | return $this->run(); 160 | } 161 | 162 | 163 | /** 164 | * Remove all the stashed states. 165 | * 166 | * @return mixed 167 | */ 168 | public function clear() 169 | { 170 | $this->arguments[] = 'clear'; 171 | return $this->run(); 172 | } 173 | 174 | 175 | /** 176 | * Save your local modifications to a new stash, 177 | * and run git reset --hard to revert them. 178 | * The part is optional and gives 179 | * the description along with the stashed state. 180 | * 181 | * @param string $message The stash message. 182 | * 183 | * @return mixed 184 | */ 185 | public function save($message = null) 186 | { 187 | $this->arguments[] = 'branch'; 188 | if ($message) { 189 | $this->arguments[] = $message; 190 | } 191 | return $this->run(); 192 | } 193 | 194 | /** 195 | * Create a stash (which is a regular commit object) 196 | * and return its object name, 197 | * without storing it anywhere in the ref namespace. 198 | * 199 | * @param string $message The stash message. 200 | * 201 | * @return mixed 202 | */ 203 | public function create($message = null) 204 | { 205 | $this->arguments[] = 'create'; 206 | if ($message) { 207 | $this->arguments[] = $message; 208 | } 209 | return $this->run(); 210 | } 211 | 212 | /** 213 | * Store a given stash created via git stash create 214 | * (which is a dangling merge commit) in the stash ref, 215 | * updating the stash reflog. 216 | * 217 | * @param string $message The stash message. 218 | * 219 | * @param null|string $commit The commit hash. 220 | * 221 | * @return mixed 222 | */ 223 | public function store($message = null, $commit = null) 224 | { 225 | $this->arguments[] = 'store'; 226 | if ($message) { 227 | $this->arguments[] = '--message '.$message; 228 | } 229 | if ($commit) { 230 | $this->arguments[] = $commit; 231 | } 232 | return $this->run(); 233 | } 234 | /** 235 | * Execute the command and return the result. 236 | * to use with message function . 237 | * 238 | * @return mixed 239 | */ 240 | public function execute() 241 | { 242 | return $this->run(); 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /src/Command/StatusCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Status command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class StatusCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const UNTRACKED_FILES_NO = 'no'; 35 | 36 | const UNTRACKED_FILES_NORMAL = 'normal'; 37 | 38 | const UNTRACKED_FILES_ALL = 'all'; 39 | 40 | const IGNORE_SUBMODULES_NONE = 'none'; 41 | 42 | const IGNORE_SUBMODULES_UNTRACKED = 'untracked'; 43 | 44 | const IGNORE_SUBMODULES_DIRTY = 'dirty'; 45 | 46 | const IGNORE_SUBMODULES_ALL = 'all'; 47 | 48 | /** 49 | * {@inheritDoc} 50 | */ 51 | protected function initializeProcessBuilder() 52 | { 53 | $this->arguments[] = 'status'; 54 | } 55 | 56 | /** 57 | * Add the short option to the command line. 58 | * 59 | * @return StatusCommandBuilder 60 | */ 61 | public function short() 62 | { 63 | $this->arguments[] = '--short'; 64 | return $this; 65 | } 66 | 67 | /** 68 | * Add the branch option to the command line. 69 | * 70 | * @return StatusCommandBuilder 71 | */ 72 | public function branch() 73 | { 74 | $this->arguments[] = '--branch'; 75 | return $this; 76 | } 77 | 78 | /** 79 | * Add the porcelain option to the command line. 80 | * 81 | * @return StatusCommandBuilder 82 | */ 83 | public function porcelain() 84 | { 85 | $this->arguments[] = '--porcelain'; 86 | return $this; 87 | } 88 | 89 | /** 90 | * Add the long option to the command line. 91 | * 92 | * @return StatusCommandBuilder 93 | */ 94 | public function long() 95 | { 96 | $this->arguments[] = '--long'; 97 | return $this; 98 | } 99 | 100 | /** 101 | * Add the untracked-files option to the command line. 102 | * 103 | * @param null|string $mode The mode. 104 | * 105 | * @return StatusCommandBuilder 106 | */ 107 | public function untrackedFiles($mode = null) 108 | { 109 | $this->arguments[] = '--untracked-files' . ($mode ? '=' . $mode : ''); 110 | return $this; 111 | } 112 | 113 | /** 114 | * Add the ignore-submodules option to the command line. 115 | * 116 | * @param null|string $when The value. 117 | * 118 | * @return StatusCommandBuilder 119 | */ 120 | public function ignoreSubmodules($when = null) 121 | { 122 | $this->arguments[] = '--ignore-submodules' . ($when ? '=' . $when : ''); 123 | return $this; 124 | } 125 | 126 | /** 127 | * Add the ignored option to the command line. 128 | * 129 | * @return StatusCommandBuilder 130 | */ 131 | public function ignored() 132 | { 133 | $this->arguments[] = '--ignored'; 134 | return $this; 135 | } 136 | 137 | /** 138 | * Add the z option to the command line. 139 | * 140 | * @return StatusCommandBuilder 141 | * 142 | * @SuppressWarnings(PHPMD.ShortMethodName) 143 | */ 144 | public function z() 145 | { 146 | $this->arguments[] = '-z'; 147 | return $this; 148 | } 149 | 150 | /** 151 | * Add the column option to the command line. 152 | * 153 | * @param null|string $options The column options. 154 | * 155 | * @return StatusCommandBuilder 156 | */ 157 | public function column($options = null) 158 | { 159 | $this->arguments[] = '--column' . ($options ? '=' . $options : ''); 160 | return $this; 161 | } 162 | 163 | /** 164 | * Add the option to the command line. 165 | * 166 | * @return StatusCommandBuilder 167 | */ 168 | public function noColumn() 169 | { 170 | $this->arguments[] = '--no-column'; 171 | return $this; 172 | } 173 | 174 | /** 175 | * Return the parsed index and work tree status. 176 | * 177 | * The result will be an associative array of all files and an status array in the following format: 178 | * 179 | * array( 180 | * '<pathspec>' => array( 181 | * 'index' => [false | "M" | "A" | "D" | "R" | "C" | "U" | "?" | "!"], 182 | * 'worktree' => [false | "M" | "A" | "D" | "R" | "C" | "U" | "?" | "!"], 183 | * ) 184 | * ) 185 | * 186 | * 187 | * @param string $pathspec A path spec. 188 | * 189 | * @param string $_ Optional list of additional path specs. 190 | * 191 | * @return array 192 | * 193 | * @SuppressWarnings(PHPMD.ShortVariableName) 194 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 195 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 196 | */ 197 | public function getStatus($pathspec = null, $_ = null) 198 | { 199 | $this->porcelain(); 200 | 201 | $status = \call_user_func_array([$this, 'execute'], \func_get_args()); 202 | $status = \explode("\n", $status); 203 | 204 | $files = []; 205 | 206 | foreach ($status as $line) { 207 | if (\trim($line)) { 208 | $index = \trim(\substr($line, 0, 1)); 209 | $worktree = \trim(\substr($line, 1, 1)); 210 | 211 | if ($index && $worktree) { 212 | $file = \trim(\substr($line, 2)); 213 | $files[$file] = [ 214 | 'index' => $index ?: false, 215 | 'worktree' => $worktree ?: false, 216 | ]; 217 | } 218 | } 219 | } 220 | 221 | return $files; 222 | } 223 | 224 | /** 225 | * Return the parsed index status. 226 | * 227 | * The result will be an associative array of all files and their modification status in the following format: 228 | * 229 | * array( 230 | * '<pathspec>' => [false | "M" | "A" | "D" | "R" | "C" | "U" | "?" | "!"], 231 | * ) 232 | * 233 | * 234 | * @param string $pathspec A path spec. 235 | * 236 | * @param string $_ Optional list of additional path specs. 237 | * 238 | * @return array 239 | * 240 | * @SuppressWarnings(PHPMD.ShortVariableName) 241 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 242 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 243 | */ 244 | public function getIndexStatus($pathspec = null, $_ = null) 245 | { 246 | $this->porcelain(); 247 | 248 | $status = \call_user_func_array([$this, 'execute'], \func_get_args()); 249 | $status = \explode("\n", $status); 250 | 251 | $files = []; 252 | 253 | foreach ($status as $line) { 254 | if ($line = \trim($line)) { 255 | $index = \substr($line, 0, 1); 256 | 257 | if ($index) { 258 | $file = \trim(\substr($line, 2)); 259 | $files[$file] = $index; 260 | } 261 | } 262 | } 263 | 264 | return $files; 265 | } 266 | 267 | /** 268 | * Return the parsed work tree status. 269 | * 270 | * The result will be an associative array of all files and their modification status in the following format: 271 | * 272 | * array( 273 | * '<pathspec>' => [false | "M" | "A" | "D" | "R" | "C" | "U" | "?" | "!"], 274 | * ) 275 | * 276 | * 277 | * @param string $pathspec A path spec. 278 | * 279 | * @param string $_ Optional list of additional path specs. 280 | * 281 | * @return array 282 | * 283 | * @SuppressWarnings(PHPMD.ShortVariableName) 284 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 285 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 286 | */ 287 | public function getWorkTreeStatus($pathspec = null, $_ = null) 288 | { 289 | $this->porcelain(); 290 | 291 | $status = \call_user_func_array([$this, 'execute'], \func_get_args()); 292 | $status = \explode("\n", $status); 293 | 294 | $files = []; 295 | 296 | foreach ($status as $line) { 297 | if ($line = \trim($line)) { 298 | $worktree = \trim(\substr($line, 1, 1)); 299 | 300 | if ($worktree) { 301 | $file = \trim(\substr($line, 2)); 302 | $files[$file] = $worktree; 303 | } 304 | } 305 | } 306 | 307 | return $files; 308 | } 309 | 310 | /** 311 | * Build the command and execute it. 312 | * 313 | * @param null|string $pathspec A path spec. 314 | * 315 | * @param null|string $_ Optional list of additional path specs. 316 | * 317 | * @return string 318 | * 319 | * @SuppressWarnings(PHPMD.ShortVariableName) 320 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 321 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 322 | */ 323 | public function execute($pathspec = null, $_ = null) 324 | { 325 | $args = \func_get_args(); 326 | if (\count($args)) { 327 | $this->arguments[] = '--'; 328 | foreach ($args as $pathspec) { 329 | $this->arguments[] = $pathspec; 330 | } 331 | } 332 | return $this->run(); 333 | } 334 | } 335 | -------------------------------------------------------------------------------- /src/Command/TagCommandBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp\Command; 24 | 25 | /** 26 | * Tag command builder. 27 | * 28 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 29 | */ 30 | class TagCommandBuilder implements CommandBuilderInterface 31 | { 32 | use CommandBuilderTrait; 33 | 34 | const CLEANUP_VERBATIM = 'verbatim'; 35 | 36 | const CLEANUP_WHITESPACE = 'whitespace'; 37 | 38 | const CLEANUP_STRIP = 'strip'; 39 | 40 | /** 41 | * Flag if signing shall be done. 42 | * 43 | * @var bool 44 | */ 45 | protected $signIsset = false; 46 | 47 | /** 48 | * Flag determining if the local user has been set. 49 | * 50 | * @var bool 51 | */ 52 | protected $localUserIsset = false; 53 | 54 | /** 55 | * {@inheritDoc} 56 | */ 57 | protected function initializeProcessBuilder() 58 | { 59 | $this->arguments[] = 'tag'; 60 | } 61 | 62 | /** 63 | * Add the annotate option to the command line. 64 | * 65 | * @return TagCommandBuilder 66 | */ 67 | public function annotate() 68 | { 69 | $this->arguments[] = '--annotate'; 70 | return $this; 71 | } 72 | 73 | /** 74 | * Add the sign option to the command line. 75 | * 76 | * @return TagCommandBuilder 77 | */ 78 | public function sign() 79 | { 80 | $this->signIsset = true; 81 | $this->arguments[] = '--sign'; 82 | return $this; 83 | } 84 | 85 | /** 86 | * Add the local-user option to the command line. 87 | * 88 | * @param string $keyId The id of the local user key. 89 | * 90 | * @return TagCommandBuilder 91 | */ 92 | public function localUser($keyId) 93 | { 94 | $this->localUserIsset = true; 95 | $this->arguments[] = '--local-user=' . $keyId; 96 | return $this; 97 | } 98 | 99 | /** 100 | * Add the force option to the command line. 101 | * 102 | * @return TagCommandBuilder 103 | */ 104 | public function force() 105 | { 106 | $this->arguments[] = '--force'; 107 | return $this; 108 | } 109 | 110 | /** 111 | * Add the delete option to the command line. 112 | * 113 | * @return TagCommandBuilder 114 | */ 115 | public function delete() 116 | { 117 | $this->arguments[] = '--delete'; 118 | return $this; 119 | } 120 | 121 | /** 122 | * Add the verify option to the command line. 123 | * 124 | * @return TagCommandBuilder 125 | */ 126 | public function verify() 127 | { 128 | $this->arguments[] = '--verify'; 129 | return $this; 130 | } 131 | 132 | /** 133 | * Add the n option to the command line. 134 | * 135 | * @param int $num The number. 136 | * 137 | * @return TagCommandBuilder 138 | * 139 | * @SuppressWarnings(PHPMD.ShortMethodName) 140 | */ 141 | public function n($num) 142 | { 143 | $this->arguments[] = '-n' . $num; 144 | return $this; 145 | } 146 | 147 | /** 148 | * Add the l option to the command line. 149 | * 150 | * @param string $pattern The pattern. 151 | * 152 | * @return TagCommandBuilder 153 | * 154 | * @SuppressWarnings(PHPMD.ShortMethodName) 155 | */ 156 | public function l($pattern) 157 | { 158 | $this->arguments[] = '--list'; 159 | $this->arguments[] = $pattern; 160 | return $this; 161 | } 162 | 163 | /** 164 | * Add the column option to the command line. 165 | * 166 | * @param null|string $options The column options. 167 | * 168 | * @return TagCommandBuilder 169 | */ 170 | public function column($options = null) 171 | { 172 | $this->arguments[] = '--column' . ($options ? '=' . $options : ''); 173 | return $this; 174 | } 175 | 176 | /** 177 | * Add the no-column option to the command line. 178 | * 179 | * @return TagCommandBuilder 180 | */ 181 | public function noColumn() 182 | { 183 | $this->arguments[] = '--no-column'; 184 | return $this; 185 | } 186 | 187 | /** 188 | * Add the contains option to the command line. 189 | * 190 | * @param string $commit The commit hash. 191 | * 192 | * @return TagCommandBuilder 193 | */ 194 | public function contains($commit) 195 | { 196 | $this->arguments[] = '--contains'; 197 | $this->arguments[] = $commit; 198 | return $this; 199 | } 200 | 201 | /** 202 | * Add the points-at option to the command line. 203 | * 204 | * @param string $object The object the tag points at. 205 | * 206 | * @return TagCommandBuilder 207 | */ 208 | public function pointsAt($object) 209 | { 210 | $this->arguments[] = '--points-at'; 211 | $this->arguments[] = $object; 212 | return $this; 213 | } 214 | 215 | /** 216 | * Add the message option to the command line. 217 | * 218 | * @param string $message The message. 219 | * 220 | * @return TagCommandBuilder 221 | */ 222 | public function message($message) 223 | { 224 | $this->arguments[] = '--message=' . $message; 225 | return $this; 226 | } 227 | 228 | /** 229 | * Add the file option to the command line. 230 | * 231 | * @param string $file The file. 232 | * 233 | * @return TagCommandBuilder 234 | */ 235 | public function file($file) 236 | { 237 | $this->arguments[] = '--file=' . $file; 238 | return $this; 239 | } 240 | 241 | /** 242 | * Add the cleanup option to the command line. 243 | * 244 | * @param string $mode The cleanup mode. 245 | * 246 | * @return TagCommandBuilder 247 | */ 248 | public function cleanup($mode) 249 | { 250 | $this->arguments[] = '--cleanup=' . $mode; 251 | return $this; 252 | } 253 | 254 | /** 255 | * Build the command and execute it. 256 | * 257 | * @param string $tagName Name of the tag. 258 | * 259 | * @param null|string $commit Commit hash to tag. 260 | * 261 | * @return string 262 | * 263 | * @SuppressWarnings(PHPMD.ShortVariableName) 264 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 265 | * @SuppressWarnings(PHPMD.CamelCaseParameterName) 266 | */ 267 | public function execute($tagName = null, $commit = null) 268 | { 269 | if (!$this->signIsset && $this->repository->getConfig()->isSignTagsEnabled()) { 270 | $this->sign()->localUser($this->repository->getConfig()->getSignCommitUser()); 271 | } else { 272 | if ($this->signIsset && !$this->localUserIsset && $this->repository->getConfig()->isSignTagsEnabled()) { 273 | $this->localUser($this->repository->getConfig()->getSignCommitUser()); 274 | } 275 | } 276 | 277 | if ($tagName) { 278 | $this->arguments[] = $tagName; 279 | } 280 | 281 | if ($commit) { 282 | $this->arguments[] = $commit; 283 | } 284 | 285 | return $this->run(); 286 | } 287 | 288 | /** 289 | * Retrieve the tag names. 290 | * 291 | * @return string[] 292 | */ 293 | public function getNames() 294 | { 295 | $tags = $this->execute(); 296 | $tags = \explode("\n", $tags); 297 | $tags = \array_map('trim', $tags); 298 | $tags = \array_filter($tags); 299 | 300 | return $tags; 301 | } 302 | } 303 | -------------------------------------------------------------------------------- /src/GitConfig.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp; 24 | 25 | use Psr\Log\LoggerInterface; 26 | use Psr\Log\NullLogger; 27 | 28 | /** 29 | * Shareable configuration for git repositories. 30 | */ 31 | class GitConfig 32 | { 33 | /** 34 | * The path to the git executable. 35 | * 36 | * @var string 37 | */ 38 | protected $gitExecutablePath = 'git'; 39 | 40 | /** 41 | * ID of the GPG certificate to sign commits. 42 | * 43 | * @var string|null 44 | */ 45 | protected $signCommitUser; 46 | 47 | /** 48 | * ID of the GPG certificate to sign tags. 49 | * 50 | * @var string|null 51 | */ 52 | protected $signTagUser; 53 | 54 | /** 55 | * Logger facility. 56 | * 57 | * @var LoggerInterface 58 | */ 59 | protected $logger; 60 | 61 | /** 62 | * Create new git config. 63 | */ 64 | public function __construct() 65 | { 66 | $this->logger = new NullLogger(); 67 | } 68 | 69 | /** 70 | * Set the git executable path. 71 | * 72 | * @param string $gitExecutablePath Path to the git executable. 73 | * 74 | * @return GitConfig 75 | */ 76 | public function setGitExecutablePath($gitExecutablePath) 77 | { 78 | $this->gitExecutablePath = (string) $gitExecutablePath; 79 | return $this; 80 | } 81 | 82 | /** 83 | * Return the git executable path. 84 | * 85 | * @return string 86 | */ 87 | public function getGitExecutablePath() 88 | { 89 | return $this->gitExecutablePath; 90 | } 91 | 92 | /** 93 | * Enable signing of commits. 94 | * 95 | * @param string $signUser The id of the GPG certificate. 96 | * 97 | * @return GitConfig 98 | */ 99 | public function enableSignCommits($signUser) 100 | { 101 | $this->signCommitUser = (string) $signUser; 102 | return $this; 103 | } 104 | 105 | /** 106 | * Disable signing of commits. 107 | * 108 | * @return $this 109 | */ 110 | public function disableSignCommits() 111 | { 112 | $this->signCommitUser = null; 113 | return $this; 114 | } 115 | 116 | /** 117 | * Determine if signing commits is enabled. 118 | * 119 | * @return boolean 120 | */ 121 | public function isSignCommitsEnabled() 122 | { 123 | return (bool) $this->signCommitUser; 124 | } 125 | 126 | /** 127 | * Get the id of the GPG certificate to sign commits with. 128 | * 129 | * @return string|null 130 | */ 131 | public function getSignCommitUser() 132 | { 133 | return $this->signCommitUser; 134 | } 135 | 136 | /** 137 | * Enable signing of tags. 138 | * 139 | * @param string $signUser The id of the GPG certificate. 140 | * 141 | * @return GitConfig 142 | */ 143 | public function enableSignTags($signUser) 144 | { 145 | $this->signTagUser = (string) $signUser; 146 | return $this; 147 | } 148 | 149 | /** 150 | * Disable signing of tags. 151 | * 152 | * @return GitConfig 153 | */ 154 | public function disableSignTags() 155 | { 156 | $this->signTagUser = null; 157 | return $this; 158 | } 159 | 160 | /** 161 | * Determine if signing tags is enabled. 162 | * 163 | * @return boolean 164 | */ 165 | public function isSignTagsEnabled() 166 | { 167 | return (bool) $this->signTagUser; 168 | } 169 | 170 | /** 171 | * Get the id of the GPG certificate to sign tags with. 172 | * 173 | * @return string|null 174 | */ 175 | public function getSignTagUser() 176 | { 177 | return $this->signTagUser; 178 | } 179 | 180 | /** 181 | * Set the logger facility. 182 | * 183 | * @param LoggerInterface $logger The logger to use. 184 | * 185 | * @return GitConfig 186 | */ 187 | public function setLogger(LoggerInterface $logger) 188 | { 189 | $this->logger = $logger; 190 | return $this; 191 | } 192 | 193 | /** 194 | * Return the logger facility. 195 | * 196 | * @return LoggerInterface 197 | */ 198 | public function getLogger() 199 | { 200 | return $this->logger; 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/GitException.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Sven Baumann 17 | * @copyright 2014-2018 Tristan Lins 18 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 19 | * @link https://github.com/bit3/git-php 20 | * @filesource 21 | */ 22 | 23 | namespace Bit3\GitPhp; 24 | 25 | use Symfony\Component\Process\Process; 26 | 27 | /** 28 | * Exception thrown when execution of git failed. 29 | */ 30 | class GitException extends \RuntimeException 31 | { 32 | /** 33 | * The working directory path. 34 | * 35 | * @var string 36 | */ 37 | protected $workingDirectory; 38 | 39 | /** 40 | * The executed command line. 41 | * 42 | * @var string 43 | */ 44 | protected $commandLine; 45 | 46 | /** 47 | * The git commands standard output. 48 | * 49 | * @var string 50 | */ 51 | protected $commandOutput; 52 | 53 | /** 54 | * The git commands error output. 55 | * 56 | * @var string 57 | */ 58 | protected $errorOutput; 59 | 60 | /** 61 | * Create a new git exception. 62 | * 63 | * @param string $message The error message. 64 | * 65 | * @param string $workingDirectory The working directory. 66 | * 67 | * @param string $commandLine The used command line. 68 | * 69 | * @param string $commandOutput The command output. 70 | * 71 | * @param string $errorOutput The command error output. 72 | */ 73 | public function __construct($message, $workingDirectory, $commandLine, $commandOutput, $errorOutput) 74 | { 75 | parent::__construct($message, 0, null); 76 | $this->workingDirectory = (string) $workingDirectory; 77 | $this->commandLine = (string) $commandLine; 78 | $this->commandOutput = (string) $commandOutput; 79 | $this->errorOutput = (string) $errorOutput; 80 | } 81 | 82 | /** 83 | * Return the working directory git was executed in. 84 | * 85 | * @return string 86 | */ 87 | public function getWorkingDirectory() 88 | { 89 | return $this->workingDirectory; 90 | } 91 | 92 | /** 93 | * Return the command line to execute git. 94 | * 95 | * @return string 96 | */ 97 | public function getCommandLine() 98 | { 99 | return $this->commandLine; 100 | } 101 | 102 | /** 103 | * Return the git commands standard output. 104 | * 105 | * @return string 106 | */ 107 | public function getCommandOutput() 108 | { 109 | return $this->commandOutput; 110 | } 111 | 112 | /** 113 | * Return the git commands error output. 114 | * 115 | * @return string 116 | */ 117 | public function getErrorOutput() 118 | { 119 | return $this->errorOutput; 120 | } 121 | 122 | /** 123 | * Create new exception from process. 124 | * 125 | * @param string $message The message to use. 126 | * 127 | * @param Process $process The process to create the message from. 128 | * 129 | * @return static 130 | */ 131 | public static function createFromProcess($message, Process $process) 132 | { 133 | return new static( 134 | \sprintf('%s [%s]', $message, $process->getCommandLine()) . 135 | PHP_EOL . \sprintf('work dir: %s', $process->getWorkingDirectory()) . 136 | PHP_EOL . $process->getErrorOutput(), 137 | $process->getWorkingDirectory(), 138 | $process->getCommandLine(), 139 | $process->getOutput(), 140 | $process->getErrorOutput() 141 | ); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/GitRepository.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author David Molineus 17 | * @author Aaron Rubin 18 | * @author Matthew Gamble 19 | * @author Ahmad Marzouq 20 | * @author Sven Baumann 21 | * @copyright 2014-2018 Tristan Lins 22 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 23 | * @link https://github.com/bit3/git-php 24 | * @filesource 25 | */ 26 | 27 | namespace Bit3\GitPhp; 28 | 29 | use Bit3\GitPhp\Command\AddCommandBuilder; 30 | use Bit3\GitPhp\Command\BranchCommandBuilder; 31 | use Bit3\GitPhp\Command\CheckoutCommandBuilder; 32 | use Bit3\GitPhp\Command\ConfigCommandBuilder; 33 | use Bit3\GitPhp\Command\MergeCommandBuilder; 34 | use Bit3\GitPhp\Command\CloneCommandBuilder; 35 | use Bit3\GitPhp\Command\CommitCommandBuilder; 36 | use Bit3\GitPhp\Command\DescribeCommandBuilder; 37 | use Bit3\GitPhp\Command\FetchCommandBuilder; 38 | use Bit3\GitPhp\Command\InitCommandBuilder; 39 | use Bit3\GitPhp\Command\LogCommandBuilder; 40 | use Bit3\GitPhp\Command\LsRemoteCommandBuilder; 41 | use Bit3\GitPhp\Command\PushCommandBuilder; 42 | use Bit3\GitPhp\Command\RemoteCommandBuilder; 43 | use Bit3\GitPhp\Command\ResetCommandBuilder; 44 | use Bit3\GitPhp\Command\RevParseCommandBuilder; 45 | use Bit3\GitPhp\Command\RmCommandBuilder; 46 | use Bit3\GitPhp\Command\ShortLogCommandBuilder; 47 | use Bit3\GitPhp\Command\ShowCommandBuilder; 48 | use Bit3\GitPhp\Command\StatusCommandBuilder; 49 | use Bit3\GitPhp\Command\TagCommandBuilder; 50 | use Bit3\GitPhp\Command\PullCommandBuilder; 51 | use Bit3\GitPhp\Command\StashCommandBuilder; 52 | 53 | /** 54 | * GIT repository adapter. 55 | * 56 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) 57 | */ 58 | class GitRepository 59 | { 60 | /** 61 | * The path to the git repository. 62 | * 63 | * @var string 64 | */ 65 | public $repositoryPath; 66 | 67 | /** 68 | * The shared git configuration. 69 | * 70 | * @var GitConfig 71 | */ 72 | public $config; 73 | 74 | /** 75 | * Create a new git repository. 76 | * 77 | * @param string $repositoryPath The path to the git repository. 78 | * 79 | * @param GitConfig $config The configuration to use. 80 | */ 81 | public function __construct($repositoryPath, GitConfig $config = null) 82 | { 83 | $this->repositoryPath = (string) $repositoryPath; 84 | $this->config = $config ?: new GitConfig(); 85 | } 86 | 87 | /** 88 | * Return the path to the git repository. 89 | * 90 | * @return string 91 | */ 92 | public function getRepositoryPath() 93 | { 94 | return $this->repositoryPath; 95 | } 96 | 97 | /** 98 | * Return the shared git config. 99 | * 100 | * @return GitConfig 101 | */ 102 | public function getConfig() 103 | { 104 | return $this->config; 105 | } 106 | 107 | /** 108 | * Determine if git is already initialized in the repository path. 109 | * 110 | * @return bool 111 | */ 112 | public function isInitialized() 113 | { 114 | return \is_dir($this->repositoryPath . DIRECTORY_SEPARATOR . '.git'); 115 | } 116 | 117 | /** 118 | * Create an init command. 119 | * 120 | * @return InitCommandBuilder 121 | */ 122 | public function init() 123 | { 124 | return new InitCommandBuilder($this); 125 | } 126 | 127 | /** 128 | * Create a clone command. 129 | * 130 | * @return CloneCommandBuilder 131 | */ 132 | public function cloneRepository() 133 | { 134 | return new CloneCommandBuilder($this); 135 | } 136 | 137 | /** 138 | * Create a config command. 139 | * 140 | * @return ConfigCommandBuilder 141 | */ 142 | public function config() 143 | { 144 | return new ConfigCommandBuilder($this); 145 | } 146 | 147 | /** 148 | * Create a remote command. 149 | * 150 | * @return RemoteCommandBuilder 151 | */ 152 | public function remote() 153 | { 154 | return new RemoteCommandBuilder($this); 155 | } 156 | 157 | /** 158 | * Create a branch command. 159 | * 160 | * @return BranchCommandBuilder 161 | */ 162 | public function branch() 163 | { 164 | return new BranchCommandBuilder($this); 165 | } 166 | 167 | /** 168 | * Create a rev-parse command. 169 | * 170 | * @return RevParseCommandBuilder 171 | */ 172 | public function revParse() 173 | { 174 | return new RevParseCommandBuilder($this); 175 | } 176 | 177 | /** 178 | * Create describe command. 179 | * 180 | * @return DescribeCommandBuilder 181 | */ 182 | public function describe() 183 | { 184 | return new DescribeCommandBuilder($this); 185 | } 186 | 187 | /** 188 | * Create reset command. 189 | * 190 | * @return ResetCommandBuilder 191 | */ 192 | public function reset() 193 | { 194 | return new ResetCommandBuilder($this); 195 | } 196 | 197 | /** 198 | * Create checkout command. 199 | * 200 | * @return CheckoutCommandBuilder 201 | */ 202 | public function checkout() 203 | { 204 | return new CheckoutCommandBuilder($this); 205 | } 206 | 207 | /** 208 | * Create push command. 209 | * 210 | * @return PushCommandBuilder 211 | */ 212 | public function push() 213 | { 214 | return new PushCommandBuilder($this); 215 | } 216 | 217 | /** 218 | * Create fetch command. 219 | * 220 | * @return FetchCommandBuilder 221 | */ 222 | public function fetch() 223 | { 224 | return new FetchCommandBuilder($this); 225 | } 226 | 227 | /** 228 | * Create status command. 229 | * 230 | * @return StatusCommandBuilder 231 | */ 232 | public function status() 233 | { 234 | return new StatusCommandBuilder($this); 235 | } 236 | 237 | /** 238 | * Create add command. 239 | * 240 | * @return AddCommandBuilder 241 | */ 242 | public function add() 243 | { 244 | return new AddCommandBuilder($this); 245 | } 246 | 247 | /** 248 | * Create rm command. 249 | * 250 | * @return RmCommandBuilder 251 | * 252 | * @SuppressWarnings(PHPMD.ShortMethodName) 253 | */ 254 | public function rm() 255 | { 256 | return new RmCommandBuilder($this); 257 | } 258 | 259 | /** 260 | * Create commit command. 261 | * 262 | * @return CommitCommandBuilder 263 | */ 264 | public function commit() 265 | { 266 | return new CommitCommandBuilder($this); 267 | } 268 | 269 | /** 270 | * Create tag command. 271 | * 272 | * @return TagCommandBuilder 273 | */ 274 | public function tag() 275 | { 276 | return new TagCommandBuilder($this); 277 | } 278 | 279 | /** 280 | * Create show command. 281 | * 282 | * @return ShowCommandBuilder 283 | */ 284 | public function show() 285 | { 286 | return new ShowCommandBuilder($this); 287 | } 288 | 289 | /** 290 | * Create log command. 291 | * 292 | * @return LogCommandBuilder 293 | */ 294 | public function log() 295 | { 296 | return new LogCommandBuilder($this); 297 | } 298 | 299 | /** 300 | * Create shortlog command. 301 | * 302 | * @return ShortLogCommandBuilder 303 | */ 304 | public function shortlog() 305 | { 306 | return new ShortLogCommandBuilder($this); 307 | } 308 | 309 | /** 310 | * Create ls-remote command. 311 | * 312 | * @return LsRemoteCommandBuilder 313 | */ 314 | public function lsRemote() 315 | { 316 | return new LsRemoteCommandBuilder($this); 317 | } 318 | 319 | /** 320 | * Create Merge command. 321 | * 322 | * @return MergeCommandBuilder 323 | */ 324 | public function merge() 325 | { 326 | return new MergeCommandBuilder($this); 327 | } 328 | 329 | /** 330 | * Create Pull command. 331 | * 332 | * @return PullCommandBuilder 333 | */ 334 | public function pull() 335 | { 336 | return new PullCommandBuilder($this); 337 | } 338 | 339 | /** 340 | * Create stash command. 341 | * 342 | * @return StashCommandBuilder 343 | */ 344 | public function stash() 345 | { 346 | return new StashCommandBuilder($this); 347 | } 348 | } 349 | -------------------------------------------------------------------------------- /tests/GitRepositoryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | * 11 | * This project is provided in good faith and hope to be usable by anyone. 12 | * 13 | * @package bit3/git-php 14 | * @author Tristan Lins 15 | * @author Christian Schiffler 16 | * @author Matthew Gamble 17 | * @author Sven Baumann 18 | * @copyright 2014-2022 Tristan Lins 19 | * @license https://github.com/bit3/git-php/blob/master/LICENSE MIT 20 | * @link https://github.com/bit3/git-php 21 | * @filesource 22 | */ 23 | 24 | namespace Bit3\GitPhp\Test; 25 | 26 | use Bit3\GitPhp\GitRepository; 27 | use PHPUnit\Framework\TestCase; 28 | use Symfony\Component\Filesystem\Filesystem; 29 | use Symfony\Component\Process\Process; 30 | 31 | /** 32 | * GIT repository unit tests. 33 | */ 34 | class GitRepositoryTest extends TestCase 35 | { 36 | /** 37 | * @var string 38 | */ 39 | protected $initializedRepositoryPath; 40 | 41 | /** 42 | * @var string 43 | */ 44 | protected $uninitializedRepositoryPath; 45 | 46 | /** 47 | * @var GitRepository 48 | */ 49 | protected $initializedGitRepository; 50 | 51 | /** 52 | * @var GitRepository 53 | */ 54 | protected $uninitializedGitRepository; 55 | 56 | public function setUp() 57 | { 58 | $this->initializedRepositoryPath = \tempnam(\sys_get_temp_dir(), 'git_'); 59 | \unlink($this->initializedRepositoryPath); 60 | \mkdir($this->initializedRepositoryPath); 61 | 62 | $this->uninitializedRepositoryPath = \tempnam(\sys_get_temp_dir(), 'git_'); 63 | \unlink($this->uninitializedRepositoryPath); 64 | \mkdir($this->uninitializedRepositoryPath); 65 | 66 | $zip = new \ZipArchive(); 67 | $zip->open(__DIR__ . DIRECTORY_SEPARATOR . 'git.zip'); 68 | $zip->extractTo($this->initializedRepositoryPath); 69 | 70 | $this->initializedGitRepository = new GitRepository($this->initializedRepositoryPath); 71 | $this->uninitializedGitRepository = new GitRepository($this->uninitializedRepositoryPath); 72 | } 73 | 74 | public function tearDown() 75 | { 76 | $fs = new Filesystem(); 77 | $fs->remove($this->initializedRepositoryPath); 78 | $fs->remove($this->uninitializedRepositoryPath); 79 | 80 | unset($this->initializedRepositoryPath); 81 | unset($this->uninitializedRepositoryPath); 82 | unset($this->initializedGitRepository); 83 | unset($this->uninitializedGitRepository); 84 | } 85 | 86 | /** 87 | * @covers \Bit3\GitPhp\GitRepository::getRepositoryPath 88 | */ 89 | public function testGetRepositoryPath() 90 | { 91 | $this->assertEquals( 92 | $this->initializedRepositoryPath, 93 | $this->initializedGitRepository->getRepositoryPath() 94 | ); 95 | $this->assertEquals( 96 | $this->uninitializedRepositoryPath, 97 | $this->uninitializedGitRepository->getRepositoryPath() 98 | ); 99 | } 100 | 101 | /** 102 | * @covers \Bit3\GitPhp\GitRepository::isInitialized 103 | */ 104 | public function testIsInitialized() 105 | { 106 | $this->assertTrue( 107 | $this->initializedGitRepository->isInitialized() 108 | ); 109 | $this->assertFalse( 110 | $this->uninitializedGitRepository->isInitialized() 111 | ); 112 | } 113 | 114 | /** 115 | * @covers \Bit3\GitPhp\GitRepository::init 116 | * @covers \Bit3\GitPhp\Command\InitCommandBuilder::execute 117 | */ 118 | public function testInit() 119 | { 120 | $this->uninitializedGitRepository->init()->execute(); 121 | 122 | $this->assertTrue( 123 | \is_dir($this->uninitializedRepositoryPath . DIRECTORY_SEPARATOR . '.git') 124 | ); 125 | } 126 | 127 | /** 128 | * @covers \Bit3\GitPhp\GitRepository::config 129 | * @covers \Bit3\GitPhp\Command\ConfigCommandBuilder::execute 130 | * @covers \Bit3\GitPhp\Command\ConfigCommandBuilder::get 131 | */ 132 | public function testConfigGetOnInitializedRepository() 133 | { 134 | $this->assertEquals( 135 | 'false', 136 | $this->initializedGitRepository->config()->file('local')->execute('core.bare') 137 | ); 138 | $this->assertEquals( 139 | 'CCA unittest', 140 | $this->initializedGitRepository->config()->file('local')->get('user.name')->execute() 141 | ); 142 | } 143 | 144 | /** 145 | * @covers \Bit3\GitPhp\GitRepository::config 146 | * @covers \Bit3\GitPhp\Command\ConfigCommandBuilder 147 | */ 148 | public function testConfigGetOnUnitializedRepository() 149 | { 150 | if (\method_exists($this, 'setExpectedException')) { 151 | $this->setExpectedException('Bit3\GitPhp\GitException'); 152 | } else { 153 | $this->expectException('Bit3\GitPhp\GitException'); 154 | } 155 | 156 | $this->uninitializedGitRepository->config()->file('local')->execute('core.bare'); 157 | $this->uninitializedGitRepository->config()->file('local')->get('user.name')->execute(); 158 | } 159 | 160 | public function testConfigSetOnInitializedRepository() 161 | { 162 | $this->initializedGitRepository->config()->file('local')->execute('user.name', 'CCA unittest 2'); 163 | 164 | $process = new Process(['git', 'config', '--local', 'user.name'], $this->initializedRepositoryPath); 165 | $process->run(); 166 | 167 | $this->assertEquals( 168 | 'CCA unittest 2', 169 | trim($process->getOutput()) 170 | ); 171 | } 172 | 173 | public function testConfigAddOnInitializedRepository() 174 | { 175 | $this->initializedGitRepository->config()->file('local')->add('user.name', 'CCA unittest 2')->execute(); 176 | 177 | $process = new Process( 178 | ['git', 'config', '--local', '--get-all', 'user.name'], $this->initializedRepositoryPath 179 | ); 180 | $process->run(); 181 | 182 | $names = \explode("\n", $process->getOutput()); 183 | $names = \array_map('trim', $names); 184 | $names = \array_filter($names); 185 | 186 | $this->assertEquals( 187 | ['CCA unittest', 'CCA unittest 2'], 188 | $names 189 | ); 190 | } 191 | 192 | public function testConfigGetAllOnInitializedRepository() 193 | { 194 | $values = $this->initializedGitRepository->config()->file('local')->getAll('gitphp.test2')->execute(); 195 | 196 | $values = \explode("\n", $values); 197 | $values = \array_map('trim', $values); 198 | $values = \array_filter($values); 199 | 200 | $this->assertEquals( 201 | ['aa123', 'ab234', 'ac345', 'bb234'], 202 | $values 203 | ); 204 | 205 | $values = $this->initializedGitRepository->config()->file('local')->getAll('gitphp.test2', '^a.+3.+$')->execute(); 206 | 207 | $values = \explode("\n", $values); 208 | $values = \array_map('trim', $values); 209 | $values = \array_filter($values); 210 | 211 | $this->assertEquals( 212 | ['ab234', 'ac345'], 213 | $values 214 | ); 215 | } 216 | 217 | /** 218 | * @covers \Bit3\GitPhp\GitRepository::remote 219 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::getNames 220 | */ 221 | public function testListRemotesOnInitializedRepository() 222 | { 223 | $this->assertEquals( 224 | ['local'], 225 | $this->initializedGitRepository->remote()->getNames() 226 | ); 227 | } 228 | 229 | /** 230 | * @covers \Bit3\GitPhp\GitRepository::remote 231 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::getNames 232 | */ 233 | public function testListRemotesOnUninitializedRepository() 234 | { 235 | if (\method_exists($this, 'setExpectedException')) { 236 | $this->setExpectedException('Bit3\GitPhp\GitException'); 237 | } else { 238 | $this->expectException('Bit3\GitPhp\GitException'); 239 | } 240 | 241 | $this->uninitializedGitRepository->remote()->getNames(); 242 | } 243 | 244 | /** 245 | * @covers \Bit3\GitPhp\GitRepository::branch 246 | * @covers \Bit3\GitPhp\Command\BranchCommandBuilder::all 247 | * @covers \Bit3\GitPhp\Command\BranchCommandBuilder::getNames 248 | */ 249 | public function testListBranchesOnInitializedRepository() 250 | { 251 | $this->assertEquals( 252 | ['master'], 253 | $this->initializedGitRepository->branch()->getNames() 254 | ); 255 | $this->assertEquals( 256 | ['master', 'remotes/local/master'], 257 | $this->initializedGitRepository->branch()->all()->getNames() 258 | ); 259 | } 260 | 261 | /** 262 | * @covers \Bit3\GitPhp\GitRepository::branch 263 | * @covers \Bit3\GitPhp\Command\BranchCommandBuilder::getNames 264 | */ 265 | public function testListBranchesOnUninitializedRepository() 266 | { 267 | if (\method_exists($this, 'setExpectedException')) { 268 | $this->setExpectedException('Bit3\GitPhp\GitException'); 269 | } else { 270 | $this->expectException('Bit3\GitPhp\GitException'); 271 | } 272 | 273 | $this->uninitializedGitRepository->branch()->getNames(); 274 | } 275 | 276 | /** 277 | * @covers \Bit3\GitPhp\GitRepository::describe 278 | * @covers \Bit3\GitPhp\Command\DescribeCommandBuilder::tags 279 | * @covers \Bit3\GitPhp\Command\DescribeCommandBuilder::all 280 | * @covers \Bit3\GitPhp\Command\DescribeCommandBuilder::execute 281 | */ 282 | public function testDescribeOnInitializedRepository() 283 | { 284 | $this->assertEquals( 285 | 'annotated-tag-2-g8dcaf85', 286 | $this->initializedGitRepository->describe()->execute() 287 | ); 288 | $this->assertEquals( 289 | 'lightweight-tag-1-g8dcaf85', 290 | $this->initializedGitRepository->describe()->tags()->execute() 291 | ); 292 | $this->assertEquals( 293 | 'heads/master', 294 | $this->initializedGitRepository->describe()->all()->execute() 295 | ); 296 | } 297 | 298 | /** 299 | * @covers \Bit3\GitPhp\GitRepository::describe 300 | * @covers \Bit3\GitPhp\Command\DescribeCommandBuilder::execute 301 | */ 302 | public function testDescribeOnUninitializedRepository() 303 | { 304 | if (\method_exists($this, 'setExpectedException')) { 305 | $this->setExpectedException('Bit3\GitPhp\GitException'); 306 | } else { 307 | $this->expectException('Bit3\GitPhp\GitException'); 308 | } 309 | 310 | $this->uninitializedGitRepository->describe()->execute(); 311 | } 312 | 313 | /** 314 | * @covers \Bit3\GitPhp\GitRepository::remote 315 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::setUrl 316 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 317 | */ 318 | public function testRemoteSetUrlOnInitializedRepository() 319 | { 320 | $this->initializedGitRepository->remote()->setUrl('local', $this->uninitializedRepositoryPath)->execute(); 321 | 322 | $process = new Process(['git', 'config', 'remote.local.url'], $this->initializedRepositoryPath); 323 | $process->run(); 324 | 325 | $this->assertEquals( 326 | \trim($process->getOutput()), 327 | $this->uninitializedRepositoryPath 328 | ); 329 | } 330 | 331 | /** 332 | * @covers \Bit3\GitPhp\GitRepository::remote 333 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::setUrl 334 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 335 | */ 336 | public function testRemoteSetUrlOnUninitializedRepository() 337 | { 338 | if (\method_exists($this, 'setExpectedException')) { 339 | $this->setExpectedException('Bit3\GitPhp\GitException'); 340 | } else { 341 | $this->expectException('Bit3\GitPhp\GitException'); 342 | } 343 | 344 | $this->uninitializedGitRepository->remote()->setUrl('local', $this->initializedRepositoryPath)->execute(); 345 | } 346 | 347 | /** 348 | * @covers \Bit3\GitPhp\GitRepository::remote 349 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::setPushUrl 350 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 351 | */ 352 | public function testRemoteSetPushUrlOnInitializedRepository() 353 | { 354 | $this->initializedGitRepository->remote()->setPushUrl('local', $this->uninitializedRepositoryPath)->execute(); 355 | 356 | $process = new Process(['git', 'config', 'remote.local.url'], $this->initializedRepositoryPath); 357 | $process->run(); 358 | 359 | $this->assertEquals( 360 | \trim($process->getOutput()), 361 | '/tmp/git' 362 | ); 363 | 364 | $process = new Process(['git', 'config', 'remote.local.pushurl'], $this->initializedRepositoryPath); 365 | $process->run(); 366 | 367 | $this->assertEquals( 368 | \trim($process->getOutput()), 369 | $this->uninitializedRepositoryPath 370 | ); 371 | } 372 | 373 | /** 374 | * @covers \Bit3\GitPhp\GitRepository::remote 375 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::setPushUrl 376 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 377 | */ 378 | public function testRemoteSetPushUrlOnUninitializedRepository() 379 | { 380 | if (\method_exists($this, 'setExpectedException')) { 381 | $this->setExpectedException('Bit3\GitPhp\GitException'); 382 | } else { 383 | $this->expectException('Bit3\GitPhp\GitException'); 384 | } 385 | 386 | $this->uninitializedGitRepository->remote()->setPushUrl('local', $this->initializedRepositoryPath)->execute(); 387 | } 388 | 389 | /** 390 | * @covers \Bit3\GitPhp\GitRepository::remote 391 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::add 392 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 393 | */ 394 | public function testRemoteAddOnInitializedRepository() 395 | { 396 | $this->initializedGitRepository->remote()->add('origin', $this->uninitializedRepositoryPath)->execute(); 397 | 398 | $process = new Process(['git', 'config', 'remote.origin.url'], $this->initializedRepositoryPath); 399 | $process->run(); 400 | 401 | $this->assertEquals( 402 | trim($process->getOutput()), 403 | $this->uninitializedRepositoryPath 404 | ); 405 | } 406 | 407 | /** 408 | * @covers \Bit3\GitPhp\GitRepository::remote 409 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::add 410 | * @covers \Bit3\GitPhp\Command\RemoteCommandBuilder::execute 411 | */ 412 | public function testRemoteAddOnUninitializedRepository() 413 | { 414 | if (\method_exists($this, 'setExpectedException')) { 415 | $this->setExpectedException('Bit3\GitPhp\GitException'); 416 | } else { 417 | $this->expectException('Bit3\GitPhp\GitException'); 418 | } 419 | 420 | $this->uninitializedGitRepository->remote()->add('origin', $this->initializedRepositoryPath)->execute(); 421 | } 422 | 423 | /** 424 | * @covers \Bit3\GitPhp\GitRepository::fetch 425 | * @covers \Bit3\GitPhp\Command\FetchCommandBuilder::execute 426 | */ 427 | public function testRemoteFetchOnInitializedRepository() 428 | { 429 | $process = new Process( 430 | ['git', 'remote', 'add', 'origin', $this->initializedRepositoryPath], $this->initializedRepositoryPath 431 | ); 432 | $process->run(); 433 | 434 | $this->initializedGitRepository->fetch()->execute(); 435 | 436 | $process = new Process(['git', 'branch', '-a'], $this->initializedRepositoryPath); 437 | $process->run(); 438 | 439 | $branches = explode("\n", $process->getOutput()); 440 | $branches = array_map('trim', $branches); 441 | $branches = array_filter($branches); 442 | 443 | $this->assertTrue( 444 | \in_array('remotes/origin/master', $branches) 445 | ); 446 | } 447 | 448 | /** 449 | * @covers \Bit3\GitPhp\GitRepository::fetch 450 | * @covers \Bit3\GitPhp\Command\FetchCommandBuilder::execute 451 | */ 452 | public function testRemoteFetchOnUninitializedRepository() 453 | { 454 | if (\method_exists($this, 'setExpectedException')) { 455 | $this->setExpectedException('Bit3\GitPhp\GitException'); 456 | } else { 457 | $this->expectException('Bit3\GitPhp\GitException'); 458 | } 459 | 460 | $this->uninitializedGitRepository->fetch()->execute(); 461 | } 462 | 463 | /** 464 | * @covers \Bit3\GitPhp\GitRepository::checkout 465 | * @covers \Bit3\GitPhp\Command\CheckoutCommandBuilder::execute 466 | */ 467 | public function testCheckoutOnInitializedRepository() 468 | { 469 | $process = new Process( 470 | ['git', 'remote', 'add', 'origin', $this->initializedRepositoryPath], $this->initializedRepositoryPath 471 | ); 472 | $process->run(); 473 | 474 | $this->initializedGitRepository->checkout()->execute('6c42d7ba78e0e956bd4e25661a6c13d826ef590a'); 475 | 476 | $process = new Process(['git', 'describe'], $this->initializedRepositoryPath); 477 | $process->run(); 478 | 479 | $this->assertEquals( 480 | \trim($process->getOutput()), 481 | 'annotated-tag' 482 | ); 483 | } 484 | 485 | /** 486 | * @covers \Bit3\GitPhp\GitRepository::checkout 487 | * @covers \Bit3\GitPhp\Command\CheckoutCommandBuilder::execute 488 | */ 489 | public function testCheckoutOnUninitializedRepository() 490 | { 491 | if (\method_exists($this, 'setExpectedException')) { 492 | $this->setExpectedException('Bit3\GitPhp\GitException'); 493 | } else { 494 | $this->expectException('Bit3\GitPhp\GitException'); 495 | } 496 | 497 | $this->uninitializedGitRepository->checkout()->execute('foo'); 498 | } 499 | 500 | public function testPushOnInitializedRepository() 501 | { 502 | $this->markTestIncomplete(); 503 | } 504 | 505 | /** 506 | * @covers \Bit3\GitPhp\GitRepository::push 507 | * @covers \Bit3\GitPhp\Command\PushCommandBuilder::execute 508 | */ 509 | public function testPushOnUninitializedRepository() 510 | { 511 | if (\method_exists($this, 'setExpectedException')) { 512 | $this->setExpectedException('Bit3\GitPhp\GitException'); 513 | } else { 514 | $this->expectException('Bit3\GitPhp\GitException'); 515 | } 516 | 517 | $this->uninitializedGitRepository->push()->execute('foo'); 518 | } 519 | 520 | /** 521 | * @covers \Bit3\GitPhp\GitRepository::status 522 | * @covers \Bit3\GitPhp\Command\StatusCommandBuilder::getStatus 523 | */ 524 | public function testStatusOnInitializedRepository() 525 | { 526 | $status = $this->initializedGitRepository->status()->getStatus(); 527 | 528 | $this->assertEquals( 529 | [ 530 | 'removed-but-staged.txt' => ['index' => 'A', 'worktree' => 'D'], 531 | 'unknown-file.txt' => ['index' => '?', 'worktree' => '?'], 532 | ], 533 | $status 534 | ); 535 | } 536 | 537 | /** 538 | * @covers \Bit3\GitPhp\GitRepository::status 539 | * @covers \Bit3\GitPhp\Command\StatusCommandBuilder::getStatus 540 | */ 541 | public function testStatusOnUninitializedRepository() 542 | { 543 | if (\method_exists($this, 'setExpectedException')) { 544 | $this->setExpectedException('Bit3\GitPhp\GitException'); 545 | } else { 546 | $this->expectException('Bit3\GitPhp\GitException'); 547 | } 548 | 549 | $this->uninitializedGitRepository->status()->getStatus(); 550 | } 551 | 552 | /** 553 | * @covers \Bit3\GitPhp\GitRepository::add 554 | * @covers \Bit3\GitPhp\Command\AddCommandBuilder::execute 555 | */ 556 | public function testAddOnInitializedRepository() 557 | { 558 | $this->initializedGitRepository->add()->execute('unknown-file.txt'); 559 | 560 | $process = new Process(['git', 'status', '-s'], $this->initializedRepositoryPath); 561 | $process->run(); 562 | 563 | $status = \explode("\n", $process->getOutput()); 564 | $status = \array_map('trim', $status); 565 | 566 | $this->assertTrue( 567 | \in_array('A unknown-file.txt', $status) 568 | ); 569 | } 570 | 571 | /** 572 | * @covers \Bit3\GitPhp\GitRepository::add 573 | * @covers \Bit3\GitPhp\Command\AddCommandBuilder::execute 574 | */ 575 | public function testAddOnUninitializedRepository() 576 | { 577 | if (\method_exists($this, 'setExpectedException')) { 578 | $this->setExpectedException('Bit3\GitPhp\GitException'); 579 | } else { 580 | $this->expectException('Bit3\GitPhp\GitException'); 581 | } 582 | 583 | $this->uninitializedGitRepository->add()->execute('unknown-file.txt'); 584 | } 585 | 586 | /** 587 | * @covers \Bit3\GitPhp\GitRepository::rm 588 | * @covers \Bit3\GitPhp\Command\RmCommandBuilder::execute 589 | */ 590 | public function testRmOnInitializedRepository() 591 | { 592 | $this->initializedGitRepository->rm()->execute('existing-file.txt'); 593 | 594 | $process = new Process(['git', 'status', '-s'], $this->initializedRepositoryPath); 595 | $process->run(); 596 | 597 | $status = \explode("\n", $process->getOutput()); 598 | $status = \array_map('trim', $status); 599 | 600 | $this->assertTrue( 601 | \in_array('D existing-file.txt', $status) 602 | ); 603 | } 604 | 605 | /** 606 | * @covers \Bit3\GitPhp\GitRepository::rm 607 | * @covers \Bit3\GitPhp\Command\RmCommandBuilder::execute 608 | */ 609 | public function testRmOnUninitializedRepository() 610 | { 611 | if (\method_exists($this, 'setExpectedException')) { 612 | $this->setExpectedException('Bit3\GitPhp\GitException'); 613 | } else { 614 | $this->expectException('Bit3\GitPhp\GitException'); 615 | } 616 | 617 | $this->uninitializedGitRepository->rm()->execute('existing-file.txt'); 618 | } 619 | 620 | /** 621 | * @covers \Bit3\GitPhp\GitRepository::commit 622 | * @covers \Bit3\GitPhp\Command\CommitCommandBuilder::message 623 | * @covers \Bit3\GitPhp\Command\CommitCommandBuilder::execute 624 | */ 625 | public function testCommitOnInitializedRepository() 626 | { 627 | $this->initializedGitRepository->commit()->message('Commit changes')->execute(); 628 | 629 | $process = new Process(['git', 'status', '-s'], $this->initializedRepositoryPath); 630 | $process->run(); 631 | 632 | $status = \explode("\n", $process->getOutput()); 633 | $status = \array_map('trim', $status); 634 | $status = \array_filter($status); 635 | 636 | $this->assertEquals( 637 | [ 638 | 'D existing-file.txt', 639 | 'D removed-but-staged.txt', 640 | '?? unknown-file.txt', 641 | ], 642 | $status 643 | ); 644 | } 645 | 646 | /** 647 | * @covers \Bit3\GitPhp\GitRepository::commit 648 | * @covers \Bit3\GitPhp\Command\CommitCommandBuilder::message 649 | * @covers \Bit3\GitPhp\Command\CommitCommandBuilder::execute 650 | */ 651 | public function testCommitOnUninitializedRepository() 652 | { 653 | if (\method_exists($this, 'setExpectedException')) { 654 | $this->setExpectedException('Bit3\GitPhp\GitException'); 655 | } else { 656 | $this->expectException('Bit3\GitPhp\GitException'); 657 | } 658 | 659 | $this->uninitializedGitRepository->commit()->message('Commit changes')->execute(); 660 | } 661 | 662 | /** 663 | * @covers \Bit3\GitPhp\GitRepository::tag 664 | * @covers \Bit3\GitPhp\Command\TagCommandBuilder::execute 665 | */ 666 | public function testTagOnInitializedRepository() 667 | { 668 | $this->initializedGitRepository->tag()->execute('unit-test'); 669 | 670 | $process = new Process(['git', 'tag'], $this->initializedRepositoryPath); 671 | $process->run(); 672 | 673 | $tags = \explode("\n", $process->getOutput()); 674 | $tags = \array_map('trim', $tags); 675 | $tags = \array_filter($tags); 676 | 677 | $this->assertTrue( 678 | \in_array('unit-test', $tags) 679 | ); 680 | } 681 | 682 | /** 683 | * @covers \Bit3\GitPhp\GitRepository::tag 684 | * @covers \Bit3\GitPhp\Command\TagCommandBuilder::execute 685 | */ 686 | public function testTagOnUninitializedRepository() 687 | { 688 | if (\method_exists($this, 'setExpectedException')) { 689 | $this->setExpectedException('Bit3\GitPhp\GitException'); 690 | } else { 691 | $this->expectException('Bit3\GitPhp\GitException'); 692 | } 693 | 694 | $this->uninitializedGitRepository->tag()->execute('unit-test'); 695 | } 696 | } 697 | -------------------------------------------------------------------------------- /tests/git.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bit3/git-php/31c66b65864f3c0cc026a58ca01c7c5c3636c9bf/tests/git.zip --------------------------------------------------------------------------------