Skip to content

Commit

Permalink
up: gitx - migrate the git tag list command to a command class.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2022
1 parent 14f0eb1 commit 06be919
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 29 deletions.
39 changes: 10 additions & 29 deletions app/Console/Controller/Gitx/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Manager\GitBranchManager;
use Inhere\Kite\Console\SubCmd\BranchCmd;
use Inhere\Kite\Console\SubCmd\GitTagCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use Inhere\Kite\Kite;
Expand Down Expand Up @@ -97,7 +98,6 @@ protected static function commandAliases(): array
'tp',
'tag-push',
],
'tagList' => ['tag', 'tags', 'tl', 'taglist'],
'tagInfo' => ['tag-info', 'ti', 'tag-show'],
];
}
Expand All @@ -109,6 +109,7 @@ protected function subCommands(): array
{
return [
BranchCmd::class,
GitTagCmd::class,
];
}

Expand Down Expand Up @@ -427,29 +428,6 @@ public function tagFindCommand(FlagsParser $fs, Input $input, Output $output): v
$output->printf($title, $tagName);
}

/**
* list all git tags for the project
*
* @arguments
* keywords Filter by input keywords
*
* @param FlagsParser $fs
* @param Output $output
*/
public function tagListCommand(FlagsParser $fs, Output $output): void
{
// git tag --sort=-creatordate 倒序排列
$cmd = 'git tag -l -n2';
$kw = $fs->getArg(0);
if ($kw) {
$cmd .= " | grep $kw";
}

CmdRunner::new($cmd)->do(true);

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

/**
* display git tag information by `git show TAG`
*
Expand Down Expand Up @@ -477,6 +455,7 @@ public function tagInfoCommand(FlagsParser $fs, Output $output): void
* @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.
*
Expand Down Expand Up @@ -510,14 +489,18 @@ public function tagNewCommand(FlagsParser $fs, Input $input, Output $output): vo
return;
}

$hashId = $fs->getOpt('hash');
$dryRun = $this->flags->getOpt('dry-run');

// $remotes = Git::new($dir)->remote->getList();
if ($tag[0] !== 'v' && !$fs->getOpt('no-auto-add-v')) {
$tag = 'v' . $tag;
}

$info = [
'Work Dir' => $dir,
// 'Origin' => $remotes,
'Hash ID' => $hashId,
'Dry Run' => $dryRun,
'New Tag' => $tag,
];

Expand All @@ -527,9 +510,9 @@ public function tagNewCommand(FlagsParser $fs, Input $input, Output $output): vo

$msg = $fs->getOpt('message');
$msg = $msg ?: "Release $tag";

// add message
$info['Message'] = $msg;

$output->aList($info, 'Information', ['ucFirst' => false]);

if (
Expand All @@ -541,14 +524,12 @@ public function tagNewCommand(FlagsParser $fs, Input $input, Output $output): vo
return;
}

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

// git tag -a $1 -m "Release $1"
// git push origin --tags
// $cmd = sprintf('git tag -a %s -m "%s" && git push origin %s', $tag, $msg, $tag);
$run = CmdRunner::new();
$run->setDryRun($dryRun);
$run->addf('git tag -a %s -m "%s"', $tag, $msg);
$run->addf('git tag -a %s -m "%s" %s', $tag, $msg, $hashId);
$run->addf('git push origin %s', $tag);
$run->runAndPrint();

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

namespace Inhere\Kite\Console\SubCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\GitxCmd\GitTagListCmd;
use Throwable;

/**
* class GitTagCmd
*
* @author inhere
* @date 2022/7/12
*/
class GitTagCmd extends Command
{
protected static string $name = 'tag';
protected static string $desc = 'git tag manage tool command. eg: list, add, del';

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

/**
* @param Input $input
* @param Output $output
*
* @throws Throwable
*/
protected function execute(Input $input, Output $output)
{
// default run
$bcCmd = new GitTagListCmd($input, $output);
$bcCmd->run($this->flags->getFlags());
}
}
41 changes: 41 additions & 0 deletions app/Console/SubCmd/GitxCmd/GitTagListCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\CmdRunner;

/**
* class GitTagListCmd
*
* @author inhere
* @date 2022/7/12
*/
class GitTagListCmd extends Command
{
protected static string $name = 'list';
protected static string $desc = 'list git tags for project';

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

protected function execute(Input $input, Output $output)
{
$fs = $this->flags;

// git tag --sort=-creatordate 倒序排列
$cmd = 'git tag -l -n2';
$kw = $fs->getArg(0);
if ($kw) {
$cmd .= " | grep $kw";
}

CmdRunner::new($cmd)->do(true);

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

0 comments on commit 06be919

Please sign in to comment.