-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-commit-plot.cr
More file actions
57 lines (46 loc) · 1.22 KB
/
git-commit-plot.cr
File metadata and controls
57 lines (46 loc) · 1.22 KB
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
require "../src/unicode_plot"
include UnicodePlot
# Build as a Git plugin:
# crystal build examples/time_axis.cr -o git-commit-plot
# mv git-commit-plot ~/.local/bin/
# Then run:
# git commit-plot
# git commit-plot /path/to/repo
directory = File.expand_path(ARGV[0]? || Dir.current)
_, terminal_width = UnicodePlot.out_stream_size(STDOUT)
plot_width = Math.max(20, terminal_width - 32)
def date_from_git(value : String) : Time
year, month, day = value.split("-").map(&.to_i)
Time.utc(year, month, day)
end
stdout = IO::Memory.new
stderr = IO::Memory.new
status = Process.run(
"git",
["-C", directory, "log", "--date=short", "--format=%ad"],
output: stdout,
error: stderr
)
unless status.success?
abort "git log failed in #{directory}: #{stderr.to_s.strip}"
end
counts = Hash(Time, Int32).new(0)
stdout.to_s.each_line do |line|
next if line.empty?
counts[date_from_git(line)] += 1
end
abort "no commits found in #{directory}" if counts.empty?
dates = counts.keys.sort!
values = dates.map { |date| counts[date].to_f64 }
plot = stairs(
dates,
values,
format: "%F",
title: "Git commits per day",
xlabel: "commit date",
ylabel: "commits",
name: "commits",
color: :green,
width: plot_width
)
puts plot