Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Created April 21, 2011 12:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanoverna/934376 to your computer and use it in GitHub Desktop.
Save stefanoverna/934376 to your computer and use it in GitHub Desktop.
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
@stefanoverna
Copy link
Author

Happy it was useful :)

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