git-auto-ai-himamshu 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/initial.txt +2 -0
- package/package.json +22 -0
- package/src/ai.js +45 -0
- package/src/git.js +47 -0
- package/src/index.js +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Git-Auto 🚀
|
|
2
|
+
|
|
3
|
+
`git-auto` is a lightweight CLI tool that automates the process of staging changes and generating professional, AI-powered commit messages. No more guessing what to write in your commit messages—let AI handle it!
|
|
4
|
+
|
|
5
|
+
## ✨ Features
|
|
6
|
+
|
|
7
|
+
- **One-Command Workflow**: Stages all changes and commits them in one go.
|
|
8
|
+
- **AI-Powered Messages**: Analyzes your `git diff` to generate a meaningful commit message.
|
|
9
|
+
- **Conventional Commits**: Follows the [Conventional Commits](https://www.conventionalcommits.org/) specification (e.g., `feat:`, `fix:`, `chore:`).
|
|
10
|
+
- **Customizable**: Override AI messages with your own or preview them before committing.
|
|
11
|
+
|
|
12
|
+
## 📦 Installation
|
|
13
|
+
|
|
14
|
+
### 1. Clone the Repository
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/your-username/git-auto.git
|
|
17
|
+
cd git-auto
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 2. Install Dependencies
|
|
21
|
+
```bash
|
|
22
|
+
npm install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 3. Link Globally
|
|
26
|
+
To use the `git-auto` command anywhere on your system:
|
|
27
|
+
```bash
|
|
28
|
+
npm link
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## ⚙️ Configuration
|
|
32
|
+
|
|
33
|
+
The tool uses the Groq API for fast, free AI generation.
|
|
34
|
+
|
|
35
|
+
1. Get a free API key from [Groq Cloud](https://console.groq.com/).
|
|
36
|
+
2. Create a `.env` file in the root directory:
|
|
37
|
+
```env
|
|
38
|
+
GROQ_API_KEY=your_api_key_here
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 🚀 Usage
|
|
42
|
+
|
|
43
|
+
### Basic Commit
|
|
44
|
+
Stages all changes and commits with an AI-generated message:
|
|
45
|
+
```bash
|
|
46
|
+
git-auto commit
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Commit with Custom Message
|
|
50
|
+
Override the AI and provide your own message:
|
|
51
|
+
```bash
|
|
52
|
+
git-auto commit -m "feat: add amazing new feature"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Preview Message (Dry Run)
|
|
56
|
+
See what the AI would generate without actually committing:
|
|
57
|
+
```bash
|
|
58
|
+
git-auto commit --dry-run
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 🛠️ How it Works
|
|
62
|
+
|
|
63
|
+
1. **Staging**: Runs `git add .` to stage all changes.
|
|
64
|
+
2. **Diffing**: Extracts the staged changes using `git diff --cached`.
|
|
65
|
+
3. **AI Generation**: Sends the diff to a Large Language Model (LLM) via Groq.
|
|
66
|
+
4. **Committing**: Executes `git commit -m "generated_message"`.
|
|
67
|
+
|
|
68
|
+
## 📜 License
|
|
69
|
+
MIT
|
package/initial.txt
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "git-auto-ai-himamshu",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"git-auto": "src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"type": "commonjs",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"commander": "^15.0.0",
|
|
18
|
+
"dotenv": "^18.0.4",
|
|
19
|
+
"openai": "^7.23.0",
|
|
20
|
+
"simple-git": "^4.0.1"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/ai.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const OpenAI = require('openai');
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
|
|
4
|
+
const openai = new OpenAI({
|
|
5
|
+
apiKey: process.env.GROQ_API_KEY,
|
|
6
|
+
baseURL: "https://api.groq.com/openai/v1"
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates a professional commit message based on the provided git diff.
|
|
11
|
+
* @param {string} diff - The git diff of staged changes.
|
|
12
|
+
* @returns {Promise<string>} - The generated commit message.
|
|
13
|
+
* @throws {Error} - If API key is missing or API call fails.
|
|
14
|
+
*/
|
|
15
|
+
async function generateCommitMessage(diff) {
|
|
16
|
+
if (!process.env.GROQ_API_KEY) {
|
|
17
|
+
throw new Error('GROQ_API_KEY is missing from .env file.');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const prompt = `
|
|
21
|
+
Analyze the following git diff and write a professional, concise commit message.
|
|
22
|
+
Follow the Conventional Commits specification (e.g., feat: ..., fix: ..., chore: ..., docs: ..., style: ..., refactor: ..., perf: ..., test: ...).
|
|
23
|
+
Only return the commit message string itself, without any quotes or explanation.
|
|
24
|
+
|
|
25
|
+
Diff:
|
|
26
|
+
${diff}
|
|
27
|
+
`.trim();
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const response = await openai.chat.completions.create({
|
|
31
|
+
model: 'qwen/qwen3.8-27b',
|
|
32
|
+
messages: [{ role: 'user', content: prompt }],
|
|
33
|
+
temperature: 0.2,
|
|
34
|
+
max_tokens: 100,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return response.choices[0].message.content.trim();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new Error(`AI generation failed: ${error.message}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
module.exports = {
|
|
44
|
+
generateCommitMessage
|
|
45
|
+
};
|
package/src/git.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const { simpleGit } = require('simple-git');
|
|
2
|
+
const git = simpleGit();
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the current directory is a git repository.
|
|
6
|
+
* @returns {Promise<boolean>}
|
|
7
|
+
*/
|
|
8
|
+
async function isGitRepo() {
|
|
9
|
+
try {
|
|
10
|
+
await git.revparse(['--is-inside-work-tree']);
|
|
11
|
+
return true;
|
|
12
|
+
} catch (e) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Stages all changes in the repository.
|
|
19
|
+
* @returns {Promise<void>}
|
|
20
|
+
*/
|
|
21
|
+
async function stageAll() {
|
|
22
|
+
await git.add('.');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves the diff of currently staged changes.
|
|
27
|
+
* @returns {Promise<string>}
|
|
28
|
+
*/
|
|
29
|
+
async function getStagedDiff() {
|
|
30
|
+
return await git.diff(['--cached']);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Commits the staged changes with the given message.
|
|
35
|
+
* @param {string} message - The commit message to use.
|
|
36
|
+
* @returns {Promise<void>}
|
|
37
|
+
*/
|
|
38
|
+
async function commit(message) {
|
|
39
|
+
await git.commit(message);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
isGitRepo,
|
|
44
|
+
stageAll,
|
|
45
|
+
getStagedDiff,
|
|
46
|
+
commit
|
|
47
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { Command } = require('commander');
|
|
3
|
+
const git = require('./git');
|
|
4
|
+
const ai = require('./ai');
|
|
5
|
+
require('dotenv').config();
|
|
6
|
+
|
|
7
|
+
const program = new Command();
|
|
8
|
+
|
|
9
|
+
program
|
|
10
|
+
.name('git-auto')
|
|
11
|
+
.description('AI-powered git commit tool')
|
|
12
|
+
.version('1.0.0');
|
|
13
|
+
|
|
14
|
+
program
|
|
15
|
+
.command('commit')
|
|
16
|
+
.description('Automatically stage and commit changes with an AI-generated message')
|
|
17
|
+
.option('-m, --message <message>', 'override the AI-generated commit message')
|
|
18
|
+
.option('--dry-run', 'preview the generated message without committing')
|
|
19
|
+
.action(async (options) => {
|
|
20
|
+
try {
|
|
21
|
+
if (!(await git.isGitRepo())) {
|
|
22
|
+
console.error('Error: Current directory is not a git repository.');
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
console.log('Staging changes...');
|
|
27
|
+
await git.stageAll();
|
|
28
|
+
|
|
29
|
+
const diff = await git.getStagedDiff();
|
|
30
|
+
if (!diff) {
|
|
31
|
+
console.log('No changes to commit.');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let commitMessage;
|
|
36
|
+
if (options.message) {
|
|
37
|
+
commitMessage = options.message;
|
|
38
|
+
console.log(`Using custom message: ${commitMessage}`);
|
|
39
|
+
} else {
|
|
40
|
+
console.log('Generating AI commit message...');
|
|
41
|
+
commitMessage = await ai.generateCommitMessage(diff);
|
|
42
|
+
console.log(`Generated message: ${commitMessage}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (options.dryRun) {
|
|
46
|
+
console.log('Dry run enabled. Skipping commit.');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
console.log('Committing changes...');
|
|
51
|
+
await git.commit(commitMessage);
|
|
52
|
+
console.log('Successfully committed changes!');
|
|
53
|
+
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error(`Error: ${error.message}`);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
program.parse(process.argv);
|