Class: Regulate::Git::Interface
- Inherits:
-
Object
- Object
- Regulate::Git::Interface
- Defined in:
- lib/regulate/git/interface.rb
Overview
The class that we’ll use to interact with our Git repositories
Class Method Summary collapse
-
.build_commit(commit_message, author_name, author_email, mode) ⇒ Object
Setup some standard commit data.
-
.commit(options, type) {|index| ... } ⇒ Object
Create a commit.
-
.commits(id) ⇒ Array
Find all commits for a given id Essentially looks for all folders in our repo whose names match the given id.
-
.current_tree ⇒ Object
The tree for the last commit on the active repo.
-
.delete(options = {}) ⇒ Object
Delete a resource directory with a commit All of the keys in the example are expected!.
-
.exists?(id) ⇒ TrueClass, FalseClass
Return whether or not a resource directory exists in the repo with the given ID.
-
.file_path_scheduled_for_deletion?(map, path) ⇒ TrueClass, FalseClass
Determine if a given file is scheduled to be deleted in the next commit for the given Index.
-
.find(id) ⇒ String
Find and return relavant data for a given id.
-
.find_all ⇒ Array
Return an array of attributes.json data strings for all IDs in the repo.
-
.find_by_version(id, version) ⇒ String
Find the contents of a file given it’s version SHA.
-
.find_rendered(id) ⇒ String
Find and return the rendered HTML for a given id.
-
.last_commit ⇒ Grit::Commit
Return a commit object representing the last commit on the active repo.
-
.save(options = {}) ⇒ Object
Save changes to a resource directory with a commit All of the keys in the example are expected!.
-
.update_working_dir(index, path) ⇒ NilClass
Update the given file in the repository’s working directory if there is a working directory present.
Class Method Details
.build_commit(commit_message, author_name, author_email, mode) ⇒ Object
Setup some standard commit data
207 208 209 210 211 212 213 |
# File 'lib/regulate/git/interface.rb', line 207 def build_commit( , , , mode ) { :name => .nil? ? "Anonymous" : , :email => ||= "[email protected]", :message => ||= mode.eql?(:create) ? "Committing resource changes." : "Deleting resource." } end |
.commit(options, type) {|index| ... } ⇒ Object
Create a commit
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/regulate/git/interface.rb', line 113 def commit( , type , &blk ) # Build up commit data commit = build_commit([:commit_message], [:author_name], [:author_email], type) # Get the index index = Regulate.repo.index # Read the tree if we are on master if parent_commit = Regulate.repo.commit('master') index.read_tree(parent_commit.tree.id) end yield index # Determine if we have a parent so we can do stuff like git log parents = parent_commit ? [parent_commit] : [] # Make a new commit and return the sha actor = Grit::Actor.new(commit[:name], commit[:email]) sha = index.commit(commit[:message], parents, actor) # Update our working directory so that the repo truly reflects the desired state # Do this for each of our files that we might have made changes to update_working_dir( index, File.join([:id],'attributes.json') ) update_working_dir( index, File.join([:id], 'rendered.html') ) sha end |
.commits(id) ⇒ Array
Find all commits for a given id Essentially looks for all folders in our repo whose names match the given id
15 16 17 |
# File 'lib/regulate/git/interface.rb', line 15 def commits( id ) Regulate.repo.log( 'master' , id ) end |
.current_tree ⇒ Object
The tree for the last commit on the active repo
237 238 239 240 |
# File 'lib/regulate/git/interface.rb', line 237 def current_tree c = last_commit c ? c.tree : nil end |
.delete(options = {}) ⇒ Object
Delete a resource directory with a commit All of the keys in the example are expected!
97 98 99 100 101 102 103 104 |
# File 'lib/regulate/git/interface.rb', line 97 def delete( = {} ) # Begin a commit operation commit( , :delete ) do |index| # Delete both of our files in the resource directory to delete the directory index.delete(File.join([:id],'attributes.json')) index.delete(File.join([:id],'rendered.html')) end end |
.exists?(id) ⇒ TrueClass, FalseClass
Return whether or not a resource directory exists in the repo with the given ID
219 220 221 |
# File 'lib/regulate/git/interface.rb', line 219 def exists?( id ) Regulate.repo.commits.any? && !(current_tree / File.join(id, 'attributes.json')).nil? end |
.file_path_scheduled_for_deletion?(map, path) ⇒ TrueClass, FalseClass
Determine if a given file is scheduled to be deleted in the next commit for the given Index.
map - The Hash map:
key - The String directory or filename.
val - The Hash submap or the String contents of the file.
path - The String path of the file including extension.
Returns the Boolean response.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/regulate/git/interface.rb', line 185 def file_path_scheduled_for_deletion?( map , path ) parts = path.split('/') if parts.size == 1 deletions = map.keys.select { |k| !map[k] } deletions.any? { |d| d == parts.first } else part = parts.shift if rest = map[part] file_path_scheduled_for_deletion?(rest, parts.join('/')) else false end end end |
.find(id) ⇒ String
Find and return relavant data for a given id
23 24 25 26 27 28 29 |
# File 'lib/regulate/git/interface.rb', line 23 def find( id ) begin ( Regulate.repo.tree / File.join( id , 'attributes.json' ) ).data rescue nil end end |
.find_all ⇒ Array
Return an array of attributes.json data strings for all IDs in the repo
34 35 36 37 38 39 |
# File 'lib/regulate/git/interface.rb', line 34 def find_all Regulate.repo.tree.contents.collect { |tree| attributes_json = ( tree / 'attributes.json' ) attributes_json.data unless attributes_json.nil? }.compact end |
.find_by_version(id, version) ⇒ String
Find the contents of a file given it’s version SHA
56 57 58 59 60 61 62 |
# File 'lib/regulate/git/interface.rb', line 56 def find_by_version( id , version ) begin ( Regulate.repo.commit(version).tree / File.join( id , 'attributes.json' ) ).data rescue nil end end |
.find_rendered(id) ⇒ String
Find and return the rendered HTML for a given id
45 46 47 48 49 50 51 |
# File 'lib/regulate/git/interface.rb', line 45 def find_rendered( id ) begin ( Regulate.repo.tree / File.join( id , 'rendered.html' ) ).data rescue nil end end |
.last_commit ⇒ Grit::Commit
Return a commit object representing the last commit on the active repo
226 227 228 229 230 231 232 233 234 |
# File 'lib/regulate/git/interface.rb', line 226 def last_commit branch = 'master' return nil unless Regulate.repo.commits(branch).any? # We should be able to use just repo.commits(branch).first here but # this is a workaround for this bug: # http://github.com/mojombo/grit/issues/issue/38 Regulate.repo.commits("#{branch}^..#{branch}").first || Regulate.repo.commits(branch).first end |
.save(options = {}) ⇒ Object
Save changes to a resource directory with a commit All of the keys in the example are expected!
77 78 79 80 81 82 83 84 |
# File 'lib/regulate/git/interface.rb', line 77 def save( = {} ) # Begin a commit operation commit( , :create ) do |index| # Persist changes to both of our needed files index.add(File.join([:id], 'attributes.json'), [:attributes]) index.add(File.join([:id], 'rendered.html'), [:rendered]) end end |
.update_working_dir(index, path) ⇒ NilClass
Update the given file in the repository’s working directory if there is a working directory present.
index - The Grit::Index with which to sync. dir - The String directory in which the file lives. name - The String name of the page (may be in human format). format - The Symbol format of the page.
Returns nothing.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/regulate/git/interface.rb', line 159 def update_working_dir( index , path ) unless Regulate.repo. Dir.chdir(::File.join(Regulate.repo.path, '..')) do if file_path_scheduled_for_deletion?(index.tree, path) Regulate.repo.git.rm({'f' => true}, '--', path) else Regulate.repo.git.checkout({}, 'HEAD', '--', path) end end end end |