BigW Consortium Gitlab

blob_controller.rb 4.66 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 20 21 22

  def new
    commit unless @repository.empty?
  end
23

24
  def create
25 26
    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
27 28
                                        failure_view: :new,
                                        failure_path: namespace_project_new_blob_path(@project.namespace, @project, @ref))
29
  end
30

31
  def show
32 33
  end

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

  def update
40 41 42 43 44 45 46 47
    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) +
          "#file-path-#{hexdigest(@path)}"
      else
        namespace_project_blob_path(@project.namespace, @project, File.join(@target_branch, @path))
      end

Douwe Maan committed
48 49 50
    create_commit(Files::UpdateService, success_path: after_edit_path,
                                        failure_view: :edit,
                                        failure_path: namespace_project_blob_path(@project.namespace, @project, @id))
51 52 53 54
  end

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

    render layout: false
  end

64
  def destroy
65 66 67 68
    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))
69 70
  end

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

    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

87 88 89
  private

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

92 93 94
    if @blob
      @blob
    else
95 96
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
Vinnie Okada committed
97
          redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path)) and return
98 99 100
        end
      end

101
      return render_404
102
    end
103
  end
104 105 106 107

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

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

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

  rescue InvalidPathError
116
    render_404
117 118 119 120 121 122
  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
123

124
  def editor_variables
125
    @target_branch = params[:target_branch]
126 127 128

    @file_path =
      if action_name.to_s == 'create'
129 130 131
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
132
        File.join(@path, params[:file_name])
133 134 135 136
      else
        @path
      end

137 138 139 140 141
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

142 143 144 145 146 147 148
    @commit_params = {
      file_path: @file_path,
      commit_message: params[:commit_message],
      file_content: params[:content],
      file_content_encoding: params[:encoding]
    }
  end
149
end