Fix tests and remove app/models/repository.rb

master
Dmitriy Zaporozhets 2013-03-31 18:50:17 +03:00
parent 154e54b46e
commit 9dc644635f
6 changed files with 5 additions and 325 deletions

View File

@ -1,170 +0,0 @@
class Repository
include Gitlab::Popen
# Repository directory name with namespace direcotry
# Examples:
# gitlab/gitolite
# diaspora
#
attr_accessor :path_with_namespace
# Grit repo object
attr_accessor :repo
# Default branch in the repository
attr_accessor :root_ref
def initialize(path_with_namespace, root_ref = 'master')
@root_ref = root_ref || "master"
@path_with_namespace = path_with_namespace
# Init grit repo object
repo
end
def raw
repo
end
def path_to_repo
@path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
end
def repo
@repo ||= Grit::Repo.new(path_to_repo)
end
def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id, root_ref)
end
def fresh_commits(n = 10)
Commit.fresh_commits(repo, n)
end
def commits_with_refs(n = 20)
Commit.commits_with_refs(repo, n)
end
def commits_since(date)
Commit.commits_since(repo, date)
end
def commits(ref, path = nil, limit = nil, offset = nil)
Commit.commits(repo, ref, path, limit, offset)
end
def last_commit_for(ref, path = nil)
commits(ref, path, 1).first
end
def commits_between(from, to)
Commit.commits_between(repo, from, to)
end
# Returns an Array of branch names
# sorted by name ASC
def branch_names
branches.map(&:name)
end
# Returns an Array of Branches
def branches
repo.branches.sort_by(&:name)
end
# Returns an Array of tag names
def tag_names
repo.tags.collect(&:name).sort.reverse
end
# Returns an Array of Tags
def tags
repo.tags.sort_by(&:name).reverse
end
# Returns an Array of branch and tag names
def ref_names
[branch_names + tag_names].flatten
end
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
def has_commits?
!!commit
rescue Grit::NoSuchPathError
false
end
def empty?
!has_commits?
end
# Discovers the default branch based on the repository's available branches
#
# - If no branches are present, returns nil
# - If one branch is present, returns its name
# - If two or more branches are present, returns the one that has a name
# matching root_ref (default_branch or 'master' if default_branch is nil)
def discover_default_branch
if branch_names.length == 0
nil
elsif branch_names.length == 1
branch_names.first
else
branch_names.select { |v| v == root_ref }.first
end
end
# Archive Project to .tar.gz
#
# Already packed repo archives stored at
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
#
def archive_repo(ref)
ref = ref || self.root_ref
commit = self.commit(ref)
return nil unless commit
# Build file path
file_name = self.path_with_namespace.gsub("/","_") + "-" + commit.id.to_s + ".tar.gz"
storage_path = Rails.root.join("tmp", "repositories")
file_path = File.join(storage_path, self.path_with_namespace, file_name)
# Put files into a directory before archiving
prefix = File.basename(self.path_with_namespace) + "/"
# Create file if not exists
unless File.exists?(file_path)
FileUtils.mkdir_p File.dirname(file_path)
file = self.repo.archive_to_file(ref, prefix, file_path)
end
file_path
end
# Return repo size in megabytes
# Cached in redis
def size
Rails.cache.fetch(cache_key(:size)) do
size = popen('du -s', path_to_repo).first.strip.to_i
(size.to_f / 1024).round(2)
end
end
def expire_cache
Rails.cache.delete(cache_key(:size))
end
def cache_key(type)
"#{type}:#{path_with_namespace}"
end
end

View File

@ -100,8 +100,8 @@ module ExtractsPath
# It is used "@project.repository.commits(@ref, @path, 1, 0)",
# because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name.
commits = @project.repository.commits(@ref, @path, 1, 0)
@commit = CommitDecorator.decorate(commits.first)
@commit = @project.repository.commits(@ref, @path, 1, 0).first
@commit = CommitDecorator.decorate(@commit)
@tree = Tree.new(@commit.tree, @ref, @path)
@tree = TreeDecorator.new(@tree)

View File

@ -35,51 +35,6 @@ describe Commit do
end
end
describe "Commit info" do
before do
@committer = double(
email: 'mike@smith.com',
name: 'Mike Smith'
)
@author = double(
email: 'john@smith.com',
name: 'John Smith'
)
@raw_commit = double(
id: "bcf03b5de6abcf03b5de6c",
author: @author,
committer: @committer,
committed_date: Date.yesterday,
message: 'Refactoring specs'
)
@commit = Commit.new(@raw_commit)
end
it { @commit.short_id.should == "bcf03b5de6a" }
it { @commit.safe_message.should == @raw_commit.message }
it { @commit.created_at.should == @raw_commit.committed_date }
it { @commit.author_email.should == @author.email }
it { @commit.author_name.should == @author.name }
it { @commit.committer_name.should == @committer.name }
it { @commit.committer_email.should == @committer.email }
it { @commit.different_committer?.should be_true }
end
describe "Class methods" do
subject { Commit }
it { should respond_to(:find_or_first) }
it { should respond_to(:fresh_commits) }
it { should respond_to(:commits_with_refs) }
it { should respond_to(:commits_since) }
it { should respond_to(:commits_between) }
it { should respond_to(:commits) }
it { should respond_to(:compare) }
end
describe "delegation" do
subject { commit }

View File

@ -187,7 +187,7 @@ describe Project do
let(:project) { create(:project) }
it "should return valid repo" do
project.repository.should be_kind_of(Repository)
project.repository.should be_kind_of(Gitlab::Git::Repository)
end
it "should return nil" do

View File

@ -1,105 +0,0 @@
require "spec_helper"
describe Repository do
let(:project) { create(:project) }
let(:repository) { project.repository }
describe "Respond to" do
subject { repository }
it { should respond_to(:repo) }
it { should respond_to(:tree) }
it { should respond_to(:root_ref) }
it { should respond_to(:tags) }
it { should respond_to(:commit) }
it { should respond_to(:commits) }
it { should respond_to(:commits_between) }
it { should respond_to(:commits_with_refs) }
it { should respond_to(:commits_since) }
it { should respond_to(:commits_between) }
end
describe "#discover_default_branch" do
let(:master) { 'master' }
let(:stable) { 'stable' }
it "returns 'master' when master exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([stable, master])
repository.discover_default_branch.should == 'master'
end
it "returns non-master when master exists but default branch is set to something else" do
repository.root_ref = 'stable'
repository.should_receive(:branch_names).at_least(:once).and_return([stable, master])
repository.discover_default_branch.should == 'stable'
end
it "returns a non-master branch when only one exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([stable])
repository.discover_default_branch.should == 'stable'
end
it "returns nil when no branch exists" do
repository.should_receive(:branch_names).at_least(:once).and_return([])
repository.discover_default_branch.should be_nil
end
end
describe :commit do
it "should return first head commit if without params" do
repository.commit.id.should == repository.repo.commits.first.id
end
it "should return valid commit" do
repository.commit(ValidCommit::ID).should be_valid_commit
end
it "should return nil" do
repository.commit("+123_4532530XYZ").should be_nil
end
end
describe :tree do
before do
@commit = repository.commit(ValidCommit::ID)
end
it "should raise error w/o arguments" do
lambda { repository.tree }.should raise_error
end
it "should return root tree for commit" do
tree = repository.tree(@commit)
tree.contents.size.should == ValidCommit::FILES_COUNT
tree.contents.map(&:name).should == ValidCommit::FILES
end
it "should return root tree for commit with correct path" do
tree = repository.tree(@commit, ValidCommit::C_FILE_PATH)
tree.contents.map(&:name).should == ValidCommit::C_FILES
end
it "should return root tree for commit with incorrect path" do
repository.tree(@commit, "invalid_path").should be_nil
end
end
describe "fresh commits" do
it { repository.fresh_commits(3).count.should == 3 }
it { repository.fresh_commits.first.id.should == "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a" }
it { repository.fresh_commits.last.id.should == "f403da73f5e62794a0447aca879360494b08f678" }
end
describe "commits_between" do
subject do
commits = repository.commits_between("3a4b4fb4cde7809f033822a171b9feae19d41fff",
"8470d70da67355c9c009e4401746b1d5410af2e3")
commits.map { |c| c.id }
end
it { should have(3).elements }
it { should include("f0f14c8eaba69ebddd766498a9d0b0e79becd633") }
it { should_not include("bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a") }
end
end

View File

@ -1,4 +1,4 @@
require "repository"
require "gitlab/git/repository"
require "project"
require "merge_request"
require "shell"
@ -39,7 +39,7 @@ class MergeRequest
end
end
class GitLabTestRepo < Repository
class GitLabTestRepo < Gitlab::Git::Repository
def repo
@repo ||= Grit::Repo.new(Rails.root.join('tmp', 'repositories', 'gitlabhq'))
end