Skip to content

Commit 66d7f2c

Browse files
author
ityaozm@gmail.com
committed
feat: add ability to select commit type
1 parent eb99fbf commit 66d7f2c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

app/Commands/CommitCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ public function handle(): int
7373
throw new TaskException('There are no cached files to commit. Try running `git add` to cache some files.');
7474
}
7575

76+
$type = $this->choice('Please choice commit type', $types = $this->configManager->get('types'), array_key_first($types));
77+
7678
$message = retry(
7779
$this->option('retry-times'),
78-
function ($attempts) use ($cachedDiff): string {
80+
function ($attempts) use ($cachedDiff, $type): string {
7981
if ($attempts > 1) {
8082
$this->output->note('retrying...');
8183
}
8284

8385
$originalMessage = $this->generatorManager
8486
->driver($this->option('generator'))
85-
->generate($this->getPrompt($cachedDiff));
87+
->generate($this->getPrompt($cachedDiff, $type));
8688
$message = $this->tryFixMessage($originalMessage);
8789
if (! str($message)->jsonValidate()) {
8890
throw new TaskException(sprintf(
@@ -282,9 +284,17 @@ private function getDiffCommand(): array
282284
return array_merge(['git', 'diff', '--cached'], $this->option('diff-options'));
283285
}
284286

285-
private function getPrompt(string $cachedDiff): string
287+
private function getPrompt(string $cachedDiff, string $type): string
286288
{
289+
$typePrompt = sprintf($this->configManager->get('type_prompt'), $type);
290+
if (array_key_first($this->configManager->get('types')) === $type) {
291+
$type = $this->configManager->get('type_mark');
292+
$typePrompt = '';
293+
}
294+
287295
return (string) str($this->configManager->get("prompts.{$this->option('prompt')}"))
296+
->replace($this->configManager->get('type_mark'), $type)
297+
->replace($this->configManager->get('type_prompt_mark'), $typePrompt)
288298
->replace($this->configManager->get('diff_mark'), $cachedDiff)
289299
->when($this->option('verbose'), function (Stringable $prompt): Stringable {
290300
$this->output->note((string) $prompt);

config/ai-commit.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@
5555
// The type prompt.
5656
'type_prompt' => '- Use commit type `%s`.',
5757

58+
'types' => [
59+
'auto' => 'Automatically generate commit type',
60+
'feat' => 'A new feature',
61+
'fix' => 'A bug fix',
62+
'docs' => 'Documentation only changes',
63+
'style' => 'Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
64+
'refactor' => 'A code change that neither fixes a bug nor adds a feature',
65+
'perf' => 'A code change that improves performance',
66+
'test' => 'Adding missing tests or correcting existing tests',
67+
'build' => 'Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)',
68+
'ci' => 'Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)',
69+
'chore' => 'Other changes that don\'t modify src or test files',
70+
'revert' => 'Reverts a previous commit',
71+
],
72+
5873
// Enable or disable git commit `--no-edit` option.
5974
'no_edit' => false,
6075

0 commit comments

Comments
 (0)