在現代軟件開發中,編寫清晰且一致的Git提交信息對于維護項目歷史和促進團隊協作至關重要。然而,為每次變更手動撰寫描述性提交信息可能耗時,尤其是處理復雜差異或大型項目時。AI Commit 是一個利用AI分析Git差異并生成符合Conventional Commits規范的提交信息的Python腳本。本文將介紹其工作原理、核心功能以及如何優化您的開發流程。
什么是AI Commit?
AI Commit 是一個Python工具,通過分析暫存的Git變更(git diff --cached
)并利用大型語言模型(LLM)生成簡潔、上下文相關的提交信息。它集成了外部API(如阿里云的Qwen模型),能夠識別代碼變更類型(功能添加、錯誤修復、重構等),并生成符合規范的提交信息,確保項目歷史清晰易懂。
核心功能
1. 智能差異分析
腳本解析Git差異,提取關鍵信息:
- 文件變更:識別新增、修改和刪除的文件。
- 變更類型:檢測重構、功能添加、錯誤修復、文檔更新或配置變更。
- 優先文件分析:重點關注重要文件類型(如
.py
、.js
、.java
)。 - 變更范圍:判斷變更影響單個文件、模塊或多個項目部分。
對于大型差異或多文件項目,AI Commit生成簡潔摘要,保留關鍵細節。
2. 上下文感知的提交信息
AI Commit根據變更類型生成符合Conventional Commits規范的提交信息,例如:
- 文檔更新:使用
docs:
前綴(例:docs: 更新README中的安裝說明
)。 - 測試變更:使用
test:
前綴(例:test: 添加用戶認證單元測試
)。 - 錯誤修復:使用
fix:
前綴(例:fix: 修復數據解析中的空指針異常
)。 - 新功能:使用
feat:
前綴(例:feat: 實現用戶配置文件API端點
)。 - 代碼重構:使用
refactor:
前綴(例:refactor: 將工具函數提取到獨立模塊
)。 - 配置變更:使用
chore:
或config:
前綴(例:chore: 更新package.json中的依賴
)。
這確保提交信息與工具如semantic-release兼容,便于生成變更日志。
3. 支持新項目初始化
腳本能檢測新項目的初始提交(例如包含README.md
、package.json
或.gitignore
等文件),生成類似init: 初始化項目結構
的提交信息。
4. 處理大型差異
對于超過8000字符或涉及10個以上文件的差異,AI Commit通過以下方式進行智能處理:
- 按文件類型分組(例:
5個.py文件,2個.js文件
)。 - 提取關鍵變更,如函數或類定義。
- 提供分層摘要,包含變更類型、主要語言和范圍。
5. 健壯的錯誤處理
腳本處理多種錯誤情況,包括:
- Git命令缺失或無效。
- API請求失敗(如超時或HTTP錯誤)。
- 環境變量(如
QWEN_API
)缺失。 - 無效或空差異。
提供清晰的錯誤信息,便于開發者排查問題。
工作流程
以下是腳本的工作流程圖,使用Mermaid展示:
- 獲取差異:運行
git diff --cached
獲取暫存變更。 - 分析差異:解析文件變更、添加/刪除行數及模式。
- 摘要生成:對于大型差異,生成簡潔摘要或提取關鍵變更。
- 生成提示:根據變更類型創建上下文感知的提示。
- 調用API:將差異或摘要發送至LLM API生成提交信息。
- 執行提交:使用
git commit -m <message>
提交。
示例代碼
以下是核心代碼片段,展示如何分析差異并生成提交信息:
def analyze_diff(diff):"""分析diff內容,返回文件變更摘要"""lines = diff.split('\n')files_info = {'new_files': [],'modified_files': [],'deleted_files': [],'total_additions': 0,'total_deletions': 0}current_file = Nonefor line in lines:if line.startswith('diff --git'):match = re.search(r'a/(.*?) b/(.*?)$', line)if match:current_file = match.group(2)elif line.startswith('new file mode'):if current_file:files_info['new_files'].append(current_file)elif line.startswith('deleted file mode'):if current_file:files_info['deleted_files'].append(current_file)elif line.startswith('index') and current_file:if current_file not in files_info['new_files'] and current_file not in files_info['deleted_files']:files_info['modified_files'].append(current_file)elif line.startswith('+') and not line.startswith('+++'):files_info['total_additions'] += 1elif line.startswith('-') and not line.startswith('---'):files_info['total_deletions'] += 1return files_infodef get_commit_message(request_body):"""調用 API 生成提交信息"""headers = {"Authorization": f"Bearer {API_KEY}","Content-Type": "application/json"}try:response = requests.post(API_URL, headers=headers, json=request_body, timeout=REQUEST_TIMEOUT)response.raise_for_status()return response.json()except requests.exceptions.Timeout:print(f"[ERROR] API 請求超時 ({REQUEST_TIMEOUT} 秒)。")return None
示例使用
假設您在一個Python項目中暫存了變更并運行腳本,輸出可能如下:
[INFO] 請求體內容:
{"model": "qwen-plus-latest","temperature": 0.3,"messages": [{"role": "system","content": "你是一個專業程序員。這是新功能相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'feat:' 前綴。僅返回一行,不要解釋。"},{"role": "user","content": "新增文件: src/api.py, tests/test_api.py; +50 -0 行"}]
}[INFO] 生成的提交信息:feat: 添加用戶認證API端點
[OK] 已提交。
生成的提交信息清晰、規范,準確反映變更內容。
開始使用
依賴
- Python 3.6+。
- Git安裝并在PATH中可用。
- 兼容的LLM服務API密鑰(如阿里云Qwen)。
- 設置環境變量
QWEN_API
。
安裝
- 保存腳本為
ai_commit.py
。 - 設置API密鑰:
export QWEN_API=your_api_key
。 - 暫存變更:
git add .
。 - 運行腳本:
python ai_commit.py
。
配置
- API_URL:默認
https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
。 - MODEL_NAME:默認
qwen-plus-latest
。 - MAX_DIFF_SIZE:限制差異大小為8000字符。
- PRIORITY_FILE_EXTENSIONS:優先處理的文件類型(如
.py
、.js
)。
實用小工具
App Store 截圖生成器、應用圖標生成器 、在線圖片壓縮和 Chrome插件-強制開啟復制-護眼模式-網頁亂碼設置編碼
乖貓記賬,AI智能分類的聊天記賬。
完整代碼
# coding: utf-8
import os
import subprocess
import requests
import json
import re
from collections import defaultdict# ==================== 配置常量 ====================
API_KEY = os.environ.get("QWEN_API")
if not API_KEY:print("[ERROR] QWEN_API environment variable not set. Please set it before running the script.")exit(1)API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
MODEL_NAME = "qwen-plus-latest"
REQUEST_TIMEOUT = 15
MAX_DIFF_SIZE = 8000
MAX_FILES_FOR_DETAIL = 10# 優先分析的文件類型
PRIORITY_FILE_EXTENSIONS = ['.py', '.js', '.ts', '.jsx', '.tsx', '.vue', '.dart','.java', '.cpp', '.c', '.go', '.rs', '.php', '.rb'
]# 語言映射
LANGUAGE_MAP = {'.py': 'Python', '.js': 'JavaScript', '.ts': 'TypeScript','.jsx': 'React', '.tsx': 'TypeScript React', '.vue': 'Vue','.java': 'Java', '.cpp': 'C++', '.c': 'C', '.go': 'Go','.rs': 'Rust', '.php': 'PHP', '.rb': 'Ruby', '.dart': 'Dart'
}# ==================== Git 操作函數 ====================
def get_git_diff():"""獲取 Git staged 變更"""try:result = subprocess.run(['git', 'diff', '--cached'],stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',check=True)return result.stdoutexcept subprocess.CalledProcessError as e:print(f"[ERROR] 獲取 Git diff 失敗: {e}")if e.stderr:print(f"[ERROR] Stderr: {e.stderr.strip()}")return Noneexcept FileNotFoundError:print("[ERROR] Git 命令未找到。請確保 Git 已安裝并在您的 PATH 中。")return Nonedef git_commit(message):"""執行 Git 提交"""try:subprocess.run(['git', 'commit', '-m', message], encoding='utf-8', check=True)print("[OK] 已提交。")except subprocess.CalledProcessError as e:print(f"[ERROR] Git commit 失敗: {e}")if e.stderr:print(f"[ERROR] Stderr: {e.stderr.strip()}")if e.stdout:print(f"[ERROR] Stdout: {e.stdout.strip()}")except FileNotFoundError:print("[ERROR] Git 命令未找到。請確保 Git 已安裝并在您的 PATH 中。")# ==================== Diff 分析函數 ====================
def analyze_diff(diff):"""分析diff內容,返回文件變更摘要"""lines = diff.split('\n')files_info = {'new_files': [],'modified_files': [],'deleted_files': [],'total_additions': 0,'total_deletions': 0}current_file = Nonefor line in lines:if line.startswith('diff --git'):match = re.search(r'a/(.*?) b/(.*?)$', line)if match:current_file = match.group(2)elif line.startswith('new file mode'):if current_file:files_info['new_files'].append(current_file)elif line.startswith('deleted file mode'):if current_file:files_info['deleted_files'].append(current_file)elif line.startswith('index') and current_file:if current_file not in files_info['new_files'] and current_file not in files_info['deleted_files']:files_info['modified_files'].append(current_file)elif line.startswith('+') and not line.startswith('+++'):files_info['total_additions'] += 1elif line.startswith('-') and not line.startswith('---'):files_info['total_deletions'] += 1return files_infodef extract_key_changes(diff):"""提取diff中的關鍵變更,優先保留重要文件和函數簽名"""lines = diff.split('\n')key_sections = []current_section = []current_file = Noneis_priority_file = Falsefunction_changes = []for line in lines:if line.startswith('diff --git'):if current_section and is_priority_file:key_sections.extend(current_section[:50])current_section = [line]match = re.search(r'b/(.*?)$', line)if match:current_file = match.group(1)file_ext = os.path.splitext(current_file)[1]is_priority_file = file_ext in PRIORITY_FILE_EXTENSIONSelif line.startswith('@@'):current_section.append(line)elif is_priority_file:current_section.append(line)if line.startswith(('+', '-')):if re.search(r'(def |function |class |interface |struct |enum )', line):function_changes.append(f"{current_file}: {line.strip()}")if current_section and is_priority_file:key_sections.extend(current_section[:50])key_diff = '\n'.join(key_sections)if len(key_diff) > MAX_DIFF_SIZE:return '\n'.join(key_sections[:100] + function_changes)return key_diffdef analyze_change_patterns(files_info, diff):"""分析變更模式,識別重構、功能添加、bug修復等"""patterns = {'is_refactoring': False,'is_feature': False,'is_bugfix': False,'is_docs': False,'is_config': False,'is_test': False,'main_language': None,'change_scope': 'multiple'}all_files = files_info['new_files'] + files_info['modified_files'] + files_info['deleted_files']# 分析文件類型分布file_types = defaultdict(int)for file in all_files:ext = os.path.splitext(file)[1].lower()file_types[ext] += 1# 確定主要編程語言if file_types:main_ext = max(file_types.items(), key=lambda x: x[1])[0]patterns['main_language'] = LANGUAGE_MAP.get(main_ext, main_ext)# 分析變更類型doc_extensions = ['.md', '.txt', '.rst']config_extensions = ['.json', '.yaml', '.yml', '.toml', '.ini', '.conf']doc_files = [f for f in all_files if any(f.lower().endswith(ext) for ext in doc_extensions) or 'readme' in f.lower()]test_files = [f for f in all_files if 'test' in f.lower() or f.endswith(('_test.py', '.test.js', '.spec.js'))]config_files = [f for f in all_files if any(f.endswith(ext) for ext in config_extensions)]total_files = len(all_files)if total_files > 0:if len(doc_files) / total_files > 0.5:patterns['is_docs'] = Trueif len(test_files) > 0:patterns['is_test'] = Trueif len(config_files) / total_files > 0.3:patterns['is_config'] = True# 通過diff內容分析變更類型if diff:diff_lower = diff.lower()refactor_keywords = ['rename', 'move', 'extract', 'refactor', 'reorganize']feature_keywords = ['add', 'new', 'implement', 'feature', 'support']bugfix_keywords = ['fix', 'bug', 'error', 'issue', 'problem', 'correct']if any(keyword in diff_lower for keyword in refactor_keywords):patterns['is_refactoring'] = Trueif any(keyword in diff_lower for keyword in feature_keywords) and files_info['new_files']:patterns['is_feature'] = Trueif any(keyword in diff_lower for keyword in bugfix_keywords):patterns['is_bugfix'] = True# 確定變更范圍if len(all_files) == 1:patterns['change_scope'] = 'single'elif len(set(os.path.dirname(f) for f in all_files)) == 1:patterns['change_scope'] = 'module'else:patterns['change_scope'] = 'multiple'return patterns# ==================== 項目檢測和摘要生成 ====================
def is_new_project_init(files_info):"""檢測是否為新項目的初始提交"""total_files = len(files_info['new_files'])if (total_files >= 5 and len(files_info['modified_files']) == 0 and len(files_info['deleted_files']) == 0):new_files_str = ' '.join(files_info['new_files']).lower()project_indicators = ['readme', 'package.json', 'requirements.txt', 'cargo.toml','pom.xml', 'build.gradle', '.gitignore', 'main.', 'index.']return any(indicator in new_files_str for indicator in project_indicators)return Falsedef create_diff_summary(files_info):"""為大型diff創建摘要"""summary_parts = []if files_info['new_files']:if len(files_info['new_files']) > 5:file_types = {}for file in files_info['new_files']:ext = os.path.splitext(file)[1] or 'no_ext'file_types[ext] = file_types.get(ext, 0) + 1type_summary = ', '.join([f"{count} {ext} files" for ext, count in file_types.items()])summary_parts.append(f"新增文件: {type_summary} (共{len(files_info['new_files'])}個文件)")else:summary_parts.append(f"新增文件: {', '.join(files_info['new_files'])}")if files_info['modified_files']:if len(files_info['modified_files']) > 5:summary_parts.append(f"修改文件: {len(files_info['modified_files'])}個文件")else:summary_parts.append(f"修改文件: {', '.join(files_info['modified_files'])}")if files_info['deleted_files']:summary_parts.append(f"刪除文件: {', '.join(files_info['deleted_files'])}")summary_parts.append(f"+{files_info['total_additions']} -{files_info['total_deletions']} 行")return '; '.join(summary_parts)def create_layered_summary(files_info, patterns):"""創建分層的變更摘要"""summary_parts = []# 第一層:變更類型change_types = []type_mapping = {'is_feature': "新功能",'is_bugfix': "錯誤修復",'is_refactoring': "代碼重構",'is_docs': "文檔更新",'is_config': "配置變更",'is_test': "測試相關"}for key, label in type_mapping.items():if patterns[key]:change_types.append(label)if change_types:summary_parts.append(f"變更類型: {', '.join(change_types)}")# 第二層:主要語言和范圍if patterns['main_language']:summary_parts.append(f"主要語言: {patterns['main_language']}")summary_parts.append(f"影響范圍: {patterns['change_scope']}")# 第三層:具體文件變更file_summary = create_diff_summary(files_info)summary_parts.append(file_summary)return '\n'.join(summary_parts)# ==================== 提示詞生成 ====================
def get_context_aware_prompt(patterns, files_info):"""根據變更模式生成上下文感知的提示詞"""base_prompt = "你是一個專業程序員。"prompt_mapping = {'is_docs': f"{base_prompt}這是文檔相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'docs:' 前綴。僅返回一行,不要解釋。",'is_test': f"{base_prompt}這是測試相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'test:' 前綴。僅返回一行,不要解釋。",'is_config': f"{base_prompt}這是配置文件相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'chore:' 或 'config:' 前綴。僅返回一行,不要解釋。",'is_refactoring': f"{base_prompt}這是代碼重構相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'refactor:' 前綴。僅返回一行,不要解釋。",'is_bugfix': f"{base_prompt}這是錯誤修復相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'fix:' 前綴。僅返回一行,不要解釋。",}for pattern_type, prompt in prompt_mapping.items():if patterns[pattern_type]:return prompt# 特征檢測if patterns['is_feature'] or len(files_info['new_files']) > len(files_info['modified_files']):return f"{base_prompt}這是新功能相關的變更,請生成符合 conventional commits 規范的提交信息,使用 'feat:' 前綴。僅返回一行,不要解釋。"return f"{base_prompt}請為以下代碼變更生成一條簡潔、符合 conventional commits 規范的提交信息,僅返回一行,不要解釋。"# ==================== API 調用函數 ====================
def build_request_body(diff):"""構建API請求體"""files_info = analyze_diff(diff)total_files = len(files_info['new_files']) + len(files_info['modified_files']) + len(files_info['deleted_files'])patterns = analyze_change_patterns(files_info, diff)use_summary = len(diff) > MAX_DIFF_SIZE or total_files > MAX_FILES_FOR_DETAILif use_summary:print(f"[INFO] Diff過大({len(diff)}字符) 或文件過多({total_files}個),使用智能摘要模式")if is_new_project_init(files_info):content = f"新項目初始化提交,包含以下變更:\n{create_diff_summary(files_info)}"system_prompt = "你是一個專業程序員。這是一個新項目的初始提交,請生成一條符合 conventional commits 規范的提交信息,通常使用 'feat: ' 或 'init: ' 開頭。僅返回一行,不要解釋。"else:content = create_layered_summary(files_info, patterns)system_prompt = get_context_aware_prompt(patterns, files_info)else:if len(diff) > MAX_DIFF_SIZE // 2:print(f"[INFO] 使用智能diff提取,原始大小: {len(diff)}字符")content = extract_key_changes(diff)print(f"[INFO] 提取后大小: {len(content)}字符")else:content = diffsystem_prompt = get_context_aware_prompt(patterns, files_info)return {"model": MODEL_NAME,"temperature": 0.3,"messages": [{"role": "system", "content": system_prompt},{"role": "user", "content": content}]}def get_commit_message(request_body):"""調用 API 生成提交信息"""headers = {"Authorization": f"Bearer {API_KEY}","Content-Type": "application/json"}try:response = requests.post(API_URL, headers=headers, json=request_body, timeout=REQUEST_TIMEOUT)response.raise_for_status()return response.json()except requests.exceptions.Timeout:print(f"[ERROR] API 請求超時 ({REQUEST_TIMEOUT} 秒)。")return Noneexcept requests.exceptions.HTTPError as e:print(f"[ERROR] API 請求失敗,HTTP 狀態碼: {e.response.status_code}")try:print(f"[ERROR] API 響應內容: {e.response.text}")except Exception:passreturn Noneexcept requests.exceptions.RequestException as e:print(f"[ERROR] API 請求發生錯誤: {e}")return Noneexcept json.JSONDecodeError:print(f"[ERROR] 解碼 API 響應失敗。狀態碼: {response.status_code if 'response' in locals() else 'N/A'}")return Nonedef extract_commit_message(response_data):"""提取提交信息"""if not response_data:return Nonetry:return response_data['choices'][0]['message']['content']except (KeyError, IndexError, TypeError) as e:print(f"[ERROR] 無法提取提交信息。錯誤: {e}。響應內容:")print(json.dumps(response_data, indent=2, ensure_ascii=False))return None# ==================== 主程序 ====================
def main():"""主程序入口"""diff = get_git_diff()if not diff:if diff is not None:print("[INFO] 沒有暫存更改,請先運行 git add。")returnrequest_body = build_request_body(diff)print("[INFO] 請求體內容:")print(json.dumps(request_body, indent=2, ensure_ascii=False))api_response = get_commit_message(request_body)if not api_response:returnmessage = extract_commit_message(api_response)if not message:returnprint("\n[INFO] 生成的提交信息:")print(f" {message}")git_commit(message)if __name__ == "__main__":main()