<commit_before>class DownloadGitRevisionController < ApplicationController
  unloadable

  before_filter :require_login
  before_filter :set_repository
  before_filter :set_project
  before_filter :can_download_git_revision

  helper :git_hosting


  def index
    if !params[:rev]
      revision = "master"
    else
      revision = params[:rev]
    end

    format = params[:download_format]

    download = DownloadGitRevision.new(@repository, revision, format)

    if !download.commit_valid
      flash.now[:error] = l(:error_download_revision_no_such_commit, :commit => revision)
      render_404
      return
    end

    begin
      content = download.content
    rescue => e
      flash.now[:error] = l(:git_archive_timeout, :timeout => e.output)
      render_404
      return
    end

    send_data(content, :filename => download.filename, :type => download.content_type)
  end


  private


    def set_repository
      @repository = Repository.find_by_id(params[:id])
      if @repository.nil?
        render_404
      end
    end


    def set_project
      @project = @repository.project
      if @project.nil?
        render_404
      end
    end


    def can_download_git_revision
      render_403 unless view_context.user_allowed_to(:download_git_revision, @project)
    end

end<commit_msg>Rework DownloadGitRevision controller<commit_after>class DownloadGitRevisionController < ApplicationController
  unloadable

  before_filter :require_login
  before_filter :set_repository
  before_filter :can_download_git_revision
  before_filter :set_download
  before_filter :validate_download

  helper :git_hosting


  def index
    begin
      send_data(@download.content, filename: @download.filename, type: @download.content_type)
    rescue => e
      flash.now[:error] = l(:git_archive_timeout, timeout: e.output)
      render_404
    end
  end


  private


    def set_repository
      begin
        @repository = Repository.find(params[:id])
      rescue ActiveRecord::RecordNotFound => e
        render_404
      else
        @project = @repository.project
        render_404 if @project.nil?
      end
    end


    def can_download_git_revision
      render_403 unless view_context.user_allowed_to(:download_git_revision, @project)
    end


    def set_download
      @download = DownloadGitRevision.new(@repository, download_revision, download_format)
    end


    def download_revision
      @download_revision ||= (params[:rev] || 'master')
    end


    def download_format
      @download_format ||= (params[:download_format] || 'tar')
    end


    def validate_download
      if !@download.commit_valid
        flash.now[:error] = l(:error_download_revision_no_such_commit, commit: download_revision)
        render_404
      end
    end

end
{'fraction_non_alphanumeric': 0.07387387387387387, 'fraction_numerical': 0.010810810810810811, 'mean_word_length': 3.16191904047976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7654158', 'n_tokens_mistral': 859, 'n_tokens_neox': 797, 'n_words': 174}