Last active
August 22, 2018 13:36
-
-
Save arlandism/b274a18a7f0735a800b29476a9daf72b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage | |
# 1) Switch to the branch you want to look at locally | |
# 2) Make sure you have the version of wip that you want to compare against. You may need to fetch first. | |
# 3) Run the script | |
GREEN = 32 | |
YELLOW = 33 | |
BLUE = 34 | |
class Match | |
attr_reader :owner, :changed_line, :best_match | |
def initialize(owner:, changed_line:, best_match:) | |
@owner = owner | |
@changed_line = changed_line | |
@best_match = best_match | |
end | |
end | |
def find_best_match(potential_matches, prospect) | |
if match = potential_matches[prospect] | |
[match, prospect] | |
else | |
parts = prospect.split("/") | |
if parts.length == 1 # can't recurse any further | |
return [nil, nil] | |
end | |
find_best_match(potential_matches, parts[0...-1].join("/")) | |
end | |
end | |
def gen_ownership | |
ownership = File.read('CODEOWNERS').split("\n").reduce({}) do |ownership, line| | |
file, owner = line.split(" ") | |
if file.nil? || owner.nil? || file.start_with?("#") # ignore non-parseable lines and comments | |
next ownership | |
end | |
file = file[0...-1] if file.end_with?("/") # the codeowners file ends directories with '/' | |
file = file[1..-1] if file.start_with?("/") # the codeowners file starts directories with '/' | |
ownership[file] = owner | |
ownership | |
end | |
end | |
def gen_changed | |
`git --no-pager diff wip --name-only`.split("\n") | |
end | |
def colorize(str, color_code) | |
"\e[#{color_code}m#{str}\e[0m" | |
end | |
ownership = gen_ownership | |
matches = [] | |
gen_changed.reduce(matches) do |acc, line| | |
owner, best_match = find_best_match(ownership, line) | |
next matches unless owner # exclude files w/o matches | |
match = Match.new(owner: owner, changed_line: line, best_match: best_match) | |
acc << match | |
acc | |
end | |
matches.each do |match| | |
puts "#{colorize(match.owner, GREEN)} owns #{colorize(match.changed_line, YELLOW)} because of match #{colorize(match.best_match, BLUE)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment