Understanding Commit-AI's design and structure.
Commit-AI/
├── main.go # Entry point
├── cmd/
│ └── root.go # CLI command handler
├── internal/
│ ├── ai/
│ │ ├── parser.go # AI response parsing
│ │ └── parser_test.go # AI tests
│ ├── config/
│ │ └── config.go # Configuration management
│ ├── git/
│ │ ├── git.go # Git utilities
│ │ └── diff.go # Diff operations
│ └── updater/
│ └── updater.go # Auto-update functionality
├── installer/
│ └── main.go # Installer
├── docs/ # Documentation
├── go.mod # Dependencies
├── Makefile # Build automation
├── build.ps1 # Windows build script
└── .gitattributes # Binary file marking
func main() {
// Set version info from build flags
cmd.SetVersion(version, buildDate, gitCommit)
// Execute CLI command
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}Responsibilities:
- Application entry point
- Version information setup
- Error handling
Main Function: runCommitAI()
Workflow:
- Load configuration
- Check for staged/unstaged changes
- Offer to stage all changes if needed
- Get staged files for scope detection
- Retrieve staged diff with binary filtering
- Send to Groq AI with enhanced prompt
- Parse AI response (3 options + report)
- Interactive selection
- Execute git commit or display
Key Features:
- Interactive prompts using
surveylibrary - Color-coded output using
fatih/color - Verbose mode for debugging
- Emoji support
- Model override capability
Key Functions:
- Constructs enhanced AI prompt
- Includes critical instructions
- Adds emoji guidance if enabled
- Returns formatted prompt for Groq API
- Extracts 3 commit options from AI response
- Extracts detailed report
- Handles invalid formats with fallback
- Returns options and report
- Validates Conventional Commits format
- Strips leading emojis for validation
- Checks for valid commit types
- Returns validation result
- Analyzes file paths
- Detects most common directory
- Returns suggested scope
- Used for auto-detection
Key Functions:
- Loads from
.envor~/.commit-ai.env - Returns Config struct with:
- APIKey
- Model (default:
llama-3.1-8b-instant) - Temperature (default: 0.7)
- MaxTokens (default: 15000)
- Saves API key to
~/.commit-ai.env - Creates file if not exists
- Sets secure permissions (600)
Key Functions:
GetIgnorePatterns()- Parses.gitignoreGetIgnorePatternsFromFile()- Reads ignore file
Key Functions:
- Retrieves staged changes
- Filters binary files (50+ types)
- Applies exclude patterns
- Truncates to maxSize
- Returns formatted diff
- Lists all staged files
- Used for scope detection
- Checks for staged changes
- Checks for unstaged changes
- Includes untracked files
- Runs
git add . - Stages all changes
- Checks if file matches binary patterns
- Uses filepath.Match for pattern matching
Key Functions:
- Queries GitHub API
- Compares versions
- Returns latest release info
- Selects correct binary for platform
- Supports Windows, macOS (Intel/ARM), Linux (amd64/arm64)
- Downloads binary from GitHub
- Saves to temporary location
- Verifies download
- Backs up current binary
- Replaces with new version
- Handles Windows/Unix differences
Functionality:
- Downloads latest binary from GitHub
- Creates installation directory
- Updates PATH environment variable
- Platform-specific installation logic
User Input
↓
CLI Handler (cmd/root.go)
↓
Git Operations (internal/git/)
├─ Check for changes
├─ Stage changes if needed
└─ Get staged diff
↓
AI Module (internal/ai/)
├─ Build prompt
├─ Send to Groq API
└─ Parse response
↓
Interactive Selection
├─ Choose commit message
├─ Choose report format
└─ Review final message
↓
Git Commit
└─ Execute git commit
- Separate concerns into dedicated packages
- Clean interfaces between modules
- Easy to test and extend
- Prevents AI from analyzing compiled code
- Reduces API token usage
- Improves commit message accuracy
- User-friendly CLI experience
- Multiple options for flexibility
- Edit capability for customization
- Optional visual enhancement
- Improves commit history readability
- Configurable via flag
- Built-in update mechanism
- Safe update with backup
- Cross-platform support
| Library | Purpose | Version |
|---|---|---|
github.com/spf13/cobra |
CLI framework | Latest |
github.com/AlecAivazis/survey/v2 |
Interactive prompts | v2.x |
github.com/algolyzer/groq-go |
Groq API client | Latest |
github.com/fatih/color |
Colored output | Latest |
github.com/joho/godotenv |
.env parsing | Latest |
context- Context managementfmt- Formattingos- OS operationsos/exec- Command executionstrings- String operationsnet/http- HTTP requestsencoding/json- JSON parsing
Environment Variables
↓
.env (project)
↓
~/.commit-ai.env (global)
↓
Default Values
↓
Config Struct
-
Configuration Errors
- Missing API key
- Invalid configuration
-
Git Errors
- Failed to get diff
- Failed to stage changes
- Failed to commit
-
AI Errors
- API request failed
- Invalid response format
- Rate limit exceeded
-
System Errors
- File I/O errors
- Permission errors
- Network errors
- Helpful error messages
- Suggestions for resolution
- Graceful degradation
- Fallback mechanisms
-
Diff Size Limiting
- Truncate to 8,000 characters
- Reduces API token usage
- Maintains accuracy
-
Binary File Filtering
- Prevents unnecessary analysis
- Reduces API calls
- Improves performance
-
Caching
- Configuration caching
- Scope detection caching
- Pattern matching caching
-
Concurrency
- Parallel file analysis (future)
- Async API calls (future)
- Stored in
~/.commit-ai.env - File permissions: 600 (read/write owner only)
- Never logged or displayed
- Validated before use
- Commit message validation
- File path validation
- API response validation
- Prevents accidental analysis of sensitive data
- Filters 50+ binary file types
- Extensible pattern matching
- Unit Tests: 85%+ coverage
- Integration Tests: Git operations
- End-to-End Tests: Full workflow
internal/ai/parser_test.go- AI module tests- Test cases for validation, parsing, scope detection
-
Plugin System
- Custom AI providers
- Custom commit templates
- Custom validation rules
-
Caching Layer
- Cache AI responses
- Cache scope detection
- Reduce API calls
-
Async Processing
- Parallel file analysis
- Background updates
- Non-blocking UI
-
Multi-Model Support
- OpenAI integration
- Anthropic integration
- Local model support
-
IDE Integration
- VSCode extension
- JetBrains plugin
- Vim/Neovim plugin