fastlane/lib/fastlane/helper/git_helper.rb in fastlane_hotfix-2.165.1 vs fastlane/lib/fastlane/helper/git_helper.rb in fastlane_hotfix-2.187.0

- old
+ new

@@ -1,9 +1,16 @@ module Fastlane module Actions GIT_MERGE_COMMIT_FILTERING_OPTIONS = [:include_merges, :exclude_merges, :only_include_merges].freeze + module SharedValues + GIT_BRANCH_ENV_VARS = %w(GIT_BRANCH BRANCH_NAME TRAVIS_BRANCH BITRISE_GIT_BRANCH CI_BUILD_REF_NAME CI_COMMIT_REF_NAME WERCKER_GIT_BRANCH BUILDKITE_BRANCH APPCENTER_BRANCH CIRCLE_BRANCH).reject do |branch| + # Removing because tests break on CircleCI + Helper.test? && branch == "CIRCLE_BRANCH" + end.freeze + end + def self.git_log_between(pretty_format, from, to, merge_commit_filtering, date_format = nil, ancestry_path) command = %w(git log) command << "--pretty=#{pretty_format}" command << "--date=#{date_format}" if date_format command << '--ancestry-path' if ancestry_path @@ -110,16 +117,38 @@ string = last_git_commit_formatted_with(format_specifier).to_s return string unless string.empty? return nil end - # Returns the current git branch - can be replaced using the environment variable `GIT_BRANCH` + # Returns the current git branch, or "HEAD" if it's not checked out to any branch + # Can be replaced using the environment variable `GIT_BRANCH` def self.git_branch - return ENV['GIT_BRANCH'] if ENV['GIT_BRANCH'].to_s.length > 0 # set by Jenkins - s = Actions.sh("git rev-parse --abbrev-ref HEAD", log: false).chomp - return s.to_s.strip if s.to_s.length > 0 + env_name = SharedValues::GIT_BRANCH_ENV_VARS.find { |env_var| FastlaneCore::Env.truthy?(env_var) } + ENV.fetch(env_name.to_s) do + self.git_branch_name_using_HEAD + end + end + + # Returns the checked out git branch name or "HEAD" if you're in detached HEAD state + def self.git_branch_name_using_HEAD + # Rescues if not a git repo or no commits in a git repo + Actions.sh("git rev-parse --abbrev-ref HEAD", log: false).chomp + rescue => err + UI.verbose("Error getting git branch: #{err.message}") nil - rescue + end + + # Returns the default git remote branch name + def self.git_remote_branch_name(remote_name) + # Rescues if not a git repo or no remote repo + if remote_name + Actions.sh("git remote show #{remote_name} | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp + else + # Query git for the current remote head + Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp + end + rescue => err + UI.verbose("Error getting git default remote branch: #{err.message}") nil end private_class_method def self.git_log_merge_commit_filtering_option(merge_commit_filtering)