record_commit.py•3.74 kB
#!/usr/bin/env python3
"""
Git Commit记录脚本
自动记录Git commit信息到MemOS的git_commit_mem
"""
import sys
import os
from pathlib import Path
from datetime import datetime
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
try:
# 使用SimpleMemoryOps,现在支持持久化存储
from simple_memory_ops_sdk import SimpleMemoryOps as MemoryOps
except ImportError:
try:
from memory_ops_sdk import MemoryOps
except ImportError:
print("Error: Cannot import memory ops SDK", file=sys.stderr)
sys.exit(1)
def record_commit(commit_hash, commit_msg, commit_author, commit_email, commit_date, changed_files):
"""
记录Git commit信息到git_commit_mem
Args:
commit_hash: commit哈希值
commit_msg: commit消息
commit_author: 作者姓名
commit_email: 作者邮箱
commit_date: commit日期
changed_files: 修改的文件列表(逗号分隔)
"""
try:
# 初始化Memory Ops SDK
mem = MemoryOps(verbose=False)
# 构造记忆内容
memory_content = f"""Git Commit: {commit_hash[:8]}
Author: {commit_author} <{commit_email}>
Date: {commit_date}
Message: {commit_msg.strip()}
Changed Files: {changed_files}"""
# 构造元数据
metadata = {
"type": "event",
"source": "git",
"commit_hash": commit_hash,
"commit_author": commit_author,
"commit_email": commit_email,
"commit_date": commit_date,
"changed_files": changed_files.split(',') if changed_files else [],
"timestamp": datetime.now().isoformat()
}
# 构造标签
tags = ["git", "commit", "development"]
if changed_files:
# 根据文件类型添加标签
files = changed_files.split(',')
for file in files:
if file.endswith('.py'):
tags.append("python")
elif file.endswith(('.js', '.ts')):
tags.append("javascript")
elif file.endswith(('.md', '.txt')):
tags.append("documentation")
elif file.endswith(('.json', '.yaml', '.yml')):
tags.append("configuration")
# 检查是否有git_commit_mem类型
available_types = mem.list_memory_types() if hasattr(mem, 'list_memory_types') else {}
memory_type = 'git_commit_mem' if 'git_commit_mem' in available_types else 'general_mem'
# 添加记忆
success = mem.add(
text=memory_content,
memory_type=memory_type,
tags=tags,
metadata=metadata
)
if success:
print(f"✅ Commit {commit_hash[:8]} recorded to {memory_type}")
else:
print(f"❌ Failed to record commit {commit_hash[:8]}")
# 关闭连接(如果支持)
if hasattr(mem, 'close'):
mem.close()
except Exception as e:
print(f"Error recording commit: {e}", file=sys.stderr)
def main():
"""主函数"""
if len(sys.argv) != 7:
print("Usage: record_commit.py <commit_hash> <commit_msg> <author> <email> <date> <changed_files>")
sys.exit(1)
commit_hash = sys.argv[1]
commit_msg = sys.argv[2]
commit_author = sys.argv[3]
commit_email = sys.argv[4]
commit_date = sys.argv[5]
changed_files = sys.argv[6]
record_commit(commit_hash, commit_msg, commit_author, commit_email, commit_date, changed_files)
if __name__ == "__main__":
main()