summaryrefslogtreecommitdiff
path: root/app/controllers/refs_controller.rb
blob: 3f81a2ca1a3cc00311982aae9f9822f1b1d9c359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
class RefsController < ApplicationController
  include Gitlab::Encode
  before_filter :project

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

  before_filter :ref
  before_filter :define_tree_vars, only: [:tree, :blob, :blame, :logs_tree]
  before_filter :render_full_content

  layout "project"

  def switch 
    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

        redirect_to new_path 
      end
      format.js do 
        @ref = params[:ref]
        define_tree_vars
        render "tree"
      end
    end
  end

  #
  # Repository preview
  #
  def tree
    respond_to do |format|
      format.html
      format.js do
        # disable cache to allow back button works
        no_cache_headers
      end
    end
  end

  def logs_tree
    contents = @tree.contents
    @logs = contents.map do |content|
      file = params[:path] ? File.join(params[:path], content.name) : content.name
      last_commit = @project.commits(@commit.id, file, 1).last
      last_commit = CommitDecorator.decorate(last_commit)
      {
        file_name: content.name, 
        commit: last_commit
      }
    end
  end

  def blob
    if @tree.is_blob?
      if @tree.text?
        encoding = detect_encoding(@tree.data)
        mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain"
      else
        mime_type = @tree.mime_type
      end

      send_data(
        @tree.data,
        type: mime_type,
        disposition: 'inline',
        filename: @tree.name
      )
    else
      head(404)
    end
  end

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

  protected

  def define_tree_vars
    params[:path] = nil if params[:path].blank?

    @repo = project.repo
    @commit = project.commit(@ref)
    @commit = CommitDecorator.decorate(@commit)
    @tree = Tree.new(@commit.tree, project, @ref, params[:path])
    @tree = TreeDecorator.new(@tree)
    @hex_path = Digest::SHA1.hexdigest(params[:path] || "/")

    if params[:path]
      @history_path = tree_file_project_ref_path(@project, @ref, params[:path])
      @logs_path = logs_file_project_ref_path(@project, @ref, params[:path]) 
    else
      @history_path = tree_project_ref_path(@project, @ref)
      @logs_path = logs_tree_project_ref_path(@project, @ref) 
    end
  rescue
    return render_404
  end
    
  def ref
    @ref = params[:id]
  end
end