Skip to content

Instantly share code, notes, and snippets.

@stefanoverna

stefanoverna/app.rb

Created Apr 21, 2011
Embed
What would you like to do?
Sinatra GIT/SVN repository viewer (http://goo.gl/q4iiS)
%w(sinatra grit).each { |gem| require gem }
mime_type :binary, 'binary/octet-stream'
set :repo, Grit::Repo.new('/Users/steffoz/dev/richcomments')
before %r{^/(\w+)} do
commit_id = params[:captures].first[0..10]
@commit = settings.repo.commits(commit_id).first
halt "No commit exists with id #{commit_id}" if @commit.nil?
end
get "/" do
@commits = settings.repo.commits
haml :index
end
get "/:commit_id" do |commit_id|
@tree = @commit.tree
@path = ""
haml :dir
end
get "/:commit_id/*" do |commit_id, path|
@object = @commit.tree / path
halt "No object exists with path #{path}" if @object.nil?
if @object.is_a? Grit::Blob
content_type :binary
@object.data
else
@tree = @object
@path = path + "/"
haml :dir
end
end
__END__
@@ index
%ul
- @commits.each do |commit|
%li
%a{ :href => "/#{commit.id[0..10]}" }= "#{commit.id[0..10]} (by #{commit.author}, #{commit.committed_date})"
@@ dir
%h1= "Commit #{@commit.id[0..10]} - Path: #{@path}"
%ul
- @tree.contents.each do |obj|
%li
%a{ :href => "/#{@commit.id}/#{@path}#{obj.name}" }= obj.name
@crcastle

This comment has been minimized.

Copy link

@crcastle crcastle commented Jun 1, 2011

This is awesome. I was having trouble digging up a really simple example of using Grit -- and I the simplicity of Sinatra. Thanks.

@stefanoverna

This comment has been minimized.

Copy link
Owner Author

@stefanoverna stefanoverna commented Jun 2, 2011

Happy it was useful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.