2013-07-16 19:19:07 +00:00
|
|
|
class Projects::BranchesController < Projects::ApplicationController
|
2014-11-13 15:20:43 +00:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2016-08-10 19:15:22 +00:00
|
|
|
include SortingHelper
|
2013-07-16 19:19:07 +00:00
|
|
|
# Authorize
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_download_code!
|
2016-09-21 14:15:12 +00:00
|
|
|
before_action :authorize_push_code!, only: [:new, :create, :destroy, :destroy_all_merged]
|
2013-07-16 19:19:07 +00:00
|
|
|
|
|
|
|
def index
|
2016-08-10 19:15:22 +00:00
|
|
|
@sort = params[:sort].presence || sort_value_name
|
2016-07-07 16:19:21 +00:00
|
|
|
@branches = BranchesFinder.new(@repository, params).execute
|
2016-03-19 21:37:54 +00:00
|
|
|
@branches = Kaminari.paginate_array(@branches).page(params[:page])
|
2016-02-12 18:42:25 +00:00
|
|
|
|
2015-11-11 22:29:29 +00:00
|
|
|
@max_commits = @branches.reduce(0) do |memo, branch|
|
2015-07-21 01:34:19 +00:00
|
|
|
diverging_commit_counts = repository.diverging_commit_counts(branch)
|
|
|
|
[memo, diverging_commit_counts[:behind], diverging_commit_counts[:ahead]].max
|
|
|
|
end
|
2016-08-01 19:56:56 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2016-08-02 16:53:10 +00:00
|
|
|
render json: @repository.branch_names
|
2016-08-01 19:56:56 +00:00
|
|
|
end
|
|
|
|
end
|
2013-07-16 19:19:07 +00:00
|
|
|
end
|
|
|
|
|
2013-08-05 14:59:58 +00:00
|
|
|
def recent
|
|
|
|
@branches = @repository.recent_branches
|
|
|
|
end
|
|
|
|
|
2013-07-16 19:19:07 +00:00
|
|
|
def create
|
2014-11-13 15:20:43 +00:00
|
|
|
branch_name = sanitize(strip_tags(params[:branch_name]))
|
2015-08-02 06:33:33 +00:00
|
|
|
branch_name = Addressable::URI.unescape(branch_name)
|
2016-02-12 18:42:25 +00:00
|
|
|
|
2014-09-21 08:29:52 +00:00
|
|
|
result = CreateBranchService.new(project, current_user).
|
2014-11-13 15:20:43 +00:00
|
|
|
execute(branch_name, ref)
|
2014-10-30 15:28:59 +00:00
|
|
|
|
2016-02-22 08:20:04 +00:00
|
|
|
if params[:issue_iid]
|
2016-11-18 13:51:52 +00:00
|
|
|
issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid])
|
2016-02-22 08:20:04 +00:00
|
|
|
SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue
|
2016-02-17 06:11:48 +00:00
|
|
|
end
|
|
|
|
|
2014-07-27 14:40:00 +00:00
|
|
|
if result[:status] == :success
|
|
|
|
@branch = result[:branch]
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to namespace_project_tree_path(@project.namespace, @project,
|
|
|
|
@branch.name)
|
2014-07-27 14:40:00 +00:00
|
|
|
else
|
|
|
|
@error = result[:message]
|
|
|
|
render action: 'new'
|
|
|
|
end
|
2013-07-16 19:19:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-08-02 06:33:33 +00:00
|
|
|
@branch_name = Addressable::URI.unescape(params[:id])
|
|
|
|
status = DeleteBranchService.new(project, current_user).execute(@branch_name)
|
2013-07-16 19:19:07 +00:00
|
|
|
respond_to do |format|
|
2015-01-24 18:02:58 +00:00
|
|
|
format.html do
|
|
|
|
redirect_to namespace_project_branches_path(@project.namespace,
|
2016-04-07 04:10:24 +00:00
|
|
|
@project), status: 303
|
2015-01-24 18:02:58 +00:00
|
|
|
end
|
2016-06-02 16:35:46 +00:00
|
|
|
format.js { render nothing: true, status: status[:return_code] }
|
2013-07-16 19:19:07 +00:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 18:42:25 +00:00
|
|
|
|
2016-09-21 14:15:12 +00:00
|
|
|
def destroy_all_merged
|
|
|
|
DeleteMergedBranchesService.new(@project, current_user).async_execute
|
|
|
|
|
|
|
|
redirect_to namespace_project_branches_path(@project.namespace, @project),
|
|
|
|
notice: 'Merged branches are being deleted. This can take some time depending on the number of branches. Please refresh the page to see changes.'
|
|
|
|
end
|
|
|
|
|
2016-02-12 18:42:25 +00: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 19:19:07 +00:00
|
|
|
end
|