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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
require 'open3'
require 'fileutils'
require_relative 'gitlab_config'
require_relative 'gitlab_logger'
class GitlabProjects
# Project name is a directory name for repository with .git at the end
# It may be namespaced or not. Like repo.git or gitlab/repo.git
attr_reader :project_name
# Absolute path to directory where repositories stored
# By default it is /home/git/repositories
attr_reader :repos_path
# Full path is an absolute path to the repository
# Ex /home/git/repositories/test.git
attr_reader :full_path
def initialize
@command = ARGV.shift
@project_name = ARGV.shift
@repos_path = GitlabConfig.new.repos_path
@full_path = File.join(@repos_path, @project_name)
end
def exec
case @command
when 'create-branch'; create_branch
when 'rm-branch'; rm_branch
when 'create-tag'; create_tag
when 'rm-tag'; rm_tag
when 'add-project'; add_project
when 'rm-project'; rm_project
when 'mv-project'; mv_project
when 'import-project'; import_project
when 'fork-project'; fork_project
when 'update-head'; update_head
else
$logger.warn "Attempt to execute invalid gitlab-projects command #{@command.inspect}."
puts 'not allowed'
false
end
end
protected
def create_branch
branch_name = ARGV.shift
ref = ARGV.shift || "HEAD"
cmd = "cd #{full_path} && git branch #{branch_name} #{ref}"
system(cmd)
end
def rm_branch
branch_name = ARGV.shift
cmd = "cd #{full_path} && git branch -D #{branch_name}"
system(cmd)
end
def create_tag
tag_name = ARGV.shift
ref = ARGV.shift || "HEAD"
cmd = "cd #{full_path} && git tag #{tag_name} #{ref}"
system(cmd)
end
def rm_tag
tag_name = ARGV.shift
cmd = "cd #{full_path} && git tag -d #{tag_name}"
system(cmd)
end
def add_project
$logger.info "Adding project #{@project_name} at <#{full_path}>."
FileUtils.mkdir_p(full_path, mode: 0770)
cmd = "cd #{full_path} && git init --bare && #{create_hooks_cmd}"
system(cmd)
end
def create_hooks_cmd
create_hooks_to(full_path)
end
def rm_project
$logger.info "Removing project #{@project_name} from <#{full_path}>."
FileUtils.rm_rf(full_path)
end
# Import project via git clone --bare
# URL must be publicly cloneable
def import_project
@source = ARGV.shift
$logger.info "Importing project #{@project_name} from <#{@source}> to <#{full_path}>."
cmd = "cd #{repos_path} && git clone --bare #{@source} #{project_name} && #{create_hooks_cmd}"
system(cmd)
end
# Move repository from one directory to another
#
# Ex.
# gitlab.git -> gitlabhq.git
# gitlab/gitlab-ci.git -> randx/six.git
#
# Wont work if target namespace directory does not exist
#
def mv_project
new_path = ARGV.shift
unless new_path
$logger.error "mv-project failed: no destination path provided."
return false
end
new_full_path = File.join(repos_path, new_path)
# verify that the source repo exists
unless File.exists?(full_path)
$logger.error "mv-project failed: source path <#{full_path}> does not exist."
return false
end
# ...and that the target repo does not exist
if File.exists?(new_full_path)
$logger.error "mv-project failed: destination path <#{new_full_path}> already exists."
return false
end
$logger.info "Moving project #{@project_name} from <#{full_path}> to <#{new_full_path}>."
FileUtils.mv(full_path, new_full_path)
end
def fork_project
new_namespace = ARGV.shift
# destination namespace must be provided
unless new_namespace
$logger.error "fork-project failed: no destination namespace provided."
return false
end
# destination namespace must exist
namespaced_path = File.join(repos_path, new_namespace)
unless File.exists?(namespaced_path)
$logger.error "fork-project failed: destination namespace <#{namespaced_path}> does not exist."
return false
end
# a project of the same name cannot already be within the destination namespace
full_destination_path = File.join(namespaced_path, project_name.split('/')[-1])
if File.exists?(full_destination_path)
$logger.error "fork-project failed: destination repository <#{full_destination_path}> already exists."
return false
end
$logger.info "Forking project from <#{full_path}> to <#{full_destination_path}>."
cmd = "cd #{namespaced_path} && git clone --bare #{full_path} && #{create_hooks_to(full_destination_path)}"
system(cmd)
end
def update_head
new_head = ARGV.shift
unless new_head
$logger.error "update-head failed: no branch provided."
return false
end
unless File.exists?(File.join(full_path, 'refs/heads', new_head))
$logger.error "update-head failed: specified branch does not exist in ref/heads."
return false
end
File.open(File.join(full_path, 'HEAD'), 'w') do |f|
f.write("ref: refs/heads/#{new_head}")
end
$logger.info "Update head in project #{project_name} to <#{new_head}>."
true
end
private
def create_hooks_to(dest_path)
"ln -s #{File.join(ROOT_PATH, 'hooks', 'update')} #{dest_path}/hooks/update"
end
end
|