您的位置 首页 linux 运维

git 统计每个仓库每个人的代码提交量

git 统计每个仓库每个人的代码提交量

项目总监必看:如何利用Git深度统计团队代码贡献?多语言实践教程揭秘!-腾讯云开发者社区-腾讯云 (tencent.com)

1. Git命令行工具的深度探索

通过git shortlog命令,我们可以轻松统计每个人的提交次数:

git shortlog -s -n

1.2.2 统计新增和删除行数

要统计每个人的新增和删除行数,我们可以使用以下命令:

git log --numstat --pretty="%aN" | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("新增行数: %d, 删除行数: %d\n", plus, minus)}'
1.1 基于bash的统计脚本

首先,我们使用bash脚本来实现代码统计功能。

#!/bin/bash

echo "统计代码提交情况:"

# 获取所有贡献者列表
authors=$(git log --format='%aN' | sort -u)

for author in $authors; do
    echo "----------------------------------------"
    echo "作者:$author"

    # 统计提交次数
    commit_count=$(git shortlog -s -n --author="$author" | awk '{print $1}')
    echo "提交次数:$commit_count"

    # 统计新增和删除行数
    line_stat=$(git log --numstat --pretty="%aN" --author="$author" | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("%d %d", plus, minus)}')
    IFS=' ' read -ra stats <<< "$line_stat"
    echo "新增行数:${stats[0]}"
    echo "删除行数:${stats[1]}"
done

需要把脚本放到 项目路径下执行。

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

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章