feat(commit.ts) : Suggestion for the user to help how to choose where to push code on remotes (#20)

* 🔨 refactor(commit.ts): rename function to getGitRemotes and improve comments
This commit renames the function to getGitRemotes to better reflect its purpose. Additionally, the comments have been improved to better explain the function's behavior.

🚀 chore(commit.ts): add support for pushing to selected remote
This commit adds support for pushing to the selected remote. The user is prompted to choose a remote from the list of available remotes, and the selected remote is used to push the changes. The push command is run with the selected remote as an argument.

🐛 fix(commit.ts): fix formatting and add missing newline at end of file
This commit fixes the formatting of the code and adds a missing newline at the end of the file. The changes do not affect the functionality of the code.

* 🐛 fix(commit.ts): fix push command to include remote name
 feat(commit.ts): add support for multiple remotes
The push command was not including the remote name, which caused the push to fail. This has been fixed. Support for multiple remotes has been added, allowing the user to choose which remote to push to. If there is only one remote, the push command will automatically use that remote.
This commit is contained in:
jessicakuijer
2023-03-21 08:14:13 +01:00
committed by GitHub
parent a6ccdb5f77
commit ff81d7e1ca
+28 -20
View File
@@ -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)
);