Skip to content

Commit

Permalink
up: rename gitx class, update some git update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 28, 2022
1 parent 9c8e759 commit 8266072
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @package Inhere\Kite\Common\Git
*/
abstract class AbstractGitLocal
abstract class AbstractGitx
{
public const GITHUB_HOST = 'https://github.com';

Expand Down Expand Up @@ -502,7 +502,7 @@ public function getWorkDir(): string
/**
* @param string $workDir
*
* @return AbstractGitLocal
* @return AbstractGitx
*/
public function setWorkDir(string $workDir): self
{
Expand Down
12 changes: 6 additions & 6 deletions app/Common/GitLocal/GitFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpGit\Repo;

/**
* class Gitflow
* class GitFactory
*
* @author inhere
*/
Expand All @@ -15,23 +15,23 @@ class GitFactory
/**
* @param string $repoDir
*
* @return AbstractGitLocal
* @return AbstractGitx
*/
public static function make(string $repoDir = ''): AbstractGitLocal
public static function make(string $repoDir = ''): AbstractGitx
{
$repo = Repo::new($repoDir);

$platform = $repo->getPlatform();
$configKey = $platform !== Repo::PLATFORM_CUSTOM ? $platform : 'git';
$settings = Kite::config()->getArray($configKey);

$typGit = match ($platform) {
$gx = match ($platform) {
Repo::PLATFORM_GITHUB => new GitHub(null, $settings),
Repo::PLATFORM_GITLAB => new GitLab(null, $settings),
default => new GitLoc(null, $settings),
};

$typGit->setRepo($repo);
return $typGit;
$gx->setRepo($repo);
return $gx;
}
}
2 changes: 1 addition & 1 deletion app/Common/GitLocal/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package Inhere\Kite\Common\Git
*/
class GitHub extends AbstractGitLocal
class GitHub extends AbstractGitx
{
public const HOST = GitConst::GITHUB_HOST;
public const GIT_HOST = GitConst::GITHUB_HOST;
Expand Down
2 changes: 1 addition & 1 deletion app/Common/GitLocal/GitLab.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package Inhere\Kite\Common\Git
*/
class GitLab extends AbstractGitLocal
class GitLab extends AbstractGitx
{
/**
* @var string
Expand Down
4 changes: 2 additions & 2 deletions app/Common/GitLocal/GitLoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* class GitLoc
*/
class GitLoc extends AbstractGitLocal
class GitLoc extends AbstractGitx
{

}
4 changes: 2 additions & 2 deletions app/Console/Component/RedirectToGitGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Inhere\Kite\Console\Component;

use Inhere\Console\Controller;
use Inhere\Kite\Console\Controller\Gitx\GitController;
use Inhere\Kite\Console\Controller\Gitx\GitxController;
use Inhere\Kite\Kite;
use Throwable;
use Toolkit\Cli\Cli;
Expand Down Expand Up @@ -38,7 +38,7 @@ public function handle(Controller $ctrl, string $command, array $args): bool
$app = $ctrl->getApp();

// resolve alias
$gitCtrl = $app->getController(GitController::getName());
$gitCtrl = $app->getController(GitxController::getName());
$action = $gitCtrl->resolveAlias($command);
$group = $ctrl->getRealGName();

Expand Down
15 changes: 9 additions & 6 deletions app/Console/Controller/Gitx/GitHubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Common\GitLocal\GitHub;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdateNoPushCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdatePushCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
Expand Down Expand Up @@ -56,7 +57,8 @@ protected static function commandAliases(): array
'pr' => 'pullRequest',
'redirectList' => ['rl'],
], [
UpdatePushCmd::getName() => UpdatePushCmd::aliases(),
UpdatePushCmd::getName() => UpdatePushCmd::aliases(),
UpdateNoPushCmd::getName() => UpdateNoPushCmd::aliases(),
]);
}

Expand All @@ -67,6 +69,7 @@ protected function subCommands(): array
{
return [
UpdatePushCmd::class,
UpdateNoPushCmd::class,
];
}

Expand All @@ -77,7 +80,7 @@ protected function getOptions(): array
{
return [
'--try,--dry-run' => 'bool;Dry-run the workflow, dont real execute',
'-w, --workdir' => 'The command work dir, default is current dir.',
'-w, --workdir' => 'The command work dir, default is current dir.',
// '-y, --yes' => 'Direct execution without confirmation',
// '-i, --interactive' => 'Run in an interactive environment[TODO]',
];
Expand All @@ -103,7 +106,7 @@ protected function beforeRun(): void

/**
* @param string $command
* @param array $args
* @param array $args
*
* @return bool
* @throws Throwable
Expand All @@ -124,7 +127,7 @@ protected function onNotFound(string $command, array $args): bool
/**
* Show a list of commands that will be redirected to git
*
* @param Input $input
* @param Input $input
* @param Output $output
*/
public function redirectListCommand(Input $input, Output $output): void
Expand All @@ -147,7 +150,7 @@ public function redirectListCommand(Input $input, Output $output): void
* --last Use the latest tag for new release
* --next Auto calc next version for new release
*
* @param Input $input
* @param Input $input
* @param Output $output
*
* @example
Expand Down Expand Up @@ -269,7 +272,7 @@ public function cloneCommand(FlagsParser $fs, Output $output): void
$repo = $fs->getArg('repo');
$name = $fs->getArg('name');

$mirror = $fs->getOpt('mirror');
$mirror = $fs->getOpt('mirror');
$mirrors = [
'fast' => 'https://hub.fastgit.org',
'cnpmjs' => 'https://github.51.al.cnpmjs.org',
Expand Down
73 changes: 24 additions & 49 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Inhere\Kite\Console\Attach\Gitlab\ProjectCmd;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdateNoPushCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdatePushCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\ResolveConflictCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
Expand Down Expand Up @@ -66,21 +68,24 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'deleteBranch' => ['del-br', 'delbr', 'dbr', 'db'],
'newBranch' => ['new-br', 'newbr', 'nbr', 'nb'],
'li' => 'linkInfo',
'cf' => 'config',
'conf' => 'config',
'rc' => 'resolve',
'new' => 'create',
'up' => 'update',
'updatePush' => ['upp', 'up-push'],
'project' => ['pj', 'info'],
'checkout' => ['co'],
'branch' => BranchCmd::aliases(),
MergeRequestCmd::getName() => MergeRequestCmd::aliases(),
ResolveConflictCmd::getName() => ResolveConflictCmd::aliases(),
];
'deleteBranch' => ['del-br', 'delbr', 'dbr', 'db'],
'newBranch' => ['new-br', 'newbr', 'nbr', 'nb'],
'li' => 'linkInfo',
'cf' => 'config',
'conf' => 'config',
'rc' => 'resolve',
'new' => 'create',
'up' => 'update',
'updatePush' => ['upp', 'up-push'],
'project' => ['pj', 'info'],
'checkout' => ['co'],
] + [
BranchCmd::getName() => BranchCmd::aliases(),
MergeRequestCmd::getName() => MergeRequestCmd::aliases(),
ResolveConflictCmd::getName() => ResolveConflictCmd::aliases(),
UpdatePushCmd::getName() => UpdatePushCmd::aliases(),
UpdateNoPushCmd::getName() => UpdateNoPushCmd::aliases(),
];
}

/**
Expand All @@ -93,6 +98,8 @@ protected function subCommands(): array
ProjectCmd::class,
MergeRequestCmd::class,
ResolveConflictCmd::class,
UpdatePushCmd::class,
UpdateNoPushCmd::class,
];
}

Expand Down Expand Up @@ -469,6 +476,7 @@ public function projectCommand(FlagsParser $fs, Output $output): void
*
* @param FlagsParser $fs
* @param Output $output
*
* @example
* {binWithCmd} group/repo
*/
Expand All @@ -488,7 +496,7 @@ public function openCommand(FlagsParser $fs, Output $output): void
if (str_starts_with($remote, 'http')) {
$link = $remote;
} else {
$link = $gitlab->getHost() . '/'. $remote;
$link = $gitlab->getHost() . '/' . $remote;
}
} else {
$info = $gitlab->getRemoteInfo($remote);
Expand Down Expand Up @@ -654,39 +662,6 @@ public function createCommand(FlagsParser $fs, Output $output): void
$output->success("Create the '$name' ok!");
}

/**
* update codes from origin and main remote repository, then push to remote
*
* @param Output $output
*
* @throws Throwable
*/
public function updatePushCommand(Output $output): void
{
// $args = $this->flags->getRawArgs();
// // add option - do push
// $args[] = '--push';
//
// // run updateCommand();
// $this->runActionWithArgs('update', $args);

$this->runUpdateByGit(true, $output);
}

/**
* update codes from origin and main remote repositories
*
* @options
* -p, --push bool;Push to origin remote after update
*
* @param FlagsParser $fs
* @param Output $output
*/
public function updateCommand(FlagsParser $fs, Output $output): void
{
$this->runUpdateByGit($fs->getOpt('push'), $output);
}

/**
* @param bool $doPush
* @param Output $output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@
use function trim;

/**
* Class GitController
* - git:tag:push add tag and push to remote
* - git:tag:delete delete the tag on remote
* Class GitxController
*/
class GitController extends Controller
class GitxController extends Controller
{
protected static string $name = 'git';

Expand Down
54 changes: 54 additions & 0 deletions app/Console/SubCmd/Gitflow/UpdateNoPushCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 Throwable;
use Toolkit\PFlag\FlagsParser;

/**
* Class UpdateNoPushCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class UpdateNoPushCmd extends Command
{
protected static string $name = 'update';
protected static string $desc = 'update codes from origin and main remote repositories';

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

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

/**
* update codes from origin and main remote repository, then push to remote
*
* @options
* --dr, --dry-run bool;Dry-run the workflow, dont real execute
* --rb, --remote-branch The remote branch name, default is current branch name.
*
* @param Input $input
* @param Output $output
*
* @return int
* @throws Throwable
*/
protected function execute(Input $input, Output $output): int
{
$flags = $this->flags->getFlags();
$flags[] = '--np';

$upCmd = new UpdatePushCmd($input, $output);
$upCmd->run($flags);

return 0;
}
}
22 changes: 13 additions & 9 deletions app/Console/SubCmd/Gitflow/UpdatePushCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,29 @@ protected function configFlags(FlagsParser $fs): void
*/
protected function execute(Input $input, Output $output): mixed
{
$typGit = GitFactory::make();
$fs = $this->flags;
$gx = GitFactory::make();

$curBranch = $typGit->getCurBranch();
$upBranch = $this->flags->getOpt('remote-branch', $curBranch);
$curBranch = $gx->getCurBranch();
$upBranch = $fs->getOpt('remote-branch', $curBranch);
$output->info("Current Branch: $curBranch, fetch remote branch: $upBranch");

$runner = CmdRunner::new();
$runner->setDryRun($this->flags->getOpt('dry-run'));
$runner->setDryRun($fs->getOpt('dry-run'));
$runner->add('git pull');
$runner->addf('git pull %s %s', $typGit->getMainRemote(), $upBranch);
$runner->addf('git pull %s %s', $gx->getMainRemote(), $upBranch);

$defBranch = $typGit->getDefaultBranch();
$defBranch = $gx->getDefaultBranch();
if ($upBranch !== $defBranch) {
$runner->addf('git pull %s %s', $typGit->getMainRemote(), $defBranch);
$runner->addf('git pull %s %s', $gx->getMainRemote(), $defBranch);
}

$si = $typGit->getStatusInfo();
$si = $gx->getStatusInfo();

if (!$fs->getOpt('not-push')) {
$runner->addf('git push %s', $si->upRemote);
}

$runner->addf('git push %s', $si->upRemote);
$runner->run(true);

$output->success('Complete. datetime: ' . date('Y-m-d H:i:s'));
Expand Down

0 comments on commit 8266072

Please sign in to comment.