Skip to content

Instantly share code, notes, and snippets.

@sbuller
Created May 25, 2012 20:22
Show Gist options
  • Save sbuller/2790380 to your computer and use it in GitHub Desktop.
Save sbuller/2790380 to your computer and use it in GitHub Desktop.
Rugged's Tree objects are unsatisfactory. This eases the pain a bit.
require 'rugged'
class TreeProxy < BasicObject
def initialize(repo, tree)
@repo = repo
@tree = tree
end
def _wrap_yield_(*args)
if args.length >= 1 and args[0].class == ::Hash and args[0].has_key? :oid
obj = @repo.lookup(args[0][:oid])
args[0] = ::TreeProxy.new(@repo, obj)
end
@block.call(*args)
end
def method_missing(*args, &block)
@block = block
if block
block = ::Kernel::lambda {|*proc_args| _wrap_yield_(*proc_args)}
end
ret = @tree.send(*args, &block)
if ret and ret.class == 'Hash' and ret.has_key? :oid
obj = @repo.lookup(ret[:oid])
ret = ::TreeProxy.new(@repo, obj)
end
ret
end
end
def objects
repo = Rugged::Repository.new('.')
ref = Rugged::Reference.lookup(repo, "refs/heads/t")
walker = Rugged::Walker.new(repo)
[repo, ref, walker]
end
repo, ref, walker = objects
a = TreeProxy.new(repo, repo.lookup(ref.target).tree)
a.each {|t| p t}
@loveybot
Copy link

Yay Ruby!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment