Class: CongressForms::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/congress_forms/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote) ⇒ Repo

Returns a new instance of Repo.



11
12
13
14
15
# File 'lib/congress_forms/repo.rb', line 11

def initialize(remote)
  @remote = remote
  @semaphore = Mutex.new
  self.auto_update = true
end

Instance Attribute Details

#auto_updateObject Also known as: auto_update?

Returns the value of attribute auto_update.



7
8
9
# File 'lib/congress_forms/repo.rb', line 7

def auto_update
  @auto_update
end

#remoteObject (readonly)

Returns the value of attribute remote.



6
7
8
# File 'lib/congress_forms/repo.rb', line 6

def remote
  @remote
end

Instance Method Details

#ageObject



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

#cloneObject



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_dirObject



85
86
87
# File 'lib/congress_forms/repo.rb', line 85

def git_dir
  location.join(".git")
end

#initialized?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/congress_forms/repo.rb', line 38

def initialized?
  File.exists?(git_dir)
end

#locationObject



17
18
19
20
21
# 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) ⇒ Object



23
24
25
# File 'lib/congress_forms/repo.rb', line 23

def location=(loc)
  @location = loc ? Pathname.new(loc) : nil
end

#updateObject



52
53
54
55
56
57
# File 'lib/congress_forms/repo.rb', line 52

def update
  begin
    update!
  rescue
  end
end

#update!Object



42
43
44
45
46
47
48
49
50
# 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