Skip to content

Commit

Permalink
fix(CommitCommand): use --cached instead of --staged in git diff
Browse files Browse the repository at this point in the history
- Update the getDiffCommand method to use --cached instead of --staged
- Adjust the getPrompt method to use cachedDiff instead of stagedDiff
  • Loading branch information
guanguans committed Jun 22, 2023
1 parent 4e472a6 commit ecdacfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions app/Commands/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ public function handle(): int
throw new TaskException(trim($process->getErrorOutput()));
}

$stagedDiff = $this->createProcess($this->getDiffCommand())->mustRun()->getOutput();
if (str($stagedDiff)->isEmpty()) {
throw new TaskException('There are no staged files to commit. Try running `git add` to stage some files.');
$cachedDiff = $this->createProcess($this->getDiffCommand())->mustRun()->getOutput();
if (str($cachedDiff)->isEmpty()) {
throw new TaskException('There are no cached files to commit. Try running `git add` to stage some files.');
}

$this->newLine();
$messages = retry(
$this->option('retry-times'),
function ($attempts) use ($stagedDiff): string {
function ($attempts) use ($cachedDiff): string {
if ($attempts > 1) {
$this->output->info('retrying...');
}

$originalMessages = $this->generatorManager->driver($this->option('generator'))->generate($this->getPrompt($stagedDiff));
$originalMessages = $this->generatorManager->driver($this->option('generator'))->generate($this->getPrompt($cachedDiff));
$messages = $this->tryFixMessages($originalMessages);
if (! str($messages)->isJson()) {
throw new TaskException(sprintf('The generated commit messages(%s) is an invalid JSON.', var_export($originalMessages, true)));
Expand Down Expand Up @@ -220,13 +220,13 @@ protected function createProcess($command, ?string $cwd = null, ?array $env = nu

protected function getDiffCommand(): array
{
return array_merge(['git', 'diff', '--staged'], $this->option('diff-options'));
return array_merge(['git', 'diff', '--cached'], $this->option('diff-options'));
}

protected function getPrompt(string $stagedDiff): string
protected function getPrompt(string $cachedDiff): string
{
return (string) str($this->configManager->get("prompts.{$this->option('prompt')}"))
->replace($this->configManager->get('diff_mark'), $stagedDiff)
->replace($this->configManager->get('diff_mark'), $cachedDiff)
->when($this->option('verbose'), function (Stringable $prompt): Stringable {
$this->output->info((string) $prompt);

Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/CommitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
->group(__DIR__, __FILE__)
->throws(TaskException::class, 'fatal: ');

it('will throw TaskException(no staged files to commit)', function (): void {
it('will throw TaskException(no cached files to commit)', function (): void {
// 重置暂存区
Process::fromShellCommandline('git reset', repository_path())->mustRun();

Expand All @@ -39,7 +39,7 @@
})
->depends('it will throw TaskException(not a git repository)')
->group(__DIR__, __FILE__)
->throws(TaskException::class, 'There are no staged files to commit. Try running `git add` to stage some files.');
->throws(TaskException::class, 'There are no cached files to commit. Try running `git add` to stage some files.');

it('will throw TaskException(The generated commit messages is an invalid JSON)', function (): void {
// 添加文件到暂存区
Expand Down Expand Up @@ -74,7 +74,7 @@
'--generator' => 'openai',
]);
})
->depends('it will throw TaskException(no staged files to commit)')
->depends('it will throw TaskException(no cached files to commit)')
->group(__DIR__, __FILE__)
->throws(TaskException::class, 'The generated commit messages(');

Expand Down

0 comments on commit ecdacfa

Please sign in to comment.