lib/gitlab_git/repository.rb in gitlab_git-5.0.0 vs lib/gitlab_git/repository.rb in gitlab_git-5.1.0

- old
+ new

@@ -18,12 +18,12 @@ attr_reader :name # Grit repo object attr_reader :grit - # Alias to old method for compatibility - alias_method :raw, :grit + # Rugged repo object + attr_reader :rugged def initialize(path) @path = path @name = path.split("/").last @root_ref = discover_default_branch @@ -33,62 +33,84 @@ @grit ||= Grit::Repo.new(path) rescue Grit::NoSuchPathError raise NoRepository.new('no repository for such path') end + # Alias to old method for compatibility + def raw + grit + end + + def rugged + @rugged ||= Rugged::Repository.new(path) + rescue Rugged::RepositoryError, Rugged::OSError + raise NoRepository.new('no repository for such path') + 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 - grit.branches.sort_by(&:name) + rugged.refs.select do |ref| + ref.name =~ /\Arefs\/heads/ + end.map do |rugged_ref| + Branch.new(rugged_ref.name, rugged_ref.target) + end.sort_by(&:name) end # Returns an Array of tag names def tag_names tags.map(&:name) end # Returns an Array of Tags def tags - grit.tags.sort_by(&:name).reverse + rugged.refs.select do |ref| + ref.name =~ /\Arefs\/tags/ + end.map do |rugged_ref| + Tag.new(rugged_ref.name, rugged_ref.target) + end.sort_by(&:name) end # Returns an Array of branch and tag names def ref_names branch_names + tag_names end + # Deprecated. Will be removed in 5.2 def heads @heads ||= grit.heads.sort_by(&:name) end def has_commits? - !!Gitlab::Git::Commit.last(self) - rescue Grit::NoSuchPathError - false + !empty? end def empty? - !has_commits? + rugged.empty? end + def repo_exists? + !!rugged + 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 current HEAD or master or first branch def discover_default_branch if branch_names.length == 0 nil elsif branch_names.length == 1 branch_names.first - elsif grit.head - grit.head.name + elsif rugged.head + Ref.extract_branch_name(rugged.head.name) elsif branch_names.include?("master") "master" elsif branch_names.first end @@ -101,15 +123,15 @@ # def archive_repo(ref, storage_path, format = "tar.gz") ref = ref || self.root_ref commit = Gitlab::Git::Commit.find(self, ref) return nil unless commit - + extension = nil git_archive_format = nil pipe_cmd = nil - + case format when "tar.bz2", "tbz", "tbz2", "tb2", "bz2" extension = ".tar.bz2" pipe_cmd = "bzip" when "tar" @@ -290,9 +312,14 @@ grit.refs.each do |r| @refs_hash[r.commit.id] << r end end @refs_hash + end + + # Lookup for rugged object by oid + def lookup(oid) + rugged.lookup(oid) end end end end