Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (81 loc) · 3.69 KB
/
Copy pathmain.py
File metadata and controls
100 lines (81 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import pandas as pd
import matplotlib.pyplot as plt
from git import Repo
from datetime import datetime
class GitAnalyzer:
def __init__(self, repo_path):
self.repo = Repo(repo_path)
if self.repo.bare:
raise Exception("仓库路径无效")
def get_contribution_data(self):
"""分析各作者的贡献量 (增加与删除的代码行数)"""
stats = []
for commit in self.repo.iter_commits():
stats.append({
'作者': commit.author.name,
'日期': datetime.fromtimestamp(commit.committed_date),
'新增行数': commit.stats.total['insertions'],
'删除行数': commit.stats.total['deletions']
})
return pd.DataFrame(stats)
def get_branch_activity(self):
"""获取各分支的最后提交时间及活跃度"""
branch_info = []
for branch in self.repo.branches:
latest_commit = branch.commit
branch_info.append({
'分支名称': branch.name,
'最后提交时间': datetime.fromtimestamp(latest_commit.committed_date),
'最后提交者': latest_commit.author.name
})
return pd.DataFrame(branch_info)
def plot_analysis(self, df_contrib, freq='D'):
"""
生成可视化图表
freq: 'ME' 代表按月, 'D' 代表按天
"""
# --- 中文字体设置 ---
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
# 创建画布:1行2列
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 7))
# 1. 作者总贡献量对比图 (柱状图)
contrib_summary = df_contrib.groupby('作者')[['新增行数', '删除行数']].sum()
contrib_summary.plot(kind='bar', stacked=True, ax=ax1, color=['#2ca02c', '#d62728'])
ax1.set_title('各作者累计贡献量', fontsize=14)
ax1.set_ylabel('代码行数')
ax1.grid(axis='y', linestyle='--', alpha=0.5)
# 2. 各开发者代码新增趋势 (多折线图)
df_trend = df_contrib.copy()
# 将日期设置为索引并按频率和作者分组
# 我们先按时间频率和作者同时分组,统计新增行数
trend_data = df_trend.groupby([pd.Grouper(key='日期', freq=freq), '作者'])['新增行数'].sum().unstack(
level=1).fillna(0)
# 添加一条“总计”线
trend_data['项目总计'] = trend_data.sum(axis=1)
# 绘图
label_text = "月度" if freq == 'ME' else "日间"
# 使用 colormap 让不同作者颜色更易区分
trend_data.plot(kind='line', ax=ax2, marker='o', markersize=4, linewidth=2)
ax2.set_title(f'各开发者代码新增趋势 ({label_text})', fontsize=14)
ax2.set_xlabel('日期')
ax2.set_ylabel('新增行数')
ax2.legend(title='开发者', bbox_to_anchor=(1.05, 1), loc='upper left') # 标签放在图外防止遮挡
ax2.grid(True, linestyle=':', alpha=0.6)
plt.tight_layout()
plt.show()
# 使用示例
if __name__ == "__main__":
# 你的仓库路径
path_to_repo = r"D:\王骏诚开发专用\抗滑桩设计平台\四期开发\GeoPile"
try:
analyzer = GitAnalyzer(path_to_repo)
df_c = analyzer.get_contribution_data()
# 打印一下基本数据方便校对
print("\n--- 分支情况 ---")
print(analyzer.get_branch_activity().to_string(index=False))
# 绘图:freq='D' 是按天,freq='ME' 是按月
analyzer.plot_analysis(df_c, freq='D')
except Exception as e:
print(f"执行出错: {e}")