Class: Gash

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/gash.rb

Overview

What is Gash?

  • Gash lets you access a Git-repo as a Hash.

  • Gash only cares about the data, not the commits.

  • Gash only cares about the latest data.

  • Gash can commit.

  • Gash doesn’t touch your working directory

  • Gash will automatically create branches if they don’t exists.

  • Gash only loads what it needs, so it handles large repos well.

How do you use it?

gash = Gash.new
gash["README"] = "new content"
gash.commit("Some changes...")

It’s also important to remember that a Gash is simply a Tree, so you can also call those methods.

<strong>See also</strong>: #new, #commit, Tree

Limitation

The only Hash-like features which currently works is #[] and #[]=, so don’t try #merge or something like that.

Defined Under Namespace

Modules: Errors, Helpers Classes: Blob, Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch = "master", repo = ".git") ⇒ Gash

Opens the repo with the specified branch.

<strong>Please note:</strong> The repo must link to the actual repo, not the working directory!



189
190
191
192
193
194
# File 'lib/gash.rb', line 189

def initialize(branch = "master", repo = ".git")
  @branch = branch
  @repository = File.expand_path(repo)
  __setobj__(Tree.new(:parent => self))
  update!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)



306
307
308
309
310
311
312
# File 'lib/gash.rb', line 306

def method_missing(meth, *args, &blk)
  target = self.__getobj__
  unless target.respond_to?(meth)
    Object.instance_method(:method_missing).bind(self).call(meth, *args, &blk)
  end
  target.__send__(meth, *args, &blk)
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



183
184
185
# File 'lib/gash.rb', line 183

def branch
  @branch
end

#repositoryObject

Returns the value of attribute repository.



183
184
185
# File 'lib/gash.rb', line 183

def repository
  @repository
end

Instance Method Details

#branch_exists?Boolean

Checks if the current branch exists

Returns:

  • (Boolean)


244
245
246
247
248
249
# File 'lib/gash.rb', line 244

def branch_exists?
  git('rev-parse', @branch, '2>&1')
  true
rescue Errors::Git
  false
end

#changed!Object

:nodoc:



200
201
202
# File 'lib/gash.rb', line 200

def changed! #:nodoc:
  @sha1 = nil
end

#commit(msg) ⇒ Object

Commit the current changes and returns the commit-hash.

Returns nil if nothing has changed.



236
237
238
239
240
241
# File 'lib/gash.rb', line 236

def commit(msg)
  return unless changed?
  commit = commit_tree(to_tree!, msg)
  @sha1 = git_tree_sha1
  commit
end

#gashObject

:nodoc:



196
197
198
# File 'lib/gash.rb', line 196

def gash #:nodoc:
  self
end

#inspectObject

:nodoc:



251
252
253
# File 'lib/gash.rb', line 251

def inspect #:nodoc:
  __getobj__.inspect
end

#update!Object

Fetch the latest data from Git; you can use this as a clear-method.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/gash.rb', line 205

def update!
  clear
  self.sha1 = git_tree_sha1
  git_tree do |line|
    line.strip!
    mode = line[0, 6]
    type = line[7]
    sha1 = line[12, 40]
    name = line[53..-1]
    if name[0] == ?" && name[-1] == ?"
      name = eval(name)
    end
    name = name[/[^\/]+$/]
    parent = if $`.empty?
      self
    else
      self[$`.chomp("/")]
    end
    parent[name, true] = case type
    when ?b
      Blob.new(:sha1 => sha1, :mode => mode)
    when ?t
      Tree.new(:sha1 => sha1, :mode => mode)
    end
  end
  self
end