-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
commit-gen.js
executable file
·201 lines (176 loc) · 4.94 KB
/
commit-gen.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { OpenAI } = require('openai');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Parse command line arguments
const argv = yargs(hideBin(process.argv))
.option('path', {
alias: 'p',
type: 'string',
description: 'Path to the file or directory',
demandOption: true,
})
.option('staged', {
alias: 's',
type: 'boolean',
description:
'Use staged changes instead of comparing against the base branch',
default: false,
})
.help()
.alias('help', 'h').argv;
// Function to find the nearest package.json and get the package name
function getPackageName(filePath) {
let dir = filePath;
while (true) {
const packageJsonPath = path.join(dir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.name) {
return packageJson.name;
} else {
console.error(`Package name not found in ${packageJsonPath}`);
process.exit(1);
}
}
const parentDir = path.dirname(dir);
if (parentDir === dir) {
console.error('Reached root directory without finding package.json');
process.exit(1);
}
dir = parentDir;
}
}
function sanitizeInput(input) {
return input.replace(/[^a-zA-Z0-9_\-\/\.]/g, '');
}
function getAllowedScopes() {
const packagesDir = path.resolve(__dirname, 'packages');
const scopes = [];
fs.readdirSync(packagesDir).forEach((dir) => {
const projectJsonPath = path.join(packagesDir, dir, 'project.json');
if (fs.existsSync(projectJsonPath)) {
const projectJson = JSON.parse(fs.readFileSync(projectJsonPath, 'utf8'));
if (projectJson.name) {
scopes.push(projectJson.name);
}
}
});
return scopes;
}
async function generateCommitMessage(patch, packageName) {
const allowedScopes = getAllowedScopes().join(', ');
const prompt = `Generate a conventional commit message for the following git patch.
RULES:
Never author BREAKING CHANGE messages, they are not allowed.
Body's lines must not be longer than 100 characters [body-max-line-length]
Provide no explanation
Focus on a statement of work of the changes:
${patch}
Allowed scopes: ${allowedScopes}
Please format the commit message as follows:
<type>(<scope>): <subject>
<body>
<footer>`;
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'never author BREAKING CHANGE commits' },
{ role: 'user', content: prompt },
],
max_tokens: 200,
});
return response.choices[0].message.content
.trim()
.replace('```markdown', '')
.replace('```', '')
.replace(/^```(?:\w+)?|```$/g, '')
.trim()
.replace(/^\`/, '')
.replace(/\`$/, '')
.trim();
}
function getGitDiffPatch(filePath, useStaged) {
try {
const sanitizedFilePath = sanitizeInput(filePath);
let patch;
if (useStaged) {
patch = execSync(`git diff --cached -- "${sanitizedFilePath}"`, {
shell: '/bin/bash',
}).toString();
} else {
const baseBranch = execSync(
'git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@g" || echo main',
{ shell: '/bin/bash' },
)
.toString()
.trim();
patch = execSync(`git diff ${baseBranch} -- "${sanitizedFilePath}"`, {
shell: '/bin/bash',
}).toString();
}
return patch;
} catch (error) {
console.error('Error getting git diff:', error.message);
process.exit(1);
}
}
// Function to generate a random filename
function generateRandomFilename() {
const adjectives = [
'quick',
'lazy',
'sleepy',
'noisy',
'hungry',
'brave',
'calm',
'eager',
'gentle',
'happy',
];
const animals = [
'fox',
'dog',
'cat',
'mouse',
'owl',
'tiger',
'lion',
'bear',
'wolf',
'eagle',
];
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const animal = animals[Math.floor(Math.random() * animals.length)];
return `ai-${adjective}-${animal}.md`;
}
async function main() {
const filePath = path.resolve(argv.path);
if (!fs.existsSync(filePath)) {
console.error(`File or directory not found: ${filePath}`);
process.exit(1);
}
const packageName = getPackageName(filePath);
const patch = getGitDiffPatch(filePath, argv.staged);
if (!patch) {
console.log('No changes detected.');
process.exit(0);
}
try {
const commitMessage = await generateCommitMessage(patch, packageName);
console.log('Generated Commit Message:');
console.log(commitMessage);
} catch (error) {
console.error('Error generating commit message:', error.message);
process.exit(1);
}
}
main();