-
Notifications
You must be signed in to change notification settings - Fork 1
/
count-commits.sh
executable file
·51 lines (43 loc) · 1.57 KB
/
count-commits.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
#!/bin/bash
# script has basic functionality, though could still be improved. Output could be improved when
# user arguments are provided. If the "count-commits.sh dan" is provided, output will be dan: number of commits,
# instead of danie-stafford: number of commits.
#provide instructor when users adds -h parameter
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
less <<EOF
Usage: $0 [options] | [authors]
DESCRIPTION
This script prints out names of committers along with number of commits
OPTIONS
-h | --help Print out usage
authors List of author names separated by a space
EOF
exit 0
fi
# get commit author from git log
gitlog=$(git log --pretty="%an")
# if no arguments, loop through git log, sort/count commits per user, format output as user - # of commits
# Note: that awk word reversal came from stack overrflow, working on understanding it...
if [ "$#" -eq 0 ]; then
git --no-pager log --pretty=":%an" | sort | uniq -c | sort -nr | awk -F: '{sub(/[ ]*/,"",$1);print $NF" - "$1}' | less
fi
# function for looping with user arugments
function loop_gitLog() {
for user in "${gitlog[@]}"; do
echo "$user"
done | grep -ic "$1"
return 0
}
# if arugments are givne, loop through eahc user argument, invoke another loop that counts the number
# of commits per given user
for userArg in "$@"; do
# requier arugments to be at least three characters
if [ "${#userArg}" -lt 3 ]; then
echo 'Please provide user names with at least 3 characters'
exit 1
else
result=$(loop_gitLog "$userArg")
echo "$userArg - $result"
exit 0
fi
done