class ASF::Git

Provide access to files stored in Git, generally to local clones that are updated via cronjobs.

Constants

GITHUB_HOST

host which can be used to get raw content from git repositories hosted at GitHub.

INFRA_PUPPET

path to the deployment branch on GitHub.

REPOSITORY

path to repository.yml in the source.

Public Class Methods

[](name) click to toggle source

Find a local git clone. Raises an exception if not found.

# File lib/whimsy/asf/git.rb, line 93
def self.[](name)
  self.find!(name)
end
find(name) click to toggle source

Find a local git clone. Returns nil if not found.

# File lib/whimsy/asf/git.rb, line 100
def self.find(name)
  repos[name]
end
find!(name) click to toggle source

Find a local git clone. Raises an exception if not found.

# File lib/whimsy/asf/git.rb, line 107
def self.find!(name)
  result = self.find(name) or raise ArgumentError, "Unable to find git clone for #{name}"

  result
end
github(file, etag = nil) click to toggle source

get a file live from github, e.g. '/apache/petri/master/info.yaml' returns body

# File lib/whimsy/asf/git.rb, line 21
def self.github(file, etag = nil)
  http = Net::HTTP.new(GITHUB_HOST, 443)
  http.use_ssl = true
  return http.request(Net::HTTP::Get.new(file)).body
end
infra_puppet(file) click to toggle source

get a file live from infrastructure puppet (e.g. 'data/common.yaml') issues a HTTP GET request, so may be slow and may fail. For applications that require faster and more dependable access, ASF::Git.find('infrastructure-puppet') may be used to get access to a clone that is updated every 10 minutes.

# File lib/whimsy/asf/git.rb, line 32
def self.infra_puppet(file)
  self.github(INFRA_PUPPET + file)
end
repo_entries() click to toggle source

Get all the Git repo entries

# File lib/whimsy/asf/git.rb, line 85
def self.repo_entries
  self.repos # refresh @@repository_entries
  @@repository_entries[:git]
end
repos() click to toggle source

Scan a list of git directories, looking for local clones.

# File lib/whimsy/asf/git.rb, line 47
def self.repos
  @semaphore.synchronize do
    git = Array(ASF::Config.get(:git))

    # reload if repository changes
    if File.exist?(REPOSITORY) && @@repository_mtime != File.mtime(REPOSITORY)
      @repos = nil
    end

    unless @repos
      @@repository_mtime = File.exist?(REPOSITORY) && File.mtime(REPOSITORY)
      @@repository_entries = YAML.load_file(REPOSITORY)
      repo_override = ASF::Config.get(:repository)
      if repo_override
        git_over = repo_override[:git]
        if git_over
          require 'wunderbar'
          Wunderbar.warn("Found override for repository.yml[:git]")
          @@repository_entries[:git].merge!(git_over)
        end
      end

      @repos = Hash[Dir[*git].map { |name|
        if Dir.exist? name
          out, _, status =
            Open3.capture3('git', 'config', '--get', 'remote.origin.url', {chdir: name})
          if status.success?
            [File.basename(out.chomp, '.git'), name]
          end
        end
      }.compact]
    end

    @repos
  end
end