-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitstats.sh
executable file
·55 lines (44 loc) · 1.95 KB
/
gitstats.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
#!/bin/bash
### shell script for counting git contributions (num_commits, num_lines)
### please update all repos to the latest commits before running this script!
### works with bash version 3.2+
# check if git exists
if [ "$(which git)" == "" ] ; then
echo "git not installed!" && exit
fi
author="NAME" # author's name or email address
homedir="HOME" # home directory
sdate="SDATE" # start date in yyyy-mm-dd
edate="EDATE" # end date in yyyy-mm-dd
stime="STIME" # start time in HH:MM:SS
etime="ETIME" # end time in HH:MM:SS
# helper function for gitstat
gitstats() { git log --shortstat --date "local" --author "$author" --since "${sdate} ${stime}" --until "${edate} ${etime}"; }
# git repos under homedir
repos=$(find "$homedir" -name .git)
# initial values
seen=() lines=0 commits=0
for d in $repos ; do
cd ${d%/*}
url=$(git remote -v | grep "push" | tr 'A-Z' 'a-z' | tr ':/\t@' ' ' | tr -s ' ') # url for git remote
url=$(sed 's/(push)//g; s/\.git//g; s/ git / /g; s/ https / /g' <<< "$url") # cleaning
# array contains: "[remote] [host] [portnum] [id] [repo]" ([portnum] is optional)
IFS=' ' urlarr=($url)
repo="${urlarr[1]} ${urlarr[@]: -2}" # extract [hostname], [id], [repo]
# if repo is already seen, do not count contributions
if [[ "${seen[@]}" =~ "${repo}" ]] ; then
continue # continue to next iteration
fi
# count contributions: num_commits, num_lines
c=$(gitstats | grep "commit" | awk 'END {print NR}')
l=$(gitstats | grep "insertion\|deletion" | awk '{i+=$4; d+=$6} END {print i+d}')
let commits=commits+$((c))
let lines=lines+$((l))
seen+=( "$repo" ) # add the repo to the seen ones
done
# print stats
printf "name: %s\n" "$author"
printf "from: %s\n" "$sdate $stime"
printf "to: %s\n" "$edate $etime"
printf "commits: %s\n" "$commits"
printf "lines: %s\n" "$lines"