mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
feat: Add support for extra args to be passed to the git commit command (#17)
* feat: Add support for extra args to be passed to the git commit command
This commit is contained in:
14
README.md
14
README.md
@@ -84,6 +84,20 @@ To remove description:
|
||||
oc config set description=false
|
||||
```
|
||||
|
||||
### Git flags
|
||||
|
||||
The `opencommit` or `oc` commands can be used in place of the `git commit -m "${generatedMessage}"` command. This means that any regular flags that are used with the `git commit` command will also be applied when using `opencommit` or `oc`.
|
||||
|
||||
```sh
|
||||
oc --no-verify
|
||||
```
|
||||
|
||||
is translated to :
|
||||
|
||||
```sh
|
||||
git commit -m "${generatedMessage}" --no-verify
|
||||
```
|
||||
|
||||
## Git hook
|
||||
|
||||
You can set OpenCommit as Git [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. Hook integrates with you IDE Source Control and allows you edit the message before commit.
|
||||
|
||||
@@ -8,7 +8,7 @@ import { hookCommand, isHookCalled } from './commands/githook.js';
|
||||
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
|
||||
import { commit } from './commands/commit';
|
||||
|
||||
const rawArgv = process.argv.slice(2);
|
||||
const extraArgs = process.argv.slice(2);
|
||||
|
||||
cli(
|
||||
{
|
||||
@@ -23,8 +23,8 @@ cli(
|
||||
if (isHookCalled) {
|
||||
prepareCommitMessageHook();
|
||||
} else {
|
||||
commit();
|
||||
commit(extraArgs);
|
||||
}
|
||||
},
|
||||
rawArgv
|
||||
extraArgs
|
||||
);
|
||||
|
||||
@@ -22,7 +22,8 @@ import chalk from 'chalk';
|
||||
import { trytm } from '../utils/trytm';
|
||||
|
||||
const generateCommitMessageFromGitDiff = async (
|
||||
diff: string
|
||||
diff: string,
|
||||
extraArgs: string[]
|
||||
): Promise<void> => {
|
||||
await assertGitRepo();
|
||||
|
||||
@@ -59,7 +60,7 @@ ${chalk.grey('——————————————————')}`
|
||||
});
|
||||
|
||||
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
|
||||
const { stdout } = await execa('git', ['commit', '-m', commitMessage]);
|
||||
const { stdout } = await execa('git', ['commit', '-m', commitMessage, ...extraArgs]);
|
||||
|
||||
outro(`${chalk.green('✔')} successfully committed`);
|
||||
|
||||
@@ -74,6 +75,7 @@ ${chalk.grey('——————————————————')}`
|
||||
|
||||
pushSpinner.start('Running `git push`');
|
||||
const { stdout } = await execa('git', ['push']);
|
||||
|
||||
pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits`);
|
||||
|
||||
if (stdout) outro(stdout);
|
||||
@@ -81,9 +83,11 @@ ${chalk.grey('——————————————————')}`
|
||||
} else outro(`${chalk.gray('✖')} process cancelled`);
|
||||
};
|
||||
|
||||
export async function commit(isStageAllFlag = false) {
|
||||
|
||||
export async function commit(extraArgs=[], isStageAllFlag = false) {
|
||||
if (isStageAllFlag) {
|
||||
const changedFiles = await getChangedFiles();
|
||||
|
||||
if (changedFiles) await gitAdd({ files: changedFiles });
|
||||
else {
|
||||
outro('No changes detected, write some code and run `oc` again');
|
||||
@@ -106,6 +110,7 @@ export async function commit(isStageAllFlag = false) {
|
||||
}
|
||||
|
||||
const stagedFilesSpinner = spinner();
|
||||
|
||||
stagedFilesSpinner.start('Counting staged files');
|
||||
|
||||
if (!stagedFiles.length) {
|
||||
@@ -118,7 +123,8 @@ export async function commit(isStageAllFlag = false) {
|
||||
isStageAllAndCommitConfirmedByUser &&
|
||||
!isCancel(isStageAllAndCommitConfirmedByUser)
|
||||
) {
|
||||
await commit(true);
|
||||
|
||||
await commit(extraArgs, true);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -136,7 +142,7 @@ export async function commit(isStageAllFlag = false) {
|
||||
await gitAdd({ files });
|
||||
}
|
||||
|
||||
await commit(false);
|
||||
await commit(extraArgs, false);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -147,7 +153,7 @@ export async function commit(isStageAllFlag = false) {
|
||||
);
|
||||
|
||||
const [, generateCommitError] = await trytm(
|
||||
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }))
|
||||
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }), extraArgs)
|
||||
);
|
||||
|
||||
if (generateCommitError) {
|
||||
|
||||
Reference in New Issue
Block a user