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

  # Authorize
  before_filter :authorize_read_project!
7
  before_filter :authorize_download_code!
8
  before_filter :require_non_empty_project
9
  before_filter :authorize_push_code!, only: [:destroy]
10

11 12
  before_filter :blob

13
  def show
14 15 16
  end

  def destroy
17
    result = Files::DeleteService.new(@project, current_user, params, @ref, @path).execute
18 19

    if result[:status] == :success
Takuya Nishigori's avatar
Takuya Nishigori committed
20
      flash[:notice] = "Your changes have been successfully committed"
21 22
      redirect_to project_tree_path(@project, @ref)
    else
23
      flash[:alert] = result[:message]
24 25 26 27
      render :show
    end
  end

skv's avatar
skv committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  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

43 44 45 46 47
  private

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

48 49 50 51 52 53 54
    if @blob
      @blob
    elsif tree.entries.any?
      redirect_to project_tree_path(@project, File.join(@ref, @path)) and return
    else
      return not_found!
    end
55 56
  end
end