cyberaxio / gitmanager
简单的PHP Git管理器
1.0.1
2020-09-05 21:17 UTC
Requires
- php: ^7.2.5
- symfony/process: ^5.1
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.6.1
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^8.5
README
安装
使用以下命令安装最新版本
$ composer require cyberaxio/gitmanager
基本用法
<?php use Cyberaxio\GitManager\Manager; // create a new repository $repository = Manager::init($path); // retrieve an existing repository $repository = Manager::find($path); // create a new repository manager (Without actually init or cloning it) $repository = Manager::new(); // You can optionally pass an array config to the new method to alter default configuration $repository = Manager::new(['port' => 12345]); // Or use the following method $repository->setConfig('port', 12345);
在仓库目录内运行命令
<?php // Get raw command output $rawOutput = $repository->rawCommand('ls'); // You can also parse the output by passing a callback $parsedOutput = $repository->command('ls')->run() ->parseOutput(function($value){ if($value == condition){ return $value; } return false; })); // You can enable debug mode for each command by chaining method debug before method call. // For example, for a branch command. $repository->branches()->debug()->all();
克隆仓库
<?php // You can clone a public repo by passing the url and the path where it should be cloned. $repository = Manager::clone($url, $path, $name = null); // This will clone the repository into $path, within a folder with same name as repository // The name parameter could be set to clone repository into another name than repo name $repository = Manager::clone($url, $path, $name = "."); // This will clone repository into $path which should be empty $repository = Manager::clone($url, $path, $name = "AnotherName"); // This will clone repository into $path/AnotherName // If it is a private repo, provide the ssh url and the path to the private key (Which should be readable by your webserver) and the port (default to 22) $repository = Manager::clone($url, $path, $name, $pathToPrivateKey, $port); // You can also do theses steps separately // First, you need to get a repository instance. $repository = Manager::new(); // Pass the path where the repo will be cloned $repository->setPath($path); // Pass the url, we will check if the repo is readable before cloning it $repository->setUrl($url); // Optionally the port if it's on a private server $repository->setPort($port); // Set the private key (or deploy key) $repository->setPrivateKey($pathToPrivateKey); // This method accept the port on second parameters for convenience $repository->setPrivateKey($pathToPrivateKey, $port); // Now clone it $repository->clone();
获取/拉取/推送仓库
<?php $repository = Manager::find($repoPath); // You can fetch a repo $repository->fetch(); // You can pull the active branch $repository->pull(); // Or specific branch and options $repository->pull($branch, $options); // You can push the repo $repository->push(); // Or specific branch and options $repository->push($branch, $options);
与分支交互
<?php // You can access branches commands like this $repository->branches()->method(); // Or save branches command into variable $branches = $repository->branches(); $branches->method();
- 获取分支信息
<?php // Return array // Get all branches (local + remote) $branches->all(); // Get only local branches $branches->local(); // Get current branch $branches->current(); // Return boolean // Check if branch exists $branches->exists('master'); // true // or $branches->has('master'); // true
流畅的方法(可链式调用)
- 创建并检出分支
<?php // create a branch with name MyNewBranch in the repo $branches->create("MyNewBranch"); // or $branches->add("MyNewBranch"); // Switch on branch MyNewBranch $branches->checkout("MyNewBranch"); // Repo is now on MyNewBranch branch // You can switch directly on branch after creating it by passing true to create method $branches->create($name, true); // You can also create a new branch and switch on by passing true to checkout method $branches->checkout($name, true); // Or chaining methods $tags->create('v3.0')->checkout('v3.0');
- 重命名分支
<?php // To rename current branch $branches->rename($newName); // To rename any other branch $branches->rename($branch, $newName);
- 删除分支
<?php // To remove a branch $branches->remove($name); // Or $branches->delete($name);
- 合并分支
<?php // To merge a branch into active branch $branches->merge($name); // To merge a branch into master branch, just chain methods $branches->checkout("master")->merge("BranchToMerge"); // Combine all theses methods (Useless example, but this is for purpose only) $name="FeatureTwo"; $branches->create($name)->checkout('master')->merge($name)->remove($name); // -> Create FeatureTwo branch, checkout on master, merge FeatureTwo into master and remove FeatureTwo
工作副本命令
<?php // You can access Working Copy commands like this $repository->workingCopy()->method(); // Or save Working Copy command into variable $workingCopy = $repository->workingCopy(); $workingCopy->method();
- 检查工作目录是否干净/脏
<?php // Theses methods return boolean $workingCopy->isDirty(); // true // Or $workingCopy->isClean(); // false
流畅的方法(可链式调用)
- 将文件添加到索引
<?php $workingCopy->add($file); // To add all file in staging $workingCopy->addAll();
- 从索引中移除文件
<?php // This will remove file from index only, and keep file on the system $workingCopy->remove($file); // If you wish to delete the file on the system, set the second parameter to false $workingCopy->remove($file, false);
- 将文件重命名到索引
<?php $workingCopy->rename($file, $to);
- 提交
<?php // To commit all staged files $workingCopy->commit($message, $options);
远程命令
<?php // You can access remotes commands like this $repository->remotes()->method(); // Or save remotes command into variable $remotes = $repository->remotes(); $remotes->method();
- 获取远程信息
<?php // Return array // Get all remotes $repository->remotes()->all(); // Check if remote exists (return boolean) $remotes->exists('origin'); // true
流畅的方法(可链式调用)
- 添加远程
<?php // Add a remote $remotes->add("upstream", $url, $options); // or $remotes->create("upstream", $url, $options);
- 移除远程
<?php // Remove a remote $remotes->remove($name); // or $remotes->delete($name);
- 重命名远程
<?php $remotes->rename($oldName, $newName);
- 设置/获取远程URL
<?php // Set remote url $remotes->setUrl($name, $url, $options); // Get remote url $remotes->getUrl($name);
- 检查远程是否可读
<?php // Check if a remote is readable $remotes->isReadable($url);
标签命令
<?php // You can access tags commands like this $repository->tags()->method(); // Or save tags command into variable $tags = $repository->tags(); $tags->method();
- 获取标签信息
<?php // Return arrays // Get all tags $tags->all(); // Return boolean // Check if tag exists $tags->exists('v1.0.rc01'); // false // Or $tags->has('v1.0.rc01'); // false
流畅的方法(可链式调用)
- 创建标签
<?php // create a tag for active branch with name v2.0 $tags->create('v2.0'); // or $tags->add('v2.0');
- 移除标签
<?php // To remove a tag $tags->remove($name); // or $tags->delete($name);
- 创建别名/重命名标签
<?php // To create an alias from tag $tags->alias($current, $new); // To rename a tag (make alias and delete) $tags->rename($oldName, $newName); // Equivalent to $tags->alias($oldName, $newName)->remove($oldName);