From b8dc5d2c5fbccc8f264a4e3cd6a046869e784c1d Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 20 Mar 2014 14:59:01 +0200 Subject: [PATCH] Implement 2 git hooks: tagf removal and commit message Signed-off-by: Dmitriy Zaporozhets --- app/views/projects/git_hooks/index.html.haml | 6 ++-- lib/gitlab/git_access.rb | 29 +++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/views/projects/git_hooks/index.html.haml b/app/views/projects/git_hooks/index.html.haml index 2313d09f4b..17fb543380 100644 --- a/app/views/projects/git_hooks/index.html.haml +++ b/app/views/projects/git_hooks/index.html.haml @@ -17,13 +17,13 @@ = f.check_box :deny_delete_tag %span.descr Dont allow users to remove git tags - .form-group + -#.form-group = f.label :force_push_regex, "Force push", class: 'control-label' .col-sm-10 = f.text_field :force_push_regex, class: "form-control" %p.hint Regular expression for branches to allow force push. Empty - allow force push to any branch - .form-group + -#.form-group = f.label :delete_branch_regex, "Branch removal", class: 'control-label' .col-sm-10 = f.text_field :delete_branch_regex, class: "form-control" @@ -32,7 +32,7 @@ .form-group = f.label :commit_message_regex, "Commit message", class: 'control-label' .col-sm-10 - = f.text_field :commit_message_regex, class: "form-control" + = f.text_field :commit_message_regex, class: "form-control", placeholder: 'Ex. Fix \d+\..*' %p.hint Commit message must match this regular expression to be pushed. Empty - allow remove of any commit message .form-actions diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 5fb5505743..ed33f60c67 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -48,12 +48,39 @@ module Gitlab else :push_code end - user.can?(action, project) + + user.can?(action, project) && + pass_git_hooks?(user, project, ref, oldrev, newrev) else false end end + def pass_git_hooks?(user, project, ref, oldrev, newrev) + return true unless project.git_hook + + git_hook = project.git_hook + + # Prevent tag removal + if git_hook.deny_delete_tag + if project.repository.tag_names.include?(ref) && newrev =~ /0000000/ + return false + end + end + + # Check commit messages unless its branch removal + if git_hook.commit_message_regex.present? && newrev !~ /00000000/ + commits = project.repository.commits_between(oldrev, newrev) + commits.each do |commit| + unless commit.safe_message =~ Regexp.new(git_hook.commit_message_regex) + return false + end + end + end + + true + end + private def user_allowed?(user)