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,7 +135,7 @@ class ApplicationController < ActionController::Base | ||
| 135 | end | 135 | end |
| 136 | 136 | ||
| 137 | def require_non_empty_project | 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 | end | 139 | end |
| 140 | 140 | ||
| 141 | def no_cache_headers | 141 | def no_cache_headers |
app/controllers/projects_controller.rb
| @@ -50,7 +50,7 @@ class ProjectsController < ApplicationController | @@ -50,7 +50,7 @@ class ProjectsController < ApplicationController | ||
| 50 | 50 | ||
| 51 | respond_to do |format| | 51 | respond_to do |format| |
| 52 | format.html do | 52 | format.html do |
| 53 | - if @project.repo_exists? && @project.has_commits? | 53 | + unless @project.empty_repo? |
| 54 | @last_push = current_user.recent_push(@project.id) | 54 | @last_push = current_user.recent_push(@project.id) |
| 55 | render :show | 55 | render :show |
| 56 | else | 56 | else |
app/roles/repository.rb
| @@ -8,6 +8,10 @@ module Repository | @@ -8,6 +8,10 @@ module Repository | ||
| 8 | false | 8 | false |
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | + def empty_repo? | ||
| 12 | + !repo_exists? || !has_commits? | ||
| 13 | + end | ||
| 14 | + | ||
| 11 | def commit(commit_id = nil) | 15 | def commit(commit_id = nil) |
| 12 | Commit.find_or_first(repo, commit_id, root_ref) | 16 | Commit.find_or_first(repo, commit_id, root_ref) |
| 13 | end | 17 | end |
| @@ -38,7 +42,7 @@ module Repository | @@ -38,7 +42,7 @@ module Repository | ||
| 38 | 42 | ||
| 39 | def has_post_receive_file? | 43 | def has_post_receive_file? |
| 40 | hook_file = File.join(path_to_repo, 'hooks', 'post-receive') | 44 | hook_file = File.join(path_to_repo, 'hooks', 'post-receive') |
| 41 | - File.exists?(hook_file) | 45 | + File.exists?(hook_file) |
| 42 | end | 46 | end |
| 43 | 47 | ||
| 44 | def tags | 48 | def tags |
| @@ -67,7 +71,7 @@ module Repository | @@ -67,7 +71,7 @@ module Repository | ||
| 67 | 71 | ||
| 68 | def repo_exists? | 72 | def repo_exists? |
| 69 | @repo_exists ||= (repo && !repo.branches.empty?) | 73 | @repo_exists ||= (repo && !repo.branches.empty?) |
| 70 | - rescue | 74 | + rescue |
| 71 | @repo_exists = false | 75 | @repo_exists = false |
| 72 | end | 76 | end |
| 73 | 77 | ||
| @@ -94,7 +98,7 @@ module Repository | @@ -94,7 +98,7 @@ module Repository | ||
| 94 | !!commit | 98 | !!commit |
| 95 | end | 99 | end |
| 96 | 100 | ||
| 97 | - def root_ref | 101 | + def root_ref |
| 98 | default_branch || "master" | 102 | default_branch || "master" |
| 99 | end | 103 | end |
| 100 | 104 | ||
| @@ -104,7 +108,7 @@ module Repository | @@ -104,7 +108,7 @@ module Repository | ||
| 104 | 108 | ||
| 105 | # Archive Project to .tar.gz | 109 | # Archive Project to .tar.gz |
| 106 | # | 110 | # |
| 107 | - # Already packed repo archives stored at | 111 | + # Already packed repo archives stored at |
| 108 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz | 112 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz |
| 109 | # | 113 | # |
| 110 | def archive_repo ref | 114 | def archive_repo ref |
| @@ -0,0 +1,22 @@ | @@ -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 |