Class: Terraspace::Cloud::Vcs::LocalGit

Inherits:
Base
  • Object
show all
Defined in:
lib/terraspace/cloud/vcs/local_git.rb,
lib/terraspace/cloud/vcs/local_git/base.rb,
lib/terraspace/cloud/vcs/local_git/azure.rb,
lib/terraspace/cloud/vcs/local_git/github.rb,
lib/terraspace/cloud/vcs/local_git/gitlab.rb,
lib/terraspace/cloud/vcs/local_git/bitbucket.rb

Defined Under Namespace

Classes: Azure, Base, Bitbucket, Github, Gitlab

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Instance Method Details

#base_varsObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 21

def base_vars
  {
    build_system: "manual",   # required
    host: host,
    full_repo: full_repo,
    branch_name: branch_name,
    sha: sha,
    dirty: dirty?,
    # commit_url: commit_url,  # provided by provider vars
    # branch_url: branch_url,  # provided by provider vars
  }
end

#branch_nameObject



71
72
73
74
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 71

def branch_name
  out = git "rev-parse --abbrev-ref HEAD"
  out unless out == "HEAD" # edge case: when branch has never been pushed
end

#dirty?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 55

def dirty?
  out = git "status --porcelain"
  !out.blank?
end

#full_repoObject



50
51
52
53
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 50

def full_repo
  uri = URI(git_url)
  uri.path.sub(/^\//,'')
end

#git(command) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 81

def git(command)
  return unless git_installed?
  out = `git #{command}`
  unless $?.success?
    logger.debug "WARN Command Failed: git #{command}".color(:yellow)
  end
  out.strip
end

#git_installed?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 91

def git_installed?
  system "type git > /dev/null 2>&1"
end

#git_repo?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 95

def git_repo?
  File.exist?('.git')
end

#git_urlObject

Works for

[email protected]:    => https://github.com/
[email protected]: => https://bitbucket.org/
[email protected]:    => https://gitlab.com/


64
65
66
67
68
69
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 64

def git_url
  remotes = git("remote").strip.split("\n")
  remote_name = remotes.size == 1 ? remotes[0] : "origin"
  out = git "config --get remote.#{remote_name}.url"
  out.sub(/\.git/,'').sub(/^git@/,'https://').sub(/\.(.*):/,'.\1/')
end

#hostObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 34

def host
  return nil unless File.exist?('.git')
  return nil if git_url.blank?
  uri = URI(git_url)
  "#{uri.scheme}://#{uri.host}"
rescue URI::InvalidURIError => e
  logger.info "WARN: #{e.class} #{e.message}".color(:yellow)
  logger.info <<~EOL
    Unable to get the host info from your local .git/config
    Will not be able to determine local git info.
    If possible, it would be a useful to provide the remote.url in your .git/config
    as an issue report or email to improve help improve this logic.
  EOL
  nil
end

#shaObject



76
77
78
79
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 76

def sha
  out = git "rev-parse HEAD"
  out unless out == "HEAD" # edge case: when branch has never been pushed
end

#varsObject



3
4
5
6
7
8
9
10
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 3

def vars
  if git_repo? && git_installed? && host
    provider_vars = vcs_class ? vcs_class.new(base_vars, git_url).vars : {}
    base_vars.merge(provider_vars).compact # remove items with nil values
  else
    { build_system: "manual" }
  end
end

#vcs_classObject



12
13
14
15
16
17
18
19
# File 'lib/terraspace/cloud/vcs/local_git.rb', line 12

def vcs_class
  case host
  when /github\.com/ then Github
  when /gitlab\.com/ then Gitlab
  when /bitbucket\.org/ then Bitbucket
  when /ssh\.dev\.azure\.com/ then Azure
  end
end