Skip to content

Commit

Permalink
up(gitx): update some logic for git branch create
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 15, 2022
1 parent 092cffc commit d1e6f02
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 99 deletions.
1 change: 1 addition & 0 deletions app/Console/Attach/Gitlab/BranchCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Toolkit\PFlag\FlagsParser;

/**
Expand Down
43 changes: 0 additions & 43 deletions app/Console/Attach/Gitlab/BranchCreateCmd.php

This file was deleted.

3 changes: 2 additions & 1 deletion app/Console/Controller/Gitx/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ public function logCommand(FlagsParser $fs, Output $output): void
* ml msg length filter.
* wl word length filter.
* --format The git log option `--pretty` value.
* can be one of oneline, short, medium, full, fuller, reference, email, raw, format:<string> and tformat:<string>.
* can be one of oneline, short, medium, full, fuller, reference, email,
* raw, format:<string> and tformat:<string>.
* -s, --style The style for generate for changelog.
* allow: markdown(<cyan>default</cyan>), simple, gh-release(ghr)
* --repo-url The git repo URL address. eg: https://github.com/inhere/kite
Expand Down
56 changes: 16 additions & 40 deletions app/Console/Controller/Gitx/GitFlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Helper\GitUtil;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use function array_keys;
use function implode;
Expand All @@ -27,11 +29,6 @@ class GitFlowController extends Controller

protected static string $desc = 'Some useful tool commands for git flow development';

/**
* @var array
*/
private array $config = [];

/**
* @var string
*/
Expand Down Expand Up @@ -59,6 +56,11 @@ protected static function commandAliases(): array
];
}

// protected function subCommands()
// {
// return [];
// }

/**
* @return string[]
*/
Expand All @@ -73,13 +75,13 @@ protected function getOptions(): array

protected function configure(): void
{
$this->config = $this->app->getArrayParam('gitflow');
$this->initParams($this->app->getArrayParam('gitflow'));

$this->forkRemote = $this->config['fork']['remote'] ?? '';
$this->mainRemote = $this->config['main']['remote'] ?? '';
$this->forkRemote = $this->params->getString('forkRemote');
$this->mainRemote = $this->params->getString('mainRemote');

if (!$this->forkRemote || !$this->mainRemote) {
$this->output->liteError('missing config for "fork.remote" and "main.remote" on "gitflow"');
$this->output->liteError('missing config for "forkRemote" and "mainRemote" on "gitflow"');
return;
}

Expand All @@ -99,13 +101,15 @@ protected function configure(): void
*
* @options
* --not-main bool;Dont push new branch to the main remote
* --dry-run bool;Dry-run the workflow, dont real execute
*
* @arguments
* branch string;The new branch name. eg: fea_6_12;required
* branch string;The new branch name. eg: fea_210612;required
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws Throwable
* @example
* Workflow:
* 1. git checkout to master
Expand All @@ -116,36 +120,8 @@ protected function configure(): void
*/
public function newBranchCommand(FlagsParser $fs, Output $output): void
{
// vdump($this->input->getArgs());
$notToMain = $fs->getOpt('not-main');
$newBranch = $fs->getArg('branch');

// $cmd = CmdRunner::new('git checkout master')->do(true);
// $cmd->afterOkRun("git pull {$this->mainRemote} master")
// ->afterOkRun("git checkout -b {$newBranch}")
// ->afterOkRun("git push -u {$this->forkRemote} {$newBranch}")
// ->afterOkRun("git push {$this->mainRemote} {$newBranch}");

// $dryRun = true;
$dryRun = $this->flags->getOpt('dry-run');

$cmd = CmdRunner::new()
->add('git checkout master')
->addf('git pull %s master', $this->mainRemote)
->add('git push') // update to origin
->addf('git checkout -b %s', $newBranch)
->addf('git push -u %s %s', $this->forkRemote, $newBranch)
->addWheref(static function () use ($notToMain) {
return $notToMain === false;
}, 'git push %s %s', $this->mainRemote, $newBranch)
// ->addf('git push %s %s', $this->mainRemote, $newBranch)
->setDryRun($dryRun)
->run(true);

if ($cmd->isSuccess()) {
// $output->error($cmd->getOutput() ?: 'Failure');
$output->success('Complete');
}
$bcCmd = new BranchCreateCmd($this->input, $output);
$bcCmd->run($fs->getFlags());
}

/**
Expand Down
34 changes: 19 additions & 15 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Inhere\Kite\Console\Attach\Gitlab\BranchCmd;
use Inhere\Kite\Console\Attach\Gitlab\ProjectCmd;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use Throwable;
Expand All @@ -37,6 +38,7 @@
use function realpath;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strpos;
use function strtoupper;
use function trim;
Expand Down Expand Up @@ -382,7 +384,7 @@ public function configCommand(FlagsParser $fs, Output $output): void
* checkout an new branch for development
*
* @options
* --not-main bool;Dont push new branch to the main remote
* --nm, --not-main bool;Dont push new branch to the main remote
*
* @arguments
* branch The new branch name. eg: fea_6_12
Expand All @@ -394,16 +396,8 @@ public function configCommand(FlagsParser $fs, Output $output): void
*/
public function newBranchCommand(FlagsParser $fs, Output $output): void
{
// $cmdName = $input->getCommand();
$cmdName = $this->getCommandName();
/** @see GitFlowController::newBranchCommand() */
$command = 'gitflow:newBranch';

$output->notice("gitlab: input '$cmdName', will redirect to '$command'");

// Console::app()->dispatch($command, $input->getArgs());
// Console::app()->dispatch($command, $this->flags->getRawArgs());
Console::app()->dispatch($command, $fs->getRawArgs());
$bcCmd = new BranchCreateCmd($this->input, $output);
$bcCmd->run($fs->getFlags());
}

/**
Expand Down Expand Up @@ -526,10 +520,12 @@ public function projectCommand(FlagsParser $fs, Output $output): void
* -m, --main bool;Open the main repo page
*
* @arguments
* remote The remote name. default: origin
* remote The remote name, can also use group/repo. default: origin
*
* @param FlagsParser $fs
* @param Output $output
* @example
* {binWithCmd} group/repo
*/
public function openCommand(FlagsParser $fs, Output $output): void
{
Expand All @@ -542,11 +538,19 @@ public function openCommand(FlagsParser $fs, Output $output): void

$remote = $fs->getArg('remote', $defRemote);

$info = $gitlab->getRemoteInfo($remote);
$link = $info->getHttpUrl();
// is url path.
if (str_contains($remote, '/')) {
if (str_starts_with($remote, 'http')) {
$link = $remote;
} else {
$link = $gitlab->getHost() . '/'. $remote;
}
} else {
$info = $gitlab->getRemoteInfo($remote);
$link = $info->getHttpUrl();
}

AppHelper::openBrowser($link);

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

Expand Down
114 changes: 114 additions & 0 deletions app/Console/SubCmd/Gitflow/BranchCreateCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\Gitflow;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Kite;

/**
* Class BranchCreateCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class BranchCreateCmd extends Command
{
protected static string $name = 'create';
protected static string $desc = 'create a new branch for git project';

/**
* @var string
*/
private string $forkRemote = '';

/**
* @var string
*/
private string $mainRemote = '';

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

protected function configure(): void
{
$this->initParams(Kite::cliApp()->getArrayParam('gitflow'));

$this->forkRemote = $this->params->getString('forkRemote');
$this->mainRemote = $this->params->getString('mainRemote');

if (!$this->forkRemote || !$this->mainRemote) {
$this->output->liteError('missing config for "forkRemote" and "mainRemote" on "gitflow"');
return;
}

$this->addCommentsVars([
'forkRemote' => $this->forkRemote,
'mainRemote' => $this->mainRemote,
]);

parent::configure();
}

/**
* 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
*
* @arguments
* branch string;The new branch name. eg: fea_6_12;required
*
* @param Input $input
* @param Output $output
*
* @return mixed
* @example
* Workflow:
* 1. git checkout to master
* 2. git pull <info>{mainRemote}</info> master
* 3. git checkout -b NEW_BRANCH
* 4. git push -u <info>{forkRemote}</info> NEW_BRANCH
* 5. git push <info>{mainRemote}</info> NEW_BRANCH
*/
protected function execute(Input $input, Output $output): mixed
{
$fs = $this->flags;

$notToMain = $fs->getOpt('not-main');
$newBranch = $fs->getArg('branch');

// $cmd = CmdRunner::new('git checkout master')->do(true);
// $cmd->afterOkRun("git pull {$this->mainRemote} master")
// ->afterOkRun("git checkout -b {$newBranch}")
// ->afterOkRun("git push -u {$this->forkRemote} {$newBranch}")
// ->afterOkRun("git push {$this->mainRemote} {$newBranch}");

// $dryRun = true;
$dryRun = $fs->getOpt('dry-run');

$cmd = CmdRunner::new()
->add('git checkout master')
->addf('git pull %s master', $this->mainRemote)
->add('git push') // update to origin
->addf('git checkout -b %s', $newBranch)
->addf('git push -u %s %s', $this->forkRemote, $newBranch)
->addWheref(static function () use ($notToMain) {
return $notToMain === false;
}, 'git push %s %s', $this->mainRemote, $newBranch)
// ->addf('git push %s %s', $this->mainRemote, $newBranch)
->setDryRun($dryRun)
->run(true);

if ($cmd->isSuccess()) {
// $output->error($cmd->getOutput() ?: 'Failure');
$output->success('Complete');
}

return 0;
}
}
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
// 'auto-sign' => true,
// 'sign-text' => 'inhere <in.798@qq.com>',
],
'gitflow' => [
// remote
'mainRemote' => 'main',
'forkRemote' => 'origin',
],
'gitlab' => [
// remote
'mainRemote' => 'main',
Expand Down

0 comments on commit d1e6f02

Please sign in to comment.