|
@@ -5,6 +5,19 @@ import os
|
|
|
import sys
|
|
|
from collections import defaultdict
|
|
|
|
|
|
+def get_current_branch():
|
|
|
+ try:
|
|
|
+ result = subprocess.run(
|
|
|
+ ["git", "branch", "--show-current"],
|
|
|
+ capture_output=True,
|
|
|
+ text=True,
|
|
|
+ check=True
|
|
|
+ )
|
|
|
+ return result.stdout.strip()
|
|
|
+ except subprocess.CalledProcessError:
|
|
|
+ return None # 非Git仓库或命令失败
|
|
|
+
|
|
|
+
|
|
|
def get_git_root(path="."):
|
|
|
"""获取当前目录所在的Git仓库根目录"""
|
|
|
try:
|
|
@@ -49,14 +62,17 @@ def count_changes_in_commit(repo_path, commit_hash):
|
|
|
|
|
|
insertions = 0
|
|
|
deletions = 0
|
|
|
+ totals=0
|
|
|
for line in result.stdout.strip().split('\n'):
|
|
|
parts = line.split('\t')
|
|
|
+
|
|
|
if len(parts) == 3:
|
|
|
- ins, dels, _ = parts
|
|
|
+ ins, dels, total = parts
|
|
|
insertions += int(ins) if ins != '-' else 0
|
|
|
deletions += int(dels) if dels != '-' else 0
|
|
|
+ totals += 1
|
|
|
|
|
|
- return insertions, deletions
|
|
|
+ return insertions, deletions,totals
|
|
|
except subprocess.CalledProcessError:
|
|
|
return 0, 0
|
|
|
|
|
@@ -93,6 +109,7 @@ def main():
|
|
|
# 获取Git仓库根目录
|
|
|
repo_path = get_git_root()
|
|
|
repo_name = os.path.basename(repo_path)
|
|
|
+ branch_name = get_current_branch()
|
|
|
|
|
|
# 获取今日提交
|
|
|
commits = get_today_commits(repo_path)
|
|
@@ -103,13 +120,15 @@ def main():
|
|
|
# 统计总变动
|
|
|
total_insertions = 0
|
|
|
total_deletions = 0
|
|
|
+ total_files = 0
|
|
|
filetype_stats = defaultdict(lambda: {"insertions": 0, "deletions": 0})
|
|
|
|
|
|
for commit in commits:
|
|
|
# 统计总变动
|
|
|
- ins, dels = count_changes_in_commit(repo_path, commit)
|
|
|
+ ins, dels, totals= count_changes_in_commit(repo_path, commit)
|
|
|
total_insertions += ins
|
|
|
total_deletions += dels
|
|
|
+ total_files += totals
|
|
|
|
|
|
# 按文件类型统计
|
|
|
commit_filetype_stats = count_changes_by_filetype(repo_path, commit)
|
|
@@ -118,12 +137,13 @@ def main():
|
|
|
filetype_stats[ext]["deletions"] += stats["deletions"]
|
|
|
|
|
|
# 打印结果
|
|
|
- print(f"Git仓库代码变动统计 - {repo_name}")
|
|
|
+ print(f"Git仓库代码变动统计-{repo_name}-{branch_name}")
|
|
|
print(f"日期: {datetime.date.today()}")
|
|
|
print(f"提交数: {len(commits)}")
|
|
|
- print(f"总插入行数: {total_insertions}")
|
|
|
- print(f"总删除行数: {total_deletions}")
|
|
|
- print(f"净变动行数: {total_insertions - total_deletions}")
|
|
|
+ print(f"总行数: {total_insertions}")
|
|
|
+ print(f"总文件数: {total_files}")
|
|
|
+ # print(f"总删除行数: {total_deletions}")
|
|
|
+ # print(f"净变动行数: {total_insertions - total_deletions}")
|
|
|
print("\n按文件类型统计:")
|
|
|
print("{:<10} {:>12} {:>12} {:>12}".format("类型", "插入", "删除", "净变动"))
|
|
|
print("-" * 48)
|