module Slugforge::Helper::Git

Constants

SHA_MAX_LENGTH

Public Instance Methods

build_git_url(account, repository) click to toggle source
# File lib/slugforge/helper/git.rb, line 71
def build_git_url(account, repository)
  account    ||= git_account
  repository ||= git_repository
  "git@github.com:#{account}/#{repository}.git"
end
git_account() click to toggle source
# File lib/slugforge/helper/git.rb, line 16
def git_account
  return nil unless git_inside_work_tree? && !git_url.empty?
  @git_account ||= git_url.match(%r|[:/]([^/]+)/[^/]+(\.git)?$|)[1]
end
git_branch() click to toggle source
# File lib/slugforge/helper/git.rb, line 26
def git_branch
  return nil unless git_inside_work_tree?
  @git_branch ||= begin
                    symbolic_ref = git_command('symbolic-ref HEAD')
                    symbolic_ref.sub(%r|^refs/heads/|, '')
                  end
end
git_inside_work_tree?() click to toggle source
# File lib/slugforge/helper/git.rb, line 7
def git_inside_work_tree?
  return @git_inside_work_tree unless @git_inside_work_tree.nil?
  @git_inside_work_tree = git_command('rev-parse --is-inside-work-tree') == 'true'
end
git_remote() click to toggle source
# File lib/slugforge/helper/git.rb, line 34
def git_remote
  return nil unless git_inside_work_tree?
  @git_remote ||= git_command("config branch.#{git_branch}.remote")
  # If we are headless just assume origin so that we can still detect other values
  @git_remote.empty? ? 'origin' : @git_remote
end
git_remote_sha(opts = {}) click to toggle source
# File lib/slugforge/helper/git.rb, line 41
def git_remote_sha(opts = {})
  return nil unless git_inside_work_tree?
  sha_length = opts[:sha_length] || SHA_MAX_LENGTH
  url        = opts[:url] || git_url
  branch     = opts[:branch] || git_branch

  @git_remote_sha = begin
                      if @git_remote_sha.nil? || opts[:memoize] == false
                        output = git_command("ls-remote #{url} #{branch}").split(" ").first
                        output =~ /^[0-9a-f]{40}$/i ? output : nil
                      else
                        @git_remote_sha
                      end
                    end

  return @git_remote_sha.slice(0...sha_length) unless @git_remote_sha.nil?
end
git_repository() click to toggle source
# File lib/slugforge/helper/git.rb, line 21
def git_repository
  return nil unless git_inside_work_tree? && !git_url.empty?
  @git_repository ||= git_url.match(%r|/([^/]+?)(\.git)?$|)[1]
end
git_sha(opts = {}) click to toggle source
# File lib/slugforge/helper/git.rb, line 59
def git_sha(opts = {})
  raise error_class, "SHA can't be detected as this is not a git repository" unless git_inside_work_tree?
  sha_length = opts[:sha_length] || SHA_MAX_LENGTH
  @git_sha ||= git_command('rev-parse HEAD').chomp
  @git_sha.slice(0...sha_length)
end
git_url() click to toggle source
# File lib/slugforge/helper/git.rb, line 66
def git_url
  return '' unless git_inside_work_tree?
  @git_url ||= git_command("config remote.#{git_remote}.url")
end
git_user() click to toggle source
# File lib/slugforge/helper/git.rb, line 12
def git_user
  @git_user ||= git_command('config github.user')
end

Private Instance Methods

git_command(cmd) click to toggle source
# File lib/slugforge/helper/git.rb, line 78
def git_command(cmd)
  path = options[:path] ? "cd '#{options[:path]}' &&" : ""
  `#{path} git #{cmd} 2> /dev/null`.chomp
end
git_info() click to toggle source
# File lib/slugforge/helper/git.rb, line 83
def git_info
  Hash[methods.select { |m| m.to_s =~/^git_/ }.map { |m| [ m.to_s, send(m) ] }]
end