Skip to content

Instantly share code, notes, and snippets.

@txus
Created November 29, 2013 13:31
Show Gist options
  • Save txus/7705718 to your computer and use it in GitHub Desktop.
Save txus/7705718 to your computer and use it in GitHub Desktop.
this could very well be the ugliest, shittiest, most procedural code I've ever written in my life
#TODO: kill me plz
def find_change(filename, subjective_line, type)
find = Regexp.escape(type == :addition ? '+' : '-')
skip = Regexp.escape(type == :addition ? '-' : '+')
line = 1
commit = @patch[line].scan(/From ([a-z0-9]+)/).first.first
line += 1 until @patch[line] =~ /\+\+\+ b\/#{filename}/
line += 1
header = @patch[line]
starts = header.scan(/@@ -(\d)/).first.first.to_i
offset = (subjective_line - starts + 1)
(subjective_line - starts + 1).times do |i|
line +=1
if @patch[line] =~ /^#{skip}/
line += 1
offset += 1
end
end
while(@patch[line] !~ /^#{find}/)
(line += 1 && offset += 1) until ((header = @patch[line]) =~ /@@/)
starts = header.scan(/@@ -(\d)/).first.first.to_i
new_offset = (subjective_line - starts + 1)
(subjective_line - starts + 1).times do |i|
line +=1
if @patch[line] =~ /^#{skip}/
line +=1
new_offset += 1
end
end
offset += new_offset
end
Location.new(commit, filename, offset)
end
@yorickpeterse
Copy link

meh, I've seen worse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment