JSON文件没有message,需要通过commitID查询message信息,写一段Python脚本
时间: 2024-05-01 20:18:26 浏览: 184
可以通过GitPython库来实现:
```python
from git import Repo
# 通过commit ID获取message信息
def get_commit_msg(commit_id):
repo = Repo() # 本地git仓库路径
commit = repo.commit(commit_id)
return commit.message.strip()
# JSON文件中没有message,通过commit ID查询message信息
def get_msg_from_json(json_file, commit_id):
import json
with open(json_file, 'r') as f:
data = json.load(f)
if "message" not in data:
return get_commit_msg(commit_id)
else:
return data["message"]
```
使用方法:
```python
msg = get_msg_from_json('test.json', '4fcce9bc5a5f5cbbf5d5b71a7670ca56132ef1f7')
print(msg)
```
其中,`test.json`是JSON文件路径,`4fcce9bc5a5f5cbbf5d5b71a7670ca56132ef1f7`是commit ID。如果JSON文件中有`message`字段,则直接返回该字段的值;否则通过commit ID查询message信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)