Created
December 14, 2020 17:55
-
-
Save XanClic/41cfcc2ac4619421883e8a97790f5c9e 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
#!/usr/bin/ruby | |
diff = `git show`.lines.map { |l| l.sub("\n", '') } | |
file = nil | |
range = nil | |
removed = [] | |
added = [] | |
diff.each do |line| | |
if line.start_with?('diff ') | |
file = line.split[2] | |
range = nil | |
puts("\033[1;31mChanges in #{file}:\033[0m") | |
elsif line.start_with?('@@ ') | |
range = line.split[1..2] * ' ' | |
removed = [] | |
added = [] | |
elsif range | |
if line.start_with?('-') | |
removed << line | |
elsif line.start_with?('+') | |
added << line | |
elsif line.start_with?(' ') | |
if !removed.empty? && !added.empty? | |
rem_str = removed.map { |l| l.sub(/^-/, '') } * "\n" | |
add_str = added.map { |l| l.sub(/^\+/, '') } * "\n" | |
add_str_collapsed = add_str.gsub(/\s+/, ' ') | |
add_str_collapsed_expanded = | |
add_str_collapsed.gsub(/ ({\s*['"])(execute|return)/, "\n\\1\\2") | |
if rem_str == add_str_collapsed_expanded | |
puts("(Whitespace expanded #{range})") | |
else | |
puts("\033[1mChanged #{range}:\033[0m") | |
puts(removed * "\n") | |
puts(added * "\n") | |
end | |
elsif !removed.empty? | |
puts("\033[1mRemoved #{range}:\033[0m") | |
puts(removed * "\n") | |
elsif !added.empty? | |
puts("\033[1mAdded #{range}:\033[0m") | |
puts(added * "\n") | |
end | |
removed = [] | |
added = [] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment