-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
executable file
·53 lines (41 loc) · 1.54 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env node
import git from 'simple-git';
import ora from 'ora';
import { gpt } from './lib/ai.mjs';
async function performCommit() {
const spinner = ora('Generating commit message...').start();
try {
const status = await git().status();
if (status.isClean() && status.not_added.length === 0) {
throw new Error('Nothing to commit');
}
await git().add('./*');
const message = await generateCommitMessage();
spinner.text = `Committing:... "${message}"`;
await git().commit(message);
spinner.succeed("Committed: " + message);
} catch (error) {
spinner.fail('Error: ' + error.message);
}
}
async function generateCommitMessage() {
const diff = await git().diff(['--', ':!package-lock.json'])
const diffStaged = await git().diff(['--staged', '--', ':!package-lock.json'])
const combined = `${diff}\n${diffStaged}`
const commitMessage = await gpt(systemPrompt, combined);
return commitMessage;
}
const systemPrompt = `
You are a git commit message generator.
You will be given a diff by the user, and you must generate a commit message based on that diff.
Output only a single line of text, with no formatting of any kind.
Do not add any explanations to the main commit message.
If you don't understand the context of the diff, print which file changed, e.g.:
Changes to package.json
Some more examples of short and descriptive commit messages:
Example 1:
Fix bug where users cannot log in
Example 2:
Add support for user avatars
`
performCommit();