#!/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
git 代码统计
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- git_stats:仓库代码统计工具之一,可以按git提交人、提交次数、修改文件数、代码行数、注释量在时间维度上进...
- gitstats 是一款git历史统计工具,可以生成定量的统计数据,并以html图表的形式展示。统计文件包括文件数...