From 177025b5dde5ca34b05744f000a1eee64501863a Mon Sep 17 00:00:00 2001
From: Yorick Peterse <yorickpeterse@gmail.com>
Date: Tue, 8 Mar 2016 17:53:00 +0100
Subject: [PATCH] Call the right hooks when removing branches

This ensures that Repository#rm_branch calls
before_remove_branch/after_remove_branch instead of just 1 random cache
expiration method.
---
 app/models/repository.rb       | 10 ++++++++--
 spec/models/repository_spec.rb | 11 +++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/app/models/repository.rb b/app/models/repository.rb
index d3ca268a659..c3ae461a016 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -144,7 +144,7 @@ class Repository
   end
 
   def rm_branch(user, branch_name)
-    expire_branches_cache
+    before_remove_branch
 
     branch = find_branch(branch_name)
     oldrev = branch.try(:target)
@@ -155,7 +155,7 @@ class Repository
       rugged.branches.delete(branch_name)
     end
 
-    expire_branches_cache
+    after_remove_branch
     true
   end
 
@@ -359,10 +359,16 @@ class Repository
     expire_branch_count_cache
   end
 
+  # Runs code before removing an existing branch.
+  def before_remove_branch
+    expire_branches_cache
+  end
+
   # Runs code after an existing branch has been removed.
   def after_remove_branch
     expire_has_visible_content_cache
     expire_branch_count_cache
+    expire_branches_cache
   end
 
   def method_missing(m, *args, &block)
diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb
index 7af80704690..b2b4d38756f 100644
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -648,4 +648,15 @@ describe Repository, models: true do
       repository.expire_tag_count_cache
     end
   end
+
+  describe '#rm_branch' do
+    let(:user) { create(:user) }
+
+    it 'removes a branch' do
+      expect(repository).to receive(:before_remove_branch)
+      expect(repository).to receive(:after_remove_branch)
+
+      repository.rm_branch(user, 'feature')
+    end
+  end
 end
-- 
GitLab