refs_controller.rb 1.25 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2 3
class RefsController < ApplicationController
  before_filter :project
  before_filter :ref
gitlabhq's avatar
gitlabhq committed
4
  before_filter :define_tree_vars, :only => [:tree, :blob]
gitlabhq's avatar
gitlabhq committed
5 6 7 8 9 10 11
  layout "project"

  # Authorize
  before_filter :add_project_abilities
  before_filter :authorize_read_project!
  before_filter :require_non_empty_project

gitlabhq's avatar
gitlabhq committed
12 13 14 15 16 17 18 19 20 21
  def switch 
    new_path = if params[:destination] == "tree"
                 tree_project_ref_path(@project, params[:ref]) 
               else
                 project_commits_path(@project, :ref => params[:ref])
               end

    redirect_to new_path
  end

gitlabhq's avatar
gitlabhq committed
22 23 24 25 26
  #
  # Repository preview
  #
  def tree
    respond_to do |format|
27
      format.html
gitlabhq's avatar
gitlabhq committed
28
      format.js do
29 30
        # disable cache to allow back button works
        no_cache_headers
gitlabhq's avatar
gitlabhq committed
31 32 33 34 35 36 37
      end
    end
  rescue
    return render_404
  end

  def blob
gitlabhq's avatar
gitlabhq committed
38
    if @tree.is_blob?
gitlabhq's avatar
gitlabhq committed
39 40 41 42 43 44 45 46 47 48
      send_data(@tree.data, :type => @tree.mime_type, :disposition => 'inline', :filename => @tree.name)
    else
      head(404)
    end
  rescue
    return render_404
  end

  protected

gitlabhq's avatar
gitlabhq committed
49 50 51 52 53 54 55
  def define_tree_vars
    @repo = project.repo
    @commit = project.commit(@ref)
    @tree = Tree.new(@commit.tree, project, @ref, params[:path])
    @tree = TreeDecorator.new(@tree)
  end
    
gitlabhq's avatar
gitlabhq committed
56 57 58 59
  def ref
    @ref = params[:id]
  end
end