source: issm/trunk-jpl/scripts/svn_repo_authors.sh@ 24992

Last change on this file since 24992 was 24992, checked in by jdquinn, 5 years ago

ADD: Script to output authors and number of commits from svn log

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1#!/bin/bash
2
3# Parses Subversion repository log messages and outputs authors and their
4# respective number of commits.
5#
6# Output can be sorted:
7#
8# -a sort output by author name
9# -c sort output by number of commits
10#
11# Output can also be limited to commits from the last year:
12#
13# -y display only commits from the last 365 days
14#
15# Will run on first 'path' parameter, or on current working directory if no
16# path is supplied.
17#
18# TODO:
19# - Add option to bin authors and commits by year
20# - Convert to using XML output from SVN log (i.e. `svn log --xml`)
21
22## Functions
23#
24
25function display_help {
26 echo "usage: ${script_name} [option] [path]" >&2
27 case "${uname_out}" in
28 Linux*)
29 display_help_linux
30 ;;
31 Darwin*)
32 display_help_mac
33 ;;
34 *)
35 ;;
36 esac
37}
38
39function display_help_mac {
40 echo " -h diplay help"
41 echo " -a sort output by author name"
42 echo " -c sort output by number of commits"
43 echo " -y display only commits from the last 365 days"
44}
45
46function display_help_linux {
47 echo " -h | --help diplay help"
48 echo " -a | --authors sort output by author name"
49 echo " -c | --commits sort output by number of commits"
50 echo " -y | --year display only commits from the last 365 days"
51}
52
53function print_authors {
54 i=0
55 until [[ $i -eq "${#authors[@]}" ]]; do
56 printf "%-15s %i\n" "${authors[$i]}" "${commits[$i]}"
57 (( i++ ))
58 done
59}
60
61function print_authors_sorted_by_author {
62 local i=0
63 until [[ $i -eq "${#authors[@]}" ]]; do
64 printf "%-15s %i\n" "${authors[$i]}" "${commits[$i]}"
65 (( i++ ))
66 done |
67 sort -n -k1
68}
69
70function print_authors_sorted_by_commits {
71 local i=0
72 until [[ $i -eq "${#authors[@]}" ]]; do
73 printf "%-15s %i\n" "${authors[$i]}" "${commits[$i]}"
74 (( i++ ))
75 done |
76 sort -rn -k2
77}
78
79## Initialize conditional variables
80#
81one_year_ago=""
82sort_by_author=0
83sort_by_commits=0
84today=""
85
86# Retrieve script name
87script_name=$(basename "$0")
88
89## Retrieve OS
90#
91uname_out=$(uname -s)
92case "${uname_out}" in
93 Linux*)
94 options=$(getopt -n ${script_name} -o hacy --long help,author,commits,year -- "$@")
95 ;;
96 Darwin*)
97 options=$(getopt hacy $*)
98 ;;
99 *)
100 printf "operating system %s not currently supported\n" "${uname_out}" >&2
101 exit 1
102 ;;
103esac
104
105## Handle options
106#
107valid_options=$?
108if [[ $valid_options -ne 0 ]]; then
109 display_help
110 exit 1
111fi
112#echo $options # Uncomment for debugging
113set -- $options
114
115while :
116do
117 case "$1" in
118 -h | --help)
119 display_help
120 exit 0
121 ;;
122 -a | --author)
123 sort_by_author=1
124 shift
125 ;;
126 -c | --commits)
127 sort_by_commits=1
128 shift
129 ;;
130 -y | --year)
131 today=$(date +%Y-%m-%d)
132 one_year_ago=$(date -v -1y -v +1d +%Y-%m-%d) # Date adjusted 365 days
133 shift
134 ;;
135 --)
136 shift
137 break
138 ;;
139 *)
140 break
141 ;;
142 esac
143done
144
145## Retrieve path
146#
147# If no path was given, use current working directory; if one or more were
148# given, just use the first one.
149#
150path="."
151num_args=${#@}
152if [[ $num_args -gt 0 ]]; then
153 path=$1
154fi
155
156authors=()
157commits=()
158
159# Get entire log once
160if [[ $one_year_ago != "" ]]; then
161 log=$(svn log -r "{${today}}:{${one_year_ago}}" "${path}")
162else
163 log=$(svn log "${path}")
164fi
165
166data_lines=$(echo "${log}" | egrep "r[0-9]+") # Get only lines from log that contain info we care about
167
168while IFS= read line; do
169 author=$(echo "${line}" | awk -F "|" '{print $2}' | tr -d ' ')
170
171 # Uncomment if we want to treat blank author as "unknown"
172 if [[ "$author" == "" ]]; then
173 author="unknown"
174 fi
175
176 have_seen_author=0
177 for (( i=0; i<${#authors[@]}; ++i )); do
178 if [[ "${authors[$i]}" == "$author" ]]; then
179 have_seen_author=1
180 break
181 fi
182 done
183
184 if [[ $have_seen_author -eq 0 ]]; then
185 authors+=("${author}")
186 commits+=(1)
187 else
188 (( commits[$i]++ ))
189 fi
190done <<< "${data_lines}"
191
192## Print results (sorted, if requested)
193#
194# NOTE: If multiple sort options are specified, the order or precedence is
195# specified as follows.
196#
197if [ $sort_by_commits -eq 1 ]; then
198 print_authors_sorted_by_commits
199elif [ $sort_by_author -eq 1 ]; then
200 print_authors_sorted_by_author
201else
202 print_authors
203fi
Note: See TracBrowser for help on using the repository browser.