-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_author_metrics.sh
165 lines (128 loc) · 4.46 KB
/
git_author_metrics.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# Determine the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}" .sh)"
LOG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.log"
console_log() {
echo "$1"
}
file_log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
console_and_file_log() {
console_log "$1"
file_log "$1"
}
# Check for arguments
if [ "$#" -lt 4 ]; then
console_log "Usage: $0 <start-date-yyyy-mm-dd> <end-date-yyyy-mm-dd> <author-name> <repo1> [repo2 ... repoN]\n" >&2
exit 1
fi
# Assign arguments
START_DATE=$1
END_DATE=$2
AUTHOR=$3
shift 3 # Remove the first three arguments, leaving only the list of repositories
REPOS=("$@")
# Validate the date format
if ! [[ "$START_DATE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || ! [[ "$END_DATE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
console_and_file_log "Invalid date format error: START_DATE=$START_DATE, END_DATE=$END_DATE. Correct format is yyyy-mm-dd"
exit 1
fi
# Initialize totals
TOTAL_ADDED=0
TOTAL_REMOVED=0
TOTAL_COMMITS=0
COMMIT_MESSAGE_LENGTHS=()
# Global array to track processed commits
declare -A PROCESSED_COMMITS
process_commit() {
local commit_hash=$1
local commit_message=$2
# Skip already processed commits
if [ "${PROCESSED_COMMITS[$commit_hash]}" ]; then
return
fi
PROCESSED_COMMITS[$commit_hash]=1
local stats=$(git show --numstat "$commit_hash" 2>> "$LOG_FILE")
# Parse added and removed lines, filtering only numeric values
local added=$(echo "$stats" | awk '$1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ {added += $1} END {print added}')
local removed=$(echo "$stats" | awk '$1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ {removed += $2} END {print removed}')
# Default to 0 if values are empty
added=${added:-0}
removed=${removed:-0}
# Update totals
TOTAL_ADDED=$((TOTAL_ADDED + added))
TOTAL_REMOVED=$((TOTAL_REMOVED + removed))
TOTAL_COMMITS=$((TOTAL_COMMITS + 1))
COMMIT_MESSAGE_LENGTHS+=(${#commit_message})
}
process_branch() {
local branch=$1
console_and_file_log "Processing branch: $branch"
# Find commits by author in the date range
local commits=$(git log "$branch" --author="$AUTHOR" --since="$START_DATE 00:00:00" --until="$END_DATE 23:59:59" --no-merges --pretty=format:"%H %s" 2>> "$LOG_FILE")
if [ -z "$commits" ]; then
file_log "No commits found for author '$AUTHOR' on branch '$branch'."
return
fi
# Process each commit
while read -r line; do
local commit_hash=$(echo "$line" | awk '{print $1}')
local commit_message=$(echo "$line" | cut -d' ' -f2-)
process_commit "$commit_hash" "$commit_message"
done <<< "$commits"
}
process_repository() {
local repo_path=$1
# Validate the repo path
if [ ! -d "$repo_path" ] || [ ! -d "$repo_path/.git" ]; then
console_and_file_log "Error: '$repo_path' is not a valid Git repository."
return
fi
# Switch to the repository directory
cd "$repo_path" || return
# Fetch all remotes
git fetch --all > /dev/null 2>> "$LOG_FILE"
if [ $? -ne 0 ]; then
console_and_file_log "Error: Failed to fetch branches in repository '$repo_path'."
return
fi
local default_remote=$(git remote | grep -E "^origin$" || git remote | head -n1)
if [ -z "$default_remote" ]; then
console_and_file_log "Error: No remote found in repository."
return
fi
# Get all branches for the current remote
local branches=$(git branch -r | awk -v remote="$default_remote" '!/->/ && $1 ~ "^"remote"/" {sub("^"remote"/", ""); print}')
# Process each branch
for branch in $branches; do
process_branch "$branch"
done
}
# Process each repo
for repo in "${REPOS[@]}"; do
process_repository "$repo"
done
# Calculate aggregate metrics
if [ ${#COMMIT_MESSAGE_LENGTHS[@]} -gt 0 ]; then
MAX_LENGTH=$(printf "%s\n" "${COMMIT_MESSAGE_LENGTHS[@]}" | sort -nr | head -n1)
MIN_LENGTH=$(printf "%s\n" "${COMMIT_MESSAGE_LENGTHS[@]}" | sort -n | head -n1)
AVG_LENGTH=$(printf "%s\n" "${COMMIT_MESSAGE_LENGTHS[@]}" | awk '{sum+=$1} END {print sum/NR}')
else
MAX_LENGTH=0
MIN_LENGTH=0
AVG_LENGTH=0
fi
SUMMARY_TABLE=$(cat <<EOF
Metric | Value
------------------------------ | ----------
Commits | $TOTAL_COMMITS
Average message length (chars) | $(printf "%.2f" "$AVG_LENGTH")
Max message length (chars) | $MAX_LENGTH
Min message length (chars) | $MIN_LENGTH
Total lines added | $TOTAL_ADDED
Total lines removed | $TOTAL_REMOVED
EOF
)
console_and_file_log "$SUMMARY_TABLE"