Skip to content

Commit d793bf1

Browse files
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
1 parent b0d27e6 commit d793bf1

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ To remove description:
8484
oc config set description=false
8585
```
8686

87+
### Git flags
88+
89+
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`.
90+
91+
```sh
92+
oc --no-verify
93+
```
94+
95+
is translated to :
96+
97+
```sh
98+
git commit -m "${generatedMessage}" --no-verify
99+
```
100+
87101
## Git hook
88102

89103
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.

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { hookCommand, isHookCalled } from './commands/githook.js';
88
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
99
import { commit } from './commands/commit';
1010

11-
const rawArgv = process.argv.slice(2);
11+
const extraArgs = process.argv.slice(2);
1212

1313
cli(
1414
{
@@ -23,8 +23,8 @@ cli(
2323
if (isHookCalled) {
2424
prepareCommitMessageHook();
2525
} else {
26-
commit();
26+
commit(extraArgs);
2727
}
2828
},
29-
rawArgv
29+
extraArgs
3030
);

src/commands/commit.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import chalk from 'chalk';
2222
import { trytm } from '../utils/trytm';
2323

2424
const generateCommitMessageFromGitDiff = async (
25-
diff: string
25+
diff: string,
26+
extraArgs: string[]
2627
): Promise<void> => {
2728
await assertGitRepo();
2829

@@ -59,7 +60,7 @@ ${chalk.grey('——————————————————')}`
5960
});
6061

6162
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
62-
const { stdout } = await execa('git', ['commit', '-m', commitMessage]);
63+
const { stdout } = await execa('git', ['commit', '-m', commitMessage, ...extraArgs]);
6364

6465
outro(`${chalk.green('✔')} successfully committed`);
6566

@@ -74,16 +75,19 @@ ${chalk.grey('——————————————————')}`
7475

7576
pushSpinner.start('Running `git push`');
7677
const { stdout } = await execa('git', ['push']);
78+
7779
pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits`);
7880

7981
if (stdout) outro(stdout);
8082
}
8183
} else outro(`${chalk.gray('✖')} process cancelled`);
8284
};
8385

84-
export async function commit(isStageAllFlag = false) {
86+
87+
export async function commit(extraArgs=[], isStageAllFlag = false) {
8588
if (isStageAllFlag) {
8689
const changedFiles = await getChangedFiles();
90+
8791
if (changedFiles) await gitAdd({ files: changedFiles });
8892
else {
8993
outro('No changes detected, write some code and run `oc` again');
@@ -106,6 +110,7 @@ export async function commit(isStageAllFlag = false) {
106110
}
107111

108112
const stagedFilesSpinner = spinner();
113+
109114
stagedFilesSpinner.start('Counting staged files');
110115

111116
if (!stagedFiles.length) {
@@ -118,7 +123,8 @@ export async function commit(isStageAllFlag = false) {
118123
isStageAllAndCommitConfirmedByUser &&
119124
!isCancel(isStageAllAndCommitConfirmedByUser)
120125
) {
121-
await commit(true);
126+
127+
await commit(extraArgs, true);
122128
process.exit(1);
123129
}
124130

@@ -136,7 +142,7 @@ export async function commit(isStageAllFlag = false) {
136142
await gitAdd({ files });
137143
}
138144

139-
await commit(false);
145+
await commit(extraArgs, false);
140146
process.exit(1);
141147
}
142148

@@ -147,7 +153,7 @@ export async function commit(isStageAllFlag = false) {
147153
);
148154

149155
const [, generateCommitError] = await trytm(
150-
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }))
156+
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }), extraArgs)
151157
);
152158

153159
if (generateCommitError) {

0 commit comments

Comments
 (0)