Skip to content

Commit

Permalink
feat: use different git command to get git branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 12, 2020
1 parent da0b1c1 commit 9373827
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
32 changes: 20 additions & 12 deletions lib/pact_broker/client/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
module PactBroker
module Client
module Git
COMMAND = 'git name-rev --name-only HEAD'.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
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
find_branch_from_env_vars || branch_from_git_command
Expand All @@ -28,7 +28,7 @@ def self.branch
# 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
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)
Expand All @@ -41,24 +41,32 @@ def self.branch_from_env_var(env_var_name)
end

def self.branch_from_git_command
branch_name = nil
branch_names = nil
begin
branch_name = execute_git_command.strip
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 PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
end

validate_branch_name(branch_name)
branch_name
validate_branch_names(branch_names)
branch_names[0]
end

def self.validate_branch_name(branch_name)
if !branch_name || branch_name.size == 0
raise PactBroker::Client::Error, "Command `#{COMMAND}` returned an empty string when trying to determine the git branch name. This is most likely not the value you want. You will need to get the branch name another way."
def self.validate_branch_names(branch_names)
if branch_names.size == 0
raise PactBroker::Client::Error, "Command `#{COMMAND}` didn't return anything that could be identified as the current branch."
end

if branch_name == "HEAD"
raise PactBroker::Client::Error, "Command `#{COMMAND}` returned 'HEAD' when trying to determine the git branch name. This is probably because the repository is in detatched HEAD state. HEAD is most likely not the value you want. You will need to get the branch name another way."
if branch_names.size > 1
raise PactBroker::Client::Error, "Command `#{COMMAND}` returned multiple branches: #{branch_names.join(", ")}. You will need to get the branch name another way."
end
end

Expand Down
24 changes: 16 additions & 8 deletions spec/lib/pact_broker/client/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ module Git
describe ".branch" do
before do
allow(ENV).to receive(:[]).and_call_original
Git::BRANCH_ENV_VAR_NAMES.each do | env_var_name |
allow(ENV).to receive(:[]).with(env_var_name).and_return(nil)
end
end

subject { Git.branch }
Expand All @@ -30,23 +27,34 @@ module Git
end
end

context "when the git branch name comes back as HEAD" do
context "when one git branch name is returned (apart from HEAD)" do
before do
allow(Git).to receive(:execute_git_command).and_return(" origin/HEAD \n origin/foo")
end

it "raises an error" do
expect(subject).to eq "foo"
end
end

context "when two git branch names are returned (apart from HEAD)" do
before do
allow(Git).to receive(:execute_git_command).and_return("HEAD")
allow(Git).to receive(:execute_git_command).and_return(" origin/HEAD \n origin/foo \n origin/bar")
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /returned 'HEAD'/
expect { subject }.to raise_error PactBroker::Client::Error, /returned multiple branches: foo, bar/
end
end


context "when the git branch name comes back as an empty string" do
before do
allow(Git).to receive(:execute_git_command).and_return("")
allow(Git).to receive(:execute_git_command).and_return(" origin/HEAD")
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /an empty string/
expect { subject }.to raise_error PactBroker::Client::Error, /didn't return anything/
end
end

Expand Down

0 comments on commit 9373827

Please sign in to comment.