BigW Consortium Gitlab

branches_controller.rb 2.03 KB
Newer Older
1
class Projects::BranchesController < Projects::ApplicationController
2
  include ActionView::Helpers::SanitizeHelper
3
  # Authorize
4 5
  before_action :require_non_empty_project
  before_action :authorize_download_code!
6
  before_action :authorize_push_code!, only: [:new, :create, :destroy]
7 8

  def index
9 10
    @sort = params[:sort] || 'name'
    @branches = @repository.branches_sorted_by(@sort)
11
    @branches = Kaminari.paginate_array(@branches).page(params[:page])
12

13
    @max_commits = @branches.reduce(0) do |memo, branch|
14 15 16
      diverging_commit_counts = repository.diverging_commit_counts(branch)
      [memo, diverging_commit_counts[:behind], diverging_commit_counts[:ahead]].max
    end
17 18
  end

19 20 21 22
  def recent
    @branches = @repository.recent_branches
  end

23
  def create
24
    branch_name = sanitize(strip_tags(params[:branch_name]))
25
    branch_name = Addressable::URI.unescape(branch_name)
26

27
    result = CreateBranchService.new(project, current_user).
28
        execute(branch_name, ref)
29

30 31 32
    if params[:issue_iid]
      issue = @project.issues.find_by(iid: params[:issue_iid])
      SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
33 34
    end

35 36
    if result[:status] == :success
      @branch = result[:branch]
Vinnie Okada committed
37 38
      redirect_to namespace_project_tree_path(@project.namespace, @project,
                                              @branch.name)
39 40 41 42
    else
      @error = result[:message]
      render action: 'new'
    end
43 44 45
  end

  def destroy
46 47
    @branch_name = Addressable::URI.unescape(params[:id])
    status = DeleteBranchService.new(project, current_user).execute(@branch_name)
48
    respond_to do |format|
Vinnie Okada committed
49 50 51 52
      format.html do
        redirect_to namespace_project_branches_path(@project.namespace,
                                                    @project)
      end
53
      format.js { render status: status[:return_code] }
54 55
    end
  end
56 57 58 59 60 61 62 63 64 65 66

  private

  def ref
    if params[:ref]
      ref_escaped = sanitize(strip_tags(params[:ref]))
      Addressable::URI.unescape(ref_escaped)
    else
      @project.default_branch
    end
  end
67
end