Module: Slugforge::Helper::Git

Defined in:
lib/slugforge/helper/git.rb

Constant Summary collapse

SHA_MAX_LENGTH =
10

Instance Method Summary collapse

Instance Method Details

#build_git_url(account, repository) ⇒ Object



71
72
73
74
75
# File 'lib/slugforge/helper/git.rb', line 71

def build_git_url(, repository)
      ||= 
  repository ||= git_repository
  "[email protected]:#{}/#{repository}.git"
end

#git_accountObject



16
17
18
19
# File 'lib/slugforge/helper/git.rb', line 16

def 
  return nil unless git_inside_work_tree? && !git_url.empty?
  @git_account ||= git_url.match(%r|[:/]([^/]+)/[^/]+(\.git)?$|)[1]
end

#git_branchObject



26
27
28
29
30
31
32
# 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?Boolean

Returns:

  • (Boolean)


7
8
9
10
# 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_remoteObject



34
35
36
37
38
39
# 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 = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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_repositoryObject



21
22
23
24
# 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 = {}) ⇒ Object

Raises:

  • (error_class)


59
60
61
62
63
64
# 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_urlObject



66
67
68
69
# 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_userObject



12
13
14
# File 'lib/slugforge/helper/git.rb', line 12

def git_user
  @git_user ||= git_command('config github.user')
end