Class: Trahald::Git

Inherits:
BackendBase show all
Defined in:
lib/trahald/git.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path, ext = "md") ⇒ Git

Returns a new instance of Git.



7
8
9
10
# File 'lib/trahald/git.rb', line 7

def initialize(repo_path, ext="md")
  @repo_dir = repo_path
  @ext = ext
end

Class Method Details

.init_repo_if_needed(dir) ⇒ Object



79
80
81
# File 'lib/trahald/git.rb', line 79

def self.init_repo_if_needed(dir)
 init_repo(dir) unless FileTest.exist? dir
end

Instance Method Details

#add!(name, body) ⇒ Object



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

def add!(name, body)
  path = "#{@repo_dir}/#{name}.#{@ext}"
  FileUtils.mkdir_p File.dirname(path)
  begin
    File.open(path, 'w'){|f| f.write(body)}
    Dir.chdir(@repo_dir){
      repo.add "#{name}.#{@ext}".force_encoding("ASCII-8BIT")
    }
    true
  rescue => exception
    puts exception
    false
  end
end

#article(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/trahald/git.rb', line 12

def article(name)
  commit = repo.commits('master', false).find{|c|
    c.diffs.first.b_path.force_encoding("ASCII-8BIT") == "#{name}.#{@ext}".force_encoding("ASCII-8BIT")
  }
  return nil unless commit
  Article.new(
    name,
    commit.diffs.first.b_blob.data.force_encoding("UTF-8"),
    commit.date
  )
end

#body(name) ⇒ Object

experimental



40
41
42
43
# File 'lib/trahald/git.rb', line 40

def body(name)
  a = article(name)
  if a; a.body else nil end
end

#body_old(name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/trahald/git.rb', line 45

def body_old(name)
  first = first_commit
  return nil unless first

  d = dirs(name)
  tree = first.tree
  file = tree / "#{name}.#{@ext}".force_encoding("ASCII-8BIT")
  return nil unless file
  file.data.force_encoding("UTF-8")
end

#commit!(message) ⇒ Object



56
57
58
# File 'lib/trahald/git.rb', line 56

def commit!(message)
  repo.commit_index(message)
end

#dataObject

experimental



61
62
63
64
65
# File 'lib/trahald/git.rb', line 61

def data
  first = first_commit
  return [] unless first
  summary 50
end

#last_modifiedObject



67
68
69
# File 'lib/trahald/git.rb', line 67

def last_modified
  first_commit.date
end

#listObject



71
72
73
74
75
76
77
# File 'lib/trahald/git.rb', line 71

def list
  first = first_commit
  return [] unless first
  list = []
  files('', first.tree, list)
  list.sort
end