fix: 优化`commit`数据统计分析的逻辑;

Description:

Log:
This commit is contained in:
mikigo 2024-03-22 11:39:50 +08:00
parent c46db0d4ac
commit 03efd9752d
2 changed files with 40 additions and 84 deletions

View File

@ -10,6 +10,7 @@ from datetime import datetime
from setting import conf from setting import conf
from src.rtk._base import transform_app_name from src.rtk._base import transform_app_name
from src.git.commit import Commit from src.git.commit import Commit
from src.depends.colorama import Fore
class CodeStatistics(Commit): class CodeStatistics(Commit):
@ -35,9 +36,9 @@ class CodeStatistics(Commit):
for i in range(100): for i in range(100):
self.ignore_txt.append(" " * i + "#") self.ignore_txt.append(" " * i + "#")
def get_git_files(self, end_commit_id, max_width=10000): def get_git_files(self, commit_id, max_width=10000):
git_files = [] git_files = []
_files = os.popen(f"cd {self.repo_path};git show --stat={max_width} {end_commit_id}").read() _files = os.popen(f"cd {self.repo_path};git show --stat={max_width} {commit_id}").read()
files = _files.split("\n") files = _files.split("\n")
for file_info in files: for file_info in files:
if "| " not in file_info: if "| " not in file_info:
@ -47,7 +48,7 @@ class CodeStatistics(Commit):
git_files.append(file_path) git_files.append(file_path)
return git_files return git_files
def compare_files(self, start_commit_id, end_commit_id, author, git_dt: datetime): def compare_files(self, commit_id, author, git_dt: datetime):
_fix_debug = [] _fix_debug = []
_new_debug = [] _new_debug = []
new_test_case_num = 0 new_test_case_num = 0
@ -56,61 +57,29 @@ class CodeStatistics(Commit):
new_method_num = 0 new_method_num = 0
del_method_num = 0 del_method_num = 0
fix_method_num = 0 fix_method_num = 0
git_files = self.get_git_files(end_commit_id=end_commit_id) git_files = self.get_git_files(commit_id=commit_id)
for filepath in git_files: for filepath in git_files:
filename = filepath.split("/")[-1] filename = filepath.split("/")[-1]
print("filepath:", filepath, "\n") dif_texts = os.popen(f"cd {self.repo_path}/;git show {commit_id} {filepath}").read()
dif_txt = os.popen(f"cd {self.repo_path}/;git diff {start_commit_id}..{end_commit_id} {filepath}").read() print(Fore.GREEN, "=" * 100, Fore.RESET)
print("=" * 100) print(filepath, "\n", dif_texts)
dif_lines = dif_texts.splitlines()
# case # case
if filename.startswith("test_"): if filename.startswith("test_"):
_contents = dif_txt.splitlines() for line in dif_lines:
contents = _contents[6:] if line.startswith("--- /dev/null"):
added = reduced = unchanged = False new_test_case_num += 1
for line in contents: break
if line.startswith("+"): elif line.startswith("+++ /dev/null"):
added = True del_test_case_num += 1
elif line.startswith("-"): break
reduced = True else:
else:
if line.startswith(tuple(self.ignore_txt)):
continue
unchanged = True
if all(
[
added is True,
reduced is False,
unchanged is False,
]
):
new_test_case_num += 1
if all(
[
added is False,
reduced is True,
unchanged is False,
]
):
del_test_case_num += 1
if all(
[
any(
[
added is True,
reduced is True,
]
),
unchanged is True,
]
):
fix_test_case_num += 1 fix_test_case_num += 1
# method # method
else: else:
print(dif_txt)
methods = [] methods = []
method_info = {} method_info = {}
for line in dif_txt.splitlines(): for line in dif_lines:
if line.startswith(("---", "+++", "@@", "@")): if line.startswith(("---", "+++", "@@", "@")):
continue continue
if "def " in line: if "def " in line:
@ -155,8 +124,7 @@ class CodeStatistics(Commit):
break break
res = { res = {
"start_commit_id": start_commit_id, "commit_id": commit_id,
"end_commit_id": end_commit_id,
"author": author, "author": author,
"git_dt": git_dt.strftime("%Y-%m-%d"), "git_dt": git_dt.strftime("%Y-%m-%d"),
"branch": self.branch, "branch": self.branch,
@ -174,14 +142,14 @@ class CodeStatistics(Commit):
os.makedirs(conf.REPORT_PATH) os.makedirs(conf.REPORT_PATH)
result_file = os.path.join( result_file = os.path.join(
conf.REPORT_PATH, conf.REPORT_PATH,
f"{self.app_name}_git_compare_result{f'_detail' if detail else ''}.json", f"{self.app_name}_git_commit_result{f'_detail' if detail else ''}.json",
) )
with open(result_file, "w", encoding="utf-8") as f: with open(result_file, "w", encoding="utf-8") as f:
f.write(json.dumps(res, ensure_ascii=False, indent=4, default=None)) f.write(json.dumps(res, ensure_ascii=False, indent=4, default=None))
with open(result_file, "r", encoding="utf-8") as f: with open(result_file, "r", encoding="utf-8") as f:
print(f.read()) print(f.read())
print(f"数据结果报告:{result_file}") print(f"{Fore.GREEN}数据结果{'详细' if detail else '汇总'}报告:{result_file}{Fore.RESET}")
def codex(self): def codex(self):
results = None results = None
@ -189,10 +157,8 @@ class CodeStatistics(Commit):
if self.startdate: if self.startdate:
commit_id_pairs = self.commit_id() commit_id_pairs = self.commit_id()
results = {} results = {}
for i, (_start_commit_id, _end_commit_id, _author, git_dt) in enumerate( for i, (commit_id, _author, git_dt) in enumerate(commit_id_pairs):
commit_id_pairs _res = self.compare_files(commit_id, _author, git_dt)
):
_res = self.compare_files(_start_commit_id, _end_commit_id, _author, git_dt)
res = deepcopy(_res) res = deepcopy(_res)
results_detail.append(_res) results_detail.append(_res)
author = res["author"] author = res["author"]
@ -202,7 +168,8 @@ class CodeStatistics(Commit):
new_method_num = res["新增方法"] new_method_num = res["新增方法"]
del_method_num = res["删除方法"] del_method_num = res["删除方法"]
fix_method_num = res["修改方法"] fix_method_num = res["修改方法"]
commit_new = res["end_commit_id"] commit_id = res["commit_id"]
_git_dt = res["git_dt"]
if results.get(author) is None: if results.get(author) is None:
results[author] = res results[author] = res
else: else:
@ -212,20 +179,22 @@ class CodeStatistics(Commit):
results[author]["新增方法"] += new_method_num results[author]["新增方法"] += new_method_num
results[author]["删除方法"] += del_method_num results[author]["删除方法"] += del_method_num
results[author]["修改方法"] += fix_method_num results[author]["修改方法"] += fix_method_num
results[author]["end_commit_id"] = commit_new results[author]["commit_id"] = commit_id
results[author]["git_dt_end"] = _git_dt
if results is None: if results is None:
raise ValueError() raise ValueError()
print(Fore.GREEN, "=" * 100, Fore.RESET)
self.write_result(results) self.write_result(results)
if results_detail: if results_detail:
self.write_result(results_detail, detail=True) self.write_result(results_detail, detail=True)
if __name__ == "__main__": if __name__ == "__main__":
app_name = "apps/autotest_public" app_name = "apps/autotest_deepin_kwin_UI"
CodeStatistics( CodeStatistics(
app_name=app_name, app_name=app_name,
branch="master", branch="at-develop/eagle",
startdate="2024-03-04", startdate="2024-01-01",
# enddate="2024-02-23", # enddate="2024-02-23",
).codex() ).codex()

View File

@ -36,11 +36,6 @@ class Commit:
@property @property
def git_logs(self) -> list: def git_logs(self) -> list:
# git_logs = re.findall(
# r"commit (.*?)\nAuthor: (.*?)\nDate: (.*?)\n",
# os.popen(f"cd {conf.APPS_PATH}/{self.app_name} && git log {self.branch}").read(),
# )
# return git_logs
_git_logs = ( _git_logs = (
os.popen(f"cd {conf.APPS_PATH}/{self.app_name} && git log {self.branch}") os.popen(f"cd {conf.APPS_PATH}/{self.app_name} && git log {self.branch}")
.read() .read()
@ -50,42 +45,34 @@ class Commit:
tmp = [] tmp = []
for line in _git_logs: for line in _git_logs:
if line.startswith("commit "): if line.startswith("commit "):
if tmp:
git_logs.append(tmp)
tmp = []
tmp.append(line.split(" ")[1].strip()) tmp.append(line.split(" ")[1].strip())
elif line.startswith("Author:"): elif line.startswith("Author:"):
tmp.append(line.split("Author: ")[-1]) tmp.append(line.split("Author: ")[-1])
elif line.startswith("Date: "): elif line.startswith("Date: "):
tmp.append(line.split("Date: ")[1].strip()) tmp.append(line.split("Date: ")[1].strip())
git_logs.append(tmp)
tmp = []
return git_logs return git_logs
def commit_id(self): def commit_id(self):
commit_ids = deque() commit_ids = deque()
flag = False
for commit_id, author, _time_str in self.git_logs: for commit_id, author, _time_str in self.git_logs:
time_str = " ".join(_time_str.split(" ")[:-1]) time_str = " ".join(_time_str.split(" ")[:-1])
git_dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %Y") git_dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %Y")
if self.startdate <= git_dt <= self.enddate: if self.startdate <= git_dt <= self.enddate:
commit_ids.appendleft([commit_id, author, git_dt]) commit_ids.appendleft([commit_id, author, git_dt])
elif git_dt < self.startdate:
if flag is False:
commit_ids.appendleft([commit_id, None, None])
flag = True
if commit_ids: if not commit_ids:
commit_id_pairs = [ raise ValueError(f"{self.startdate}{self.enddate} 没有获取到有效的 commit id")
[commit_ids[i][0], commit_ids[i + 1][0], commit_ids[i + 1][1], commit_ids[i + 1][2]]
for i in range(len(commit_ids) - 1)
]
return commit_id_pairs
raise ValueError(f"{self.startdate}{self.enddate} 没有获取到有效的 commit id") return commit_ids
if __name__ == "__main__": if __name__ == "__main__":
Commit( Commit(
app_name="apps/autotest_deepin_downloader", app_name="apps/autotest_deepin_kwin_UI",
branch="master", branch="at-develop/eagle",
startdate="2024-02-27", startdate="2024-03-20",
).commit_id() ).commit_id()