Skip to content

Commit

Permalink
feat: update git command use to determine branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 16, 2020
1 parent ac7f9b7 commit 3dae935
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions lib/pact/provider_verifier/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,60 @@
module Pact
module ProviderVerifier
module Git
COMMAND = 'git rev-parse --abbrev-ref HEAD'
COMMAND = 'git branch --remote --verbose --no-abbrev --contains'.freeze
BRANCH_ENV_VAR_names = %w{BUILDKITE_BRANCH CIRCLE_BRANCH TRAVIS_BRANCH GIT_BRANCH GIT_LOCAL_BRANCH APPVEYOR_REPO_BRANCH CI_COMMIT_REF_NAME}.freeze

def self.branch
`#{COMMAND}`.strip
rescue StandardError => e
raise Pact::ProviderVerifier::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
find_branch_from_env_vars || branch_from_git_command
end

# private

def self.find_branch_from_env_vars
BRANCH_ENV_VAR_names.collect { |env_var_name| branch_from_env_var(env_var_name) }.compact.first
end

def self.branch_from_env_var(env_var_name)
val = ENV[env_var_name]
if val && val.strip.size > 0
val
else
nil
end
end

def self.branch_from_git_command
branch_names = nil
begin
branch_names = execute_git_command
.split("\n")
.collect(&:strip)
.reject(&:empty?)
.collect(&:split)
.collect(&:first)
.collect{ |line| line.gsub(/^origin\//, '') }
.reject{ |line| line == "HEAD" }

rescue StandardError => e
raise Pact::ProviderVerifier::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
end

validate_branch_names(branch_names)
branch_names[0]
end

def self.validate_branch_names(branch_names)
if branch_names.size == 0
raise Pact::ProviderVerifier::Error, "Command `#{COMMAND}` didn't return anything that could be identified as the current branch."
end

if branch_names.size > 1
raise Pact::ProviderVerifier::Error, "Command `#{COMMAND}` returned multiple branches: #{branch_names.join(", ")}. You will need to get the branch name another way."
end
end

def self.execute_git_command
`#{COMMAND}`
end
end
end
Expand Down

0 comments on commit 3dae935

Please sign in to comment.