diff --git a/app/controllers/projects/git/trees_controller.rb b/app/controllers/projects/git/trees_controller.rb index 5a4d0f7fb..2a1c5778d 100644 --- a/app/controllers/projects/git/trees_controller.rb +++ b/app/controllers/projects/git/trees_controller.rb @@ -40,7 +40,7 @@ class Projects::Git::TreesController < Projects::Git::BaseController end def destroy - if @branch && @project.default_branch != @branch.name && @project.delete_branch(@branch, current_user).present? + if @branch && @project.delete_branch(@branch, current_user) render :nothing => true else render :nothing => true, :status => 422 diff --git a/lib/modules/models/git.rb b/lib/modules/models/git.rb index fea8dac00..2de1d4b58 100644 --- a/lib/modules/models/git.rb +++ b/lib/modules/models/git.rb @@ -39,8 +39,12 @@ module Modules end def delete_branch(branch, user) + return false if default_branch == branch.name message = repo.git.native(:branch, {}, '-D', branch.name) - Resque.enqueue(GitHook,owner.uname, name, GitHook::ZERO, branch.commit.id, "refs/heads/#{branch.name}", 'commit', "user-#{user.id}", message) + if message.present? + Resque.enqueue(GitHook,owner.uname, name, GitHook::ZERO, branch.commit.id, "refs/heads/#{branch.name}", 'commit', "user-#{user.id}", message) + end + return message.present? end def update_file(path, data, options = {}) diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 1a0b0e217..e61151ab9 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -86,4 +86,55 @@ describe Project do lambda {FactoryGirl.create(:project, :name => "...\nbeatiful_name\n for project")}.should raise_error(ActiveRecord::RecordInvalid) end end + + context 'manage branches' do + let!(:project) { FactoryGirl.create(:project_with_commit) } + let(:branch) { project.repo.branches.detect{|b| b.name == 'conflicts'} } + let(:master) { project.repo.branches.detect{|b| b.name == 'master'} } + let(:user) { FactoryGirl.create(:user) } + before { stub_redis } + + context '#delete_branch' do + it 'ensures that returns true on success' do + project.delete_branch(branch, user).should be_true + end + + it 'ensures that branch has been deleted' do + lambda { project.delete_branch(branch, user) }.should change{ project.repo.branches.count }.by(-1) + end + + it 'ensures that returns false on delete master' do + project.delete_branch(master, user).should be_false + end + + it 'ensures that master has not been deleted' do + lambda { project.delete_branch(master, user) }.should change{ project.repo.branches.count }.by(0) + end + + it 'ensures that returns false on delete wrong branch' do + project.delete_branch(branch, user) + project.delete_branch(branch, user).should be_false + end + end + + context '#restore_branch' do + before do + project.delete_branch(branch, user) + end + + xit 'ensures that returns true on success' do + project.restore_branch(branch.name, branch.commit.id).should be_true + end + + it 'ensures that branch has been restored' do + lambda { project.restore_branch(branch.name, branch.commit.id) }.should change{ project.repo.branches.count }.by(1) + end + + xit 'ensures that returns false on restore wrong branch' do + project.restore_branch(branch.name, GitHook::ZERO).should be_false + end + end + + end + end