Skip to content

Commit

Permalink
up: refactor git branch commands to independent class. up the git, gi…
Browse files Browse the repository at this point in the history
…tlab group
  • Loading branch information
inhere committed Jun 27, 2022
1 parent 03121e8 commit 26037c2
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 167 deletions.
13 changes: 9 additions & 4 deletions app/Console/Attach/Gitlab/BranchCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchListCmd;
use Throwable;
use Toolkit\PFlag\FlagsParser;

/**
* Class BranchCreateCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class BranchCmd extends Command
{
Expand All @@ -28,6 +29,8 @@ protected function subCommands(): array
return [
BranchInitCmd::class,
BranchCreateCmd::class,
BranchListCmd::class,
BranchDeleteCmd::class,
];
}

Expand All @@ -42,10 +45,12 @@ protected function configFlags(FlagsParser $fs): void
* @param Input $input
* @param Output $output
*
* @return int|mixed
* @throws Throwable
*/
protected function execute(Input $input, Output $output): mixed
protected function execute(Input $input, Output $output): void
{
return $this->showHelp();
// default run
$bcCmd = new BranchListCmd($input, $output);
$bcCmd->run($this->flags->getFlags());
}
}
117 changes: 117 additions & 0 deletions app/Console/Attach/Gitlab/BranchDeleteCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Attach\Gitlab;

use Inhere\Console\Command;
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Helper\AppHelper;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
use function implode;
use function strpos;

/**
* Class BranchDeleteCmd
*/
class BranchDeleteCmd extends Command
{
protected static string $name = 'delete';
protected static string $desc = 'quick delete branches from local, origin and main remote';

public static function aliases(): array
{
return ['del'];
}

protected function configFlags(FlagsParser $fs): void
{
// $this->flags->addOptByRule($name, $rule);
}

/**
* @options
* -f, --force bool;Force execute delete command, ignore error
* --nm, --not-main bool;Dont delete branch on the main remote
*
* @arguments
* branches... array;The want deleted branch name(s). eg: fea_6_12;required
*
* @param Input $input
* @param Output $output
*
*/
protected function execute(Input $input, Output $output): void
{
$fs = $this->flags;
$gl = AppHelper::newGitlab();

$names = $fs->getArg('branches');
if (!$names) {
throw new PromptException('please input an branch name');
}

$force = $fs->getOpt('force');
$notMain = $fs->getOpt('not-main');
$dryRun = $this->flags->getOpt('dry-run');

$deletedNum = 0;
$mainRemote = $gl->getMainRemote();
$output->colored('Will deleted: ' . implode(',', $names));
foreach ($names as $name) {
if (strpos($name, ',') > 0) {
$nameList = Str::explode($name, ',');
} else {
$nameList = [$name];
}

foreach ($nameList as $brName) {
$deletedNum++;
$run = CmdRunner::new();
$run->setDryRun($dryRun);

if ($force) {
$run->setIgnoreError(true);
}

$this->doDeleteBranch($brName, $mainRemote, $run, $notMain);
}
}

// $output->info('update git branch list after deleted');
// git fetch main --prune
// $run = CmdRunner::new();
// $run->add('git fetch origin --prune');
// $run->addf('git fetch %s --prune', $mainRemote);
// $run->run(true);

$output->success('Completed. Total delete: ' . $deletedNum);
}

/**
* @param string $name
* @param string $mainRemote
* @param CmdRunner $run
* @param bool $notMain
*/
protected function doDeleteBranch(string $name, string $mainRemote, CmdRunner $run, bool $notMain): void
{
$this->output->title("delete the branch: $name", [
'indent' => 0,
]);

$run->addf('git branch --delete %s', $name);
// git push origin --delete BRANCH
$run->addf('git push origin --delete %s', $name);

if (false === $notMain) {
// git push main --delete BRANCH
$run->addf('git push %s --delete %s', $mainRemote, $name);
}

$run->run(true);
}

}
2 changes: 1 addition & 1 deletion app/Console/Attach/Gitlab/BranchInitCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class BranchInitCmd extends Command
{
protected static string $name = 'init';
protected static string $desc = 'quick init testing, qa, pre branches for gitlab project';
protected static string $desc = 'quick init defined branches for gitlab project';

protected function configFlags(FlagsParser $fs): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class ProjectInit extends Command
class ProjectInitCmd extends Command
{
protected static string $name = 'init';
protected static string $desc = 'init a gitlab project information';
Expand Down
105 changes: 13 additions & 92 deletions app/Console/Controller/Gitx/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Inhere\Kite\Common\GitLocal\GitHub;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Manager\GitBranchManager;
use Inhere\Kite\Console\SubCmd\BranchCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use Inhere\Kite\Kite;
Expand All @@ -38,14 +39,11 @@
use Toolkit\Stdlib\Str;
use Toolkit\Sys\Proc\ProcTasks;
use function abs;
use function array_keys;
use function implode;
use function realpath;
use function sprintf;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function trim;

/**
Expand Down Expand Up @@ -104,6 +102,16 @@ protected static function commandAliases(): array
];
}

/**
* @return array
*/
protected function subCommands(): array
{
return [
BranchCmd::class,
];
}

/**
* @return string[]
*/
Expand Down Expand Up @@ -248,94 +256,6 @@ public function infoCommand(FlagsParser $fs, Output $output): void
]);
}

/**
* list branch by git branch
*
* @options
* -a, --all bool;Display all branches
* -r, --remote Display branches for the given remote
* --on, --only-name bool;Only display branch name
* --inline bool;Only display branch name and print inline
* -s, --search The keyword name for search branches, allow multi by comma.
* Start with ^ for exclude.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function branchCommand(FlagsParser $fs, Output $output): void
{
$opts = [];
// $dir = $this->getFlags()->getOpt('workdir');
$repo = Repo::new();

$remote = '';
$inline = $fs->getOpt('inline');
if ($fs->getOpt('all')) {
$opts['all'] = true;
} elseif ($remote = $fs->getOpt('remote')) {
$opts['remotes'] = true;
}

$list = $repo->getGit()->branch->getList($opts);

$onlyName = $fs->getOpt('only-name');
$keyword = $fs->getOpt('search');
$keyword = strlen($keyword) > 1 ? $keyword : '';

$msg = 'Branch List';
if (strlen($remote) > 1) {
$msg .= " Of '$remote'";
}

$exclude = '';
if ($keyword) {
$msg .= "(keyword: $keyword)";
if ($keyword[0] === '^') {
$exclude = Str::splitTrimmed(substr($keyword, 1));
$keyword = '';
} else {
$keyword = Str::splitTrimmed($keyword);
}
}

$output->colored($msg . ':');

$matched = [];
$rmtLen = strlen($remote) + 1;
foreach ($list as $name => $item) {
if ($remote) {
$pos = strpos($name, $remote . '/');
if ($pos !== 0) {
continue;
}

$name = substr($name, $rmtLen);
}

// 排除匹配
if ($exclude) {
if (Str::has($name, $exclude)) {
continue;
}

// 包含匹配搜索
} elseif ($keyword && !Str::has($name, $keyword)) {
continue;
}

$matched[$name] = $item;
}

// \vdump($keyword, $remote, $list);
if ($inline) {
$output->println(implode(',', array_keys($matched)));
} elseif ($onlyName) {
$output->println(array_keys($matched));
} else {
$output->table($matched, 'Git Branches');
}
}

/**
* Update branch list from remotes
*
Expand Down Expand Up @@ -870,7 +790,6 @@ public function logCommand(FlagsParser $fs, Output $output): void
*/
public function changelogCommand(FlagsParser $fs, Output $output): void
{
$builder = Git::new()->newCmd('log');
// see https://devhints.io/git-log-format
// useful options:
// --no-merges
Expand All @@ -887,6 +806,8 @@ public function changelogCommand(FlagsParser $fs, Output $output): void
$fetch->runAndPrint();
}

$builder = $repo->newCmd('log');

// git log v1.0.7...v1.0.8 --pretty=format:'<project>/commit/%H %s' --reverse
// git log v1.0.7...v1.0.7 --pretty=format:'<li> <a href="https://github.com/inhere/<project>/commit/%H">view commit &bull;</a> %s</li> ' --reverse
// git log v1.0.7...HEAD --pretty=format:'<li> <a href="https://github.com/inhere/<project>/commit/%H">view commit &bull;</a> %s</li> ' --reverse
Expand Down

0 comments on commit 26037c2

Please sign in to comment.