blob_controller.rb 5.5 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
  # Raised when given an invalid file path
8
  InvalidPathError = Class.new(StandardError)
D
Dmitriy Zaporozhets 已提交
9

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
    update_ref

28
    create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
29
                                        success_path: -> { namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @file_path)) },
D
Douwe Maan 已提交
30 31
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
D
Dmitriy Zaporozhets 已提交
32
  end
33

34
  def show
D
Douwe Maan 已提交
35 36
    environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
    @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
37 38
  end

D
Dmitriy Zaporozhets 已提交
39
  def edit
J
Jacob Vosmaer 已提交
40
    blob.load_all_data!(@repository)
D
Dmitriy Zaporozhets 已提交
41 42 43
  end

  def update
44
    @path = params[:file_path] if params[:file_path].present?
45
    create_commit(Files::UpdateService, success_path: -> { after_edit_path },
D
Douwe Maan 已提交
46 47
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
48 49 50 51

  rescue Files::UpdateService::FileChangedError
    @conflict = true
    render :edit
D
Dmitriy Zaporozhets 已提交
52 53 54 55
  end

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

    render layout: false
  end

65
  def destroy
66
    create_commit(Files::DestroyService, success_notice: "The file has been successfully deleted.",
67
                                         success_path: -> { namespace_project_tree_path(@project.namespace, @project, @target_branch) },
68 69
                                         failure_view: :show,
                                         failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
70 71
  end

S
skv 已提交
72
  def diff
73 74
    apply_diff_view_cookie!

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

    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

90 91
  private

92 93 94 95 96
  def update_ref
    branch_exists = @repository.find_branch(@target_branch)
    @ref = @target_branch if branch_exists
  end

97
  def blob
98
    @blob ||= Blob.decorate(@repository.blob_at(@commit.id, @path))
99

100 101 102
    if @blob
      @blob
    else
D
Dmitriy Zaporozhets 已提交
103 104
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
D
Douwe Maan 已提交
105
          return redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path))
D
Dmitriy Zaporozhets 已提交
106 107 108
        end
      end

V
Valery Sizov 已提交
109
      return render_404
110
    end
111
  end
D
Dmitriy Zaporozhets 已提交
112 113 114 115

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

V
Valery Sizov 已提交
116
    return render_404 unless @commit
D
Dmitriy Zaporozhets 已提交
117 118 119 120 121 122 123
  end

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

  rescue InvalidPathError
V
Valery Sizov 已提交
124
    render_404
D
Dmitriy Zaporozhets 已提交
125 126
  end

127 128 129 130 131 132 133 134
  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) +
        "##{hexdigest(@path)}"
    else
      namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
    end
D
Dmitriy Zaporozhets 已提交
135
  end
V
Valery Sizov 已提交
136

137
  def editor_variables
138
    @target_branch = params[:target_branch]
139 140 141

    @file_path =
      if action_name.to_s == 'create'
L
liyakun 已提交
142 143 144
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
145
        File.join(@path, params[:file_name])
146 147
      elsif params[:file_path].present?
        params[:file_path]
148 149 150 151
      else
        @path
      end

L
liyakun 已提交
152 153 154 155 156
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

157 158 159
    @commit_params = {
      file_path: @file_path,
      commit_message: params[:commit_message],
160
      previous_path: @path,
161
      file_content: params[:content],
162 163
      file_content_encoding: params[:encoding],
      last_commit_sha: params[:last_commit_sha]
164 165
    }
  end
166 167 168 169 170 171

  def validate_diff_params
    if [:since, :to, :offset].any? { |key| params[key].blank? }
      render nothing: true
    end
  end
172 173 174 175 176

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