Skip to content

Commit 9383432

Browse files
committed
Gracefully handle large file lists in list_files helper
Previously, if the list of files was larger than the allowed number of arguments for the OS, this would fail. Use our `execute` helper to automatically handle argument splitting.
1 parent 523b70c commit 9383432

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/overcommit/git_repo.rb

+10-9
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ def modified_files(options)
109109
# @return [Array<String>] list of absolute file paths
110110
def list_files(paths = [], options = {})
111111
ref = options[:ref] || 'HEAD'
112-
path_list =
113-
if OS.windows?
114-
paths = paths.map { |path| path.gsub('"', '""') }
115-
paths.empty? ? '' : "\"#{paths.join('" "')}\""
116-
else
117-
paths.shelljoin
118-
end
119-
`git ls-tree --name-only #{ref} #{path_list}`.
120-
split(/\n/).
112+
113+
result = Overcommit::Utils.execute(%W[git ls-tree --name-only #{ref}], args: paths)
114+
unless result.success?
115+
raise Overcommit::Exceptions::Error,
116+
"Error listing files. EXIT STATUS(es): #{result.statuses}.\n" \
117+
"STDOUT(s): #{result.stdouts}.\n" \
118+
"STDERR(s): #{result.stderrs}."
119+
end
120+
121+
result.stdout.split(/\n/).
121122
map { |relative_file| File.expand_path(relative_file) }.
122123
reject { |file| File.directory?(file) } # Exclude submodule directories
123124
end

spec/overcommit/git_repo_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@
217217
end
218218
end
219219
end
220+
221+
context 'when the git ls-tree command fails for whatever reason' do
222+
before do
223+
result = double('result', success?: false, statuses: [1], stdouts: '', stderrs: '')
224+
allow(Overcommit::Utils).
225+
to receive(:execute).
226+
with(%w[git ls-tree --name-only HEAD], args: []).
227+
and_return(result)
228+
end
229+
230+
it 'raises' do
231+
expect { subject }.to raise_error Overcommit::Exceptions::Error
232+
end
233+
end
220234
end
221235

222236
describe '.tracked?' do

0 commit comments

Comments
 (0)