blob_controller.rb 3.8 KB
Newer Older
1
# Controller for viewing a file's blame
2
class Projects::BlobController < Projects::ApplicationController
3
  include ExtractsPath
4

D
Dmitriy Zaporozhets 已提交
5 6 7
  # Raised when given an invalid file path
  class InvalidPathError < StandardError; end

8
  before_filter :authorize_download_code!
D
Dmitriy Zaporozhets 已提交
9
  before_filter :require_non_empty_project, except: [:new, :create]
10
  before_filter :authorize_push_code!, only: [:destroy]
D
Dmitriy Zaporozhets 已提交
11 12 13 14 15 16 17 18 19 20
  before_filter :assign_blob_vars
  before_filter :commit, except: [:new, :create]
  before_filter :blob, except: [:new, :create]
  before_filter :from_merge_request, only: [:edit, :update]
  before_filter :after_edit_path, only: [:edit, :update]
  before_filter :require_branch_head, only: [:edit, :update]

  def new
    commit unless @repository.empty?
  end
21

D
Dmitriy Zaporozhets 已提交
22 23 24 25 26 27
  def create
    file_path = File.join(@path, File.basename(params[:file_name]))
    result = Files::CreateService.new(@project, current_user, params, @ref, file_path).execute

    if result[:status] == :success
      flash[:notice] = "Your changes have been successfully committed"
V
Vinnie Okada 已提交
28
      redirect_to namespace_project_blob_path(@project.namespace, @project, File.join(@ref, file_path))
D
Dmitriy Zaporozhets 已提交
29 30
    else
      flash[:alert] = result[:message]
31
      render :new
D
Dmitriy Zaporozhets 已提交
32 33
    end
  end
34

35
  def show
36 37
  end

D
Dmitriy Zaporozhets 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  def edit
    @last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
  end

  def update
    result = Files::UpdateService.
      new(@project, current_user, params, @ref, @path).execute

    if result[:status] == :success
      flash[:notice] = "Your changes have been successfully committed"

      if from_merge_request
        from_merge_request.reload_code
      end

      redirect_to after_edit_path
    else
      flash[:alert] = result[:message]
56
      render :edit
D
Dmitriy Zaporozhets 已提交
57 58 59 60 61
    end
  end

  def preview
    @content = params[:content]
62
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
D
Dmitriy Zaporozhets 已提交
63 64 65 66 67
    @diff_lines = Gitlab::Diff::Parser.new.parse(diffy.diff.scan(/.*\n/))

    render layout: false
  end

68
  def destroy
69
    result = Files::DeleteService.new(@project, current_user, params, @ref, @path).execute
70 71

    if result[:status] == :success
T
Takuya Nishigori 已提交
72
      flash[:notice] = "Your changes have been successfully committed"
V
Vinnie Okada 已提交
73 74
      redirect_to namespace_project_tree_path(@project.namespace, @project,
                                              @ref)
75
    else
76
      flash[:alert] = result[:message]
77 78 79 80
      render :show
    end
  end

S
skv 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  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

96 97 98 99 100
  private

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

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

110 111
      return not_found!
    end
112
  end
D
Dmitriy Zaporozhets 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

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

    return not_found! unless @commit
  end

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


  rescue InvalidPathError
    not_found!
  end

  def after_edit_path
    @after_edit_path ||=
      if from_merge_request
V
Vinnie Okada 已提交
132
        diffs_namespace_project_merge_request_path(from_merge_request.target_project.namespace, from_merge_request.target_project, from_merge_request) +
D
Dmitriy Zaporozhets 已提交
133 134
          "#file-path-#{hexdigest(@path)}"
      else
V
Vinnie Okada 已提交
135
        namespace_project_blob_path(@project.namespace, @project, @id)
D
Dmitriy Zaporozhets 已提交
136 137 138 139 140 141 142
      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
143
end