Class: Regulate::Git::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/regulate/git/interface.rb

Overview

The class that we’ll use to interact with our Git repositories

Class Method Summary collapse

Class Method Details

.build_commit(commit_message, author_name, author_email, mode) ⇒ Object

Setup some standard commit data

Parameters:

  • commit_message (String)

    Our commit message

  • author_name (String)

    Our author name

  • author_email (String)

    Our author email

  • mode (String)

    The type of action being taken in the commit



207
208
209
210
211
212
213
# File 'lib/regulate/git/interface.rb', line 207

def build_commit( commit_message , author_name , author_email , mode )
  {
    :name     => author_name.nil? ? "Anonymous" : author_name,
    :email    => author_email    ||= "[email protected]",
    :message  => commit_message  ||= mode.eql?(:create) ? "Committing resource changes." : "Deleting resource."
  }
end

.commit(options, type) {|index| ... } ⇒ Object

Create a commit

Parameters:

  • options (Hash)

    A hash containing all the data we’ll need to make a commit

  • type (Symbol)

    The type of commit that we’re making - used to create commit messages and set other standard data

  • &blk (Proc)

    The meat of the commit to be performed in other convenience functions

Yields:

  • (index)

    Our actual commit operations performed in their respective convenience functions

Yield Parameters:

  • Our (Grit::Index)

    repo index



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( options , type , &blk )
  # Build up commit data
  commit = build_commit(options[:commit_message],
                        options[:author_name],
                        options[: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(options[:id],'attributes.json') )
  update_working_dir( index, File.join(options[: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

Parameters:

  • id (String)

    The id to search for

Returns:

  • (Array)

    An array of Grit::Commit objects



15
16
17
# File 'lib/regulate/git/interface.rb', line 15

def commits( id )
  Regulate.repo.log( 'master' , id )
end

.current_treeObject

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!

Examples:

A sample delete call

Regulate::Git::Interface.delete({
  :id => id,
  :commit_message => commit_message,
  :author_name => author_name,
  :author_email => author_email
})

Parameters:

  • options (Hash) (defaults to: {})

    A hash containing all the data we’ll need to make a commit



97
98
99
100
101
102
103
104
# File 'lib/regulate/git/interface.rb', line 97

def delete( options = {} )
  # Begin a commit operation
  commit( options , :delete ) do |index|
    # Delete both of our files in the resource directory to delete the directory
    index.delete(File.join(options[:id],'attributes.json'))
    index.delete(File.join(options[:id],'rendered.html'))
  end
end

.exists?(id) ⇒ TrueClass, FalseClass

Return whether or not a resource directory exists in the repo with the given ID

Parameters:

  • id (String)

    The ID that we are checking

Returns:

  • (TrueClass, FalseClass)


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.

Parameters:

  • map (Grit::Tree)

    The tree containing our path that we are checking

  • path (String)

    The path/path-to-file to check

Returns:

  • (TrueClass, FalseClass)

See Also:



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

Parameters:

  • id (String)

    The id to search for

Returns:

  • (String)

    The contents of the attributes.json file for the 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_allArray

Return an array of attributes.json data strings for all IDs in the repo

Returns:

  • (Array)

    An array of JSON strings



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

Parameters:

  • version (String)

    The version SHA hash of the blob

Returns:

  • (String)

    The contents of the blob at that version



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

Parameters:

  • id (String)

    The id to search for

Returns:

  • (String)

    The contents of the rendered.html file for the 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_commitGrit::Commit

Return a commit object representing the last commit on the active repo

Returns:

  • (Grit::Commit)

    A Grit::Commit object representing the most recent commit on the active repository



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!

Examples:

A sample save call

Regulate::Git::Interface.save({
  :id => id,
  :commit_message => commit_message,
  :author_name => author_name,
  :author_email => author_email,
  :attributes => attributes_json,
  :rendered => rendered_html
})

Parameters:

  • options (Hash) (defaults to: {})

    A hash containing all the data we’ll need to make a commit



77
78
79
80
81
82
83
84
# File 'lib/regulate/git/interface.rb', line 77

def save( options = {} )
  # Begin a commit operation
  commit( options , :create ) do |index|
    # Persist changes to both of our needed files
    index.add(File.join(options[:id], 'attributes.json'), options[:attributes])
    index.add(File.join(options[:id], 'rendered.html'), options[: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.

Parameters:

  • index (Grit::Index)

    The git index that we are working on

  • path (String)

    The path/path-to-file that we want to update to represent the true state of the repo

Returns:

  • (NilClass)

See Also:



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.bare
    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