diff --git a/app/models/git_repository.rb b/app/models/git_repository.rb index e94c1faa1..9028f16db 100644 --- a/app/models/git_repository.rb +++ b/app/models/git_repository.rb @@ -1,4 +1,7 @@ # frozen_string_literal: true + +require 'tempfile' + # Responsible for all git knowledge of a repo # Caches a local mirror (not a full checkout) and creates a workspace when deploying class GitRepository @@ -184,13 +187,16 @@ def instance_cache(key) # success: stdout as string # error: nil def capture_stdout(*command, dir: repo_cache_dir) - success, output = Samson::CommandExecutor.execute( - *command, - whitelist_env: ['HOME', 'PATH'], - timeout: 30.minutes, - err: '/dev/null', - dir: dir - ) - output.strip if success + Tempfile.create('git-stderr') do |error_file| + success, output = Samson::CommandExecutor.execute( + *command, + whitelist_env: ['HOME', 'PATH'], + timeout: 30.minutes, + err: error_file.path, + dir: dir + ) + Rails.logger.error("Failed to run command #{command}: #{error_file.read}") unless success + output.strip if success + end end end diff --git a/test/models/git_repository_test.rb b/test/models/git_repository_test.rb index 0c44eb831..533b7ce4a 100644 --- a/test/models/git_repository_test.rb +++ b/test/models/git_repository_test.rb @@ -101,6 +101,12 @@ def call repository.commit_from_ref('NOT A VALID REF').must_be_nil end + it 'logs an error if ref does not exist' do + create_repo_with_tags + Rails.logger.expects(:error).with { |message| message.must_match(/^Failed to run command/) } + repository.commit_from_ref('NOT A VALID REF') + end + it 'returns the commit of a branch' do create_repo_with_an_additional_branch('my_branch') repository.commit_from_ref('my_branch').must_match /^[0-9a-f]{40}$/