Project.repository should never be nil so you can call repository.exists? or repository.empty?

Also specify separate project factory for project with filled repo
master
Dmitriy Zaporozhets 2013-04-01 16:56:25 +03:00
parent 49b024f5f5
commit 541d899410
13 changed files with 51 additions and 54 deletions

View File

@ -96,7 +96,7 @@ module ApplicationHelper
] ]
project_nav = [] project_nav = []
if @project && @project.repository && @project.repository.root_ref if @project && @project.repository.exists? && @project.repository.root_ref
project_nav = [ project_nav = [
{ label: "#{simple_sanitize(@project.name_with_namespace)} - Issues", url: project_issues_path(@project) }, { label: "#{simple_sanitize(@project.name_with_namespace)} - Issues", url: project_issues_path(@project) },
{ label: "#{simple_sanitize(@project.name_with_namespace)} - Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) }, { label: "#{simple_sanitize(@project.name_with_namespace)} - Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) },

View File

@ -141,13 +141,7 @@ class Project < ActiveRecord::Base
end end
def repository def repository
if path @repository ||= Repository.new(path_with_namespace, default_branch)
@repository ||= Repository.new(path_with_namespace, default_branch)
else
nil
end
rescue Gitlab::Git::NoRepository
nil
end end
def saved? def saved?
@ -332,14 +326,14 @@ class Project < ActiveRecord::Base
end end
def valid_repo? def valid_repo?
repo repository.exists?
rescue rescue
errors.add(:path, "Invalid repository path") errors.add(:path, "Invalid repository path")
false false
end end
def empty_repo? def empty_repo?
!repository || repository.empty? !repository.exists? || repository.empty?
end end
def ensure_satellite_exists def ensure_satellite_exists
@ -363,7 +357,7 @@ class Project < ActiveRecord::Base
end end
def repo_exists? def repo_exists?
@repo_exists ||= (repository && repository.branches.present?) @repo_exists ||= repository.exists?
rescue rescue
@repo_exists = false @repo_exists = false
end end

View File

@ -3,6 +3,16 @@ class Repository
def initialize(path_with_namespace, default_branch) def initialize(path_with_namespace, default_branch)
@raw_repository = Gitlab::Git::Repository.new(path_with_namespace, default_branch) @raw_repository = Gitlab::Git::Repository.new(path_with_namespace, default_branch)
rescue Gitlab::Git::Repository::NoRepository
nil
end
def exists?
raw_repository
end
def empty?
raw_repository.empty?
end end
def commit(id = nil) def commit(id = nil)

View File

@ -4,7 +4,7 @@
.form-horizontal= render "shared/clone_panel" .form-horizontal= render "shared/clone_panel"
.span4.pull-right .span4.pull-right
.pull-right .pull-right
- unless @project.empty_repo? - if @project.empty_repo?
- if can? current_user, :download_code, @project - if can? current_user, :download_code, @project
= link_to archive_project_repository_path(@project), class: "btn-small btn grouped" do = link_to archive_project_repository_path(@project), class: "btn-small btn grouped" do
%i.icon-download-alt %i.icon-download-alt

View File

@ -3,14 +3,14 @@ module SharedProject
# Create a project without caring about what it's called # Create a project without caring about what it's called
And "I own a project" do And "I own a project" do
@project = create(:project) @project = create(:project_with_code)
@project.team << [@user, :master] @project.team << [@user, :master]
end end
# Create a specific project called "Shop" # Create a specific project called "Shop"
And 'I own project "Shop"' do And 'I own project "Shop"' do
@project = Project.find_by_name "Shop" @project = Project.find_by_name "Shop"
@project ||= create(:project, name: "Shop") @project ||= create(:project_with_code, name: "Shop")
@project.team << [@user, :master] @project.team << [@user, :master]
end end

View File

@ -372,7 +372,7 @@ module Gitlab
ref = params[:ref_name] || user_project.try(:default_branch) || 'master' ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
commits = user_project.repository.commits(ref, nil, per_page, page * per_page) commits = user_project.repository.commits(ref, nil, per_page, page * per_page)
present CommitDecorator.decorate(commits), with: Entities::RepoCommit present commits, with: Entities::RepoCommit
end end
# Get a project snippets # Get a project snippets

View File

@ -85,8 +85,8 @@ module ExtractsPath
# - @id - A string representing the joined ref and path # - @id - A string representing the joined ref and path
# - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA) # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA)
# - @path - A string representing the filesystem path # - @path - A string representing the filesystem path
# - @commit - A CommitDecorator representing the commit from the given ref # - @commit - A Commit representing the commit from the given ref
# - @tree - A TreeDecorator representing the tree at the given ref/path # - @tree - A Tree representing the tree at the given ref/path
# #
# If the :id parameter appears to be requesting a specific response format, # If the :id parameter appears to be requesting a specific response format,
# that will be handled as well. # that will be handled as well.

View File

@ -187,7 +187,7 @@ module Gitlab
def reference_commit(identifier) def reference_commit(identifier)
if @project.valid_repo? && commit = @project.repository.commit(identifier) if @project.valid_repo? && commit = @project.repository.commit(identifier)
link_to(identifier, project_commit_url(@project, commit), html_options.merge(title: CommitDecorator.new(commit).link_title, class: "gfm gfm-commit #{html_options[:class]}")) link_to(identifier, project_commit_url(@project, commit), html_options.merge(title: commit.link_title, class: "gfm gfm-commit #{html_options[:class]}"))
end end
end end
end end

View File

@ -25,7 +25,7 @@ FactoryGirl.define do
factory :project do factory :project do
sequence(:name) { |n| "project#{n}" } sequence(:name) { |n| "project#{n}" }
path { 'gitlabhq' } path { name.downcase.gsub(/\s/, '_') }
creator creator
end end
@ -34,6 +34,10 @@ FactoryGirl.define do
issues_tracker_id { "project_name_in_redmine" } issues_tracker_id { "project_name_in_redmine" }
end end
factory :project_with_code, parent: :project do
path { 'gitlabhq' }
end
factory :group do factory :group do
sequence(:name) { |n| "group#{n}" } sequence(:name) { |n| "group#{n}" }
path { name.downcase.gsub(/\s/, '_') } path { name.downcase.gsub(/\s/, '_') }

View File

@ -7,7 +7,7 @@ describe GitlabMarkdownHelper do
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') } let(:user) { create(:user, username: 'gfm') }
let(:commit) { CommitDecorator.decorate(Commit.new(project.repository.commit)) } let(:commit) { project.repository.commit) }
let(:issue) { create(:issue, project: project) } let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, project: project) } let(:merge_request) { create(:merge_request, project: project) }
let(:snippet) { create(:snippet, project: project) } let(:snippet) { create(:snippet, project: project) }

View File

@ -3,35 +3,32 @@ require 'spec_helper'
describe Commit do describe Commit do
let(:commit) { create(:project).repository.commit } let(:commit) { create(:project).repository.commit }
describe CommitDecorator do
let(:decorator) { CommitDecorator.new(commit) }
describe '#title' do describe '#title' do
it "returns no_commit_message when safe_message is blank" do it "returns no_commit_message when safe_message is blank" do
decorator.stub(:safe_message).and_return('') commit.stub(:safe_message).and_return('')
decorator.title.should == "--no commit message" commit.title.should == "--no commit message"
end end
it "truncates a message without a newline at 70 characters" do it "truncates a message without a newline at 70 characters" do
message = commit.safe_message * 10 message = commit.safe_message * 10
decorator.stub(:safe_message).and_return(message) commit.stub(:safe_message).and_return(message)
decorator.title.should == "#{message[0..69]}&hellip;" commit.title.should == "#{message[0..69]}&hellip;"
end end
it "truncates a message with a newline before 80 characters at the newline" do it "truncates a message with a newline before 80 characters at the newline" do
message = commit.safe_message.split(" ").first message = commit.safe_message.split(" ").first
decorator.stub(:safe_message).and_return(message + "\n" + message) commit.stub(:safe_message).and_return(message + "\n" + message)
decorator.title.should == message commit.title.should == message
end end
it "truncates a message with a newline after 80 characters at 70 characters" do it "truncates a message with a newline after 80 characters at 70 characters" do
message = (commit.safe_message * 10) + "\n" message = (commit.safe_message * 10) + "\n"
decorator.stub(:safe_message).and_return(message) commit.stub(:safe_message).and_return(message)
decorator.title.should == "#{message[0..69]}&hellip;" commit.title.should == "#{message[0..69]}&hellip;"
end
end end
end end

View File

@ -119,7 +119,7 @@ describe Project do
end end
describe :update_merge_requests do describe :update_merge_requests do
let(:project) { create(:project) } let(:project) { create(:project_with_code) }
before do before do
@merge_request = create(:merge_request, project: project) @merge_request = create(:merge_request, project: project)
@ -187,11 +187,7 @@ describe Project do
let(:project) { create(:project) } let(:project) { create(:project) }
it "should return valid repo" do it "should return valid repo" do
project.repository.should be_kind_of(Gitlab::Git::Repository) project.repository.should be_kind_of(Repository)
end
it "should return nil" do
Project.new(path: "empty").repository.should be_nil
end end
end end
@ -249,7 +245,7 @@ describe Project do
end end
describe :open_branches do describe :open_branches do
let(:project) { create(:project) } let(:project) { create(:project_with_code) }
before do before do
project.protected_branches.create(name: 'master') project.protected_branches.create(name: 'master')

View File

@ -47,11 +47,7 @@ Spork.prefork do
config.use_transactional_fixtures = false config.use_transactional_fixtures = false
config.before do config.before do
# Use tmp dir for FS manipulations TestEnv.init
temp_repos_path = Rails.root.join('tmp', 'test-git-base-path')
Gitlab.config.gitlab_shell.stub(repos_path: temp_repos_path)
FileUtils.rm_rf temp_repos_path
FileUtils.mkdir_p temp_repos_path
end end
end end
end end