Skip to content

Commit

Permalink
feat: add new git op command and some consts
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 28, 2022
1 parent e327ea5 commit ae5f053
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/Common/GitLocal/GitConst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Common\GitLocal;

/**
* class GitConst
*
* @author inhere
* @date 2022/10/28
*/
class GitConst
{
public const GITHUB_HOST = 'https://github.com';
public const GITHUB_GIT = 'git@github.com';

/**
* remote url host type
*/
public const HOST_TYPE_GH = 'github';
public const HOST_TYPE_GL = 'gitlab';
public const HOST_TYPE_LOC = 'gitloc';

public const ALLOW_HOST_TYPES = [
self::HOST_TYPE_GH,
self::HOST_TYPE_GL,
];
}
72 changes: 72 additions & 0 deletions app/Console/SubCmd/Gitflow/UpdatePushCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\Common\GitLocal\GitFactory;
use Toolkit\PFlag\FlagsParser;
use function date;

/**
* Class UpdatePushCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class UpdatePushCmd extends Command
{
protected static string $name = 'up-push';
protected static string $desc = 'quick delete git branches from local, origin, main remote';

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

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
* --np, --not-push bool;Not push to remote repo after updated.
* --rb, --remote-branch The remote branch name, default is current branch name.
*
* @param Input $input
* @param Output $output
*
* @return mixed
*/
protected function execute(Input $input, Output $output): mixed
{
$typGit = GitFactory::make();

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

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

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

$si = $typGit->getStatusInfo();

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

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

0 comments on commit ae5f053

Please sign in to comment.