Module: PDK::Util::Git

Defined in:
lib/pdk/util/git.rb

Class Method Summary collapse

Class Method Details

.bare_repo?(maybe_repo) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/pdk/util/git.rb', line 80

def self.bare_repo?(maybe_repo)
  env = { 'GIT_DIR' => maybe_repo }
  rev_parse = git_with_env(env, 'rev-parse', '--is-bare-repository')

  rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true'
end

.clear_cached_informationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears any cached information for git queries Should only be used during testing



144
145
146
147
# File 'lib/pdk/util/git.rb', line 144

def self.clear_cached_information
  @git_repo_expire_cache = nil
  @git_repo_cache = nil
end

.describe(path, ref = nil) ⇒ Object



130
131
132
133
134
135
# File 'lib/pdk/util/git.rb', line 130

def self.describe(path, ref = nil)
  args = ['--git-dir', path, 'describe', '--all', '--long', '--always', ref].compact
  result = git(*args)
  raise PDK::Util::GitError, args, result unless result[:exit_code].zero?
  result[:stdout].strip
end

.git(*args) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/pdk/util/git.rb', line 51

def self.git(*args)
  require 'pdk/cli/exec'

  PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')

  PDK::CLI::Exec.execute(git_bin, *args)
end

.git_binObject



42
43
44
45
46
47
48
49
# File 'lib/pdk/util/git.rb', line 42

def self.git_bin
  require 'pdk/cli/exec'

  git_bin = Gem.win_platform? ? 'git.exe' : 'git'
  vendored_bin_path = File.join(git_bindir, git_bin)

  PDK::CLI::Exec.try_vendored_bin(vendored_bin_path, git_bin)
end

.git_bindirObject



24
25
26
# File 'lib/pdk/util/git.rb', line 24

def self.git_bindir
  @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin')
end

.git_pathsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pdk/util/git.rb', line 28

def self.git_paths
  @paths ||= begin
    paths = [File.join(PDK::Util.pdk_package_basedir, git_bindir)]

    if Gem.win_platform?
      paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'bin')
      paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'libexec', 'git-core')
      paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'usr', 'bin')
    end

    paths
  end
end

.git_with_env(env, *args) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/pdk/util/git.rb', line 59

def self.git_with_env(env, *args)
  require 'pdk/cli/exec'

  PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git')

  PDK::CLI::Exec.execute_with_env(env, git_bin, *args)
end

.ls_remote(repo, ref) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pdk/util/git.rb', line 109

def self.ls_remote(repo, ref)
  if PDK::Util::Filesystem.directory?(repo)
    repo = 'file://' + repo
  end

  output = git('ls-remote', '--refs', repo, ref)

  unless output[:exit_code].zero?
    PDK.logger.error output[:stdout]
    PDK.logger.error output[:stderr]
    raise PDK::CLI::ExitWithError, _('Unable to access the template repository "%{repository}"') % {
      repository: repo,
    }
  end

  matching_refs = output[:stdout].split(%r{\r?\n}).map { |r| r.split("\t") }
  matching_ref = matching_refs.find { |_sha, remote_ref| remote_ref == "refs/tags/#{ref}" || remote_ref == "refs/remotes/origin/#{ref}" || remote_ref == "refs/heads/#{ref}" }
  raise PDK::CLI::ExitWithError, _('Unable to find a branch or tag named "%{ref}" in %{repo}') % { ref: ref, repo: repo } if matching_ref.nil?
  matching_ref.first
end

.remote_repo?(maybe_repo) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/pdk/util/git.rb', line 87

def self.remote_repo?(maybe_repo)
  git('ls-remote', '--exit-code', maybe_repo)[:exit_code].zero?
end

.repo?(maybe_repo) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pdk/util/git.rb', line 67

def self.repo?(maybe_repo)
  result = cached_git_query(maybe_repo, :repo?)
  return result unless result.nil?
  result = if PDK::Util::Filesystem.directory?(maybe_repo)
             # Use boolean shortcircuiting here. The mostly likely type of git repo
             # is a "normal" repo with a working tree. Bare repos do not have work tree
             work_tree?(maybe_repo) || bare_repo?(maybe_repo)
           else
             remote_repo?(maybe_repo)
           end
  cache_query_result(maybe_repo, :repo?, result)
end

.tag?(git_remote, tag_name) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/pdk/util/git.rb', line 137

def self.tag?(git_remote, tag_name)
  git('ls-remote', '--tags', '--exit-code', git_remote, tag_name)[:exit_code].zero?
end

.work_dir_clean?(repo) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



102
103
104
105
106
107
# File 'lib/pdk/util/git.rb', line 102

def self.work_dir_clean?(repo)
  raise PDK::CLI::ExitWithError, _('Unable to locate git work dir "%{workdir}"') % { workdir: repo } unless PDK::Util::Filesystem.directory?(repo)
  raise PDK::CLI::ExitWithError, _('Unable to locate git dir "%{gitdir}"') % { gitdir: repo } unless PDK::Util::Filesystem.directory?(File.join(repo, '.git'))

  git('--work-tree', repo, '--git-dir', File.join(repo, '.git'), 'status', '--untracked-files=no', '--porcelain', repo)[:stdout].empty?
end

.work_tree?(path) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
# File 'lib/pdk/util/git.rb', line 91

def self.work_tree?(path)
  return false unless PDK::Util::Filesystem.directory?(path)
  result = cached_git_query(path, :work_tree?)
  return result unless result.nil?

  Dir.chdir(path) do
    rev_parse = git('rev-parse', '--is-inside-work-tree')
    cache_query_result(path, :work_tree?, rev_parse[:exit_code].zero? && rev_parse[:stdout].strip == 'true')
  end
end