Skip to content

Instantly share code, notes, and snippets.

@ckolderup
Last active December 14, 2015 14:38
Show Gist options
  • Save ckolderup/5101608 to your computer and use it in GitHub Desktop.
Save ckolderup/5101608 to your computer and use it in GitHub Desktop.
Add a histogram when you cd into any git repos showing your commits across all branches over the past half-year. Requires Zach Holman's `spark` script (`brew install spark`): https://github.com/holman/spark latest version: updated spark-action.rb to take in a directory name. todo: put ruby script in git, change weeks to an optional fileopts flag…
cd() {
if [[ $@ == '-' ]]; then
builtin cd "$@" > /dev/null # We'll handle pwd.
else
builtin cd "$@"
fi
# you could use git rev-parse below instead but it'll trigger in all directories in the repo
if ls .git &> /dev/null; then
echo -e " \033[1;34m"`~/bin/spark-action.rb . 26`"\033[0m"
fi
}
#!/usr/bin/env ruby
class Time
def week
strftime('%U').to_i
end
end
WEEK_SECONDS = 7 * 24 * 60 * 60
def time_between?(t, min, max)
(t.year == min.year && t.week >= min.week) || (t.year == max.year && t.week <= max.week)
end
def git_hist(dir, ago_secs)
now = Time.now
min = now - ago_secs
`git --git-dir=#{dir}/.git --work-tree=#{dir} log --branches=* --author='#{ENV['USER']}' --since="#{ago_secs/WEEK_SECONDS} weeks ago" --pretty=format:'%at'`.map {|line|
t = Time.at(line.split(' ').last.to_i)
time_between?(t, min, now) ? t : nil
}.compact.group_by {|x| x.strftime('%G%U') }.inject({}) {|res, (k, v)|
res[k] = v.size
res
}
end
def spark_vals(weeks, commits, min)
(0...weeks).map {|i|
commits[(min + WEEK_SECONDS * i).strftime('%G%U')] || 0
}.join(',')
end
def stats_since(dir, ago)
ago_secs = ago * 60 * 60 * 24 * 7
spark_vals(ago, git_hist(dir, ago_secs), Time.now - ago_secs)
end
print `spark #{stats_since(ARGV[0], ARGV[1].to_i)}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment