|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the guanguans/ai-commit. |
| 7 | + * |
| 8 | + * (c) guanguans <[email protected]> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Commands; |
| 14 | + |
| 15 | +use App\Contracts\GeneratorContract; |
| 16 | +use App\Contracts\OutputAwareContract; |
| 17 | +use App\Exceptions\TaskException; |
| 18 | +use App\GeneratorManager; |
| 19 | +use Illuminate\Console\Scheduling\Schedule; |
| 20 | +use Illuminate\Support\Collection; |
| 21 | +use Illuminate\Support\Stringable; |
| 22 | +use LaravelZero\Framework\Commands\Command; |
| 23 | +use Symfony\Component\Console\Input\InputInterface; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | +use Symfony\Component\Process\Process; |
| 26 | + |
| 27 | +class CommitCommand extends Command |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $signature = /** @lang text */ |
| 33 | + ' |
| 34 | + commit |
| 35 | + {--commit-options=* : Append options for the `git commit` command <comment>[default: "--edit"]</comment>} |
| 36 | + {--diff-options=* : Append options for the `git diff` command <comment>[default: ":!*.lock"]</comment>} |
| 37 | + {--generator=openai : Specify generator} |
| 38 | + {--num=3 : Specify number of generated messages} |
| 39 | + {--template= : Specify template of messages generated} |
| 40 | + '; |
| 41 | + |
| 42 | + protected $description = 'Automagically generate commit messages with AI.'; |
| 43 | + |
| 44 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + public function handle() |
| 49 | + { |
| 50 | + $this->task('1. Checking environment', function () use (&$stagedDiff) { |
| 51 | + $isInsideWorkTree = Process::fromShellCommandline('git rev-parse --is-inside-work-tree') |
| 52 | + ->mustRun() |
| 53 | + ->getOutput(); |
| 54 | + if (! \str($isInsideWorkTree)->rtrim()->is('true')) { |
| 55 | + $message = <<<'message' |
| 56 | +It looks like you are not in a git repository. |
| 57 | +Please run this command from the root of a git repository, or initialize one using `git init`. |
| 58 | +message; |
| 59 | + |
| 60 | + throw new TaskException($message); |
| 61 | + } |
| 62 | + |
| 63 | + $stagedDiff = (new Process($this->getDiffCommand()))->mustRun()->getOutput(); |
| 64 | + if (empty($stagedDiff)) { |
| 65 | + throw new TaskException('There are no staged files to commit. Try running `git add` to stage some files.'); |
| 66 | + } |
| 67 | + }, 'checking...'); |
| 68 | + |
| 69 | + $this->task('2. Generating commit messages', function () use (&$commitMessages, $stagedDiff) { |
| 70 | + $commitMessages = $this->getGenerator()->generate($this->getPromptOfAI($stagedDiff)); |
| 71 | + if (\str($commitMessages)->isEmpty()) { |
| 72 | + throw new TaskException('No commit messages generated.'); |
| 73 | + } |
| 74 | + |
| 75 | + if (! is_json($commitMessages)) { |
| 76 | + throw new TaskException('The generated commit messages is an invalid JSON.'); |
| 77 | + } |
| 78 | + |
| 79 | + $this->line(''); |
| 80 | + $this->line(''); |
| 81 | + }, 'generating...'); |
| 82 | + |
| 83 | + $this->task('3. Choosing commit message', function () use ($commitMessages, &$commitMessage) { |
| 84 | + $commitMessages = collect(json_decode($commitMessages, true)); |
| 85 | + |
| 86 | + $chosenSubject = $this->choice('Please choice a commit message', $commitMessages->pluck('subject', 'id')->all()); |
| 87 | + |
| 88 | + $commitMessage = $commitMessages->first(function ($commitMessage) use ($chosenSubject) { |
| 89 | + return $commitMessage['subject'] === $chosenSubject; |
| 90 | + }); |
| 91 | + }, 'choosing...'); |
| 92 | + |
| 93 | + $this->task('4. Committing message', function () use ($commitMessage) { |
| 94 | + (new Process($this->getCommitCommand($commitMessage))) |
| 95 | + ->setTty(true) |
| 96 | + ->setTimeout(null) |
| 97 | + ->mustRun(); |
| 98 | + }, 'committing...'); |
| 99 | + |
| 100 | + return self::SUCCESS; |
| 101 | + } |
| 102 | + |
| 103 | + protected function getDiffCommand(): array |
| 104 | + { |
| 105 | + return array_merge( |
| 106 | + ['git', 'diff', '--staged'], |
| 107 | + $this->option('diff-options') ?: $this->laravel->get('config')->get('ai-commit.diff-options') |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + protected function getGenerator(): GeneratorContract |
| 112 | + { |
| 113 | + $generator = $this->laravel->get(GeneratorManager::class)->driver( |
| 114 | + $this->option('generator') ?: $this->laravel->get('config')->get('ai-commit.generator') |
| 115 | + ); |
| 116 | + |
| 117 | + return tap($generator, function (GeneratorContract $generator) { |
| 118 | + $generator instanceof OutputAwareContract and $generator->setOutput($this->output); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + protected function getCommitCommand(array $commitMessage): array |
| 123 | + { |
| 124 | + return collect($commitMessage) |
| 125 | + ->filter(function ($val) { |
| 126 | + return $val && is_string($val); |
| 127 | + }) |
| 128 | + ->map(function (string $val) { |
| 129 | + return trim($val, " \t\n\r\x0B"); |
| 130 | + }) |
| 131 | + ->pipe(function (Collection $collection): array { |
| 132 | + return array_merge( |
| 133 | + ['git', 'commit', '--message', $collection->implode(str_repeat(PHP_EOL, 2))], |
| 134 | + $this->option('commit-options') ?: $this->laravel->get('config')->get('ai-commit.commit-options') |
| 135 | + ); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + protected function getPromptOfAI(string $stagedDiff): string |
| 140 | + { |
| 141 | + return \str($this->option('template') ?: $this->laravel->get('config')->get('ai-commit.template')) |
| 142 | + ->replace( |
| 143 | + ['{{diff}}', '{{num}}'], |
| 144 | + [$stagedDiff, $this->option('num') ?: $this->laravel->get('config')->get('ai-commit.num')] |
| 145 | + ) |
| 146 | + ->when($this->option('verbose'), function (Stringable $diff) { |
| 147 | + $this->line(''); |
| 148 | + $this->line('==================================================='); |
| 149 | + $this->line($diff); |
| 150 | + $this->output->write('==================================================='); |
| 151 | + }) |
| 152 | + ->__toString(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Define the command's schedule. |
| 157 | + * |
| 158 | + * @return void |
| 159 | + */ |
| 160 | + public function schedule(Schedule $schedule) |
| 161 | + { |
| 162 | + // $schedule->command(static::class)->everyMinute(); |
| 163 | + } |
| 164 | +} |
0 commit comments