git 统计每个仓库每个人的代码提交量
项目总监必看:如何利用Git深度统计团队代码贡献?多语言实践教程揭秘!-腾讯云开发者社区-腾讯云 (tencent.com)
1. Git命令行工具的深度探索
通过git shortlog命令,我们可以轻松统计每个人的提交次数:
git shortlog -s -n
1.2.2 统计新增和删除行数
要统计每个人的新增和删除行数,我们可以使用以下命令:
需要把脚本放到 项目路径下执行。
3. Python实现
Python也可以轻松地调用子进程。我们可以使用subprocess
模块来实现。
以下是使用Python实现统计Git代码提交情况的完整代码:
import subprocess
def git_stats(repo_path):
# 获取所有贡献者
cmd_authors = ["git", "log", "--format='%aN'", "--no-merges"]
authors_output = subprocess.Popen(cmd_authors, cwd=repo_path, stdout=subprocess.PIPE).communicate()[0].decode()
unique_authors = set(authors_output.splitlines())
stats = {}
for author in unique_authors:
# 统计每个贡献者的提交次数
cmd_commits = ["git", "shortlog", "-s", "-n", "--author=" + author]
commits_output = subprocess.Popen(cmd_commits, cwd=repo_path, stdout=subprocess.PIPE).communicate()[0].decode()
commit_count = int(commits_output.strip().split()[0])
# 统计每个贡献者的新增和删除行数
cmd_lines = ["git", "log", "--numstat", "--author=" + author, "--pretty=tformat:", "--no-merges"]
lines_output = subprocess.Popen(cmd_lines, cwd=repo_path, stdout=subprocess.PIPE).communicate()[0].decode()
added, deleted = 0, 0
for line in lines_output.splitlines():
if '\t' in line:
a, d, _ = line.split('\t')
added += int(a)
deleted += int(d)
stats[author] = {"commits": commit_count, "added": added, "deleted": deleted}
return stats
if __name__ == "__main__":
repo = "/path/to/repo" # 修改为你的仓库路径
statistics = git_stats(repo)
for author, data in statistics.items():
print(f"Author: {author}")
print(f"Commits: {data['commits']}")
print(f"Added lines: {data['added']}")
print(f"Deleted lines: {data['deleted']}")
print("-------------------------")
欢迎来撩 : 汇总all