Class: Saddler::Reporter::Support::Git::Repository
- Inherits:
-
Object
- Object
- Saddler::Reporter::Support::Git::Repository
- Defined in:
- lib/saddler/reporter/support/git/repository.rb
Overview
Git repository support utility for saddler-reporter
Instance Attribute Summary collapse
-
#git ⇒ ::Git
readonly
Git repository object.
Instance Method Summary collapse
-
#config ⇒ ::Git::Config
Git config instance.
-
#current_branch ⇒ String
Current branch name.
-
#dig_sha(target) ⇒ String?
Object’s sha.
-
#env_current_branch ⇒ String?
Current branch name from env.
-
#env_push_endpoint ⇒ String?
Push endpoint from env.
-
#env_tracking_branch_name ⇒ String?
Tracking branch name from env.
-
#git_branches ⇒ ::Git::Branches
Git branches.
-
#git_tracking_branch_name ⇒ String?
Tracking branch name.
-
#head ⇒ ::Git::Object
Git object for ‘HEAD`.
-
#initialize(path, options = {}) ⇒ Repository
constructor
Build git repository support utility object.
-
#merge_commit?(commit) ⇒ Boolean
True if commit is a merge commit.
-
#merging_object ⇒ ::Git::Object
This for GitHub pull request diff file.
-
#merging_sha ⇒ String
Merging_object’s sha.
-
#origin_tracking ⇒ ::Git::Object?
Git object for ‘origin/tracking_branch_name`.
-
#push_endpoint ⇒ String
Push endpoint (defaults to: ‘github.com’).
-
#remote_urls ⇒ Array<String>
Remote urls.
-
#slug ⇒ String
‘user/repo` from remote_urls.
-
#tracking ⇒ ::Git::Object?
Git object for ‘tracking_branch_name`.
-
#tracking_branch_name ⇒ String
Tracking branch name.
Constructor Details
#initialize(path, options = {}) ⇒ Repository
Build git repository support utility object
22 23 24 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 22 def initialize(path, = {}) @git = ::Git.open(path, ) end |
Instance Attribute Details
#git ⇒ ::Git (readonly)
Returns git repository object.
11 12 13 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 11 def git @git end |
Instance Method Details
#config ⇒ ::Git::Config
Returns git config instance.
111 112 113 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 111 def config @git.config end |
#current_branch ⇒ String
Returns current branch name.
44 45 46 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 44 def current_branch env_current_branch || @git.current_branch end |
#dig_sha(target) ⇒ String?
Returns object’s sha.
96 97 98 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 96 def dig_sha(target) target && target.sha end |
#env_current_branch ⇒ String?
Returns current branch name from env.
176 177 178 179 180 181 182 183 184 |
# 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 |
#env_push_endpoint ⇒ String?
Returns push endpoint from env.
171 172 173 |
# 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 |
#env_tracking_branch_name ⇒ String?
Returns tracking branch name from env.
127 128 129 130 131 132 |
# 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 |
#git_branches ⇒ ::Git::Branches
Returns git branches.
81 82 83 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 81 def git_branches @git_branches ||= @git.branches end |
#git_tracking_branch_name ⇒ String?
Returns tracking branch name.
142 143 144 145 146 147 148 149 150 151 152 |
# 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 |
#head ⇒ ::Git::Object
Returns git object for ‘HEAD`.
49 50 51 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 49 def head @git.object('HEAD') end |
#merge_commit?(commit) ⇒ Boolean
Returns true if commit is a merge commit.
157 158 159 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 157 def merge_commit?(commit) commit.parents.count == 2 end |
#merging_object ⇒ ::Git::Object
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)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 |
#merging_sha ⇒ String
Returns merging_object’s sha.
54 55 56 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 54 def merging_sha merging_object.sha end |
#origin_tracking ⇒ ::Git::Object?
Returns git object for ‘origin/tracking_branch_name`.
101 102 103 104 105 106 |
# 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 |
#push_endpoint ⇒ String
Returns push endpoint (defaults to: ‘github.com’).
162 163 164 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 162 def push_endpoint (env_push_endpoint || 'github.com').chomp('/') end |
#remote_urls ⇒ Array<String>
Returns remote urls.
37 38 39 40 41 |
# File 'lib/saddler/reporter/support/git/repository.rb', line 37 def remote_urls @git .remotes .map(&:url) end |
#slug ⇒ String
Returns ‘user/repo` from remote_urls.
27 28 29 30 31 32 33 34 |
# 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 |
#tracking ⇒ ::Git::Object?
Returns git object for ‘tracking_branch_name`.
86 87 88 89 90 91 |
# 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 |
#tracking_branch_name ⇒ String
Returns tracking branch name.
118 119 120 121 122 123 124 |
# 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 |