git 代码统计

#!/bin/bash
set -e

# Git project URL
git_proj_url='https://demo.com.cn/scm/demo'
# Branch name to checkout
branch_name="feature-dev"

# Array of repositories to process
repos=(
"repodemo1"
"repodemo2"
)

# Associative array mapping English author names to Chinese names
declare -A authors_map=(
    ["ceshi"]="测试"
)

# Get the current date in YYYY-MM-DD format
day_time=$(date +'%Y-%m-%d')

# Function to process a single repository
function process_repo() {
    local repo=$1
    local repo_url="$git_proj_url/$repo.git"
    
    # Check if the repository directory exists, if not, clone it
    if [ ! -d "$repo" ]; then
        git clone "$repo_url" "$repo" || { echo "Failed to clone $repo"; exit 1; }
    fi
    
    pushd "$repo" > /dev/null
    
    # Checkout the specified branch
    git checkout "$branch_name"  >/dev/null || { echo "Failed to checkout branch $branch_name in $repo"; popd > /dev/null; rm -rf "$repo"; exit 1; }

    # Iterate over each author in the authors_map
    for key in "${!authors_map[@]}"; do
        authors_en=$key
        authors_cn=${authors_map[$key]}
        echo -en "[$day_time],[$repo],$authors_cn,"
        # Get the commit statistics for the author for the current day
        git log --since="$day_time 00:00:00" --until="$day_time 23:59:59" --author="$authors_en" --pretty=tformat: --numstat | awk 'BEGIN { add=0; subs=0; loc=0 }{ add += $1; subs += $2; loc += $1 - $2 } END { printf "%s,%s,%s\n", add, subs, loc }'
    done
    
    popd > /dev/null
    rm -rf "$repo"
}

# Trap to handle script interruption
trap 'echo "Script interrupted, cleaning up..."; exit 1' INT TERM

# Create the header for the repository summary CSV file
echo "统计时间,仓库名称,提交用户,提交行数,删除行数,总行数" > git_repos_summary_${day_time}.csv
# Process each repository and append the results to the repository summary CSV file
for repo in "${repos[@]}"; do
    process_repo "$repo" >> git_repos_summary_${day_time}.csv
done

# Read the content of the repository summary CSV file
file_content=$(<"git_repos_summary_${day_time}.csv")

# Create the header for the author summary CSV file
echo "统计时间,提交用户,提交行数,删除行数,总行数" > git_authors_summary_${day_time}.csv
# Iterate over each author in the authors_map
for key in "${!authors_map[@]}"; do
    authors_en=$key
    authors_cn=${authors_map[$key]}
    # Escape special characters in the author's name for use in awk
    escaped_author=$(printf '%s\n' "$authors_cn" | sed 's/[][\\^$.|?*+(){}]/\\&/g')
    echo -en "[$day_time],[$authors_cn],"
    # Calculate the total commit statistics for the author across all repositories
    echo "$file_content" | awk -F ',' -v author="$escaped_author" '
        BEGIN { add=0; subs=0; loc=0 }
        $0 ~ author {
            add += $4;
            subs += $5;
            loc += $6
        }
        END { printf "%s,%s,%s\n", add, subs, loc }
    '
done >>  git_authors_summary_${day_time}.csv
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容