class Herdsman::GitRepo

Attributes

env[R]
path[R]

Public Class Methods

new(env, path) click to toggle source
# File lib/herdsman/git_repo.rb, line 6
def initialize(env, path)
  @env  = env
  @path = path
end

Public Instance Methods

current_head() click to toggle source
# File lib/herdsman/git_repo.rb, line 24
def current_head
  current_branch_name || current_tag_name || abbreviated_commit_ref
end
fetch!() click to toggle source
# File lib/herdsman/git_repo.rb, line 19
def fetch!
  _, _, proc_status = git_command('fetch --all')
  raise 'Fetch failed' unless proc_status.exitstatus.zero?
end
git_dir() click to toggle source
# File lib/herdsman/git_repo.rb, line 15
def git_dir
  git_output('rev-parse --git-dir')
end
has_modified_files?() click to toggle source
# File lib/herdsman/git_repo.rb, line 32
def has_modified_files?
  status.modified_files.any?
end
has_unpulled_commits?() click to toggle source
# File lib/herdsman/git_repo.rb, line 40
def has_unpulled_commits?
  git_output('log --oneline ..@{u}').lines.any?
end
has_unpushed_commits?() click to toggle source
# File lib/herdsman/git_repo.rb, line 36
def has_unpushed_commits?
  git_output('log --oneline @{u}..').lines.any?
end
has_untracked_files?() click to toggle source
# File lib/herdsman/git_repo.rb, line 28
def has_untracked_files?
  status.untracked_files.any?
end
initialized?() click to toggle source
# File lib/herdsman/git_repo.rb, line 11
def initialized?
  !git_dir.empty? && File.exist?(path + '/' + git_dir)
end
last_fetched() click to toggle source
# File lib/herdsman/git_repo.rb, line 50
def last_fetched
  File.mtime(File.join(File.expand_path(path, Dir.pwd), '.git/FETCH_HEAD'))
rescue
  Time.at(0)
end
revision?(revision) click to toggle source
# File lib/herdsman/git_repo.rb, line 44
def revision?(revision)
  [current_branch_name, current_tag_name, abbreviated_commit_ref].include?(
    revision,
  )
end

Private Instance Methods

abbreviated_commit_ref() click to toggle source
# File lib/herdsman/git_repo.rb, line 70
def abbreviated_commit_ref
  commit_ref = git_output('rev-parse HEAD')
  commit_ref[0, 7].to_s unless commit_ref.empty?
end
current_branch_name() click to toggle source
# File lib/herdsman/git_repo.rb, line 60
def current_branch_name
  branch_name = git_output('symbolic-ref HEAD --short')
  branch_name unless branch_name.empty?
end
current_tag_name() click to toggle source
# File lib/herdsman/git_repo.rb, line 65
def current_tag_name
  tag_name = git_output('describe --exact-match --tags HEAD')
  tag_name unless tag_name.empty?
end
git_command(command) click to toggle source
# File lib/herdsman/git_repo.rb, line 83
def git_command(command)
  Dir.chdir(File.expand_path(path, Dir.pwd)) do
    Open3.capture3("#{env.git_command} #{command}")
  end
end
git_output(command) click to toggle source
# File lib/herdsman/git_repo.rb, line 79
def git_output(command)
  git_command(command).first.chomp
end
status() click to toggle source
# File lib/herdsman/git_repo.rb, line 75
def status
  StatusParser.new(git_output('status --porcelain'))
end