blob_controller.rb 5.83 KB
Newer Older
1
# Controller for viewing a file's blame
2
class Projects::BlobController < Projects::ApplicationController
3
  include ExtractsPath
4
  include CreatesCommit
5
  include RendersBlob
6
  include ActionView::Helpers::SanitizeHelper
7

8
  # Raised when given an invalid file path
9
  InvalidPathError = Class.new(StandardError)
10

11 12
  prepend_before_action :authenticate_user!, only: [:edit]

13 14
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :authorize_download_code!
15
  before_action :authorize_edit_tree!, only: [:new, :create, :update, :destroy]
16 17 18
  before_action :assign_blob_vars
  before_action :commit, except: [:new, :create]
  before_action :blob, except: [:new, :create]
19
  before_action :require_branch_head, only: [:edit, :update]
20
  before_action :editor_variables, except: [:show, :preview, :diff]
21
  before_action :validate_diff_params, only: :diff
22
  before_action :set_last_commit_sha, only: [:edit, :update]
23 24 25 26

  def new
    commit unless @repository.empty?
  end
27

28
  def create
29
    create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
30
                                        success_path: -> { namespace_project_blob_path(@project.namespace, @project, File.join(@branch_name, @file_path)) },
Douwe Maan's avatar
Douwe Maan committed
31 32
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
33
  end
34

35
  def show
36
    conditionally_expand_blob(@blob)
Douwe Maan's avatar
Douwe Maan committed
37

38 39 40 41 42
    respond_to do |format|
      format.html do
        environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
        @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last

43 44
        @last_commit = @repository.last_commit_for_path(@commit.id, @blob.path)

45 46 47 48 49 50 51
        render 'show'
      end

      format.json do
        render_blob_json(@blob)
      end
    end
52 53
  end

54
  def edit
55
    if can_collaborate_with_project?
56
      blob.load_all_data!
57 58 59
    else
      redirect_to action: 'show'
    end
60 61 62
  end

  def update
63
    @path = params[:file_path] if params[:file_path].present?
64
    create_commit(Files::UpdateService, success_path: -> { after_edit_path },
Douwe Maan's avatar
Douwe Maan committed
65 66
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
67 68 69 70

  rescue Files::UpdateService::FileChangedError
    @conflict = true
    render :edit
71 72 73 74
  end

  def preview
    @content = params[:content]
75
    @blob.load_all_data!
76
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
77 78
    diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
    diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
79
    @diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
80 81 82 83

    render layout: false
  end

84
  def destroy
85 86 87 88
    create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
                                        success_path: -> { namespace_project_tree_path(@project.namespace, @project, @branch_name) },
                                        failure_view: :show,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
89 90
  end

skv's avatar
skv committed
91
  def diff
92 93
    apply_diff_view_cookie!

94 95 96 97 98
    @blob.load_all_data!
    @lines = Gitlab::Highlight.highlight(@blob.path, @blob.data, repository: @repository).lines

    @form = UnfoldForm.new(params)
    @lines = @lines[@form.since - 1..@form.to - 1].map(&:html_safe)
skv's avatar
skv committed
99 100 101 102 103 104 105 106 107 108 109 110

    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

111 112 113
  private

  def blob
114
    @blob ||= @repository.blob_at(@commit.id, @path)
115

116 117 118
    if @blob
      @blob
    else
119 120
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
Douwe Maan's avatar
Douwe Maan committed
121
          return redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path))
122 123 124
        end
      end

125
      return render_404
126
    end
127
  end
128 129 130 131

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

132
    return render_404 unless @commit
133 134 135 136 137 138 139
  end

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

  rescue InvalidPathError
140
    render_404
141 142
  end

143 144
  def after_edit_path
    from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.find_by(iid: params[:from_merge_request_iid])
145
    if from_merge_request && @branch_name == @ref
146 147 148
      diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
        "##{hexdigest(@path)}"
    else
149
      namespace_project_blob_path(@project.namespace, @project, File.join(@branch_name, @path))
150
    end
151
  end
152

153
  def editor_variables
154
    @branch_name = params[:branch_name]
155 156 157

    @file_path =
      if action_name.to_s == 'create'
158 159 160
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
161
        File.join(@path, params[:file_name])
162 163
      elsif params[:file_path].present?
        params[:file_path]
164 165 166 167
      else
        @path
      end

168 169 170 171 172
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

173 174 175
    @commit_params = {
      file_path: @file_path,
      commit_message: params[:commit_message],
176
      previous_path: @path,
177
      file_content: params[:content],
178 179
      file_content_encoding: params[:encoding],
      last_commit_sha: params[:last_commit_sha]
180 181
    }
  end
182 183 184 185 186 187

  def validate_diff_params
    if [:since, :to, :offset].any? { |key| params[key].blank? }
      render nothing: true
    end
  end
188 189 190 191 192

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