BigW Consortium Gitlab

blob_controller.rb 5.04 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 16
  before_action :assign_blob_vars
  before_action :commit, except: [:new, :create]
  before_action :blob, except: [:new, :create]
  before_action :from_merge_request, only: [:edit, :update]
17
  before_action :require_branch_head, only: [:edit, :update]
18
  before_action :editor_variables, except: [:show, :preview, :diff]
19
  before_action :validate_diff_params, only: :diff
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 committed
28 29
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
30
  end
31

32
  def show
33 34
  end

35 36
  def edit
    @last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
Jacob Vosmaer committed
37
    blob.load_all_data!(@repository)
38 39 40
  end

  def update
41
    if params[:file_path].present?
42
      @previous_path = @path
43
      @path = params[:file_path]
44
      @commit_params[:file_path] = @path
45 46
    end

47 48 49
    after_edit_path =
      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) +
50
          "#file-path-#{hexdigest(@path)}"
51
      else
52
        namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
53 54
      end

Douwe Maan committed
55 56 57
    create_commit(Files::UpdateService, success_path: after_edit_path,
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
58 59 60 61
  end

  def preview
    @content = params[:content]
Jacob Vosmaer committed
62
    @blob.load_all_data!(@repository)
63
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
64 65
    diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
    diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
66
    @diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
67 68 69 70

    render layout: false
  end

71
  def destroy
72 73 74 75
    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))
76 77
  end

skv committed
78
  def diff
79 80
    apply_diff_view_cookie!

81
    @form  = UnfoldForm.new(params)
82
    @lines = Gitlab::Highlight.highlight_lines(repository, @ref, @path)
83
    @lines = @lines[@form.since - 1..@form.to - 1]
skv committed
84 85 86 87 88 89 90 91 92 93 94 95

    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

96 97 98
  private

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

101 102 103
    if @blob
      @blob
    else
104 105
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
Vinnie Okada committed
106
          redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path)) and return
107 108 109
        end
      end

110
      return render_404
111
    end
112
  end
113 114 115 116

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

117
    return render_404 unless @commit
118 119 120 121 122 123 124
  end

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

  rescue InvalidPathError
125
    render_404
126 127 128 129 130 131
  end

  def from_merge_request
    # If blob edit was initiated from merge request page
    @from_merge_request ||= MergeRequest.find_by(id: params[:from_merge_request_id])
  end
132

133
  def editor_variables
134
    @target_branch = params[:target_branch]
135 136 137

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

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

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

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