File: git-effort

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (234 lines) | stat: -rwxr-xr-x 4,432 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bash

tmp=$(git_extra_mktemp)
above=0
# if the output won't be printed to tty, disable the color
test -t 1 && to_tty=true
color=

#
# print usage message
#
usage() {
  echo 1>&2 "usage: git effort [--above <value>] [<path>...] [-- [<log options>...]]"
}

#
# get dates for the given <commit>
#
dates() {
  eval "git log $args_to_git_log --pretty='format: %ad' --date=short -- \"$1\""
}

# tput, being quiet about unknown capabilities
tputq() {
  tput "$@" 2>/dev/null
  return 0
}

#
# hide cursor
#

hide_cursor() {
  tputq civis
}

#
# show cursor, and remove temporary file
#

show_cursor_and_cleanup() {
  tputq cnorm
  tputq sgr0
  rm "$tmp" > /dev/null 2>&1
  exit 0
}

#
# get active days for the given <commit>
#

active_days() {
  echo "$1" | sort -r | uniq | wc -l
}

#
# set 'color' based on the given <num>
#

color_for() {
  if [ "$to_tty" = true ]; then
    if   [ "$1" -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
    elif [ "$1" -gt 150 ]; then color="$(tputq setaf 1)"  # red
    elif [ "$1" -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
    elif [ "$1" -gt 100 ]; then color="$(tputq setaf 2)"  # green
    elif [ "$1" -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
    elif [ "$1" -gt 50 ]; then color="$(tputq setaf 5)"  # purplish
    elif [ "$1" -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
    elif [ "$1" -gt 10 ]; then color="$(tputq setaf 3)"  # yellow
    else color="$(tputq sgr0)" # default color
    fi
  else
    color=""
  fi
}

#
# compute the effort of the given <path ...>
#

effort() {
    path=$1
    local commit_dates
    local color reset_color commits len dot f_dot i msg active
    reset_color=""
    test "$to_tty" = true && reset_color="$(tputq sgr0)"
    if ! commit_dates=$(dates "$path"); then
      exit 255
    fi

    # Ensure it's not just an empty line
    if [ -z "$(head -c 1 <<<"$commit_dates")" ]
    then
      exit 0
    fi

    commits=$(wc -l <<<"$commit_dates")
    color='90'

    # ignore <= --above
    test "$commits" -le "$above" && exit 0

    # commits
    color_for $(( commits - above ))
    len=${#path}
    dot="."
    f_dot="$path"
    i=0 ; while test $i -lt $(( columns - len )) ; do
      f_dot=$f_dot$dot
      i=$((i+1))
    done

    msg=$(printf "  ${color}%s %-10d" "$f_dot" "$commits")

    # active days
    active=$(active_days "$commit_dates")
    color_for $(( active - above ))
    msg="$msg $(printf "${color} %d${reset_color}\n" "$active")"
    echo "$msg"
}

#
# print heading
#

heading() {
  echo
  printf "  %-${columns}s %-10s %s\n" 'path' 'commits' 'active days'
  echo
}

#
# output sorted results
#

sort_effort() {
  clear
  echo " "
  heading
  < "$tmp" sort -rn -k 2
}

#
# get processor count, default 8
#
procs() {
  if ! (nproc --all || getconf NPROCESSORS_ONLN || getconf _NPROCESSORS_ONLN || grep -c processor /proc/cpuinfo) 2>/dev/null; then
      echo 8
  fi
}

declare -a paths=()
while [ "${#}" -ge 1 ] ; do

  case "$1" in
    --above)
      shift
      above=$1
      ;;
    --)
      shift
      args_to_git_log=$(printf " %q" "${@:1}")
      break
      ;;
    --*)
      usage
      echo 1>&2 "error: unknown argument $1"
      echo 1>&2 "error: if that argument was meant for git-log,"
      echo 1>&2 "error: please put it after two dashes ( -- )."
      exit 1
      ;;
    *)
      paths+=( "$1" )
      ;;
  esac

  shift
done

# Exit if above-value is not an int
if [ -z "${above##*[!0-9]*}" ] ; then
  echo "error: argument to --above was not an integer" 1>&2
  exit 1
fi

# remove empty quotes that appear when there are no arguments
args_to_git_log="${args_to_git_log#\ \'\'}"
export args_to_git_log

# [path ...]

if test "${#paths}" -eq 0; then
  save_ifs=$IFS
  IFS=$(echo -en "\n\b")
  paths=($(git ls-files))
  IFS=$save_ifs
  unset save_ifs
fi

# set column width to match longest filename
max=0
for path in "${paths[@]}"; do
    cur=${#path}
    if [[ $max -lt $cur ]]; then
        max=$cur
    fi
done
columns=$(( max + 5 ))
export columns

# hide cursor

hide_cursor
trap show_cursor_and_cleanup INT

heading

# send paths to effort
nPaths=${#paths[@]}
nProcs=$(procs)
nJobs="\j"
for ((i=0; i<nPaths; ++i))
do
    while (( ${nJobs@P} >= nProcs )); do
        wait -n
    done
    effort "${paths[i]}" &
done|tee "$tmp"

# if more than one path, sort and print
test "$(wc -l "$tmp" | awk '{print $1}')" -gt 1 && sort_effort
echo

show_cursor_and_cleanup