2011-12-04 21:19:49 +01:00
|
|
|
require File.join(Rails.root, "lib", "gitlabhq", "git_host")
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
class Repository
|
2011-12-07 00:27:07 +01:00
|
|
|
REPO_N = 0
|
|
|
|
REPO_R = 1
|
|
|
|
REPO_RW = 2
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def self.default_ref
|
|
|
|
"master"
|
|
|
|
end
|
|
|
|
|
2011-12-07 00:27:07 +01:00
|
|
|
def self.access_options
|
|
|
|
{
|
|
|
|
"Denied" => REPO_N,
|
|
|
|
"Pull" => REPO_R,
|
|
|
|
"Pull & Push" => REPO_RW
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
2011-11-15 09:34:30 +01:00
|
|
|
def path
|
2011-11-11 00:28:26 +01:00
|
|
|
@path ||= project.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_id
|
|
|
|
project.id
|
|
|
|
end
|
|
|
|
|
2011-12-13 20:39:02 +01:00
|
|
|
def write_hooks
|
|
|
|
%w(post-receive).each do |hook|
|
|
|
|
write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_hook(name, content)
|
2011-12-13 01:03:26 +01:00
|
|
|
hook_file = File.join(project.path_to_repo, 'hooks', name)
|
|
|
|
|
|
|
|
File.open(hook_file, 'w') do |f|
|
|
|
|
f.write(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
File.chmod(0775, hook_file)
|
|
|
|
end
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
def repo
|
|
|
|
@repo ||= Grit::Repo.new(project.path_to_repo)
|
|
|
|
end
|
|
|
|
|
2011-11-11 00:28:26 +01:00
|
|
|
def url_to_repo
|
2011-12-05 08:43:53 +01:00
|
|
|
Gitlabhq::GitHost.url_to_repo(path)
|
2011-11-11 00:28:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def path_to_repo
|
2011-12-04 00:44:59 +01:00
|
|
|
GIT_HOST["base_path"] + path + ".git"
|
2011-11-11 00:28:26 +01:00
|
|
|
end
|
|
|
|
|
2011-12-05 08:43:53 +01:00
|
|
|
def update_repository
|
2011-12-04 21:19:49 +01:00
|
|
|
Gitlabhq::GitHost.system.new.configure do |c|
|
2011-12-07 00:27:07 +01:00
|
|
|
c.update_project(path, project)
|
2011-11-11 00:28:26 +01:00
|
|
|
end
|
2011-12-13 20:39:02 +01:00
|
|
|
|
2011-12-28 08:08:40 +01:00
|
|
|
write_hooks if File.exists?(project.path_to_repo)
|
2011-11-11 00:28:26 +01:00
|
|
|
end
|
|
|
|
|
2011-12-05 08:43:53 +01:00
|
|
|
def destroy_repository
|
2011-12-04 21:19:49 +01:00
|
|
|
Gitlabhq::GitHost.system.new.configure do |c|
|
2011-11-11 00:28:26 +01:00
|
|
|
c.destroy_project(@project)
|
|
|
|
end
|
2011-11-10 23:51:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo_exists?
|
|
|
|
repo rescue false
|
|
|
|
end
|
|
|
|
|
2011-11-11 00:28:26 +01:00
|
|
|
def tags
|
|
|
|
repo.tags.map(&:name).sort.reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
def heads
|
|
|
|
@heads ||= repo.heads
|
2011-11-10 23:51:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def tree(fcommit, path = nil)
|
|
|
|
fcommit = commit if fcommit == :head
|
|
|
|
tree = fcommit.tree
|
|
|
|
path ? (tree / path) : tree
|
|
|
|
end
|
|
|
|
|
2011-11-11 00:28:26 +01:00
|
|
|
def commit(commit_id = nil)
|
2011-11-27 16:35:49 +01:00
|
|
|
commit = if commit_id
|
|
|
|
repo.commits(commit_id).first
|
|
|
|
else
|
|
|
|
repo.commits.first
|
|
|
|
end
|
|
|
|
Commit.new(commit) if commit
|
2011-11-11 00:28:26 +01:00
|
|
|
end
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
def fresh_commits(n = 10)
|
|
|
|
commits = heads.map do |h|
|
2011-11-27 16:35:49 +01:00
|
|
|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
|
2011-11-10 23:51:19 +01:00
|
|
|
end.flatten.uniq { |c| c.id }
|
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
commits[0...n]
|
|
|
|
end
|
|
|
|
|
2012-01-04 07:17:41 +01:00
|
|
|
def commits_with_refs(n = 20)
|
2012-01-04 07:19:41 +01:00
|
|
|
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
|
2012-01-04 07:17:41 +01:00
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
2012-01-04 21:19:41 +01:00
|
|
|
end
|
2012-01-04 07:17:41 +01:00
|
|
|
|
2012-01-04 21:19:41 +01:00
|
|
|
commits[0..n]
|
2012-01-04 07:17:41 +01:00
|
|
|
end
|
|
|
|
|
2011-11-10 23:51:19 +01:00
|
|
|
def commits_since(date)
|
|
|
|
commits = heads.map do |h|
|
2011-11-27 16:35:49 +01:00
|
|
|
repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
|
2011-11-10 23:51:19 +01:00
|
|
|
end.flatten.uniq { |c| c.id }
|
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
end
|
2011-11-27 16:35:49 +01:00
|
|
|
|
|
|
|
def commits(ref, path = nil, limit = nil, offset = nil)
|
|
|
|
if path
|
|
|
|
repo.log(ref, path, :max_count => limit, :skip => offset)
|
|
|
|
elsif limit && offset
|
|
|
|
repo.commits(ref, limit, offset)
|
|
|
|
else
|
|
|
|
repo.commits(ref)
|
|
|
|
end.map{ |c| Commit.new(c) }
|
|
|
|
end
|
2011-12-14 17:38:52 +01:00
|
|
|
|
|
|
|
def commits_between(from, to)
|
|
|
|
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
|
|
|
end
|
2011-11-10 23:51:19 +01:00
|
|
|
end
|