Skip to content

Commit

Permalink
👔 up: update some git and gitlab commands logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 8, 2023
1 parent 045e203 commit ec8ceeb
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/Common/GitLocal/GitLab/GlProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getMainPid(): string
*/
public function getForkGroup(): string
{
return $this->forkGroup;
return $this->forkGroup ?: $this->group;
}

/**
Expand All @@ -82,7 +82,7 @@ public function getForkPid(): string
{
if (!$this->forkPid) {
// string pid: group + %2F + repo
$this->forkPid = $this->forkGroup . self::PID_SEP . $this->repo;
$this->forkPid = $this->getForkGroup() . self::PID_SEP . $this->repo;
}

return $this->forkPid;
Expand Down
1 change: 0 additions & 1 deletion app/Console/SubCmd/GitlabCmd/BranchCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Console\SubCmd\GitxCmd\BranchListCmd;
use Throwable;
use Toolkit\PFlag\FlagsParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\Gitflow;
namespace Inhere\Kite\Console\SubCmd\GitlabCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
Expand Down Expand Up @@ -65,8 +65,8 @@ protected function configure(): void
* checkout an new branch for development
*
* @options
* --nm, --not-main bool;Dont push new branch to the main remote
* --dry-run bool;Dry-run the workflow, dont real execute
* --nm, --not-main bool;Dont push new branch to the main remote
* --dry, --dry-run bool;Dry-run the workflow, dont real execute
*
* @arguments
* branch string;The new branch name, allow var: {ymd};required
Expand Down Expand Up @@ -102,7 +102,7 @@ protected function execute(Input $input, Output $output): mixed

$bs = $repo->getBranchInfos();
if ($bs->hasBranch($brName, BranchInfos::FROM_ALL)) {
$output->warning("Branch '%s' has been exists, please use checkout to switch");
$output->warning("Branch '$brName' has been exists, please use checkout to switch");
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion app/Console/SubCmd/GitlabCmd/MergeRequestCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function aliases(): array
}

/**
* generate an PR link for given project information
* Create new merge requests(PR/MR) by given project information
*
* @options
* -s, --source The source branch name.
Expand Down Expand Up @@ -151,6 +151,7 @@ protected function execute(Input $input, Output $output): int
];

$tipInfo = array_merge([
'direct' => $isDirect,
'name' => $pjName,
'glPath' => "$group/$repo",
], $prInfo);
Expand Down
79 changes: 79 additions & 0 deletions app/Console/SubCmd/GitxCmd/BatchRunCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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 Inhere\Kite\Common\Cmd;
use Toolkit\FsUtil\Dir;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;

/**
* Class BatchRunCmd
*/
class BatchRunCmd extends Command
{
protected static string $name = 'run';
protected static string $desc = 'batch run custom command on multi repository dir';

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

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

/**
* @options
* -i, --include Include match filter
* -e, --exclude Exclude match filter
* -c, --cmd Exec the command on each git repo dir
* Built in vars:
* {dir} current dir path.
* {d_name} current dir name.
* {p_dir} parent dir path.;true
* @arguments
* dirs... array;The parent dir for multi git repository;required
*
* @param Input $input
* @param Output $output
*
* @return int
* @example
* {cmdPath} -c 'pwd' ../
* {cmdPath} -c 'git status' ../
* {cmdPath} -c 'echo {d_name}' ../
* {cmdPath} -c 'kite git log 3' ../
*/
protected function execute(Input $input, Output $output): int
{
$fs = $this->flags;
$cmd = $fs->getOpt('cmd');
$dirs = $fs->getArg('dirs');

foreach ($dirs as $dir) {
$output->colored("In the parent dir: " . $dir);
$vars = [
'p_dir' => $dir,
];

foreach (Dir::getDirs($dir) as $subDir) {
$repoDir = Dir::join($dir, $subDir);

$output->colored("- In the git repo dir: " . $repoDir);
$vars['dir'] = $repoDir;
$vars['d_name'] = $subDir;

$cmdline = Str::renderVars($cmd, $vars, '{%s}');
Cmd::new('', $repoDir)->setCmdline($cmdline)->runAndPrint();
}
}

return 0;
}
}
63 changes: 63 additions & 0 deletions app/Console/SubCmd/GitxCmd/BranchCleanCmd.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\Repo;

/**
* class BranchCleanCmd
*
* @author inhere
* @date 2023/1/9
*/
class BranchCleanCmd extends Command
{
protected static string $name = 'clean';
protected static string $desc = 'quickly clean git branches by input conditions';

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

/**
* @options
* -a, --all bool;Display branches for all remote and local
* -r, --remote Both delete local and remote branches
* -s, --search The keywords for search branches, allow multi by comma.
* Start with ^ for exclude.
* --m, --match The branch name search match, support var and regex.
* eg: fix_{ymd}, fix_{ymd}*, fix_[\d]{6}.
* --cond, --condition The condition for clean handle logic.
* eg: $ymd < 180Day
*
* @param Input $input
* @param Output $output
* @example
* {binWithCmd} -m fix_{ymd} --cond '$ymd < 180Day'
*/
protected function execute(Input $input, Output $output): void
{
$fs = $this->flags;
$opts = [];
$repo = Repo::new();

$remote = '';
$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, $search);

foreach ($list as $item) {
$output->println("branch: {$item['name']}");
}
}
}

0 comments on commit ec8ceeb

Please sign in to comment.