blob: 3115868dfe3a51cd591502792264f95d6f0eadb5 (
plain)
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
|
require 'open3'
require 'fileutils'
require_relative 'gitlab_config'
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 'add-project'; add_project
when 'rm-project'; rm_project
when 'mv-project'; mv_project
when 'import-project'; import_project
when 'fork-project'; fork_project
else
puts 'not allowed'
false
end
end
protected
def add_project
FileUtils.mkdir_p(full_path, mode: 0770)
cmd = "cd #{full_path} && git init --bare && #{create_hooks_cmd}"
system(cmd)
end
def create_hooks_cmd
pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive')
up_hook_path = File.join(ROOT_PATH, 'hooks', 'update')
"ln -s #{pr_hook_path} #{full_path}/hooks/post-receive && ln -s #{up_hook_path} #{full_path}/hooks/update"
end
def rm_project
FileUtils.rm_rf(full_path)
end
# Import project via git clone --bare
# URL must be publicly clonable
def import_project
@source = ARGV.shift
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
return false unless new_path
new_full_path = File.join(repos_path, new_path)
# check if source repo exists
# and target repo does not exist
return false unless File.exists?(full_path)
return false if File.exists?(new_full_path)
FileUtils.mv(full_path, new_full_path)
end
def fork_project
new_namespace = ARGV.shift
return false unless new_namespace
namespaced_path = File.join(repos_path, new_namespace)
return false unless File.exists?(namespaced_path)
cmd = "cd #{namespaced_path} && git clone --bare #{@full_path}"
system(cmd)
end
end
|