BigW Consortium Gitlab

refs_controller.rb 1.88 KB
Newer Older
gitlabhq committed
1
class RefsController < ApplicationController
randx committed
2
  include Gitlab::Encode
gitlabhq committed
3 4 5 6 7
  before_filter :project

  # Authorize
  before_filter :add_project_abilities
  before_filter :authorize_read_project!
8
  before_filter :authorize_code_access!
gitlabhq committed
9 10
  before_filter :require_non_empty_project

11
  before_filter :ref
12
  before_filter :define_tree_vars, :only => [:tree, :blob, :blame]
13 14
  before_filter :render_full_content

15 16
  layout "project"

gitlabhq committed
17
  def switch 
18 19 20 21 22 23 24
    respond_to do |format| 
      format.html do 
        new_path = if params[:destination] == "tree"
                     tree_project_ref_path(@project, params[:ref]) 
                   else
                     project_commits_path(@project, :ref => params[:ref])
                   end
gitlabhq committed
25

26 27 28 29 30 31 32 33
        redirect_to new_path 
      end
      format.js do 
        @ref = params[:ref]
        define_tree_vars
        render "tree"
      end
    end
gitlabhq committed
34 35
  end

gitlabhq committed
36 37 38 39 40
  #
  # Repository preview
  #
  def tree
    respond_to do |format|
41
      format.html
42 43 44 45
      format.js do
        # disable cache to allow back button works
        no_cache_headers
      end
gitlabhq committed
46 47 48 49
    end
  end

  def blob
gitlabhq committed
50
    if @tree.is_blob?
51 52 53 54 55 56 57
      if @tree.text?
        encoding = detect_encoding(@tree.data)
        mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain"
      else
        mime_type = @tree.mime_type
      end

58 59
      send_data(
        @tree.data,
60
        :type => mime_type,
61 62 63
        :disposition => 'inline',
        :filename => @tree.name
      )
gitlabhq committed
64 65 66 67 68
    else
      head(404)
    end
  end

69 70 71 72
  def blame
    @blame = Grit::Blob.blame(@repo, @commit.id, params[:path])
  end

gitlabhq committed
73 74
  protected

gitlabhq committed
75
  def define_tree_vars
76 77
    params[:path] = nil if params[:path].blank?

gitlabhq committed
78 79 80 81
    @repo = project.repo
    @commit = project.commit(@ref)
    @tree = Tree.new(@commit.tree, project, @ref, params[:path])
    @tree = TreeDecorator.new(@tree)
Dmitriy Zaporozhets committed
82 83
  rescue
    return render_404
gitlabhq committed
84 85
  end
    
gitlabhq committed
86 87 88 89
  def ref
    @ref = params[:id]
  end
end