diff --git a/app/models/repository.rb b/app/models/repository.rb
index f76b770e867d3ab8754fcdb0b84896d8d570021b..c5b6ee80dc6a3bda97d2224c14b9f8ef2b63e29c 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -484,7 +484,7 @@ class Repository
     root_ref_commit = commit(root_ref)
 
     if branch_commit
-      rugged.merge_base(root_ref_commit.id, branch_commit.id) == branch_commit.id
+      is_ancestor?(branch_commit.id, root_ref_commit.id)
     else
       nil
     end
@@ -494,6 +494,11 @@ class Repository
     rugged.merge_base(first_commit_id, second_commit_id)
   end
 
+  def is_ancestor?(ancestor_id, descendant_id)
+    merge_base(ancestor_id, descendant_id) == ancestor_id
+  end
+
+
   def search_files(query, ref)
     offset = 2
     args = %W(#{Gitlab.config.git.bin_path} grep -i -n --before-context #{offset} --after-context #{offset} -e #{query} #{ref || root_ref})