forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_guilt.rb
43 lines (36 loc) · 1.16 KB
/
git_guilt.rb
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
# frozen_string_literal: true
module Overcommit::Hook::PostCommit
# Calculates the change in blame since the last revision.
#
# @see https://www.npmjs.com/package/git-guilt
class GitGuilt < Base
PLUS_MINUS_REGEX = /^(.*?)(?:(\++)|(-+))$/
GREEN = 32
RED = 31
def run
return :pass if initial_commit?
result = execute(command)
return :fail, result.stderr unless result.success?
return :pass if result.stdout.strip.empty?
output = []
result.stdout.scan(PLUS_MINUS_REGEX) do |user, plus, minus|
plus = color(GREEN, plus)
minus = color(RED, minus)
output << "#{user}#{plus}#{minus}"
end
[:warn, output.join("\n")]
end
private
# Returns text wrapped in ANSI escape code necessary to produce a given
# color/text display.
#
# Taken from Overcommit::Logger as a temporary workaround.
# TODO: expose logger instance to hooks for colorized output
#
# @param code [String] ANSI escape code, e.g. '1;33' for "bold yellow"
# @param str [String] string to wrap
def color(code, str)
STDOUT.tty? ? "\033[#{code}m#{str}\033[0m" : str
end
end
end