Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TomDoc #54

Merged
merged 1 commit into from
Apr 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions lib/rugged/repository.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,38 +1,78 @@
module Rugged module Rugged
# Repository is an interface into a Git repository on-disk. It's the primary
# interface between your app and the main Git objects Rugged makes available
# to you.
class Repository class Repository

# Pretty formatting of a Repository.
#
# Returns a very pretty String.
def inspect def inspect
"#<Rugged::Repository:#{object_id} {path: #{path.inspect}}>" "#<Rugged::Repository:#{object_id} {path: #{path.inspect}}>"
end end


# Get the most recent commit from this repo # Get the most recent commit from this repo.
# #
# Returns a Rugged::Commit object # Returns a Rugged::Commit object.
def last_commit def last_commit
self.lookup self.head.target self.lookup self.head.target
end end


# Walks over a set of commits using Rugged::Walker.
#
# from - The String SHA1 to push onto Walker to begin our walk.
# sorting - The sorting order of the commits, as defined in the README.
# block - A block that we pass into walker#each.
#
# Returns nothing if called with a block, otherwise returns an instance of
# Enumerable::Enumerator containing Rugged::Commit objects.
def walk(from, sorting=Rugged::SORT_DATE, &block) def walk(from, sorting=Rugged::SORT_DATE, &block)
walker = Rugged::Walker.new(self) walker = Rugged::Walker.new(self)
walker.sorting(sorting) walker.sorting(sorting)
walker.push(from) walker.push(from)
walker.each(&block) walker.each(&block)
end end


# Find the HEAD of this repository.
#
# Returns a Reference.
def head def head
ref = Reference.lookup(self, "HEAD") ref = Reference.lookup(self, "HEAD")
ref.resolve ref.resolve
end end


# Look up a SHA1.
#
# Returns one of the four classes that inherit from Rugged::Object.
def lookup(oid) def lookup(oid)
Rugged::Object.lookup(self, oid) Rugged::Object.lookup(self, oid)
end end


# Look up a single reference by name # Look up a single reference by name.
#
# Example:
#
# repo.ref 'refs/heads/master'
# # => #<Rugged::Reference:2199125780 {name: "refs/heads/master",
# target: "25b5d3b40c4eadda8098172b26c68cf151109799"}>
#
# Returns a Rugged::Reference.
def ref(ref_name) def ref(ref_name)
Rugged::Reference.lookup(self, ref_name) Rugged::Reference.lookup(self, ref_name)
end end


# Retuns all the References that match a pattern.
#
# refs - A Regexp (or String) to search for. Optional.
#
# Examples:
#
# # All refs
# repo.refs
#
# # All remote refs
# repo.refs 'refs/remotes'
#
# Returns an Array of References.
def refs(pattern = nil) def refs(pattern = nil)
r = [] r = []
ref_names.each do |ref_name| ref_names.each do |ref_name|
Expand All @@ -46,26 +86,39 @@ def refs(pattern = nil)
r r
end end


# The names of all the refs.
#
# Returns an Enumerable::Enumerator containing all the String ref names.
def ref_names def ref_names
Rugged::Reference.each(self) Rugged::Reference.each(self)
end end


# All of the tags in the repository.
#
# Returns an Enumerable::Enumerator containing all the String tag names.
def tags(pattern="") def tags(pattern="")
Rugged::Tag.each(self, pattern) Rugged::Tag.each(self, pattern)
end end


# All of the remotes in the repository.
#
# Returns an Enumerable::Enumerator containing all the String remote names.
def remotes def remotes
Rugged::Remote.each(self) Rugged::Remote.each(self)
end end


# Get the content of a file at a specific revision.
#
# revision - The String SHA1.
# path - The String file path.
#
# Returns a String.
def file_at(revision, path) def file_at(revision, path)
tree = Rugged::Commit.lookup(self, revision).tree tree = Rugged::Commit.lookup(self, revision).tree
subtree = tree.get_subtree(path) subtree = tree.get_subtree(path)
blob_data = subtree.get_entry(File.basename path) blob_data = subtree.get_entry(File.basename path)
blob = Rugged::Blob.lookup(self, blob_data[:oid]) blob = Rugged::Blob.lookup(self, blob_data[:oid])
blob.content blob.content
end end

end end

end
end
65 changes: 64 additions & 1 deletion test/repo_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,19 +58,82 @@
rm_loose(oid) rm_loose(oid)
end end


test "can use the builtin walk method" do test "can walk in a block" do
oid = "a4a7dce85cf63874e984719f4fdd239f5145052f" oid = "a4a7dce85cf63874e984719f4fdd239f5145052f"
list = [] list = []
@repo.walk(oid) { |c| list << c } @repo.walk(oid) { |c| list << c }
assert list.map {|c| c.oid[0,5] }.join('.'), "a4a7d.c4780.9fd73.4a202.5b5b0.84960" assert list.map {|c| c.oid[0,5] }.join('.'), "a4a7d.c4780.9fd73.4a202.5b5b0.84960"
end end


test "can walk without a block" do
commits = @repo.walk('a4a7dce85cf63874e984719f4fdd239f5145052f')

assert commits.kind_of?(Enumerable)
assert commits.count > 0
end

test "can lookup an object" do
object = @repo.lookup("8496071c1b46c854b31185ea97743be6a8774479")

assert object.kind_of?(Rugged::Commit)
end

test "can find a ref" do
ref = @repo.ref('refs/heads/master')

assert ref.kind_of?(Rugged::Reference)
assert_equal 'refs/heads/master', ref.name
end

test "can return all refs" do
refs = @repo.refs

assert_equal 4, refs.length
end

test "can return all refs that match" do
refs = @repo.refs 'refs/heads'

assert_equal 2, refs.length
end

test "can return the names of all refs" do
refs = @repo.ref_names

refs.each {|name| assert name.kind_of?(String)}
assert_equal 4, refs.count
end

test "can return all tags" do
tags = @repo.tags

assert_equal 2, tags.count
end

test "can return all tags that match" do
tags = @repo.tags 'v0.9'

assert_equal 1, tags.count
end

test "can return all remotes" do
remotes = @repo.remotes

assert_equal 1, remotes.count
end

test "can lookup head from repo" do test "can lookup head from repo" do
head = @repo.head head = @repo.head
assert_equal "36060c58702ed4c2a40832c51758d5344201d89a", head.target assert_equal "36060c58702ed4c2a40832c51758d5344201d89a", head.target
assert_equal :direct, head.type assert_equal :direct, head.type
end end


test "can access a file" do
sha = '36060c58702ed4c2a40832c51758d5344201d89a'
content = @repo.file_at(sha, 'new.txt')
assert_equal "new file\n", content
end

test "garbage collection methods don't crash" do test "garbage collection methods don't crash" do
Rugged::Repository.new(@path) Rugged::Repository.new(@path)
ObjectSpace.garbage_collect ObjectSpace.garbage_collect
Expand Down