Skip to content

Commit

Permalink
added iterating method
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Georg committed Mar 17, 2013
1 parent c8c1421 commit ef9dba5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"covered_percent": 94.83
"covered_percent": 92.31
}
}
44 changes: 42 additions & 2 deletions lib/ridley/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def traverse(git, tree, path)
end
end

def rev_to_oid(git, rev)
Rugged::Reference.lookup(git, rev).resolve.target
end


extend self

end
Expand Down Expand Up @@ -46,6 +51,8 @@ def read

class Repository

include Enumerable

attr :git

def initialize(git_repository, options = {})
Expand All @@ -60,9 +67,42 @@ def initialize(git_repository, options = {})
@cache = Cache.new(@git)
end

def each( refs, path = '/' )
walker = Rugged::Walker.new(@git)
case(refs)
when String then
walker.push(Utils.rev_to_oid(git, refs))
when Array then
refs.each do |r|
walker.push(Utils.rev_to_oid(git, r))
end
when Range then
walker.push( Utils.rev_to_oid(git, refs.end) )
walker.hide( Utils.rev_to_oid(git, refs.begin) )
end
seen = Set.new
walker.each do |commit|
tree = Utils.traverse(@git, commit.tree, path)
unless seen.include? tree
yield self[tree]
seen << tree
end
end
return self
end

def []( name, path = '/' )
ref = Rugged::Reference.lookup(git, name)
base = git.lookup(ref.resolve.target).tree
base = nil
case name
when String then
return self[ git.lookup(Rugged::Reference.lookup(git, name).resolve.target), path ]
when Rugged::Commit then
base = name.tree
when Rugged::Tree then
base = name
else
raise "Unknown arg #{name}"
end
Cookbook.new(git, Utils.traverse(git, base, path) , :cache => cache)
end

Expand Down
10 changes: 9 additions & 1 deletion lib/ridley/git/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class Environment < Borx::Environment
def call_method(binding, receiver, method, *args, &block)
if receiver == IO && method == 'read'
path = File.expand_path(args.first, '/')
return Utils.traverse(@git, @tree, path).content
blob = Utils.traverse(@git, @tree, path)
if blob.nil?
raise Error::ENOENT, "No such file or directory - #{path}"
end
return blob.content
end
return super
end
Expand Down Expand Up @@ -45,6 +49,10 @@ def checksums
return @checksums
end

def tree_id
return tree.oid
end

private

# Dummy method. The file list will be loaded lazily
Expand Down
12 changes: 11 additions & 1 deletion spec/ridley-git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def empty_manifest
it "calculates the contents correctly" do
repo = Ridley::Git::Repository.new(git)
cb = repo['HEAD']
checksums = cb.checksums
checksums = cb.checksums
checksums['d7fb90833b4458b5ea33fbf2e4847068'].read.should == <<'METADATA'.strip
name "foo"
version "0.1.0"
Expand All @@ -207,6 +207,16 @@ def empty_manifest
cb.metadata.long_description.should == "# Foo"
end

it "eaches correctly" do
repo = Ridley::Git::Repository.new(git)
expect{|b| repo.each('HEAD', &b) }.to yield_with_args(Ridley::Git::Cookbook)
end

it "eaches correctly with an array of refs" do
repo = Ridley::Git::Repository.new(git)
expect{|b| repo.each(['HEAD'], &b) }.to yield_with_args(Ridley::Git::Cookbook)
end

end

end

0 comments on commit ef9dba5

Please sign in to comment.