Files
gpt4free/etc/tool/commit.py
kqlio67 54ef1a511c docs: update providers documentation and enhance support for Blackbox HAR auth
- Added "No auth / HAR file" authentication type in providers-and-models.md
- Added "Video generation" column to provider tables for future capability
- Updated model counts and provider capabilities throughout documentation
- Fixed ARTA provider with improved error handling and response validation
- Enhanced AllenAI provider with vision model support and proper image handling
- Significantly improved Blackbox provider:
  - Added HAR file authentication support
  - Added subscription status checking
  - Added premium/demo model differentiation
  - Improved session handling and error recovery
- Enhanced DDG provider with better error handling for challenges
- Improved PollinationsAI and PollinationsImage providers' model handling
- Added VideoModel class in g4f/models.py
- Added audio/video generation indicators in GUI components
- Added new Ai2 models: olmo-1-7b, olmo-2-32b, olmo-4-synthetic
- Added new commit message generation tool in etc/tool/commit.py
2025-04-04 13:36:28 +03:00

106 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""
AI Commit Message Generator using gpt4free (g4f)
This tool uses AI to generate meaningful git commit messages based on
staged changes. It analyzes the git diff and suggests appropriate commit
messages following conventional commit format.
Usage:
python -m etc.tool.commit
"""
import subprocess
import sys
from g4f.client import Client
def get_git_diff():
"""Get the current git diff for staged changes"""
try:
diff_process = subprocess.run(
["git", "diff", "--staged"],
capture_output=True,
text=True
)
return diff_process.stdout
except Exception as e:
print(f"Error running git diff: {e}")
return None
def generate_commit_message(diff_text):
"""Generate a commit message based on the git diff"""
if not diff_text or diff_text.strip() == "":
return "No changes staged for commit"
client = Client()
prompt = f"""
{diff_text}
```
Analyze ONLY the exact changes in this git diff and create a precise commit message.
FORMAT:
1. First line: "<type>: <summary>" (max 70 chars)
- Type: feat, fix, docs, refactor, test, etc.
- Summary must describe ONLY actual changes shown in the diff
2. Leave one blank line
3. Add sufficient bullet points to:
- Describe ALL specific changes seen in the diff
- Reference exact functions/files/components that were modified
- Do NOT mention anything not explicitly shown in the code changes
- Avoid general statements or assumptions not directly visible in diff
- Include enough points to cover all significant changes (don't limit to a specific number)
IMPORTANT: Be 100% factual. Only mention code that was actually changed. Never invent or assume changes not shown in the diff. If unsure about a change's purpose, describe what changed rather than why. Output nothing except for the commit message, and don't surround it in quotes.
"""
try:
response = client.chat.completions.create(
model="claude-3.7-sonnet",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"Error generating commit message: {e}")
return None
def main():
print("Fetching git diff...")
diff = get_git_diff()
if diff is None:
print("Failed to get git diff. Are you in a git repository?")
sys.exit(1)
if diff.strip() == "":
print("No changes staged for commit. Stage changes with 'git add' first.")
sys.exit(0)
print("Generating commit message...")
commit_message = generate_commit_message(diff)
if commit_message:
print("\nGenerated commit message:")
print("-" * 50)
print(commit_message)
print("-" * 50)
user_input = input("\nDo you want to use this commit message? (y/n): ")
if user_input.lower() == 'y':
try:
subprocess.run(
["git", "commit", "-m", commit_message],
check=True
)
print("Commit successful!")
except subprocess.CalledProcessError as e:
print(f"Error making commit: {e}")
else:
print("Failed to generate commit message.")
if __name__ == "__main__":
main()