Skip to content

Commit

Permalink
Log error output when a git command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
orien committed Jan 14, 2025
1 parent 81eea0f commit 0778ded
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 14 additions & 8 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/models/git_repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}$/
Expand Down

0 comments on commit 0778ded

Please sign in to comment.