-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
repository_trait.rb
131 lines (104 loc) · 2.82 KB
/
repository_trait.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module Project::RepositoryTrait
as_trait do
def valid_repo?
repo
rescue
errors.add(:path, "Invalid repository path")
false
end
def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id)
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 commits_between(from, to)
Commit.commits_between(repo, from, to)
end
def write_hooks
%w(post-receive).each do |hook|
write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
end
end
def path_to_repo_satellite
File.join(Rails.root, "tmp", "repo_satellites", self.path)
end
def satellite_exists?
File.exist? path_to_repo_satellite
end
def create_repo_satellite
`git clone #{url_to_repo} #{path_to_repo_satellite}`
Dir.chdir(path_to_repo_satellite) do
primary_branch = Grit::Repo.new(".").heads.first.name #usually it`s master
`git checkout -b __parking_branch`
`git branch -D #{primary_branch}`
end
end
def write_hook(name, content)
hook_file = File.join(path_to_repo, 'hooks', name)
File.open(hook_file, 'w') do |f|
f.write(content)
end
File.chmod(0775, hook_file)
end
def has_post_receive_file?
hook_file = File.join(path_to_repo, 'hooks', 'post-receive')
File.exists?(hook_file)
end
def tags
repo.tags.map(&:name).sort.reverse
end
def repo
@repo ||= Grit::Repo.new(path_to_repo)
end
def url_to_repo
Gitlabhq::GitHost.url_to_repo(path)
end
def path_to_repo
File.join(GIT_HOST["base_path"], "#{path}.git")
end
def update_repository
Gitlabhq::GitHost.system.update_project(path, self)
write_hooks if File.exists?(path_to_repo)
end
def destroy_repository
Gitlabhq::GitHost.system.destroy_project(self)
end
def repo_exists?
@repo_exists ||= (repo && !repo.branches.empty?)
rescue
@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
def has_commits?
!!commit
end
def root_ref
default_branch || "master"
end
end
end