class Saddler::Reporter::Support::Git::Repository
Git
repository support utility for saddler-reporter
Attributes
Public Class Methods
Build git repository support utility object
@param path [String] working_dir @param options [Hash] Git.open options (see ::Git.open)
@see github.com/schacon/ruby-git
# File lib/saddler/reporter/support/git/repository.rb, line 22 def initialize(path, options = {}) @git = ::Git.open(path, options) end
Public Instance Methods
@return [::Git::Config] git config instance
@see github.com/schacon/ruby-git
# File lib/saddler/reporter/support/git/repository.rb, line 111 def config @git.config end
@return [String] current branch name
# File lib/saddler/reporter/support/git/repository.rb, line 44 def current_branch env_current_branch || @git.current_branch end
@param target [#sha]
@return [String, nil] object’s sha
# File lib/saddler/reporter/support/git/repository.rb, line 96 def dig_sha(target) target && target.sha end
@return [String, nil] current branch name from env
# File lib/saddler/reporter/support/git/repository.rb, line 176 def env_current_branch env_branch = EnvBranch.new do if ENV['CURRENT_BRANCH'] && !ENV['CURRENT_BRANCH'].empty? ENV['CURRENT_BRANCH'] end end env_branch.branch_name end
@example via ssh
'git@github.com:packsaddle/ruby-saddler-reporter-support-git.git' #=> 'github.com'
@return [String, nil] push endpoint from env
# File lib/saddler/reporter/support/git/repository.rb, line 171 def env_push_endpoint ENV['PUSH_ENDPOINT'] if ENV['PUSH_ENDPOINT'] && !ENV['PUSH_ENDPOINT'].empty? end
@return [String, nil] tracking branch name from env
# File lib/saddler/reporter/support/git/repository.rb, line 127 def env_tracking_branch_name # GitHub pull request builder plugin (for Jenkins) if ENV['ghprbTargetBranch'] && !ENV['ghprbTargetBranch'].empty? ENV['ghprbTargetBranch'] end end
@return [::Git::Branches] git branches
# File lib/saddler/reporter/support/git/repository.rb, line 81 def git_branches @git_branches ||= @git.branches end
@example tracking branch
# from git config { "branch.spike/no-valid-master.merge" => "refs/heads/develop" } => "develop"
@return [String, nil] tracking branch name
@see stackoverflow.com/questions/4950725/how-do-i-get-git-to-show-me-which-branches-are-tracking-what
# File lib/saddler/reporter/support/git/repository.rb, line 142 def git_tracking_branch_name config .select { |k, _| /\Abranch.*merge\Z/ =~ k } .values .map do |v| match = %r{\Arefs/heads/(.*)\Z}.match(v) match ? match[1] : nil end.compact .uniq .shift end
@return [::Git::Object] git object for ‘HEAD`
# File lib/saddler/reporter/support/git/repository.rb, line 49 def head @git.object('HEAD') end
@param commit [::Git::Object]
@return [Boolean] true if commit is a merge commit
# File lib/saddler/reporter/support/git/repository.rb, line 157 def merge_commit?(commit) commit.parents.count == 2 end
This for GitHub pull request diff file. if head is commit which already merged, head’s parent objects include merging object and (master or origin/master)
@return [::Git::Object] merging object
# File lib/saddler/reporter/support/git/repository.rb, line 64 def merging_object return head unless merge_commit?(head) if ENV['ghprbActualCommit'] && !ENV['ghprbActualCommit'].empty? # GitHub pull request builder plugin (for Jenkins) commit = head.parents.select do |parent| parent.sha == ENV['ghprbActualCommit'] end else commit = head.parents.select do |parent| ![dig_sha(tracking), dig_sha(origin_tracking)].compact.include?(parent.sha) end end return commit.last if commit.count == 1 head # fallback end
@return [String] merging_object
‘s sha
# File lib/saddler/reporter/support/git/repository.rb, line 54 def merging_sha merging_object.sha end
@return [::Git::Object, nil] git object for ‘origin/tracking_branch_name`
# File lib/saddler/reporter/support/git/repository.rb, line 101 def origin_tracking target = "origin/#{tracking_branch_name}" return unless git_branches[target] @git.object(target) end
@return [String] push endpoint (defaults to: ‘github.com’)
# File lib/saddler/reporter/support/git/repository.rb, line 162 def push_endpoint (env_push_endpoint || 'github.com').chomp('/') end
@return [Array<String>] remote urls
# File lib/saddler/reporter/support/git/repository.rb, line 37 def remote_urls @git .remotes .map(&:url) end
@return [String] ‘user/repo` from remote_urls
# File lib/saddler/reporter/support/git/repository.rb, line 27 def slug slug_regex = %r{\A/?(?<slug>.*?)(?:\.git)?\Z} remote_urls.map do |url| uri = GitCloneUrl.parse(url) match = slug_regex.match(uri.path) match[:slug] if match end.compact.first end
@return [::Git::Object, nil] git object for ‘tracking_branch_name`
# File lib/saddler/reporter/support/git/repository.rb, line 86 def tracking target = tracking_branch_name return unless git_branches[target] @git.object(target) end
@return [String] tracking branch name
@raise [NoTrackingBranchNameError] if there is no tracking branch name
# File lib/saddler/reporter/support/git/repository.rb, line 118 def tracking_branch_name @tracking_branch_name ||= begin name = env_tracking_branch_name || git_tracking_branch_name raise NoTrackingBranchNameError if !name || name.empty? name end end