tree_controller.rb 1.74 KB
Newer Older
1 2
# frozen_string_literal: true

3
# Controller for viewing a repository's file structure
4 5
class Projects::TreeController < Projects::ApplicationController
  include ExtractsPath
6
  include CreatesCommit
Stan Hu's avatar
Stan Hu committed
7
  include ActionView::Helpers::SanitizeHelper
8
  include RedirectsForMissingPathOnTree
9

10 11
  around_action :allow_gitaly_ref_name_caching, only: [:show]

12 13
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :assign_ref_vars
Stan Hu's avatar
Stan Hu committed
14
  before_action :assign_dir_vars, only: [:create_dir]
15
  before_action :authorize_download_code!
16
  before_action :authorize_edit_tree!, only: [:create_dir]
17

18
  def show
19
    return render_404 unless @repository.commit(@ref)
20

21 22
    if tree.entries.empty?
      if @repository.blob_at(@commit.id, @path)
23
        return redirect_to project_blob_path(@project, File.join(@ref, @path))
24
      elsif @path.present?
25
        return redirect_to_tree_root_for_missing_path(@project, @ref, @path)
26 27
      end
    end
28

29
    respond_to do |format|
30
      format.html do
31
        lfs_blob_ids
32 33
        @last_commit = @repository.last_commit_for_path(@commit.id, @tree.path) || @commit
      end
34 35
    end
  end
Stan Hu's avatar
Stan Hu committed
36 37

  def create_dir
38
    return render_404 unless @commit_params.values.all?
Stan Hu's avatar
Stan Hu committed
39

40
    create_commit(Files::CreateDirService,  success_notice: _("The directory has been successfully created."),
41 42
                                            success_path: project_tree_path(@project, File.join(@branch_name, @dir_name)),
                                            failure_path: project_tree_path(@project, @ref))
Stan Hu's avatar
Stan Hu committed
43 44
  end

45 46
  private

Stan Hu's avatar
Stan Hu committed
47
  def assign_dir_vars
48
    @branch_name = params[:branch_name]
49

Stan Hu's avatar
Stan Hu committed
50 51 52
    @dir_name = File.join(@path, params[:dir_name])
    @commit_params = {
      file_path: @dir_name,
53
      commit_message: params[:commit_message]
Stan Hu's avatar
Stan Hu committed
54 55
    }
  end
56
end