Class: OMF::Web::GitContentRepository
- Inherits:
-
ContentRepository
- Object
- Common::LObject
- ContentRepository
- OMF::Web::GitContentRepository
- 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 collapse
- @@git_repositories =
{}
Constants inherited from ContentRepository
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
end.
-
#top_dir ⇒ Object
readonly
end.
Class Method Summary collapse
-
.[](name) ⇒ Object
Return the repository which is referenced to by elements in ‘opts’.
-
.register_git_repo(name, top_dir, is_primary = false) ⇒ Object
Register an existing GIT repo to the system.
Instance Method Summary collapse
- #_find_files(search_pattern, tree, dir_path, res) ⇒ Object
- #_get_path(content_descr) ⇒ Object
-
#create_content_proxy_for(content_descr) ⇒ Object
Load content described by either a hash or a straightforward path and return a ‘ContentProxy’ holding it.
-
#create_url(path, strictly_new = true) ⇒ Object
Create a URL for a file with ‘path’ in.
-
#find_files(search_pattern, opts = {}) ⇒ Object
Return an array of file names which are in the repository and match ‘search_pattern’.
-
#initialize(name, top_dir) ⇒ GitContentRepository
constructor
A new instance of GitContentRepository.
- #write(content_descr, content, message) ⇒ Object
Methods inherited from ContentRepository
#absolute_path, absolute_path_for, create_content_proxy_for, create_url, find_files, find_repo_for, #mime_type_for_file, #read, read_content
Methods included from Common::Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
#initialize(name, top_dir) ⇒ GitContentRepository
Returns a new instance of GitContentRepository.
60 61 62 63 64 65 66 |
# File 'lib/omf-web/content/git_repository.rb', line 60 def initialize(name, top_dir) @name = name @top_dir = top_dir @repo = Grit::Repo.new(top_dir) @url_prefix = "git:#{name}:" #@@git_repositories['git:foo'] = self end |
Instance Attribute Details
#name ⇒ Object (readonly)
end
58 59 60 |
# File 'lib/omf-web/content/git_repository.rb', line 58 def name @name end |
#top_dir ⇒ Object (readonly)
end
58 59 60 |
# File 'lib/omf-web/content/git_repository.rb', line 58 def top_dir @top_dir end |
Class Method Details
.[](name) ⇒ Object
Return the repository which is referenced to by elements in ‘opts’.
22 23 24 25 26 27 |
# File 'lib/omf-web/content/git_repository.rb', line 22 def self.[](name) unless repo = @@git_repositories[name.to_sym] raise "Unknown git repo '#{name}'" end repo end |
.register_git_repo(name, top_dir, is_primary = false) ⇒ Object
Register an existing GIT repo to the system. It will be consulted for all content url’s strarting with ‘git:top_dir:’. If ‘is_primary’ is set to true, it will become the default repo for all newly created content in this app.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/omf-web/content/git_repository.rb', line 35 def self.register_git_repo(name, top_dir, is_primary = false) name = name.to_sym if @@git_repositories[name] warn "Ignoring repeated registration of git rep '#{name}'" return end repo = @@git_repositories[name] = GitContentRepository.new(name, top_dir) if is_primary @@primary_repository = repo end end |
Instance Method Details
#_find_files(search_pattern, tree, dir_path, res) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/omf-web/content/git_repository.rb', line 140 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 res << {:path => path, :name => e.name, :mime_type => mt, #:id => Base64.encode64(long_name).gsub("\n", ''), :size => e.size, :blob => e.id} end # name = e.name # if File.fnmatch(search_pattern, long_name) # res << long_name # end end end res end |
#_get_path(content_descr) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/omf-web/content/git_repository.rb', line 165 def _get_path(content_descr) if content_descr.is_a? String path = content_descr.to_s if path.start_with? 'git:' path = path.split(':')[2] end elsif content_descr.is_a? Hash descr = content_descr if (url = descr[:url]) path = url.split(':')[2] # git:repo_name:path else path = descr[:path] end unless path raise "Missing 'path' or 'url' in content description (#{descr.inspect})" end path = path.to_s else raise "Unsupported type '#{content_descr.class}'" end unless path raise "Can't find path information in '#{content_descr.inspect}'" end return path end |
#create_content_proxy_for(content_descr) ⇒ Object
Load content described by either a hash or a straightforward path and return a ‘ContentProxy’ holding it.
If descr is true, return nil if file for which proxy is requested already exists.
@return: Content proxy
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/omf-web/content/git_repository.rb', line 87 def create_content_proxy_for(content_descr) path = _get_path(content_descr) # TODO: Make sure that key is really unique across multiple repositories descr = descr ? descr.dup : {} url = @url_prefix + path key = Digest::MD5.hexdigest(url) descr[:url] = url descr[:url_key] = key descr[:path] = path descr[:name] = url # Should be something human digestable if (descr[:strictly_new]) Dir.chdir(@top_dir) do return nil if File.exist?(path) end end proxy = ContentProxy.create(descr, self) return proxy end |
#create_url(path, strictly_new = true) ⇒ Object
Create a URL for a file with ‘path’ in. If ‘strictly_new’ is true, returns nil if ‘path’ already exists.
72 73 74 75 76 |
# File 'lib/omf-web/content/git_repository.rb', line 72 def create_url(path, strictly_new = true) return "git:" # TODO: Need to add code to select proper repository return GitContentRepository.create_url(path, strictly_new) end |
#find_files(search_pattern, opts = {}) ⇒ Object
Return an array of file names which are in the repository and match ‘search_pattern’
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/omf-web/content/git_repository.rb', line 128 def find_files(search_pattern, opts = {}) search_pattern = Regexp.new(search_pattern) tree = @repo.tree res = [] fs = _find_files(search_pattern, tree, nil, res) if (mt = opts[:mime_type]) fs = fs.select { |f| f[:mime_type] == mt } end fs end |
#write(content_descr, content, message) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/omf-web/content/git_repository.rb', line 106 def write(content_descr, content, ) path = _get_path(content_descr) Dir.chdir(@top_dir) do unless File.writable?(path) 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( || 'no message') end end |