Module: Fastlane::Helper::Git

Defined in:
lib/fastlane/plugin/emerge/helper/git.rb

Class Method Summary collapse

Class Method Details

.base_shaObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 50

def self.base_sha
  current_branch = branch
  remote_head = remote_head_branch
  return nil if current_branch.nil? || remote_head.nil?

  shell_command = "git merge-base #{remote_head} #{current_branch}"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  return nil if stdout.strip.empty? || !status.success?
  current_sha = sha
  stdout.strip == current_sha ? nil : stdout.strip
end

.branchObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 7

def self.branch
  shell_command = "git rev-parse --abbrev-ref HEAD"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  unless status.success?
    UI.error("Failed to get the current branch name")
    return nil
  end

  branch_name = stdout.strip
  if branch_name == "HEAD"
    # We're in a detached HEAD state
    # Find all branches that contains the current HEAD commit
    #
    # Example output:
    # * (HEAD detached at dec13a5)
    # telkins/detached-test
    # remotes/origin/telkins/detached-test
    #
    # So far I've seen this output be fairly stable
    # If the input is invalid for whatever reason, sed/awk will return an empty string
    shell_command = "git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }'"
    UI.command(shell_command)
    head_stdout, _, head_status = Open3.capture3(shell_command)

    unless head_status.success?
      UI.error("Failed to get the current branch name for detached HEAD")
      return nil
    end

    branch_name = head_stdout.strip
  end

  branch_name == "HEAD" ? nil : branch_name
end

.previous_shaObject



63
64
65
66
67
68
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 63

def self.previous_sha
  shell_command = "git rev-parse HEAD^"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  stdout.strip if status.success?
end

.primary_remoteObject



70
71
72
73
74
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 70

def self.primary_remote
  remote = remote()
  return nil if remote.nil?
  remote.include?("origin") ? "origin" : remote.first
end

.remoteObject



98
99
100
101
102
103
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 98

def self.remote
  shell_command = "git remote"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  stdout.split("\n") if status.success?
end

.remote_head_branch(remote = primary_remote) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 76

def self.remote_head_branch(remote = primary_remote)
  return nil if remote.nil?
  shell_command = "git remote show #{remote}"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  return nil if stdout.nil? || !status.success?
  stdout
    .split("\n")
    .map(&:strip)
    .find { |line| line.start_with?("HEAD branch: ") }
    &.split(' ')
    &.last
end

.remote_url(remote = primary_remote) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 90

def self.remote_url(remote = primary_remote)
  return nil if remote.nil?
  shell_command = "git config --get remote.#{remote}.url"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  stdout if status.success?
end

.shaObject



43
44
45
46
47
48
# File 'lib/fastlane/plugin/emerge/helper/git.rb', line 43

def self.sha
  shell_command = "git rev-parse HEAD"
  UI.command(shell_command)
  stdout, _, status = Open3.capture3(shell_command)
  stdout.strip if status.success?
end