feat: --all flag (#182)

Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
This commit is contained in:
Rocktim
2023-04-01 15:49:34 +05:30
committed by GitHub
parent ad2533eb2f
commit ebe83a493e
5 changed files with 55 additions and 5 deletions

View File

@@ -56,10 +56,11 @@ git add <files...>
aicommits aicommits
``` ```
`aicommits` passes down unknown flags to `git commit`, so you can pass in [`commit` flags](https://git-scm.com/docs/git-commit) (with some exceptions (e.g. `--all`): `aicommits` passes down unknown flags to `git commit`, so you can pass in [`commit` flags](https://git-scm.com/docs/git-commit).
For example, you can stage all changes in tracked files with as you commit:
```sh ```sh
aicommits --dry-run aicommits --all # or -a
``` ```
> 👉 **Tip:** Use the `aic` alias if `aicommits` is too long for you. > 👉 **Tip:** Use the `aic` alias if `aicommits` is too long for you.
@@ -73,6 +74,10 @@ aicommits --generate <i> # or -g <i>
> Warning: this uses more tokens, meaning it costs more. > Warning: this uses more tokens, meaning it costs more.
```sh
aicommits --all
```
### Git hook ### Git hook
You can also integrate _aicommits_ with Git via the [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. This lets you use Git like you normally would, and edit the commit message before committing. You can also integrate _aicommits_ with Git via the [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. This lets you use Git like you normally would, and edit the commit message before committing.

View File

@@ -29,6 +29,12 @@ cli(
description: 'Files to exclude from AI analysis', description: 'Files to exclude from AI analysis',
alias: 'x', alias: 'x',
}, },
all: {
type: Boolean,
description: 'Automatically stage changes in tracked files for the commit',
alias: 'a',
default: false,
},
}, },
commands: [ commands: [
@@ -49,6 +55,7 @@ cli(
aicommits( aicommits(
argv.flags.generate, argv.flags.generate,
argv.flags.exclude, argv.flags.exclude,
argv.flags.all,
rawArgv, rawArgv,
); );
} }

View File

@@ -17,18 +17,24 @@ import { KnownError, handleCliError } from '../utils/error.js';
export default async ( export default async (
generate: number | undefined, generate: number | undefined,
excludeFiles: string[], excludeFiles: string[],
stageAll: boolean,
rawArgv: string[], rawArgv: string[],
) => (async () => { ) => (async () => {
intro(bgCyan(black(' aicommits '))); intro(bgCyan(black(' aicommits ')));
await assertGitRepo(); await assertGitRepo();
const detectingFiles = spinner(); const detectingFiles = spinner();
if (stageAll) {
await execa('git', ['add', '--all']);
}
detectingFiles.start('Detecting staged files'); detectingFiles.start('Detecting staged files');
const staged = await getStagedDiff(excludeFiles); const staged = await getStagedDiff(excludeFiles);
if (!staged) { if (!staged) {
detectingFiles.stop('Detecting staged files'); detectingFiles.stop('Detecting staged files');
throw new KnownError('No staged changes found. Make sure to stage your changes with `git add`.'); throw new KnownError('No staged changes found. Stage your changes manually, or automatically stage all changes with the `--all` flag.');
} }
detectingFiles.stop(`${getDetectedMessage(staged.files)}:\n${ detectingFiles.stop(`${getDetectedMessage(staged.files)}:\n${

View File

@@ -31,7 +31,7 @@ export default testSuite(({ describe }) => {
const { stdout, exitCode } = await aicommits(['--exclude', 'data.json'], { reject: false }); const { stdout, exitCode } = await aicommits(['--exclude', 'data.json'], { reject: false });
expect(exitCode).toBe(1); expect(exitCode).toBe(1);
expect(stdout).toMatch('No staged changes found. Make sure to stage your changes with `git add`.'); expect(stdout).toMatch('No staged changes found.');
await fixture.rm(); await fixture.rm();
}); });
@@ -61,6 +61,38 @@ export default testSuite(({ describe }) => {
await fixture.rm(); await fixture.rm();
}); });
test('Accepts --all flag, staging all changes before commit', async () => {
const { fixture, aicommits } = await createFixture(files);
const git = await createGit(fixture.path);
await git('add', ['data.json']);
await git('commit', ['-m', 'wip']);
await fixture.writeFile('data.json', 'Test');
const statusBefore = await git('status', ['--short', '--untracked-files=no']);
expect(statusBefore.stdout).toBe(' M data.json');
const committing = aicommits(['--all']);
committing.stdout!.on('data', (buffer: Buffer) => {
const stdout = buffer.toString();
if (stdout.match('└')) {
committing.stdin!.write('y');
committing.stdin!.end();
}
});
await committing;
const statusAfter = await git('status', ['--short', '--untracked-files=no']);
expect(statusAfter.stdout).toBe('');
const { stdout: commitMessage } = await git('log', ['-n1', '--oneline']);
console.log('Committed with:', commitMessage);
await fixture.rm();
});
test('Accepts --generate flag, overriding config', async ({ onTestFail }) => { test('Accepts --generate flag, overriding config', async ({ onTestFail }) => {
const { fixture, aicommits } = await createFixture({ const { fixture, aicommits } = await createFixture({
...files, ...files,

View File

@@ -17,7 +17,7 @@ export default testSuite(({ describe }) => {
const { stdout, exitCode } = await aicommits([], { reject: false }); const { stdout, exitCode } = await aicommits([], { reject: false });
expect(exitCode).toBe(1); expect(exitCode).toBe(1);
expect(stdout).toMatch('No staged changes found. Make sure to stage your changes with `git add`.'); expect(stdout).toMatch('No staged changes found. Stage your changes manually, or automatically stage all changes with the `--all` flag.');
await fixture.rm(); await fixture.rm();
}); });
}); });