Class: Nwiki::Core::GitAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/nwiki/core/git_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo_path) ⇒ GitAccess

Returns a new instance of GitAccess.



37
38
39
# File 'lib/nwiki/core/git_access.rb', line 37

def initialize repo_path
  @repo = Rugged::Repository.new(::File.expand_path(repo_path))
end

Instance Method Details

#all_filesObject



73
74
75
76
77
78
79
80
81
# File 'lib/nwiki/core/git_access.rb', line 73

def all_files
  [].tap do |result|
    target =  @repo.head.target
    @repo.lookup(target).tree.walk_blobs do |path, object|
      fullpath = path + object[:name]
      result << Entry.new(fullpath, @repo.lookup(object[:oid]))
    end
  end
end

#authorObject



57
58
59
60
61
# File 'lib/nwiki/core/git_access.rb', line 57

def author
  author_entry = config.tip.tree.get_entry('author')
  author_blob = @repo.lookup(author_entry[:oid])
  author_blob.text.chomp.force_encoding('UTF-8')
end

#configObject



41
42
43
# File 'lib/nwiki/core/git_access.rb', line 41

def config
  Rugged::Branch.lookup(@repo, 'config')
end

#find_fileObject



63
64
65
66
67
68
69
70
71
# File 'lib/nwiki/core/git_access.rb', line 63

def find_file
  target =  @repo.head.target
  @repo.lookup(target).tree.walk_blobs do |path, object|
    fullpath = path + object[:name]
    if yield(fullpath)
      return Entry.new(fullpath, @repo.lookup(object[:oid]))
    end
  end
end

#logObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nwiki/core/git_access.rb', line 83

def log
  walker = Rugged::Walker.new(@repo).tap do |w|
    w.sorting(Rugged::SORT_DATE) # new -> old
    w.push(@repo.head.target)
  end
  [].tap do |result|
    walker.walk.each do |commit|
      commit_time = Time.at(commit.epoch_time)
      parent = commit.parents.first
      next unless parent
      diff = parent.diff(commit)
      diff.deltas.reject(&:deleted?).each do |delta|
        new_file_object = delta.new_file
        path = new_file_object[:path].force_encoding('UTF-8')
        result << Diff.new(Entry.new(path, @repo.lookup(new_file_object[:oid])), commit_time)
      end
    end
  end
end

#subtitleObject



51
52
53
54
55
# File 'lib/nwiki/core/git_access.rb', line 51

def subtitle
  subtitle_entry = config.tip.tree.get_entry('subtitle')
  subtitle_blob = @repo.lookup(subtitle_entry[:oid])
  subtitle_blob.text.chomp.force_encoding('UTF-8')
end

#titleObject



45
46
47
48
49
# File 'lib/nwiki/core/git_access.rb', line 45

def title
  title_entry = config.tip.tree.get_entry('title')
  title_blob = @repo.lookup(title_entry[:oid])
  title_blob.text.chomp.force_encoding('UTF-8')
end