blob_controller.rb 5.37 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 ActionView::Helpers::SanitizeHelper
6

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]
20 21 22 23

  def new
    commit unless @repository.empty?
  end
24

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)),
Douwe Maan's avatar
Douwe Maan committed
28 29
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
30
  end
31

32
  def show
Douwe Maan's avatar
Douwe Maan committed
33 34
    environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
    @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
35 36
  end

37
  def edit
Jacob Vosmaer's avatar
Jacob Vosmaer committed
38
    blob.load_all_data!(@repository)
39 40 41
  end

  def update
42
    @path = params[:file_path] if params[:file_path].present?
Douwe Maan's avatar
Douwe Maan committed
43 44 45
    create_commit(Files::UpdateService, success_path: after_edit_path,
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
46 47 48 49

  rescue Files::UpdateService::FileChangedError
    @conflict = true
    render :edit
50 51 52 53
  end

  def preview
    @content = params[:content]
Jacob Vosmaer's avatar
Jacob Vosmaer committed
54
    @blob.load_all_data!(@repository)
55
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
56 57
    diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
    diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
58
    @diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
59 60 61 62

    render layout: false
  end

63
  def destroy
64 65 66 67
    create_commit(Files::DestroyService, 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))
68 69
  end

skv's avatar
skv committed
70
  def diff
71 72
    apply_diff_view_cookie!

73
    @form  = UnfoldForm.new(params)
74
    @lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
75
    @lines = @lines[@form.since - 1..@form.to - 1]
skv's avatar
skv committed
76 77 78 79 80 81 82 83 84 85 86 87

    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

88 89 90
  private

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

93 94 95
    if @blob
      @blob
    else
96 97
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
Douwe Maan's avatar
Douwe Maan committed
98
          return redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path))
99 100 101
        end
      end

102
      return render_404
103
    end
104
  end
105 106 107 108

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

109
    return render_404 unless @commit
110 111 112 113 114 115 116
  end

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

  rescue InvalidPathError
117
    render_404
118 119
  end

120 121 122 123 124 125 126 127
  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
128
  end
129

130
  def editor_variables
131
    @target_branch = params[:target_branch]
132 133 134

    @file_path =
      if action_name.to_s == 'create'
135 136 137
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
138
        File.join(@path, params[:file_name])
139 140
      elsif params[:file_path].present?
        params[:file_path]
141 142 143 144
      else
        @path
      end

145 146 147 148 149
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

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

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

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