-
-
Save ganmacs/06fc1074bc761b2122b445f338829662 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/env ruby | |
class FileDiffExtractor | |
DIFF_HUNK_REGEX = / | |
^@@\s | |
[^\s]+\s # Ignore old file range | |
\+(\d+)(?:,(\d+))? # Extract range of hunk containing start line and number of lines | |
\s@@.*$ | |
/x | |
def call | |
modified_files.each do |name| | |
File.open(name, "r") do |f| | |
`git diff --no-color --no-ext-diff -U0 --cached -- "#{name}"`. | |
scan(DIFF_HUNK_REGEX) do |start_line, lines_added| | |
lines_added = (lines_added || 0).to_i | |
start_line = start_line.to_i | |
f.each_line.with_index(1) do |e, i| | |
next unless i.between?(start_line, start_line + lines_added) | |
next if e == "\n" || e == '' | |
puts e | |
end | |
end | |
end | |
end | |
end | |
def modified_files | |
`git diff --name-only -z --diff-filter=ACMR --ignore-submodules=all --cached`. | |
split("\0"). | |
map(&:strip). | |
reject(&:empty?). | |
map { |relative_file| File.expand_path(relative_file) } | |
end | |
end | |
FileDiffExtractor.new.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment