Skip to content

Commit 75a2f49

Browse files
author
ityaozm@gmail.com
committed
refactor(CommitCommand): Simplify commit message generation flow
- Removed redundant task wrappers around message generation. - Consolidated the retry logic for commit message generation. - Improved readability and maintainability of the code by streamlining the implementation.
1 parent 89d7410 commit 75a2f49

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

app/Commands/CommitCommand.php

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -62,87 +62,87 @@ public function __construct(GeneratorManager $generatorManager)
6262
*/
6363
public function handle(): void
6464
{
65-
$this->task('1. Generating commit message', function () use (&$message): void {
66-
// Ensure git is installed and the current directory is a git repository.
67-
$this->createProcess(['git', 'rev-parse', '--is-inside-work-tree'])->mustRun();
65+
// Ensure git is installed and the current directory is a git repository.
66+
$this->createProcess(['git', 'rev-parse', '--is-inside-work-tree'])->mustRun();
6867

69-
$cachedDiff = $this->option('diff') ?: $this->createProcess($this->diffCommand())->mustRun()->getOutput();
70-
if ('' === $cachedDiff) {
71-
throw new TaskException('There are no cached files to commit. Try running `git add` to cache some files.');
72-
}
68+
$cachedDiff = $this->option('diff') ?: $this->createProcess($this->diffCommand())->mustRun()->getOutput();
69+
if ('' === $cachedDiff) {
70+
throw new TaskException('There are no cached files to commit. Try running `git add` to cache some files.');
71+
}
7372

74-
$type = $this->choice(
75-
'Please choice commit type',
76-
$types = $this->configManager->get('types'),
77-
array_key_first($types)
78-
);
73+
$type = $this->choice(
74+
'Please choice commit type',
75+
$types = $this->configManager->get('types'),
76+
array_key_first($types)
77+
);
78+
79+
$message = retry(
80+
$this->option('retry-times'),
81+
function ($attempts) use ($cachedDiff, $type): string {
82+
if ($attempts > 1) {
83+
$this->output->note('retrying...');
84+
}
85+
86+
$originalMessage = $this->generatorManager
87+
->driver($this->option('generator'))
88+
->generate($this->promptFor($cachedDiff, $type));
89+
$message = $this->tryFixMessage($originalMessage);
90+
if (! str($message)->jsonValidate()) {
91+
throw new TaskException(sprintf(
92+
'The generated commit message(%s) is an invalid JSON.',
93+
var_export($originalMessage, true)
94+
));
95+
}
96+
97+
return $message;
98+
},
99+
$this->option('retry-sleep'),
100+
$this->configManager->get('retry.when')
101+
);
102+
// $this->task('1. Generating commit message', function () use (&$message): void {
103+
// }, 'generating...'.PHP_EOL);
104+
105+
$message = collect(json_decode($message, true, 512, JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR))
106+
->map(static function ($content) {
107+
if (\is_array($content)) {
108+
return collect($content)
109+
->transform(static function (string $line): string {
110+
return (string) str($line)->trim(" \t\n\r\x0B")->start('- ');
111+
})
112+
->implode(PHP_EOL);
113+
}
114+
115+
return $content;
116+
})
117+
->tap(function (Collection $message): void {
118+
$message = $message->put('', '')->sortKeysUsing(static function (string $a, string $b): int {
119+
$rules = ['subject', '', 'body'];
79120

80-
$message = retry(
81-
$this->option('retry-times'),
82-
function ($attempts) use ($cachedDiff, $type): string {
83-
if ($attempts > 1) {
84-
$this->output->note('retrying...');
85-
}
86-
87-
$originalMessage = $this->generatorManager
88-
->driver($this->option('generator'))
89-
->generate($this->promptFor($cachedDiff, $type));
90-
$message = $this->tryFixMessage($originalMessage);
91-
if (! str($message)->jsonValidate()) {
92-
throw new TaskException(sprintf(
93-
'The generated commit message(%s) is an invalid JSON.',
94-
var_export($originalMessage, true)
95-
));
96-
}
97-
98-
return $message;
99-
},
100-
$this->option('retry-sleep'),
101-
$this->configManager->get('retry.when')
102-
);
103-
}, 'generating...'.PHP_EOL);
104-
105-
$this->task(PHP_EOL.'2. Confirming commit message', function () use (&$message): void {
106-
$message = collect(json_decode($message, true, 512, JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR))
107-
->map(static function ($content) {
108-
if (\is_array($content)) {
109-
return collect($content)
110-
->transform(static function (string $line): string {
111-
return (string) str($line)->trim(" \t\n\r\x0B")->start('- ');
112-
})
113-
->implode(PHP_EOL);
114-
}
115-
116-
return $content;
117-
})
118-
->tap(function (Collection $message): void {
119-
$message = $message->put('', '')->sortKeysUsing(static function (string $a, string $b): int {
120-
$rules = ['subject', '', 'body'];
121-
122-
return array_search($a, $rules, true) <=> array_search($b, $rules, true);
123-
});
124-
// $this->table($message->keys()->all(), [$message->all()]);
125-
$this->output->horizontalTable($message->keys()->all(), [$message->all()]);
126-
})
127-
->tap(function (): void {
128-
if (! $this->confirm('Do you want to commit this message?', true)) {
129-
$this->output->note('regenerating...');
130-
$this->handle();
131-
}
121+
return array_search($a, $rules, true) <=> array_search($b, $rules, true);
132122
});
133-
}, 'confirming...'.PHP_EOL);
123+
// $this->table($message->keys()->all(), [$message->all()]);
124+
$this->output->horizontalTable($message->keys()->all(), [$message->all()]);
125+
})
126+
->tap(function (): void {
127+
if (! $this->confirm('Do you want to commit this message?', true)) {
128+
$this->output->note('regenerating...');
129+
$this->handle();
130+
}
131+
});
132+
// $this->task(PHP_EOL.'2. Confirming commit message', function () use (&$message): void {
133+
// }, 'confirming...'.PHP_EOL);
134134

135-
$this->task(PHP_EOL.'3. Committing message', function () use ($message): void {
136-
if ($this->option('dry-run')) {
137-
$this->info($this->hydrateMessage($message));
135+
if ($this->option('dry-run')) {
136+
$this->info($this->hydrateMessage($message));
138137

139-
return;
140-
}
138+
return;
139+
}
141140

142-
tap($this->createProcess($this->commitCommandFor($message)), function (Process $process): void {
143-
$this->shouldEdit() and $process->setTty(true);
144-
})->setTimeout(null)->mustRun();
145-
}, 'committing...'.PHP_EOL);
141+
tap($this->createProcess($this->commitCommandFor($message)), function (Process $process): void {
142+
$this->shouldEdit() and $process->setTty(true);
143+
})->setTimeout(null)->mustRun();
144+
// $this->task(PHP_EOL.'3. Committing message', function (): void {
145+
// }, 'committing...'.PHP_EOL);
146146

147147
$this->output->success('Successfully generated and committed message.');
148148
}

0 commit comments

Comments
 (0)