Commit 9dc644635f99dffa26555eeb1c92f988221ccb6a
1 parent
154e54b4
Exists in
master
and in
4 other branches
Fix tests and remove app/models/repository.rb
Showing
6 changed files
with
5 additions
and
325 deletions
Show diff stats
app/models/repository.rb
| @@ -1,170 +0,0 @@ | @@ -1,170 +0,0 @@ | ||
| 1 | -class Repository | ||
| 2 | - include Gitlab::Popen | ||
| 3 | - | ||
| 4 | - # Repository directory name with namespace direcotry | ||
| 5 | - # Examples: | ||
| 6 | - # gitlab/gitolite | ||
| 7 | - # diaspora | ||
| 8 | - # | ||
| 9 | - attr_accessor :path_with_namespace | ||
| 10 | - | ||
| 11 | - # Grit repo object | ||
| 12 | - attr_accessor :repo | ||
| 13 | - | ||
| 14 | - # Default branch in the repository | ||
| 15 | - attr_accessor :root_ref | ||
| 16 | - | ||
| 17 | - def initialize(path_with_namespace, root_ref = 'master') | ||
| 18 | - @root_ref = root_ref || "master" | ||
| 19 | - @path_with_namespace = path_with_namespace | ||
| 20 | - | ||
| 21 | - # Init grit repo object | ||
| 22 | - repo | ||
| 23 | - end | ||
| 24 | - | ||
| 25 | - def raw | ||
| 26 | - repo | ||
| 27 | - end | ||
| 28 | - | ||
| 29 | - def path_to_repo | ||
| 30 | - @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git") | ||
| 31 | - end | ||
| 32 | - | ||
| 33 | - def repo | ||
| 34 | - @repo ||= Grit::Repo.new(path_to_repo) | ||
| 35 | - end | ||
| 36 | - | ||
| 37 | - def commit(commit_id = nil) | ||
| 38 | - Commit.find_or_first(repo, commit_id, root_ref) | ||
| 39 | - end | ||
| 40 | - | ||
| 41 | - def fresh_commits(n = 10) | ||
| 42 | - Commit.fresh_commits(repo, n) | ||
| 43 | - end | ||
| 44 | - | ||
| 45 | - def commits_with_refs(n = 20) | ||
| 46 | - Commit.commits_with_refs(repo, n) | ||
| 47 | - end | ||
| 48 | - | ||
| 49 | - def commits_since(date) | ||
| 50 | - Commit.commits_since(repo, date) | ||
| 51 | - end | ||
| 52 | - | ||
| 53 | - def commits(ref, path = nil, limit = nil, offset = nil) | ||
| 54 | - Commit.commits(repo, ref, path, limit, offset) | ||
| 55 | - end | ||
| 56 | - | ||
| 57 | - def last_commit_for(ref, path = nil) | ||
| 58 | - commits(ref, path, 1).first | ||
| 59 | - end | ||
| 60 | - | ||
| 61 | - def commits_between(from, to) | ||
| 62 | - Commit.commits_between(repo, from, to) | ||
| 63 | - end | ||
| 64 | - | ||
| 65 | - # Returns an Array of branch names | ||
| 66 | - # sorted by name ASC | ||
| 67 | - def branch_names | ||
| 68 | - branches.map(&:name) | ||
| 69 | - end | ||
| 70 | - | ||
| 71 | - # Returns an Array of Branches | ||
| 72 | - def branches | ||
| 73 | - repo.branches.sort_by(&:name) | ||
| 74 | - end | ||
| 75 | - | ||
| 76 | - # Returns an Array of tag names | ||
| 77 | - def tag_names | ||
| 78 | - repo.tags.collect(&:name).sort.reverse | ||
| 79 | - end | ||
| 80 | - | ||
| 81 | - # Returns an Array of Tags | ||
| 82 | - def tags | ||
| 83 | - repo.tags.sort_by(&:name).reverse | ||
| 84 | - end | ||
| 85 | - | ||
| 86 | - # Returns an Array of branch and tag names | ||
| 87 | - def ref_names | ||
| 88 | - [branch_names + tag_names].flatten | ||
| 89 | - end | ||
| 90 | - | ||
| 91 | - def heads | ||
| 92 | - @heads ||= repo.heads | ||
| 93 | - end | ||
| 94 | - | ||
| 95 | - def tree(fcommit, path = nil) | ||
| 96 | - fcommit = commit if fcommit == :head | ||
| 97 | - tree = fcommit.tree | ||
| 98 | - path ? (tree / path) : tree | ||
| 99 | - end | ||
| 100 | - | ||
| 101 | - def has_commits? | ||
| 102 | - !!commit | ||
| 103 | - rescue Grit::NoSuchPathError | ||
| 104 | - false | ||
| 105 | - end | ||
| 106 | - | ||
| 107 | - def empty? | ||
| 108 | - !has_commits? | ||
| 109 | - end | ||
| 110 | - | ||
| 111 | - # Discovers the default branch based on the repository's available branches | ||
| 112 | - # | ||
| 113 | - # - If no branches are present, returns nil | ||
| 114 | - # - If one branch is present, returns its name | ||
| 115 | - # - If two or more branches are present, returns the one that has a name | ||
| 116 | - # matching root_ref (default_branch or 'master' if default_branch is nil) | ||
| 117 | - def discover_default_branch | ||
| 118 | - if branch_names.length == 0 | ||
| 119 | - nil | ||
| 120 | - elsif branch_names.length == 1 | ||
| 121 | - branch_names.first | ||
| 122 | - else | ||
| 123 | - branch_names.select { |v| v == root_ref }.first | ||
| 124 | - end | ||
| 125 | - end | ||
| 126 | - | ||
| 127 | - # Archive Project to .tar.gz | ||
| 128 | - # | ||
| 129 | - # Already packed repo archives stored at | ||
| 130 | - # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz | ||
| 131 | - # | ||
| 132 | - def archive_repo(ref) | ||
| 133 | - ref = ref || self.root_ref | ||
| 134 | - commit = self.commit(ref) | ||
| 135 | - return nil unless commit | ||
| 136 | - | ||
| 137 | - # Build file path | ||
| 138 | - file_name = self.path_with_namespace.gsub("/","_") + "-" + commit.id.to_s + ".tar.gz" | ||
| 139 | - storage_path = Rails.root.join("tmp", "repositories") | ||
| 140 | - file_path = File.join(storage_path, self.path_with_namespace, file_name) | ||
| 141 | - | ||
| 142 | - # Put files into a directory before archiving | ||
| 143 | - prefix = File.basename(self.path_with_namespace) + "/" | ||
| 144 | - | ||
| 145 | - # Create file if not exists | ||
| 146 | - unless File.exists?(file_path) | ||
| 147 | - FileUtils.mkdir_p File.dirname(file_path) | ||
| 148 | - file = self.repo.archive_to_file(ref, prefix, file_path) | ||
| 149 | - end | ||
| 150 | - | ||
| 151 | - file_path | ||
| 152 | - end | ||
| 153 | - | ||
| 154 | - # Return repo size in megabytes | ||
| 155 | - # Cached in redis | ||
| 156 | - def size | ||
| 157 | - Rails.cache.fetch(cache_key(:size)) do | ||
| 158 | - size = popen('du -s', path_to_repo).first.strip.to_i | ||
| 159 | - (size.to_f / 1024).round(2) | ||
| 160 | - end | ||
| 161 | - end | ||
| 162 | - | ||
| 163 | - def expire_cache | ||
| 164 | - Rails.cache.delete(cache_key(:size)) | ||
| 165 | - end | ||
| 166 | - | ||
| 167 | - def cache_key(type) | ||
| 168 | - "#{type}:#{path_with_namespace}" | ||
| 169 | - end | ||
| 170 | -end |
lib/extracts_path.rb
| @@ -100,8 +100,8 @@ module ExtractsPath | @@ -100,8 +100,8 @@ module ExtractsPath | ||
| 100 | 100 | ||
| 101 | # It is used "@project.repository.commits(@ref, @path, 1, 0)", | 101 | # It is used "@project.repository.commits(@ref, @path, 1, 0)", |
| 102 | # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name. | 102 | # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name. |
| 103 | - commits = @project.repository.commits(@ref, @path, 1, 0) | ||
| 104 | - @commit = CommitDecorator.decorate(commits.first) | 103 | + @commit = @project.repository.commits(@ref, @path, 1, 0).first |
| 104 | + @commit = CommitDecorator.decorate(@commit) | ||
| 105 | 105 | ||
| 106 | @tree = Tree.new(@commit.tree, @ref, @path) | 106 | @tree = Tree.new(@commit.tree, @ref, @path) |
| 107 | @tree = TreeDecorator.new(@tree) | 107 | @tree = TreeDecorator.new(@tree) |
spec/models/commit_spec.rb
| @@ -35,51 +35,6 @@ describe Commit do | @@ -35,51 +35,6 @@ describe Commit do | ||
| 35 | end | 35 | end |
| 36 | end | 36 | end |
| 37 | 37 | ||
| 38 | - describe "Commit info" do | ||
| 39 | - before do | ||
| 40 | - @committer = double( | ||
| 41 | - email: 'mike@smith.com', | ||
| 42 | - name: 'Mike Smith' | ||
| 43 | - ) | ||
| 44 | - | ||
| 45 | - @author = double( | ||
| 46 | - email: 'john@smith.com', | ||
| 47 | - name: 'John Smith' | ||
| 48 | - ) | ||
| 49 | - | ||
| 50 | - @raw_commit = double( | ||
| 51 | - id: "bcf03b5de6abcf03b5de6c", | ||
| 52 | - author: @author, | ||
| 53 | - committer: @committer, | ||
| 54 | - committed_date: Date.yesterday, | ||
| 55 | - message: 'Refactoring specs' | ||
| 56 | - ) | ||
| 57 | - | ||
| 58 | - @commit = Commit.new(@raw_commit) | ||
| 59 | - end | ||
| 60 | - | ||
| 61 | - it { @commit.short_id.should == "bcf03b5de6a" } | ||
| 62 | - it { @commit.safe_message.should == @raw_commit.message } | ||
| 63 | - it { @commit.created_at.should == @raw_commit.committed_date } | ||
| 64 | - it { @commit.author_email.should == @author.email } | ||
| 65 | - it { @commit.author_name.should == @author.name } | ||
| 66 | - it { @commit.committer_name.should == @committer.name } | ||
| 67 | - it { @commit.committer_email.should == @committer.email } | ||
| 68 | - it { @commit.different_committer?.should be_true } | ||
| 69 | - end | ||
| 70 | - | ||
| 71 | - describe "Class methods" do | ||
| 72 | - subject { Commit } | ||
| 73 | - | ||
| 74 | - it { should respond_to(:find_or_first) } | ||
| 75 | - it { should respond_to(:fresh_commits) } | ||
| 76 | - it { should respond_to(:commits_with_refs) } | ||
| 77 | - it { should respond_to(:commits_since) } | ||
| 78 | - it { should respond_to(:commits_between) } | ||
| 79 | - it { should respond_to(:commits) } | ||
| 80 | - it { should respond_to(:compare) } | ||
| 81 | - end | ||
| 82 | - | ||
| 83 | describe "delegation" do | 38 | describe "delegation" do |
| 84 | subject { commit } | 39 | subject { commit } |
| 85 | 40 |
spec/models/project_spec.rb
| @@ -187,7 +187,7 @@ describe Project do | @@ -187,7 +187,7 @@ describe Project do | ||
| 187 | let(:project) { create(:project) } | 187 | let(:project) { create(:project) } |
| 188 | 188 | ||
| 189 | it "should return valid repo" do | 189 | it "should return valid repo" do |
| 190 | - project.repository.should be_kind_of(Repository) | 190 | + project.repository.should be_kind_of(Gitlab::Git::Repository) |
| 191 | end | 191 | end |
| 192 | 192 | ||
| 193 | it "should return nil" do | 193 | it "should return nil" do |
spec/models/repository_spec.rb
| @@ -1,105 +0,0 @@ | @@ -1,105 +0,0 @@ | ||
| 1 | -require "spec_helper" | ||
| 2 | - | ||
| 3 | -describe Repository do | ||
| 4 | - let(:project) { create(:project) } | ||
| 5 | - let(:repository) { project.repository } | ||
| 6 | - | ||
| 7 | - describe "Respond to" do | ||
| 8 | - subject { repository } | ||
| 9 | - | ||
| 10 | - it { should respond_to(:repo) } | ||
| 11 | - it { should respond_to(:tree) } | ||
| 12 | - it { should respond_to(:root_ref) } | ||
| 13 | - it { should respond_to(:tags) } | ||
| 14 | - it { should respond_to(:commit) } | ||
| 15 | - it { should respond_to(:commits) } | ||
| 16 | - it { should respond_to(:commits_between) } | ||
| 17 | - it { should respond_to(:commits_with_refs) } | ||
| 18 | - it { should respond_to(:commits_since) } | ||
| 19 | - it { should respond_to(:commits_between) } | ||
| 20 | - end | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - describe "#discover_default_branch" do | ||
| 24 | - let(:master) { 'master' } | ||
| 25 | - let(:stable) { 'stable' } | ||
| 26 | - | ||
| 27 | - it "returns 'master' when master exists" do | ||
| 28 | - repository.should_receive(:branch_names).at_least(:once).and_return([stable, master]) | ||
| 29 | - repository.discover_default_branch.should == 'master' | ||
| 30 | - end | ||
| 31 | - | ||
| 32 | - it "returns non-master when master exists but default branch is set to something else" do | ||
| 33 | - repository.root_ref = 'stable' | ||
| 34 | - repository.should_receive(:branch_names).at_least(:once).and_return([stable, master]) | ||
| 35 | - repository.discover_default_branch.should == 'stable' | ||
| 36 | - end | ||
| 37 | - | ||
| 38 | - it "returns a non-master branch when only one exists" do | ||
| 39 | - repository.should_receive(:branch_names).at_least(:once).and_return([stable]) | ||
| 40 | - repository.discover_default_branch.should == 'stable' | ||
| 41 | - end | ||
| 42 | - | ||
| 43 | - it "returns nil when no branch exists" do | ||
| 44 | - repository.should_receive(:branch_names).at_least(:once).and_return([]) | ||
| 45 | - repository.discover_default_branch.should be_nil | ||
| 46 | - end | ||
| 47 | - end | ||
| 48 | - | ||
| 49 | - describe :commit do | ||
| 50 | - it "should return first head commit if without params" do | ||
| 51 | - repository.commit.id.should == repository.repo.commits.first.id | ||
| 52 | - end | ||
| 53 | - | ||
| 54 | - it "should return valid commit" do | ||
| 55 | - repository.commit(ValidCommit::ID).should be_valid_commit | ||
| 56 | - end | ||
| 57 | - | ||
| 58 | - it "should return nil" do | ||
| 59 | - repository.commit("+123_4532530XYZ").should be_nil | ||
| 60 | - end | ||
| 61 | - end | ||
| 62 | - | ||
| 63 | - describe :tree do | ||
| 64 | - before do | ||
| 65 | - @commit = repository.commit(ValidCommit::ID) | ||
| 66 | - end | ||
| 67 | - | ||
| 68 | - it "should raise error w/o arguments" do | ||
| 69 | - lambda { repository.tree }.should raise_error | ||
| 70 | - end | ||
| 71 | - | ||
| 72 | - it "should return root tree for commit" do | ||
| 73 | - tree = repository.tree(@commit) | ||
| 74 | - tree.contents.size.should == ValidCommit::FILES_COUNT | ||
| 75 | - tree.contents.map(&:name).should == ValidCommit::FILES | ||
| 76 | - end | ||
| 77 | - | ||
| 78 | - it "should return root tree for commit with correct path" do | ||
| 79 | - tree = repository.tree(@commit, ValidCommit::C_FILE_PATH) | ||
| 80 | - tree.contents.map(&:name).should == ValidCommit::C_FILES | ||
| 81 | - end | ||
| 82 | - | ||
| 83 | - it "should return root tree for commit with incorrect path" do | ||
| 84 | - repository.tree(@commit, "invalid_path").should be_nil | ||
| 85 | - end | ||
| 86 | - end | ||
| 87 | - | ||
| 88 | - describe "fresh commits" do | ||
| 89 | - it { repository.fresh_commits(3).count.should == 3 } | ||
| 90 | - it { repository.fresh_commits.first.id.should == "bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a" } | ||
| 91 | - it { repository.fresh_commits.last.id.should == "f403da73f5e62794a0447aca879360494b08f678" } | ||
| 92 | - end | ||
| 93 | - | ||
| 94 | - describe "commits_between" do | ||
| 95 | - subject do | ||
| 96 | - commits = repository.commits_between("3a4b4fb4cde7809f033822a171b9feae19d41fff", | ||
| 97 | - "8470d70da67355c9c009e4401746b1d5410af2e3") | ||
| 98 | - commits.map { |c| c.id } | ||
| 99 | - end | ||
| 100 | - | ||
| 101 | - it { should have(3).elements } | ||
| 102 | - it { should include("f0f14c8eaba69ebddd766498a9d0b0e79becd633") } | ||
| 103 | - it { should_not include("bcf03b5de6c33f3869ef70d68cf06e679d1d7f9a") } | ||
| 104 | - end | ||
| 105 | -end |
spec/support/stubbed_repository.rb
| 1 | -require "repository" | 1 | +require "gitlab/git/repository" |
| 2 | require "project" | 2 | require "project" |
| 3 | require "merge_request" | 3 | require "merge_request" |
| 4 | require "shell" | 4 | require "shell" |
| @@ -39,7 +39,7 @@ class MergeRequest | @@ -39,7 +39,7 @@ class MergeRequest | ||
| 39 | end | 39 | end |
| 40 | end | 40 | end |
| 41 | 41 | ||
| 42 | -class GitLabTestRepo < Repository | 42 | +class GitLabTestRepo < Gitlab::Git::Repository |
| 43 | def repo | 43 | def repo |
| 44 | @repo ||= Grit::Repo.new(Rails.root.join('tmp', 'repositories', 'gitlabhq')) | 44 | @repo ||= Grit::Repo.new(Rails.root.join('tmp', 'repositories', 'gitlabhq')) |
| 45 | end | 45 | end |