From ccd461ec0601858d89e4c2864dc313af022253b1 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 14 Jan 2023 11:55:04 +0800 Subject: [PATCH] :necktie: up: update git, gitlab, github some sub commands logic --- .../Controller/Gitx/GitHubController.php | 13 +-- .../Controller/Gitx/GitLabController.php | 14 --- .../Controller/Gitx/GitxController.php | 90 +------------------ ...eeController.php => SubtreeController.php} | 2 +- .../SubCmd/Gitflow/BranchCreateCmd.php | 15 ++-- .../SubCmd/GitlabCmd/BranchCleanCmd.php | 2 +- app/Console/SubCmd/GitlabCmd/BranchCmd.php | 7 +- app/Console/SubCmd/GitxCmd/BatchCmd.php | 50 +++++++++++ app/Console/SubCmd/GitxCmd/BatchStatusCmd.php | 63 +++++++++++++ .../SubCmd/{ => GitxCmd}/BranchCmd.php | 9 +- .../{Gitflow => GitxCmd}/BranchDeleteCmd.php | 2 +- .../{Gitflow => GitxCmd}/BranchListCmd.php | 35 ++------ app/Console/SubCmd/GitxCmd/ChangelogCmd.php | 2 +- app/Console/SubCmd/GitxCmd/GitTagCmd.php | 1 + app/Console/SubCmd/GitxCmd/GitTagInfoCmd.php | 50 +++++++++++ 15 files changed, 200 insertions(+), 155 deletions(-) rename app/Console/Controller/Gitx/{GitSubtreeController.php => SubtreeController.php} (99%) create mode 100644 app/Console/SubCmd/GitxCmd/BatchCmd.php create mode 100644 app/Console/SubCmd/GitxCmd/BatchStatusCmd.php rename app/Console/SubCmd/{ => GitxCmd}/BranchCmd.php (80%) rename app/Console/SubCmd/{Gitflow => GitxCmd}/BranchDeleteCmd.php (96%) rename app/Console/SubCmd/{Gitflow => GitxCmd}/BranchListCmd.php (71%) create mode 100644 app/Console/SubCmd/GitxCmd/GitTagInfoCmd.php diff --git a/app/Console/Controller/Gitx/GitHubController.php b/app/Console/Controller/Gitx/GitHubController.php index ba033b9..8c0d2bb 100644 --- a/app/Console/Controller/Gitx/GitHubController.php +++ b/app/Console/Controller/Gitx/GitHubController.php @@ -20,6 +20,7 @@ use Inhere\Kite\Console\SubCmd\Gitflow\UpdatePushCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\AddCommitCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\AddCommitPushCmd; +use Inhere\Kite\Console\SubCmd\GitxCmd\BatchCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\ChangelogCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\GitEmojiCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\GitTagCmd; @@ -56,19 +57,12 @@ public static function aliases(): array protected static function commandAliases(): array { - return array_merge([ + return [ 'wf' => 'workflow', 'rls' => 'release', 'pr' => 'pullRequest', 'redirectList' => ['rl'], - ], [ - GitEmojiCmd::getName() => GitEmojiCmd::aliases(), - UpdatePushCmd::getName() => UpdatePushCmd::aliases(), - UpdateNoPushCmd::getName() => UpdateNoPushCmd::aliases(), - AddCommitCmd::getName() => AddCommitCmd::aliases(), - AddCommitPushCmd::getName() => AddCommitPushCmd::aliases(), - ChangelogCmd::getName() => ChangelogCmd::aliases(), - ]); + ]; } /** @@ -77,6 +71,7 @@ protected static function commandAliases(): array protected function subCommands(): array { return [ + BatchCmd::class, GitTagCmd::class, GitEmojiCmd::class, UpdatePushCmd::class, diff --git a/app/Console/Controller/Gitx/GitLabController.php b/app/Console/Controller/Gitx/GitLabController.php index 3390f66..7983cf0 100644 --- a/app/Console/Controller/Gitx/GitLabController.php +++ b/app/Console/Controller/Gitx/GitLabController.php @@ -74,23 +74,9 @@ protected static function commandAliases(): array 'li' => 'linkInfo', 'cf' => 'config', 'conf' => 'config', - 'rc' => 'resolve', 'new' => 'create', - 'up' => 'update', - 'updatePush' => ['upp', 'up-push'], 'project' => ['pj', 'info'], 'checkout' => ['co'], - ] + [ - GitLogCmd::getName() => GitLogCmd::aliases(), - GitEmojiCmd::getName() => GitEmojiCmd::aliases(), - BranchCmd::getName() => BranchCmd::aliases(), - MergeRequestCmd::getName() => MergeRequestCmd::aliases(), - ResolveConflictCmd::getName() => ResolveConflictCmd::aliases(), - UpdatePushCmd::getName() => UpdatePushCmd::aliases(), - UpdateNoPushCmd::getName() => UpdateNoPushCmd::aliases(), - AddCommitCmd::getName() => AddCommitCmd::aliases(), - AddCommitPushCmd::getName() => AddCommitPushCmd::aliases(), - ChangelogCmd::getName() => ChangelogCmd::aliases(), ]; } diff --git a/app/Console/Controller/Gitx/GitxController.php b/app/Console/Controller/Gitx/GitxController.php index 647e280..cc4a66f 100644 --- a/app/Console/Controller/Gitx/GitxController.php +++ b/app/Console/Controller/Gitx/GitxController.php @@ -18,18 +18,16 @@ use Inhere\Kite\Console\Manager\GitBranchManager; use Inhere\Kite\Console\SubCmd\GitxCmd\AddCommitCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\AddCommitPushCmd; +use Inhere\Kite\Console\SubCmd\GitxCmd\BatchCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\BranchCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\ChangelogCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\GitEmojiCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\GitLogCmd; use Inhere\Kite\Console\SubCmd\GitxCmd\GitTagCmd; -use Inhere\Kite\Console\SubCmd\GitxCmd\GitTagCreateCmd; -use Inhere\Kite\Console\SubCmd\GitxCmd\GitTagDelCmd; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\GitUtil; use Inhere\Kite\Kite; use PhpGit\Repo; -use Throwable; use Toolkit\Cli\Cli; use Toolkit\Cli\Util\Clog; use Toolkit\FsUtil\FS; @@ -75,25 +73,6 @@ protected static function commandAliases(): array 'update' => ['up', 'pul', 'pull'], 'batchPull' => ['bp', 'bpul', 'bpull'], 'tagFind' => ['tagfind', 'tag-find'], - 'tagNew' => [ - 'tagnew', - 'tag-new', - 'tn', - 'newtag', - 'new-tag', - 'tagpush', - 'tp', - 'tag-push', - ], - 'tagInfo' => ['tag-info', 'ti', 'tag-show'], - ] + [ - BranchCmd::getName() => BranchCmd::aliases(), - GitTagCmd::getName() => GitTagCmd::aliases(), - GitLogCmd::getName() => GitLogCmd::aliases(), - GitEmojiCmd::getName() => GitEmojiCmd::aliases(), - ChangelogCmd::getName() => ChangelogCmd::aliases(), - AddCommitCmd::getName() => AddCommitCmd::aliases(), - AddCommitPushCmd::getName() => AddCommitPushCmd::aliases(), ]; } @@ -103,6 +82,7 @@ protected static function commandAliases(): array protected function subCommands(): array { return [ + BatchCmd::class, BranchCmd::class, GitTagCmd::class, GitLogCmd::class, @@ -322,7 +302,6 @@ public function openCommand(FlagsParser $fs, Output $output): void $info = $repo->getRemoteInfo($remote); AppHelper::openBrowser($info->getHttpUrl()); - $output->success('Complete'); } @@ -423,69 +402,4 @@ public function tagFindCommand(FlagsParser $fs, Input $input, Output $output): v $output->printf($title, $tagName); } - /** - * display git tag information by `git show TAG` - * - * @arguments - * tag string;Tag name for show info;required - * - * @param FlagsParser $fs - * @param Output $output - */ - public function tagInfoCommand(FlagsParser $fs, Output $output): void - { - $tag = $fs->getArg('tag'); - - $commands = [ - "git show $tag", - ]; - - CmdRunner::new()->batch($commands)->runAndPrint(); - $output->success('Complete'); - } - - /** - * Add new tag version and push to the remote git repos - * - * @options - * -v, --version The new tag version. e.g: v2.0.4 - * -m, --message The message for add new tag. - * --hash The hash ID for add new tag. default is HEAD - * -n, --next bool;Auto calc next version for add new tag. - * --no-auto-add-v bool;Not auto add 'v' for add tag version. - * - * @param FlagsParser $fs - * @param Output $output - * - * @throws Throwable - * @deprecated - */ - public function tagNewCommand(FlagsParser $fs, Output $output): void - { - $output->warning('TIP: deprecated, please call `git tag create`'); - $cmd = new GitTagCreateCmd($this->input, $output); - $cmd->run($fs->getFlags()); - } - - /** - * delete an local and remote tag by `git tag` - * - * @options - * -r, --remote The remote name. default origin - * -v, --tag The tag version. eg: v2.0.3 - * --no-remote bool;Only delete local tag - * - * @param FlagsParser $fs - * @param Output $output - * - * @throws Throwable - * @deprecated - */ - public function tagDeleteCommand(FlagsParser $fs, Output $output): void - { - $output->warning('TIP: deprecated, please call `git tag delete`'); - $cmd = new GitTagDelCmd($this->input, $output); - $cmd->run($fs->getFlags()); - } - } diff --git a/app/Console/Controller/Gitx/GitSubtreeController.php b/app/Console/Controller/Gitx/SubtreeController.php similarity index 99% rename from app/Console/Controller/Gitx/GitSubtreeController.php rename to app/Console/Controller/Gitx/SubtreeController.php index edbc312..2318f5c 100644 --- a/app/Console/Controller/Gitx/GitSubtreeController.php +++ b/app/Console/Controller/Gitx/SubtreeController.php @@ -31,7 +31,7 @@ * * @author inhere */ -class GitSubtreeController extends Controller +class SubtreeController extends Controller { public const TYPE_SSL = 'git@github.com:'; public const TYPE_HTTPS = 'https://github.com/'; diff --git a/app/Console/SubCmd/Gitflow/BranchCreateCmd.php b/app/Console/SubCmd/Gitflow/BranchCreateCmd.php index b3a2cb4..165323c 100644 --- a/app/Console/SubCmd/Gitflow/BranchCreateCmd.php +++ b/app/Console/SubCmd/Gitflow/BranchCreateCmd.php @@ -9,9 +9,10 @@ use Inhere\Kite\Common\GitLocal\AbstractGitx; use Inhere\Kite\Common\GitLocal\GitFactory; use PhpGit\Info\BranchInfos; +use Toolkit\Stdlib\Helper\Assert; use function date; +use function preg_replace_callback; use function str_contains; -use function str_replace; /** * Class BranchCreateCmd @@ -68,13 +69,15 @@ protected function configure(): void * --dry-run bool;Dry-run the workflow, dont real execute * * @arguments - * branch string;The new branch name. eg: fea_220612;required + * branch string;The new branch name, allow var: {ymd};required * * @param Input $input * @param Output $output * * @return mixed * @example + * {binWithCmd} fea_220612 + * {binWithCmd} fix_{ymd} * Workflow: * 1. git checkout to master * 2. git pull {mainRemote} master @@ -86,13 +89,15 @@ protected function execute(Input $input, Output $output): mixed { $fs = $this->flags; - $repo = $this->gx->getRepo(); $brName = $fs->getArg('branch'); - if (str_contains($brName, '{ymd}')) { - $brName = str_replace('{ymd}', date('ymd'), $brName); + if (str_contains($brName, '{')) { + $brName = preg_replace_callback('/{([\w-]+)}/', static fn(array $m) => date($m[1]) ?: $m[0], $brName); + + Assert::isFalse(str_contains($brName, '{'), 'invalid name var for create branch'); } $output->info('fetch latest information from remote: ' . $this->mainRemote); + $repo = $this->gx->getRepo(); $repo->gitCmd('fetch', $this->mainRemote, '-np')->runAndPrint(); $bs = $repo->getBranchInfos(); diff --git a/app/Console/SubCmd/GitlabCmd/BranchCleanCmd.php b/app/Console/SubCmd/GitlabCmd/BranchCleanCmd.php index c3217af..8949399 100644 --- a/app/Console/SubCmd/GitlabCmd/BranchCleanCmd.php +++ b/app/Console/SubCmd/GitlabCmd/BranchCleanCmd.php @@ -23,7 +23,7 @@ class BranchCleanCmd extends Command public static function aliases(): array { - return ['clear']; + return ['clear', 'clr']; } protected function configFlags(FlagsParser $fs): void diff --git a/app/Console/SubCmd/GitlabCmd/BranchCmd.php b/app/Console/SubCmd/GitlabCmd/BranchCmd.php index e48560a..eed792f 100644 --- a/app/Console/SubCmd/GitlabCmd/BranchCmd.php +++ b/app/Console/SubCmd/GitlabCmd/BranchCmd.php @@ -6,7 +6,7 @@ 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 Inhere\Kite\Console\SubCmd\GitxCmd\BranchListCmd; use Throwable; use Toolkit\PFlag\FlagsParser; @@ -50,7 +50,8 @@ protected function configFlags(FlagsParser $fs): void protected function execute(Input $input, Output $output): void { // default run - $bcCmd = new BranchListCmd($input, $output); - $bcCmd->run($this->flags->getFlags()); + $this->showHelp(); + // $bcCmd = new BranchListCmd($input, $output); + // $bcCmd->run($this->flags->getFlags()); } } diff --git a/app/Console/SubCmd/GitxCmd/BatchCmd.php b/app/Console/SubCmd/GitxCmd/BatchCmd.php new file mode 100644 index 0000000..d65a9e8 --- /dev/null +++ b/app/Console/SubCmd/GitxCmd/BatchCmd.php @@ -0,0 +1,50 @@ +flags->addOptByRule($name, $rule); + } + + /** + * Do execute command + * + * @param Input $input + * @param Output $output + * + * @throws Throwable + */ + protected function execute(Input $input, Output $output): void + { + // default run + $this->showHelp(); + } +} diff --git a/app/Console/SubCmd/GitxCmd/BatchStatusCmd.php b/app/Console/SubCmd/GitxCmd/BatchStatusCmd.php new file mode 100644 index 0000000..fa38426 --- /dev/null +++ b/app/Console/SubCmd/GitxCmd/BatchStatusCmd.php @@ -0,0 +1,63 @@ +flags->addOptByRule($name, $rule); + } + + /** + * @options + * -i, --include Include match filter + * -e, --exclude Exclude match filter + * + * @arguments + * dirs... array;The parent dir for multi git repository;required + * + * @param Input $input + * @param Output $output + * + * @return int + */ + protected function execute(Input $input, Output $output): int + { + $fs = $this->flags; + $dirs = $fs->getArg('dirs'); + + foreach ($dirs as $dir) { + $output->colored("In the parent dir: " . $dir); + + foreach (Dir::getDirs($dir) as $subDir) { + $gitDir = Dir::join($dir, $subDir); + $output->colored("- In the git repo dir: " . $gitDir); + + Git::new($gitDir)->status->display(); + } + } + + return 0; + } +} diff --git a/app/Console/SubCmd/BranchCmd.php b/app/Console/SubCmd/GitxCmd/BranchCmd.php similarity index 80% rename from app/Console/SubCmd/BranchCmd.php rename to app/Console/SubCmd/GitxCmd/BranchCmd.php index e5de0ad..bf82121 100644 --- a/app/Console/SubCmd/BranchCmd.php +++ b/app/Console/SubCmd/GitxCmd/BranchCmd.php @@ -1,11 +1,10 @@ run($this->flags->getFlags()); + $this->showHelp(); + // $bcCmd = new BranchListCmd($input, $output); + // $bcCmd->run($this->flags->getFlags()); } } diff --git a/app/Console/SubCmd/Gitflow/BranchDeleteCmd.php b/app/Console/SubCmd/GitxCmd/BranchDeleteCmd.php similarity index 96% rename from app/Console/SubCmd/Gitflow/BranchDeleteCmd.php rename to app/Console/SubCmd/GitxCmd/BranchDeleteCmd.php index 0a77040..52f825b 100644 --- a/app/Console/SubCmd/Gitflow/BranchDeleteCmd.php +++ b/app/Console/SubCmd/GitxCmd/BranchDeleteCmd.php @@ -1,6 +1,6 @@ getOpt('inline'); + $search = $fs->getOpt('search'); + 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 : ''; + $list = $repo->getGit()->branch->getList($opts, $search); $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); - } + if (strlen($search) > 1) { + $msg .= "(keyword: $search)"; } $output->colored($msg . ':'); @@ -93,24 +83,13 @@ protected function execute(Input $input, Output $output) $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) { + } elseif ($fs->getOpt('only-name')) { $output->println(array_keys($matched)); } else { $output->table($matched, 'Git Branches'); diff --git a/app/Console/SubCmd/GitxCmd/ChangelogCmd.php b/app/Console/SubCmd/GitxCmd/ChangelogCmd.php index fb2953d..c0a3b49 100644 --- a/app/Console/SubCmd/GitxCmd/ChangelogCmd.php +++ b/app/Console/SubCmd/GitxCmd/ChangelogCmd.php @@ -24,7 +24,7 @@ class ChangelogCmd extends Command { protected static string $name = 'changelog'; - protected static string $desc = 'git branch manage tool command'; + protected static string $desc = 'collect git change log information by `git log`'; public static function aliases(): array { diff --git a/app/Console/SubCmd/GitxCmd/GitTagCmd.php b/app/Console/SubCmd/GitxCmd/GitTagCmd.php index 5320a78..d5cad70 100644 --- a/app/Console/SubCmd/GitxCmd/GitTagCmd.php +++ b/app/Console/SubCmd/GitxCmd/GitTagCmd.php @@ -29,6 +29,7 @@ protected function subCommands(): array GitTagListCmd::class, GitTagDelCmd::class, GitTagCreateCmd::class, + GitTagInfoCmd::class, ]; } diff --git a/app/Console/SubCmd/GitxCmd/GitTagInfoCmd.php b/app/Console/SubCmd/GitxCmd/GitTagInfoCmd.php new file mode 100644 index 0000000..6780b4c --- /dev/null +++ b/app/Console/SubCmd/GitxCmd/GitTagInfoCmd.php @@ -0,0 +1,50 @@ + 'string;Tag name for show info;required', + ]; + } + + /** + * display git tag information by `git show TAG` + * + * @param Input $input + * @param Output $output + */ + protected function execute(Input $input, Output $output) + { + $fs = $this->flags; + $tag = $fs->getArg('tag'); + + $commands = [ + "git show $tag", + ]; + + CmdRunner::new()->batch($commands)->runAndPrint(); + $output->success('Complete'); + } +}