| 1 |
#!/bin/bash |
| 2 |
# a script to count a few keywords through project history |
| 3 |
header="todo fixme hack" # what to count |
| 4 |
output="todo-table" |
| 5 |
inflate=10000 # how much to multiply the relative counts with |
| 6 |
interval=100 # how many commits to skip when sampling |
| 7 |
skim=no # whether to just look at current state |
| 8 |
|
| 9 |
function cleanup() { |
| 10 |
[[ $skim == yes ]] && return |
| 11 |
{ |
| 12 |
git reset --hard |
| 13 |
git checkout master |
| 14 |
} > /dev/null |
| 15 |
} |
| 16 |
|
| 17 |
function counter() { |
| 18 |
local lines=$(git ls-files | grep -P "\.(c|cpp|h|py|sh|pl|java|2da|txt|ac|in)$" | |
| 19 |
xargs wc -l | awk 'END { print $1 }') |
| 20 |
local counts |
| 21 |
for w in $header; do |
| 22 |
counts="$counts $(git grep -I -i $w | wc -l)" |
| 23 |
done |
| 24 |
local label=$(git log --format="%ad" --date=short | head -n1) |
| 25 |
# local label=$(git describe 2>/dev/null) |
| 26 |
# [[ -z $label ]] && label=$(git log --oneline | sed -n 's,^\(\S*\) .*,\1,p; q') |
| 27 |
counts="$label $lines ${counts# }" |
| 28 |
# generate linecount-relative values |
| 29 |
local m=$inflate |
| 30 |
echo $counts | awk "{ printf(\"%s %d %f %f %f\\n\", \$1, \$2, \$3/\$2*$m, \$4/\$2*$m, \$5/\$2*$m) }" |
| 31 |
} |
| 32 |
|
| 33 |
if [[ -n $(git status --porcelain --untracked-files=no) ]]; then |
| 34 |
echo "Bailing out, you were in the middle of something" |
| 35 |
exit 1 |
| 36 |
fi |
| 37 |
|
| 38 |
trap cleanup INT EXIT |
| 39 |
|
| 40 |
> "$output" |
| 41 |
|
| 42 |
if [[ $1 == --last ]]; then |
| 43 |
output=/dev/stdout |
| 44 |
skim=yes |
| 45 |
else |
| 46 |
echo "Started counting..." |
| 47 |
fi |
| 48 |
|
| 49 |
counter >> "$output" |
| 50 |
|
| 51 |
if [[ $skim != yes ]]; then |
| 52 |
while git checkout HEAD~$interval > /dev/null; do |
| 53 |
counter >> "$output" || exit 2 |
| 54 |
done |
| 55 |
|
| 56 |
# reverse the file for normal arrow of time |
| 57 |
echo "commit $header" >> "$output" |
| 58 |
tac "$output" > "2$output" |
| 59 |
mv "2$output" "$output" |
| 60 |
sed -i 's, *,\t,g' "$output" |
| 61 |
|
| 62 |
echo -e "Done, switching back to master!\n" |
| 63 |
echo "The count data is in $output" |
| 64 |
fi |
| 65 |
|
| 66 |
true << REXAMPLE |
| 67 |
data <- read.csv("$output", sep="\t") |
| 68 |
data[,1] <- as.POSIXct(data[,1]) |
| 69 |
par(ps = 12) |
| 70 |
with(data, plot(commit, todo, type="l", col="blue")) |
| 71 |
with(data, lines(commit, fixme, type="l", col="orange")) |
| 72 |
with(data, lines(commit, hack, type="l", col="red")) |
| 73 |
REXAMPLE |