2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-05-02 10:18:07 +01:00
|
|
|
class Projects::Git::TreesController < Projects::Git::BaseController
|
2012-09-19 20:29:36 +01:00
|
|
|
before_filter lambda{redirect_to @project if params[:treeish] == @project.default_branch and params[:path].blank?}, :only => :show
|
2012-09-20 12:10:52 +01:00
|
|
|
before_filter :set_sha
|
2012-09-19 20:29:36 +01:00
|
|
|
skip_before_filter :set_branch_and_tree, :only => :archive
|
2011-04-04 12:28:33 +01:00
|
|
|
|
2012-07-17 09:02:56 +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
|
2011-03-10 12:39:24 +00:00
|
|
|
end
|
2012-04-17 11:18:03 +01:00
|
|
|
|
|
|
|
def archive
|
2012-09-19 20:29:36 +01:00
|
|
|
format = params[:format]
|
2012-09-20 12:10:52 +01:00
|
|
|
if (@sha =~ /^#{@project.owner.uname}-#{@project.name}-/) && !(@sha =~ /[\s]+/) && (format =~ /^[\w]+$/)
|
|
|
|
@sha = @sha.gsub(/^#{@project.owner.uname}-#{@project.name}-/, '')
|
|
|
|
@commit = @project.repo.commits(@sha, 1).first
|
2012-09-19 20:29:36 +01:00
|
|
|
end
|
2012-07-17 09:02:56 +01:00
|
|
|
raise Grit::NoSuchPathError unless @commit
|
2012-09-20 12:10:52 +01:00
|
|
|
name = "#{@project.owner.uname}-#{@project.name}-#{@sha}"
|
2012-09-19 20:29:36 +01:00
|
|
|
fullname = "#{name}.#{format == 'tar' ? 'tar.gz' : 'zip'}"
|
2012-04-17 11:18:03 +01:00
|
|
|
file = Tempfile.new fullname, 'tmp'
|
2012-09-20 12:10:52 +01:00
|
|
|
system("cd #{@project.path}; git archive --format=#{format} --prefix=#{name}/ #{@sha} #{format == 'tar' ? ' | gzip -9' : ''} > #{file.path}")
|
2012-04-17 11:18:03 +01:00
|
|
|
file.close
|
2012-09-19 20:29:36 +01:00
|
|
|
send_file file.path, :disposition => 'attachment', :type => "application/#{format == 'tar' ? 'x-tar-gz' : 'zip'}", :filename => fullname
|
2012-04-17 11:18:03 +01:00
|
|
|
end
|
2012-09-20 12:10:52 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_sha
|
|
|
|
commit = @project.repo.commits(@treeish, 1).first
|
|
|
|
@sha = commit ? commit.id : @treeish
|
|
|
|
end
|
|
|
|
|
2012-01-30 20:39:34 +00:00
|
|
|
end
|