diff --git a/app/controllers/projects/git/base_controller.rb b/app/controllers/projects/git/base_controller.rb index a77c39ffd..60dd9792b 100644 --- a/app/controllers/projects/git/base_controller.rb +++ b/app/controllers/projects/git/base_controller.rb @@ -10,7 +10,11 @@ class Projects::Git::BaseController < Projects::BaseController protected def set_treeish_and_path - @treeish = params[:treeish].presence || @project.default_branch + @treeish = params[:treeish].presence + unless @treeish + commit = @project.repo.commits(@project.default_branch).first + @treeish = commit ? commit.id : @project.default_branch + end @path = params[:path] end diff --git a/app/controllers/projects/git/trees_controller.rb b/app/controllers/projects/git/trees_controller.rb index 45dae2aa1..c133dfa3b 100644 --- a/app/controllers/projects/git/trees_controller.rb +++ b/app/controllers/projects/git/trees_controller.rb @@ -1,6 +1,8 @@ # -*- 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 +11,17 @@ 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.log(@treeish, nil, :max_count => 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 diff --git a/app/views/projects/base/_repo_block.html.haml b/app/views/projects/base/_repo_block.html.haml index f301d63b3..9913c451c 100644 --- a/app/views/projects/base/_repo_block.html.haml +++ b/app/views/projects/base/_repo_block.html.haml @@ -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}-#{@treeish}" + %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 ? diff --git a/spec/controllers/projects/git/git_trees_controller_spec.rb b/spec/controllers/projects/git/git_trees_controller_spec.rb index 61c4d68af..4f4941c02 100644 --- a/spec/controllers/projects/git/git_trees_controller_spec.rb +++ b/spec/controllers/projects/git/git_trees_controller_spec.rb @@ -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