Skip to content

Commit 5f63c68

Browse files
committed
feat(CommitCommand): Add InputArgument for path
- Add InputArgument for path to CommitCommand - CreateProcess method to handle command and cwd - Update configure method to include InputArgument
1 parent e39ba85 commit 5f63c68

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

app/Commands/CommitCommand.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Support\Collection;
2020
use Illuminate\Support\Stringable;
2121
use LaravelZero\Framework\Commands\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
2223
use Symfony\Component\Console\Input\InputOption;
2324
use Symfony\Component\Process\Process;
2425

@@ -53,6 +54,7 @@ public function __construct()
5354
protected function configure()
5455
{
5556
$this->setDefinition([
57+
new InputArgument('path', InputArgument::OPTIONAL, 'The working directory', $this->configManager::localPath('')),
5658
new InputOption('commit-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git commit` command', $this->configManager->get('commit_options')),
5759
new InputOption('diff-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git diff` command', $this->configManager->get('diff_options')),
5860
new InputOption('generator', 'g', InputOption::VALUE_REQUIRED, 'Specify generator name', $this->configManager->get('generator')),
@@ -64,8 +66,8 @@ protected function configure()
6466

6567
public function handle()
6668
{
67-
$this->task('1. Checking run environment', function () use (&$stagedDiff) {
68-
$isInsideWorkTree = Process::fromShellCommandline('git rev-parse --is-inside-work-tree')
69+
$this->task(' Checking run environment', function () use (&$stagedDiff) {
70+
$isInsideWorkTree = $this->createProcess('git rev-parse --is-inside-work-tree')
6971
->mustRun()
7072
->getOutput();
7173
if (! \str($isInsideWorkTree)->rtrim()->is('true')) {
@@ -77,13 +79,13 @@ public function handle()
7779
throw new TaskException($message);
7880
}
7981

80-
$stagedDiff = (new Process($this->getDiffCommand()))->mustRun()->getOutput();
82+
$stagedDiff = $this->createProcess($this->getDiffCommand())->mustRun()->getOutput();
8183
if (empty($stagedDiff)) {
8284
throw new TaskException('There are no staged files to commit. Try running `git add` to stage some files.');
8385
}
8486
}, 'checking...');
8587

86-
$this->task('2. Generating commit messages', function () use (&$messages, $stagedDiff) {
88+
$this->task(' Generating commit messages', function () use (&$messages, $stagedDiff) {
8789
$generator = $this->laravel->get(GeneratorManager::class)->driver($this->option('generator'));
8890
$messages = $generator->generate($this->getPromptOfAI($stagedDiff));
8991
if (\str($messages)->isEmpty()) {
@@ -99,16 +101,16 @@ public function handle()
99101
$this->line('');
100102
}, 'generating...');
101103

102-
$this->task('3. Choosing commit message', function () use ($messages, &$message) {
104+
$this->task(' Choosing commit message', function () use ($messages, &$message) {
103105
$messages = collect(json_decode($messages, true));
104106
$chosenSubject = $this->choice('Please choice a commit message', $messages->pluck('subject', 'id')->all());
105107
$message = $messages->first(function ($message) use ($chosenSubject) {
106108
return $message['subject'] === $chosenSubject;
107109
});
108110
}, 'choosing...');
109111

110-
$this->task('4. Committing message', function () use ($message) {
111-
(new Process($this->getCommitCommand($message)))
112+
$this->task(' Committing message', function () use ($message) {
113+
$this->createProcess($this->getCommitCommand($message))
112114
->setTty(true)
113115
->setTimeout(null)
114116
->mustRun();
@@ -117,6 +119,20 @@ public function handle()
117119
return self::SUCCESS;
118120
}
119121

122+
/**
123+
* @param string|array $command
124+
*/
125+
protected function createProcess($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60): Process
126+
{
127+
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
128+
null === $cwd and $cwd = $this->argument('path');
129+
if (is_string($command)) {
130+
return Process::fromShellCommandline($command, $cwd, $env, $input, $timeout);
131+
}
132+
133+
return new Process($command, $cwd, $env, $input, $timeout);
134+
}
135+
120136
protected function getDiffCommand(): array
121137
{
122138
return array_merge(['git', 'diff', '--staged'], $this->option('diff-options'));
@@ -176,22 +192,22 @@ protected function getCommitCommand(array $message): array
176192
->map(function (string $val) {
177193
return trim($val, " \t\n\r\x0B");
178194
})
179-
->pipe(function (Collection $collection): array {
195+
->pipe(function (Collection $message): array {
180196
$options = collect($this->option('commit-options'))
181197
->push('--edit')
182-
->pipe(function (Collection $collection): Collection {
183-
$noEdit = $this->option('no-edit') ?: $this->configManager->get('no_edit');
198+
->pipe(function (Collection $options): Collection {
199+
$noEdit = $this->option('no-edit') ?: ! $this->configManager->get('edit');
184200
if ($noEdit) {
185-
return $collection->filter(function (string $option): bool {
201+
return $options->filter(function (string $option): bool {
186202
return '--edit' !== $option;
187203
});
188204
}
189205

190-
return $collection;
206+
return $options;
191207
})
192208
->all();
193209

194-
return array_merge(['git', 'commit', '--message', $collection->implode(str_repeat(PHP_EOL, 2))], $options);
210+
return array_merge(['git', 'commit', '--message', $message->implode(str_repeat(PHP_EOL, 2))], $options);
195211
});
196212
}
197213

config/ai-commit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
],
2929

3030
/**
31-
* Force no edit mode.
31+
* Force edit mode.
3232
*/
33-
'no_edit' => false,
33+
'edit' => true,
3434

3535
/**
3636
* The prompt name.

0 commit comments

Comments
 (0)