|
| 1 | +require 'fastlane_core/print_table' |
| 2 | +require 'open3' |
| 3 | + |
| 4 | +module Fastlane |
| 5 | + module Helper |
| 6 | + module Git |
| 7 | + def self.branch |
| 8 | + shell_command = "git rev-parse --abbrev-ref HEAD" |
| 9 | + UI.command(shell_command) |
| 10 | + stdout, _, status = Open3.capture3(shell_command) |
| 11 | + stdout.strip if status.success? |
| 12 | + end |
| 13 | + |
| 14 | + def self.sha |
| 15 | + shell_command = "git rev-parse HEAD" |
| 16 | + UI.command(shell_command) |
| 17 | + stdout, _, status = Open3.capture3(shell_command) |
| 18 | + stdout.strip if status.success? |
| 19 | + end |
| 20 | + |
| 21 | + def self.base_sha |
| 22 | + shell_command = "git merge-base #{remote_head_branch} #{branch}" |
| 23 | + UI.command(shell_command) |
| 24 | + stdout, _, status = Open3.capture3(shell_command) |
| 25 | + return nil if stdout.strip.empty? || !status.success? |
| 26 | + current_sha = sha |
| 27 | + stdout.strip == current_sha ? nil : stdout.strip |
| 28 | + end |
| 29 | + |
| 30 | + def self.primary_remote |
| 31 | + remote = remote() |
| 32 | + return nil if remote.nil? |
| 33 | + remote.include?("origin") ? "origin" : remote.first |
| 34 | + end |
| 35 | + |
| 36 | + def self.remote_head_branch(remote = primary_remote) |
| 37 | + return nil if remote.nil? |
| 38 | + shell_command = "git remote show #{remote}" |
| 39 | + UI.command(shell_command) |
| 40 | + stdout, _, status = Open3.capture3(shell_command) |
| 41 | + return nil if stdout.nil? || !status.success? |
| 42 | + stdout |
| 43 | + .split("\n") |
| 44 | + .map(&:strip) |
| 45 | + .find { |line| line.start_with?("HEAD branch: ") } |
| 46 | + &.split(' ') |
| 47 | + &.last |
| 48 | + end |
| 49 | + |
| 50 | + def self.remote_url(remote = primary_remote) |
| 51 | + return nil if remote.nil? |
| 52 | + shell_command = "git config --get remote.#{remote}.url" |
| 53 | + UI.command(shell_command) |
| 54 | + stdout, _, status = Open3.capture3(shell_command) |
| 55 | + stdout if status.success? |
| 56 | + end |
| 57 | + |
| 58 | + def self.remote |
| 59 | + shell_command = "git remote" |
| 60 | + UI.command(shell_command) |
| 61 | + stdout, _, status = Open3.capture3(shell_command) |
| 62 | + stdout.split("\n") if status.success? |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments