class CongressForms::Repo

Attributes

auto_update[RW]
auto_update?[RW]
remote[R]

Public Class Methods

new(remote) click to toggle source
# File lib/congress_forms/repo.rb, line 11
def initialize(remote)
  @remote = remote
  @semaphore = Mutex.new
  self.auto_update = true
end

Public Instance Methods

age() click to toggle source
# File lib/congress_forms/repo.rb, line 59
def age
  repo_touched_at = File.mtime(git_dir.join("HEAD"))
  Time.now - repo_touched_at
end
clone() click to toggle source
# File lib/congress_forms/repo.rb, line 27
def clone
  system(
    "git",
    "clone",
    "--quiet",
    "--depth", "1",
    remote,
    location.to_s
  ) or raise Error, "Error cloning repo at #{remote}"
end
find(file) click to toggle source
# File lib/congress_forms/repo.rb, line 64
def find(file)
  lock do
    clone unless initialized?

    update if auto_update? && age > 5*60 # update every 5m

    repo_file = system(
      "git",
      "-C", location.to_s,
      "ls-files", "--error-unmatch",
      "--", file
    )

    raise Errno::ENOENT, file unless repo_file

    path = location.join(file).to_s

    [File.read(path), File.mtime(path)]
  end
end
git_dir() click to toggle source
# File lib/congress_forms/repo.rb, line 85
def git_dir
  location.join(".git")
end
initialized?() click to toggle source
# File lib/congress_forms/repo.rb, line 38
def initialized?
  File.exists?(git_dir)
end
location() click to toggle source
# File lib/congress_forms/repo.rb, line 17
def location
  @location ||= Pathname.new(Dir.mktmpdir).tap do |tmpdir|
    Kernel.at_exit{ FileUtils.rm_r(tmpdir) }
  end
end
location=(loc) click to toggle source
# File lib/congress_forms/repo.rb, line 23
def location=(loc)
  @location = loc ? Pathname.new(loc) : nil
end
update() click to toggle source
# File lib/congress_forms/repo.rb, line 52
def update
  begin
    update!
  rescue
  end
end
update!() click to toggle source
# File lib/congress_forms/repo.rb, line 42
def update!
  system(
    "git",
    "-C", location.to_s,
    "pull",
    "--quiet",
    "--ff-only"
  ) or raise Error, "Error updating git repo at #{location}"
end

Private Instance Methods

lock(&block) click to toggle source
# File lib/congress_forms/repo.rb, line 91
def lock(&block)
  @semaphore.synchronize(&block)
end