企业项目管理、ORK、研发管理与敏捷开发工具平台

网站首页 > 精选文章 正文

Git工作量统计

wudianyun 2025-03-06 20:12:33 精选文章 22 ℃

#!/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

Tags:

最近发表
标签列表