summaryrefslogtreecommitdiff
path: root/app/controllers/projects/branches_controller.rb
blob: cff1a907dc22be246f23e09b9cee0ed5d840bf65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Projects::BranchesController < Projects::ApplicationController
  include ActionView::Helpers::SanitizeHelper
  # Authorize
  before_filter :require_non_empty_project

  before_filter :authorize_download_code!
  before_filter :authorize_push_code!, only: [:create, :destroy]

  def index
    @sort = params[:sort] || 'name'
    @branches = @repository.branches_sorted_by(@sort)
    @branches = Kaminari.paginate_array(@branches).page(params[:page]).per(30)
  end

  def recent
    @branches = @repository.recent_branches
  end

  def create
    branch_name = sanitize(strip_tags(params[:branch_name]))
    ref = sanitize(strip_tags(params[:ref]))
    result = CreateBranchService.new(project, current_user).
        execute(branch_name, ref)

    if result[:status] == :success
      @branch = result[:branch]
      redirect_to project_tree_path(@project, @branch.name)
    else
      @error = result[:message]
      render action: 'new'
    end
  end

  def destroy
    DeleteBranchService.new(project, current_user).execute(params[:id])
    @branch_name = params[:id]

    respond_to do |format|
      format.html { redirect_to project_branches_path(@project) }
      format.js
    end
  end
end