Commit a4633537737d1ea85c74b2089fa1c82e407c0cfa
1 parent
7e76610d
Exists in
master
and in
4 other branches
Add "empty_repo?" method to Repository role
Replaces two calls that this method simplifies
Showing
4 changed files
with
32 additions
and
6 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -135,7 +135,7 @@ class ApplicationController < ActionController::Base |
| 135 | 135 | end |
| 136 | 136 | |
| 137 | 137 | def require_non_empty_project |
| 138 | - redirect_to @project unless @project.repo_exists? && @project.has_commits? | |
| 138 | + redirect_to @project if @project.empty_repo? | |
| 139 | 139 | end |
| 140 | 140 | |
| 141 | 141 | def no_cache_headers | ... | ... |
app/controllers/projects_controller.rb
| ... | ... | @@ -50,7 +50,7 @@ class ProjectsController < ApplicationController |
| 50 | 50 | |
| 51 | 51 | respond_to do |format| |
| 52 | 52 | format.html do |
| 53 | - if @project.repo_exists? && @project.has_commits? | |
| 53 | + unless @project.empty_repo? | |
| 54 | 54 | @last_push = current_user.recent_push(@project.id) |
| 55 | 55 | render :show |
| 56 | 56 | else | ... | ... |
app/roles/repository.rb
| ... | ... | @@ -8,6 +8,10 @@ module Repository |
| 8 | 8 | false |
| 9 | 9 | end |
| 10 | 10 | |
| 11 | + def empty_repo? | |
| 12 | + !repo_exists? || !has_commits? | |
| 13 | + end | |
| 14 | + | |
| 11 | 15 | def commit(commit_id = nil) |
| 12 | 16 | Commit.find_or_first(repo, commit_id, root_ref) |
| 13 | 17 | end |
| ... | ... | @@ -38,7 +42,7 @@ module Repository |
| 38 | 42 | |
| 39 | 43 | def has_post_receive_file? |
| 40 | 44 | hook_file = File.join(path_to_repo, 'hooks', 'post-receive') |
| 41 | - File.exists?(hook_file) | |
| 45 | + File.exists?(hook_file) | |
| 42 | 46 | end |
| 43 | 47 | |
| 44 | 48 | def tags |
| ... | ... | @@ -67,7 +71,7 @@ module Repository |
| 67 | 71 | |
| 68 | 72 | def repo_exists? |
| 69 | 73 | @repo_exists ||= (repo && !repo.branches.empty?) |
| 70 | - rescue | |
| 74 | + rescue | |
| 71 | 75 | @repo_exists = false |
| 72 | 76 | end |
| 73 | 77 | |
| ... | ... | @@ -94,7 +98,7 @@ module Repository |
| 94 | 98 | !!commit |
| 95 | 99 | end |
| 96 | 100 | |
| 97 | - def root_ref | |
| 101 | + def root_ref | |
| 98 | 102 | default_branch || "master" |
| 99 | 103 | end |
| 100 | 104 | |
| ... | ... | @@ -104,7 +108,7 @@ module Repository |
| 104 | 108 | |
| 105 | 109 | # Archive Project to .tar.gz |
| 106 | 110 | # |
| 107 | - # Already packed repo archives stored at | |
| 111 | + # Already packed repo archives stored at | |
| 108 | 112 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz |
| 109 | 113 | # |
| 110 | 114 | def archive_repo ref | ... | ... |
| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Project, "Repository" do | |
| 4 | + let(:project) { build(:project) } | |
| 5 | + | |
| 6 | + describe "#empty_repo?" do | |
| 7 | + it "should return true if the repo doesn't exist" do | |
| 8 | + project.stub(repo_exists?: false, has_commits?: true) | |
| 9 | + project.should be_empty_repo | |
| 10 | + end | |
| 11 | + | |
| 12 | + it "should return true if the repo has commits" do | |
| 13 | + project.stub(repo_exists?: true, has_commits?: false) | |
| 14 | + project.should be_empty_repo | |
| 15 | + end | |
| 16 | + | |
| 17 | + it "should return false if the repo exists and has commits" do | |
| 18 | + project.stub(repo_exists?: true, has_commits?: true) | |
| 19 | + project.should_not be_empty_repo | |
| 20 | + end | |
| 21 | + end | |
| 22 | +end | ... | ... |