Skip to content

Commit d0599c5

Browse files
authored
Merge pull request #19 from andev0x/optimization/func
Optimization/func
2 parents b9e8ccc + 4ecd13b commit d0599c5

8 files changed

Lines changed: 1880 additions & 44 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Thumbs.db
3737
gitmit
3838
dist/
3939
build/
40+
/bin/
4041

4142
# Env
4243
.env

docs/OPTIMIZATION.md

Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
# Gitmit Optimization Guide
2+
3+
## Overview
4+
5+
This document describes the intelligent optimizations implemented in Gitmit to provide smart, context-aware commit message suggestions based purely on git changes analysis and template matching - all working locally without any internet connection or AI.
6+
7+
## Key Optimizations
8+
9+
### 1. Intelligent Git Diff Analysis
10+
11+
The analyzer now performs deep pattern detection on git diffs:
12+
13+
#### Code Structure Detection
14+
- **Function Detection**: Identifies new or modified function declarations
15+
- **Struct Detection**: Recognizes struct definitions and changes
16+
- **Method Detection**: Detects method additions and modifications
17+
- **Import Analysis**: Tracks import changes to understand dependencies
18+
19+
#### Pattern Recognition
20+
The system detects these change patterns:
21+
- `error-handling`: Addition of error handling code
22+
- `test-addition`: New test functions
23+
- `import-changes`: Import modifications
24+
- `documentation`: Comment additions (3+ comments)
25+
- `refactoring`: Large-scale code changes (10+ adds/removes)
26+
- `configuration`: Config file changes
27+
- `api-changes`: HTTP/router/handler modifications
28+
- `database`: SQL/GORM/query changes
29+
- `performance`: Concurrency/optimization code
30+
- `security`: Authentication/encryption changes
31+
32+
#### Enhanced Action Determination
33+
The analyzer now considers:
34+
- Security updates (security, vulnerability keywords)
35+
- Performance improvements (optimize, cache, goroutine)
36+
- Style changes (formatting, whitespace)
37+
- Bug fixes (fix, bug, issue, resolve keywords)
38+
- Test updates (test files)
39+
40+
### 2. Context-Aware Template Scoring
41+
42+
Templates are now scored based on multiple factors:
43+
44+
#### Scoring Criteria
45+
- **Base Score**: 1.0 for all templates
46+
- **Pattern Matching**: +2.0 for templates matching detected patterns
47+
- **Structure Detection**: +1.5 for using detected functions/structs
48+
- **Purpose Relevance**: +1.0 for meaningful purpose placeholders
49+
- **File Type Context**: +0.5-1.5 based on file extensions
50+
- **Major Changes**: +1.0 for restructure/refactor templates
51+
- **Generic Penalty**: -0.5 for generic templates when specific info exists
52+
53+
#### Template Selection Process
54+
1. Score all available templates
55+
2. Sort by score (highest first)
56+
3. Select highest-scored template not in recent history
57+
4. Fall back to highest-scored if all are recent duplicates
58+
59+
### 3. Enhanced Template Library
60+
61+
The template library has been expanded with:
62+
63+
#### New Categories
64+
- `handler`: For request handler changes
65+
- `middleware`: For middleware modifications
66+
- `service`: For service layer changes
67+
- `util`: For utility functions
68+
- `parser`: For parsing logic
69+
- `analyzer`: For analysis code
70+
71+
#### New Action Types
72+
- `SECURITY`: Security-focused commits
73+
- `PERF`: Performance improvements
74+
- `STYLE`: Code formatting changes
75+
- `TEST`: Test modifications
76+
77+
#### More Variations
78+
Each category now includes 3-6 template variations for better context matching.
79+
80+
### 4. Multi-File Pattern Detection
81+
82+
The system now analyzes patterns across multiple files:
83+
84+
#### Detected Patterns
85+
- **feature-addition**: 3+ new files (60% of changes)
86+
- **bug-fix-cascade**: 3+ modifications with "fix" keywords
87+
- **refactor-sweep**: Mix of additions, modifications, and deletions (4+ files)
88+
- **test-suite-update**: 70%+ test files changed
89+
- **config-update**: 70%+ config files changed
90+
- **api-redesign**: 3+ API/handler files modified
91+
- **database-migration**: 2+ migration/schema files changed
92+
93+
#### Automatic Adjustments
94+
When multi-file patterns are detected, the system automatically:
95+
- Adjusts commit action type
96+
- Updates purpose description
97+
- Improves message context
98+
99+
### 5. Intelligent Scope Detection
100+
101+
Smart scope determination based on:
102+
103+
#### Single Topic
104+
If all changes are in the same topic, use that topic as scope.
105+
106+
#### Single Directory
107+
If changes span topics but share a directory, use directory as scope.
108+
109+
#### Multiple Related Topics
110+
For 2-3 related topics, create combined scope (e.g., "api,service,handler").
111+
112+
#### Many Topics
113+
For many topics, use most common topic or "core".
114+
115+
### 6. Enhanced Item Detection
116+
117+
Item selection now prioritizes:
118+
1. Detected function names
119+
2. Detected struct names
120+
3. Detected method names
121+
4. Original file-based item
122+
123+
This ensures commit messages reference actual code elements.
124+
125+
## How It Works
126+
127+
### Analysis Flow
128+
129+
```
130+
Git Changes → Parser → Analyzer → Templater → Formatter → Commit Message
131+
↓ ↓ ↓ ↓ ↓
132+
Diff Data Changes Patterns Scoring Final
133+
+ Stats + Context + Select Format
134+
```
135+
136+
### Example: Adding a New Function
137+
138+
**Input**: New function in `internal/parser/git.go`
139+
```go
140+
+func ParseCommitHistory() ([]Commit, error) {
141+
+ // implementation
142+
+}
143+
```
144+
145+
**Analysis**:
146+
- Action: `A` (added)
147+
- Detected Function: `ParseCommitHistory`
148+
- Topic: `parser` (from directory)
149+
- Pattern: `error-handling` (return error)
150+
- Item: `ParseCommitHistory` (from function)
151+
152+
**Template Scoring**:
153+
- "feat(parser): add {item} for {purpose}" → Score: 4.5
154+
- "feat(parser): implement {item}" → Score: 4.0
155+
- "feat: add {item} in {topic}" → Score: 2.5
156+
157+
**Result**: `feat(parser): add ParseCommitHistory for commit history parsing`
158+
159+
### Example: Bug Fix Across Multiple Files
160+
161+
**Input**:
162+
- Modified `internal/handler/user.go`
163+
- Modified `internal/service/user.go`
164+
- Modified `internal/validator/user.go`
165+
166+
All contain "fix" keywords.
167+
168+
**Analysis**:
169+
- Multi-file pattern: `bug-fix-cascade`
170+
- Action: `fix` (auto-adjusted)
171+
- Scope: `handler,service,validator`
172+
- Purpose: `resolve issue across multiple components` (auto-adjusted)
173+
174+
**Result**: `fix(handler,service,validator): resolve issue across multiple components`
175+
176+
## Configuration
177+
178+
### Custom Topic Mappings
179+
180+
Create `.commit_suggest.json` in your project root:
181+
182+
```json
183+
{
184+
"topicMappings": {
185+
"controllers": "api",
186+
"models": "db",
187+
"views": "ui"
188+
},
189+
"keywordMappings": {
190+
"authenticate": "authentication",
191+
"authorize": "authorization",
192+
"sanitize": "input validation"
193+
}
194+
}
195+
```
196+
197+
### Custom Templates
198+
199+
Place `templates.json` next to the executable:
200+
201+
```json
202+
{
203+
"A": {
204+
"mymodule": [
205+
"feat(mymodule): add {item} for {purpose}",
206+
"feat(mymodule): implement {item}"
207+
]
208+
}
209+
}
210+
```
211+
212+
## Performance
213+
214+
All optimizations run locally with:
215+
- **No network calls**: 100% offline operation
216+
- **Fast analysis**: Processes changes in milliseconds
217+
- **Low memory**: Efficient pattern matching
218+
- **No AI required**: Pure algorithmic approach
219+
220+
## Best Practices
221+
222+
### 1. Stage Related Changes Together
223+
Group related changes in commits for better pattern detection:
224+
```bash
225+
git add internal/parser/*.go
226+
gitmit propose
227+
```
228+
229+
### 2. Use Meaningful Names
230+
The system extracts function/struct names, so use descriptive names:
231+
```go
232+
// Good: func ValidateUserCredentials()
233+
// Better for commit messages than: func validate()
234+
```
235+
236+
### 3. Add Context Comments
237+
The analyzer detects documentation patterns:
238+
```go
239+
// Adding validation for user input
240+
func ValidateInput(input string) error {
241+
// implementation
242+
}
243+
```
244+
245+
### 4. Consistent File Organization
246+
Better scope detection with organized structure:
247+
```
248+
internal/
249+
├── handler/
250+
├── service/
251+
└── repository/
252+
```
253+
254+
## Testing the Optimizations
255+
256+
### Test Pattern Detection
257+
```bash
258+
# Add error handling
259+
git add file-with-error-handling.go
260+
./bin/gitmit propose --dry-run
261+
262+
# Add tests
263+
git add *_test.go
264+
./bin/gitmit propose --dry-run
265+
266+
# Multi-file refactor
267+
git add internal/*/refactored-files.go
268+
./bin/gitmit propose --dry-run
269+
```
270+
271+
### Test Scope Detection
272+
```bash
273+
# Changes in single module
274+
git add internal/parser/*.go
275+
./bin/gitmit propose --dry-run
276+
277+
# Changes across modules
278+
git add internal/*/*.go
279+
./bin/gitmit propose --dry-run
280+
```
281+
282+
## Future Enhancements
283+
284+
Potential improvements for local-only operation:
285+
- Learn from project-specific commit history
286+
- Build project-specific keyword dictionaries
287+
- Detect naming conventions automatically
288+
- Suggest breaking change indicators
289+
- Integration with git hooks for validation
290+
- Custom pattern definition files
291+
292+
## Troubleshooting
293+
294+
### Generic Messages
295+
**Issue**: Getting generic commit messages
296+
**Solution**:
297+
- Ensure meaningful function/struct names
298+
- Add more context in code changes
299+
- Stage related files together
300+
- Check `.commit_suggest.json` for custom mappings
301+
302+
### Incorrect Action Type
303+
**Issue**: Wrong commit type (feat vs fix)
304+
**Solution**:
305+
- Use clear keywords in changes ("fix", "bug", "optimize")
306+
- Ensure proper file naming (e.g., `*_test.go` for tests)
307+
- Check multi-file pattern detection
308+
309+
### Poor Scope Detection
310+
**Issue**: Scope doesn't reflect actual changes
311+
**Solution**:
312+
- Organize files in clear directory structure
313+
- Keep related changes in same directories
314+
- Use consistent module naming
315+
316+
## Contributing
317+
318+
When adding new optimizations:
319+
1. Keep everything local (no network calls)
320+
2. Use algorithmic approaches (no AI/LLM)
321+
3. Test with various commit types
322+
4. Document pattern detection logic
323+
5. Add template variations for new patterns
324+
325+
---
326+
327+
For more information, see [README.md](README.md) and [CONTRIBUTING.md](CONTRIBUTING.md).

0 commit comments

Comments
 (0)