Merge pull request #653 from warpc/385-change-name-for-git-archives

[refs #385] Support download project source code by build clients(rpmbuild)
This commit is contained in:
Vladimir Sharshov 2012-09-20 06:37:13 -07:00
commit 7e28920bc7
3 changed files with 18 additions and 9 deletions

View File

@ -1,6 +1,7 @@
# -*- 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'
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
def show
@tree = @tree / @path if @path.present?
@ -9,13 +10,18 @@ class Projects::Git::TreesController < Projects::Git::BaseController
end
def archive
@commit = @project.repo.log(@treeish, nil, :max_count => 1).first
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}#{@project.repo.tags.include?(@treeish) ? "-#{@treeish}" : ''}-#{@commit.id[0..19]}"
fullname = "#{name}.#{params[:format] == 'tar' ? 'tar.gz' : 'zip'}"
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=#{params[:format]} --prefix=#{name}/ #{@treeish} #{params[:format] == 'tar' ? ' | gzip -9' : ''} > #{file.path}")
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/#{params[:format] == 'tar' ? 'x-tar-gz' : 'zip'}", :filename => fullname
send_file file.path, :disposition => 'attachment', :type => "application/#{format == 'tar' ? 'x-tar-gz' : 'zip'}", :filename => fullname
end
end

View File

@ -6,8 +6,9 @@
=image_tag 'zip.png', :alt => 'ZIP'
%b.caret
%ul.dropdown-menu
%li=link_to "tar.gz", archive_path(project, @treeish, 'tar')
%li=link_to "zip", archive_path(project, @treeish, 'zip')
- file_name = "#{@project.owner.uname}-#{@project.name}-#{@commit.id}"
%li=link_to "tar.gz", archive_path(project, file_name, 'tar')
%li=link_to "zip", archive_path(project, file_name, 'zip')
= text_field_tag :url, git_repo_url(project.git_repo_name), :class => 'name', :spellcheck => 'false', :readonly => true
.git_help ?

View File

@ -12,7 +12,9 @@ describe Projects::Git::TreesController do
@project = FactoryGirl.create(:project)
@another_user = FactoryGirl.create(:user)
@params = {:owner_name => @project.owner.uname, :project_name => @project.name, :treeish => 'master'}
@params = { :owner_name => @project.owner.uname,
:project_name => @project.name,
:treeish => "#{@project.owner.uname}-#{@project.name}-master"}
end
context 'for guest' do