From a4633537737d1ea85c74b2089fa1c82e407c0cfa Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 4 Sep 2012 11:37:38 -0400 Subject: [PATCH] Add "empty_repo?" method to Repository role Replaces two calls that this method simplifies --- app/controllers/application_controller.rb | 2 +- app/controllers/projects_controller.rb | 2 +- app/roles/repository.rb | 12 ++++++++---- spec/roles/repository_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 spec/roles/repository_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9aab250dbd4..d53b23bb246 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -135,7 +135,7 @@ class ApplicationController < ActionController::Base end def require_non_empty_project - redirect_to @project unless @project.repo_exists? && @project.has_commits? + redirect_to @project if @project.empty_repo? end def no_cache_headers diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index bd7f811e59f..170b8892936 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -50,7 +50,7 @@ class ProjectsController < ApplicationController respond_to do |format| format.html do - if @project.repo_exists? && @project.has_commits? + unless @project.empty_repo? @last_push = current_user.recent_push(@project.id) render :show else diff --git a/app/roles/repository.rb b/app/roles/repository.rb index 5fa950db6d6..5f6c35414e1 100644 --- a/app/roles/repository.rb +++ b/app/roles/repository.rb @@ -8,6 +8,10 @@ module Repository false end + def empty_repo? + !repo_exists? || !has_commits? + end + def commit(commit_id = nil) Commit.find_or_first(repo, commit_id, root_ref) end @@ -38,7 +42,7 @@ module Repository def has_post_receive_file? hook_file = File.join(path_to_repo, 'hooks', 'post-receive') - File.exists?(hook_file) + File.exists?(hook_file) end def tags @@ -67,7 +71,7 @@ module Repository def repo_exists? @repo_exists ||= (repo && !repo.branches.empty?) - rescue + rescue @repo_exists = false end @@ -94,7 +98,7 @@ module Repository !!commit end - def root_ref + def root_ref default_branch || "master" end @@ -104,7 +108,7 @@ module Repository # Archive Project to .tar.gz # - # Already packed repo archives stored at + # Already packed repo archives stored at # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz # def archive_repo ref diff --git a/spec/roles/repository_spec.rb b/spec/roles/repository_spec.rb new file mode 100644 index 00000000000..62aecc1341f --- /dev/null +++ b/spec/roles/repository_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Project, "Repository" do + let(:project) { build(:project) } + + describe "#empty_repo?" do + it "should return true if the repo doesn't exist" do + project.stub(repo_exists?: false, has_commits?: true) + project.should be_empty_repo + end + + it "should return true if the repo has commits" do + project.stub(repo_exists?: true, has_commits?: false) + project.should be_empty_repo + end + + it "should return false if the repo exists and has commits" do + project.stub(repo_exists?: true, has_commits?: true) + project.should_not be_empty_repo + end + end +end