Skip to content

Commit

Permalink
👔 up: update git, gitlab, github some sub commands logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 14, 2023
1 parent d50813f commit ccd461e
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 155 deletions.
13 changes: 4 additions & 9 deletions app/Console/Controller/Gitx/GitHubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
]);
];
}

/**
Expand All @@ -77,6 +71,7 @@ protected static function commandAliases(): array
protected function subCommands(): array
{
return [
BatchCmd::class,
GitTagCmd::class,
GitEmojiCmd::class,
UpdatePushCmd::class,
Expand Down
14 changes: 0 additions & 14 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}

Expand Down
90 changes: 2 additions & 88 deletions app/Console/Controller/Gitx/GitxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
];
}

Expand All @@ -103,6 +82,7 @@ protected static function commandAliases(): array
protected function subCommands(): array
{
return [
BatchCmd::class,
BranchCmd::class,
GitTagCmd::class,
GitLogCmd::class,
Expand Down Expand Up @@ -322,7 +302,6 @@ public function openCommand(FlagsParser $fs, Output $output): void
$info = $repo->getRemoteInfo($remote);

AppHelper::openBrowser($info->getHttpUrl());

$output->success('Complete');
}

Expand Down Expand Up @@ -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 <comment>origin</comment>
* -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down
15 changes: 10 additions & 5 deletions app/Console/SubCmd/Gitflow/BranchCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <info>{mainRemote}</info> master
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion app/Console/SubCmd/GitlabCmd/BranchCleanCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BranchCleanCmd extends Command

public static function aliases(): array
{
return ['clear'];
return ['clear', 'clr'];
}

protected function configFlags(FlagsParser $fs): void
Expand Down
7 changes: 4 additions & 3 deletions app/Console/SubCmd/GitlabCmd/BranchCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
50 changes: 50 additions & 0 deletions app/Console/SubCmd/GitxCmd/BatchCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\GitxCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Throwable;
use Toolkit\PFlag\FlagsParser;

/**
* Class BranchCmd
*/
class BatchCmd extends Command
{
protected static string $name = 'batch';
protected static string $desc = 'batch run or handle git commands';

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

protected function subCommands(): array
{
return [
BatchStatusCmd::class,
BatchRunCmd::class,
];
}

protected function configFlags(FlagsParser $fs): void
{
// $this->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();
}
}
63 changes: 63 additions & 0 deletions app/Console/SubCmd/GitxCmd/BatchStatusCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\GitxCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use PhpGit\Git;
use Toolkit\FsUtil\Dir;
use Toolkit\PFlag\FlagsParser;

/**
* Class BranchDeleteCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class BatchStatusCmd extends Command
{
protected static string $name = 'status';
protected static string $desc = 'quick check git status for multi repository dir';

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

protected function configFlags(FlagsParser $fs): void
{
// $this->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;
}
}

0 comments on commit ccd461e

Please sign in to comment.