-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgencmt.py
258 lines (216 loc) · 8.12 KB
/
gencmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import subprocess
import json
import requests
import os
import typer
from dotenv import load_dotenv
app = typer.Typer()
load_dotenv()
gemini_key = os.getenv("GEMINI_KEY")
proxy_port = os.getenv("PROXY_PORT")
proxy_user = os.getenv("PROXY_USER")
proxy_pass = os.getenv("PROXY_PASS")
proxy_ip = os.getenv("PROXY_IP")
def get_git_diff():
# 获取当前工作目录
current_directory = os.getcwd()
try:
# 执行 git diff 命令并获取输出,cwd指定工作目录
result = subprocess.run(
['git', 'diff', 'HEAD'],
capture_output=True,
text=True,
check=True,
cwd=current_directory # 确保在当前目录下执行
)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Error while executing git diff: {e}")
return None
# 渲染模板
def render_template(diff):
template = '''
你的任务是产生一个遵循 Conventional Commits 规范的 git commit message,使用中文的流畅语言和 emoji。 Conventional Commits 规范是一种轻量级的 commit message 约定,用于创建明确的提交历史记录。
以下是要提交的变更的 git diff:
<diff>
{{diff}}
</diff>
仔细分析 diff 以理解所做的更改。请注意:
- 被修改、新增或删除的文件
- 更改的性质(例如:错误修复、新功能、重构等)
- 任何破坏性更改
根据你的分析,请按照以下步骤产生 commit message:
1. 确定适当的提交类型。常见类型包括:
- 🎉 init: 初始化
- 🚀 release: 发布新版本
- 🎨 style: 代码风格修改(不影响代码运行的变动)
- ✨ feat: 添加新功能
- 🐛 fix: 修复 bug
- 📝 docs: 对文档进行修改
- ♻️ refactor: 代码重构(既不是新增功能,也不是修改 bug 的代码变动)
- ⚡ perf: 提高性能的代码修改
- 🧑💻 dx: 优化开发体验
- 🔨 workflow: 工作流变动
- 🏷️ types: 类型声明修改
- 🚧 wip: 工作正在进行中
- ✅ test: 测试用例添加及修改
- 🔨 build: 影响构建系统或外部依赖关系的更改
- 👷 ci: 更改 CI 配置文件和脚本
- ❓ chore: 其它不涉及源码以及测试的修改
- ⬆️ deps: 依赖项修改
- 🔖 release: 发布新版本
2. 如果提交引入了破坏性更改,在类型/范围后面加上感叹号。
3. 在主题行中提供一个简短的、祈使语气的更改描述。
4. 如果需要,在提交正文中添加更详细的更改说明,解释更改的动机并与先前的行为进行对比。
5. 如果有破坏性更改,请在提交正文的末尾描述它们,以"破坏性更改:"开头。
按以下格式输出你的回应:
<commit_message>
[emoji][类型][可选范围]: [描述]
[可选正文]
[可选页脚]
</commit_message>
确保 commit message 遵守以下规则:
- 主题行不应超过 50 个字符
- 正文应在 72 个字元处换行
- 在主题行中使用祈使句和现在式
- 主题行结尾不要加句号
- 用空白行将主题与正文分开
- 使用正文解释做了什么和为什么,而不是如何做
记住,一个好的 commit message 应该能够完成以下句子:"如果应用,这个提交将 [你的主题行]"。
请确保使用中文的流畅语言编写 commit message,并在适当的地方加入 emoji 以增强可读性和表达力。
'''
# 使用 Python 的 string replace 方法来渲染模板
return template.replace('{{diff}}', diff)
# 调用 API
def call_api_aisino(content):
url = 'http://172.16.21.190:8060/v1/chat/completions'
headers = {
'Content-Type': 'application/json'
}
payload = {
"model": "Default",
"strategy": "Bare",
"messages": [
{
"role": "user",
"content": content
}
],
"stream": "false",
"trace_id": "123",
"max_new_tokens": 2000,
"super_search": True
}
try:
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
# print("API Response:", response.json())
# 提取并打印返回内容中的 'content' 部分
content_response = response.json().get('choices', [{}])[0].get('message', {}).get('content', '')
print(content_response)
else:
print(f"Failed to call API. Status Code: {response.status_code}")
except Exception as e:
print(f"Error while calling the API: {e}")
def call_api_gemini(content):
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
headers = {
"Content-Type": "application/json"
}
payload = {
"contents": [
{
"parts": [
{
"text": content
}
]
}
]
}
# 使用你的 API 密钥
api_key = gemini_key
params = {"key": api_key}
if proxy_user and proxy_pass:
proxies = {
'http': f'socks5://{proxy_user}:{proxy_pass}@{proxy_ip}:{proxy_port}',
'https': f'socks5://{proxy_user}:{proxy_pass}@{proxy_ip}:{proxy_port}'
}
else:
proxies = {
'http': f'socks5://{proxy_ip}:{proxy_port}',
'https': f'socks5://{proxy_ip}:{proxy_port}'
}
# print(proxies)
try:
# 发送请求
response = requests.post(url, headers=headers, json=payload, params=params, proxies=proxies)
if response.status_code == 200:
# 打印响应内容
content_response = response.json()
# print("API Response Content:\n", content_response)
res_txt = content_response['candidates'][0]['content']['parts'][0]['text']
print(res_txt)
else:
print(f"Failed to call API. Status Code: {response.status_code}")
except Exception as e:
print(f"Error while calling the API: {e}")
# def call_api(content):
# # call_api_gemini(content)
# call_api_aisino(content)
@app.command('g', help='gen commit,type can be aisino or gemini')
def gencmt(type: str='aisino'):
# 获取 git diff 输出
diff = get_git_diff()
if diff:
print("👨🍳 get commit,be waiting!")
# 渲染模板
content = render_template(diff)
# print("Rendered content:\n", content)
# 调用 API
if type == 'aisino' or type == 'a':
call_api_aisino(content)
elif type == 'gemini' or type == 'g':
call_api_gemini(content)
else:
print(f"wrong type: {type},try aisino")
call_api_aisino(content)
else:
print("🤗 no diff, you can make `git add somefile`")
# vim /usr/local/bin/git-gencmt
# python3 /home/ys/workspace/python/EffTools/gencmt.py g --type g
# chmod +x /usr/local/bin/git-gencmt
@app.command('env', help='extract example env')
def create_example_env(file_path='.env'):
# 目标 .env.example 文件路径
example_file_path = '.env.example'
# 检查 .env 文件是否存在
if not os.path.exists(file_path):
print(f"Error: {file_path} does not exist.")
return
# 打开 .env 文件读取内容
with open(file_path, 'r') as f:
lines = f.readlines()
# 处理文件内容,替换每行的 value 部分为 'VALUE'
modified_lines = []
for line in lines:
# 忽略空行和注释行
if line.strip() == '' or line.strip().startswith('#'):
modified_lines.append(line)
else:
# 分离 key 和 value
parts = line.split('=')
if len(parts) == 2:
key = parts[0]
modified_line = f"{key}=VALUE\n"
modified_lines.append(modified_line)
else:
# 如果格式不对,保留原始行
modified_lines.append(line)
# 将修改后的内容写入 .env.example 文件
with open(example_file_path, 'w') as f:
f.writelines(modified_lines)
print(f".env.example file created with keys replaced by 'VALUE'.")
if __name__ == "__main__":
# main()
app()