diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-12-08 16:42:10 +0100 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-12-08 16:42:10 +0100 |
commit | df6750d3d6b562c8a6a0a57c12dfd694da38a0e8 (patch) | |
tree | 54f15d83ddef9b524144cf6c49385b5e98c96509 /app | |
parent | 792f2bbe0b306bda266dfc817edf33b4c9e36a0f (diff) | |
download | gitlab-ce-df6750d3d6b562c8a6a0a57c12dfd694da38a0e8.tar.gz |
Default target branch to patch-n when editing file in protected branch
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/blob_controller.rb | 12 | ||||
-rw-r--r-- | app/helpers/branches_helper.rb | 2 | ||||
-rw-r--r-- | app/helpers/tree_helper.rb | 13 | ||||
-rw-r--r-- | app/models/repository.rb | 11 | ||||
-rw-r--r-- | app/views/shared/_new_commit_form.html.haml | 2 |
5 files changed, 33 insertions, 7 deletions
diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb index 31a33bfd237..62163682936 100644 --- a/app/controllers/projects/blob_controller.rb +++ b/app/controllers/projects/blob_controller.rb @@ -162,12 +162,20 @@ class Projects::BlobController < Projects::ApplicationController end def sanitized_new_branch_name - @new_branch ||= sanitize(strip_tags(params[:new_branch])) + sanitize(strip_tags(params[:new_branch])) end def editor_variables @current_branch = @ref - @new_branch = params[:new_branch].present? ? sanitized_new_branch_name : @ref + + @new_branch = + if params[:new_branch].present? + sanitized_new_branch_name + elsif ::Gitlab::GitAccess.new(current_user, @project).can_push_to_branch?(@ref) + @ref + else + @repository.next_patch_branch + end @file_path = if action_name.to_s == 'create' diff --git a/app/helpers/branches_helper.rb b/app/helpers/branches_helper.rb index d6eaa7d57bc..e39548e17e1 100644 --- a/app/helpers/branches_helper.rb +++ b/app/helpers/branches_helper.rb @@ -11,7 +11,7 @@ module BranchesHelper def can_push_branch?(project, branch_name) return false unless project.repository.branch_names.include?(branch_name) - + ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(branch_name) end end diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb index 6afa1aacc5b..ac75c9a14b7 100644 --- a/app/helpers/tree_helper.rb +++ b/app/helpers/tree_helper.rb @@ -48,10 +48,17 @@ module TreeHelper def allowed_tree_edit?(project = nil, ref = nil) project ||= @project - ref ||= @ref - return false unless project.repository.branch_names.include?(ref) + can?(current_user, :push_code, project) + end - ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref) + def tree_edit_branch(project = @project, ref = @ref) + if allowed_tree_edit? + if can_push_branch?(project, ref) + ref + else + project.repository.next_patch_branch + end + end end def can_delete_or_replace?(blob) diff --git a/app/models/repository.rb b/app/models/repository.rb index 1d43307e1e7..1edec52c09e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -329,6 +329,17 @@ class Repository commit(sha) end + def next_patch_branch + patch_branch_ids = self.branch_names.map do |n| + result = n.match(/\Apatch-([0-9]+)\z/) + result[1].to_i if result + end.compact + + highest_patch_branch_id = patch_branch_ids.max || 0 + + "patch-#{highest_patch_branch_id + 1}" + end + # Remove archives older than 2 hours def branches_sorted_by(value) case value diff --git a/app/views/shared/_new_commit_form.html.haml b/app/views/shared/_new_commit_form.html.haml index 31b02ed93d0..074080e44c9 100644 --- a/app/views/shared/_new_commit_form.html.haml +++ b/app/views/shared/_new_commit_form.html.haml @@ -4,7 +4,7 @@ .form-group.branch = label_tag 'new_branch', 'Target branch', class: 'control-label' .col-sm-10 - = text_field_tag 'new_branch', @new_branch || @ref, required: true, class: "form-control js-new-branch" + = text_field_tag 'new_branch', @new_branch || tree_edit_branch, required: true, class: "form-control js-new-branch" .form-group.js-create-merge-request-form-group .col-sm-offset-2.col-sm-10 |