Skip to main content

git/gitstats

1#!/usr/bin/env ruby
2# This script gives me a brief summary of my current Git changes, by
3# telling me the number of lines I'm adding/deleting, e.g.
4#
5# +++ 455 additions
6# --- 9 deletions
7#
8# It's just for my own amusement, and shouldn't be relied on for
9# anything serious.
11deletions = 0
12additions = 0
14`git diff`.each_line do |line|
15 if line.start_with? "-" and not line.start_with? "--- "
16 deletions += 1
17 elsif line.start_with? "+" and not line.start_with? "+++ "
18 additions += 1
19 end
20end
22`git diff --cached`.each_line do |line|
23 if line.start_with? "-" and not line.start_with? "--- "
24 deletions += 1
25 elsif line.start_with? "+" and not line.start_with? "+++ "
26 additions += 1
27 end
28end
30# https://stackoverflow.com/q/1489183/1558022
31class String
32 def colorize(color_code)
33 "\e[#{color_code}m#{self}\e[0m"
34 end
36 def red
37 colorize(31)
38 end
40 def green
41 colorize(32)
42 end
43end
45additions = additions.to_s
46deletions = deletions.to_s
48max_len = [additions.size, deletions.size].max
50puts "+++ #{additions.rjust(max_len)} addition#{additions == "1" ? "" : "s"}".green
51puts "--- #{deletions.rjust(max_len)} deletion#{deletions == "1" ? "" : "s"}".red