Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/overcommit/hook_context/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(config, args, input, **options)
@args = args
@input = input
@options = options
@input_mutex = Mutex.new
end

# Executes a command as if it were a regular git hook, passing all
Expand Down Expand Up @@ -95,7 +96,13 @@ def all_files
#
# @return [String]
def input_string
@input_string ||= @input.read
return @input_string if defined?(@input_string)

@input_mutex.synchronize do
@input_string = @input.read unless defined?(@input_string)
end

@input_string
end

# Returns an array of lines passed to the hook via the standard input
Expand Down
51 changes: 51 additions & 0 deletions spec/overcommit/hook_context/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,57 @@
it { should == ['line 1', 'line 2'] }
end

describe '#input_string' do
let(:input_class) do
Class.new do
attr_reader :read_count

def initialize(value)
@value = value
@read_count = 0
@lock = Mutex.new
@read_started = Queue.new
@read_finished = Queue.new
end

def read
@lock.synchronize do
@read_count += 1
raise 'input stream was read more than once' if @read_count > 1
end

@read_started << true
@read_finished.pop
@value
end

def wait_until_reading
@read_started.pop
end

def finish_reading
@read_finished << true
end
end
end

let(:input) { input_class.new("line 1\nline 2\n") }

it 'shares one input stream read across concurrent callers' do
first_reader = Thread.new { context.input_string }
input.wait_until_reading

second_reader = Thread.new { context.input_string }
Thread.pass until second_reader.status == 'sleep'

input.finish_reading
results = [first_reader, second_reader].map(&:value)

results.should == Array.new(2, "line 1\nline 2\n")
input.read_count.should == 1
end
end

describe '#post_fail_message' do
subject { context.post_fail_message }

Expand Down
Loading