From 66d7f2ce2519e97ac2779c6c999d771b06274b2f Mon Sep 17 00:00:00 2001 From: "ityaozm@gmail.com" Date: Tue, 11 Jun 2024 18:18:29 +0800 Subject: [PATCH] feat: add ability to select commit type --- app/Commands/CommitCommand.php | 16 +++++++++++++--- config/ai-commit.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/Commands/CommitCommand.php b/app/Commands/CommitCommand.php index 45eebde..d41aeba 100644 --- a/app/Commands/CommitCommand.php +++ b/app/Commands/CommitCommand.php @@ -73,16 +73,18 @@ public function handle(): int throw new TaskException('There are no cached files to commit. Try running `git add` to cache some files.'); } + $type = $this->choice('Please choice commit type', $types = $this->configManager->get('types'), array_key_first($types)); + $message = retry( $this->option('retry-times'), - function ($attempts) use ($cachedDiff): string { + function ($attempts) use ($cachedDiff, $type): string { if ($attempts > 1) { $this->output->note('retrying...'); } $originalMessage = $this->generatorManager ->driver($this->option('generator')) - ->generate($this->getPrompt($cachedDiff)); + ->generate($this->getPrompt($cachedDiff, $type)); $message = $this->tryFixMessage($originalMessage); if (! str($message)->jsonValidate()) { throw new TaskException(sprintf( @@ -282,9 +284,17 @@ private function getDiffCommand(): array return array_merge(['git', 'diff', '--cached'], $this->option('diff-options')); } - private function getPrompt(string $cachedDiff): string + private function getPrompt(string $cachedDiff, string $type): string { + $typePrompt = sprintf($this->configManager->get('type_prompt'), $type); + if (array_key_first($this->configManager->get('types')) === $type) { + $type = $this->configManager->get('type_mark'); + $typePrompt = ''; + } + return (string) str($this->configManager->get("prompts.{$this->option('prompt')}")) + ->replace($this->configManager->get('type_mark'), $type) + ->replace($this->configManager->get('type_prompt_mark'), $typePrompt) ->replace($this->configManager->get('diff_mark'), $cachedDiff) ->when($this->option('verbose'), function (Stringable $prompt): Stringable { $this->output->note((string) $prompt); diff --git a/config/ai-commit.php b/config/ai-commit.php index 7a9fcd8..e532e58 100644 --- a/config/ai-commit.php +++ b/config/ai-commit.php @@ -55,6 +55,21 @@ // The type prompt. 'type_prompt' => '- Use commit type `%s`.', + 'types' => [ + 'auto' => 'Automatically generate commit type', + 'feat' => 'A new feature', + 'fix' => 'A bug fix', + 'docs' => 'Documentation only changes', + 'style' => 'Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)', + 'refactor' => 'A code change that neither fixes a bug nor adds a feature', + 'perf' => 'A code change that improves performance', + 'test' => 'Adding missing tests or correcting existing tests', + 'build' => 'Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)', + 'ci' => 'Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)', + 'chore' => 'Other changes that don\'t modify src or test files', + 'revert' => 'Reverts a previous commit', + ], + // Enable or disable git commit `--no-edit` option. 'no_edit' => false,