blob_controller.rb 5.2 KB
Newer Older
1
# Controller for viewing a file's blame
2
class Projects::BlobController < Projects::ApplicationController
3
  include ExtractsPath
4
  include CreatesCommit
V
Valery Sizov 已提交
5
  include ActionView::Helpers::SanitizeHelper
6

D
Dmitriy Zaporozhets 已提交
7 8 9
  # Raised when given an invalid file path
  class InvalidPathError < StandardError; end

10 11
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :authorize_download_code!
12
  before_action :authorize_edit_tree!, only: [:new, :create, :edit, :update, :destroy]
13 14 15
  before_action :assign_blob_vars
  before_action :commit, except: [:new, :create]
  before_action :blob, except: [:new, :create]
16
  before_action :require_branch_head, only: [:edit, :update]
17
  before_action :editor_variables, except: [:show, :preview, :diff]
18
  before_action :validate_diff_params, only: :diff
19
  before_action :set_last_commit_sha, only: [:edit, :update]
D
Dmitriy Zaporozhets 已提交
20 21 22 23

  def new
    commit unless @repository.empty?
  end
24

D
Dmitriy Zaporozhets 已提交
25
  def create
26 27
    create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
                                        success_path: namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)),
D
Douwe Maan 已提交
28 29
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
D
Dmitriy Zaporozhets 已提交
30
  end
31

32
  def show
33 34
  end

D
Dmitriy Zaporozhets 已提交
35
  def edit
J
Jacob Vosmaer 已提交
36
    blob.load_all_data!(@repository)
D
Dmitriy Zaporozhets 已提交
37 38 39
  end

  def update
40
    @path = params[:file_path] if params[:file_path].present?
D
Douwe Maan 已提交
41 42 43
    create_commit(Files::UpdateService, success_path: after_edit_path,
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
44 45 46 47

  rescue Files::UpdateService::FileChangedError
    @conflict = true
    render :edit
D
Dmitriy Zaporozhets 已提交
48 49 50 51
  end

  def preview
    @content = params[:content]
J
Jacob Vosmaer 已提交
52
    @blob.load_all_data!(@repository)
53
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
54 55
    diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
    diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
56
    @diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
D
Dmitriy Zaporozhets 已提交
57 58 59 60

    render layout: false
  end

61
  def destroy
62 63 64 65
    create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
                                        success_path: namespace_project_tree_path(@project.namespace, @project, @target_branch),
                                        failure_view: :show,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
66 67
  end

S
skv 已提交
68
  def diff
69 70
    apply_diff_view_cookie!

71
    @form  = UnfoldForm.new(params)
72
    @lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
73
    @lines = @lines[@form.since - 1..@form.to - 1]
S
skv 已提交
74 75 76 77 78 79 80 81 82 83 84 85

    if @form.bottom?
      @match_line = ''
    else
      lines_length = @lines.length - 1
      line = [@form.since, lines_length].join(',')
      @match_line = "@@ -#{line}+#{line} @@"
    end

    render layout: false
  end

86 87 88
  private

  def blob
89
    @blob ||= Blob.decorate(@repository.blob_at(@commit.id, @path))
90

91 92 93
    if @blob
      @blob
    else
D
Dmitriy Zaporozhets 已提交
94 95
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
V
Vinnie Okada 已提交
96
          redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path)) and return
D
Dmitriy Zaporozhets 已提交
97 98 99
        end
      end

V
Valery Sizov 已提交
100
      return render_404
101
    end
102
  end
D
Dmitriy Zaporozhets 已提交
103 104 105 106

  def commit
    @commit = @repository.commit(@ref)

V
Valery Sizov 已提交
107
    return render_404 unless @commit
D
Dmitriy Zaporozhets 已提交
108 109 110 111 112 113 114
  end

  def assign_blob_vars
    @id = params[:id]
    @ref, @path = extract_ref(@id)

  rescue InvalidPathError
V
Valery Sizov 已提交
115
    render_404
D
Dmitriy Zaporozhets 已提交
116 117
  end

118 119 120 121
  def after_edit_path
    from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.find_by(iid: params[:from_merge_request_iid])
    if from_merge_request && @target_branch == @ref
      diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
J
James Lopez 已提交
122
        "#file-path-#{hexdigest(@path)}"
123 124 125
    else
      namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
    end
D
Dmitriy Zaporozhets 已提交
126
  end
V
Valery Sizov 已提交
127

128
  def editor_variables
129
    @target_branch = params[:target_branch]
130 131 132

    @file_path =
      if action_name.to_s == 'create'
L
liyakun 已提交
133 134 135
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
136
        File.join(@path, params[:file_name])
137 138
      elsif params[:file_path].present?
        params[:file_path]
139 140 141 142
      else
        @path
      end

L
liyakun 已提交
143 144 145 146 147
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

148 149 150
    @commit_params = {
      file_path: @file_path,
      commit_message: params[:commit_message],
151
      previous_path: @path,
152
      file_content: params[:content],
153 154
      file_content_encoding: params[:encoding],
      last_commit_sha: params[:last_commit_sha]
155 156
    }
  end
157 158 159 160 161 162

  def validate_diff_params
    if [:since, :to, :offset].any? { |key| params[key].blank? }
      render nothing: true
    end
  end
163 164 165 166 167

  def set_last_commit_sha
    @last_commit_sha = Gitlab::Git::Commit.
      last_for_path(@repository, @ref, @path).sha
  end
168
end