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 doesn’t touch your working directory

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

  • Gash only cares about the latest data.

  • Gash can commit.

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

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

  • Gash got pretty good documentation.

  • Gash got a bug tracker.

  • Gash is being developed at GitHub.

Some of these “rules” might change it the future.

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.

See also: #new, #commit, Tree

Credits

This code is based upon git-shelve, and is released under the same license; LGPL.

Defined Under Namespace

Modules: Errors, Helpers Classes: Blob, Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Opens the repo with the specified branch.



331
332
333
334
335
336
337
# File 'lib/gash.rb', line 331

def initialize(repo = ".", branch = "master")
  @branch = branch
  @repository = repo
  @repository = find_repo(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)



449
450
451
452
453
454
455
# File 'lib/gash.rb', line 449

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.



328
329
330
# File 'lib/gash.rb', line 328

def branch
  @branch
end

#repositoryObject

Returns the value of attribute repository.



328
329
330
# File 'lib/gash.rb', line 328

def repository
  @repository
end

Instance Method Details

#branch_exists?Boolean

Checks if the current branch exists

Returns:

  • (Boolean)


384
385
386
# File 'lib/gash.rb', line 384

def branch_exists?
  git_status('rev-parse', @branch) == 0
end

#changed!Object

:nodoc:



343
344
345
# File 'lib/gash.rb', line 343

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

#commit(msg) ⇒ Object

Commit the current changes and returns the commit-hash.

Returns nil if nothing has changed.



376
377
378
379
380
381
# File 'lib/gash.rb', line 376

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

#gashObject

:nodoc:



339
340
341
# File 'lib/gash.rb', line 339

def gash #:nodoc:
  self
end

#inspectObject

:nodoc:



388
389
390
# File 'lib/gash.rb', line 388

def inspect #:nodoc:
  __getobj__.inspect
end

#update!Object

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/gash.rb', line 348

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]
    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