From 19bea3495cf72f7f196642bd881673abf29f31e9 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 25 May 2022 21:02:05 +0200 Subject: [PATCH] Read output from git patch --- lib/format-staged/io.rb | 30 ++++++++++++++++-------------- lib/format_staged.rb | 11 ++++------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 5a80acd..6e99768 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -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 diff --git a/lib/format_staged.rb b/lib/format_staged.rb index d44f5aa..df5c058 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -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