2013-01-03 20:09:18 +01:00
|
|
|
class Repository
|
|
|
|
# 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')
|
2013-01-04 07:43:25 +01:00
|
|
|
@root_ref = root_ref || "master"
|
2013-01-03 20:09:18 +01:00
|
|
|
@path_with_namespace = path_with_namespace
|
2013-01-04 07:43:25 +01:00
|
|
|
|
|
|
|
# Init grit repo object
|
|
|
|
repo
|
2013-01-03 20:09:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
|
|
|
repo
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_to_repo
|
2013-02-11 18:16:59 +01:00
|
|
|
@path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
|
2013-01-03 20:09:18 +01:00
|
|
|
end
|
|
|
|
|
2013-01-04 07:43:25 +01:00
|
|
|
def repo
|
|
|
|
@repo ||= Grit::Repo.new(path_to_repo)
|
|
|
|
end
|
|
|
|
|
2013-01-03 20:09:18 +01:00
|
|
|
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
|
|
|
|
def branch_names
|
|
|
|
repo.branches.collect(&:name).sort
|
|
|
|
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
|
|
|
|
|
2013-01-04 07:43:25 +01:00
|
|
|
def empty?
|
2013-01-03 20:09:18 +01:00
|
|
|
!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
|
2013-02-15 13:58:57 +01:00
|
|
|
file_name = self.path_with_namespace.gsub("/","_") + "-" + commit.id.to_s + ".tar.gz"
|
2013-01-10 21:55:34 +01:00
|
|
|
storage_path = Rails.root.join("tmp", "repositories")
|
2013-02-15 14:50:42 +01:00
|
|
|
file_path = File.join(storage_path, self.path_with_namespace, file_name)
|
2013-01-03 20:09:18 +01:00
|
|
|
|
|
|
|
# Put files into a directory before archiving
|
2013-03-05 16:06:22 +01:00
|
|
|
prefix = File.basename(self.path_with_namespace) + "/"
|
2013-01-03 20:09:18 +01:00
|
|
|
|
|
|
|
# Create file if not exists
|
|
|
|
unless File.exists?(file_path)
|
2013-02-14 14:29:24 +01:00
|
|
|
FileUtils.mkdir_p File.dirname(file_path)
|
2013-01-03 20:09:18 +01:00
|
|
|
file = self.repo.archive_to_file(ref, prefix, file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
file_path
|
|
|
|
end
|
|
|
|
end
|