Class: Saddler::Reporter::Support::Git::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/saddler/reporter/support/git/repository.rb

Overview

Git repository support utility for saddler-reporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Repository

Build git repository support utility object

Parameters:

  • path (String)

    working_dir

  • options (Hash) (defaults to: {})

    Git.open options (see ::Git.open)

See Also:



22
23
24
# File 'lib/saddler/reporter/support/git/repository.rb', line 22

def initialize(path, options = {})
  @git = ::Git.open(path, options)
end

Instance Attribute Details

#git::Git (readonly)

Returns git repository object.

Returns:

  • (::Git)

    git repository object

See Also:



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.

Returns:

  • (::Git::Config)

    git config instance

See Also:



111
112
113
# File 'lib/saddler/reporter/support/git/repository.rb', line 111

def config
  @git.config
end

#current_branchString

Returns current branch name.

Returns:

  • (String)

    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.

Parameters:

  • target (#sha)

Returns:

  • (String, nil)

    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_branchString?

Returns current branch name from env.

Returns:

  • (String, nil)

    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_endpointString?

Returns push endpoint from env.

Examples:

via ssh

'[email protected]:packsaddle/ruby-saddler-reporter-support-git.git'
#=> 'github.com'

Returns:

  • (String, nil)

    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_nameString?

Returns tracking branch name from env.

Returns:

  • (String, nil)

    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.

Returns:

  • (::Git::Branches)

    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_nameString?

Returns tracking branch name.

Examples:

tracking branch

# from git config
{ "branch.spike/no-valid-master.merge" => "refs/heads/develop" }
=> "develop"

Returns:

  • (String, nil)

    tracking branch name

See Also:



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`.

Returns:

  • (::Git::Object)

    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.

Parameters:

  • commit (::Git::Object)

Returns:

  • (Boolean)

    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)

Returns:

  • (::Git::Object)

    merging object



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_shaString

Returns merging_object’s sha.

Returns:

  • (String)

    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`.

Returns:

  • (::Git::Object, nil)

    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_endpointString

Returns push endpoint (defaults to: ‘github.com’).

Returns:

  • (String)

    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_urlsArray<String>

Returns remote urls.

Returns:

  • (Array<String>)

    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

#slugString

Returns ‘user/repo` from remote_urls.

Returns:

  • (String)

    ‘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`.

Returns:

  • (::Git::Object, nil)

    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_nameString

Returns tracking branch name.

Returns:

  • (String)

    tracking branch name

Raises:



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