From 4026b75784defa11d17d6b7885033f0e1b9d6f59 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Tue, 10 Mar 2015 20:51:38 +0500 Subject: [PATCH 1/2] [#446] aliased projects should have same default branch --- .../projects/projects_controller.rb | 4 +--- app/models/concerns/project/default_branch.rb | 5 ++++- app/models/project.rb | 5 +++++ spec/models/project_spec.rb | 18 ++++++++++++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/controllers/projects/projects_controller.rb b/app/controllers/projects/projects_controller.rb index 7f220d307..03eab4223 100644 --- a/app/controllers/projects/projects_controller.rb +++ b/app/controllers/projects/projects_controller.rb @@ -48,9 +48,7 @@ class Projects::ProjectsController < Projects::BaseController end def edit - @project_aliases = Project.where.not(id: @project.id). - where('alias_from_id IN (:ids) OR id IN (:ids)', { ids: [@project.alias_from_id, @project.id] }). - paginate(page: current_page) + @project_aliases = Project.project_aliases(@project).paginate(page: current_page) end def create diff --git a/app/models/concerns/project/default_branch.rb b/app/models/concerns/project/default_branch.rb index 672c9d186..dfd9213a2 100644 --- a/app/models/concerns/project/default_branch.rb +++ b/app/models/concerns/project/default_branch.rb @@ -63,7 +63,10 @@ module Project::DefaultBranch # Private: Set git head. def set_new_git_head - `cd #{path} && git symbolic-ref HEAD refs/heads/#{self.default_branch}` if self.default_branch_changed? && self.repo.branches.map(&:name).include?(self.default_branch) + if self.default_branch_changed? && self.repo.branches.map(&:name).include?(self.default_branch) + self.repo.git.send(:'symbolic-ref', {}, 'HEAD', "refs/heads/#{self.default_branch}") + Project.project_aliases(self).update_all default_branch: self.default_branch + end end # Private: Validation for checking that the default branch is exist. diff --git a/app/models/project.rb b/app/models/project.rb index f8cef7c4e..506a455ce 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -93,6 +93,11 @@ class Project < ActiveRecord::Base (projects.owner_id in (?) AND projects.owner_type = 'User')", group_owner_ids, user_owner_ids) } + scope :project_aliases, ->(project) { + where.not(id: project.id). + where('alias_from_id IN (:ids) OR id IN (:ids)', { ids: [project.alias_from_id, project.id].compact }) + } + before_validation :truncate_name, on: :create before_save -> { self.owner_uname = owner.uname if owner_uname.blank? || owner_id_changed? || owner_type_changed? } before_create :set_maintainer diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 51a734d23..1267e5153 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -4,7 +4,7 @@ describe Project do before { stub_symlink_methods } context '#fork' do - let(:root_project) { FactoryGirl.create(:project) } + let(:root_project) { FactoryGirl.create(:project_with_commit) } let(:child_project) { root_project.fork(FactoryGirl.create(:user)) } let(:child_child_project) { child_project.fork(FactoryGirl.create(:user)) } let(:alias_project) { root_project.fork(FactoryGirl.create(:user), is_alias: true) } @@ -41,6 +41,20 @@ describe Project do expect(alias_alias_project.alias_from).to eq(root_project) expect{ Grit::Repo.new(alias_alias_project.path) }.to_not raise_exception end + + it 'ensures that aliased projects have a same default branch' do + new_default_branch = 'conflicts' + alias_project.update_attributes default_branch: new_default_branch + expect(alias_project.parent.default_branch).to eq(new_default_branch) + end + + it 'ensures that forked project allowed to have another default branch' do + fill_project root_project + fill_project child_project + new_default_branch = 'conflicts' + child_project.update_attributes default_branch: new_default_branch + expect(child_project.parent.default_branch).to_not eq(new_default_branch) + end end context 'for destroy' do @@ -229,7 +243,7 @@ describe Project do url = 'http://abf-downloads.rosalinux.ru/abf_personal/repository/test-mass-import' visibility = 'open' - + Project.run_mass_import(url, "abf-worker-service-1-3.src.rpm\nredir-2.2.1-7.res6.src.rpm\n", visibility, owner, repository.id) Project.count.should == 2 From 1c6f8c512779283f951a7dd63f78d2cfd2df007e Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Tue, 10 Mar 2015 20:54:42 +0500 Subject: [PATCH 2/2] [#446] small refactoring --- app/models/concerns/project/default_branch.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/project/default_branch.rb b/app/models/concerns/project/default_branch.rb index dfd9213a2..bb0aaf902 100644 --- a/app/models/concerns/project/default_branch.rb +++ b/app/models/concerns/project/default_branch.rb @@ -63,9 +63,9 @@ module Project::DefaultBranch # Private: Set git head. def set_new_git_head - if self.default_branch_changed? && self.repo.branches.map(&:name).include?(self.default_branch) - self.repo.git.send(:'symbolic-ref', {}, 'HEAD', "refs/heads/#{self.default_branch}") - Project.project_aliases(self).update_all default_branch: self.default_branch + if default_branch_changed? && repo.branches.map(&:name).include?(default_branch) + repo.git.send(:'symbolic-ref', {}, 'HEAD', "refs/heads/#{default_branch}") + Project.project_aliases(self).update_all default_branch: default_branch end end