Skip to content

Commit

Permalink
feat: --all flag (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
rocktimsaikia and privatenumber committed Apr 1, 2023
1 parent ad2533e commit ebe83a4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ git add <files...>
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
aicommits --dry-run
aicommits --all # or -a
```

> 👉 **Tip:** Use the `aic` alias if `aicommits` is too long for you.
Expand All @@ -73,6 +74,10 @@ aicommits --generate <i> # or -g <i>

> Warning: this uses more tokens, meaning it costs more.
```sh
aicommits --all
```

### 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.
Expand Down
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ cli(
description: 'Files to exclude from AI analysis',
alias: 'x',
},
all: {
type: Boolean,
description: 'Automatically stage changes in tracked files for the commit',
alias: 'a',
default: false,
},
},

commands: [
Expand All @@ -49,6 +55,7 @@ cli(
aicommits(
argv.flags.generate,
argv.flags.exclude,
argv.flags.all,
rawArgv,
);
}
Expand Down
8 changes: 7 additions & 1 deletion src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ import { KnownError, handleCliError } from '../utils/error.js';
export default async (
generate: number | undefined,
excludeFiles: string[],
stageAll: boolean,
rawArgv: string[],
) => (async () => {

Check warning on line 22 in src/commands/aicommits.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Async arrow function has a complexity of 12. Maximum allowed is 10

Check warning on line 22 in src/commands/aicommits.ts

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Async arrow function has a complexity of 12. Maximum allowed is 10

Check warning on line 22 in src/commands/aicommits.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Async arrow function has a complexity of 12. Maximum allowed is 10
intro(bgCyan(black(' aicommits ')));
await assertGitRepo();

const detectingFiles = spinner();

if (stageAll) {
await execa('git', ['add', '--all']);
}

detectingFiles.start('Detecting staged files');
const staged = await getStagedDiff(excludeFiles);

if (!staged) {
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${
Expand Down
34 changes: 33 additions & 1 deletion tests/specs/cli/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default testSuite(({ describe }) => {

const { stdout, exitCode } = await aicommits(['--exclude', 'data.json'], { reject: false });
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();
});

Expand Down Expand Up @@ -61,6 +61,38 @@ export default testSuite(({ describe }) => {
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 }) => {
const { fixture, aicommits } = await createFixture({
...files,
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/cli/error-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default testSuite(({ describe }) => {

const { stdout, exitCode } = await aicommits([], { reject: false });
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();
});
});
Expand Down

0 comments on commit ebe83a4

Please sign in to comment.