From 8860238da4a8ab7bec4b57dbc2d2e1be3c7e0f20 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 31 Jan 2013 22:26:49 +0600 Subject: [PATCH] [refs #878] fix some bugs when no master branch --- app/controllers/projects/git/base_controller.rb | 3 +-- app/controllers/projects/pull_requests_controller.rb | 4 ++-- app/models/project.rb | 8 ++++++++ app/models/pull_request.rb | 4 ++-- app/views/projects/base/_submenu.html.haml | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/controllers/projects/git/base_controller.rb b/app/controllers/projects/git/base_controller.rb index fbf60d44c..f8976c2d8 100644 --- a/app/controllers/projects/git/base_controller.rb +++ b/app/controllers/projects/git/base_controller.rb @@ -10,8 +10,7 @@ class Projects::Git::BaseController < Projects::BaseController protected def set_treeish_and_path - @treeish = params[:treeish].presence || @project.default_branch - @path = params[:path] + @treeish, @path = @project.default_head(params[:treeish]), params[:path] end def set_branch_and_tree diff --git a/app/controllers/projects/pull_requests_controller.rb b/app/controllers/projects/pull_requests_controller.rb index 63ce7cdc0..b0157df05 100644 --- a/app/controllers/projects/pull_requests_controller.rb +++ b/app/controllers/projects/pull_requests_controller.rb @@ -152,8 +152,8 @@ class Projects::PullRequestsController < Projects::BaseController @pull.issue.body = pull_params[:issue_attributes][:body].presence end @pull.from_project = @project - @pull.to_ref = (pull_params[:to_ref].presence if pull_params) || @pull.to_project.default_branch - @pull.from_ref = params[:treeish].presence || (pull_params[:from_ref].presence if pull_params) || @pull.from_project.default_branch + @pull.to_ref = (pull_params[:to_ref].presence if pull_params) || @pull.to_project.default_head(params[:treeish]) + @pull.from_ref = params[:treeish].presence || (pull_params[:from_ref].presence if pull_params) || @pull.from_project.default_head(params[:treeish]) @pull.from_project_owner_uname = @pull.from_project.owner.uname @pull.from_project_name = @pull.from_project.name end diff --git a/app/models/project.rb b/app/models/project.rb index b9a795d45..2f58f40e7 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -178,6 +178,14 @@ class Project < ActiveRecord::Base AbfWorker::BuildListsPublishTaskManager.destroy_project_from_repository self, repository end + def default_head treeish = nil # maybe need change 'head'? + if repo.branches_and_tags.map(&:name).include?(treeish || default_branch) + treeish + else + repo.branches_and_tags[0].try(:name) || 'master' + end + end + protected def truncate_name diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index a67af1587..ae549e93e 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -125,7 +125,7 @@ class PullRequest < ActiveRecord::Base def self.check_ref(record, attr, value) project = attr == :from_ref ? record.from_project : record.to_project return if project.blank? - if record.to_project.repo.commits.count > 0 + if record.to_project.repo.branches.count > 0 record.errors.add attr, I18n.t('projects.pull_requests.wrong_ref') unless project.repo.branches_and_tags.map(&:name).include?(value) else record.errors.add attr, I18n.t('projects.pull_requests.empty_repo') @@ -179,8 +179,8 @@ class PullRequest < ActiveRecord::Base system 'git', 'tag', '-d', from_ref, to_ref system 'git fetch --tags' tags, head = repo.tags.map(&:name), to_project == from_project ? 'origin' : 'head' + system 'git', 'checkout', to_ref unless tags.include? to_ref - system 'git', 'checkout', to_ref system 'git', 'reset', '--hard', "origin/#{to_ref}" end unless tags.include? from_ref diff --git a/app/views/projects/base/_submenu.html.haml b/app/views/projects/base/_submenu.html.haml index 67b6fb37d..e74348da6 100644 --- a/app/views/projects/base/_submenu.html.haml +++ b/app/views/projects/base/_submenu.html.haml @@ -1,5 +1,5 @@ - content_for :submenu do - - act = action_name.to_sym; contr = controller_name.to_sym; treeish = params[:treeish].presence || @project.default_branch + - act = action_name.to_sym; contr = controller_name.to_sym; treeish = @project.default_head(params[:treeish]) .left .table-sort-left=image_tag visibility_icon(@project.visibility) .table-sort-right=@project.name