在git中可以使用--pretty=format命令修饰日志:
# --pretty=format:"xxx" 自定义的输出格式
# --date="format:%Y%m%d" 自定义的日期格式
# -1 显示的commit次数
git log --pretty=format:"%h %cd %s" --date="format:%Y%m%d" -1
git log --pretty=format:"{\"id\": \"%h\", \"data\": \"%cd\", \"message\": \"%s\"}" --date="format:%Y%m%d" -1
常用的格式如下:
格式说明**%H输出commit id(完整)%h输出commit id(前10位)%s输出commit message(git commit -m "xxx"中的内容)%cd输出commit date(日期+时间)date='format:%Y%m%d'**
设置%cd的输出格式:
年份:%Y
月份:%m
天数:%d
小时:%H
分钟:%M
秒数:%S
Python的git库对--pretty=format命令进行了封装:
import json
import git
# 获取commit信息 # {"commit":"abcde12345","date":"20230414","summary":"xxx"}
repo = git.Repo('/home/Project/test/src')
commit_dict = json.loads(repo.git.log('--pretty=format:{"commit":"%h", "date":"%cd", "summary":"%s"}', date='format:%Y%m%d', max_count=1))
print(commit_dict)
除此之外,也可以使用commit方法获取信息:
import json
import git
# 获取当前项目最后一次递交的commit信息
repo = git.Repo('/home/Project/test/src')
message = repo.commit().message
date = repo.commit().committed_datetime
date_str = datetime.datetime.strftime(date, '%Y%m%d')
# 获取指定id的commit信息
commit_id = 'abcde12345'
message = repo.commit(commit_id).message
date = repo.commit(commit_id).committed_datetime
date_str = datetime.datetime.strftime(date, '%Y%m%d')
版权归原作者 三采 所有, 如有侵权,请联系我们删除。