blob_controller.rb 5.72 KB
Newer Older
1
# Controller for viewing a file's blame
2
class Projects::BlobController < Projects::ApplicationController
3
  include ExtractsPath
4
  include CreatesMergeRequestForCommit
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!
Stan Hu's avatar
Stan Hu committed
12
  before_action :authorize_push_code!, only: [:destroy, :create]
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 19
  before_action :editor_variables, except: [:show, :preview, :diff]
  before_action :after_edit_path, only: [:edit, :update]
20 21 22 23

  def new
    commit unless @repository.empty?
  end
24

25
  def create
26
    result = Files::CreateService.new(@project, current_user, @commit_params).execute
27 28

    if result[:status] == :success
Stan Hu's avatar
Stan Hu committed
29
      flash[:notice] = "The changes have been successfully committed"
30
      respond_to do |format|
31 32
        format.html { redirect_to after_create_path }
        format.json { render json: { message: "success", filePath: after_create_path } }
33
      end
34 35
    else
      flash[:alert] = result[:message]
36 37
      respond_to do |format|
        format.html { render :new }
Stan Hu's avatar
Stan Hu committed
38
        format.json { render json: { message: "failed", filePath: namespace_project_blob_path(@project.namespace, @project, @id) } }
39
      end
40 41
    end
  end
42

43
  def show
44 45
  end

46 47 48 49 50
  def edit
    @last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
  end

  def update
51
    result = Files::UpdateService.new(@project, current_user, @commit_params).execute
52 53 54

    if result[:status] == :success
      flash[:notice] = "Your changes have been successfully committed"
55
      respond_to do |format|
56
        format.html { redirect_to after_edit_path }
57 58
        format.json { render json: { message: "success", filePath: after_edit_path } }
      end
59 60
    else
      flash[:alert] = result[:message]
61 62 63 64
      respond_to do |format|
        format.html { render :edit }
        format.json { render json: { message: "failed", filePath: namespace_project_new_blob_path(@project.namespace, @project, @id) } }
      end
65 66 67 68 69
    end
  end

  def preview
    @content = params[:content]
70
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
71 72 73 74 75
    @diff_lines = Gitlab::Diff::Parser.new.parse(diffy.diff.scan(/.*\n/))

    render layout: false
  end

76
  def destroy
77
    result = Files::DeleteService.new(@project, current_user, @commit_params).execute
78 79

    if result[:status] == :success
Takuya Nishigori's avatar
Takuya Nishigori committed
80
      flash[:notice] = "Your changes have been successfully committed"
81
      redirect_to after_destroy_path
82
    else
83
      flash[:alert] = result[:message]
84 85 86 87
      render :show
    end
  end

skv's avatar
skv committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  def diff
    @form = UnfoldForm.new(params)
    @lines = @blob.data.lines[@form.since - 1..@form.to - 1]

    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

103 104 105 106 107
  private

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

108 109 110
    if @blob
      @blob
    else
111 112
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
Vinnie Okada's avatar
Vinnie Okada committed
113
          redirect_to namespace_project_tree_path(@project.namespace, @project, File.join(@ref, @path)) and return
114 115 116
        end
      end

117
      return render_404
118
    end
119
  end
120 121 122 123

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

124
    return render_404 unless @commit
125 126 127 128 129 130 131
  end

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

  rescue InvalidPathError
132
    render_404
133 134
  end

135 136 137 138 139 140 141 142 143
  def after_create_path
    @after_create_path ||=
      if create_merge_request?
        new_merge_request_path
      else
        namespace_project_blob_path(@project.namespace, @project, File.join(@new_branch, @file_path))
      end
  end

144 145
  def after_edit_path
    @after_edit_path ||=
146 147 148
      if create_merge_request?
        new_merge_request_path
      elsif from_merge_request && @new_branch == @ref
Vinnie Okada's avatar
Vinnie Okada committed
149
        diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
150 151
          "#file-path-#{hexdigest(@path)}"
      else
152 153 154 155 156 157 158 159 160 161
        namespace_project_blob_path(@project.namespace, @project, File.join(@new_branch, @path))
      end
  end

  def after_destroy_path
    @after_destroy_path ||=
      if create_merge_request?
        new_merge_request_path
      else
        namespace_project_tree_path(@project.namespace, @project, @new_branch)
162 163 164 165 166 167 168
      end
  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
169 170 171 172

  def sanitized_new_branch_name
    @new_branch ||= sanitize(strip_tags(params[:new_branch]))
  end
173 174 175

  def editor_variables
    @current_branch = @ref
176
    @new_branch = params[:new_branch].present? ? sanitized_new_branch_name : @ref
177 178 179

    @file_path =
      if action_name.to_s == 'create'
180 181 182
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
183
        File.join(@path, params[:file_name])
184 185 186 187
      else
        @path
      end

188 189 190 191 192
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

193 194 195
    @commit_params = {
      file_path: @file_path,
      current_branch: @current_branch,
196
      target_branch: @new_branch,
197 198 199 200 201
      commit_message: params[:commit_message],
      file_content: params[:content],
      file_content_encoding: params[:encoding]
    }
  end
202
end