diff --git a/src/commands/commit.ts b/src/commands/commit.ts index a6ccf5b..ae144a7 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -3,24 +3,17 @@ import { GenerateCommitMessageErrorEnum, generateCommitMessageWithChatCompletion } from '../generateCommitMessageFromGitDiff'; -import { - assertGitRepo, - getChangedFiles, - getDiff, - getStagedFiles, - gitAdd -} from '../utils/git'; -import { - spinner, - confirm, - outro, - isCancel, - intro, - multiselect -} from '@clack/prompts'; +import { assertGitRepo, getStagedGitDiff, getDiff, getStagedFiles, gitAdd } from '../utils/git'; +import { spinner, confirm, outro, isCancel, intro, multiselect, select } from '@clack/prompts'; import chalk from 'chalk'; import { trytm } from '../utils/trytm'; +// Adding a function to get the list of remotes +const getGitRemotes = async () => { + const { stdout } = await execa('git', ['remote']); + return stdout.split('\n').filter((remote) => remote.trim() !== ''); +}; + const generateCommitMessageFromGitDiff = async ( diff: string, extraArgs: string[] @@ -65,18 +58,31 @@ ${chalk.grey('——————————————————')}` outro(`${chalk.green('✔')} successfully committed`); outro(stdout); - + const remotes = await getGitRemotes(); + + if (remotes.length === 1) { const isPushConfirmedByUser = await confirm({ message: 'Do you want to run `git push`?' }); if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) { const pushSpinner = spinner(); + pushSpinner.start(`Running \`git push ${remotes[0]}\``); + const { stdout } = await execa('git', ['push', remotes[0]]); + pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits to ${remotes[0]}`); + if (stdout) outro(stdout); + } else { + const selectedRemote = await select({ + message: 'Choose a remote to push to', + choices: remotes.map((remote) => ({ title: remote, value: remote })), + }); + + if (!isCancel(selectedRemote)) { + const pushSpinner = spinner(); + pushSpinner.start(`Running \`git push ${selectedRemote}\``); + const { stdout } = await execa('git', ['push', selectedRemote]); + pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits to ${selectedRemote}`); - pushSpinner.start('Running `git push`'); - const { stdout } = await execa('git', ['push']); - - pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits`); if (stdout) outro(stdout); } @@ -152,6 +158,8 @@ export async function commit(extraArgs=[], isStageAllFlag = false) { .join('\n')}` ); + await generateCommitMessageFromGitDiff(staged.diff); +} const [, generateCommitError] = await trytm( generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }), extraArgs) );