Read output from git patch

This commit is contained in:
Sven Weidauer 2022-05-25 21:02:05 +02:00
parent 721ded5e42
commit 19bea3495c
2 changed files with 20 additions and 21 deletions

View file

@ -2,22 +2,11 @@
require 'English'
class FormatStaged
def get_output(*args, lines: true)
def get_output(*args, lines: true, silent: false)
puts "> #{args.join(' ')}" if @verbose
output = IO.popen(args, err: :err) do |io|
if lines
io.readlines.map(&:chomp)
else
io.read
end
end
if @verbose && lines
output.each do |line|
puts "< #{line}"
end
end
r = IO.popen(args, err: :err)
output = read_output(r, lines: lines, silent: silent)
raise 'Failed to run command' unless $CHILD_STATUS.success?
@ -40,4 +29,17 @@ class FormatStaged
[pid, r]
end
def read_output(r, lines: true, silent: false)
result = r.read
splits = result.split("\n")
if @verbose && !silent
splits.each do |line|
puts "< #{line}"
end
end
r.close
lines ? splits : result
end
end

View file

@ -67,12 +67,7 @@ class FormatStaged
pid2, r = pipe_command format_command, source: r
pid3, r = pipe_command 'git', 'hash-object', '-w', '--stdin', source: r
result = r.readlines.map(&:chomp)
if @verbose
result.each do |line|
puts "< #{line}"
end
end
result = read_output(r, lines: false).chomp
Process.wait pid1
raise "Cannot read #{file.dst_hash} from object database" unless $CHILD_STATUS.success?
@ -83,7 +78,7 @@ class FormatStaged
Process.wait pid3
raise 'Error writing formatted file back to object database' unless $CHILD_STATUS.success? && !result.empty?
result.first
result
end
def object_is_empty(hash)
@ -102,6 +97,8 @@ class FormatStaged
patch_out.write patch
patch_out.close
read_output r
Process.wait pid
raise 'Error applying patch' unless $CHILD_STATUS.success?
end