From 69e34af76c48132682806f6a32714212fc73d280 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= <rdavila84@gmail.com>
Date: Sat, 21 May 2016 03:18:59 -0500
Subject: [PATCH] Fix for triggering git hooks configured through Project
 settings.

* We should skip hooks only for new branch.
* The spec was using a wrong rev as the blank rev.
---
 lib/gitlab/git_access.rb           | 6 ++++--
 spec/lib/gitlab/git_access_spec.rb | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb
index 072491db47d6e..0f2bec983cd4e 100644
--- a/lib/gitlab/git_access.rb
+++ b/lib/gitlab/git_access.rb
@@ -211,11 +211,13 @@ def git_hook_check(user, project, ref, oldrev, newrev)
           return build_status_object(true)
         end
 
+        blank_oldrev = Gitlab::Git.blank_ref?(oldrev)
+
         # if oldrev is blank, the branch was just created
-        oldrev = project.default_branch if Gitlab::Git.blank_ref?(oldrev)
+        oldrev = project.default_branch if blank_oldrev
 
         commits(newrev, oldrev, project).each do |commit|
-          next if commit_from_annex_sync?(commit.safe_message) || old_commit?(commit)
+          next if commit_from_annex_sync?(commit.safe_message) || (blank_oldrev && old_commit?(commit))
 
           if status_object = check_commit(commit, git_hook)
             return status_object
diff --git a/spec/lib/gitlab/git_access_spec.rb b/spec/lib/gitlab/git_access_spec.rb
index 7d98ce5ed3e01..0c9599d28a901 100644
--- a/spec/lib/gitlab/git_access_spec.rb
+++ b/spec/lib/gitlab/git_access_spec.rb
@@ -401,7 +401,7 @@ def self.updated_permissions_matrix
         project.git_hook.update(commit_message_regex: "Change some files")
 
         # push to new branch, so use a blank old rev and new ref
-        expect(access.git_hook_check(user, project, 'refs/heads/master', '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9', '570e7b2abdd848b95f2f578043fc23bd6f6fd24d')).to be_allowed
+        expect(access.git_hook_check(user, project, 'refs/heads/master', Gitlab::Git::BLANK_SHA, '570e7b2abdd848b95f2f578043fc23bd6f6fd24d')).to be_allowed
       end
     end
 
-- 
GitLab