Class: OMF::Web::GitContentRepository

Inherits:
ContentRepository show all
Defined in:
lib/omf-web/content/git_repository.rb

Overview

This class provides an interface to a GIT repository It retrieves, archives and versions content.

Constant Summary

Constants inherited from ContentRepository

ContentRepository::MIME_TYPE, ContentRepository::REPO_PLUGINS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ContentRepository

#_get_path, #absolute_path, absolute_path_for, create, create_content_proxy_for, #create_content_proxy_for, create_url, #exist?, find_files, find_repo_for, #mime_type_for_file, #path, #read, read_content, #read_only?, reference_dir, reference_dir=, register_mime_type, register_repo, #to_s

Constructor Details

#initialize(name, opts) ⇒ GitContentRepository

Returns a new instance of GitContentRepository.



19
20
21
22
# File 'lib/omf-web/content/git_repository.rb', line 19

def initialize(name, opts)
  super
  @repo = Grit::Repo.new(@top_dir)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/omf-web/content/git_repository.rb', line 17

def name
  @name
end

#top_dirObject (readonly)

Returns the value of attribute top_dir.



17
18
19
# File 'lib/omf-web/content/git_repository.rb', line 17

def top_dir
  @top_dir
end

Instance Method Details

#_find_files(search_pattern, tree, dir_path, res) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/omf-web/content/git_repository.rb', line 62

def _find_files(search_pattern, tree, dir_path, res)
  tree.contents.each do |e|
    d = e.name
    long_name = dir_path ? "#{dir_path}/#{d}" : d

    if e.is_a? Grit::Tree
      _find_files(search_pattern, e, long_name, res)
    else
      if long_name.match(search_pattern)
        mt = mime_type_for_file(e.name)
        #path = @url_prefix + long_name
        path = long_name
        res << {path: path, url: get_url_for_path(path), name: e.name,
                mime_type: mt,
                #:id => Base64.encode64(long_name).gsub("\n", ''),
                size: e.size, blob: e.id}
      end
    end
  end
  res
end

#find_files(search_pattern, opts = {}) ⇒ Object

Return an array of file names which are in the repository and match ‘search_pattern’



54
55
56
57
58
59
60
# File 'lib/omf-web/content/git_repository.rb', line 54

def find_files(search_pattern, opts = {})
  search_pattern = Regexp.new(search_pattern)
  tree = @repo.tree
  res = []
  fs = _find_files(search_pattern, tree, nil, res)
  fs
end

#get_url_for_path(path) ⇒ Object

Return a URL for a path in this repo



46
47
48
# File 'lib/omf-web/content/git_repository.rb', line 46

def get_url_for_path(path)
  @url_prefix + path
end

#write(content_descr, content, message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omf-web/content/git_repository.rb', line 24

def write(content_descr, content, message)
  raise ReadOnlyContentRepositoryException.new if @read_only

  path = _get_path(content_descr)
  Dir.chdir(@top_dir) do
    d_name = File.dirname(path)
    FileUtils.mkpath(d_name) unless File.exist?(d_name)
    unless File.writable?(path) || File.writable?(d_name)
      raise "Cannot write to file '#{path}'"
    end
    f = File.open(path, 'w')
    f.write(content)
    f.close

    @repo.add(path)
    # TODO: Should set info about committing user which should be in thread context
    @repo.commit_index(message || 'no message')
  end
end