假定需求是分析千行代码bug率,就需要知道代码分支在一段时间内变化情况,然后根据bug数量进行计算。这里记录下获取代码行数变化的命令
# 首先准备好查询开始时间(东八区)
start_date = datetime.datetime.now() - datetime.timedelta(days=latest_days)
epoch = datetime.datetime.utcfromtimestamp(0)
seconds = (start_date - epoch).total_seconds()
asc_time = time.asctime(time.localtime(seconds))
asc_time += " +0800" # "Fri Feb 28 05:17:03 2020 +0800"
# 这里省略了切换代码分支并拉取最新代码的命令
cmd_cd = f'cd {self.code_path}'
# 这里是获取代码分支上从某个时间点开始的commit记录 倒序
cmd_rev_list = f'git rev-list -n 30000 --reverse --since="{asc_time}" master'
rev_list_ret = os.popen(f"{cmd_cd} && {cmd_rev_list}").read()
# 提取commit id
commit_id = rev_list_ret.split('\n')[0]
# 此命令可以获取两个commit之间的代码变化情况
cmd_diff_stat = f'git diff {commit_id} HEAD --stat'
lines_info = os.popen(f"{cmd_cd} && {cmd_diff_stat}").read()
line_info = lines_info.split('\n')[-2:][0]
print(line_info)
# 只提取新增代码行(未处理删除、修改)
return int(re.search(", (\d+) insertions", line_info).groups()[0])
结果截图
image.png
网友评论