Class: GitQueue::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/git_queue/storage.rb

Overview

Backend storage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Storage

Returns a new instance of Storage.



14
15
16
# File 'lib/git_queue/storage.rb', line 14

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/git_queue/storage.rb', line 8

def path
  @path
end

Class Method Details

.create(path) ⇒ Object



10
11
12
# File 'lib/git_queue/storage.rb', line 10

def self.create(path)
  Rugged::Repository.init_at(path)
end

Instance Method Details

#history(size = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git_queue/storage.rb', line 40

def history(size = nil)
  walker = Rugged::Walker.new(driver)
  walker.push(driver.head.target.oid)
  histories = []
  walker.each_with_index do |commit, index|
    break if size && index > size - 1
    histories << commit.message
  end
  walker.reset
  histories
end

#load_queueObject



18
19
20
21
22
# File 'lib/git_queue/storage.rb', line 18

def load_queue
  return [] if driver.empty?
  sha = driver.head.target.tree.get_entry(QUEUE_FILE_NAME)[:oid]
  driver.lookup(sha).content.force_encoding("UTF-8").split("\n")
end

#store_queue(queue, message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/git_queue/storage.rb', line 24

def store_queue(queue, message)
  oid = driver.write(queue.join("\n"), :blob)
  index = driver.index
  index.add( path: QUEUE_FILE_NAME, oid: oid, mode: 0100644)
  Rugged::Commit.create(
    driver,
    tree: index.write_tree(driver),
    author: author,
    committer: author,
    parents: parents,
    update_ref: 'HEAD',
    message: message
  )
  load_queue
end