2012-06-07 14:44:57 +02:00
|
|
|
module Repository
|
2012-08-28 23:04:06 +02:00
|
|
|
include GitHost
|
|
|
|
|
2012-06-07 14:44:57 +02:00
|
|
|
def valid_repo?
|
|
|
|
repo
|
|
|
|
rescue
|
|
|
|
errors.add(:path, "Invalid repository path")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2012-09-04 17:37:38 +02:00
|
|
|
def empty_repo?
|
|
|
|
!repo_exists? || !has_commits?
|
|
|
|
end
|
|
|
|
|
2012-06-07 14:44:57 +02: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
|
|
|
|
|
2012-10-15 18:51:11 +02:00
|
|
|
def last_commit_for(ref, path = nil)
|
|
|
|
commits(ref, path, 1).first
|
|
|
|
end
|
|
|
|
|
2012-06-07 14:44:57 +02:00
|
|
|
def commits_between(from, to)
|
|
|
|
Commit.commits_between(repo, from, to)
|
|
|
|
end
|
|
|
|
|
|
|
|
def satellite
|
2012-10-26 00:56:24 +02:00
|
|
|
@satellite ||= Gitlab::Satellite::Satellite.new(self)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_post_receive_file?
|
|
|
|
hook_file = File.join(path_to_repo, 'hooks', 'post-receive')
|
2012-09-04 17:37:38 +02:00
|
|
|
File.exists?(hook_file)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-09-26 19:21:38 +02:00
|
|
|
# Returns an Array of branch names
|
2012-09-26 19:34:51 +02:00
|
|
|
def branch_names
|
2012-09-16 22:21:46 +02:00
|
|
|
repo.branches.collect(&:name).sort
|
|
|
|
end
|
|
|
|
|
2012-09-26 19:38:14 +02:00
|
|
|
# Returns an Array of Branches
|
|
|
|
def branches
|
|
|
|
repo.branches.sort_by(&:name)
|
|
|
|
end
|
|
|
|
|
2012-09-26 19:21:38 +02:00
|
|
|
# Returns an Array of tag names
|
2012-09-26 19:34:51 +02:00
|
|
|
def tag_names
|
2012-09-16 22:21:46 +02:00
|
|
|
repo.tags.collect(&:name).sort.reverse
|
|
|
|
end
|
|
|
|
|
2012-09-26 19:38:14 +02:00
|
|
|
# Returns an Array of Tags
|
|
|
|
def tags
|
|
|
|
repo.tags.sort_by(&:name).reverse
|
|
|
|
end
|
|
|
|
|
2012-09-26 19:21:38 +02:00
|
|
|
# Returns an Array of branch and tag names
|
2012-09-16 22:21:46 +02:00
|
|
|
def ref_names
|
2012-09-26 19:34:51 +02:00
|
|
|
[branch_names + tag_names].flatten
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
|
|
|
@repo ||= Grit::Repo.new(path_to_repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_to_repo
|
2012-11-22 20:41:16 +01:00
|
|
|
git_host.url_to_repo(path_with_namespace)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def path_to_repo
|
2012-11-22 20:41:16 +01:00
|
|
|
File.join(Gitlab.config.git_base_path, namespace_dir, "#{path}.git")
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_dir
|
2012-11-23 19:11:09 +01:00
|
|
|
namespace.try(:path) || ''
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_repository
|
2012-08-28 23:04:06 +02:00
|
|
|
git_host.update_repository(self)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_repository
|
2012-08-28 23:04:06 +02:00
|
|
|
git_host.remove_repository(self)
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo_exists?
|
|
|
|
@repo_exists ||= (repo && !repo.branches.empty?)
|
2012-09-04 17:37:38 +02:00
|
|
|
rescue
|
2012-06-07 14:44:57 +02:00
|
|
|
@repo_exists = false
|
|
|
|
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 open_branches
|
|
|
|
if protected_branches.empty?
|
|
|
|
self.repo.heads
|
|
|
|
else
|
|
|
|
pnames = protected_branches.map(&:name)
|
|
|
|
self.repo.heads.reject { |h| pnames.include?(h.name) }
|
|
|
|
end.sort_by(&:name)
|
|
|
|
end
|
|
|
|
|
2012-09-05 07:01:20 +02:00
|
|
|
# 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
|
2012-09-26 19:34:51 +02:00
|
|
|
if branch_names.length == 0
|
2012-09-05 07:01:20 +02:00
|
|
|
nil
|
2012-09-26 19:34:51 +02:00
|
|
|
elsif branch_names.length == 1
|
|
|
|
branch_names.first
|
2012-09-05 07:01:20 +02:00
|
|
|
else
|
2012-09-26 19:34:51 +02:00
|
|
|
branch_names.select { |v| v == root_ref }.first
|
2012-09-05 07:01:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-07 14:44:57 +02:00
|
|
|
def has_commits?
|
|
|
|
!!commit
|
2012-10-23 10:29:47 +02:00
|
|
|
rescue Grit::NoSuchPathError
|
|
|
|
false
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|
|
|
|
|
2012-09-04 17:37:38 +02:00
|
|
|
def root_ref
|
2012-06-07 14:44:57 +02:00
|
|
|
default_branch || "master"
|
|
|
|
end
|
|
|
|
|
2012-09-05 07:01:20 +02:00
|
|
|
def root_ref?(branch)
|
2012-06-07 14:44:57 +02:00
|
|
|
root_ref == branch
|
|
|
|
end
|
2012-06-13 08:03:53 +02:00
|
|
|
|
|
|
|
# Archive Project to .tar.gz
|
|
|
|
#
|
2012-09-04 17:37:38 +02:00
|
|
|
# Already packed repo archives stored at
|
2012-06-13 08:03:53 +02:00
|
|
|
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
|
|
|
|
#
|
2012-09-05 07:01:20 +02:00
|
|
|
def archive_repo(ref)
|
2012-06-13 08:03:53 +02:00
|
|
|
ref = ref || self.root_ref
|
|
|
|
commit = self.commit(ref)
|
|
|
|
return nil unless commit
|
|
|
|
|
|
|
|
# Build file path
|
2012-11-23 19:11:09 +01:00
|
|
|
file_name = self.path + "-" + commit.id.to_s + ".tar.gz"
|
|
|
|
storage_path = Rails.root.join("tmp", "repositories", self.path)
|
2012-06-13 08:03:53 +02:00
|
|
|
file_path = File.join(storage_path, file_name)
|
|
|
|
|
2012-06-27 19:21:44 +02:00
|
|
|
# Put files into a directory before archiving
|
2012-11-23 19:11:09 +01:00
|
|
|
prefix = self.path + "/"
|
2012-06-27 19:21:44 +02:00
|
|
|
|
2012-06-13 08:03:53 +02:00
|
|
|
# Create file if not exists
|
|
|
|
unless File.exists?(file_path)
|
|
|
|
FileUtils.mkdir_p storage_path
|
2012-06-27 19:21:44 +02:00
|
|
|
file = self.repo.archive_to_file(ref, prefix, file_path)
|
2012-06-13 08:03:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
file_path
|
|
|
|
end
|
2012-07-02 20:51:48 +02:00
|
|
|
|
|
|
|
def ssh_url_to_repo
|
|
|
|
url_to_repo
|
|
|
|
end
|
|
|
|
|
|
|
|
def http_url_to_repo
|
2012-09-05 07:01:20 +02:00
|
|
|
http_url = [Gitlab.config.url, "/", path, ".git"].join('')
|
2012-07-02 20:51:48 +02:00
|
|
|
end
|
2012-10-21 11:12:14 +02:00
|
|
|
|
|
|
|
# Check if current branch name is marked as protected in the system
|
|
|
|
def protected_branch? branch_name
|
|
|
|
protected_branches.map(&:name).include?(branch_name)
|
|
|
|
end
|
2012-06-07 14:44:57 +02:00
|
|
|
end
|