rosa-build/app/controllers/projects/git/trees_controller.rb

28 lines
1.3 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
class Projects::Git::TreesController < Projects::Git::BaseController
before_filter lambda{redirect_to @project if params[:treeish] == @project.default_branch and params[:path].blank?}, :only => :show
skip_before_filter :set_branch_and_tree, :only => :archive
2011-04-04 12:28:33 +01:00
def show
@tree = @tree / @path if @path.present?
@commit = @branch.present? ? @branch.commit() : @project.repo.log(@treeish, @path, :max_count => 1).first
render 'empty' unless @commit
end
def archive
format = params[:format]
if (@treeish =~ /^#{@project.owner.uname}-#{@project.name}-/) && !(@treeish =~ /[\s]+/) && (format =~ /^[\w]+$/)
@treeish = @treeish.gsub(/^#{@project.owner.uname}-#{@project.name}-/, '')
@commit = @project.repo.commits(@treeish, 1).first
end
raise Grit::NoSuchPathError unless @commit
name = "#{@project.owner.uname}-#{@project.name}-#{@treeish}"
fullname = "#{name}.#{format == 'tar' ? 'tar.gz' : 'zip'}"
file = Tempfile.new fullname, 'tmp'
system("cd #{@project.path}; git archive --format=#{format} --prefix=#{name}/ #{@treeish} #{format == 'tar' ? ' | gzip -9' : ''} > #{file.path}")
file.close
send_file file.path, :disposition => 'attachment', :type => "application/#{format == 'tar' ? 'x-tar-gz' : 'zip'}", :filename => fullname
end
2012-01-30 20:39:34 +00:00
end