Class: CongressForms::Repo
- Inherits:
-
Object
- Object
- CongressForms::Repo
- Defined in:
- lib/congress_forms/repo.rb
Instance Attribute Summary collapse
-
#auto_update ⇒ Object
(also: #auto_update?)
Returns the value of attribute auto_update.
-
#remote ⇒ Object
readonly
Returns the value of attribute remote.
Instance Method Summary collapse
- #age ⇒ Object
- #clone ⇒ Object
- #find(file) ⇒ Object
- #git_dir ⇒ Object
-
#initialize(remote) ⇒ Repo
constructor
A new instance of Repo.
- #initialized? ⇒ Boolean
- #location ⇒ Object
- #location=(loc) ⇒ Object
- #update ⇒ Object
- #update! ⇒ Object
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_update ⇒ Object 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 |
#remote ⇒ Object (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
#age ⇒ Object
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 |
#clone ⇒ Object
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_dir ⇒ Object
85 86 87 |
# File 'lib/congress_forms/repo.rb', line 85 def git_dir location.join(".git") end |
#initialized? ⇒ Boolean
38 39 40 |
# File 'lib/congress_forms/repo.rb', line 38 def initialized? File.exists?(git_dir) end |
#location ⇒ Object
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 |
#update ⇒ Object
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 |