2013-07-16 15:19:07 -04:00
|
|
|
class Projects::BranchesController < Projects::ApplicationController
|
2014-11-13 10:20:43 -05:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2013-07-16 15:19:07 -04:00
|
|
|
# Authorize
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_download_code!
|
2015-12-03 12:53:17 -05:00
|
|
|
before_action :authorize_push_code!, only: [:new, :create, :destroy]
|
2013-07-16 15:19:07 -04:00
|
|
|
|
|
|
|
def index
|
2014-05-23 07:25:55 -04:00
|
|
|
@sort = params[:sort] || 'name'
|
|
|
|
@branches = @repository.branches_sorted_by(@sort)
|
2016-03-19 17:37:54 -04:00
|
|
|
@branches = Kaminari.paginate_array(@branches).page(params[:page])
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2015-11-11 17:29:29 -05:00
|
|
|
@max_commits = @branches.reduce(0) do |memo, branch|
|
2015-07-20 21:34:19 -04:00
|
|
|
diverging_commit_counts = repository.diverging_commit_counts(branch)
|
|
|
|
[memo, diverging_commit_counts[:behind], diverging_commit_counts[:ahead]].max
|
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
|
2013-08-05 10:59:58 -04:00
|
|
|
def recent
|
|
|
|
@branches = @repository.recent_branches
|
|
|
|
end
|
|
|
|
|
2013-07-16 15:19:07 -04:00
|
|
|
def create
|
2014-11-13 10:20:43 -05:00
|
|
|
branch_name = sanitize(strip_tags(params[:branch_name]))
|
2015-08-02 02:33:33 -04:00
|
|
|
branch_name = Addressable::URI.unescape(branch_name)
|
2016-02-12 13:42:25 -05:00
|
|
|
|
2014-09-21 04:29:52 -04:00
|
|
|
result = CreateBranchService.new(project, current_user).
|
2014-11-13 10:20:43 -05:00
|
|
|
execute(branch_name, ref)
|
2014-10-30 11:28:59 -04:00
|
|
|
|
2016-02-22 03:20:04 -05:00
|
|
|
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
|
2016-02-17 01:11:48 -05:00
|
|
|
end
|
|
|
|
|
2014-07-27 10:40:00 -04:00
|
|
|
if result[:status] == :success
|
|
|
|
@branch = result[:branch]
|
2015-01-24 13:02:58 -05:00
|
|
|
redirect_to namespace_project_tree_path(@project.namespace, @project,
|
|
|
|
@branch.name)
|
2014-07-27 10:40:00 -04:00
|
|
|
else
|
|
|
|
@error = result[:message]
|
|
|
|
render action: 'new'
|
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-08-02 02:33:33 -04:00
|
|
|
@branch_name = Addressable::URI.unescape(params[:id])
|
|
|
|
status = DeleteBranchService.new(project, current_user).execute(@branch_name)
|
2013-07-16 15:19:07 -04:00
|
|
|
respond_to do |format|
|
2015-01-24 13:02:58 -05:00
|
|
|
format.html do
|
|
|
|
redirect_to namespace_project_branches_path(@project.namespace,
|
2016-04-07 00:10:24 -04:00
|
|
|
@project), status: 303
|
2015-01-24 13:02:58 -05:00
|
|
|
end
|
2016-06-02 12:35:46 -04:00
|
|
|
format.js { render nothing: true, status: status[:return_code] }
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 13:42:25 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ref
|
|
|
|
if params[:ref]
|
|
|
|
ref_escaped = sanitize(strip_tags(params[:ref]))
|
|
|
|
Addressable::URI.unescape(ref_escaped)
|
|
|
|
else
|
|
|
|
@project.default_branch
|
|
|
|
end
|
|
|
|
end
|
2013-07-16 15:19:07 -04:00
|
|
|
end
|